blob: a2b186ec5a36e6fa7b3e7e67e0b1356074c672c3 [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
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 }
212 itf2class_T *i2c;
213 for (i2c = itf->class_itf2class; i2c != NULL; i2c = i2c->i2c_next)
Bram Moolenaard0200c82023-01-28 15:19:40 +0000214 if (i2c->i2c_class == cl && i2c->i2c_is_method == is_method)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000215 break;
216 if (i2c == NULL)
217 {
218 siemsg("class %s not found on interface %s",
219 cl->class_name, itf->class_name);
220 return 0;
221 }
222 int *table = (int *)(i2c + 1);
223 return table[idx];
224}
225
226/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000227 * Handle ":class" and ":abstract class" up to ":endclass".
Bram Moolenaar554d0312023-01-05 19:59:18 +0000228 * Handle ":interface" up to ":endinterface".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000229 */
230 void
231ex_class(exarg_T *eap)
232{
Bram Moolenaar83ae6152023-02-25 19:59:31 +0000233 int is_class = eap->cmdidx == CMD_class; // FALSE for :interface
234 long start_lnum = SOURCING_LNUM;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000235
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000236 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000237 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000238 if (is_abstract)
239 {
240 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
241 {
242 semsg(_(e_invalid_argument_str), arg);
243 return;
244 }
245 arg = skipwhite(arg + 5);
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000246 is_class = TRUE;
247 }
248
249 if (!current_script_is_vim9()
250 || (cmdmod.cmod_flags & CMOD_LEGACY)
251 || !getline_equal(eap->getline, eap->cookie, getsourceline))
252 {
253 if (is_class)
254 emsg(_(e_class_can_only_be_defined_in_vim9_script));
255 else
256 emsg(_(e_interface_can_only_be_defined_in_vim9_script));
257 return;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000258 }
259
260 if (!ASCII_ISUPPER(*arg))
261 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000262 if (is_class)
263 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
264 else
265 semsg(_(e_interface_name_must_start_with_uppercase_letter_str),
266 arg);
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000267 return;
268 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000269 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
270 if (!IS_WHITE_OR_NUL(*name_end))
271 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000272 semsg(_(e_white_space_required_after_name_str), arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000273 return;
274 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000275 char_u *name_start = arg;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000276
Bram Moolenaara86655a2023-01-12 17:06:27 +0000277 // "export class" gets used when creating the class, don't use "is_export"
278 // for the items inside the class.
279 int class_export = is_export;
280 is_export = FALSE;
281
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000282 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000283 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000284
Bram Moolenaar83677162023-01-08 19:54:10 +0000285 // Name for "extends BaseClass"
286 char_u *extends = NULL;
287
Bram Moolenaar94674f22023-01-06 18:42:20 +0000288 // Names for "implements SomeInterface"
289 garray_T ga_impl;
290 ga_init2(&ga_impl, sizeof(char_u *), 5);
291
292 arg = skipwhite(name_end);
293 while (*arg != NUL && *arg != '#' && *arg != '\n')
294 {
295 // TODO:
Bram Moolenaar94674f22023-01-06 18:42:20 +0000296 // specifies SomeInterface
Bram Moolenaar83677162023-01-08 19:54:10 +0000297 if (STRNCMP(arg, "extends", 7) == 0 && IS_WHITE_OR_NUL(arg[7]))
298 {
299 if (extends != NULL)
300 {
301 emsg(_(e_duplicate_extends));
302 goto early_ret;
303 }
304 arg = skipwhite(arg + 7);
305 char_u *end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
306 if (!IS_WHITE_OR_NUL(*end))
307 {
308 semsg(_(e_white_space_required_after_name_str), arg);
309 goto early_ret;
310 }
311 extends = vim_strnsave(arg, end - arg);
312 if (extends == NULL)
313 goto early_ret;
314
315 arg = skipwhite(end + 1);
316 }
317 else if (STRNCMP(arg, "implements", 10) == 0
318 && IS_WHITE_OR_NUL(arg[10]))
Bram Moolenaar94674f22023-01-06 18:42:20 +0000319 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000320 if (ga_impl.ga_len > 0)
321 {
322 emsg(_(e_duplicate_implements));
323 goto early_ret;
324 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000325 arg = skipwhite(arg + 10);
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000326
327 for (;;)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000328 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000329 char_u *impl_end = find_name_end(arg, NULL, NULL,
330 FNE_CHECK_START);
331 if (!IS_WHITE_OR_NUL(*impl_end) && *impl_end != ',')
332 {
333 semsg(_(e_white_space_required_after_name_str), arg);
334 goto early_ret;
335 }
336 char_u *iname = vim_strnsave(arg, impl_end - arg);
337 if (iname == NULL)
338 goto early_ret;
339 for (int i = 0; i < ga_impl.ga_len; ++i)
340 if (STRCMP(((char_u **)ga_impl.ga_data)[i], iname) == 0)
341 {
342 semsg(_(e_duplicate_interface_after_implements_str),
343 iname);
344 vim_free(iname);
345 goto early_ret;
346 }
347 if (ga_add_string(&ga_impl, iname) == FAIL)
348 {
349 vim_free(iname);
350 goto early_ret;
351 }
352 if (*impl_end != ',')
353 {
354 arg = skipwhite(impl_end);
355 break;
356 }
357 arg = skipwhite(impl_end + 1);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000358 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000359 }
360 else
361 {
362 semsg(_(e_trailing_characters_str), arg);
363early_ret:
Bram Moolenaar83677162023-01-08 19:54:10 +0000364 vim_free(extends);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000365 ga_clear_strings(&ga_impl);
366 return;
367 }
368 }
369
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000370 garray_T type_list; // list of pointers to allocated types
371 ga_init2(&type_list, sizeof(type_T *), 10);
372
Bram Moolenaard505d172022-12-18 21:42:55 +0000373 // Growarray with class members declared in the class.
374 garray_T classmembers;
375 ga_init2(&classmembers, sizeof(ocmember_T), 10);
376
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000377 // Growarray with functions declared in the class.
378 garray_T classfunctions;
379 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +0000380
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000381 // Growarray with object members declared in the class.
382 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +0000383 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000384
385 // Growarray with object methods declared in the class.
386 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000387 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000388
389 /*
Bram Moolenaar554d0312023-01-05 19:59:18 +0000390 * Go over the body of the class/interface until "endclass" or
391 * "endinterface" is found.
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000392 */
393 char_u *theline = NULL;
394 int success = FALSE;
395 for (;;)
396 {
397 vim_free(theline);
398 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
399 if (theline == NULL)
400 break;
401 char_u *line = skipwhite(theline);
402
Bram Moolenaar418b5472022-12-20 13:38:22 +0000403 // Skip empty and comment lines.
404 if (*line == NUL)
405 continue;
406 if (*line == '#')
407 {
408 if (vim9_bad_comment(line))
409 break;
410 continue;
411 }
412
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000413 char_u *p = line;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000414 char *end_name = is_class ? "endclass" : "endinterface";
415 if (checkforcmd(&p, end_name, is_class ? 4 : 5))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000416 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000417 if (STRNCMP(line, end_name, is_class ? 8 : 12) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000418 semsg(_(e_command_cannot_be_shortened_str), line);
419 else if (*p == '|' || !ends_excmd2(line, p))
420 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +0000421 else
422 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000423 break;
424 }
Bram Moolenaar554d0312023-01-05 19:59:18 +0000425 char *wrong_name = is_class ? "endinterface" : "endclass";
426 if (checkforcmd(&p, wrong_name, is_class ? 5 : 4))
427 {
Bram Moolenaar657aea72023-01-27 13:16:19 +0000428 semsg(_(e_invalid_command_str_expected_str), line, end_name);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000429 break;
430 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000431
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000432 int has_public = FALSE;
433 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000434 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000435 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000436 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000437 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000438 break;
439 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000440 has_public = TRUE;
441 p = skipwhite(line + 6);
442
Bram Moolenaard505d172022-12-18 21:42:55 +0000443 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000444 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000445 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000446 break;
447 }
448 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000449
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000450 int has_static = FALSE;
451 char_u *ps = p;
452 if (checkforcmd(&p, "static", 4))
453 {
454 if (STRNCMP(ps, "static", 6) != 0)
455 {
456 semsg(_(e_command_cannot_be_shortened_str), ps);
457 break;
458 }
459 has_static = TRUE;
460 p = skipwhite(ps + 6);
461 }
462
Bram Moolenaard505d172022-12-18 21:42:55 +0000463 // object members (public, read access, private):
464 // "this._varname"
465 // "this.varname"
466 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000467 if (STRNCMP(p, "this", 4) == 0)
468 {
469 if (p[4] != '.' || !eval_isnamec1(p[5]))
470 {
471 semsg(_(e_invalid_object_member_declaration_str), p);
472 break;
473 }
474 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +0000475 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +0000476 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +0000477 char_u *init_expr = NULL;
478 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000479 &varname_end, &type_list, &type,
480 is_class ? &init_expr: NULL) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000481 break;
482 if (add_member(&objmembers, varname, varname_end,
483 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000484 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000485 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000486 break;
487 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000488 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000489
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000490 // constructors:
491 // def new()
492 // enddef
493 // def newOther()
494 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000495 // object methods and class functions:
496 // def SomeMethod()
497 // enddef
498 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000499 // enddef
500 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000501 // def <Tval> someMethod()
502 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000503 else if (checkforcmd(&p, "def", 3))
504 {
505 exarg_T ea;
506 garray_T lines_to_free;
507
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000508 // TODO: error for "public static def Func()"?
509
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000510 CLEAR_FIELD(ea);
511 ea.cmd = line;
512 ea.arg = p;
513 ea.cmdidx = CMD_def;
514 ea.getline = eap->getline;
515 ea.cookie = eap->cookie;
516
517 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000518 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free,
519 is_class ? CF_CLASS : CF_INTERFACE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000520 ga_clear_strings(&lines_to_free);
521
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000522 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000523 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000524 char_u *name = uf->uf_name;
525 int is_new = STRNCMP(name, "new", 3) == 0;
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000526 if (is_new && is_abstract)
527 {
528 emsg(_(e_cannot_define_new_function_in_abstract_class));
529 success = FALSE;
530 break;
531 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000532 garray_T *fgap = has_static || is_new
533 ? &classfunctions : &objmethods;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000534 // Check the name isn't used already.
535 for (int i = 0; i < fgap->ga_len; ++i)
536 {
537 char_u *n = ((ufunc_T **)fgap->ga_data)[i]->uf_name;
538 if (STRCMP(name, n) == 0)
539 {
540 semsg(_(e_duplicate_function_str), name);
541 break;
542 }
543 }
544
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000545 if (ga_grow(fgap, 1) == OK)
546 {
547 if (is_new)
548 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000549
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000550 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
551 ++fgap->ga_len;
552 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000553 }
554 }
555
556 // class members
557 else if (has_static)
558 {
559 // class members (public, read access, private):
560 // "static _varname"
561 // "static varname"
562 // "public static varname"
563 char_u *varname = p;
564 char_u *varname_end = NULL;
565 type_T *type = NULL;
566 char_u *init_expr = NULL;
567 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000568 &varname_end, &type_list, &type,
569 is_class ? &init_expr : NULL) == FAIL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000570 break;
571 if (add_member(&classmembers, varname, varname_end,
572 has_public, type, init_expr) == FAIL)
573 {
574 vim_free(init_expr);
575 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000576 }
577 }
578
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000579 else
580 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000581 if (is_class)
582 semsg(_(e_not_valid_command_in_class_str), line);
583 else
584 semsg(_(e_not_valid_command_in_interface_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000585 break;
586 }
587 }
588 vim_free(theline);
589
Bram Moolenaar83677162023-01-08 19:54:10 +0000590 class_T *extends_cl = NULL; // class from "extends" argument
591
592 /*
593 * Check a few things before defining the class.
594 */
595
596 // Check the "extends" class is valid.
597 if (success && extends != NULL)
598 {
599 typval_T tv;
600 tv.v_type = VAR_UNKNOWN;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000601 if (eval_variable_import(extends, &tv) == FAIL)
Bram Moolenaar83677162023-01-08 19:54:10 +0000602 {
603 semsg(_(e_class_name_not_found_str), extends);
604 success = FALSE;
605 }
606 else
607 {
608 if (tv.v_type != VAR_CLASS
609 || tv.vval.v_class == NULL
610 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) != 0)
611 {
612 semsg(_(e_cannot_extend_str), extends);
613 success = FALSE;
614 }
615 else
616 {
617 extends_cl = tv.vval.v_class;
618 ++extends_cl->class_refcount;
619 }
620 clear_tv(&tv);
621 }
622 }
623 VIM_CLEAR(extends);
624
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000625 class_T **intf_classes = NULL;
626
Bram Moolenaar83677162023-01-08 19:54:10 +0000627 // Check all "implements" entries are valid.
Bram Moolenaar94674f22023-01-06 18:42:20 +0000628 if (success && ga_impl.ga_len > 0)
629 {
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000630 intf_classes = ALLOC_CLEAR_MULT(class_T *, ga_impl.ga_len);
631
Bram Moolenaar94674f22023-01-06 18:42:20 +0000632 for (int i = 0; i < ga_impl.ga_len && success; ++i)
633 {
634 char_u *impl = ((char_u **)ga_impl.ga_data)[i];
635 typval_T tv;
636 tv.v_type = VAR_UNKNOWN;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000637 if (eval_variable_import(impl, &tv) == FAIL)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000638 {
639 semsg(_(e_interface_name_not_found_str), impl);
640 success = FALSE;
641 break;
642 }
643
644 if (tv.v_type != VAR_CLASS
645 || tv.vval.v_class == NULL
646 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0)
647 {
648 semsg(_(e_not_valid_interface_str), impl);
649 success = FALSE;
650 }
651
Bram Moolenaar94674f22023-01-06 18:42:20 +0000652 class_T *ifcl = tv.vval.v_class;
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000653 intf_classes[i] = ifcl;
654 ++ifcl->class_refcount;
655
656 // check the members of the interface match the members of the class
Bram Moolenaar94674f22023-01-06 18:42:20 +0000657 for (int loop = 1; loop <= 2 && success; ++loop)
658 {
659 // loop == 1: check class members
660 // loop == 2: check object members
661 int if_count = loop == 1 ? ifcl->class_class_member_count
662 : ifcl->class_obj_member_count;
663 if (if_count == 0)
664 continue;
665 ocmember_T *if_ms = loop == 1 ? ifcl->class_class_members
666 : ifcl->class_obj_members;
667 ocmember_T *cl_ms = (ocmember_T *)(loop == 1
668 ? classmembers.ga_data
669 : objmembers.ga_data);
670 int cl_count = loop == 1 ? classmembers.ga_len
671 : objmembers.ga_len;
672 for (int if_i = 0; if_i < if_count; ++if_i)
673 {
674 int cl_i;
675 for (cl_i = 0; cl_i < cl_count; ++cl_i)
676 {
677 ocmember_T *m = &cl_ms[cl_i];
678 if (STRCMP(if_ms[if_i].ocm_name, m->ocm_name) == 0)
679 {
680 // TODO: check type
681 break;
682 }
683 }
684 if (cl_i == cl_count)
685 {
686 semsg(_(e_member_str_of_interface_str_not_implemented),
687 if_ms[if_i].ocm_name, impl);
688 success = FALSE;
689 break;
690 }
691 }
692 }
693
694 // check the functions/methods of the interface match the
695 // functions/methods of the class
696 for (int loop = 1; loop <= 2 && success; ++loop)
697 {
698 // loop == 1: check class functions
699 // loop == 2: check object methods
700 int if_count = loop == 1 ? ifcl->class_class_function_count
701 : ifcl->class_obj_method_count;
702 if (if_count == 0)
703 continue;
704 ufunc_T **if_fp = loop == 1 ? ifcl->class_class_functions
705 : ifcl->class_obj_methods;
706 ufunc_T **cl_fp = (ufunc_T **)(loop == 1
707 ? classfunctions.ga_data
708 : objmethods.ga_data);
709 int cl_count = loop == 1 ? classfunctions.ga_len
710 : objmethods.ga_len;
711 for (int if_i = 0; if_i < if_count; ++if_i)
712 {
713 char_u *if_name = if_fp[if_i]->uf_name;
714 int cl_i;
715 for (cl_i = 0; cl_i < cl_count; ++cl_i)
716 {
717 char_u *cl_name = cl_fp[cl_i]->uf_name;
718 if (STRCMP(if_name, cl_name) == 0)
719 {
720 // TODO: check return and argument types
721 break;
722 }
723 }
724 if (cl_i == cl_count)
725 {
726 semsg(_(e_function_str_of_interface_str_not_implemented),
727 if_name, impl);
728 success = FALSE;
729 break;
730 }
731 }
732 }
733
734 clear_tv(&tv);
735 }
736 }
737
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000738 if (success)
739 {
740 // Check no function argument name is used as an object/class member.
741 for (int loop = 1; loop <= 2 && success; ++loop)
742 {
743 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
744
745 for (int fi = 0; fi < gap->ga_len && success; ++fi)
746 {
747 ufunc_T *uf = ((ufunc_T **)gap->ga_data)[fi];
748
749 for (int i = 0; i < uf->uf_args.ga_len && success; ++i)
750 {
751 char_u *aname = ((char_u **)uf->uf_args.ga_data)[i];
752 for (int il = 1; il <= 2 && success; ++il)
753 {
754 // For a "new()" function "this.member" arguments are
755 // OK. TODO: check for the "this." prefix.
Bram Moolenaarb40a2fb2023-01-13 19:18:38 +0000756 if (STRNCMP(uf->uf_name, "new", 3) == 0 && il == 2)
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000757 continue;
758 garray_T *mgap = il == 1 ? &classmembers : &objmembers;
759 for (int mi = 0; mi < mgap->ga_len; ++mi)
760 {
761 char_u *mname = ((ocmember_T *)mgap->ga_data
762 + mi)->ocm_name;
763 if (STRCMP(aname, mname) == 0)
764 {
765 success = FALSE;
766 semsg(_(e_argument_already_declared_in_class_str),
767 aname);
768 break;
769 }
770 }
771 }
772 }
773 }
774 }
775 }
776
777
Bram Moolenaareb533502022-12-14 15:06:11 +0000778 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000779 if (success)
780 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000781 // "endclass" encountered without failures: Create the class.
782
Bram Moolenaareb533502022-12-14 15:06:11 +0000783 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000784 if (cl == NULL)
785 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000786 if (!is_class)
787 cl->class_flags = CLASS_INTERFACE;
788
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000789 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +0000790 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +0000791 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000792 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000793
Bram Moolenaard0200c82023-01-28 15:19:40 +0000794 if (extends_cl != NULL)
795 {
796 cl->class_extends = extends_cl;
797 extends_cl->class_flags |= CLASS_EXTENDED;
798 }
Bram Moolenaar83677162023-01-08 19:54:10 +0000799
Bram Moolenaard505d172022-12-18 21:42:55 +0000800 // Add class and object members to "cl".
801 if (add_members_to_class(&classmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000802 extends_cl == NULL ? NULL
803 : extends_cl->class_class_members,
804 extends_cl == NULL ? 0
805 : extends_cl->class_class_member_count,
806 &cl->class_class_members,
807 &cl->class_class_member_count) == FAIL
Bram Moolenaard505d172022-12-18 21:42:55 +0000808 || add_members_to_class(&objmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000809 extends_cl == NULL ? NULL
810 : extends_cl->class_obj_members,
811 extends_cl == NULL ? 0
812 : extends_cl->class_obj_member_count,
813 &cl->class_obj_members,
814 &cl->class_obj_member_count) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000815 goto cleanup;
816
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000817 if (ga_impl.ga_len > 0)
818 {
819 // Move the "implements" names into the class.
820 cl->class_interface_count = ga_impl.ga_len;
821 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
822 if (cl->class_interfaces == NULL)
823 goto cleanup;
824 for (int i = 0; i < ga_impl.ga_len; ++i)
825 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
826 VIM_CLEAR(ga_impl.ga_data);
827 ga_impl.ga_len = 0;
828
Bram Moolenaard0200c82023-01-28 15:19:40 +0000829 cl->class_interfaces_cl = intf_classes;
830 intf_classes = NULL;
831 }
832
833 if (cl->class_interface_count > 0 || extends_cl != NULL)
834 {
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000835 // For each interface add a lookuptable for the member index on the
836 // interface to the member index in this class.
Bram Moolenaard0200c82023-01-28 15:19:40 +0000837 // And a lookuptable for the object method index on the interface
838 // to the object method index in this class.
839 // Also do this for the extended class, if any.
840 for (int i = 0; i <= cl->class_interface_count; ++i)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000841 {
Bram Moolenaard0200c82023-01-28 15:19:40 +0000842 class_T *ifcl = i < cl->class_interface_count
843 ? cl->class_interfaces_cl[i]
844 : extends_cl;
845 if (ifcl == NULL)
846 continue;
847
848 // Table for members.
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000849 itf2class_T *if2cl = alloc_clear(sizeof(itf2class_T)
850 + ifcl->class_obj_member_count * sizeof(int));
851 if (if2cl == NULL)
852 goto cleanup;
853 if2cl->i2c_next = ifcl->class_itf2class;
854 ifcl->class_itf2class = if2cl;
855 if2cl->i2c_class = cl;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000856 if2cl->i2c_is_method = FALSE;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000857
858 for (int if_i = 0; if_i < ifcl->class_obj_member_count; ++if_i)
Bram Moolenaard0200c82023-01-28 15:19:40 +0000859 for (int cl_i = 0; cl_i < cl->class_obj_member_count;
860 ++cl_i)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000861 {
862 if (STRCMP(ifcl->class_obj_members[if_i].ocm_name,
Bram Moolenaard0200c82023-01-28 15:19:40 +0000863 cl->class_obj_members[cl_i].ocm_name) == 0)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000864 {
865 int *table = (int *)(if2cl + 1);
866 table[if_i] = cl_i;
867 break;
868 }
869 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000870
Bram Moolenaard0200c82023-01-28 15:19:40 +0000871 // Table for methods.
872 if2cl = alloc_clear(sizeof(itf2class_T)
873 + ifcl->class_obj_method_count * sizeof(int));
874 if (if2cl == NULL)
875 goto cleanup;
876 if2cl->i2c_next = ifcl->class_itf2class;
877 ifcl->class_itf2class = if2cl;
878 if2cl->i2c_class = cl;
879 if2cl->i2c_is_method = TRUE;
880
881 for (int if_i = 0; if_i < ifcl->class_obj_method_count; ++if_i)
882 {
883 int done = FALSE;
884 for (int cl_i = 0; cl_i < objmethods.ga_len; ++cl_i)
885 {
886 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
887 ((ufunc_T **)objmethods.ga_data)[cl_i]->uf_name)
888 == 0)
889 {
890 int *table = (int *)(if2cl + 1);
891 table[if_i] = cl_i;
892 done = TRUE;
893 break;
894 }
895 }
896
897 if (!done && extends_cl != NULL)
898 {
899 for (int cl_i = 0;
900 cl_i < extends_cl->class_obj_member_count; ++cl_i)
901 {
902 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
903 extends_cl->class_obj_methods[cl_i]->uf_name)
904 == 0)
905 {
906 int *table = (int *)(if2cl + 1);
907 table[if_i] = cl_i;
908 break;
909 }
910 }
911 }
912 }
913 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000914 }
915
Bram Moolenaar554d0312023-01-05 19:59:18 +0000916 if (is_class && cl->class_class_member_count > 0)
Bram Moolenaard505d172022-12-18 21:42:55 +0000917 {
918 // Allocate a typval for each class member and initialize it.
919 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
920 cl->class_class_member_count);
921 if (cl->class_members_tv != NULL)
922 for (int i = 0; i < cl->class_class_member_count; ++i)
923 {
924 ocmember_T *m = &cl->class_class_members[i];
925 typval_T *tv = &cl->class_members_tv[i];
926 if (m->ocm_init != NULL)
927 {
928 typval_T *etv = eval_expr(m->ocm_init, eap);
929 if (etv != NULL)
930 {
931 *tv = *etv;
932 vim_free(etv);
933 }
934 }
935 else
936 {
937 // TODO: proper default value
938 tv->v_type = m->ocm_type->tt_type;
939 tv->vval.v_string = NULL;
940 }
941 }
942 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000943
944 int have_new = FALSE;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000945 for (int i = 0; i < classfunctions.ga_len; ++i)
946 if (STRCMP(((ufunc_T **)classfunctions.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000947 "new") == 0)
948 {
949 have_new = TRUE;
950 break;
951 }
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000952 if (is_class && !is_abstract && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000953 {
954 // No new() method was defined, add the default constructor.
955 garray_T fga;
956 ga_init2(&fga, 1, 1000);
957 ga_concat(&fga, (char_u *)"new(");
958 for (int i = 0; i < cl->class_obj_member_count; ++i)
959 {
960 if (i > 0)
961 ga_concat(&fga, (char_u *)", ");
962 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000963 ocmember_T *m = cl->class_obj_members + i;
964 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000965 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000966 }
967 ga_concat(&fga, (char_u *)")\nenddef\n");
968 ga_append(&fga, NUL);
969
970 exarg_T fea;
971 CLEAR_FIELD(fea);
972 fea.cmdidx = CMD_def;
973 fea.cmd = fea.arg = fga.ga_data;
974
975 garray_T lines_to_free;
976 ga_init2(&lines_to_free, sizeof(char_u *), 50);
977
Bram Moolenaar2c011312023-01-07 10:51:30 +0000978 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000979
980 ga_clear_strings(&lines_to_free);
981 vim_free(fga.ga_data);
982
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000983 if (nf != NULL && ga_grow(&classfunctions, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000984 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000985 ((ufunc_T **)classfunctions.ga_data)[classfunctions.ga_len]
986 = nf;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000987 ++classfunctions.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000988
989 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000990 nf->uf_ret_type = get_type_ptr(&type_list);
991 if (nf->uf_ret_type != NULL)
992 {
993 nf->uf_ret_type->tt_type = VAR_OBJECT;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +0000994 nf->uf_ret_type->tt_class = cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000995 nf->uf_ret_type->tt_argcount = 0;
996 nf->uf_ret_type->tt_args = NULL;
997 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000998 }
999 }
1000
Bram Moolenaar58b40092023-01-11 15:59:05 +00001001 // Move all the functions into the created class.
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001002 // loop 1: class functions, loop 2: object methods
1003 for (int loop = 1; loop <= 2; ++loop)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001004 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001005 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
1006 int *fcount = loop == 1 ? &cl->class_class_function_count
1007 : &cl->class_obj_method_count;
1008 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
1009 : &cl->class_obj_methods;
1010
Bram Moolenaar83677162023-01-08 19:54:10 +00001011 int parent_count = 0;
1012 if (extends_cl != NULL)
1013 // Include functions from the parent.
1014 parent_count = loop == 1
1015 ? extends_cl->class_class_function_count
1016 : extends_cl->class_obj_method_count;
1017
1018 *fcount = parent_count + gap->ga_len;
1019 if (*fcount == 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001020 {
1021 *fup = NULL;
1022 continue;
1023 }
Bram Moolenaar83677162023-01-08 19:54:10 +00001024 *fup = ALLOC_MULT(ufunc_T *, *fcount);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001025 if (*fup == NULL)
1026 goto cleanup;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001027
Bram Moolenaar58b40092023-01-11 15:59:05 +00001028 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
1029 vim_free(gap->ga_data);
1030 if (loop == 1)
1031 cl->class_class_function_count_child = gap->ga_len;
1032 else
1033 cl->class_obj_method_count_child = gap->ga_len;
1034
Bram Moolenaar83677162023-01-08 19:54:10 +00001035 int skipped = 0;
1036 for (int i = 0; i < parent_count; ++i)
1037 {
1038 // Copy functions from the parent. Can't use the same
1039 // function, because "uf_class" is different and compilation
1040 // will have a different result.
Bram Moolenaar58b40092023-01-11 15:59:05 +00001041 // Put them after the functions in the current class, object
1042 // methods may be overruled, then "super.Method()" is used to
1043 // find a method from the parent.
Bram Moolenaar83677162023-01-08 19:54:10 +00001044 // Skip "new" functions. TODO: not all of them.
1045 if (loop == 1 && STRNCMP(
1046 extends_cl->class_class_functions[i]->uf_name,
1047 "new", 3) == 0)
1048 ++skipped;
1049 else
Bram Moolenaar58b40092023-01-11 15:59:05 +00001050 {
1051 ufunc_T *pf = (loop == 1
Bram Moolenaar83677162023-01-08 19:54:10 +00001052 ? extends_cl->class_class_functions
Bram Moolenaar58b40092023-01-11 15:59:05 +00001053 : extends_cl->class_obj_methods)[i];
1054 (*fup)[gap->ga_len + i - skipped] = copy_function(pf);
1055
1056 // If the child class overrides a function from the parent
1057 // the signature must be equal.
1058 char_u *pname = pf->uf_name;
1059 for (int ci = 0; ci < gap->ga_len; ++ci)
1060 {
1061 ufunc_T *cf = (*fup)[ci];
1062 char_u *cname = cf->uf_name;
1063 if (STRCMP(pname, cname) == 0)
1064 {
1065 where_T where = WHERE_INIT;
1066 where.wt_func_name = (char *)pname;
1067 (void)check_type(pf->uf_func_type, cf->uf_func_type,
1068 TRUE, where);
1069 }
1070 }
1071 }
Bram Moolenaar83677162023-01-08 19:54:10 +00001072 }
1073
Bram Moolenaar83677162023-01-08 19:54:10 +00001074 *fcount -= skipped;
1075
1076 // Set the class pointer on all the functions and object methods.
1077 for (int i = 0; i < *fcount; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001078 {
1079 ufunc_T *fp = (*fup)[i];
1080 fp->uf_class = cl;
1081 if (loop == 2)
1082 fp->uf_flags |= FC_OBJECT;
1083 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001084 }
1085
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001086 cl->class_type.tt_type = VAR_CLASS;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001087 cl->class_type.tt_class = cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001088 cl->class_object_type.tt_type = VAR_OBJECT;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001089 cl->class_object_type.tt_class = cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001090 cl->class_type_list = type_list;
1091
1092 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +00001093 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001094
1095 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001096 // TODO: handle other context, e.g. in a function
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001097 typval_T tv;
1098 tv.v_type = VAR_CLASS;
1099 tv.vval.v_class = cl;
Bram Moolenaara86655a2023-01-12 17:06:27 +00001100 is_export = class_export;
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001101 SOURCING_LNUM = start_lnum;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001102 set_var_const(cl->class_name, current_sctx.sc_sid,
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001103 NULL, &tv, FALSE, 0, 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001104 return;
1105 }
1106
1107cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +00001108 if (cl != NULL)
1109 {
1110 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001111 vim_free(cl->class_class_functions);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001112 if (cl->class_interfaces != NULL)
1113 {
1114 for (int i = 0; i < cl->class_interface_count; ++i)
1115 vim_free(cl->class_interfaces[i]);
1116 vim_free(cl->class_interfaces);
1117 }
1118 if (cl->class_interfaces_cl != NULL)
1119 {
1120 for (int i = 0; i < cl->class_interface_count; ++i)
1121 class_unref(cl->class_interfaces_cl[i]);
1122 vim_free(cl->class_interfaces_cl);
1123 }
Bram Moolenaareb533502022-12-14 15:06:11 +00001124 vim_free(cl->class_obj_members);
1125 vim_free(cl->class_obj_methods);
1126 vim_free(cl);
1127 }
1128
Bram Moolenaar83677162023-01-08 19:54:10 +00001129 vim_free(extends);
1130 class_unref(extends_cl);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001131
1132 if (intf_classes != NULL)
1133 {
1134 for (int i = 0; i < ga_impl.ga_len; ++i)
1135 class_unref(intf_classes[i]);
1136 vim_free(intf_classes);
1137 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001138 ga_clear_strings(&ga_impl);
1139
Bram Moolenaard505d172022-12-18 21:42:55 +00001140 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001141 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001142 garray_T *gap = round == 1 ? &classmembers : &objmembers;
1143 if (gap->ga_len == 0 || gap->ga_data == NULL)
1144 continue;
1145
1146 for (int i = 0; i < gap->ga_len; ++i)
1147 {
1148 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
1149 vim_free(m->ocm_name);
1150 vim_free(m->ocm_init);
1151 }
1152 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001153 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001154
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001155 for (int i = 0; i < objmethods.ga_len; ++i)
1156 {
1157 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
1158 func_clear_free(uf, FALSE);
1159 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001160 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001161
1162 for (int i = 0; i < classfunctions.ga_len; ++i)
1163 {
1164 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
1165 func_clear_free(uf, FALSE);
1166 }
1167 ga_clear(&classfunctions);
1168
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001169 clear_type_list(&type_list);
1170}
1171
1172/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001173 * Find member "name" in class "cl", set "member_idx" to the member index and
1174 * return its type.
1175 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001176 */
1177 type_T *
1178class_member_type(
1179 class_T *cl,
1180 char_u *name,
1181 char_u *name_end,
1182 int *member_idx)
1183{
1184 *member_idx = -1; // not found (yet)
1185 size_t len = name_end - name;
1186
1187 for (int i = 0; i < cl->class_obj_member_count; ++i)
1188 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001189 ocmember_T *m = cl->class_obj_members + i;
1190 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001191 {
1192 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001193 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001194 }
1195 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001196
1197 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001198 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001199}
1200
1201/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001202 * Handle ":enum" up to ":endenum".
1203 */
1204 void
1205ex_enum(exarg_T *eap UNUSED)
1206{
1207 // TODO
1208}
1209
1210/*
1211 * Handle ":type".
1212 */
1213 void
1214ex_type(exarg_T *eap UNUSED)
1215{
1216 // TODO
1217}
1218
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001219/*
1220 * Evaluate what comes after a class:
1221 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001222 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001223 * - class constructor: SomeClass.new()
1224 * - object member: someObject.varname
1225 * - object method: someObject.SomeMethod()
1226 *
1227 * "*arg" points to the '.'.
1228 * "*arg" is advanced to after the member name or method call.
1229 *
1230 * Returns FAIL or OK.
1231 */
1232 int
1233class_object_index(
1234 char_u **arg,
1235 typval_T *rettv,
1236 evalarg_T *evalarg,
1237 int verbose UNUSED) // give error messages
1238{
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001239 if (VIM_ISWHITE((*arg)[1]))
1240 {
1241 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
1242 return FAIL;
1243 }
1244
1245 ++*arg;
1246 char_u *name = *arg;
1247 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1248 if (name_end == name)
1249 return FAIL;
1250 size_t len = name_end - name;
1251
Bram Moolenaar552bdca2023-02-17 21:08:50 +00001252 class_T *cl;
1253 if (rettv->v_type == VAR_CLASS)
1254 cl = rettv->vval.v_class;
1255 else // VAR_OBJECT
1256 {
1257 if (rettv->vval.v_object == NULL)
1258 {
1259 emsg(_(e_using_null_object));
1260 return FAIL;
1261 }
1262 cl = rettv->vval.v_object->obj_class;
1263 }
1264
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001265 if (*name_end == '(')
1266 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001267 int on_class = rettv->v_type == VAR_CLASS;
1268 int count = on_class ? cl->class_class_function_count
1269 : cl->class_obj_method_count;
1270 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001271 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001272 ufunc_T *fp = on_class ? cl->class_class_functions[i]
1273 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +00001274 // Use a separate pointer to avoid that ASAN complains about
1275 // uf_name[] only being 4 characters.
1276 char_u *ufname = (char_u *)fp->uf_name;
1277 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001278 {
1279 typval_T argvars[MAX_FUNC_ARGS + 1];
1280 int argcount = 0;
1281
1282 char_u *argp = name_end;
1283 int ret = get_func_arguments(&argp, evalarg, 0,
1284 argvars, &argcount);
1285 if (ret == FAIL)
1286 return FAIL;
1287
1288 funcexe_T funcexe;
1289 CLEAR_FIELD(funcexe);
1290 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001291 if (rettv->v_type == VAR_OBJECT)
1292 {
1293 funcexe.fe_object = rettv->vval.v_object;
1294 ++funcexe.fe_object->obj_refcount;
1295 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001296
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001297 // Clear the class or object after calling the function, in
1298 // case the refcount is one.
1299 typval_T tv_tofree = *rettv;
1300 rettv->v_type = VAR_UNKNOWN;
1301
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001302 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001303 int error = call_user_func_check(fp, argcount, argvars,
1304 rettv, &funcexe, NULL);
1305
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001306 // Clear the previous rettv and the arguments.
1307 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001308 for (int idx = 0; idx < argcount; ++idx)
1309 clear_tv(&argvars[idx]);
1310
1311 if (error != FCERR_NONE)
1312 {
1313 user_func_error(error, printable_func_name(fp),
1314 funcexe.fe_found_var);
1315 return FAIL;
1316 }
1317 *arg = argp;
1318 return OK;
1319 }
1320 }
1321
1322 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
1323 }
1324
1325 else if (rettv->v_type == VAR_OBJECT)
1326 {
1327 for (int i = 0; i < cl->class_obj_member_count; ++i)
1328 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001329 ocmember_T *m = &cl->class_obj_members[i];
1330 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001331 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001332 if (*name == '_')
1333 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001334 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001335 return FAIL;
1336 }
1337
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001338 // The object only contains a pointer to the class, the member
1339 // values array follows right after that.
1340 object_T *obj = rettv->vval.v_object;
1341 typval_T *tv = (typval_T *)(obj + 1) + i;
1342 copy_tv(tv, rettv);
1343 object_unref(obj);
1344
1345 *arg = name_end;
1346 return OK;
1347 }
1348 }
1349
1350 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
1351 }
1352
Bram Moolenaard505d172022-12-18 21:42:55 +00001353 else if (rettv->v_type == VAR_CLASS)
1354 {
1355 // class member
1356 for (int i = 0; i < cl->class_class_member_count; ++i)
1357 {
1358 ocmember_T *m = &cl->class_class_members[i];
1359 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
1360 {
1361 if (*name == '_')
1362 {
1363 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
1364 return FAIL;
1365 }
1366
1367 typval_T *tv = &cl->class_members_tv[i];
1368 copy_tv(tv, rettv);
1369 class_unref(cl);
1370
1371 *arg = name_end;
1372 return OK;
1373 }
1374 }
1375
1376 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
1377 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001378
1379 return FAIL;
1380}
1381
1382/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001383 * If "arg" points to a class or object method, return it.
1384 * Otherwise return NULL.
1385 */
1386 ufunc_T *
1387find_class_func(char_u **arg)
1388{
1389 char_u *name = *arg;
1390 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1391 if (name_end == name || *name_end != '.')
1392 return NULL;
1393
1394 size_t len = name_end - name;
1395 typval_T tv;
1396 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001397 if (eval_variable(name, (int)len,
1398 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001399 return NULL;
1400 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001401 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001402
1403 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1404 : tv.vval.v_object->obj_class;
1405 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001406 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001407 char_u *fname = name_end + 1;
1408 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1409 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001410 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001411 len = fname_end - fname;
1412
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001413 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1414 : cl->class_obj_method_count;
1415 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1416 : cl->class_obj_methods;
1417 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001418 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001419 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001420 // Use a separate pointer to avoid that ASAN complains about
1421 // uf_name[] only being 4 characters.
1422 char_u *ufname = (char_u *)fp->uf_name;
1423 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001424 {
1425 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001426 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001427 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001428 }
1429
Bram Moolenaareb533502022-12-14 15:06:11 +00001430fail_after_eval:
1431 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001432 return NULL;
1433}
1434
1435/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001436 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1437 * index in class.class_class_members[].
1438 * If "cl_ret" is not NULL set it to the class.
1439 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001440 */
1441 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001442class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001443{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001444 if (cctx == NULL || cctx->ctx_ufunc == NULL
1445 || cctx->ctx_ufunc->uf_class == NULL)
1446 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001447 class_T *cl = cctx->ctx_ufunc->uf_class;
1448
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001449 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001450 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001451 ocmember_T *m = &cl->class_class_members[i];
1452 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001453 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001454 if (cl_ret != NULL)
1455 *cl_ret = cl;
1456 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001457 }
1458 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001459 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001460}
1461
1462/*
Bram Moolenaar62a69232023-01-24 15:07:04 +00001463 * Return TRUE if current context "cctx_arg" is inside class "cl".
1464 * Return FALSE if not.
1465 */
1466 int
1467inside_class(cctx_T *cctx_arg, class_T *cl)
1468{
1469 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
1470 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class == cl)
1471 return TRUE;
1472 return FALSE;
1473}
1474
1475/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001476 * Make a copy of an object.
1477 */
1478 void
1479copy_object(typval_T *from, typval_T *to)
1480{
1481 *to = *from;
1482 if (to->vval.v_object != NULL)
1483 ++to->vval.v_object->obj_refcount;
1484}
1485
1486/*
1487 * Free an object.
1488 */
1489 static void
1490object_clear(object_T *obj)
1491{
1492 class_T *cl = obj->obj_class;
1493
1494 // the member values are just after the object structure
1495 typval_T *tv = (typval_T *)(obj + 1);
1496 for (int i = 0; i < cl->class_obj_member_count; ++i)
1497 clear_tv(tv + i);
1498
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001499 // Remove from the list headed by "first_object".
1500 object_cleared(obj);
1501
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001502 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001503 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001504}
1505
1506/*
1507 * Unreference an object.
1508 */
1509 void
1510object_unref(object_T *obj)
1511{
1512 if (obj != NULL && --obj->obj_refcount <= 0)
1513 object_clear(obj);
1514}
1515
1516/*
1517 * Make a copy of a class.
1518 */
1519 void
1520copy_class(typval_T *from, typval_T *to)
1521{
1522 *to = *from;
1523 if (to->vval.v_class != NULL)
1524 ++to->vval.v_class->class_refcount;
1525}
1526
1527/*
1528 * Unreference a class. Free it when the reference count goes down to zero.
1529 */
1530 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001531class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001532{
Bram Moolenaard505d172022-12-18 21:42:55 +00001533 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001534 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001535 // Freeing what the class contains may recursively come back here.
1536 // Clear "class_name" first, if it is NULL the class does not need to
1537 // be freed.
1538 VIM_CLEAR(cl->class_name);
1539
Bram Moolenaar83677162023-01-08 19:54:10 +00001540 class_unref(cl->class_extends);
1541
Bram Moolenaar94674f22023-01-06 18:42:20 +00001542 for (int i = 0; i < cl->class_interface_count; ++i)
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001543 {
Bram Moolenaar94674f22023-01-06 18:42:20 +00001544 vim_free(((char_u **)cl->class_interfaces)[i]);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001545 if (cl->class_interfaces_cl[i] != NULL)
1546 class_unref(cl->class_interfaces_cl[i]);
1547 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001548 vim_free(cl->class_interfaces);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001549 vim_free(cl->class_interfaces_cl);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001550
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001551 itf2class_T *next;
1552 for (itf2class_T *i2c = cl->class_itf2class; i2c != NULL; i2c = next)
1553 {
1554 next = i2c->i2c_next;
1555 vim_free(i2c);
1556 }
1557
Bram Moolenaard505d172022-12-18 21:42:55 +00001558 for (int i = 0; i < cl->class_class_member_count; ++i)
1559 {
1560 ocmember_T *m = &cl->class_class_members[i];
1561 vim_free(m->ocm_name);
1562 vim_free(m->ocm_init);
1563 if (cl->class_members_tv != NULL)
1564 clear_tv(&cl->class_members_tv[i]);
1565 }
1566 vim_free(cl->class_class_members);
1567 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001568
1569 for (int i = 0; i < cl->class_obj_member_count; ++i)
1570 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001571 ocmember_T *m = &cl->class_obj_members[i];
1572 vim_free(m->ocm_name);
1573 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001574 }
1575 vim_free(cl->class_obj_members);
1576
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001577 for (int i = 0; i < cl->class_class_function_count; ++i)
1578 {
1579 ufunc_T *uf = cl->class_class_functions[i];
1580 func_clear_free(uf, FALSE);
1581 }
1582 vim_free(cl->class_class_functions);
1583
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001584 for (int i = 0; i < cl->class_obj_method_count; ++i)
1585 {
1586 ufunc_T *uf = cl->class_obj_methods[i];
1587 func_clear_free(uf, FALSE);
1588 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001589 vim_free(cl->class_obj_methods);
1590
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001591 clear_type_list(&cl->class_type_list);
1592
1593 vim_free(cl);
1594 }
1595}
1596
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001597static object_T *first_object = NULL;
1598
1599/*
1600 * Call this function when an object has been created. It will be added to the
1601 * list headed by "first_object".
1602 */
1603 void
1604object_created(object_T *obj)
1605{
1606 if (first_object != NULL)
1607 {
1608 obj->obj_next_used = first_object;
1609 first_object->obj_prev_used = obj;
1610 }
1611 first_object = obj;
1612}
1613
1614/*
1615 * Call this function when an object has been cleared and is about to be freed.
1616 * It is removed from the list headed by "first_object".
1617 */
1618 void
1619object_cleared(object_T *obj)
1620{
1621 if (obj->obj_next_used != NULL)
1622 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1623 if (obj->obj_prev_used != NULL)
1624 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1625 else if (first_object == obj)
1626 first_object = obj->obj_next_used;
1627}
1628
1629/*
1630 * Go through the list of all objects and free items without "copyID".
1631 */
1632 int
1633object_free_nonref(int copyID)
1634{
1635 int did_free = FALSE;
1636 object_T *next_obj;
1637
1638 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
1639 {
1640 next_obj = obj->obj_next_used;
1641 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1642 {
1643 // Free the object and items it contains.
1644 object_clear(obj);
1645 did_free = TRUE;
1646 }
1647 }
1648
1649 return did_free;
1650}
1651
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001652
1653#endif // FEAT_EVAL