blob: b04fb0feccd30bc326e9078de77fc7fa2b018413 [file] [log] [blame]
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9class.c: Vim9 script class support
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
24/*
Bram Moolenaard505d172022-12-18 21:42:55 +000025 * Parse a member declaration, both object and class member.
26 * Returns OK or FAIL. When OK then "varname_end" is set to just after the
27 * variable name and "type_ret" is set to the decleared or detected type.
28 * "init_expr" is set to the initialisation expression (allocated), if there is
Bram Moolenaar554d0312023-01-05 19:59:18 +000029 * one. For an interface "init_expr" is NULL.
Bram Moolenaard505d172022-12-18 21:42:55 +000030 */
31 static int
32parse_member(
33 exarg_T *eap,
34 char_u *line,
35 char_u *varname,
36 int has_public, // TRUE if "public" seen before "varname"
37 char_u **varname_end,
38 garray_T *type_list,
39 type_T **type_ret,
40 char_u **init_expr)
41{
42 *varname_end = to_name_end(varname, FALSE);
43 if (*varname == '_' && has_public)
44 {
45 semsg(_(e_public_member_name_cannot_start_with_underscore_str), line);
46 return FAIL;
47 }
48
49 char_u *colon = skipwhite(*varname_end);
50 char_u *type_arg = colon;
51 type_T *type = NULL;
52 if (*colon == ':')
53 {
54 if (VIM_ISWHITE(**varname_end))
55 {
56 semsg(_(e_no_white_space_allowed_before_colon_str), varname);
57 return FAIL;
58 }
59 if (!VIM_ISWHITE(colon[1]))
60 {
61 semsg(_(e_white_space_required_after_str_str), ":", varname);
62 return FAIL;
63 }
64 type_arg = skipwhite(colon + 1);
65 type = parse_type(&type_arg, type_list, TRUE);
66 if (type == NULL)
67 return FAIL;
68 }
69
70 char_u *expr_start = skipwhite(type_arg);
71 char_u *expr_end = expr_start;
72 if (type == NULL && *expr_start != '=')
73 {
74 emsg(_(e_type_or_initialization_required));
75 return FAIL;
76 }
77
78 if (*expr_start == '=')
79 {
80 if (!VIM_ISWHITE(expr_start[-1]) || !VIM_ISWHITE(expr_start[1]))
81 {
82 semsg(_(e_white_space_required_before_and_after_str_at_str),
83 "=", type_arg);
84 return FAIL;
85 }
86 expr_start = skipwhite(expr_start + 1);
87
88 expr_end = expr_start;
89 evalarg_T evalarg;
90 fill_evalarg_from_eap(&evalarg, eap, FALSE);
91 skip_expr(&expr_end, NULL);
92
93 if (type == NULL)
94 {
95 // No type specified, use the type of the initializer.
96 typval_T tv;
97 tv.v_type = VAR_UNKNOWN;
98 char_u *expr = expr_start;
99 int res = eval0(expr, &tv, eap, &evalarg);
100
101 if (res == OK)
Bram Moolenaare83c1332023-01-02 21:04:04 +0000102 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000103 type = typval2type(&tv, get_copyID(), type_list,
104 TVTT_DO_MEMBER);
Bram Moolenaare83c1332023-01-02 21:04:04 +0000105 clear_tv(&tv);
106 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000107 if (type == NULL)
108 {
109 semsg(_(e_cannot_get_object_member_type_from_initializer_str),
110 expr_start);
111 clear_evalarg(&evalarg, NULL);
112 return FAIL;
113 }
114 }
115 clear_evalarg(&evalarg, NULL);
116 }
117 if (!valid_declaration_type(type))
118 return FAIL;
119
120 *type_ret = type;
121 if (expr_end > expr_start)
Bram Moolenaar554d0312023-01-05 19:59:18 +0000122 {
123 if (init_expr == NULL)
124 {
125 emsg(_(e_cannot_initialize_member_in_interface));
126 return FAIL;
127 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000128 *init_expr = vim_strnsave(expr_start, expr_end - expr_start);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000129 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000130 return OK;
131}
132
133/*
134 * Add a member to an object or a class.
135 * Returns OK when successful, "init_expr" will be consumed then.
136 * Returns FAIL otherwise, caller might need to free "init_expr".
137 */
138 static int
139add_member(
140 garray_T *gap,
141 char_u *varname,
142 char_u *varname_end,
143 int has_public,
144 type_T *type,
145 char_u *init_expr)
146{
147 if (ga_grow(gap, 1) == FAIL)
148 return FAIL;
149 ocmember_T *m = ((ocmember_T *)gap->ga_data) + gap->ga_len;
150 m->ocm_name = vim_strnsave(varname, varname_end - varname);
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +0000151 m->ocm_access = has_public ? VIM_ACCESS_ALL
152 : *varname == '_' ? VIM_ACCESS_PRIVATE : VIM_ACCESS_READ;
Bram Moolenaard505d172022-12-18 21:42:55 +0000153 m->ocm_type = type;
154 if (init_expr != NULL)
155 m->ocm_init = init_expr;
156 ++gap->ga_len;
157 return OK;
158}
159
160/*
161 * Move the class or object members found while parsing a class into the class.
162 * "gap" contains the found members.
Bram Moolenaar83677162023-01-08 19:54:10 +0000163 * "parent_members" points to the members in the parent class (if any)
164 * "parent_count" is the number of members in the parent class
Bram Moolenaard505d172022-12-18 21:42:55 +0000165 * "members" will be set to the newly allocated array of members and
166 * "member_count" set to the number of members.
167 * Returns OK or FAIL.
168 */
169 static int
170add_members_to_class(
171 garray_T *gap,
Bram Moolenaar83677162023-01-08 19:54:10 +0000172 ocmember_T *parent_members,
173 int parent_count,
Bram Moolenaard505d172022-12-18 21:42:55 +0000174 ocmember_T **members,
175 int *member_count)
176{
Bram Moolenaar83677162023-01-08 19:54:10 +0000177 *member_count = parent_count + gap->ga_len;
178 *members = *member_count == 0 ? NULL
179 : ALLOC_MULT(ocmember_T, *member_count);
180 if (*member_count > 0 && *members == NULL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000181 return FAIL;
Bram Moolenaar83677162023-01-08 19:54:10 +0000182 for (int i = 0; i < parent_count; ++i)
183 {
184 // parent members need to be copied
Bram Moolenaarae3205a2023-01-15 20:49:00 +0000185 ocmember_T *m = *members + i;
186 *m = parent_members[i];
187 m->ocm_name = vim_strsave(m->ocm_name);
188 if (m->ocm_init != NULL)
189 m->ocm_init = vim_strsave(m->ocm_init);
Bram Moolenaar83677162023-01-08 19:54:10 +0000190 }
Bram Moolenaar8efdcee2022-12-19 12:18:09 +0000191 if (gap->ga_len > 0)
Bram Moolenaar83677162023-01-08 19:54:10 +0000192 // new members are moved
193 mch_memmove(*members + parent_count,
194 gap->ga_data, sizeof(ocmember_T) * gap->ga_len);
Bram Moolenaard505d172022-12-18 21:42:55 +0000195 VIM_CLEAR(gap->ga_data);
196 return OK;
197}
198
199/*
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000200 * Convert a member index "idx" of interface "itf" to the member index of class
201 * "cl" implementing that interface.
202 */
203 int
Bram Moolenaard0200c82023-01-28 15:19:40 +0000204object_index_from_itf_index(class_T *itf, int is_method, int idx, class_T *cl)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000205{
Bram Moolenaard0200c82023-01-28 15:19:40 +0000206 if (idx > (is_method ? itf->class_obj_method_count
207 : itf->class_obj_member_count))
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000208 {
209 siemsg("index %d out of range for interface %s", idx, itf->class_name);
210 return 0;
211 }
Yegappan Lakshmanan74cc13c2023-08-13 17:41:26 +0200212
213 // If "cl" is the interface or the class that is extended, then the method
214 // index can be used directly and there is no need to search for the method
215 // index in one of the child classes.
216 if (cl == itf)
217 return idx;
218
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000219 itf2class_T *i2c;
220 for (i2c = itf->class_itf2class; i2c != NULL; i2c = i2c->i2c_next)
Bram Moolenaard0200c82023-01-28 15:19:40 +0000221 if (i2c->i2c_class == cl && i2c->i2c_is_method == is_method)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000222 break;
223 if (i2c == NULL)
224 {
225 siemsg("class %s not found on interface %s",
226 cl->class_name, itf->class_name);
227 return 0;
228 }
229 int *table = (int *)(i2c + 1);
230 return table[idx];
231}
232
233/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000234 * Handle ":class" and ":abstract class" up to ":endclass".
Bram Moolenaar554d0312023-01-05 19:59:18 +0000235 * Handle ":interface" up to ":endinterface".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000236 */
237 void
238ex_class(exarg_T *eap)
239{
Bram Moolenaar83ae6152023-02-25 19:59:31 +0000240 int is_class = eap->cmdidx == CMD_class; // FALSE for :interface
241 long start_lnum = SOURCING_LNUM;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000242
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000243 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000244 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000245 if (is_abstract)
246 {
247 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
248 {
249 semsg(_(e_invalid_argument_str), arg);
250 return;
251 }
252 arg = skipwhite(arg + 5);
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000253 is_class = TRUE;
254 }
255
256 if (!current_script_is_vim9()
257 || (cmdmod.cmod_flags & CMOD_LEGACY)
258 || !getline_equal(eap->getline, eap->cookie, getsourceline))
259 {
260 if (is_class)
261 emsg(_(e_class_can_only_be_defined_in_vim9_script));
262 else
263 emsg(_(e_interface_can_only_be_defined_in_vim9_script));
264 return;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000265 }
266
267 if (!ASCII_ISUPPER(*arg))
268 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000269 if (is_class)
270 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
271 else
272 semsg(_(e_interface_name_must_start_with_uppercase_letter_str),
273 arg);
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000274 return;
275 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000276 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
277 if (!IS_WHITE_OR_NUL(*name_end))
278 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000279 semsg(_(e_white_space_required_after_name_str), arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000280 return;
281 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000282 char_u *name_start = arg;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000283
Bram Moolenaara86655a2023-01-12 17:06:27 +0000284 // "export class" gets used when creating the class, don't use "is_export"
285 // for the items inside the class.
286 int class_export = is_export;
287 is_export = FALSE;
288
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000289 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000290 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000291
Bram Moolenaar83677162023-01-08 19:54:10 +0000292 // Name for "extends BaseClass"
293 char_u *extends = NULL;
294
Bram Moolenaar94674f22023-01-06 18:42:20 +0000295 // Names for "implements SomeInterface"
296 garray_T ga_impl;
297 ga_init2(&ga_impl, sizeof(char_u *), 5);
298
299 arg = skipwhite(name_end);
300 while (*arg != NUL && *arg != '#' && *arg != '\n')
301 {
302 // TODO:
Bram Moolenaar94674f22023-01-06 18:42:20 +0000303 // specifies SomeInterface
Bram Moolenaar83677162023-01-08 19:54:10 +0000304 if (STRNCMP(arg, "extends", 7) == 0 && IS_WHITE_OR_NUL(arg[7]))
305 {
306 if (extends != NULL)
307 {
308 emsg(_(e_duplicate_extends));
309 goto early_ret;
310 }
311 arg = skipwhite(arg + 7);
312 char_u *end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
313 if (!IS_WHITE_OR_NUL(*end))
314 {
315 semsg(_(e_white_space_required_after_name_str), arg);
316 goto early_ret;
317 }
318 extends = vim_strnsave(arg, end - arg);
319 if (extends == NULL)
320 goto early_ret;
321
322 arg = skipwhite(end + 1);
323 }
324 else if (STRNCMP(arg, "implements", 10) == 0
325 && IS_WHITE_OR_NUL(arg[10]))
Bram Moolenaar94674f22023-01-06 18:42:20 +0000326 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000327 if (ga_impl.ga_len > 0)
328 {
329 emsg(_(e_duplicate_implements));
330 goto early_ret;
331 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000332 arg = skipwhite(arg + 10);
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000333
334 for (;;)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000335 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000336 char_u *impl_end = find_name_end(arg, NULL, NULL,
337 FNE_CHECK_START);
338 if (!IS_WHITE_OR_NUL(*impl_end) && *impl_end != ',')
339 {
340 semsg(_(e_white_space_required_after_name_str), arg);
341 goto early_ret;
342 }
343 char_u *iname = vim_strnsave(arg, impl_end - arg);
344 if (iname == NULL)
345 goto early_ret;
346 for (int i = 0; i < ga_impl.ga_len; ++i)
347 if (STRCMP(((char_u **)ga_impl.ga_data)[i], iname) == 0)
348 {
349 semsg(_(e_duplicate_interface_after_implements_str),
350 iname);
351 vim_free(iname);
352 goto early_ret;
353 }
354 if (ga_add_string(&ga_impl, iname) == FAIL)
355 {
356 vim_free(iname);
357 goto early_ret;
358 }
359 if (*impl_end != ',')
360 {
361 arg = skipwhite(impl_end);
362 break;
363 }
364 arg = skipwhite(impl_end + 1);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000365 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000366 }
367 else
368 {
369 semsg(_(e_trailing_characters_str), arg);
370early_ret:
Bram Moolenaar83677162023-01-08 19:54:10 +0000371 vim_free(extends);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000372 ga_clear_strings(&ga_impl);
373 return;
374 }
375 }
376
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000377 garray_T type_list; // list of pointers to allocated types
378 ga_init2(&type_list, sizeof(type_T *), 10);
379
Bram Moolenaard505d172022-12-18 21:42:55 +0000380 // Growarray with class members declared in the class.
381 garray_T classmembers;
382 ga_init2(&classmembers, sizeof(ocmember_T), 10);
383
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000384 // Growarray with functions declared in the class.
385 garray_T classfunctions;
386 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +0000387
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000388 // Growarray with object members declared in the class.
389 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +0000390 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000391
392 // Growarray with object methods declared in the class.
393 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000394 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000395
396 /*
Bram Moolenaar554d0312023-01-05 19:59:18 +0000397 * Go over the body of the class/interface until "endclass" or
398 * "endinterface" is found.
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000399 */
400 char_u *theline = NULL;
401 int success = FALSE;
402 for (;;)
403 {
404 vim_free(theline);
405 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
406 if (theline == NULL)
407 break;
408 char_u *line = skipwhite(theline);
409
Bram Moolenaar418b5472022-12-20 13:38:22 +0000410 // Skip empty and comment lines.
411 if (*line == NUL)
412 continue;
413 if (*line == '#')
414 {
415 if (vim9_bad_comment(line))
416 break;
417 continue;
418 }
419
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000420 char_u *p = line;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000421 char *end_name = is_class ? "endclass" : "endinterface";
422 if (checkforcmd(&p, end_name, is_class ? 4 : 5))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000423 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000424 if (STRNCMP(line, end_name, is_class ? 8 : 12) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000425 semsg(_(e_command_cannot_be_shortened_str), line);
426 else if (*p == '|' || !ends_excmd2(line, p))
427 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +0000428 else
429 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000430 break;
431 }
Bram Moolenaar554d0312023-01-05 19:59:18 +0000432 char *wrong_name = is_class ? "endinterface" : "endclass";
433 if (checkforcmd(&p, wrong_name, is_class ? 5 : 4))
434 {
Bram Moolenaar657aea72023-01-27 13:16:19 +0000435 semsg(_(e_invalid_command_str_expected_str), line, end_name);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000436 break;
437 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000438
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000439 int has_public = FALSE;
440 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000441 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000442 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000443 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000444 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000445 break;
446 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000447 has_public = TRUE;
448 p = skipwhite(line + 6);
449
Bram Moolenaard505d172022-12-18 21:42:55 +0000450 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000451 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000452 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000453 break;
454 }
455 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000456
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000457 int has_static = FALSE;
458 char_u *ps = p;
459 if (checkforcmd(&p, "static", 4))
460 {
461 if (STRNCMP(ps, "static", 6) != 0)
462 {
463 semsg(_(e_command_cannot_be_shortened_str), ps);
464 break;
465 }
466 has_static = TRUE;
467 p = skipwhite(ps + 6);
468 }
469
Bram Moolenaard505d172022-12-18 21:42:55 +0000470 // object members (public, read access, private):
471 // "this._varname"
472 // "this.varname"
473 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000474 if (STRNCMP(p, "this", 4) == 0)
475 {
476 if (p[4] != '.' || !eval_isnamec1(p[5]))
477 {
478 semsg(_(e_invalid_object_member_declaration_str), p);
479 break;
480 }
481 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +0000482 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +0000483 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +0000484 char_u *init_expr = NULL;
485 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000486 &varname_end, &type_list, &type,
487 is_class ? &init_expr: NULL) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000488 break;
489 if (add_member(&objmembers, varname, varname_end,
490 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000491 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000492 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000493 break;
494 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000495 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000496
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000497 // constructors:
498 // def new()
499 // enddef
500 // def newOther()
501 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000502 // object methods and class functions:
503 // def SomeMethod()
504 // enddef
505 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000506 // enddef
507 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000508 // def <Tval> someMethod()
509 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000510 else if (checkforcmd(&p, "def", 3))
511 {
512 exarg_T ea;
513 garray_T lines_to_free;
514
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000515 // TODO: error for "public static def Func()"?
516
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000517 CLEAR_FIELD(ea);
518 ea.cmd = line;
519 ea.arg = p;
520 ea.cmdidx = CMD_def;
521 ea.getline = eap->getline;
522 ea.cookie = eap->cookie;
523
524 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000525 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free,
526 is_class ? CF_CLASS : CF_INTERFACE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000527 ga_clear_strings(&lines_to_free);
528
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000529 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000530 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000531 char_u *name = uf->uf_name;
532 int is_new = STRNCMP(name, "new", 3) == 0;
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000533 if (is_new && is_abstract)
534 {
535 emsg(_(e_cannot_define_new_function_in_abstract_class));
536 success = FALSE;
537 break;
538 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000539 garray_T *fgap = has_static || is_new
540 ? &classfunctions : &objmethods;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000541 // Check the name isn't used already.
542 for (int i = 0; i < fgap->ga_len; ++i)
543 {
544 char_u *n = ((ufunc_T **)fgap->ga_data)[i]->uf_name;
545 if (STRCMP(name, n) == 0)
546 {
547 semsg(_(e_duplicate_function_str), name);
548 break;
549 }
550 }
551
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000552 if (ga_grow(fgap, 1) == OK)
553 {
554 if (is_new)
555 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000556
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000557 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
558 ++fgap->ga_len;
559 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000560 }
561 }
562
563 // class members
564 else if (has_static)
565 {
566 // class members (public, read access, private):
567 // "static _varname"
568 // "static varname"
569 // "public static varname"
570 char_u *varname = p;
571 char_u *varname_end = NULL;
572 type_T *type = NULL;
573 char_u *init_expr = NULL;
574 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000575 &varname_end, &type_list, &type,
576 is_class ? &init_expr : NULL) == FAIL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000577 break;
578 if (add_member(&classmembers, varname, varname_end,
579 has_public, type, init_expr) == FAIL)
580 {
581 vim_free(init_expr);
582 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000583 }
584 }
585
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000586 else
587 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000588 if (is_class)
589 semsg(_(e_not_valid_command_in_class_str), line);
590 else
591 semsg(_(e_not_valid_command_in_interface_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000592 break;
593 }
594 }
595 vim_free(theline);
596
Bram Moolenaar83677162023-01-08 19:54:10 +0000597 class_T *extends_cl = NULL; // class from "extends" argument
598
599 /*
600 * Check a few things before defining the class.
601 */
602
603 // Check the "extends" class is valid.
604 if (success && extends != NULL)
605 {
606 typval_T tv;
607 tv.v_type = VAR_UNKNOWN;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000608 if (eval_variable_import(extends, &tv) == FAIL)
Bram Moolenaar83677162023-01-08 19:54:10 +0000609 {
610 semsg(_(e_class_name_not_found_str), extends);
611 success = FALSE;
612 }
613 else
614 {
615 if (tv.v_type != VAR_CLASS
616 || tv.vval.v_class == NULL
617 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) != 0)
618 {
619 semsg(_(e_cannot_extend_str), extends);
620 success = FALSE;
621 }
622 else
623 {
624 extends_cl = tv.vval.v_class;
625 ++extends_cl->class_refcount;
626 }
627 clear_tv(&tv);
628 }
629 }
630 VIM_CLEAR(extends);
631
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000632 class_T **intf_classes = NULL;
633
Bram Moolenaar83677162023-01-08 19:54:10 +0000634 // Check all "implements" entries are valid.
Bram Moolenaar94674f22023-01-06 18:42:20 +0000635 if (success && ga_impl.ga_len > 0)
636 {
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000637 intf_classes = ALLOC_CLEAR_MULT(class_T *, ga_impl.ga_len);
638
Bram Moolenaar94674f22023-01-06 18:42:20 +0000639 for (int i = 0; i < ga_impl.ga_len && success; ++i)
640 {
641 char_u *impl = ((char_u **)ga_impl.ga_data)[i];
642 typval_T tv;
643 tv.v_type = VAR_UNKNOWN;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000644 if (eval_variable_import(impl, &tv) == FAIL)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000645 {
646 semsg(_(e_interface_name_not_found_str), impl);
647 success = FALSE;
648 break;
649 }
650
651 if (tv.v_type != VAR_CLASS
652 || tv.vval.v_class == NULL
653 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0)
654 {
655 semsg(_(e_not_valid_interface_str), impl);
656 success = FALSE;
657 }
658
Bram Moolenaar94674f22023-01-06 18:42:20 +0000659 class_T *ifcl = tv.vval.v_class;
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000660 intf_classes[i] = ifcl;
661 ++ifcl->class_refcount;
662
663 // check the members of the interface match the members of the class
Bram Moolenaar94674f22023-01-06 18:42:20 +0000664 for (int loop = 1; loop <= 2 && success; ++loop)
665 {
666 // loop == 1: check class members
667 // loop == 2: check object members
668 int if_count = loop == 1 ? ifcl->class_class_member_count
669 : ifcl->class_obj_member_count;
670 if (if_count == 0)
671 continue;
672 ocmember_T *if_ms = loop == 1 ? ifcl->class_class_members
673 : ifcl->class_obj_members;
674 ocmember_T *cl_ms = (ocmember_T *)(loop == 1
675 ? classmembers.ga_data
676 : objmembers.ga_data);
677 int cl_count = loop == 1 ? classmembers.ga_len
678 : objmembers.ga_len;
679 for (int if_i = 0; if_i < if_count; ++if_i)
680 {
681 int cl_i;
682 for (cl_i = 0; cl_i < cl_count; ++cl_i)
683 {
684 ocmember_T *m = &cl_ms[cl_i];
685 if (STRCMP(if_ms[if_i].ocm_name, m->ocm_name) == 0)
686 {
687 // TODO: check type
688 break;
689 }
690 }
691 if (cl_i == cl_count)
692 {
693 semsg(_(e_member_str_of_interface_str_not_implemented),
694 if_ms[if_i].ocm_name, impl);
695 success = FALSE;
696 break;
697 }
698 }
699 }
700
701 // check the functions/methods of the interface match the
702 // functions/methods of the class
703 for (int loop = 1; loop <= 2 && success; ++loop)
704 {
705 // loop == 1: check class functions
706 // loop == 2: check object methods
707 int if_count = loop == 1 ? ifcl->class_class_function_count
708 : ifcl->class_obj_method_count;
709 if (if_count == 0)
710 continue;
711 ufunc_T **if_fp = loop == 1 ? ifcl->class_class_functions
712 : ifcl->class_obj_methods;
713 ufunc_T **cl_fp = (ufunc_T **)(loop == 1
714 ? classfunctions.ga_data
715 : objmethods.ga_data);
716 int cl_count = loop == 1 ? classfunctions.ga_len
717 : objmethods.ga_len;
718 for (int if_i = 0; if_i < if_count; ++if_i)
719 {
720 char_u *if_name = if_fp[if_i]->uf_name;
721 int cl_i;
722 for (cl_i = 0; cl_i < cl_count; ++cl_i)
723 {
724 char_u *cl_name = cl_fp[cl_i]->uf_name;
725 if (STRCMP(if_name, cl_name) == 0)
726 {
727 // TODO: check return and argument types
728 break;
729 }
730 }
731 if (cl_i == cl_count)
732 {
733 semsg(_(e_function_str_of_interface_str_not_implemented),
734 if_name, impl);
735 success = FALSE;
736 break;
737 }
738 }
739 }
740
741 clear_tv(&tv);
742 }
743 }
744
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000745 if (success)
746 {
h-east61378a12023-04-18 19:07:29 +0100747 // Check no function argument name is used as a class member.
748 // (Object members are always accessed with "this." prefix, so no need
749 // to check them.)
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000750 for (int loop = 1; loop <= 2 && success; ++loop)
751 {
752 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
753
754 for (int fi = 0; fi < gap->ga_len && success; ++fi)
755 {
756 ufunc_T *uf = ((ufunc_T **)gap->ga_data)[fi];
757
758 for (int i = 0; i < uf->uf_args.ga_len && success; ++i)
759 {
760 char_u *aname = ((char_u **)uf->uf_args.ga_data)[i];
h-east61378a12023-04-18 19:07:29 +0100761 garray_T *mgap = &classmembers;
762
763 for (int mi = 0; mi < mgap->ga_len; ++mi)
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000764 {
h-east61378a12023-04-18 19:07:29 +0100765 char_u *mname = ((ocmember_T *)mgap->ga_data + mi)
766 ->ocm_name;
767 if (STRCMP(aname, mname) == 0)
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000768 {
h-east61378a12023-04-18 19:07:29 +0100769 success = FALSE;
770
771 if (uf->uf_script_ctx.sc_sid > 0)
772 SOURCING_LNUM = uf->uf_script_ctx.sc_lnum;
773
774 semsg(_(e_argument_already_declared_in_class_str),
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000775 aname);
h-east61378a12023-04-18 19:07:29 +0100776 break;
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000777 }
778 }
779 }
780 }
781 }
782 }
783
784
Bram Moolenaareb533502022-12-14 15:06:11 +0000785 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000786 if (success)
787 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000788 // "endclass" encountered without failures: Create the class.
789
Bram Moolenaareb533502022-12-14 15:06:11 +0000790 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000791 if (cl == NULL)
792 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000793 if (!is_class)
794 cl->class_flags = CLASS_INTERFACE;
795
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000796 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +0000797 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +0000798 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000799 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000800
Bram Moolenaard0200c82023-01-28 15:19:40 +0000801 if (extends_cl != NULL)
802 {
803 cl->class_extends = extends_cl;
804 extends_cl->class_flags |= CLASS_EXTENDED;
805 }
Bram Moolenaar83677162023-01-08 19:54:10 +0000806
Bram Moolenaard505d172022-12-18 21:42:55 +0000807 // Add class and object members to "cl".
808 if (add_members_to_class(&classmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000809 extends_cl == NULL ? NULL
810 : extends_cl->class_class_members,
811 extends_cl == NULL ? 0
812 : extends_cl->class_class_member_count,
813 &cl->class_class_members,
814 &cl->class_class_member_count) == FAIL
Bram Moolenaard505d172022-12-18 21:42:55 +0000815 || add_members_to_class(&objmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000816 extends_cl == NULL ? NULL
817 : extends_cl->class_obj_members,
818 extends_cl == NULL ? 0
819 : extends_cl->class_obj_member_count,
820 &cl->class_obj_members,
821 &cl->class_obj_member_count) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000822 goto cleanup;
823
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000824 if (ga_impl.ga_len > 0)
825 {
826 // Move the "implements" names into the class.
827 cl->class_interface_count = ga_impl.ga_len;
828 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
829 if (cl->class_interfaces == NULL)
830 goto cleanup;
831 for (int i = 0; i < ga_impl.ga_len; ++i)
832 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
833 VIM_CLEAR(ga_impl.ga_data);
834 ga_impl.ga_len = 0;
835
Bram Moolenaard0200c82023-01-28 15:19:40 +0000836 cl->class_interfaces_cl = intf_classes;
837 intf_classes = NULL;
838 }
839
840 if (cl->class_interface_count > 0 || extends_cl != NULL)
841 {
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000842 // For each interface add a lookuptable for the member index on the
843 // interface to the member index in this class.
Bram Moolenaard0200c82023-01-28 15:19:40 +0000844 // And a lookuptable for the object method index on the interface
845 // to the object method index in this class.
846 // Also do this for the extended class, if any.
847 for (int i = 0; i <= cl->class_interface_count; ++i)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000848 {
Bram Moolenaard0200c82023-01-28 15:19:40 +0000849 class_T *ifcl = i < cl->class_interface_count
850 ? cl->class_interfaces_cl[i]
851 : extends_cl;
852 if (ifcl == NULL)
853 continue;
854
855 // Table for members.
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000856 itf2class_T *if2cl = alloc_clear(sizeof(itf2class_T)
857 + ifcl->class_obj_member_count * sizeof(int));
858 if (if2cl == NULL)
859 goto cleanup;
860 if2cl->i2c_next = ifcl->class_itf2class;
861 ifcl->class_itf2class = if2cl;
862 if2cl->i2c_class = cl;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000863 if2cl->i2c_is_method = FALSE;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000864
865 for (int if_i = 0; if_i < ifcl->class_obj_member_count; ++if_i)
Bram Moolenaard0200c82023-01-28 15:19:40 +0000866 for (int cl_i = 0; cl_i < cl->class_obj_member_count;
867 ++cl_i)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000868 {
869 if (STRCMP(ifcl->class_obj_members[if_i].ocm_name,
Bram Moolenaard0200c82023-01-28 15:19:40 +0000870 cl->class_obj_members[cl_i].ocm_name) == 0)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000871 {
872 int *table = (int *)(if2cl + 1);
873 table[if_i] = cl_i;
874 break;
875 }
876 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000877
Bram Moolenaard0200c82023-01-28 15:19:40 +0000878 // Table for methods.
879 if2cl = alloc_clear(sizeof(itf2class_T)
880 + ifcl->class_obj_method_count * sizeof(int));
881 if (if2cl == NULL)
882 goto cleanup;
883 if2cl->i2c_next = ifcl->class_itf2class;
884 ifcl->class_itf2class = if2cl;
885 if2cl->i2c_class = cl;
886 if2cl->i2c_is_method = TRUE;
887
888 for (int if_i = 0; if_i < ifcl->class_obj_method_count; ++if_i)
889 {
890 int done = FALSE;
891 for (int cl_i = 0; cl_i < objmethods.ga_len; ++cl_i)
892 {
893 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
894 ((ufunc_T **)objmethods.ga_data)[cl_i]->uf_name)
895 == 0)
896 {
897 int *table = (int *)(if2cl + 1);
898 table[if_i] = cl_i;
899 done = TRUE;
900 break;
901 }
902 }
903
904 if (!done && extends_cl != NULL)
905 {
906 for (int cl_i = 0;
Yegappan Lakshmanan57a02cc2023-08-13 10:19:38 +0200907 cl_i < extends_cl->class_obj_method_count; ++cl_i)
Bram Moolenaard0200c82023-01-28 15:19:40 +0000908 {
909 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
910 extends_cl->class_obj_methods[cl_i]->uf_name)
911 == 0)
912 {
913 int *table = (int *)(if2cl + 1);
Yegappan Lakshmanana456b122023-08-16 20:14:37 +0200914 table[if_i] = objmethods.ga_len + cl_i;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000915 break;
916 }
917 }
918 }
919 }
920 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000921 }
922
Bram Moolenaar554d0312023-01-05 19:59:18 +0000923 if (is_class && cl->class_class_member_count > 0)
Bram Moolenaard505d172022-12-18 21:42:55 +0000924 {
925 // Allocate a typval for each class member and initialize it.
926 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
927 cl->class_class_member_count);
928 if (cl->class_members_tv != NULL)
929 for (int i = 0; i < cl->class_class_member_count; ++i)
930 {
931 ocmember_T *m = &cl->class_class_members[i];
932 typval_T *tv = &cl->class_members_tv[i];
933 if (m->ocm_init != NULL)
934 {
935 typval_T *etv = eval_expr(m->ocm_init, eap);
936 if (etv != NULL)
937 {
938 *tv = *etv;
939 vim_free(etv);
940 }
941 }
942 else
943 {
944 // TODO: proper default value
945 tv->v_type = m->ocm_type->tt_type;
946 tv->vval.v_string = NULL;
947 }
948 }
949 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000950
951 int have_new = FALSE;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000952 for (int i = 0; i < classfunctions.ga_len; ++i)
953 if (STRCMP(((ufunc_T **)classfunctions.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000954 "new") == 0)
955 {
956 have_new = TRUE;
957 break;
958 }
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000959 if (is_class && !is_abstract && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000960 {
961 // No new() method was defined, add the default constructor.
962 garray_T fga;
963 ga_init2(&fga, 1, 1000);
964 ga_concat(&fga, (char_u *)"new(");
965 for (int i = 0; i < cl->class_obj_member_count; ++i)
966 {
967 if (i > 0)
968 ga_concat(&fga, (char_u *)", ");
969 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000970 ocmember_T *m = cl->class_obj_members + i;
971 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000972 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000973 }
974 ga_concat(&fga, (char_u *)")\nenddef\n");
975 ga_append(&fga, NUL);
976
977 exarg_T fea;
978 CLEAR_FIELD(fea);
979 fea.cmdidx = CMD_def;
980 fea.cmd = fea.arg = fga.ga_data;
981
982 garray_T lines_to_free;
983 ga_init2(&lines_to_free, sizeof(char_u *), 50);
984
Bram Moolenaar2c011312023-01-07 10:51:30 +0000985 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000986
987 ga_clear_strings(&lines_to_free);
988 vim_free(fga.ga_data);
989
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000990 if (nf != NULL && ga_grow(&classfunctions, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000991 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000992 ((ufunc_T **)classfunctions.ga_data)[classfunctions.ga_len]
993 = nf;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000994 ++classfunctions.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000995
996 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000997 nf->uf_ret_type = get_type_ptr(&type_list);
998 if (nf->uf_ret_type != NULL)
999 {
1000 nf->uf_ret_type->tt_type = VAR_OBJECT;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001001 nf->uf_ret_type->tt_class = cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001002 nf->uf_ret_type->tt_argcount = 0;
1003 nf->uf_ret_type->tt_args = NULL;
1004 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001005 }
1006 }
1007
Bram Moolenaar58b40092023-01-11 15:59:05 +00001008 // Move all the functions into the created class.
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001009 // loop 1: class functions, loop 2: object methods
1010 for (int loop = 1; loop <= 2; ++loop)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001011 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001012 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
1013 int *fcount = loop == 1 ? &cl->class_class_function_count
1014 : &cl->class_obj_method_count;
1015 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
1016 : &cl->class_obj_methods;
1017
Bram Moolenaar83677162023-01-08 19:54:10 +00001018 int parent_count = 0;
1019 if (extends_cl != NULL)
1020 // Include functions from the parent.
1021 parent_count = loop == 1
1022 ? extends_cl->class_class_function_count
1023 : extends_cl->class_obj_method_count;
1024
1025 *fcount = parent_count + gap->ga_len;
1026 if (*fcount == 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001027 {
1028 *fup = NULL;
1029 continue;
1030 }
Bram Moolenaar83677162023-01-08 19:54:10 +00001031 *fup = ALLOC_MULT(ufunc_T *, *fcount);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001032 if (*fup == NULL)
1033 goto cleanup;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001034
Ernie Rael114ec812023-06-04 18:11:35 +01001035 if (gap->ga_len != 0)
1036 mch_memmove(*fup, gap->ga_data,
1037 sizeof(ufunc_T *) * gap->ga_len);
Bram Moolenaar58b40092023-01-11 15:59:05 +00001038 vim_free(gap->ga_data);
1039 if (loop == 1)
1040 cl->class_class_function_count_child = gap->ga_len;
1041 else
1042 cl->class_obj_method_count_child = gap->ga_len;
1043
Bram Moolenaar83677162023-01-08 19:54:10 +00001044 int skipped = 0;
1045 for (int i = 0; i < parent_count; ++i)
1046 {
1047 // Copy functions from the parent. Can't use the same
1048 // function, because "uf_class" is different and compilation
1049 // will have a different result.
Bram Moolenaar58b40092023-01-11 15:59:05 +00001050 // Put them after the functions in the current class, object
1051 // methods may be overruled, then "super.Method()" is used to
1052 // find a method from the parent.
Bram Moolenaar83677162023-01-08 19:54:10 +00001053 // Skip "new" functions. TODO: not all of them.
1054 if (loop == 1 && STRNCMP(
1055 extends_cl->class_class_functions[i]->uf_name,
1056 "new", 3) == 0)
1057 ++skipped;
1058 else
Bram Moolenaar58b40092023-01-11 15:59:05 +00001059 {
1060 ufunc_T *pf = (loop == 1
Bram Moolenaar83677162023-01-08 19:54:10 +00001061 ? extends_cl->class_class_functions
Bram Moolenaar58b40092023-01-11 15:59:05 +00001062 : extends_cl->class_obj_methods)[i];
1063 (*fup)[gap->ga_len + i - skipped] = copy_function(pf);
1064
1065 // If the child class overrides a function from the parent
1066 // the signature must be equal.
1067 char_u *pname = pf->uf_name;
1068 for (int ci = 0; ci < gap->ga_len; ++ci)
1069 {
1070 ufunc_T *cf = (*fup)[ci];
1071 char_u *cname = cf->uf_name;
1072 if (STRCMP(pname, cname) == 0)
1073 {
1074 where_T where = WHERE_INIT;
1075 where.wt_func_name = (char *)pname;
1076 (void)check_type(pf->uf_func_type, cf->uf_func_type,
1077 TRUE, where);
1078 }
1079 }
1080 }
Bram Moolenaar83677162023-01-08 19:54:10 +00001081 }
1082
Bram Moolenaar83677162023-01-08 19:54:10 +00001083 *fcount -= skipped;
1084
1085 // Set the class pointer on all the functions and object methods.
1086 for (int i = 0; i < *fcount; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001087 {
1088 ufunc_T *fp = (*fup)[i];
1089 fp->uf_class = cl;
1090 if (loop == 2)
1091 fp->uf_flags |= FC_OBJECT;
1092 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001093 }
1094
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001095 cl->class_type.tt_type = VAR_CLASS;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001096 cl->class_type.tt_class = cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001097 cl->class_object_type.tt_type = VAR_OBJECT;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001098 cl->class_object_type.tt_class = cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001099 cl->class_type_list = type_list;
1100
1101 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +00001102 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001103
1104 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001105 // TODO: handle other context, e.g. in a function
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001106 typval_T tv;
1107 tv.v_type = VAR_CLASS;
1108 tv.vval.v_class = cl;
Bram Moolenaara86655a2023-01-12 17:06:27 +00001109 is_export = class_export;
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001110 SOURCING_LNUM = start_lnum;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001111 set_var_const(cl->class_name, current_sctx.sc_sid,
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001112 NULL, &tv, FALSE, 0, 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001113 return;
1114 }
1115
1116cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +00001117 if (cl != NULL)
1118 {
1119 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001120 vim_free(cl->class_class_functions);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001121 if (cl->class_interfaces != NULL)
1122 {
1123 for (int i = 0; i < cl->class_interface_count; ++i)
1124 vim_free(cl->class_interfaces[i]);
1125 vim_free(cl->class_interfaces);
1126 }
1127 if (cl->class_interfaces_cl != NULL)
1128 {
1129 for (int i = 0; i < cl->class_interface_count; ++i)
1130 class_unref(cl->class_interfaces_cl[i]);
1131 vim_free(cl->class_interfaces_cl);
1132 }
Bram Moolenaareb533502022-12-14 15:06:11 +00001133 vim_free(cl->class_obj_members);
1134 vim_free(cl->class_obj_methods);
1135 vim_free(cl);
1136 }
1137
Bram Moolenaar83677162023-01-08 19:54:10 +00001138 vim_free(extends);
1139 class_unref(extends_cl);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001140
1141 if (intf_classes != NULL)
1142 {
1143 for (int i = 0; i < ga_impl.ga_len; ++i)
1144 class_unref(intf_classes[i]);
1145 vim_free(intf_classes);
1146 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001147 ga_clear_strings(&ga_impl);
1148
Bram Moolenaard505d172022-12-18 21:42:55 +00001149 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001150 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001151 garray_T *gap = round == 1 ? &classmembers : &objmembers;
1152 if (gap->ga_len == 0 || gap->ga_data == NULL)
1153 continue;
1154
1155 for (int i = 0; i < gap->ga_len; ++i)
1156 {
1157 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
1158 vim_free(m->ocm_name);
1159 vim_free(m->ocm_init);
1160 }
1161 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001162 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001163
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001164 for (int i = 0; i < objmethods.ga_len; ++i)
1165 {
1166 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
1167 func_clear_free(uf, FALSE);
1168 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001169 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001170
1171 for (int i = 0; i < classfunctions.ga_len; ++i)
1172 {
1173 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
1174 func_clear_free(uf, FALSE);
1175 }
1176 ga_clear(&classfunctions);
1177
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001178 clear_type_list(&type_list);
1179}
1180
1181/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001182 * Find member "name" in class "cl", set "member_idx" to the member index and
1183 * return its type.
1184 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001185 */
1186 type_T *
1187class_member_type(
1188 class_T *cl,
1189 char_u *name,
1190 char_u *name_end,
1191 int *member_idx)
1192{
1193 *member_idx = -1; // not found (yet)
1194 size_t len = name_end - name;
1195
1196 for (int i = 0; i < cl->class_obj_member_count; ++i)
1197 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001198 ocmember_T *m = cl->class_obj_members + i;
1199 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001200 {
1201 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001202 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001203 }
1204 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001205
1206 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001207 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001208}
1209
1210/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001211 * Handle ":enum" up to ":endenum".
1212 */
1213 void
1214ex_enum(exarg_T *eap UNUSED)
1215{
1216 // TODO
1217}
1218
1219/*
1220 * Handle ":type".
1221 */
1222 void
1223ex_type(exarg_T *eap UNUSED)
1224{
1225 // TODO
1226}
1227
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001228/*
1229 * Evaluate what comes after a class:
1230 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001231 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001232 * - class constructor: SomeClass.new()
1233 * - object member: someObject.varname
1234 * - object method: someObject.SomeMethod()
1235 *
1236 * "*arg" points to the '.'.
1237 * "*arg" is advanced to after the member name or method call.
1238 *
1239 * Returns FAIL or OK.
1240 */
1241 int
1242class_object_index(
1243 char_u **arg,
1244 typval_T *rettv,
1245 evalarg_T *evalarg,
1246 int verbose UNUSED) // give error messages
1247{
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001248 if (VIM_ISWHITE((*arg)[1]))
1249 {
1250 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
1251 return FAIL;
1252 }
1253
1254 ++*arg;
1255 char_u *name = *arg;
1256 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1257 if (name_end == name)
1258 return FAIL;
1259 size_t len = name_end - name;
1260
Bram Moolenaar552bdca2023-02-17 21:08:50 +00001261 class_T *cl;
1262 if (rettv->v_type == VAR_CLASS)
1263 cl = rettv->vval.v_class;
1264 else // VAR_OBJECT
1265 {
1266 if (rettv->vval.v_object == NULL)
1267 {
1268 emsg(_(e_using_null_object));
1269 return FAIL;
1270 }
1271 cl = rettv->vval.v_object->obj_class;
1272 }
1273
Bram Moolenaard13dd302023-03-11 20:56:35 +00001274 if (cl == NULL)
1275 {
1276 emsg(_(e_incomplete_type));
1277 return FAIL;
1278 }
1279
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001280 if (*name_end == '(')
1281 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001282 int on_class = rettv->v_type == VAR_CLASS;
1283 int count = on_class ? cl->class_class_function_count
1284 : cl->class_obj_method_count;
1285 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001286 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001287 ufunc_T *fp = on_class ? cl->class_class_functions[i]
1288 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +00001289 // Use a separate pointer to avoid that ASAN complains about
1290 // uf_name[] only being 4 characters.
1291 char_u *ufname = (char_u *)fp->uf_name;
1292 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001293 {
1294 typval_T argvars[MAX_FUNC_ARGS + 1];
1295 int argcount = 0;
1296
1297 char_u *argp = name_end;
1298 int ret = get_func_arguments(&argp, evalarg, 0,
1299 argvars, &argcount);
1300 if (ret == FAIL)
1301 return FAIL;
1302
1303 funcexe_T funcexe;
1304 CLEAR_FIELD(funcexe);
1305 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001306 if (rettv->v_type == VAR_OBJECT)
1307 {
1308 funcexe.fe_object = rettv->vval.v_object;
1309 ++funcexe.fe_object->obj_refcount;
1310 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001311
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001312 // Clear the class or object after calling the function, in
1313 // case the refcount is one.
1314 typval_T tv_tofree = *rettv;
1315 rettv->v_type = VAR_UNKNOWN;
1316
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001317 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001318 int error = call_user_func_check(fp, argcount, argvars,
1319 rettv, &funcexe, NULL);
1320
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001321 // Clear the previous rettv and the arguments.
1322 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001323 for (int idx = 0; idx < argcount; ++idx)
1324 clear_tv(&argvars[idx]);
1325
1326 if (error != FCERR_NONE)
1327 {
1328 user_func_error(error, printable_func_name(fp),
1329 funcexe.fe_found_var);
1330 return FAIL;
1331 }
1332 *arg = argp;
1333 return OK;
1334 }
1335 }
1336
1337 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
1338 }
1339
1340 else if (rettv->v_type == VAR_OBJECT)
1341 {
1342 for (int i = 0; i < cl->class_obj_member_count; ++i)
1343 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001344 ocmember_T *m = &cl->class_obj_members[i];
1345 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001346 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001347 if (*name == '_')
1348 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001349 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001350 return FAIL;
1351 }
1352
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001353 // The object only contains a pointer to the class, the member
1354 // values array follows right after that.
1355 object_T *obj = rettv->vval.v_object;
1356 typval_T *tv = (typval_T *)(obj + 1) + i;
1357 copy_tv(tv, rettv);
1358 object_unref(obj);
1359
1360 *arg = name_end;
1361 return OK;
1362 }
1363 }
1364
1365 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
1366 }
1367
Bram Moolenaard505d172022-12-18 21:42:55 +00001368 else if (rettv->v_type == VAR_CLASS)
1369 {
1370 // class member
1371 for (int i = 0; i < cl->class_class_member_count; ++i)
1372 {
1373 ocmember_T *m = &cl->class_class_members[i];
1374 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
1375 {
1376 if (*name == '_')
1377 {
1378 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
1379 return FAIL;
1380 }
1381
1382 typval_T *tv = &cl->class_members_tv[i];
1383 copy_tv(tv, rettv);
1384 class_unref(cl);
1385
1386 *arg = name_end;
1387 return OK;
1388 }
1389 }
1390
1391 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
1392 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001393
1394 return FAIL;
1395}
1396
1397/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001398 * If "arg" points to a class or object method, return it.
1399 * Otherwise return NULL.
1400 */
1401 ufunc_T *
1402find_class_func(char_u **arg)
1403{
1404 char_u *name = *arg;
1405 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1406 if (name_end == name || *name_end != '.')
1407 return NULL;
1408
1409 size_t len = name_end - name;
1410 typval_T tv;
1411 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001412 if (eval_variable(name, (int)len,
1413 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001414 return NULL;
1415 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001416 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001417
1418 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1419 : tv.vval.v_object->obj_class;
1420 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001421 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001422 char_u *fname = name_end + 1;
1423 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1424 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001425 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001426 len = fname_end - fname;
1427
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001428 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1429 : cl->class_obj_method_count;
1430 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1431 : cl->class_obj_methods;
1432 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001433 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001434 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001435 // Use a separate pointer to avoid that ASAN complains about
1436 // uf_name[] only being 4 characters.
1437 char_u *ufname = (char_u *)fp->uf_name;
1438 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001439 {
1440 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001441 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001442 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001443 }
1444
Bram Moolenaareb533502022-12-14 15:06:11 +00001445fail_after_eval:
1446 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001447 return NULL;
1448}
1449
1450/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001451 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1452 * index in class.class_class_members[].
1453 * If "cl_ret" is not NULL set it to the class.
1454 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001455 */
1456 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001457class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001458{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001459 if (cctx == NULL || cctx->ctx_ufunc == NULL
1460 || cctx->ctx_ufunc->uf_class == NULL)
1461 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001462 class_T *cl = cctx->ctx_ufunc->uf_class;
1463
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001464 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001465 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001466 ocmember_T *m = &cl->class_class_members[i];
1467 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001468 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001469 if (cl_ret != NULL)
1470 *cl_ret = cl;
1471 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001472 }
1473 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001474 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001475}
1476
1477/*
Bram Moolenaar62a69232023-01-24 15:07:04 +00001478 * Return TRUE if current context "cctx_arg" is inside class "cl".
1479 * Return FALSE if not.
1480 */
1481 int
1482inside_class(cctx_T *cctx_arg, class_T *cl)
1483{
1484 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
1485 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class == cl)
1486 return TRUE;
1487 return FALSE;
1488}
1489
1490/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001491 * Make a copy of an object.
1492 */
1493 void
1494copy_object(typval_T *from, typval_T *to)
1495{
1496 *to = *from;
1497 if (to->vval.v_object != NULL)
1498 ++to->vval.v_object->obj_refcount;
1499}
1500
1501/*
1502 * Free an object.
1503 */
1504 static void
1505object_clear(object_T *obj)
1506{
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001507 // Avoid a recursive call, it can happen if "obj" has a circular reference.
1508 obj->obj_refcount = INT_MAX;
1509
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001510 class_T *cl = obj->obj_class;
1511
Jia-Ju Bai5b0889b2023-08-13 20:04:04 +02001512 if (!cl)
1513 return;
1514
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001515 // the member values are just after the object structure
1516 typval_T *tv = (typval_T *)(obj + 1);
1517 for (int i = 0; i < cl->class_obj_member_count; ++i)
1518 clear_tv(tv + i);
1519
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001520 // Remove from the list headed by "first_object".
1521 object_cleared(obj);
1522
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001523 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001524 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001525}
1526
1527/*
1528 * Unreference an object.
1529 */
1530 void
1531object_unref(object_T *obj)
1532{
1533 if (obj != NULL && --obj->obj_refcount <= 0)
1534 object_clear(obj);
1535}
1536
1537/*
1538 * Make a copy of a class.
1539 */
1540 void
1541copy_class(typval_T *from, typval_T *to)
1542{
1543 *to = *from;
1544 if (to->vval.v_class != NULL)
1545 ++to->vval.v_class->class_refcount;
1546}
1547
1548/*
1549 * Unreference a class. Free it when the reference count goes down to zero.
1550 */
1551 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001552class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001553{
Bram Moolenaard505d172022-12-18 21:42:55 +00001554 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001555 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001556 // Freeing what the class contains may recursively come back here.
1557 // Clear "class_name" first, if it is NULL the class does not need to
1558 // be freed.
1559 VIM_CLEAR(cl->class_name);
1560
Bram Moolenaar83677162023-01-08 19:54:10 +00001561 class_unref(cl->class_extends);
1562
Bram Moolenaar94674f22023-01-06 18:42:20 +00001563 for (int i = 0; i < cl->class_interface_count; ++i)
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001564 {
Bram Moolenaar94674f22023-01-06 18:42:20 +00001565 vim_free(((char_u **)cl->class_interfaces)[i]);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001566 if (cl->class_interfaces_cl[i] != NULL)
1567 class_unref(cl->class_interfaces_cl[i]);
1568 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001569 vim_free(cl->class_interfaces);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001570 vim_free(cl->class_interfaces_cl);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001571
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001572 itf2class_T *next;
1573 for (itf2class_T *i2c = cl->class_itf2class; i2c != NULL; i2c = next)
1574 {
1575 next = i2c->i2c_next;
1576 vim_free(i2c);
1577 }
1578
Bram Moolenaard505d172022-12-18 21:42:55 +00001579 for (int i = 0; i < cl->class_class_member_count; ++i)
1580 {
1581 ocmember_T *m = &cl->class_class_members[i];
1582 vim_free(m->ocm_name);
1583 vim_free(m->ocm_init);
1584 if (cl->class_members_tv != NULL)
1585 clear_tv(&cl->class_members_tv[i]);
1586 }
1587 vim_free(cl->class_class_members);
1588 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001589
1590 for (int i = 0; i < cl->class_obj_member_count; ++i)
1591 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001592 ocmember_T *m = &cl->class_obj_members[i];
1593 vim_free(m->ocm_name);
1594 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001595 }
1596 vim_free(cl->class_obj_members);
1597
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001598 for (int i = 0; i < cl->class_class_function_count; ++i)
1599 {
1600 ufunc_T *uf = cl->class_class_functions[i];
1601 func_clear_free(uf, FALSE);
1602 }
1603 vim_free(cl->class_class_functions);
1604
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001605 for (int i = 0; i < cl->class_obj_method_count; ++i)
1606 {
1607 ufunc_T *uf = cl->class_obj_methods[i];
1608 func_clear_free(uf, FALSE);
1609 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001610 vim_free(cl->class_obj_methods);
1611
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001612 clear_type_list(&cl->class_type_list);
1613
1614 vim_free(cl);
1615 }
1616}
1617
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001618static object_T *first_object = NULL;
1619
1620/*
1621 * Call this function when an object has been created. It will be added to the
1622 * list headed by "first_object".
1623 */
1624 void
1625object_created(object_T *obj)
1626{
1627 if (first_object != NULL)
1628 {
1629 obj->obj_next_used = first_object;
1630 first_object->obj_prev_used = obj;
1631 }
1632 first_object = obj;
1633}
1634
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001635static object_T *next_nonref_obj = NULL;
1636
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001637/*
1638 * Call this function when an object has been cleared and is about to be freed.
1639 * It is removed from the list headed by "first_object".
1640 */
1641 void
1642object_cleared(object_T *obj)
1643{
1644 if (obj->obj_next_used != NULL)
1645 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1646 if (obj->obj_prev_used != NULL)
1647 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1648 else if (first_object == obj)
1649 first_object = obj->obj_next_used;
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001650
1651 // update the next object to check if needed
1652 if (obj == next_nonref_obj)
1653 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001654}
1655
1656/*
1657 * Go through the list of all objects and free items without "copyID".
1658 */
1659 int
1660object_free_nonref(int copyID)
1661{
1662 int did_free = FALSE;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001663
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001664 for (object_T *obj = first_object; obj != NULL; obj = next_nonref_obj)
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001665 {
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001666 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001667 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1668 {
1669 // Free the object and items it contains.
1670 object_clear(obj);
1671 did_free = TRUE;
1672 }
1673 }
1674
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001675 next_nonref_obj = NULL;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001676 return did_free;
1677}
1678
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001679
1680#endif // FEAT_EVAL