blob: d64e35ec012437fde1f24d0c82ace1691afebc4b [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);
151 m->ocm_access = has_public ? ACCESS_ALL
152 : *varname == '_' ? ACCESS_PRIVATE : ACCESS_READ;
153 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
204object_index_from_itf_index(class_T *itf, int idx, class_T *cl)
205{
206 if (idx > itf->class_obj_member_count)
207 {
208 siemsg("index %d out of range for interface %s", idx, itf->class_name);
209 return 0;
210 }
211 itf2class_T *i2c;
212 for (i2c = itf->class_itf2class; i2c != NULL; i2c = i2c->i2c_next)
213 if (i2c->i2c_class == cl)
214 break;
215 if (i2c == NULL)
216 {
217 siemsg("class %s not found on interface %s",
218 cl->class_name, itf->class_name);
219 return 0;
220 }
221 int *table = (int *)(i2c + 1);
222 return table[idx];
223}
224
225/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000226 * Handle ":class" and ":abstract class" up to ":endclass".
Bram Moolenaar554d0312023-01-05 19:59:18 +0000227 * Handle ":interface" up to ":endinterface".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000228 */
229 void
230ex_class(exarg_T *eap)
231{
Bram Moolenaar554d0312023-01-05 19:59:18 +0000232 int is_class = eap->cmdidx == CMD_class; // FALSE for :interface
233
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000234 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000235 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000236 if (is_abstract)
237 {
238 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
239 {
240 semsg(_(e_invalid_argument_str), arg);
241 return;
242 }
243 arg = skipwhite(arg + 5);
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000244 is_class = TRUE;
245 }
246
247 if (!current_script_is_vim9()
248 || (cmdmod.cmod_flags & CMOD_LEGACY)
249 || !getline_equal(eap->getline, eap->cookie, getsourceline))
250 {
251 if (is_class)
252 emsg(_(e_class_can_only_be_defined_in_vim9_script));
253 else
254 emsg(_(e_interface_can_only_be_defined_in_vim9_script));
255 return;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000256 }
257
258 if (!ASCII_ISUPPER(*arg))
259 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000260 if (is_class)
261 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
262 else
263 semsg(_(e_interface_name_must_start_with_uppercase_letter_str),
264 arg);
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000265 return;
266 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000267 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
268 if (!IS_WHITE_OR_NUL(*name_end))
269 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000270 semsg(_(e_white_space_required_after_name_str), arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000271 return;
272 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000273 char_u *name_start = arg;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000274
Bram Moolenaara86655a2023-01-12 17:06:27 +0000275 // "export class" gets used when creating the class, don't use "is_export"
276 // for the items inside the class.
277 int class_export = is_export;
278 is_export = FALSE;
279
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000280 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000281 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000282
Bram Moolenaar83677162023-01-08 19:54:10 +0000283 // Name for "extends BaseClass"
284 char_u *extends = NULL;
285
Bram Moolenaar94674f22023-01-06 18:42:20 +0000286 // Names for "implements SomeInterface"
287 garray_T ga_impl;
288 ga_init2(&ga_impl, sizeof(char_u *), 5);
289
290 arg = skipwhite(name_end);
291 while (*arg != NUL && *arg != '#' && *arg != '\n')
292 {
293 // TODO:
Bram Moolenaar94674f22023-01-06 18:42:20 +0000294 // specifies SomeInterface
Bram Moolenaar83677162023-01-08 19:54:10 +0000295 if (STRNCMP(arg, "extends", 7) == 0 && IS_WHITE_OR_NUL(arg[7]))
296 {
297 if (extends != NULL)
298 {
299 emsg(_(e_duplicate_extends));
300 goto early_ret;
301 }
302 arg = skipwhite(arg + 7);
303 char_u *end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
304 if (!IS_WHITE_OR_NUL(*end))
305 {
306 semsg(_(e_white_space_required_after_name_str), arg);
307 goto early_ret;
308 }
309 extends = vim_strnsave(arg, end - arg);
310 if (extends == NULL)
311 goto early_ret;
312
313 arg = skipwhite(end + 1);
314 }
315 else if (STRNCMP(arg, "implements", 10) == 0
316 && IS_WHITE_OR_NUL(arg[10]))
Bram Moolenaar94674f22023-01-06 18:42:20 +0000317 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000318 if (ga_impl.ga_len > 0)
319 {
320 emsg(_(e_duplicate_implements));
321 goto early_ret;
322 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000323 arg = skipwhite(arg + 10);
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000324
325 for (;;)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000326 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000327 char_u *impl_end = find_name_end(arg, NULL, NULL,
328 FNE_CHECK_START);
329 if (!IS_WHITE_OR_NUL(*impl_end) && *impl_end != ',')
330 {
331 semsg(_(e_white_space_required_after_name_str), arg);
332 goto early_ret;
333 }
334 char_u *iname = vim_strnsave(arg, impl_end - arg);
335 if (iname == NULL)
336 goto early_ret;
337 for (int i = 0; i < ga_impl.ga_len; ++i)
338 if (STRCMP(((char_u **)ga_impl.ga_data)[i], iname) == 0)
339 {
340 semsg(_(e_duplicate_interface_after_implements_str),
341 iname);
342 vim_free(iname);
343 goto early_ret;
344 }
345 if (ga_add_string(&ga_impl, iname) == FAIL)
346 {
347 vim_free(iname);
348 goto early_ret;
349 }
350 if (*impl_end != ',')
351 {
352 arg = skipwhite(impl_end);
353 break;
354 }
355 arg = skipwhite(impl_end + 1);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000356 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000357 }
358 else
359 {
360 semsg(_(e_trailing_characters_str), arg);
361early_ret:
Bram Moolenaar83677162023-01-08 19:54:10 +0000362 vim_free(extends);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000363 ga_clear_strings(&ga_impl);
364 return;
365 }
366 }
367
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000368 garray_T type_list; // list of pointers to allocated types
369 ga_init2(&type_list, sizeof(type_T *), 10);
370
Bram Moolenaard505d172022-12-18 21:42:55 +0000371 // Growarray with class members declared in the class.
372 garray_T classmembers;
373 ga_init2(&classmembers, sizeof(ocmember_T), 10);
374
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000375 // Growarray with functions declared in the class.
376 garray_T classfunctions;
377 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +0000378
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000379 // Growarray with object members declared in the class.
380 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +0000381 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000382
383 // Growarray with object methods declared in the class.
384 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000385 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000386
387 /*
Bram Moolenaar554d0312023-01-05 19:59:18 +0000388 * Go over the body of the class/interface until "endclass" or
389 * "endinterface" is found.
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000390 */
391 char_u *theline = NULL;
392 int success = FALSE;
393 for (;;)
394 {
395 vim_free(theline);
396 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
397 if (theline == NULL)
398 break;
399 char_u *line = skipwhite(theline);
400
Bram Moolenaar418b5472022-12-20 13:38:22 +0000401 // Skip empty and comment lines.
402 if (*line == NUL)
403 continue;
404 if (*line == '#')
405 {
406 if (vim9_bad_comment(line))
407 break;
408 continue;
409 }
410
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000411 char_u *p = line;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000412 char *end_name = is_class ? "endclass" : "endinterface";
413 if (checkforcmd(&p, end_name, is_class ? 4 : 5))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000414 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000415 if (STRNCMP(line, end_name, is_class ? 8 : 12) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000416 semsg(_(e_command_cannot_be_shortened_str), line);
417 else if (*p == '|' || !ends_excmd2(line, p))
418 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +0000419 else
420 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000421 break;
422 }
Bram Moolenaar554d0312023-01-05 19:59:18 +0000423 char *wrong_name = is_class ? "endinterface" : "endclass";
424 if (checkforcmd(&p, wrong_name, is_class ? 5 : 4))
425 {
Bram Moolenaar657aea72023-01-27 13:16:19 +0000426 semsg(_(e_invalid_command_str_expected_str), line, end_name);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000427 break;
428 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000429
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000430 int has_public = FALSE;
431 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000432 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000433 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000434 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000435 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000436 break;
437 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000438 has_public = TRUE;
439 p = skipwhite(line + 6);
440
Bram Moolenaard505d172022-12-18 21:42:55 +0000441 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000442 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000443 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000444 break;
445 }
446 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000447
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000448 int has_static = FALSE;
449 char_u *ps = p;
450 if (checkforcmd(&p, "static", 4))
451 {
452 if (STRNCMP(ps, "static", 6) != 0)
453 {
454 semsg(_(e_command_cannot_be_shortened_str), ps);
455 break;
456 }
457 has_static = TRUE;
458 p = skipwhite(ps + 6);
459 }
460
Bram Moolenaard505d172022-12-18 21:42:55 +0000461 // object members (public, read access, private):
462 // "this._varname"
463 // "this.varname"
464 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000465 if (STRNCMP(p, "this", 4) == 0)
466 {
467 if (p[4] != '.' || !eval_isnamec1(p[5]))
468 {
469 semsg(_(e_invalid_object_member_declaration_str), p);
470 break;
471 }
472 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +0000473 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +0000474 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +0000475 char_u *init_expr = NULL;
476 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000477 &varname_end, &type_list, &type,
478 is_class ? &init_expr: NULL) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000479 break;
480 if (add_member(&objmembers, varname, varname_end,
481 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000482 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000483 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000484 break;
485 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000486 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000487
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000488 // constructors:
489 // def new()
490 // enddef
491 // def newOther()
492 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000493 // object methods and class functions:
494 // def SomeMethod()
495 // enddef
496 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000497 // enddef
498 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000499 // def <Tval> someMethod()
500 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000501 else if (checkforcmd(&p, "def", 3))
502 {
503 exarg_T ea;
504 garray_T lines_to_free;
505
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000506 // TODO: error for "public static def Func()"?
507
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000508 CLEAR_FIELD(ea);
509 ea.cmd = line;
510 ea.arg = p;
511 ea.cmdidx = CMD_def;
512 ea.getline = eap->getline;
513 ea.cookie = eap->cookie;
514
515 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000516 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free,
517 is_class ? CF_CLASS : CF_INTERFACE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000518 ga_clear_strings(&lines_to_free);
519
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000520 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000521 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000522 char_u *name = uf->uf_name;
523 int is_new = STRNCMP(name, "new", 3) == 0;
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000524 if (is_new && is_abstract)
525 {
526 emsg(_(e_cannot_define_new_function_in_abstract_class));
527 success = FALSE;
528 break;
529 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000530 garray_T *fgap = has_static || is_new
531 ? &classfunctions : &objmethods;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000532 // Check the name isn't used already.
533 for (int i = 0; i < fgap->ga_len; ++i)
534 {
535 char_u *n = ((ufunc_T **)fgap->ga_data)[i]->uf_name;
536 if (STRCMP(name, n) == 0)
537 {
538 semsg(_(e_duplicate_function_str), name);
539 break;
540 }
541 }
542
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000543 if (ga_grow(fgap, 1) == OK)
544 {
545 if (is_new)
546 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000547
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000548 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
549 ++fgap->ga_len;
550 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000551 }
552 }
553
554 // class members
555 else if (has_static)
556 {
557 // class members (public, read access, private):
558 // "static _varname"
559 // "static varname"
560 // "public static varname"
561 char_u *varname = p;
562 char_u *varname_end = NULL;
563 type_T *type = NULL;
564 char_u *init_expr = NULL;
565 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000566 &varname_end, &type_list, &type,
567 is_class ? &init_expr : NULL) == FAIL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000568 break;
569 if (add_member(&classmembers, varname, varname_end,
570 has_public, type, init_expr) == FAIL)
571 {
572 vim_free(init_expr);
573 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000574 }
575 }
576
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000577 else
578 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000579 if (is_class)
580 semsg(_(e_not_valid_command_in_class_str), line);
581 else
582 semsg(_(e_not_valid_command_in_interface_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000583 break;
584 }
585 }
586 vim_free(theline);
587
Bram Moolenaar83677162023-01-08 19:54:10 +0000588 class_T *extends_cl = NULL; // class from "extends" argument
589
590 /*
591 * Check a few things before defining the class.
592 */
593
594 // Check the "extends" class is valid.
595 if (success && extends != NULL)
596 {
597 typval_T tv;
598 tv.v_type = VAR_UNKNOWN;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000599 if (eval_variable_import(extends, &tv) == FAIL)
Bram Moolenaar83677162023-01-08 19:54:10 +0000600 {
601 semsg(_(e_class_name_not_found_str), extends);
602 success = FALSE;
603 }
604 else
605 {
606 if (tv.v_type != VAR_CLASS
607 || tv.vval.v_class == NULL
608 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) != 0)
609 {
610 semsg(_(e_cannot_extend_str), extends);
611 success = FALSE;
612 }
613 else
614 {
615 extends_cl = tv.vval.v_class;
616 ++extends_cl->class_refcount;
617 }
618 clear_tv(&tv);
619 }
620 }
621 VIM_CLEAR(extends);
622
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000623 class_T **intf_classes = NULL;
624
Bram Moolenaar83677162023-01-08 19:54:10 +0000625 // Check all "implements" entries are valid.
Bram Moolenaar94674f22023-01-06 18:42:20 +0000626 if (success && ga_impl.ga_len > 0)
627 {
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000628 intf_classes = ALLOC_CLEAR_MULT(class_T *, ga_impl.ga_len);
629
Bram Moolenaar94674f22023-01-06 18:42:20 +0000630 for (int i = 0; i < ga_impl.ga_len && success; ++i)
631 {
632 char_u *impl = ((char_u **)ga_impl.ga_data)[i];
633 typval_T tv;
634 tv.v_type = VAR_UNKNOWN;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000635 if (eval_variable_import(impl, &tv) == FAIL)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000636 {
637 semsg(_(e_interface_name_not_found_str), impl);
638 success = FALSE;
639 break;
640 }
641
642 if (tv.v_type != VAR_CLASS
643 || tv.vval.v_class == NULL
644 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0)
645 {
646 semsg(_(e_not_valid_interface_str), impl);
647 success = FALSE;
648 }
649
Bram Moolenaar94674f22023-01-06 18:42:20 +0000650 class_T *ifcl = tv.vval.v_class;
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000651 intf_classes[i] = ifcl;
652 ++ifcl->class_refcount;
653
654 // check the members of the interface match the members of the class
Bram Moolenaar94674f22023-01-06 18:42:20 +0000655 for (int loop = 1; loop <= 2 && success; ++loop)
656 {
657 // loop == 1: check class members
658 // loop == 2: check object members
659 int if_count = loop == 1 ? ifcl->class_class_member_count
660 : ifcl->class_obj_member_count;
661 if (if_count == 0)
662 continue;
663 ocmember_T *if_ms = loop == 1 ? ifcl->class_class_members
664 : ifcl->class_obj_members;
665 ocmember_T *cl_ms = (ocmember_T *)(loop == 1
666 ? classmembers.ga_data
667 : objmembers.ga_data);
668 int cl_count = loop == 1 ? classmembers.ga_len
669 : objmembers.ga_len;
670 for (int if_i = 0; if_i < if_count; ++if_i)
671 {
672 int cl_i;
673 for (cl_i = 0; cl_i < cl_count; ++cl_i)
674 {
675 ocmember_T *m = &cl_ms[cl_i];
676 if (STRCMP(if_ms[if_i].ocm_name, m->ocm_name) == 0)
677 {
678 // TODO: check type
679 break;
680 }
681 }
682 if (cl_i == cl_count)
683 {
684 semsg(_(e_member_str_of_interface_str_not_implemented),
685 if_ms[if_i].ocm_name, impl);
686 success = FALSE;
687 break;
688 }
689 }
690 }
691
692 // check the functions/methods of the interface match the
693 // functions/methods of the class
694 for (int loop = 1; loop <= 2 && success; ++loop)
695 {
696 // loop == 1: check class functions
697 // loop == 2: check object methods
698 int if_count = loop == 1 ? ifcl->class_class_function_count
699 : ifcl->class_obj_method_count;
700 if (if_count == 0)
701 continue;
702 ufunc_T **if_fp = loop == 1 ? ifcl->class_class_functions
703 : ifcl->class_obj_methods;
704 ufunc_T **cl_fp = (ufunc_T **)(loop == 1
705 ? classfunctions.ga_data
706 : objmethods.ga_data);
707 int cl_count = loop == 1 ? classfunctions.ga_len
708 : objmethods.ga_len;
709 for (int if_i = 0; if_i < if_count; ++if_i)
710 {
711 char_u *if_name = if_fp[if_i]->uf_name;
712 int cl_i;
713 for (cl_i = 0; cl_i < cl_count; ++cl_i)
714 {
715 char_u *cl_name = cl_fp[cl_i]->uf_name;
716 if (STRCMP(if_name, cl_name) == 0)
717 {
718 // TODO: check return and argument types
719 break;
720 }
721 }
722 if (cl_i == cl_count)
723 {
724 semsg(_(e_function_str_of_interface_str_not_implemented),
725 if_name, impl);
726 success = FALSE;
727 break;
728 }
729 }
730 }
731
732 clear_tv(&tv);
733 }
734 }
735
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000736 if (success)
737 {
738 // Check no function argument name is used as an object/class member.
739 for (int loop = 1; loop <= 2 && success; ++loop)
740 {
741 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
742
743 for (int fi = 0; fi < gap->ga_len && success; ++fi)
744 {
745 ufunc_T *uf = ((ufunc_T **)gap->ga_data)[fi];
746
747 for (int i = 0; i < uf->uf_args.ga_len && success; ++i)
748 {
749 char_u *aname = ((char_u **)uf->uf_args.ga_data)[i];
750 for (int il = 1; il <= 2 && success; ++il)
751 {
752 // For a "new()" function "this.member" arguments are
753 // OK. TODO: check for the "this." prefix.
Bram Moolenaarb40a2fb2023-01-13 19:18:38 +0000754 if (STRNCMP(uf->uf_name, "new", 3) == 0 && il == 2)
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000755 continue;
756 garray_T *mgap = il == 1 ? &classmembers : &objmembers;
757 for (int mi = 0; mi < mgap->ga_len; ++mi)
758 {
759 char_u *mname = ((ocmember_T *)mgap->ga_data
760 + mi)->ocm_name;
761 if (STRCMP(aname, mname) == 0)
762 {
763 success = FALSE;
764 semsg(_(e_argument_already_declared_in_class_str),
765 aname);
766 break;
767 }
768 }
769 }
770 }
771 }
772 }
773 }
774
775
Bram Moolenaareb533502022-12-14 15:06:11 +0000776 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000777 if (success)
778 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000779 // "endclass" encountered without failures: Create the class.
780
Bram Moolenaareb533502022-12-14 15:06:11 +0000781 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000782 if (cl == NULL)
783 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000784 if (!is_class)
785 cl->class_flags = CLASS_INTERFACE;
786
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000787 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +0000788 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +0000789 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000790 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000791
Bram Moolenaar83677162023-01-08 19:54:10 +0000792 cl->class_extends = extends_cl;
793
Bram Moolenaard505d172022-12-18 21:42:55 +0000794 // Add class and object members to "cl".
795 if (add_members_to_class(&classmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000796 extends_cl == NULL ? NULL
797 : extends_cl->class_class_members,
798 extends_cl == NULL ? 0
799 : extends_cl->class_class_member_count,
800 &cl->class_class_members,
801 &cl->class_class_member_count) == FAIL
Bram Moolenaard505d172022-12-18 21:42:55 +0000802 || add_members_to_class(&objmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000803 extends_cl == NULL ? NULL
804 : extends_cl->class_obj_members,
805 extends_cl == NULL ? 0
806 : extends_cl->class_obj_member_count,
807 &cl->class_obj_members,
808 &cl->class_obj_member_count) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000809 goto cleanup;
810
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000811 if (ga_impl.ga_len > 0)
812 {
813 // Move the "implements" names into the class.
814 cl->class_interface_count = ga_impl.ga_len;
815 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
816 if (cl->class_interfaces == NULL)
817 goto cleanup;
818 for (int i = 0; i < ga_impl.ga_len; ++i)
819 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
820 VIM_CLEAR(ga_impl.ga_data);
821 ga_impl.ga_len = 0;
822
823 // For each interface add a lookuptable for the member index on the
824 // interface to the member index in this class.
825 for (int i = 0; i < cl->class_interface_count; ++i)
826 {
827 class_T *ifcl = intf_classes[i];
828 itf2class_T *if2cl = alloc_clear(sizeof(itf2class_T)
829 + ifcl->class_obj_member_count * sizeof(int));
830 if (if2cl == NULL)
831 goto cleanup;
832 if2cl->i2c_next = ifcl->class_itf2class;
833 ifcl->class_itf2class = if2cl;
834 if2cl->i2c_class = cl;
835
836 for (int if_i = 0; if_i < ifcl->class_obj_member_count; ++if_i)
837 for (int cl_i = 0; cl_i < cl->class_obj_member_count; ++cl_i)
838 {
839 if (STRCMP(ifcl->class_obj_members[if_i].ocm_name,
840 cl->class_obj_members[cl_i].ocm_name) == 0)
841 {
842 int *table = (int *)(if2cl + 1);
843 table[if_i] = cl_i;
844 break;
845 }
846 }
847 }
848
849 cl->class_interfaces_cl = intf_classes;
850 intf_classes = NULL;
851 }
852
Bram Moolenaar554d0312023-01-05 19:59:18 +0000853 if (is_class && cl->class_class_member_count > 0)
Bram Moolenaard505d172022-12-18 21:42:55 +0000854 {
855 // Allocate a typval for each class member and initialize it.
856 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
857 cl->class_class_member_count);
858 if (cl->class_members_tv != NULL)
859 for (int i = 0; i < cl->class_class_member_count; ++i)
860 {
861 ocmember_T *m = &cl->class_class_members[i];
862 typval_T *tv = &cl->class_members_tv[i];
863 if (m->ocm_init != NULL)
864 {
865 typval_T *etv = eval_expr(m->ocm_init, eap);
866 if (etv != NULL)
867 {
868 *tv = *etv;
869 vim_free(etv);
870 }
871 }
872 else
873 {
874 // TODO: proper default value
875 tv->v_type = m->ocm_type->tt_type;
876 tv->vval.v_string = NULL;
877 }
878 }
879 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000880
881 int have_new = FALSE;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000882 for (int i = 0; i < classfunctions.ga_len; ++i)
883 if (STRCMP(((ufunc_T **)classfunctions.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000884 "new") == 0)
885 {
886 have_new = TRUE;
887 break;
888 }
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000889 if (is_class && !is_abstract && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000890 {
891 // No new() method was defined, add the default constructor.
892 garray_T fga;
893 ga_init2(&fga, 1, 1000);
894 ga_concat(&fga, (char_u *)"new(");
895 for (int i = 0; i < cl->class_obj_member_count; ++i)
896 {
897 if (i > 0)
898 ga_concat(&fga, (char_u *)", ");
899 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000900 ocmember_T *m = cl->class_obj_members + i;
901 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000902 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000903 }
904 ga_concat(&fga, (char_u *)")\nenddef\n");
905 ga_append(&fga, NUL);
906
907 exarg_T fea;
908 CLEAR_FIELD(fea);
909 fea.cmdidx = CMD_def;
910 fea.cmd = fea.arg = fga.ga_data;
911
912 garray_T lines_to_free;
913 ga_init2(&lines_to_free, sizeof(char_u *), 50);
914
Bram Moolenaar2c011312023-01-07 10:51:30 +0000915 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000916
917 ga_clear_strings(&lines_to_free);
918 vim_free(fga.ga_data);
919
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000920 if (nf != NULL && ga_grow(&classfunctions, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000921 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000922 ((ufunc_T **)classfunctions.ga_data)[classfunctions.ga_len]
923 = nf;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000924 ++classfunctions.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000925
926 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000927 nf->uf_ret_type = get_type_ptr(&type_list);
928 if (nf->uf_ret_type != NULL)
929 {
930 nf->uf_ret_type->tt_type = VAR_OBJECT;
931 nf->uf_ret_type->tt_member = (type_T *)cl;
932 nf->uf_ret_type->tt_argcount = 0;
933 nf->uf_ret_type->tt_args = NULL;
934 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000935 }
936 }
937
Bram Moolenaar58b40092023-01-11 15:59:05 +0000938 // Move all the functions into the created class.
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000939 // loop 1: class functions, loop 2: object methods
940 for (int loop = 1; loop <= 2; ++loop)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000941 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000942 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
943 int *fcount = loop == 1 ? &cl->class_class_function_count
944 : &cl->class_obj_method_count;
945 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
946 : &cl->class_obj_methods;
947
Bram Moolenaar83677162023-01-08 19:54:10 +0000948 int parent_count = 0;
949 if (extends_cl != NULL)
950 // Include functions from the parent.
951 parent_count = loop == 1
952 ? extends_cl->class_class_function_count
953 : extends_cl->class_obj_method_count;
954
955 *fcount = parent_count + gap->ga_len;
956 if (*fcount == 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000957 {
958 *fup = NULL;
959 continue;
960 }
Bram Moolenaar83677162023-01-08 19:54:10 +0000961 *fup = ALLOC_MULT(ufunc_T *, *fcount);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000962 if (*fup == NULL)
963 goto cleanup;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000964
Bram Moolenaar58b40092023-01-11 15:59:05 +0000965 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
966 vim_free(gap->ga_data);
967 if (loop == 1)
968 cl->class_class_function_count_child = gap->ga_len;
969 else
970 cl->class_obj_method_count_child = gap->ga_len;
971
Bram Moolenaar83677162023-01-08 19:54:10 +0000972 int skipped = 0;
973 for (int i = 0; i < parent_count; ++i)
974 {
975 // Copy functions from the parent. Can't use the same
976 // function, because "uf_class" is different and compilation
977 // will have a different result.
Bram Moolenaar58b40092023-01-11 15:59:05 +0000978 // Put them after the functions in the current class, object
979 // methods may be overruled, then "super.Method()" is used to
980 // find a method from the parent.
Bram Moolenaar83677162023-01-08 19:54:10 +0000981 // Skip "new" functions. TODO: not all of them.
982 if (loop == 1 && STRNCMP(
983 extends_cl->class_class_functions[i]->uf_name,
984 "new", 3) == 0)
985 ++skipped;
986 else
Bram Moolenaar58b40092023-01-11 15:59:05 +0000987 {
988 ufunc_T *pf = (loop == 1
Bram Moolenaar83677162023-01-08 19:54:10 +0000989 ? extends_cl->class_class_functions
Bram Moolenaar58b40092023-01-11 15:59:05 +0000990 : extends_cl->class_obj_methods)[i];
991 (*fup)[gap->ga_len + i - skipped] = copy_function(pf);
992
993 // If the child class overrides a function from the parent
994 // the signature must be equal.
995 char_u *pname = pf->uf_name;
996 for (int ci = 0; ci < gap->ga_len; ++ci)
997 {
998 ufunc_T *cf = (*fup)[ci];
999 char_u *cname = cf->uf_name;
1000 if (STRCMP(pname, cname) == 0)
1001 {
1002 where_T where = WHERE_INIT;
1003 where.wt_func_name = (char *)pname;
1004 (void)check_type(pf->uf_func_type, cf->uf_func_type,
1005 TRUE, where);
1006 }
1007 }
1008 }
Bram Moolenaar83677162023-01-08 19:54:10 +00001009 }
1010
Bram Moolenaar83677162023-01-08 19:54:10 +00001011 *fcount -= skipped;
1012
1013 // Set the class pointer on all the functions and object methods.
1014 for (int i = 0; i < *fcount; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001015 {
1016 ufunc_T *fp = (*fup)[i];
1017 fp->uf_class = cl;
1018 if (loop == 2)
1019 fp->uf_flags |= FC_OBJECT;
1020 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001021 }
1022
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001023 cl->class_type.tt_type = VAR_CLASS;
1024 cl->class_type.tt_member = (type_T *)cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001025 cl->class_object_type.tt_type = VAR_OBJECT;
1026 cl->class_object_type.tt_member = (type_T *)cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001027 cl->class_type_list = type_list;
1028
1029 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +00001030 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001031
1032 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001033 // TODO: handle other context, e.g. in a function
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001034 typval_T tv;
1035 tv.v_type = VAR_CLASS;
1036 tv.vval.v_class = cl;
Bram Moolenaara86655a2023-01-12 17:06:27 +00001037 is_export = class_export;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001038 set_var_const(cl->class_name, current_sctx.sc_sid,
1039 NULL, &tv, FALSE, ASSIGN_DECL, 0);
1040 return;
1041 }
1042
1043cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +00001044 if (cl != NULL)
1045 {
1046 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001047 vim_free(cl->class_class_functions);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001048 if (cl->class_interfaces != NULL)
1049 {
1050 for (int i = 0; i < cl->class_interface_count; ++i)
1051 vim_free(cl->class_interfaces[i]);
1052 vim_free(cl->class_interfaces);
1053 }
1054 if (cl->class_interfaces_cl != NULL)
1055 {
1056 for (int i = 0; i < cl->class_interface_count; ++i)
1057 class_unref(cl->class_interfaces_cl[i]);
1058 vim_free(cl->class_interfaces_cl);
1059 }
Bram Moolenaareb533502022-12-14 15:06:11 +00001060 vim_free(cl->class_obj_members);
1061 vim_free(cl->class_obj_methods);
1062 vim_free(cl);
1063 }
1064
Bram Moolenaar83677162023-01-08 19:54:10 +00001065 vim_free(extends);
1066 class_unref(extends_cl);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001067
1068 if (intf_classes != NULL)
1069 {
1070 for (int i = 0; i < ga_impl.ga_len; ++i)
1071 class_unref(intf_classes[i]);
1072 vim_free(intf_classes);
1073 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001074 ga_clear_strings(&ga_impl);
1075
Bram Moolenaard505d172022-12-18 21:42:55 +00001076 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001077 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001078 garray_T *gap = round == 1 ? &classmembers : &objmembers;
1079 if (gap->ga_len == 0 || gap->ga_data == NULL)
1080 continue;
1081
1082 for (int i = 0; i < gap->ga_len; ++i)
1083 {
1084 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
1085 vim_free(m->ocm_name);
1086 vim_free(m->ocm_init);
1087 }
1088 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001089 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001090
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001091 for (int i = 0; i < objmethods.ga_len; ++i)
1092 {
1093 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
1094 func_clear_free(uf, FALSE);
1095 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001096 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001097
1098 for (int i = 0; i < classfunctions.ga_len; ++i)
1099 {
1100 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
1101 func_clear_free(uf, FALSE);
1102 }
1103 ga_clear(&classfunctions);
1104
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001105 clear_type_list(&type_list);
1106}
1107
1108/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001109 * Find member "name" in class "cl", set "member_idx" to the member index and
1110 * return its type.
1111 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001112 */
1113 type_T *
1114class_member_type(
1115 class_T *cl,
1116 char_u *name,
1117 char_u *name_end,
1118 int *member_idx)
1119{
1120 *member_idx = -1; // not found (yet)
1121 size_t len = name_end - name;
1122
1123 for (int i = 0; i < cl->class_obj_member_count; ++i)
1124 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001125 ocmember_T *m = cl->class_obj_members + i;
1126 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001127 {
1128 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001129 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001130 }
1131 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001132
1133 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001134 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001135}
1136
1137/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001138 * Handle ":enum" up to ":endenum".
1139 */
1140 void
1141ex_enum(exarg_T *eap UNUSED)
1142{
1143 // TODO
1144}
1145
1146/*
1147 * Handle ":type".
1148 */
1149 void
1150ex_type(exarg_T *eap UNUSED)
1151{
1152 // TODO
1153}
1154
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001155/*
1156 * Evaluate what comes after a class:
1157 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001158 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001159 * - class constructor: SomeClass.new()
1160 * - object member: someObject.varname
1161 * - object method: someObject.SomeMethod()
1162 *
1163 * "*arg" points to the '.'.
1164 * "*arg" is advanced to after the member name or method call.
1165 *
1166 * Returns FAIL or OK.
1167 */
1168 int
1169class_object_index(
1170 char_u **arg,
1171 typval_T *rettv,
1172 evalarg_T *evalarg,
1173 int verbose UNUSED) // give error messages
1174{
1175 // int evaluate = evalarg != NULL
1176 // && (evalarg->eval_flags & EVAL_EVALUATE);
1177
1178 if (VIM_ISWHITE((*arg)[1]))
1179 {
1180 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
1181 return FAIL;
1182 }
1183
1184 ++*arg;
1185 char_u *name = *arg;
1186 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1187 if (name_end == name)
1188 return FAIL;
1189 size_t len = name_end - name;
1190
1191 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
1192 : rettv->vval.v_object->obj_class;
1193 if (*name_end == '(')
1194 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001195 int on_class = rettv->v_type == VAR_CLASS;
1196 int count = on_class ? cl->class_class_function_count
1197 : cl->class_obj_method_count;
1198 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001199 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001200 ufunc_T *fp = on_class ? cl->class_class_functions[i]
1201 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +00001202 // Use a separate pointer to avoid that ASAN complains about
1203 // uf_name[] only being 4 characters.
1204 char_u *ufname = (char_u *)fp->uf_name;
1205 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001206 {
1207 typval_T argvars[MAX_FUNC_ARGS + 1];
1208 int argcount = 0;
1209
1210 char_u *argp = name_end;
1211 int ret = get_func_arguments(&argp, evalarg, 0,
1212 argvars, &argcount);
1213 if (ret == FAIL)
1214 return FAIL;
1215
1216 funcexe_T funcexe;
1217 CLEAR_FIELD(funcexe);
1218 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001219 if (rettv->v_type == VAR_OBJECT)
1220 {
1221 funcexe.fe_object = rettv->vval.v_object;
1222 ++funcexe.fe_object->obj_refcount;
1223 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001224
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001225 // Clear the class or object after calling the function, in
1226 // case the refcount is one.
1227 typval_T tv_tofree = *rettv;
1228 rettv->v_type = VAR_UNKNOWN;
1229
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001230 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001231 int error = call_user_func_check(fp, argcount, argvars,
1232 rettv, &funcexe, NULL);
1233
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001234 // Clear the previous rettv and the arguments.
1235 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001236 for (int idx = 0; idx < argcount; ++idx)
1237 clear_tv(&argvars[idx]);
1238
1239 if (error != FCERR_NONE)
1240 {
1241 user_func_error(error, printable_func_name(fp),
1242 funcexe.fe_found_var);
1243 return FAIL;
1244 }
1245 *arg = argp;
1246 return OK;
1247 }
1248 }
1249
1250 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
1251 }
1252
1253 else if (rettv->v_type == VAR_OBJECT)
1254 {
1255 for (int i = 0; i < cl->class_obj_member_count; ++i)
1256 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001257 ocmember_T *m = &cl->class_obj_members[i];
1258 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001259 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001260 if (*name == '_')
1261 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001262 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001263 return FAIL;
1264 }
1265
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001266 // The object only contains a pointer to the class, the member
1267 // values array follows right after that.
1268 object_T *obj = rettv->vval.v_object;
1269 typval_T *tv = (typval_T *)(obj + 1) + i;
1270 copy_tv(tv, rettv);
1271 object_unref(obj);
1272
1273 *arg = name_end;
1274 return OK;
1275 }
1276 }
1277
1278 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
1279 }
1280
Bram Moolenaard505d172022-12-18 21:42:55 +00001281 else if (rettv->v_type == VAR_CLASS)
1282 {
1283 // class member
1284 for (int i = 0; i < cl->class_class_member_count; ++i)
1285 {
1286 ocmember_T *m = &cl->class_class_members[i];
1287 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
1288 {
1289 if (*name == '_')
1290 {
1291 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
1292 return FAIL;
1293 }
1294
1295 typval_T *tv = &cl->class_members_tv[i];
1296 copy_tv(tv, rettv);
1297 class_unref(cl);
1298
1299 *arg = name_end;
1300 return OK;
1301 }
1302 }
1303
1304 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
1305 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001306
1307 return FAIL;
1308}
1309
1310/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001311 * If "arg" points to a class or object method, return it.
1312 * Otherwise return NULL.
1313 */
1314 ufunc_T *
1315find_class_func(char_u **arg)
1316{
1317 char_u *name = *arg;
1318 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1319 if (name_end == name || *name_end != '.')
1320 return NULL;
1321
1322 size_t len = name_end - name;
1323 typval_T tv;
1324 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001325 if (eval_variable(name, (int)len,
1326 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001327 return NULL;
1328 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001329 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001330
1331 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1332 : tv.vval.v_object->obj_class;
1333 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001334 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001335 char_u *fname = name_end + 1;
1336 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1337 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001338 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001339 len = fname_end - fname;
1340
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001341 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1342 : cl->class_obj_method_count;
1343 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1344 : cl->class_obj_methods;
1345 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001346 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001347 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001348 // Use a separate pointer to avoid that ASAN complains about
1349 // uf_name[] only being 4 characters.
1350 char_u *ufname = (char_u *)fp->uf_name;
1351 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001352 {
1353 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001354 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001355 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001356 }
1357
Bram Moolenaareb533502022-12-14 15:06:11 +00001358fail_after_eval:
1359 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001360 return NULL;
1361}
1362
1363/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001364 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1365 * index in class.class_class_members[].
1366 * If "cl_ret" is not NULL set it to the class.
1367 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001368 */
1369 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001370class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001371{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001372 if (cctx == NULL || cctx->ctx_ufunc == NULL
1373 || cctx->ctx_ufunc->uf_class == NULL)
1374 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001375 class_T *cl = cctx->ctx_ufunc->uf_class;
1376
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001377 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001378 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001379 ocmember_T *m = &cl->class_class_members[i];
1380 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001381 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001382 if (cl_ret != NULL)
1383 *cl_ret = cl;
1384 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001385 }
1386 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001387 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001388}
1389
1390/*
Bram Moolenaar62a69232023-01-24 15:07:04 +00001391 * Return TRUE if current context "cctx_arg" is inside class "cl".
1392 * Return FALSE if not.
1393 */
1394 int
1395inside_class(cctx_T *cctx_arg, class_T *cl)
1396{
1397 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
1398 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class == cl)
1399 return TRUE;
1400 return FALSE;
1401}
1402
1403/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001404 * Make a copy of an object.
1405 */
1406 void
1407copy_object(typval_T *from, typval_T *to)
1408{
1409 *to = *from;
1410 if (to->vval.v_object != NULL)
1411 ++to->vval.v_object->obj_refcount;
1412}
1413
1414/*
1415 * Free an object.
1416 */
1417 static void
1418object_clear(object_T *obj)
1419{
1420 class_T *cl = obj->obj_class;
1421
1422 // the member values are just after the object structure
1423 typval_T *tv = (typval_T *)(obj + 1);
1424 for (int i = 0; i < cl->class_obj_member_count; ++i)
1425 clear_tv(tv + i);
1426
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001427 // Remove from the list headed by "first_object".
1428 object_cleared(obj);
1429
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001430 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001431 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001432}
1433
1434/*
1435 * Unreference an object.
1436 */
1437 void
1438object_unref(object_T *obj)
1439{
1440 if (obj != NULL && --obj->obj_refcount <= 0)
1441 object_clear(obj);
1442}
1443
1444/*
1445 * Make a copy of a class.
1446 */
1447 void
1448copy_class(typval_T *from, typval_T *to)
1449{
1450 *to = *from;
1451 if (to->vval.v_class != NULL)
1452 ++to->vval.v_class->class_refcount;
1453}
1454
1455/*
1456 * Unreference a class. Free it when the reference count goes down to zero.
1457 */
1458 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001459class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001460{
Bram Moolenaard505d172022-12-18 21:42:55 +00001461 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001462 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001463 // Freeing what the class contains may recursively come back here.
1464 // Clear "class_name" first, if it is NULL the class does not need to
1465 // be freed.
1466 VIM_CLEAR(cl->class_name);
1467
Bram Moolenaar83677162023-01-08 19:54:10 +00001468 class_unref(cl->class_extends);
1469
Bram Moolenaar94674f22023-01-06 18:42:20 +00001470 for (int i = 0; i < cl->class_interface_count; ++i)
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001471 {
Bram Moolenaar94674f22023-01-06 18:42:20 +00001472 vim_free(((char_u **)cl->class_interfaces)[i]);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001473 if (cl->class_interfaces_cl[i] != NULL)
1474 class_unref(cl->class_interfaces_cl[i]);
1475 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001476 vim_free(cl->class_interfaces);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001477 vim_free(cl->class_interfaces_cl);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001478
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001479 itf2class_T *next;
1480 for (itf2class_T *i2c = cl->class_itf2class; i2c != NULL; i2c = next)
1481 {
1482 next = i2c->i2c_next;
1483 vim_free(i2c);
1484 }
1485
Bram Moolenaard505d172022-12-18 21:42:55 +00001486 for (int i = 0; i < cl->class_class_member_count; ++i)
1487 {
1488 ocmember_T *m = &cl->class_class_members[i];
1489 vim_free(m->ocm_name);
1490 vim_free(m->ocm_init);
1491 if (cl->class_members_tv != NULL)
1492 clear_tv(&cl->class_members_tv[i]);
1493 }
1494 vim_free(cl->class_class_members);
1495 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001496
1497 for (int i = 0; i < cl->class_obj_member_count; ++i)
1498 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001499 ocmember_T *m = &cl->class_obj_members[i];
1500 vim_free(m->ocm_name);
1501 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001502 }
1503 vim_free(cl->class_obj_members);
1504
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001505 for (int i = 0; i < cl->class_class_function_count; ++i)
1506 {
1507 ufunc_T *uf = cl->class_class_functions[i];
1508 func_clear_free(uf, FALSE);
1509 }
1510 vim_free(cl->class_class_functions);
1511
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001512 for (int i = 0; i < cl->class_obj_method_count; ++i)
1513 {
1514 ufunc_T *uf = cl->class_obj_methods[i];
1515 func_clear_free(uf, FALSE);
1516 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001517 vim_free(cl->class_obj_methods);
1518
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001519 clear_type_list(&cl->class_type_list);
1520
1521 vim_free(cl);
1522 }
1523}
1524
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001525static object_T *first_object = NULL;
1526
1527/*
1528 * Call this function when an object has been created. It will be added to the
1529 * list headed by "first_object".
1530 */
1531 void
1532object_created(object_T *obj)
1533{
1534 if (first_object != NULL)
1535 {
1536 obj->obj_next_used = first_object;
1537 first_object->obj_prev_used = obj;
1538 }
1539 first_object = obj;
1540}
1541
1542/*
1543 * Call this function when an object has been cleared and is about to be freed.
1544 * It is removed from the list headed by "first_object".
1545 */
1546 void
1547object_cleared(object_T *obj)
1548{
1549 if (obj->obj_next_used != NULL)
1550 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1551 if (obj->obj_prev_used != NULL)
1552 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1553 else if (first_object == obj)
1554 first_object = obj->obj_next_used;
1555}
1556
1557/*
1558 * Go through the list of all objects and free items without "copyID".
1559 */
1560 int
1561object_free_nonref(int copyID)
1562{
1563 int did_free = FALSE;
1564 object_T *next_obj;
1565
1566 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
1567 {
1568 next_obj = obj->obj_next_used;
1569 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1570 {
1571 // Free the object and items it contains.
1572 object_clear(obj);
1573 did_free = TRUE;
1574 }
1575 }
1576
1577 return did_free;
1578}
1579
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001580
1581#endif // FEAT_EVAL