blob: 4431640d4cbeb74176dcd3272fcb8f5792d98284 [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 Moolenaar554d0312023-01-05 19:59:18 +0000233 int is_class = eap->cmdidx == CMD_class; // FALSE for :interface
234
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000235 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000236 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000237 if (is_abstract)
238 {
239 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
240 {
241 semsg(_(e_invalid_argument_str), arg);
242 return;
243 }
244 arg = skipwhite(arg + 5);
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000245 is_class = TRUE;
246 }
247
248 if (!current_script_is_vim9()
249 || (cmdmod.cmod_flags & CMOD_LEGACY)
250 || !getline_equal(eap->getline, eap->cookie, getsourceline))
251 {
252 if (is_class)
253 emsg(_(e_class_can_only_be_defined_in_vim9_script));
254 else
255 emsg(_(e_interface_can_only_be_defined_in_vim9_script));
256 return;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000257 }
258
259 if (!ASCII_ISUPPER(*arg))
260 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000261 if (is_class)
262 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
263 else
264 semsg(_(e_interface_name_must_start_with_uppercase_letter_str),
265 arg);
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000266 return;
267 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000268 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
269 if (!IS_WHITE_OR_NUL(*name_end))
270 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000271 semsg(_(e_white_space_required_after_name_str), arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000272 return;
273 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000274 char_u *name_start = arg;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000275
Bram Moolenaara86655a2023-01-12 17:06:27 +0000276 // "export class" gets used when creating the class, don't use "is_export"
277 // for the items inside the class.
278 int class_export = is_export;
279 is_export = FALSE;
280
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000281 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000282 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000283
Bram Moolenaar83677162023-01-08 19:54:10 +0000284 // Name for "extends BaseClass"
285 char_u *extends = NULL;
286
Bram Moolenaar94674f22023-01-06 18:42:20 +0000287 // Names for "implements SomeInterface"
288 garray_T ga_impl;
289 ga_init2(&ga_impl, sizeof(char_u *), 5);
290
291 arg = skipwhite(name_end);
292 while (*arg != NUL && *arg != '#' && *arg != '\n')
293 {
294 // TODO:
Bram Moolenaar94674f22023-01-06 18:42:20 +0000295 // specifies SomeInterface
Bram Moolenaar83677162023-01-08 19:54:10 +0000296 if (STRNCMP(arg, "extends", 7) == 0 && IS_WHITE_OR_NUL(arg[7]))
297 {
298 if (extends != NULL)
299 {
300 emsg(_(e_duplicate_extends));
301 goto early_ret;
302 }
303 arg = skipwhite(arg + 7);
304 char_u *end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
305 if (!IS_WHITE_OR_NUL(*end))
306 {
307 semsg(_(e_white_space_required_after_name_str), arg);
308 goto early_ret;
309 }
310 extends = vim_strnsave(arg, end - arg);
311 if (extends == NULL)
312 goto early_ret;
313
314 arg = skipwhite(end + 1);
315 }
316 else if (STRNCMP(arg, "implements", 10) == 0
317 && IS_WHITE_OR_NUL(arg[10]))
Bram Moolenaar94674f22023-01-06 18:42:20 +0000318 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000319 if (ga_impl.ga_len > 0)
320 {
321 emsg(_(e_duplicate_implements));
322 goto early_ret;
323 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000324 arg = skipwhite(arg + 10);
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000325
326 for (;;)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000327 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000328 char_u *impl_end = find_name_end(arg, NULL, NULL,
329 FNE_CHECK_START);
330 if (!IS_WHITE_OR_NUL(*impl_end) && *impl_end != ',')
331 {
332 semsg(_(e_white_space_required_after_name_str), arg);
333 goto early_ret;
334 }
335 char_u *iname = vim_strnsave(arg, impl_end - arg);
336 if (iname == NULL)
337 goto early_ret;
338 for (int i = 0; i < ga_impl.ga_len; ++i)
339 if (STRCMP(((char_u **)ga_impl.ga_data)[i], iname) == 0)
340 {
341 semsg(_(e_duplicate_interface_after_implements_str),
342 iname);
343 vim_free(iname);
344 goto early_ret;
345 }
346 if (ga_add_string(&ga_impl, iname) == FAIL)
347 {
348 vim_free(iname);
349 goto early_ret;
350 }
351 if (*impl_end != ',')
352 {
353 arg = skipwhite(impl_end);
354 break;
355 }
356 arg = skipwhite(impl_end + 1);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000357 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000358 }
359 else
360 {
361 semsg(_(e_trailing_characters_str), arg);
362early_ret:
Bram Moolenaar83677162023-01-08 19:54:10 +0000363 vim_free(extends);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000364 ga_clear_strings(&ga_impl);
365 return;
366 }
367 }
368
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000369 garray_T type_list; // list of pointers to allocated types
370 ga_init2(&type_list, sizeof(type_T *), 10);
371
Bram Moolenaard505d172022-12-18 21:42:55 +0000372 // Growarray with class members declared in the class.
373 garray_T classmembers;
374 ga_init2(&classmembers, sizeof(ocmember_T), 10);
375
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000376 // Growarray with functions declared in the class.
377 garray_T classfunctions;
378 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +0000379
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000380 // Growarray with object members declared in the class.
381 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +0000382 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000383
384 // Growarray with object methods declared in the class.
385 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000386 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000387
388 /*
Bram Moolenaar554d0312023-01-05 19:59:18 +0000389 * Go over the body of the class/interface until "endclass" or
390 * "endinterface" is found.
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000391 */
392 char_u *theline = NULL;
393 int success = FALSE;
394 for (;;)
395 {
396 vim_free(theline);
397 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
398 if (theline == NULL)
399 break;
400 char_u *line = skipwhite(theline);
401
Bram Moolenaar418b5472022-12-20 13:38:22 +0000402 // Skip empty and comment lines.
403 if (*line == NUL)
404 continue;
405 if (*line == '#')
406 {
407 if (vim9_bad_comment(line))
408 break;
409 continue;
410 }
411
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000412 char_u *p = line;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000413 char *end_name = is_class ? "endclass" : "endinterface";
414 if (checkforcmd(&p, end_name, is_class ? 4 : 5))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000415 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000416 if (STRNCMP(line, end_name, is_class ? 8 : 12) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000417 semsg(_(e_command_cannot_be_shortened_str), line);
418 else if (*p == '|' || !ends_excmd2(line, p))
419 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +0000420 else
421 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000422 break;
423 }
Bram Moolenaar554d0312023-01-05 19:59:18 +0000424 char *wrong_name = is_class ? "endinterface" : "endclass";
425 if (checkforcmd(&p, wrong_name, is_class ? 5 : 4))
426 {
Bram Moolenaar657aea72023-01-27 13:16:19 +0000427 semsg(_(e_invalid_command_str_expected_str), line, end_name);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000428 break;
429 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000430
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000431 int has_public = FALSE;
432 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000433 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000434 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000435 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000436 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000437 break;
438 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000439 has_public = TRUE;
440 p = skipwhite(line + 6);
441
Bram Moolenaard505d172022-12-18 21:42:55 +0000442 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000443 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000444 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000445 break;
446 }
447 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000448
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000449 int has_static = FALSE;
450 char_u *ps = p;
451 if (checkforcmd(&p, "static", 4))
452 {
453 if (STRNCMP(ps, "static", 6) != 0)
454 {
455 semsg(_(e_command_cannot_be_shortened_str), ps);
456 break;
457 }
458 has_static = TRUE;
459 p = skipwhite(ps + 6);
460 }
461
Bram Moolenaard505d172022-12-18 21:42:55 +0000462 // object members (public, read access, private):
463 // "this._varname"
464 // "this.varname"
465 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000466 if (STRNCMP(p, "this", 4) == 0)
467 {
468 if (p[4] != '.' || !eval_isnamec1(p[5]))
469 {
470 semsg(_(e_invalid_object_member_declaration_str), p);
471 break;
472 }
473 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +0000474 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +0000475 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +0000476 char_u *init_expr = NULL;
477 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000478 &varname_end, &type_list, &type,
479 is_class ? &init_expr: NULL) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000480 break;
481 if (add_member(&objmembers, varname, varname_end,
482 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000483 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000484 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000485 break;
486 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000487 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000488
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000489 // constructors:
490 // def new()
491 // enddef
492 // def newOther()
493 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000494 // object methods and class functions:
495 // def SomeMethod()
496 // enddef
497 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000498 // enddef
499 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000500 // def <Tval> someMethod()
501 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000502 else if (checkforcmd(&p, "def", 3))
503 {
504 exarg_T ea;
505 garray_T lines_to_free;
506
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000507 // TODO: error for "public static def Func()"?
508
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000509 CLEAR_FIELD(ea);
510 ea.cmd = line;
511 ea.arg = p;
512 ea.cmdidx = CMD_def;
513 ea.getline = eap->getline;
514 ea.cookie = eap->cookie;
515
516 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000517 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free,
518 is_class ? CF_CLASS : CF_INTERFACE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000519 ga_clear_strings(&lines_to_free);
520
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000521 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000522 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000523 char_u *name = uf->uf_name;
524 int is_new = STRNCMP(name, "new", 3) == 0;
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000525 if (is_new && is_abstract)
526 {
527 emsg(_(e_cannot_define_new_function_in_abstract_class));
528 success = FALSE;
529 break;
530 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000531 garray_T *fgap = has_static || is_new
532 ? &classfunctions : &objmethods;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000533 // Check the name isn't used already.
534 for (int i = 0; i < fgap->ga_len; ++i)
535 {
536 char_u *n = ((ufunc_T **)fgap->ga_data)[i]->uf_name;
537 if (STRCMP(name, n) == 0)
538 {
539 semsg(_(e_duplicate_function_str), name);
540 break;
541 }
542 }
543
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000544 if (ga_grow(fgap, 1) == OK)
545 {
546 if (is_new)
547 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000548
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000549 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
550 ++fgap->ga_len;
551 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000552 }
553 }
554
555 // class members
556 else if (has_static)
557 {
558 // class members (public, read access, private):
559 // "static _varname"
560 // "static varname"
561 // "public static varname"
562 char_u *varname = p;
563 char_u *varname_end = NULL;
564 type_T *type = NULL;
565 char_u *init_expr = NULL;
566 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000567 &varname_end, &type_list, &type,
568 is_class ? &init_expr : NULL) == FAIL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000569 break;
570 if (add_member(&classmembers, varname, varname_end,
571 has_public, type, init_expr) == FAIL)
572 {
573 vim_free(init_expr);
574 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000575 }
576 }
577
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000578 else
579 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000580 if (is_class)
581 semsg(_(e_not_valid_command_in_class_str), line);
582 else
583 semsg(_(e_not_valid_command_in_interface_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000584 break;
585 }
586 }
587 vim_free(theline);
588
Bram Moolenaar83677162023-01-08 19:54:10 +0000589 class_T *extends_cl = NULL; // class from "extends" argument
590
591 /*
592 * Check a few things before defining the class.
593 */
594
595 // Check the "extends" class is valid.
596 if (success && extends != NULL)
597 {
598 typval_T tv;
599 tv.v_type = VAR_UNKNOWN;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000600 if (eval_variable_import(extends, &tv) == FAIL)
Bram Moolenaar83677162023-01-08 19:54:10 +0000601 {
602 semsg(_(e_class_name_not_found_str), extends);
603 success = FALSE;
604 }
605 else
606 {
607 if (tv.v_type != VAR_CLASS
608 || tv.vval.v_class == NULL
609 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) != 0)
610 {
611 semsg(_(e_cannot_extend_str), extends);
612 success = FALSE;
613 }
614 else
615 {
616 extends_cl = tv.vval.v_class;
617 ++extends_cl->class_refcount;
618 }
619 clear_tv(&tv);
620 }
621 }
622 VIM_CLEAR(extends);
623
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000624 class_T **intf_classes = NULL;
625
Bram Moolenaar83677162023-01-08 19:54:10 +0000626 // Check all "implements" entries are valid.
Bram Moolenaar94674f22023-01-06 18:42:20 +0000627 if (success && ga_impl.ga_len > 0)
628 {
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000629 intf_classes = ALLOC_CLEAR_MULT(class_T *, ga_impl.ga_len);
630
Bram Moolenaar94674f22023-01-06 18:42:20 +0000631 for (int i = 0; i < ga_impl.ga_len && success; ++i)
632 {
633 char_u *impl = ((char_u **)ga_impl.ga_data)[i];
634 typval_T tv;
635 tv.v_type = VAR_UNKNOWN;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000636 if (eval_variable_import(impl, &tv) == FAIL)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000637 {
638 semsg(_(e_interface_name_not_found_str), impl);
639 success = FALSE;
640 break;
641 }
642
643 if (tv.v_type != VAR_CLASS
644 || tv.vval.v_class == NULL
645 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0)
646 {
647 semsg(_(e_not_valid_interface_str), impl);
648 success = FALSE;
649 }
650
Bram Moolenaar94674f22023-01-06 18:42:20 +0000651 class_T *ifcl = tv.vval.v_class;
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000652 intf_classes[i] = ifcl;
653 ++ifcl->class_refcount;
654
655 // check the members of the interface match the members of the class
Bram Moolenaar94674f22023-01-06 18:42:20 +0000656 for (int loop = 1; loop <= 2 && success; ++loop)
657 {
658 // loop == 1: check class members
659 // loop == 2: check object members
660 int if_count = loop == 1 ? ifcl->class_class_member_count
661 : ifcl->class_obj_member_count;
662 if (if_count == 0)
663 continue;
664 ocmember_T *if_ms = loop == 1 ? ifcl->class_class_members
665 : ifcl->class_obj_members;
666 ocmember_T *cl_ms = (ocmember_T *)(loop == 1
667 ? classmembers.ga_data
668 : objmembers.ga_data);
669 int cl_count = loop == 1 ? classmembers.ga_len
670 : objmembers.ga_len;
671 for (int if_i = 0; if_i < if_count; ++if_i)
672 {
673 int cl_i;
674 for (cl_i = 0; cl_i < cl_count; ++cl_i)
675 {
676 ocmember_T *m = &cl_ms[cl_i];
677 if (STRCMP(if_ms[if_i].ocm_name, m->ocm_name) == 0)
678 {
679 // TODO: check type
680 break;
681 }
682 }
683 if (cl_i == cl_count)
684 {
685 semsg(_(e_member_str_of_interface_str_not_implemented),
686 if_ms[if_i].ocm_name, impl);
687 success = FALSE;
688 break;
689 }
690 }
691 }
692
693 // check the functions/methods of the interface match the
694 // functions/methods of the class
695 for (int loop = 1; loop <= 2 && success; ++loop)
696 {
697 // loop == 1: check class functions
698 // loop == 2: check object methods
699 int if_count = loop == 1 ? ifcl->class_class_function_count
700 : ifcl->class_obj_method_count;
701 if (if_count == 0)
702 continue;
703 ufunc_T **if_fp = loop == 1 ? ifcl->class_class_functions
704 : ifcl->class_obj_methods;
705 ufunc_T **cl_fp = (ufunc_T **)(loop == 1
706 ? classfunctions.ga_data
707 : objmethods.ga_data);
708 int cl_count = loop == 1 ? classfunctions.ga_len
709 : objmethods.ga_len;
710 for (int if_i = 0; if_i < if_count; ++if_i)
711 {
712 char_u *if_name = if_fp[if_i]->uf_name;
713 int cl_i;
714 for (cl_i = 0; cl_i < cl_count; ++cl_i)
715 {
716 char_u *cl_name = cl_fp[cl_i]->uf_name;
717 if (STRCMP(if_name, cl_name) == 0)
718 {
719 // TODO: check return and argument types
720 break;
721 }
722 }
723 if (cl_i == cl_count)
724 {
725 semsg(_(e_function_str_of_interface_str_not_implemented),
726 if_name, impl);
727 success = FALSE;
728 break;
729 }
730 }
731 }
732
733 clear_tv(&tv);
734 }
735 }
736
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000737 if (success)
738 {
739 // Check no function argument name is used as an object/class member.
740 for (int loop = 1; loop <= 2 && success; ++loop)
741 {
742 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
743
744 for (int fi = 0; fi < gap->ga_len && success; ++fi)
745 {
746 ufunc_T *uf = ((ufunc_T **)gap->ga_data)[fi];
747
748 for (int i = 0; i < uf->uf_args.ga_len && success; ++i)
749 {
750 char_u *aname = ((char_u **)uf->uf_args.ga_data)[i];
751 for (int il = 1; il <= 2 && success; ++il)
752 {
753 // For a "new()" function "this.member" arguments are
754 // OK. TODO: check for the "this." prefix.
Bram Moolenaarb40a2fb2023-01-13 19:18:38 +0000755 if (STRNCMP(uf->uf_name, "new", 3) == 0 && il == 2)
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000756 continue;
757 garray_T *mgap = il == 1 ? &classmembers : &objmembers;
758 for (int mi = 0; mi < mgap->ga_len; ++mi)
759 {
760 char_u *mname = ((ocmember_T *)mgap->ga_data
761 + mi)->ocm_name;
762 if (STRCMP(aname, mname) == 0)
763 {
764 success = FALSE;
765 semsg(_(e_argument_already_declared_in_class_str),
766 aname);
767 break;
768 }
769 }
770 }
771 }
772 }
773 }
774 }
775
776
Bram Moolenaareb533502022-12-14 15:06:11 +0000777 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000778 if (success)
779 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000780 // "endclass" encountered without failures: Create the class.
781
Bram Moolenaareb533502022-12-14 15:06:11 +0000782 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000783 if (cl == NULL)
784 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000785 if (!is_class)
786 cl->class_flags = CLASS_INTERFACE;
787
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000788 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +0000789 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +0000790 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000791 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000792
Bram Moolenaard0200c82023-01-28 15:19:40 +0000793 if (extends_cl != NULL)
794 {
795 cl->class_extends = extends_cl;
796 extends_cl->class_flags |= CLASS_EXTENDED;
797 }
Bram Moolenaar83677162023-01-08 19:54:10 +0000798
Bram Moolenaard505d172022-12-18 21:42:55 +0000799 // Add class and object members to "cl".
800 if (add_members_to_class(&classmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000801 extends_cl == NULL ? NULL
802 : extends_cl->class_class_members,
803 extends_cl == NULL ? 0
804 : extends_cl->class_class_member_count,
805 &cl->class_class_members,
806 &cl->class_class_member_count) == FAIL
Bram Moolenaard505d172022-12-18 21:42:55 +0000807 || add_members_to_class(&objmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000808 extends_cl == NULL ? NULL
809 : extends_cl->class_obj_members,
810 extends_cl == NULL ? 0
811 : extends_cl->class_obj_member_count,
812 &cl->class_obj_members,
813 &cl->class_obj_member_count) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000814 goto cleanup;
815
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000816 if (ga_impl.ga_len > 0)
817 {
818 // Move the "implements" names into the class.
819 cl->class_interface_count = ga_impl.ga_len;
820 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
821 if (cl->class_interfaces == NULL)
822 goto cleanup;
823 for (int i = 0; i < ga_impl.ga_len; ++i)
824 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
825 VIM_CLEAR(ga_impl.ga_data);
826 ga_impl.ga_len = 0;
827
Bram Moolenaard0200c82023-01-28 15:19:40 +0000828 cl->class_interfaces_cl = intf_classes;
829 intf_classes = NULL;
830 }
831
832 if (cl->class_interface_count > 0 || extends_cl != NULL)
833 {
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000834 // For each interface add a lookuptable for the member index on the
835 // interface to the member index in this class.
Bram Moolenaard0200c82023-01-28 15:19:40 +0000836 // And a lookuptable for the object method index on the interface
837 // to the object method index in this class.
838 // Also do this for the extended class, if any.
839 for (int i = 0; i <= cl->class_interface_count; ++i)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000840 {
Bram Moolenaard0200c82023-01-28 15:19:40 +0000841 class_T *ifcl = i < cl->class_interface_count
842 ? cl->class_interfaces_cl[i]
843 : extends_cl;
844 if (ifcl == NULL)
845 continue;
846
847 // Table for members.
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000848 itf2class_T *if2cl = alloc_clear(sizeof(itf2class_T)
849 + ifcl->class_obj_member_count * sizeof(int));
850 if (if2cl == NULL)
851 goto cleanup;
852 if2cl->i2c_next = ifcl->class_itf2class;
853 ifcl->class_itf2class = if2cl;
854 if2cl->i2c_class = cl;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000855 if2cl->i2c_is_method = FALSE;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000856
857 for (int if_i = 0; if_i < ifcl->class_obj_member_count; ++if_i)
Bram Moolenaard0200c82023-01-28 15:19:40 +0000858 for (int cl_i = 0; cl_i < cl->class_obj_member_count;
859 ++cl_i)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000860 {
861 if (STRCMP(ifcl->class_obj_members[if_i].ocm_name,
Bram Moolenaard0200c82023-01-28 15:19:40 +0000862 cl->class_obj_members[cl_i].ocm_name) == 0)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000863 {
864 int *table = (int *)(if2cl + 1);
865 table[if_i] = cl_i;
866 break;
867 }
868 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000869
Bram Moolenaard0200c82023-01-28 15:19:40 +0000870 // Table for methods.
871 if2cl = alloc_clear(sizeof(itf2class_T)
872 + ifcl->class_obj_method_count * sizeof(int));
873 if (if2cl == NULL)
874 goto cleanup;
875 if2cl->i2c_next = ifcl->class_itf2class;
876 ifcl->class_itf2class = if2cl;
877 if2cl->i2c_class = cl;
878 if2cl->i2c_is_method = TRUE;
879
880 for (int if_i = 0; if_i < ifcl->class_obj_method_count; ++if_i)
881 {
882 int done = FALSE;
883 for (int cl_i = 0; cl_i < objmethods.ga_len; ++cl_i)
884 {
885 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
886 ((ufunc_T **)objmethods.ga_data)[cl_i]->uf_name)
887 == 0)
888 {
889 int *table = (int *)(if2cl + 1);
890 table[if_i] = cl_i;
891 done = TRUE;
892 break;
893 }
894 }
895
896 if (!done && extends_cl != NULL)
897 {
898 for (int cl_i = 0;
899 cl_i < extends_cl->class_obj_member_count; ++cl_i)
900 {
901 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
902 extends_cl->class_obj_methods[cl_i]->uf_name)
903 == 0)
904 {
905 int *table = (int *)(if2cl + 1);
906 table[if_i] = cl_i;
907 break;
908 }
909 }
910 }
911 }
912 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000913 }
914
Bram Moolenaar554d0312023-01-05 19:59:18 +0000915 if (is_class && cl->class_class_member_count > 0)
Bram Moolenaard505d172022-12-18 21:42:55 +0000916 {
917 // Allocate a typval for each class member and initialize it.
918 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
919 cl->class_class_member_count);
920 if (cl->class_members_tv != NULL)
921 for (int i = 0; i < cl->class_class_member_count; ++i)
922 {
923 ocmember_T *m = &cl->class_class_members[i];
924 typval_T *tv = &cl->class_members_tv[i];
925 if (m->ocm_init != NULL)
926 {
927 typval_T *etv = eval_expr(m->ocm_init, eap);
928 if (etv != NULL)
929 {
930 *tv = *etv;
931 vim_free(etv);
932 }
933 }
934 else
935 {
936 // TODO: proper default value
937 tv->v_type = m->ocm_type->tt_type;
938 tv->vval.v_string = NULL;
939 }
940 }
941 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000942
943 int have_new = FALSE;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000944 for (int i = 0; i < classfunctions.ga_len; ++i)
945 if (STRCMP(((ufunc_T **)classfunctions.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000946 "new") == 0)
947 {
948 have_new = TRUE;
949 break;
950 }
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000951 if (is_class && !is_abstract && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000952 {
953 // No new() method was defined, add the default constructor.
954 garray_T fga;
955 ga_init2(&fga, 1, 1000);
956 ga_concat(&fga, (char_u *)"new(");
957 for (int i = 0; i < cl->class_obj_member_count; ++i)
958 {
959 if (i > 0)
960 ga_concat(&fga, (char_u *)", ");
961 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000962 ocmember_T *m = cl->class_obj_members + i;
963 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000964 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000965 }
966 ga_concat(&fga, (char_u *)")\nenddef\n");
967 ga_append(&fga, NUL);
968
969 exarg_T fea;
970 CLEAR_FIELD(fea);
971 fea.cmdidx = CMD_def;
972 fea.cmd = fea.arg = fga.ga_data;
973
974 garray_T lines_to_free;
975 ga_init2(&lines_to_free, sizeof(char_u *), 50);
976
Bram Moolenaar2c011312023-01-07 10:51:30 +0000977 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000978
979 ga_clear_strings(&lines_to_free);
980 vim_free(fga.ga_data);
981
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000982 if (nf != NULL && ga_grow(&classfunctions, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000983 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000984 ((ufunc_T **)classfunctions.ga_data)[classfunctions.ga_len]
985 = nf;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000986 ++classfunctions.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000987
988 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000989 nf->uf_ret_type = get_type_ptr(&type_list);
990 if (nf->uf_ret_type != NULL)
991 {
992 nf->uf_ret_type->tt_type = VAR_OBJECT;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +0000993 nf->uf_ret_type->tt_class = cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000994 nf->uf_ret_type->tt_argcount = 0;
995 nf->uf_ret_type->tt_args = NULL;
996 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000997 }
998 }
999
Bram Moolenaar58b40092023-01-11 15:59:05 +00001000 // Move all the functions into the created class.
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001001 // loop 1: class functions, loop 2: object methods
1002 for (int loop = 1; loop <= 2; ++loop)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001003 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001004 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
1005 int *fcount = loop == 1 ? &cl->class_class_function_count
1006 : &cl->class_obj_method_count;
1007 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
1008 : &cl->class_obj_methods;
1009
Bram Moolenaar83677162023-01-08 19:54:10 +00001010 int parent_count = 0;
1011 if (extends_cl != NULL)
1012 // Include functions from the parent.
1013 parent_count = loop == 1
1014 ? extends_cl->class_class_function_count
1015 : extends_cl->class_obj_method_count;
1016
1017 *fcount = parent_count + gap->ga_len;
1018 if (*fcount == 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001019 {
1020 *fup = NULL;
1021 continue;
1022 }
Bram Moolenaar83677162023-01-08 19:54:10 +00001023 *fup = ALLOC_MULT(ufunc_T *, *fcount);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001024 if (*fup == NULL)
1025 goto cleanup;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001026
Bram Moolenaar58b40092023-01-11 15:59:05 +00001027 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
1028 vim_free(gap->ga_data);
1029 if (loop == 1)
1030 cl->class_class_function_count_child = gap->ga_len;
1031 else
1032 cl->class_obj_method_count_child = gap->ga_len;
1033
Bram Moolenaar83677162023-01-08 19:54:10 +00001034 int skipped = 0;
1035 for (int i = 0; i < parent_count; ++i)
1036 {
1037 // Copy functions from the parent. Can't use the same
1038 // function, because "uf_class" is different and compilation
1039 // will have a different result.
Bram Moolenaar58b40092023-01-11 15:59:05 +00001040 // Put them after the functions in the current class, object
1041 // methods may be overruled, then "super.Method()" is used to
1042 // find a method from the parent.
Bram Moolenaar83677162023-01-08 19:54:10 +00001043 // Skip "new" functions. TODO: not all of them.
1044 if (loop == 1 && STRNCMP(
1045 extends_cl->class_class_functions[i]->uf_name,
1046 "new", 3) == 0)
1047 ++skipped;
1048 else
Bram Moolenaar58b40092023-01-11 15:59:05 +00001049 {
1050 ufunc_T *pf = (loop == 1
Bram Moolenaar83677162023-01-08 19:54:10 +00001051 ? extends_cl->class_class_functions
Bram Moolenaar58b40092023-01-11 15:59:05 +00001052 : extends_cl->class_obj_methods)[i];
1053 (*fup)[gap->ga_len + i - skipped] = copy_function(pf);
1054
1055 // If the child class overrides a function from the parent
1056 // the signature must be equal.
1057 char_u *pname = pf->uf_name;
1058 for (int ci = 0; ci < gap->ga_len; ++ci)
1059 {
1060 ufunc_T *cf = (*fup)[ci];
1061 char_u *cname = cf->uf_name;
1062 if (STRCMP(pname, cname) == 0)
1063 {
1064 where_T where = WHERE_INIT;
1065 where.wt_func_name = (char *)pname;
1066 (void)check_type(pf->uf_func_type, cf->uf_func_type,
1067 TRUE, where);
1068 }
1069 }
1070 }
Bram Moolenaar83677162023-01-08 19:54:10 +00001071 }
1072
Bram Moolenaar83677162023-01-08 19:54:10 +00001073 *fcount -= skipped;
1074
1075 // Set the class pointer on all the functions and object methods.
1076 for (int i = 0; i < *fcount; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001077 {
1078 ufunc_T *fp = (*fup)[i];
1079 fp->uf_class = cl;
1080 if (loop == 2)
1081 fp->uf_flags |= FC_OBJECT;
1082 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001083 }
1084
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001085 cl->class_type.tt_type = VAR_CLASS;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001086 cl->class_type.tt_class = cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001087 cl->class_object_type.tt_type = VAR_OBJECT;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001088 cl->class_object_type.tt_class = cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001089 cl->class_type_list = type_list;
1090
1091 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +00001092 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001093
1094 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001095 // TODO: handle other context, e.g. in a function
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001096 typval_T tv;
1097 tv.v_type = VAR_CLASS;
1098 tv.vval.v_class = cl;
Bram Moolenaara86655a2023-01-12 17:06:27 +00001099 is_export = class_export;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001100 set_var_const(cl->class_name, current_sctx.sc_sid,
1101 NULL, &tv, FALSE, ASSIGN_DECL, 0);
1102 return;
1103 }
1104
1105cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +00001106 if (cl != NULL)
1107 {
1108 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001109 vim_free(cl->class_class_functions);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001110 if (cl->class_interfaces != NULL)
1111 {
1112 for (int i = 0; i < cl->class_interface_count; ++i)
1113 vim_free(cl->class_interfaces[i]);
1114 vim_free(cl->class_interfaces);
1115 }
1116 if (cl->class_interfaces_cl != NULL)
1117 {
1118 for (int i = 0; i < cl->class_interface_count; ++i)
1119 class_unref(cl->class_interfaces_cl[i]);
1120 vim_free(cl->class_interfaces_cl);
1121 }
Bram Moolenaareb533502022-12-14 15:06:11 +00001122 vim_free(cl->class_obj_members);
1123 vim_free(cl->class_obj_methods);
1124 vim_free(cl);
1125 }
1126
Bram Moolenaar83677162023-01-08 19:54:10 +00001127 vim_free(extends);
1128 class_unref(extends_cl);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001129
1130 if (intf_classes != NULL)
1131 {
1132 for (int i = 0; i < ga_impl.ga_len; ++i)
1133 class_unref(intf_classes[i]);
1134 vim_free(intf_classes);
1135 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001136 ga_clear_strings(&ga_impl);
1137
Bram Moolenaard505d172022-12-18 21:42:55 +00001138 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001139 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001140 garray_T *gap = round == 1 ? &classmembers : &objmembers;
1141 if (gap->ga_len == 0 || gap->ga_data == NULL)
1142 continue;
1143
1144 for (int i = 0; i < gap->ga_len; ++i)
1145 {
1146 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
1147 vim_free(m->ocm_name);
1148 vim_free(m->ocm_init);
1149 }
1150 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001151 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001152
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001153 for (int i = 0; i < objmethods.ga_len; ++i)
1154 {
1155 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
1156 func_clear_free(uf, FALSE);
1157 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001158 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001159
1160 for (int i = 0; i < classfunctions.ga_len; ++i)
1161 {
1162 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
1163 func_clear_free(uf, FALSE);
1164 }
1165 ga_clear(&classfunctions);
1166
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001167 clear_type_list(&type_list);
1168}
1169
1170/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001171 * Find member "name" in class "cl", set "member_idx" to the member index and
1172 * return its type.
1173 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001174 */
1175 type_T *
1176class_member_type(
1177 class_T *cl,
1178 char_u *name,
1179 char_u *name_end,
1180 int *member_idx)
1181{
1182 *member_idx = -1; // not found (yet)
1183 size_t len = name_end - name;
1184
1185 for (int i = 0; i < cl->class_obj_member_count; ++i)
1186 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001187 ocmember_T *m = cl->class_obj_members + i;
1188 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001189 {
1190 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001191 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001192 }
1193 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001194
1195 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001196 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001197}
1198
1199/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001200 * Handle ":enum" up to ":endenum".
1201 */
1202 void
1203ex_enum(exarg_T *eap UNUSED)
1204{
1205 // TODO
1206}
1207
1208/*
1209 * Handle ":type".
1210 */
1211 void
1212ex_type(exarg_T *eap UNUSED)
1213{
1214 // TODO
1215}
1216
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001217/*
1218 * Evaluate what comes after a class:
1219 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001220 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001221 * - class constructor: SomeClass.new()
1222 * - object member: someObject.varname
1223 * - object method: someObject.SomeMethod()
1224 *
1225 * "*arg" points to the '.'.
1226 * "*arg" is advanced to after the member name or method call.
1227 *
1228 * Returns FAIL or OK.
1229 */
1230 int
1231class_object_index(
1232 char_u **arg,
1233 typval_T *rettv,
1234 evalarg_T *evalarg,
1235 int verbose UNUSED) // give error messages
1236{
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001237 if (VIM_ISWHITE((*arg)[1]))
1238 {
1239 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
1240 return FAIL;
1241 }
1242
1243 ++*arg;
1244 char_u *name = *arg;
1245 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1246 if (name_end == name)
1247 return FAIL;
1248 size_t len = name_end - name;
1249
Bram Moolenaar552bdca2023-02-17 21:08:50 +00001250 class_T *cl;
1251 if (rettv->v_type == VAR_CLASS)
1252 cl = rettv->vval.v_class;
1253 else // VAR_OBJECT
1254 {
1255 if (rettv->vval.v_object == NULL)
1256 {
1257 emsg(_(e_using_null_object));
1258 return FAIL;
1259 }
1260 cl = rettv->vval.v_object->obj_class;
1261 }
1262
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001263 if (*name_end == '(')
1264 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001265 int on_class = rettv->v_type == VAR_CLASS;
1266 int count = on_class ? cl->class_class_function_count
1267 : cl->class_obj_method_count;
1268 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001269 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001270 ufunc_T *fp = on_class ? cl->class_class_functions[i]
1271 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +00001272 // Use a separate pointer to avoid that ASAN complains about
1273 // uf_name[] only being 4 characters.
1274 char_u *ufname = (char_u *)fp->uf_name;
1275 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001276 {
1277 typval_T argvars[MAX_FUNC_ARGS + 1];
1278 int argcount = 0;
1279
1280 char_u *argp = name_end;
1281 int ret = get_func_arguments(&argp, evalarg, 0,
1282 argvars, &argcount);
1283 if (ret == FAIL)
1284 return FAIL;
1285
1286 funcexe_T funcexe;
1287 CLEAR_FIELD(funcexe);
1288 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001289 if (rettv->v_type == VAR_OBJECT)
1290 {
1291 funcexe.fe_object = rettv->vval.v_object;
1292 ++funcexe.fe_object->obj_refcount;
1293 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001294
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001295 // Clear the class or object after calling the function, in
1296 // case the refcount is one.
1297 typval_T tv_tofree = *rettv;
1298 rettv->v_type = VAR_UNKNOWN;
1299
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001300 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001301 int error = call_user_func_check(fp, argcount, argvars,
1302 rettv, &funcexe, NULL);
1303
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001304 // Clear the previous rettv and the arguments.
1305 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001306 for (int idx = 0; idx < argcount; ++idx)
1307 clear_tv(&argvars[idx]);
1308
1309 if (error != FCERR_NONE)
1310 {
1311 user_func_error(error, printable_func_name(fp),
1312 funcexe.fe_found_var);
1313 return FAIL;
1314 }
1315 *arg = argp;
1316 return OK;
1317 }
1318 }
1319
1320 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
1321 }
1322
1323 else if (rettv->v_type == VAR_OBJECT)
1324 {
1325 for (int i = 0; i < cl->class_obj_member_count; ++i)
1326 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001327 ocmember_T *m = &cl->class_obj_members[i];
1328 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001329 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001330 if (*name == '_')
1331 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001332 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001333 return FAIL;
1334 }
1335
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001336 // The object only contains a pointer to the class, the member
1337 // values array follows right after that.
1338 object_T *obj = rettv->vval.v_object;
1339 typval_T *tv = (typval_T *)(obj + 1) + i;
1340 copy_tv(tv, rettv);
1341 object_unref(obj);
1342
1343 *arg = name_end;
1344 return OK;
1345 }
1346 }
1347
1348 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
1349 }
1350
Bram Moolenaard505d172022-12-18 21:42:55 +00001351 else if (rettv->v_type == VAR_CLASS)
1352 {
1353 // class member
1354 for (int i = 0; i < cl->class_class_member_count; ++i)
1355 {
1356 ocmember_T *m = &cl->class_class_members[i];
1357 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
1358 {
1359 if (*name == '_')
1360 {
1361 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
1362 return FAIL;
1363 }
1364
1365 typval_T *tv = &cl->class_members_tv[i];
1366 copy_tv(tv, rettv);
1367 class_unref(cl);
1368
1369 *arg = name_end;
1370 return OK;
1371 }
1372 }
1373
1374 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
1375 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001376
1377 return FAIL;
1378}
1379
1380/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001381 * If "arg" points to a class or object method, return it.
1382 * Otherwise return NULL.
1383 */
1384 ufunc_T *
1385find_class_func(char_u **arg)
1386{
1387 char_u *name = *arg;
1388 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1389 if (name_end == name || *name_end != '.')
1390 return NULL;
1391
1392 size_t len = name_end - name;
1393 typval_T tv;
1394 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001395 if (eval_variable(name, (int)len,
1396 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001397 return NULL;
1398 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001399 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001400
1401 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1402 : tv.vval.v_object->obj_class;
1403 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001404 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001405 char_u *fname = name_end + 1;
1406 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1407 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001408 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001409 len = fname_end - fname;
1410
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001411 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1412 : cl->class_obj_method_count;
1413 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1414 : cl->class_obj_methods;
1415 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001416 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001417 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001418 // Use a separate pointer to avoid that ASAN complains about
1419 // uf_name[] only being 4 characters.
1420 char_u *ufname = (char_u *)fp->uf_name;
1421 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001422 {
1423 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001424 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001425 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001426 }
1427
Bram Moolenaareb533502022-12-14 15:06:11 +00001428fail_after_eval:
1429 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001430 return NULL;
1431}
1432
1433/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001434 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1435 * index in class.class_class_members[].
1436 * If "cl_ret" is not NULL set it to the class.
1437 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001438 */
1439 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001440class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001441{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001442 if (cctx == NULL || cctx->ctx_ufunc == NULL
1443 || cctx->ctx_ufunc->uf_class == NULL)
1444 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001445 class_T *cl = cctx->ctx_ufunc->uf_class;
1446
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001447 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001448 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001449 ocmember_T *m = &cl->class_class_members[i];
1450 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001451 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001452 if (cl_ret != NULL)
1453 *cl_ret = cl;
1454 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001455 }
1456 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001457 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001458}
1459
1460/*
Bram Moolenaar62a69232023-01-24 15:07:04 +00001461 * Return TRUE if current context "cctx_arg" is inside class "cl".
1462 * Return FALSE if not.
1463 */
1464 int
1465inside_class(cctx_T *cctx_arg, class_T *cl)
1466{
1467 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
1468 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class == cl)
1469 return TRUE;
1470 return FALSE;
1471}
1472
1473/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001474 * Make a copy of an object.
1475 */
1476 void
1477copy_object(typval_T *from, typval_T *to)
1478{
1479 *to = *from;
1480 if (to->vval.v_object != NULL)
1481 ++to->vval.v_object->obj_refcount;
1482}
1483
1484/*
1485 * Free an object.
1486 */
1487 static void
1488object_clear(object_T *obj)
1489{
1490 class_T *cl = obj->obj_class;
1491
1492 // the member values are just after the object structure
1493 typval_T *tv = (typval_T *)(obj + 1);
1494 for (int i = 0; i < cl->class_obj_member_count; ++i)
1495 clear_tv(tv + i);
1496
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001497 // Remove from the list headed by "first_object".
1498 object_cleared(obj);
1499
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001500 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001501 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001502}
1503
1504/*
1505 * Unreference an object.
1506 */
1507 void
1508object_unref(object_T *obj)
1509{
1510 if (obj != NULL && --obj->obj_refcount <= 0)
1511 object_clear(obj);
1512}
1513
1514/*
1515 * Make a copy of a class.
1516 */
1517 void
1518copy_class(typval_T *from, typval_T *to)
1519{
1520 *to = *from;
1521 if (to->vval.v_class != NULL)
1522 ++to->vval.v_class->class_refcount;
1523}
1524
1525/*
1526 * Unreference a class. Free it when the reference count goes down to zero.
1527 */
1528 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001529class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001530{
Bram Moolenaard505d172022-12-18 21:42:55 +00001531 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001532 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001533 // Freeing what the class contains may recursively come back here.
1534 // Clear "class_name" first, if it is NULL the class does not need to
1535 // be freed.
1536 VIM_CLEAR(cl->class_name);
1537
Bram Moolenaar83677162023-01-08 19:54:10 +00001538 class_unref(cl->class_extends);
1539
Bram Moolenaar94674f22023-01-06 18:42:20 +00001540 for (int i = 0; i < cl->class_interface_count; ++i)
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001541 {
Bram Moolenaar94674f22023-01-06 18:42:20 +00001542 vim_free(((char_u **)cl->class_interfaces)[i]);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001543 if (cl->class_interfaces_cl[i] != NULL)
1544 class_unref(cl->class_interfaces_cl[i]);
1545 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001546 vim_free(cl->class_interfaces);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001547 vim_free(cl->class_interfaces_cl);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001548
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001549 itf2class_T *next;
1550 for (itf2class_T *i2c = cl->class_itf2class; i2c != NULL; i2c = next)
1551 {
1552 next = i2c->i2c_next;
1553 vim_free(i2c);
1554 }
1555
Bram Moolenaard505d172022-12-18 21:42:55 +00001556 for (int i = 0; i < cl->class_class_member_count; ++i)
1557 {
1558 ocmember_T *m = &cl->class_class_members[i];
1559 vim_free(m->ocm_name);
1560 vim_free(m->ocm_init);
1561 if (cl->class_members_tv != NULL)
1562 clear_tv(&cl->class_members_tv[i]);
1563 }
1564 vim_free(cl->class_class_members);
1565 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001566
1567 for (int i = 0; i < cl->class_obj_member_count; ++i)
1568 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001569 ocmember_T *m = &cl->class_obj_members[i];
1570 vim_free(m->ocm_name);
1571 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001572 }
1573 vim_free(cl->class_obj_members);
1574
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001575 for (int i = 0; i < cl->class_class_function_count; ++i)
1576 {
1577 ufunc_T *uf = cl->class_class_functions[i];
1578 func_clear_free(uf, FALSE);
1579 }
1580 vim_free(cl->class_class_functions);
1581
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001582 for (int i = 0; i < cl->class_obj_method_count; ++i)
1583 {
1584 ufunc_T *uf = cl->class_obj_methods[i];
1585 func_clear_free(uf, FALSE);
1586 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001587 vim_free(cl->class_obj_methods);
1588
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001589 clear_type_list(&cl->class_type_list);
1590
1591 vim_free(cl);
1592 }
1593}
1594
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001595static object_T *first_object = NULL;
1596
1597/*
1598 * Call this function when an object has been created. It will be added to the
1599 * list headed by "first_object".
1600 */
1601 void
1602object_created(object_T *obj)
1603{
1604 if (first_object != NULL)
1605 {
1606 obj->obj_next_used = first_object;
1607 first_object->obj_prev_used = obj;
1608 }
1609 first_object = obj;
1610}
1611
1612/*
1613 * Call this function when an object has been cleared and is about to be freed.
1614 * It is removed from the list headed by "first_object".
1615 */
1616 void
1617object_cleared(object_T *obj)
1618{
1619 if (obj->obj_next_used != NULL)
1620 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1621 if (obj->obj_prev_used != NULL)
1622 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1623 else if (first_object == obj)
1624 first_object = obj->obj_next_used;
1625}
1626
1627/*
1628 * Go through the list of all objects and free items without "copyID".
1629 */
1630 int
1631object_free_nonref(int copyID)
1632{
1633 int did_free = FALSE;
1634 object_T *next_obj;
1635
1636 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
1637 {
1638 next_obj = obj->obj_next_used;
1639 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1640 {
1641 // Free the object and items it contains.
1642 object_clear(obj);
1643 did_free = TRUE;
1644 }
1645 }
1646
1647 return did_free;
1648}
1649
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001650
1651#endif // FEAT_EVAL