blob: 1ead953eb02b878e02d16ee1e0fc6f4205178dcf [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 Moolenaarc1c365c2022-12-04 20:13:24 +0000200 * Handle ":class" and ":abstract class" up to ":endclass".
Bram Moolenaar554d0312023-01-05 19:59:18 +0000201 * Handle ":interface" up to ":endinterface".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000202 */
203 void
204ex_class(exarg_T *eap)
205{
Bram Moolenaar554d0312023-01-05 19:59:18 +0000206 int is_class = eap->cmdidx == CMD_class; // FALSE for :interface
207
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000208 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000209 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000210 if (is_abstract)
211 {
212 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
213 {
214 semsg(_(e_invalid_argument_str), arg);
215 return;
216 }
217 arg = skipwhite(arg + 5);
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000218 is_class = TRUE;
219 }
220
221 if (!current_script_is_vim9()
222 || (cmdmod.cmod_flags & CMOD_LEGACY)
223 || !getline_equal(eap->getline, eap->cookie, getsourceline))
224 {
225 if (is_class)
226 emsg(_(e_class_can_only_be_defined_in_vim9_script));
227 else
228 emsg(_(e_interface_can_only_be_defined_in_vim9_script));
229 return;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000230 }
231
232 if (!ASCII_ISUPPER(*arg))
233 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000234 if (is_class)
235 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
236 else
237 semsg(_(e_interface_name_must_start_with_uppercase_letter_str),
238 arg);
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000239 return;
240 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000241 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
242 if (!IS_WHITE_OR_NUL(*name_end))
243 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000244 semsg(_(e_white_space_required_after_name_str), arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000245 return;
246 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000247 char_u *name_start = arg;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000248
Bram Moolenaara86655a2023-01-12 17:06:27 +0000249 // "export class" gets used when creating the class, don't use "is_export"
250 // for the items inside the class.
251 int class_export = is_export;
252 is_export = FALSE;
253
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000254 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000255 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000256
Bram Moolenaar83677162023-01-08 19:54:10 +0000257 // Name for "extends BaseClass"
258 char_u *extends = NULL;
259
Bram Moolenaar94674f22023-01-06 18:42:20 +0000260 // Names for "implements SomeInterface"
261 garray_T ga_impl;
262 ga_init2(&ga_impl, sizeof(char_u *), 5);
263
264 arg = skipwhite(name_end);
265 while (*arg != NUL && *arg != '#' && *arg != '\n')
266 {
267 // TODO:
Bram Moolenaar94674f22023-01-06 18:42:20 +0000268 // specifies SomeInterface
Bram Moolenaar83677162023-01-08 19:54:10 +0000269 if (STRNCMP(arg, "extends", 7) == 0 && IS_WHITE_OR_NUL(arg[7]))
270 {
271 if (extends != NULL)
272 {
273 emsg(_(e_duplicate_extends));
274 goto early_ret;
275 }
276 arg = skipwhite(arg + 7);
277 char_u *end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
278 if (!IS_WHITE_OR_NUL(*end))
279 {
280 semsg(_(e_white_space_required_after_name_str), arg);
281 goto early_ret;
282 }
283 extends = vim_strnsave(arg, end - arg);
284 if (extends == NULL)
285 goto early_ret;
286
287 arg = skipwhite(end + 1);
288 }
289 else if (STRNCMP(arg, "implements", 10) == 0
290 && IS_WHITE_OR_NUL(arg[10]))
Bram Moolenaar94674f22023-01-06 18:42:20 +0000291 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000292 if (ga_impl.ga_len > 0)
293 {
294 emsg(_(e_duplicate_implements));
295 goto early_ret;
296 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000297 arg = skipwhite(arg + 10);
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000298
299 for (;;)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000300 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000301 char_u *impl_end = find_name_end(arg, NULL, NULL,
302 FNE_CHECK_START);
303 if (!IS_WHITE_OR_NUL(*impl_end) && *impl_end != ',')
304 {
305 semsg(_(e_white_space_required_after_name_str), arg);
306 goto early_ret;
307 }
308 char_u *iname = vim_strnsave(arg, impl_end - arg);
309 if (iname == NULL)
310 goto early_ret;
311 for (int i = 0; i < ga_impl.ga_len; ++i)
312 if (STRCMP(((char_u **)ga_impl.ga_data)[i], iname) == 0)
313 {
314 semsg(_(e_duplicate_interface_after_implements_str),
315 iname);
316 vim_free(iname);
317 goto early_ret;
318 }
319 if (ga_add_string(&ga_impl, iname) == FAIL)
320 {
321 vim_free(iname);
322 goto early_ret;
323 }
324 if (*impl_end != ',')
325 {
326 arg = skipwhite(impl_end);
327 break;
328 }
329 arg = skipwhite(impl_end + 1);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000330 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000331 }
332 else
333 {
334 semsg(_(e_trailing_characters_str), arg);
335early_ret:
Bram Moolenaar83677162023-01-08 19:54:10 +0000336 vim_free(extends);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000337 ga_clear_strings(&ga_impl);
338 return;
339 }
340 }
341
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000342 garray_T type_list; // list of pointers to allocated types
343 ga_init2(&type_list, sizeof(type_T *), 10);
344
Bram Moolenaard505d172022-12-18 21:42:55 +0000345 // Growarray with class members declared in the class.
346 garray_T classmembers;
347 ga_init2(&classmembers, sizeof(ocmember_T), 10);
348
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000349 // Growarray with functions declared in the class.
350 garray_T classfunctions;
351 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +0000352
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000353 // Growarray with object members declared in the class.
354 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +0000355 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000356
357 // Growarray with object methods declared in the class.
358 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000359 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000360
361 /*
Bram Moolenaar554d0312023-01-05 19:59:18 +0000362 * Go over the body of the class/interface until "endclass" or
363 * "endinterface" is found.
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000364 */
365 char_u *theline = NULL;
366 int success = FALSE;
367 for (;;)
368 {
369 vim_free(theline);
370 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
371 if (theline == NULL)
372 break;
373 char_u *line = skipwhite(theline);
374
Bram Moolenaar418b5472022-12-20 13:38:22 +0000375 // Skip empty and comment lines.
376 if (*line == NUL)
377 continue;
378 if (*line == '#')
379 {
380 if (vim9_bad_comment(line))
381 break;
382 continue;
383 }
384
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000385 char_u *p = line;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000386 char *end_name = is_class ? "endclass" : "endinterface";
387 if (checkforcmd(&p, end_name, is_class ? 4 : 5))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000388 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000389 if (STRNCMP(line, end_name, is_class ? 8 : 12) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000390 semsg(_(e_command_cannot_be_shortened_str), line);
391 else if (*p == '|' || !ends_excmd2(line, p))
392 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +0000393 else
394 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000395 break;
396 }
Bram Moolenaar554d0312023-01-05 19:59:18 +0000397 char *wrong_name = is_class ? "endinterface" : "endclass";
398 if (checkforcmd(&p, wrong_name, is_class ? 5 : 4))
399 {
400 semsg(_(e_invalid_command_str), line);
401 break;
402 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000403
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000404 int has_public = FALSE;
405 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000406 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000407 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000408 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000409 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000410 break;
411 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000412 has_public = TRUE;
413 p = skipwhite(line + 6);
414
Bram Moolenaard505d172022-12-18 21:42:55 +0000415 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000416 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000417 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000418 break;
419 }
420 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000421
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000422 int has_static = FALSE;
423 char_u *ps = p;
424 if (checkforcmd(&p, "static", 4))
425 {
426 if (STRNCMP(ps, "static", 6) != 0)
427 {
428 semsg(_(e_command_cannot_be_shortened_str), ps);
429 break;
430 }
431 has_static = TRUE;
432 p = skipwhite(ps + 6);
433 }
434
Bram Moolenaard505d172022-12-18 21:42:55 +0000435 // object members (public, read access, private):
436 // "this._varname"
437 // "this.varname"
438 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000439 if (STRNCMP(p, "this", 4) == 0)
440 {
441 if (p[4] != '.' || !eval_isnamec1(p[5]))
442 {
443 semsg(_(e_invalid_object_member_declaration_str), p);
444 break;
445 }
446 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +0000447 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +0000448 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +0000449 char_u *init_expr = NULL;
450 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000451 &varname_end, &type_list, &type,
452 is_class ? &init_expr: NULL) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000453 break;
454 if (add_member(&objmembers, varname, varname_end,
455 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000456 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000457 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000458 break;
459 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000460 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000461
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000462 // constructors:
463 // def new()
464 // enddef
465 // def newOther()
466 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000467 // object methods and class functions:
468 // def SomeMethod()
469 // enddef
470 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000471 // enddef
472 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000473 // def <Tval> someMethod()
474 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000475 else if (checkforcmd(&p, "def", 3))
476 {
477 exarg_T ea;
478 garray_T lines_to_free;
479
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000480 // TODO: error for "public static def Func()"?
481
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000482 CLEAR_FIELD(ea);
483 ea.cmd = line;
484 ea.arg = p;
485 ea.cmdidx = CMD_def;
486 ea.getline = eap->getline;
487 ea.cookie = eap->cookie;
488
489 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000490 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free,
491 is_class ? CF_CLASS : CF_INTERFACE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000492 ga_clear_strings(&lines_to_free);
493
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000494 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000495 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000496 char_u *name = uf->uf_name;
497 int is_new = STRNCMP(name, "new", 3) == 0;
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000498 if (is_new && is_abstract)
499 {
500 emsg(_(e_cannot_define_new_function_in_abstract_class));
501 success = FALSE;
502 break;
503 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000504 garray_T *fgap = has_static || is_new
505 ? &classfunctions : &objmethods;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000506 // Check the name isn't used already.
507 for (int i = 0; i < fgap->ga_len; ++i)
508 {
509 char_u *n = ((ufunc_T **)fgap->ga_data)[i]->uf_name;
510 if (STRCMP(name, n) == 0)
511 {
512 semsg(_(e_duplicate_function_str), name);
513 break;
514 }
515 }
516
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000517 if (ga_grow(fgap, 1) == OK)
518 {
519 if (is_new)
520 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000521
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000522 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
523 ++fgap->ga_len;
524 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000525 }
526 }
527
528 // class members
529 else if (has_static)
530 {
531 // class members (public, read access, private):
532 // "static _varname"
533 // "static varname"
534 // "public static varname"
535 char_u *varname = p;
536 char_u *varname_end = NULL;
537 type_T *type = NULL;
538 char_u *init_expr = NULL;
539 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000540 &varname_end, &type_list, &type,
541 is_class ? &init_expr : NULL) == FAIL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000542 break;
543 if (add_member(&classmembers, varname, varname_end,
544 has_public, type, init_expr) == FAIL)
545 {
546 vim_free(init_expr);
547 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000548 }
549 }
550
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000551 else
552 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000553 if (is_class)
554 semsg(_(e_not_valid_command_in_class_str), line);
555 else
556 semsg(_(e_not_valid_command_in_interface_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000557 break;
558 }
559 }
560 vim_free(theline);
561
Bram Moolenaar83677162023-01-08 19:54:10 +0000562 class_T *extends_cl = NULL; // class from "extends" argument
563
564 /*
565 * Check a few things before defining the class.
566 */
567
568 // Check the "extends" class is valid.
569 if (success && extends != NULL)
570 {
571 typval_T tv;
572 tv.v_type = VAR_UNKNOWN;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000573 if (eval_variable_import(extends, &tv) == FAIL)
Bram Moolenaar83677162023-01-08 19:54:10 +0000574 {
575 semsg(_(e_class_name_not_found_str), extends);
576 success = FALSE;
577 }
578 else
579 {
580 if (tv.v_type != VAR_CLASS
581 || tv.vval.v_class == NULL
582 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) != 0)
583 {
584 semsg(_(e_cannot_extend_str), extends);
585 success = FALSE;
586 }
587 else
588 {
589 extends_cl = tv.vval.v_class;
590 ++extends_cl->class_refcount;
591 }
592 clear_tv(&tv);
593 }
594 }
595 VIM_CLEAR(extends);
596
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000597 class_T **intf_classes = NULL;
598
Bram Moolenaar83677162023-01-08 19:54:10 +0000599 // Check all "implements" entries are valid.
Bram Moolenaar94674f22023-01-06 18:42:20 +0000600 if (success && ga_impl.ga_len > 0)
601 {
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000602 intf_classes = ALLOC_CLEAR_MULT(class_T *, ga_impl.ga_len);
603
Bram Moolenaar94674f22023-01-06 18:42:20 +0000604 for (int i = 0; i < ga_impl.ga_len && success; ++i)
605 {
606 char_u *impl = ((char_u **)ga_impl.ga_data)[i];
607 typval_T tv;
608 tv.v_type = VAR_UNKNOWN;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000609 if (eval_variable_import(impl, &tv) == FAIL)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000610 {
611 semsg(_(e_interface_name_not_found_str), impl);
612 success = FALSE;
613 break;
614 }
615
616 if (tv.v_type != VAR_CLASS
617 || tv.vval.v_class == NULL
618 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0)
619 {
620 semsg(_(e_not_valid_interface_str), impl);
621 success = FALSE;
622 }
623
Bram Moolenaar94674f22023-01-06 18:42:20 +0000624 class_T *ifcl = tv.vval.v_class;
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000625 intf_classes[i] = ifcl;
626 ++ifcl->class_refcount;
627
628 // check the members of the interface match the members of the class
Bram Moolenaar94674f22023-01-06 18:42:20 +0000629 for (int loop = 1; loop <= 2 && success; ++loop)
630 {
631 // loop == 1: check class members
632 // loop == 2: check object members
633 int if_count = loop == 1 ? ifcl->class_class_member_count
634 : ifcl->class_obj_member_count;
635 if (if_count == 0)
636 continue;
637 ocmember_T *if_ms = loop == 1 ? ifcl->class_class_members
638 : ifcl->class_obj_members;
639 ocmember_T *cl_ms = (ocmember_T *)(loop == 1
640 ? classmembers.ga_data
641 : objmembers.ga_data);
642 int cl_count = loop == 1 ? classmembers.ga_len
643 : objmembers.ga_len;
644 for (int if_i = 0; if_i < if_count; ++if_i)
645 {
646 int cl_i;
647 for (cl_i = 0; cl_i < cl_count; ++cl_i)
648 {
649 ocmember_T *m = &cl_ms[cl_i];
650 if (STRCMP(if_ms[if_i].ocm_name, m->ocm_name) == 0)
651 {
652 // TODO: check type
653 break;
654 }
655 }
656 if (cl_i == cl_count)
657 {
658 semsg(_(e_member_str_of_interface_str_not_implemented),
659 if_ms[if_i].ocm_name, impl);
660 success = FALSE;
661 break;
662 }
663 }
664 }
665
666 // check the functions/methods of the interface match the
667 // functions/methods of the class
668 for (int loop = 1; loop <= 2 && success; ++loop)
669 {
670 // loop == 1: check class functions
671 // loop == 2: check object methods
672 int if_count = loop == 1 ? ifcl->class_class_function_count
673 : ifcl->class_obj_method_count;
674 if (if_count == 0)
675 continue;
676 ufunc_T **if_fp = loop == 1 ? ifcl->class_class_functions
677 : ifcl->class_obj_methods;
678 ufunc_T **cl_fp = (ufunc_T **)(loop == 1
679 ? classfunctions.ga_data
680 : objmethods.ga_data);
681 int cl_count = loop == 1 ? classfunctions.ga_len
682 : objmethods.ga_len;
683 for (int if_i = 0; if_i < if_count; ++if_i)
684 {
685 char_u *if_name = if_fp[if_i]->uf_name;
686 int cl_i;
687 for (cl_i = 0; cl_i < cl_count; ++cl_i)
688 {
689 char_u *cl_name = cl_fp[cl_i]->uf_name;
690 if (STRCMP(if_name, cl_name) == 0)
691 {
692 // TODO: check return and argument types
693 break;
694 }
695 }
696 if (cl_i == cl_count)
697 {
698 semsg(_(e_function_str_of_interface_str_not_implemented),
699 if_name, impl);
700 success = FALSE;
701 break;
702 }
703 }
704 }
705
706 clear_tv(&tv);
707 }
708 }
709
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000710 if (success)
711 {
712 // Check no function argument name is used as an object/class member.
713 for (int loop = 1; loop <= 2 && success; ++loop)
714 {
715 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
716
717 for (int fi = 0; fi < gap->ga_len && success; ++fi)
718 {
719 ufunc_T *uf = ((ufunc_T **)gap->ga_data)[fi];
720
721 for (int i = 0; i < uf->uf_args.ga_len && success; ++i)
722 {
723 char_u *aname = ((char_u **)uf->uf_args.ga_data)[i];
724 for (int il = 1; il <= 2 && success; ++il)
725 {
726 // For a "new()" function "this.member" arguments are
727 // OK. TODO: check for the "this." prefix.
Bram Moolenaarb40a2fb2023-01-13 19:18:38 +0000728 if (STRNCMP(uf->uf_name, "new", 3) == 0 && il == 2)
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000729 continue;
730 garray_T *mgap = il == 1 ? &classmembers : &objmembers;
731 for (int mi = 0; mi < mgap->ga_len; ++mi)
732 {
733 char_u *mname = ((ocmember_T *)mgap->ga_data
734 + mi)->ocm_name;
735 if (STRCMP(aname, mname) == 0)
736 {
737 success = FALSE;
738 semsg(_(e_argument_already_declared_in_class_str),
739 aname);
740 break;
741 }
742 }
743 }
744 }
745 }
746 }
747 }
748
749
Bram Moolenaareb533502022-12-14 15:06:11 +0000750 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000751 if (success)
752 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000753 // "endclass" encountered without failures: Create the class.
754
Bram Moolenaareb533502022-12-14 15:06:11 +0000755 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000756 if (cl == NULL)
757 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000758 if (!is_class)
759 cl->class_flags = CLASS_INTERFACE;
760
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000761 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +0000762 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +0000763 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000764 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000765
Bram Moolenaar83677162023-01-08 19:54:10 +0000766 cl->class_extends = extends_cl;
767
Bram Moolenaar94674f22023-01-06 18:42:20 +0000768 if (ga_impl.ga_len > 0)
769 {
770 // Move the "implements" names into the class.
771 cl->class_interface_count = ga_impl.ga_len;
772 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
773 if (cl->class_interfaces == NULL)
774 goto cleanup;
775 for (int i = 0; i < ga_impl.ga_len; ++i)
776 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
Bram Moolenaar7d4d87b2023-01-06 18:59:08 +0000777 VIM_CLEAR(ga_impl.ga_data);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000778 ga_impl.ga_len = 0;
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000779
780 cl->class_interfaces_cl = intf_classes;
781 intf_classes = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +0000782 }
783
Bram Moolenaard505d172022-12-18 21:42:55 +0000784 // Add class and object members to "cl".
785 if (add_members_to_class(&classmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000786 extends_cl == NULL ? NULL
787 : extends_cl->class_class_members,
788 extends_cl == NULL ? 0
789 : extends_cl->class_class_member_count,
790 &cl->class_class_members,
791 &cl->class_class_member_count) == FAIL
Bram Moolenaard505d172022-12-18 21:42:55 +0000792 || add_members_to_class(&objmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000793 extends_cl == NULL ? NULL
794 : extends_cl->class_obj_members,
795 extends_cl == NULL ? 0
796 : extends_cl->class_obj_member_count,
797 &cl->class_obj_members,
798 &cl->class_obj_member_count) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000799 goto cleanup;
800
Bram Moolenaar554d0312023-01-05 19:59:18 +0000801 if (is_class && cl->class_class_member_count > 0)
Bram Moolenaard505d172022-12-18 21:42:55 +0000802 {
803 // Allocate a typval for each class member and initialize it.
804 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
805 cl->class_class_member_count);
806 if (cl->class_members_tv != NULL)
807 for (int i = 0; i < cl->class_class_member_count; ++i)
808 {
809 ocmember_T *m = &cl->class_class_members[i];
810 typval_T *tv = &cl->class_members_tv[i];
811 if (m->ocm_init != NULL)
812 {
813 typval_T *etv = eval_expr(m->ocm_init, eap);
814 if (etv != NULL)
815 {
816 *tv = *etv;
817 vim_free(etv);
818 }
819 }
820 else
821 {
822 // TODO: proper default value
823 tv->v_type = m->ocm_type->tt_type;
824 tv->vval.v_string = NULL;
825 }
826 }
827 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000828
829 int have_new = FALSE;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000830 for (int i = 0; i < classfunctions.ga_len; ++i)
831 if (STRCMP(((ufunc_T **)classfunctions.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000832 "new") == 0)
833 {
834 have_new = TRUE;
835 break;
836 }
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000837 if (is_class && !is_abstract && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000838 {
839 // No new() method was defined, add the default constructor.
840 garray_T fga;
841 ga_init2(&fga, 1, 1000);
842 ga_concat(&fga, (char_u *)"new(");
843 for (int i = 0; i < cl->class_obj_member_count; ++i)
844 {
845 if (i > 0)
846 ga_concat(&fga, (char_u *)", ");
847 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000848 ocmember_T *m = cl->class_obj_members + i;
849 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000850 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000851 }
852 ga_concat(&fga, (char_u *)")\nenddef\n");
853 ga_append(&fga, NUL);
854
855 exarg_T fea;
856 CLEAR_FIELD(fea);
857 fea.cmdidx = CMD_def;
858 fea.cmd = fea.arg = fga.ga_data;
859
860 garray_T lines_to_free;
861 ga_init2(&lines_to_free, sizeof(char_u *), 50);
862
Bram Moolenaar2c011312023-01-07 10:51:30 +0000863 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000864
865 ga_clear_strings(&lines_to_free);
866 vim_free(fga.ga_data);
867
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000868 if (nf != NULL && ga_grow(&classfunctions, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000869 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000870 ((ufunc_T **)classfunctions.ga_data)[classfunctions.ga_len]
871 = nf;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000872 ++classfunctions.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000873
874 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000875 nf->uf_ret_type = get_type_ptr(&type_list);
876 if (nf->uf_ret_type != NULL)
877 {
878 nf->uf_ret_type->tt_type = VAR_OBJECT;
879 nf->uf_ret_type->tt_member = (type_T *)cl;
880 nf->uf_ret_type->tt_argcount = 0;
881 nf->uf_ret_type->tt_args = NULL;
882 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000883 }
884 }
885
Bram Moolenaar58b40092023-01-11 15:59:05 +0000886 // Move all the functions into the created class.
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000887 // loop 1: class functions, loop 2: object methods
888 for (int loop = 1; loop <= 2; ++loop)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000889 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000890 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
891 int *fcount = loop == 1 ? &cl->class_class_function_count
892 : &cl->class_obj_method_count;
893 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
894 : &cl->class_obj_methods;
895
Bram Moolenaar83677162023-01-08 19:54:10 +0000896 int parent_count = 0;
897 if (extends_cl != NULL)
898 // Include functions from the parent.
899 parent_count = loop == 1
900 ? extends_cl->class_class_function_count
901 : extends_cl->class_obj_method_count;
902
903 *fcount = parent_count + gap->ga_len;
904 if (*fcount == 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000905 {
906 *fup = NULL;
907 continue;
908 }
Bram Moolenaar83677162023-01-08 19:54:10 +0000909 *fup = ALLOC_MULT(ufunc_T *, *fcount);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000910 if (*fup == NULL)
911 goto cleanup;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000912
Bram Moolenaar58b40092023-01-11 15:59:05 +0000913 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
914 vim_free(gap->ga_data);
915 if (loop == 1)
916 cl->class_class_function_count_child = gap->ga_len;
917 else
918 cl->class_obj_method_count_child = gap->ga_len;
919
Bram Moolenaar83677162023-01-08 19:54:10 +0000920 int skipped = 0;
921 for (int i = 0; i < parent_count; ++i)
922 {
923 // Copy functions from the parent. Can't use the same
924 // function, because "uf_class" is different and compilation
925 // will have a different result.
Bram Moolenaar58b40092023-01-11 15:59:05 +0000926 // Put them after the functions in the current class, object
927 // methods may be overruled, then "super.Method()" is used to
928 // find a method from the parent.
Bram Moolenaar83677162023-01-08 19:54:10 +0000929 // Skip "new" functions. TODO: not all of them.
930 if (loop == 1 && STRNCMP(
931 extends_cl->class_class_functions[i]->uf_name,
932 "new", 3) == 0)
933 ++skipped;
934 else
Bram Moolenaar58b40092023-01-11 15:59:05 +0000935 {
936 ufunc_T *pf = (loop == 1
Bram Moolenaar83677162023-01-08 19:54:10 +0000937 ? extends_cl->class_class_functions
Bram Moolenaar58b40092023-01-11 15:59:05 +0000938 : extends_cl->class_obj_methods)[i];
939 (*fup)[gap->ga_len + i - skipped] = copy_function(pf);
940
941 // If the child class overrides a function from the parent
942 // the signature must be equal.
943 char_u *pname = pf->uf_name;
944 for (int ci = 0; ci < gap->ga_len; ++ci)
945 {
946 ufunc_T *cf = (*fup)[ci];
947 char_u *cname = cf->uf_name;
948 if (STRCMP(pname, cname) == 0)
949 {
950 where_T where = WHERE_INIT;
951 where.wt_func_name = (char *)pname;
952 (void)check_type(pf->uf_func_type, cf->uf_func_type,
953 TRUE, where);
954 }
955 }
956 }
Bram Moolenaar83677162023-01-08 19:54:10 +0000957 }
958
Bram Moolenaar83677162023-01-08 19:54:10 +0000959 *fcount -= skipped;
960
961 // Set the class pointer on all the functions and object methods.
962 for (int i = 0; i < *fcount; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000963 {
964 ufunc_T *fp = (*fup)[i];
965 fp->uf_class = cl;
966 if (loop == 2)
967 fp->uf_flags |= FC_OBJECT;
968 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000969 }
970
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000971 cl->class_type.tt_type = VAR_CLASS;
972 cl->class_type.tt_member = (type_T *)cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000973 cl->class_object_type.tt_type = VAR_OBJECT;
974 cl->class_object_type.tt_member = (type_T *)cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000975 cl->class_type_list = type_list;
976
977 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +0000978 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000979
980 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +0000981 // TODO: handle other context, e.g. in a function
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000982 typval_T tv;
983 tv.v_type = VAR_CLASS;
984 tv.vval.v_class = cl;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000985 is_export = class_export;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000986 set_var_const(cl->class_name, current_sctx.sc_sid,
987 NULL, &tv, FALSE, ASSIGN_DECL, 0);
988 return;
989 }
990
991cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +0000992 if (cl != NULL)
993 {
994 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000995 vim_free(cl->class_class_functions);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000996 if (cl->class_interfaces != NULL)
997 {
998 for (int i = 0; i < cl->class_interface_count; ++i)
999 vim_free(cl->class_interfaces[i]);
1000 vim_free(cl->class_interfaces);
1001 }
1002 if (cl->class_interfaces_cl != NULL)
1003 {
1004 for (int i = 0; i < cl->class_interface_count; ++i)
1005 class_unref(cl->class_interfaces_cl[i]);
1006 vim_free(cl->class_interfaces_cl);
1007 }
Bram Moolenaareb533502022-12-14 15:06:11 +00001008 vim_free(cl->class_obj_members);
1009 vim_free(cl->class_obj_methods);
1010 vim_free(cl);
1011 }
1012
Bram Moolenaar83677162023-01-08 19:54:10 +00001013 vim_free(extends);
1014 class_unref(extends_cl);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001015
1016 if (intf_classes != NULL)
1017 {
1018 for (int i = 0; i < ga_impl.ga_len; ++i)
1019 class_unref(intf_classes[i]);
1020 vim_free(intf_classes);
1021 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001022 ga_clear_strings(&ga_impl);
1023
Bram Moolenaard505d172022-12-18 21:42:55 +00001024 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001025 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001026 garray_T *gap = round == 1 ? &classmembers : &objmembers;
1027 if (gap->ga_len == 0 || gap->ga_data == NULL)
1028 continue;
1029
1030 for (int i = 0; i < gap->ga_len; ++i)
1031 {
1032 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
1033 vim_free(m->ocm_name);
1034 vim_free(m->ocm_init);
1035 }
1036 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001037 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001038
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001039 for (int i = 0; i < objmethods.ga_len; ++i)
1040 {
1041 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
1042 func_clear_free(uf, FALSE);
1043 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001044 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001045
1046 for (int i = 0; i < classfunctions.ga_len; ++i)
1047 {
1048 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
1049 func_clear_free(uf, FALSE);
1050 }
1051 ga_clear(&classfunctions);
1052
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001053 clear_type_list(&type_list);
1054}
1055
1056/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001057 * Find member "name" in class "cl", set "member_idx" to the member index and
1058 * return its type.
1059 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001060 */
1061 type_T *
1062class_member_type(
1063 class_T *cl,
1064 char_u *name,
1065 char_u *name_end,
1066 int *member_idx)
1067{
1068 *member_idx = -1; // not found (yet)
1069 size_t len = name_end - name;
1070
1071 for (int i = 0; i < cl->class_obj_member_count; ++i)
1072 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001073 ocmember_T *m = cl->class_obj_members + i;
1074 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001075 {
1076 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001077 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001078 }
1079 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001080
1081 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001082 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001083}
1084
1085/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001086 * Handle ":enum" up to ":endenum".
1087 */
1088 void
1089ex_enum(exarg_T *eap UNUSED)
1090{
1091 // TODO
1092}
1093
1094/*
1095 * Handle ":type".
1096 */
1097 void
1098ex_type(exarg_T *eap UNUSED)
1099{
1100 // TODO
1101}
1102
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001103/*
1104 * Evaluate what comes after a class:
1105 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001106 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001107 * - class constructor: SomeClass.new()
1108 * - object member: someObject.varname
1109 * - object method: someObject.SomeMethod()
1110 *
1111 * "*arg" points to the '.'.
1112 * "*arg" is advanced to after the member name or method call.
1113 *
1114 * Returns FAIL or OK.
1115 */
1116 int
1117class_object_index(
1118 char_u **arg,
1119 typval_T *rettv,
1120 evalarg_T *evalarg,
1121 int verbose UNUSED) // give error messages
1122{
1123 // int evaluate = evalarg != NULL
1124 // && (evalarg->eval_flags & EVAL_EVALUATE);
1125
1126 if (VIM_ISWHITE((*arg)[1]))
1127 {
1128 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
1129 return FAIL;
1130 }
1131
1132 ++*arg;
1133 char_u *name = *arg;
1134 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1135 if (name_end == name)
1136 return FAIL;
1137 size_t len = name_end - name;
1138
1139 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
1140 : rettv->vval.v_object->obj_class;
1141 if (*name_end == '(')
1142 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001143 int on_class = rettv->v_type == VAR_CLASS;
1144 int count = on_class ? cl->class_class_function_count
1145 : cl->class_obj_method_count;
1146 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001147 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001148 ufunc_T *fp = on_class ? cl->class_class_functions[i]
1149 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +00001150 // Use a separate pointer to avoid that ASAN complains about
1151 // uf_name[] only being 4 characters.
1152 char_u *ufname = (char_u *)fp->uf_name;
1153 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001154 {
1155 typval_T argvars[MAX_FUNC_ARGS + 1];
1156 int argcount = 0;
1157
1158 char_u *argp = name_end;
1159 int ret = get_func_arguments(&argp, evalarg, 0,
1160 argvars, &argcount);
1161 if (ret == FAIL)
1162 return FAIL;
1163
1164 funcexe_T funcexe;
1165 CLEAR_FIELD(funcexe);
1166 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001167 if (rettv->v_type == VAR_OBJECT)
1168 {
1169 funcexe.fe_object = rettv->vval.v_object;
1170 ++funcexe.fe_object->obj_refcount;
1171 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001172
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001173 // Clear the class or object after calling the function, in
1174 // case the refcount is one.
1175 typval_T tv_tofree = *rettv;
1176 rettv->v_type = VAR_UNKNOWN;
1177
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001178 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001179 int error = call_user_func_check(fp, argcount, argvars,
1180 rettv, &funcexe, NULL);
1181
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001182 // Clear the previous rettv and the arguments.
1183 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001184 for (int idx = 0; idx < argcount; ++idx)
1185 clear_tv(&argvars[idx]);
1186
1187 if (error != FCERR_NONE)
1188 {
1189 user_func_error(error, printable_func_name(fp),
1190 funcexe.fe_found_var);
1191 return FAIL;
1192 }
1193 *arg = argp;
1194 return OK;
1195 }
1196 }
1197
1198 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
1199 }
1200
1201 else if (rettv->v_type == VAR_OBJECT)
1202 {
1203 for (int i = 0; i < cl->class_obj_member_count; ++i)
1204 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001205 ocmember_T *m = &cl->class_obj_members[i];
1206 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001207 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001208 if (*name == '_')
1209 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001210 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001211 return FAIL;
1212 }
1213
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001214 // The object only contains a pointer to the class, the member
1215 // values array follows right after that.
1216 object_T *obj = rettv->vval.v_object;
1217 typval_T *tv = (typval_T *)(obj + 1) + i;
1218 copy_tv(tv, rettv);
1219 object_unref(obj);
1220
1221 *arg = name_end;
1222 return OK;
1223 }
1224 }
1225
1226 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
1227 }
1228
Bram Moolenaard505d172022-12-18 21:42:55 +00001229 else if (rettv->v_type == VAR_CLASS)
1230 {
1231 // class member
1232 for (int i = 0; i < cl->class_class_member_count; ++i)
1233 {
1234 ocmember_T *m = &cl->class_class_members[i];
1235 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
1236 {
1237 if (*name == '_')
1238 {
1239 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
1240 return FAIL;
1241 }
1242
1243 typval_T *tv = &cl->class_members_tv[i];
1244 copy_tv(tv, rettv);
1245 class_unref(cl);
1246
1247 *arg = name_end;
1248 return OK;
1249 }
1250 }
1251
1252 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
1253 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001254
1255 return FAIL;
1256}
1257
1258/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001259 * If "arg" points to a class or object method, return it.
1260 * Otherwise return NULL.
1261 */
1262 ufunc_T *
1263find_class_func(char_u **arg)
1264{
1265 char_u *name = *arg;
1266 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1267 if (name_end == name || *name_end != '.')
1268 return NULL;
1269
1270 size_t len = name_end - name;
1271 typval_T tv;
1272 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001273 if (eval_variable(name, (int)len,
1274 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001275 return NULL;
1276 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001277 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001278
1279 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1280 : tv.vval.v_object->obj_class;
1281 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001282 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001283 char_u *fname = name_end + 1;
1284 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1285 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001286 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001287 len = fname_end - fname;
1288
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001289 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1290 : cl->class_obj_method_count;
1291 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1292 : cl->class_obj_methods;
1293 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001294 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001295 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001296 // Use a separate pointer to avoid that ASAN complains about
1297 // uf_name[] only being 4 characters.
1298 char_u *ufname = (char_u *)fp->uf_name;
1299 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001300 {
1301 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001302 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001303 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001304 }
1305
Bram Moolenaareb533502022-12-14 15:06:11 +00001306fail_after_eval:
1307 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001308 return NULL;
1309}
1310
1311/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001312 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1313 * index in class.class_class_members[].
1314 * If "cl_ret" is not NULL set it to the class.
1315 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001316 */
1317 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001318class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001319{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001320 if (cctx == NULL || cctx->ctx_ufunc == NULL
1321 || cctx->ctx_ufunc->uf_class == NULL)
1322 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001323 class_T *cl = cctx->ctx_ufunc->uf_class;
1324
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001325 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001326 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001327 ocmember_T *m = &cl->class_class_members[i];
1328 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001329 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001330 if (cl_ret != NULL)
1331 *cl_ret = cl;
1332 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001333 }
1334 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001335 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001336}
1337
1338/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001339 * Make a copy of an object.
1340 */
1341 void
1342copy_object(typval_T *from, typval_T *to)
1343{
1344 *to = *from;
1345 if (to->vval.v_object != NULL)
1346 ++to->vval.v_object->obj_refcount;
1347}
1348
1349/*
1350 * Free an object.
1351 */
1352 static void
1353object_clear(object_T *obj)
1354{
1355 class_T *cl = obj->obj_class;
1356
1357 // the member values are just after the object structure
1358 typval_T *tv = (typval_T *)(obj + 1);
1359 for (int i = 0; i < cl->class_obj_member_count; ++i)
1360 clear_tv(tv + i);
1361
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001362 // Remove from the list headed by "first_object".
1363 object_cleared(obj);
1364
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001365 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001366 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001367}
1368
1369/*
1370 * Unreference an object.
1371 */
1372 void
1373object_unref(object_T *obj)
1374{
1375 if (obj != NULL && --obj->obj_refcount <= 0)
1376 object_clear(obj);
1377}
1378
1379/*
1380 * Make a copy of a class.
1381 */
1382 void
1383copy_class(typval_T *from, typval_T *to)
1384{
1385 *to = *from;
1386 if (to->vval.v_class != NULL)
1387 ++to->vval.v_class->class_refcount;
1388}
1389
1390/*
1391 * Unreference a class. Free it when the reference count goes down to zero.
1392 */
1393 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001394class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001395{
Bram Moolenaard505d172022-12-18 21:42:55 +00001396 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001397 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001398 // Freeing what the class contains may recursively come back here.
1399 // Clear "class_name" first, if it is NULL the class does not need to
1400 // be freed.
1401 VIM_CLEAR(cl->class_name);
1402
Bram Moolenaar83677162023-01-08 19:54:10 +00001403 class_unref(cl->class_extends);
1404
Bram Moolenaar94674f22023-01-06 18:42:20 +00001405 for (int i = 0; i < cl->class_interface_count; ++i)
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001406 {
Bram Moolenaar94674f22023-01-06 18:42:20 +00001407 vim_free(((char_u **)cl->class_interfaces)[i]);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001408 if (cl->class_interfaces_cl[i] != NULL)
1409 class_unref(cl->class_interfaces_cl[i]);
1410 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001411 vim_free(cl->class_interfaces);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001412 vim_free(cl->class_interfaces_cl);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001413
Bram Moolenaard505d172022-12-18 21:42:55 +00001414 for (int i = 0; i < cl->class_class_member_count; ++i)
1415 {
1416 ocmember_T *m = &cl->class_class_members[i];
1417 vim_free(m->ocm_name);
1418 vim_free(m->ocm_init);
1419 if (cl->class_members_tv != NULL)
1420 clear_tv(&cl->class_members_tv[i]);
1421 }
1422 vim_free(cl->class_class_members);
1423 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001424
1425 for (int i = 0; i < cl->class_obj_member_count; ++i)
1426 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001427 ocmember_T *m = &cl->class_obj_members[i];
1428 vim_free(m->ocm_name);
1429 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001430 }
1431 vim_free(cl->class_obj_members);
1432
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001433 for (int i = 0; i < cl->class_class_function_count; ++i)
1434 {
1435 ufunc_T *uf = cl->class_class_functions[i];
1436 func_clear_free(uf, FALSE);
1437 }
1438 vim_free(cl->class_class_functions);
1439
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001440 for (int i = 0; i < cl->class_obj_method_count; ++i)
1441 {
1442 ufunc_T *uf = cl->class_obj_methods[i];
1443 func_clear_free(uf, FALSE);
1444 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001445 vim_free(cl->class_obj_methods);
1446
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001447 clear_type_list(&cl->class_type_list);
1448
1449 vim_free(cl);
1450 }
1451}
1452
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001453static object_T *first_object = NULL;
1454
1455/*
1456 * Call this function when an object has been created. It will be added to the
1457 * list headed by "first_object".
1458 */
1459 void
1460object_created(object_T *obj)
1461{
1462 if (first_object != NULL)
1463 {
1464 obj->obj_next_used = first_object;
1465 first_object->obj_prev_used = obj;
1466 }
1467 first_object = obj;
1468}
1469
1470/*
1471 * Call this function when an object has been cleared and is about to be freed.
1472 * It is removed from the list headed by "first_object".
1473 */
1474 void
1475object_cleared(object_T *obj)
1476{
1477 if (obj->obj_next_used != NULL)
1478 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1479 if (obj->obj_prev_used != NULL)
1480 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1481 else if (first_object == obj)
1482 first_object = obj->obj_next_used;
1483}
1484
1485/*
1486 * Go through the list of all objects and free items without "copyID".
1487 */
1488 int
1489object_free_nonref(int copyID)
1490{
1491 int did_free = FALSE;
1492 object_T *next_obj;
1493
1494 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
1495 {
1496 next_obj = obj->obj_next_used;
1497 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1498 {
1499 // Free the object and items it contains.
1500 object_clear(obj);
1501 did_free = TRUE;
1502 }
1503 }
1504
1505 return did_free;
1506}
1507
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001508
1509#endif // FEAT_EVAL