blob: 18f36bdb4c469b71cb4e18a0f44003f6e1852c55 [file] [log] [blame]
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9class.c: Vim9 script class support
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
24/*
Bram Moolenaard505d172022-12-18 21:42:55 +000025 * Parse a member declaration, both object and class member.
26 * Returns OK or FAIL. When OK then "varname_end" is set to just after the
27 * variable name and "type_ret" is set to the decleared or detected type.
28 * "init_expr" is set to the initialisation expression (allocated), if there is
Bram Moolenaar554d0312023-01-05 19:59:18 +000029 * one. For an interface "init_expr" is NULL.
Bram Moolenaard505d172022-12-18 21:42:55 +000030 */
31 static int
32parse_member(
33 exarg_T *eap,
34 char_u *line,
35 char_u *varname,
36 int has_public, // TRUE if "public" seen before "varname"
37 char_u **varname_end,
38 garray_T *type_list,
39 type_T **type_ret,
40 char_u **init_expr)
41{
42 *varname_end = to_name_end(varname, FALSE);
43 if (*varname == '_' && has_public)
44 {
45 semsg(_(e_public_member_name_cannot_start_with_underscore_str), line);
46 return FAIL;
47 }
48
49 char_u *colon = skipwhite(*varname_end);
50 char_u *type_arg = colon;
51 type_T *type = NULL;
52 if (*colon == ':')
53 {
54 if (VIM_ISWHITE(**varname_end))
55 {
56 semsg(_(e_no_white_space_allowed_before_colon_str), varname);
57 return FAIL;
58 }
59 if (!VIM_ISWHITE(colon[1]))
60 {
61 semsg(_(e_white_space_required_after_str_str), ":", varname);
62 return FAIL;
63 }
64 type_arg = skipwhite(colon + 1);
65 type = parse_type(&type_arg, type_list, TRUE);
66 if (type == NULL)
67 return FAIL;
68 }
69
70 char_u *expr_start = skipwhite(type_arg);
71 char_u *expr_end = expr_start;
72 if (type == NULL && *expr_start != '=')
73 {
74 emsg(_(e_type_or_initialization_required));
75 return FAIL;
76 }
77
78 if (*expr_start == '=')
79 {
80 if (!VIM_ISWHITE(expr_start[-1]) || !VIM_ISWHITE(expr_start[1]))
81 {
82 semsg(_(e_white_space_required_before_and_after_str_at_str),
83 "=", type_arg);
84 return FAIL;
85 }
86 expr_start = skipwhite(expr_start + 1);
87
88 expr_end = expr_start;
89 evalarg_T evalarg;
90 fill_evalarg_from_eap(&evalarg, eap, FALSE);
91 skip_expr(&expr_end, NULL);
92
93 if (type == NULL)
94 {
95 // No type specified, use the type of the initializer.
96 typval_T tv;
97 tv.v_type = VAR_UNKNOWN;
98 char_u *expr = expr_start;
99 int res = eval0(expr, &tv, eap, &evalarg);
100
101 if (res == OK)
Bram Moolenaare83c1332023-01-02 21:04:04 +0000102 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000103 type = typval2type(&tv, get_copyID(), type_list,
104 TVTT_DO_MEMBER);
Bram Moolenaare83c1332023-01-02 21:04:04 +0000105 clear_tv(&tv);
106 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000107 if (type == NULL)
108 {
109 semsg(_(e_cannot_get_object_member_type_from_initializer_str),
110 expr_start);
111 clear_evalarg(&evalarg, NULL);
112 return FAIL;
113 }
114 }
115 clear_evalarg(&evalarg, NULL);
116 }
117 if (!valid_declaration_type(type))
118 return FAIL;
119
120 *type_ret = type;
121 if (expr_end > expr_start)
Bram Moolenaar554d0312023-01-05 19:59:18 +0000122 {
123 if (init_expr == NULL)
124 {
125 emsg(_(e_cannot_initialize_member_in_interface));
126 return FAIL;
127 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000128 *init_expr = vim_strnsave(expr_start, expr_end - expr_start);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000129 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000130 return OK;
131}
132
133/*
134 * Add a member to an object or a class.
135 * Returns OK when successful, "init_expr" will be consumed then.
136 * Returns FAIL otherwise, caller might need to free "init_expr".
137 */
138 static int
139add_member(
140 garray_T *gap,
141 char_u *varname,
142 char_u *varname_end,
143 int has_public,
144 type_T *type,
145 char_u *init_expr)
146{
147 if (ga_grow(gap, 1) == FAIL)
148 return FAIL;
149 ocmember_T *m = ((ocmember_T *)gap->ga_data) + gap->ga_len;
150 m->ocm_name = vim_strnsave(varname, varname_end - varname);
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +0000151 m->ocm_access = has_public ? VIM_ACCESS_ALL
152 : *varname == '_' ? VIM_ACCESS_PRIVATE : VIM_ACCESS_READ;
Bram Moolenaard505d172022-12-18 21:42:55 +0000153 m->ocm_type = type;
154 if (init_expr != NULL)
155 m->ocm_init = init_expr;
156 ++gap->ga_len;
157 return OK;
158}
159
160/*
161 * Move the class or object members found while parsing a class into the class.
162 * "gap" contains the found members.
Bram Moolenaar83677162023-01-08 19:54:10 +0000163 * "parent_members" points to the members in the parent class (if any)
164 * "parent_count" is the number of members in the parent class
Bram Moolenaard505d172022-12-18 21:42:55 +0000165 * "members" will be set to the newly allocated array of members and
166 * "member_count" set to the number of members.
167 * Returns OK or FAIL.
168 */
169 static int
170add_members_to_class(
171 garray_T *gap,
Bram Moolenaar83677162023-01-08 19:54:10 +0000172 ocmember_T *parent_members,
173 int parent_count,
Bram Moolenaard505d172022-12-18 21:42:55 +0000174 ocmember_T **members,
175 int *member_count)
176{
Bram Moolenaar83677162023-01-08 19:54:10 +0000177 *member_count = parent_count + gap->ga_len;
178 *members = *member_count == 0 ? NULL
179 : ALLOC_MULT(ocmember_T, *member_count);
180 if (*member_count > 0 && *members == NULL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000181 return FAIL;
Bram Moolenaar83677162023-01-08 19:54:10 +0000182 for (int i = 0; i < parent_count; ++i)
183 {
184 // parent members need to be copied
Bram Moolenaarae3205a2023-01-15 20:49:00 +0000185 ocmember_T *m = *members + i;
186 *m = parent_members[i];
187 m->ocm_name = vim_strsave(m->ocm_name);
188 if (m->ocm_init != NULL)
189 m->ocm_init = vim_strsave(m->ocm_init);
Bram Moolenaar83677162023-01-08 19:54:10 +0000190 }
Bram Moolenaar8efdcee2022-12-19 12:18:09 +0000191 if (gap->ga_len > 0)
Bram Moolenaar83677162023-01-08 19:54:10 +0000192 // new members are moved
193 mch_memmove(*members + parent_count,
194 gap->ga_data, sizeof(ocmember_T) * gap->ga_len);
Bram Moolenaard505d172022-12-18 21:42:55 +0000195 VIM_CLEAR(gap->ga_data);
196 return OK;
197}
198
199/*
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000200 * Convert a member index "idx" of interface "itf" to the member index of class
201 * "cl" implementing that interface.
202 */
203 int
Bram Moolenaard0200c82023-01-28 15:19:40 +0000204object_index_from_itf_index(class_T *itf, int is_method, int idx, class_T *cl)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000205{
Bram Moolenaard0200c82023-01-28 15:19:40 +0000206 if (idx > (is_method ? itf->class_obj_method_count
207 : itf->class_obj_member_count))
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000208 {
209 siemsg("index %d out of range for interface %s", idx, itf->class_name);
210 return 0;
211 }
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 {
h-east61378a12023-04-18 19:07:29 +0100740 // Check no function argument name is used as a class member.
741 // (Object members are always accessed with "this." prefix, so no need
742 // to check them.)
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000743 for (int loop = 1; loop <= 2 && success; ++loop)
744 {
745 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
746
747 for (int fi = 0; fi < gap->ga_len && success; ++fi)
748 {
749 ufunc_T *uf = ((ufunc_T **)gap->ga_data)[fi];
750
751 for (int i = 0; i < uf->uf_args.ga_len && success; ++i)
752 {
753 char_u *aname = ((char_u **)uf->uf_args.ga_data)[i];
h-east61378a12023-04-18 19:07:29 +0100754 garray_T *mgap = &classmembers;
755
756 for (int mi = 0; mi < mgap->ga_len; ++mi)
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000757 {
h-east61378a12023-04-18 19:07:29 +0100758 char_u *mname = ((ocmember_T *)mgap->ga_data + mi)
759 ->ocm_name;
760 if (STRCMP(aname, mname) == 0)
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000761 {
h-east61378a12023-04-18 19:07:29 +0100762 success = FALSE;
763
764 if (uf->uf_script_ctx.sc_sid > 0)
765 SOURCING_LNUM = uf->uf_script_ctx.sc_lnum;
766
767 semsg(_(e_argument_already_declared_in_class_str),
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000768 aname);
h-east61378a12023-04-18 19:07:29 +0100769 break;
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000770 }
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;
Yegappan Lakshmanan57a02cc2023-08-13 10:19:38 +0200900 cl_i < extends_cl->class_obj_method_count; ++cl_i)
Bram Moolenaard0200c82023-01-28 15:19:40 +0000901 {
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
Ernie Rael114ec812023-06-04 18:11:35 +01001028 if (gap->ga_len != 0)
1029 mch_memmove(*fup, gap->ga_data,
1030 sizeof(ufunc_T *) * gap->ga_len);
Bram Moolenaar58b40092023-01-11 15:59:05 +00001031 vim_free(gap->ga_data);
1032 if (loop == 1)
1033 cl->class_class_function_count_child = gap->ga_len;
1034 else
1035 cl->class_obj_method_count_child = gap->ga_len;
1036
Bram Moolenaar83677162023-01-08 19:54:10 +00001037 int skipped = 0;
1038 for (int i = 0; i < parent_count; ++i)
1039 {
1040 // Copy functions from the parent. Can't use the same
1041 // function, because "uf_class" is different and compilation
1042 // will have a different result.
Bram Moolenaar58b40092023-01-11 15:59:05 +00001043 // Put them after the functions in the current class, object
1044 // methods may be overruled, then "super.Method()" is used to
1045 // find a method from the parent.
Bram Moolenaar83677162023-01-08 19:54:10 +00001046 // Skip "new" functions. TODO: not all of them.
1047 if (loop == 1 && STRNCMP(
1048 extends_cl->class_class_functions[i]->uf_name,
1049 "new", 3) == 0)
1050 ++skipped;
1051 else
Bram Moolenaar58b40092023-01-11 15:59:05 +00001052 {
1053 ufunc_T *pf = (loop == 1
Bram Moolenaar83677162023-01-08 19:54:10 +00001054 ? extends_cl->class_class_functions
Bram Moolenaar58b40092023-01-11 15:59:05 +00001055 : extends_cl->class_obj_methods)[i];
1056 (*fup)[gap->ga_len + i - skipped] = copy_function(pf);
1057
1058 // If the child class overrides a function from the parent
1059 // the signature must be equal.
1060 char_u *pname = pf->uf_name;
1061 for (int ci = 0; ci < gap->ga_len; ++ci)
1062 {
1063 ufunc_T *cf = (*fup)[ci];
1064 char_u *cname = cf->uf_name;
1065 if (STRCMP(pname, cname) == 0)
1066 {
1067 where_T where = WHERE_INIT;
1068 where.wt_func_name = (char *)pname;
1069 (void)check_type(pf->uf_func_type, cf->uf_func_type,
1070 TRUE, where);
1071 }
1072 }
1073 }
Bram Moolenaar83677162023-01-08 19:54:10 +00001074 }
1075
Bram Moolenaar83677162023-01-08 19:54:10 +00001076 *fcount -= skipped;
1077
1078 // Set the class pointer on all the functions and object methods.
1079 for (int i = 0; i < *fcount; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001080 {
1081 ufunc_T *fp = (*fup)[i];
1082 fp->uf_class = cl;
1083 if (loop == 2)
1084 fp->uf_flags |= FC_OBJECT;
1085 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001086 }
1087
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001088 cl->class_type.tt_type = VAR_CLASS;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001089 cl->class_type.tt_class = cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001090 cl->class_object_type.tt_type = VAR_OBJECT;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001091 cl->class_object_type.tt_class = cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001092 cl->class_type_list = type_list;
1093
1094 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +00001095 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001096
1097 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001098 // TODO: handle other context, e.g. in a function
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001099 typval_T tv;
1100 tv.v_type = VAR_CLASS;
1101 tv.vval.v_class = cl;
Bram Moolenaara86655a2023-01-12 17:06:27 +00001102 is_export = class_export;
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001103 SOURCING_LNUM = start_lnum;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001104 set_var_const(cl->class_name, current_sctx.sc_sid,
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001105 NULL, &tv, FALSE, 0, 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001106 return;
1107 }
1108
1109cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +00001110 if (cl != NULL)
1111 {
1112 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001113 vim_free(cl->class_class_functions);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001114 if (cl->class_interfaces != NULL)
1115 {
1116 for (int i = 0; i < cl->class_interface_count; ++i)
1117 vim_free(cl->class_interfaces[i]);
1118 vim_free(cl->class_interfaces);
1119 }
1120 if (cl->class_interfaces_cl != NULL)
1121 {
1122 for (int i = 0; i < cl->class_interface_count; ++i)
1123 class_unref(cl->class_interfaces_cl[i]);
1124 vim_free(cl->class_interfaces_cl);
1125 }
Bram Moolenaareb533502022-12-14 15:06:11 +00001126 vim_free(cl->class_obj_members);
1127 vim_free(cl->class_obj_methods);
1128 vim_free(cl);
1129 }
1130
Bram Moolenaar83677162023-01-08 19:54:10 +00001131 vim_free(extends);
1132 class_unref(extends_cl);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001133
1134 if (intf_classes != NULL)
1135 {
1136 for (int i = 0; i < ga_impl.ga_len; ++i)
1137 class_unref(intf_classes[i]);
1138 vim_free(intf_classes);
1139 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001140 ga_clear_strings(&ga_impl);
1141
Bram Moolenaard505d172022-12-18 21:42:55 +00001142 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001143 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001144 garray_T *gap = round == 1 ? &classmembers : &objmembers;
1145 if (gap->ga_len == 0 || gap->ga_data == NULL)
1146 continue;
1147
1148 for (int i = 0; i < gap->ga_len; ++i)
1149 {
1150 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
1151 vim_free(m->ocm_name);
1152 vim_free(m->ocm_init);
1153 }
1154 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001155 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001156
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001157 for (int i = 0; i < objmethods.ga_len; ++i)
1158 {
1159 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
1160 func_clear_free(uf, FALSE);
1161 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001162 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001163
1164 for (int i = 0; i < classfunctions.ga_len; ++i)
1165 {
1166 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
1167 func_clear_free(uf, FALSE);
1168 }
1169 ga_clear(&classfunctions);
1170
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001171 clear_type_list(&type_list);
1172}
1173
1174/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001175 * Find member "name" in class "cl", set "member_idx" to the member index and
1176 * return its type.
1177 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001178 */
1179 type_T *
1180class_member_type(
1181 class_T *cl,
1182 char_u *name,
1183 char_u *name_end,
1184 int *member_idx)
1185{
1186 *member_idx = -1; // not found (yet)
1187 size_t len = name_end - name;
1188
1189 for (int i = 0; i < cl->class_obj_member_count; ++i)
1190 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001191 ocmember_T *m = cl->class_obj_members + i;
1192 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001193 {
1194 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001195 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001196 }
1197 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001198
1199 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001200 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001201}
1202
1203/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001204 * Handle ":enum" up to ":endenum".
1205 */
1206 void
1207ex_enum(exarg_T *eap UNUSED)
1208{
1209 // TODO
1210}
1211
1212/*
1213 * Handle ":type".
1214 */
1215 void
1216ex_type(exarg_T *eap UNUSED)
1217{
1218 // TODO
1219}
1220
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001221/*
1222 * Evaluate what comes after a class:
1223 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001224 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001225 * - class constructor: SomeClass.new()
1226 * - object member: someObject.varname
1227 * - object method: someObject.SomeMethod()
1228 *
1229 * "*arg" points to the '.'.
1230 * "*arg" is advanced to after the member name or method call.
1231 *
1232 * Returns FAIL or OK.
1233 */
1234 int
1235class_object_index(
1236 char_u **arg,
1237 typval_T *rettv,
1238 evalarg_T *evalarg,
1239 int verbose UNUSED) // give error messages
1240{
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001241 if (VIM_ISWHITE((*arg)[1]))
1242 {
1243 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
1244 return FAIL;
1245 }
1246
1247 ++*arg;
1248 char_u *name = *arg;
1249 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1250 if (name_end == name)
1251 return FAIL;
1252 size_t len = name_end - name;
1253
Bram Moolenaar552bdca2023-02-17 21:08:50 +00001254 class_T *cl;
1255 if (rettv->v_type == VAR_CLASS)
1256 cl = rettv->vval.v_class;
1257 else // VAR_OBJECT
1258 {
1259 if (rettv->vval.v_object == NULL)
1260 {
1261 emsg(_(e_using_null_object));
1262 return FAIL;
1263 }
1264 cl = rettv->vval.v_object->obj_class;
1265 }
1266
Bram Moolenaard13dd302023-03-11 20:56:35 +00001267 if (cl == NULL)
1268 {
1269 emsg(_(e_incomplete_type));
1270 return FAIL;
1271 }
1272
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001273 if (*name_end == '(')
1274 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001275 int on_class = rettv->v_type == VAR_CLASS;
1276 int count = on_class ? cl->class_class_function_count
1277 : cl->class_obj_method_count;
1278 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001279 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001280 ufunc_T *fp = on_class ? cl->class_class_functions[i]
1281 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +00001282 // Use a separate pointer to avoid that ASAN complains about
1283 // uf_name[] only being 4 characters.
1284 char_u *ufname = (char_u *)fp->uf_name;
1285 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001286 {
1287 typval_T argvars[MAX_FUNC_ARGS + 1];
1288 int argcount = 0;
1289
1290 char_u *argp = name_end;
1291 int ret = get_func_arguments(&argp, evalarg, 0,
1292 argvars, &argcount);
1293 if (ret == FAIL)
1294 return FAIL;
1295
1296 funcexe_T funcexe;
1297 CLEAR_FIELD(funcexe);
1298 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001299 if (rettv->v_type == VAR_OBJECT)
1300 {
1301 funcexe.fe_object = rettv->vval.v_object;
1302 ++funcexe.fe_object->obj_refcount;
1303 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001304
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001305 // Clear the class or object after calling the function, in
1306 // case the refcount is one.
1307 typval_T tv_tofree = *rettv;
1308 rettv->v_type = VAR_UNKNOWN;
1309
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001310 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001311 int error = call_user_func_check(fp, argcount, argvars,
1312 rettv, &funcexe, NULL);
1313
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001314 // Clear the previous rettv and the arguments.
1315 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001316 for (int idx = 0; idx < argcount; ++idx)
1317 clear_tv(&argvars[idx]);
1318
1319 if (error != FCERR_NONE)
1320 {
1321 user_func_error(error, printable_func_name(fp),
1322 funcexe.fe_found_var);
1323 return FAIL;
1324 }
1325 *arg = argp;
1326 return OK;
1327 }
1328 }
1329
1330 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
1331 }
1332
1333 else if (rettv->v_type == VAR_OBJECT)
1334 {
1335 for (int i = 0; i < cl->class_obj_member_count; ++i)
1336 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001337 ocmember_T *m = &cl->class_obj_members[i];
1338 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001339 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001340 if (*name == '_')
1341 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001342 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001343 return FAIL;
1344 }
1345
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001346 // The object only contains a pointer to the class, the member
1347 // values array follows right after that.
1348 object_T *obj = rettv->vval.v_object;
1349 typval_T *tv = (typval_T *)(obj + 1) + i;
1350 copy_tv(tv, rettv);
1351 object_unref(obj);
1352
1353 *arg = name_end;
1354 return OK;
1355 }
1356 }
1357
1358 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
1359 }
1360
Bram Moolenaard505d172022-12-18 21:42:55 +00001361 else if (rettv->v_type == VAR_CLASS)
1362 {
1363 // class member
1364 for (int i = 0; i < cl->class_class_member_count; ++i)
1365 {
1366 ocmember_T *m = &cl->class_class_members[i];
1367 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
1368 {
1369 if (*name == '_')
1370 {
1371 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
1372 return FAIL;
1373 }
1374
1375 typval_T *tv = &cl->class_members_tv[i];
1376 copy_tv(tv, rettv);
1377 class_unref(cl);
1378
1379 *arg = name_end;
1380 return OK;
1381 }
1382 }
1383
1384 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
1385 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001386
1387 return FAIL;
1388}
1389
1390/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001391 * If "arg" points to a class or object method, return it.
1392 * Otherwise return NULL.
1393 */
1394 ufunc_T *
1395find_class_func(char_u **arg)
1396{
1397 char_u *name = *arg;
1398 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1399 if (name_end == name || *name_end != '.')
1400 return NULL;
1401
1402 size_t len = name_end - name;
1403 typval_T tv;
1404 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001405 if (eval_variable(name, (int)len,
1406 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001407 return NULL;
1408 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001409 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001410
1411 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1412 : tv.vval.v_object->obj_class;
1413 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001414 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001415 char_u *fname = name_end + 1;
1416 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1417 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001418 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001419 len = fname_end - fname;
1420
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001421 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1422 : cl->class_obj_method_count;
1423 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1424 : cl->class_obj_methods;
1425 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001426 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001427 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001428 // Use a separate pointer to avoid that ASAN complains about
1429 // uf_name[] only being 4 characters.
1430 char_u *ufname = (char_u *)fp->uf_name;
1431 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001432 {
1433 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001434 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001435 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001436 }
1437
Bram Moolenaareb533502022-12-14 15:06:11 +00001438fail_after_eval:
1439 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001440 return NULL;
1441}
1442
1443/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001444 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1445 * index in class.class_class_members[].
1446 * If "cl_ret" is not NULL set it to the class.
1447 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001448 */
1449 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001450class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001451{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001452 if (cctx == NULL || cctx->ctx_ufunc == NULL
1453 || cctx->ctx_ufunc->uf_class == NULL)
1454 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001455 class_T *cl = cctx->ctx_ufunc->uf_class;
1456
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001457 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001458 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001459 ocmember_T *m = &cl->class_class_members[i];
1460 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001461 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001462 if (cl_ret != NULL)
1463 *cl_ret = cl;
1464 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001465 }
1466 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001467 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001468}
1469
1470/*
Bram Moolenaar62a69232023-01-24 15:07:04 +00001471 * Return TRUE if current context "cctx_arg" is inside class "cl".
1472 * Return FALSE if not.
1473 */
1474 int
1475inside_class(cctx_T *cctx_arg, class_T *cl)
1476{
1477 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
1478 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class == cl)
1479 return TRUE;
1480 return FALSE;
1481}
1482
1483/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001484 * Make a copy of an object.
1485 */
1486 void
1487copy_object(typval_T *from, typval_T *to)
1488{
1489 *to = *from;
1490 if (to->vval.v_object != NULL)
1491 ++to->vval.v_object->obj_refcount;
1492}
1493
1494/*
1495 * Free an object.
1496 */
1497 static void
1498object_clear(object_T *obj)
1499{
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001500 // Avoid a recursive call, it can happen if "obj" has a circular reference.
1501 obj->obj_refcount = INT_MAX;
1502
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001503 class_T *cl = obj->obj_class;
1504
1505 // the member values are just after the object structure
1506 typval_T *tv = (typval_T *)(obj + 1);
1507 for (int i = 0; i < cl->class_obj_member_count; ++i)
1508 clear_tv(tv + i);
1509
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001510 // Remove from the list headed by "first_object".
1511 object_cleared(obj);
1512
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001513 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001514 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001515}
1516
1517/*
1518 * Unreference an object.
1519 */
1520 void
1521object_unref(object_T *obj)
1522{
1523 if (obj != NULL && --obj->obj_refcount <= 0)
1524 object_clear(obj);
1525}
1526
1527/*
1528 * Make a copy of a class.
1529 */
1530 void
1531copy_class(typval_T *from, typval_T *to)
1532{
1533 *to = *from;
1534 if (to->vval.v_class != NULL)
1535 ++to->vval.v_class->class_refcount;
1536}
1537
1538/*
1539 * Unreference a class. Free it when the reference count goes down to zero.
1540 */
1541 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001542class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001543{
Bram Moolenaard505d172022-12-18 21:42:55 +00001544 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001545 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001546 // Freeing what the class contains may recursively come back here.
1547 // Clear "class_name" first, if it is NULL the class does not need to
1548 // be freed.
1549 VIM_CLEAR(cl->class_name);
1550
Bram Moolenaar83677162023-01-08 19:54:10 +00001551 class_unref(cl->class_extends);
1552
Bram Moolenaar94674f22023-01-06 18:42:20 +00001553 for (int i = 0; i < cl->class_interface_count; ++i)
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001554 {
Bram Moolenaar94674f22023-01-06 18:42:20 +00001555 vim_free(((char_u **)cl->class_interfaces)[i]);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001556 if (cl->class_interfaces_cl[i] != NULL)
1557 class_unref(cl->class_interfaces_cl[i]);
1558 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001559 vim_free(cl->class_interfaces);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001560 vim_free(cl->class_interfaces_cl);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001561
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001562 itf2class_T *next;
1563 for (itf2class_T *i2c = cl->class_itf2class; i2c != NULL; i2c = next)
1564 {
1565 next = i2c->i2c_next;
1566 vim_free(i2c);
1567 }
1568
Bram Moolenaard505d172022-12-18 21:42:55 +00001569 for (int i = 0; i < cl->class_class_member_count; ++i)
1570 {
1571 ocmember_T *m = &cl->class_class_members[i];
1572 vim_free(m->ocm_name);
1573 vim_free(m->ocm_init);
1574 if (cl->class_members_tv != NULL)
1575 clear_tv(&cl->class_members_tv[i]);
1576 }
1577 vim_free(cl->class_class_members);
1578 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001579
1580 for (int i = 0; i < cl->class_obj_member_count; ++i)
1581 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001582 ocmember_T *m = &cl->class_obj_members[i];
1583 vim_free(m->ocm_name);
1584 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001585 }
1586 vim_free(cl->class_obj_members);
1587
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001588 for (int i = 0; i < cl->class_class_function_count; ++i)
1589 {
1590 ufunc_T *uf = cl->class_class_functions[i];
1591 func_clear_free(uf, FALSE);
1592 }
1593 vim_free(cl->class_class_functions);
1594
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001595 for (int i = 0; i < cl->class_obj_method_count; ++i)
1596 {
1597 ufunc_T *uf = cl->class_obj_methods[i];
1598 func_clear_free(uf, FALSE);
1599 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001600 vim_free(cl->class_obj_methods);
1601
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001602 clear_type_list(&cl->class_type_list);
1603
1604 vim_free(cl);
1605 }
1606}
1607
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001608static object_T *first_object = NULL;
1609
1610/*
1611 * Call this function when an object has been created. It will be added to the
1612 * list headed by "first_object".
1613 */
1614 void
1615object_created(object_T *obj)
1616{
1617 if (first_object != NULL)
1618 {
1619 obj->obj_next_used = first_object;
1620 first_object->obj_prev_used = obj;
1621 }
1622 first_object = obj;
1623}
1624
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001625static object_T *next_nonref_obj = NULL;
1626
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001627/*
1628 * Call this function when an object has been cleared and is about to be freed.
1629 * It is removed from the list headed by "first_object".
1630 */
1631 void
1632object_cleared(object_T *obj)
1633{
1634 if (obj->obj_next_used != NULL)
1635 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1636 if (obj->obj_prev_used != NULL)
1637 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1638 else if (first_object == obj)
1639 first_object = obj->obj_next_used;
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001640
1641 // update the next object to check if needed
1642 if (obj == next_nonref_obj)
1643 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001644}
1645
1646/*
1647 * Go through the list of all objects and free items without "copyID".
1648 */
1649 int
1650object_free_nonref(int copyID)
1651{
1652 int did_free = FALSE;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001653
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001654 for (object_T *obj = first_object; obj != NULL; obj = next_nonref_obj)
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001655 {
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001656 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001657 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1658 {
1659 // Free the object and items it contains.
1660 object_clear(obj);
1661 did_free = TRUE;
1662 }
1663 }
1664
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001665 next_nonref_obj = NULL;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001666 return did_free;
1667}
1668
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001669
1670#endif // FEAT_EVAL