blob: a1466be87d50721a052affe328cf9c49c0912085 [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
185 *members[i] = parent_members[i];
186 members[i]->ocm_name = vim_strsave(members[i]->ocm_name);
187 if (members[i]->ocm_init != NULL)
188 members[i]->ocm_init = vim_strsave(members[i]->ocm_init);
189 }
Bram Moolenaar8efdcee2022-12-19 12:18:09 +0000190 if (gap->ga_len > 0)
Bram Moolenaar83677162023-01-08 19:54:10 +0000191 // new members are moved
192 mch_memmove(*members + parent_count,
193 gap->ga_data, sizeof(ocmember_T) * gap->ga_len);
Bram Moolenaard505d172022-12-18 21:42:55 +0000194 VIM_CLEAR(gap->ga_data);
195 return OK;
196}
197
198/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000199 * Handle ":class" and ":abstract class" up to ":endclass".
Bram Moolenaar554d0312023-01-05 19:59:18 +0000200 * Handle ":interface" up to ":endinterface".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000201 */
202 void
203ex_class(exarg_T *eap)
204{
Bram Moolenaar554d0312023-01-05 19:59:18 +0000205 int is_class = eap->cmdidx == CMD_class; // FALSE for :interface
206
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000207 if (!current_script_is_vim9()
208 || (cmdmod.cmod_flags & CMOD_LEGACY)
209 || !getline_equal(eap->getline, eap->cookie, getsourceline))
210 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000211 if (is_class)
212 emsg(_(e_class_can_only_be_defined_in_vim9_script));
213 else
214 emsg(_(e_interface_can_only_be_defined_in_vim9_script));
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000215 return;
216 }
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000217
218 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000219 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000220 if (is_abstract)
221 {
222 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
223 {
224 semsg(_(e_invalid_argument_str), arg);
225 return;
226 }
227 arg = skipwhite(arg + 5);
228 }
229
230 if (!ASCII_ISUPPER(*arg))
231 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000232 if (is_class)
233 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
234 else
235 semsg(_(e_interface_name_must_start_with_uppercase_letter_str),
236 arg);
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000237 return;
238 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000239 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
240 if (!IS_WHITE_OR_NUL(*name_end))
241 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000242 semsg(_(e_white_space_required_after_name_str), arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000243 return;
244 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000245 char_u *name_start = arg;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000246
Bram Moolenaara86655a2023-01-12 17:06:27 +0000247 // "export class" gets used when creating the class, don't use "is_export"
248 // for the items inside the class.
249 int class_export = is_export;
250 is_export = FALSE;
251
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000252 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000253 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000254
Bram Moolenaar83677162023-01-08 19:54:10 +0000255 // Name for "extends BaseClass"
256 char_u *extends = NULL;
257
Bram Moolenaar94674f22023-01-06 18:42:20 +0000258 // Names for "implements SomeInterface"
259 garray_T ga_impl;
260 ga_init2(&ga_impl, sizeof(char_u *), 5);
261
262 arg = skipwhite(name_end);
263 while (*arg != NUL && *arg != '#' && *arg != '\n')
264 {
265 // TODO:
Bram Moolenaar94674f22023-01-06 18:42:20 +0000266 // specifies SomeInterface
Bram Moolenaar83677162023-01-08 19:54:10 +0000267 if (STRNCMP(arg, "extends", 7) == 0 && IS_WHITE_OR_NUL(arg[7]))
268 {
269 if (extends != NULL)
270 {
271 emsg(_(e_duplicate_extends));
272 goto early_ret;
273 }
274 arg = skipwhite(arg + 7);
275 char_u *end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
276 if (!IS_WHITE_OR_NUL(*end))
277 {
278 semsg(_(e_white_space_required_after_name_str), arg);
279 goto early_ret;
280 }
281 extends = vim_strnsave(arg, end - arg);
282 if (extends == NULL)
283 goto early_ret;
284
285 arg = skipwhite(end + 1);
286 }
287 else if (STRNCMP(arg, "implements", 10) == 0
288 && IS_WHITE_OR_NUL(arg[10]))
Bram Moolenaar94674f22023-01-06 18:42:20 +0000289 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000290 if (ga_impl.ga_len > 0)
291 {
292 emsg(_(e_duplicate_implements));
293 goto early_ret;
294 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000295 arg = skipwhite(arg + 10);
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000296
297 for (;;)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000298 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000299 char_u *impl_end = find_name_end(arg, NULL, NULL,
300 FNE_CHECK_START);
301 if (!IS_WHITE_OR_NUL(*impl_end) && *impl_end != ',')
302 {
303 semsg(_(e_white_space_required_after_name_str), arg);
304 goto early_ret;
305 }
306 char_u *iname = vim_strnsave(arg, impl_end - arg);
307 if (iname == NULL)
308 goto early_ret;
309 for (int i = 0; i < ga_impl.ga_len; ++i)
310 if (STRCMP(((char_u **)ga_impl.ga_data)[i], iname) == 0)
311 {
312 semsg(_(e_duplicate_interface_after_implements_str),
313 iname);
314 vim_free(iname);
315 goto early_ret;
316 }
317 if (ga_add_string(&ga_impl, iname) == FAIL)
318 {
319 vim_free(iname);
320 goto early_ret;
321 }
322 if (*impl_end != ',')
323 {
324 arg = skipwhite(impl_end);
325 break;
326 }
327 arg = skipwhite(impl_end + 1);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000328 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000329 }
330 else
331 {
332 semsg(_(e_trailing_characters_str), arg);
333early_ret:
Bram Moolenaar83677162023-01-08 19:54:10 +0000334 vim_free(extends);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000335 ga_clear_strings(&ga_impl);
336 return;
337 }
338 }
339
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000340 garray_T type_list; // list of pointers to allocated types
341 ga_init2(&type_list, sizeof(type_T *), 10);
342
Bram Moolenaard505d172022-12-18 21:42:55 +0000343 // Growarray with class members declared in the class.
344 garray_T classmembers;
345 ga_init2(&classmembers, sizeof(ocmember_T), 10);
346
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000347 // Growarray with functions declared in the class.
348 garray_T classfunctions;
349 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +0000350
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000351 // Growarray with object members declared in the class.
352 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +0000353 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000354
355 // Growarray with object methods declared in the class.
356 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000357 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000358
359 /*
Bram Moolenaar554d0312023-01-05 19:59:18 +0000360 * Go over the body of the class/interface until "endclass" or
361 * "endinterface" is found.
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000362 */
363 char_u *theline = NULL;
364 int success = FALSE;
365 for (;;)
366 {
367 vim_free(theline);
368 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
369 if (theline == NULL)
370 break;
371 char_u *line = skipwhite(theline);
372
Bram Moolenaar418b5472022-12-20 13:38:22 +0000373 // Skip empty and comment lines.
374 if (*line == NUL)
375 continue;
376 if (*line == '#')
377 {
378 if (vim9_bad_comment(line))
379 break;
380 continue;
381 }
382
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000383 char_u *p = line;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000384 char *end_name = is_class ? "endclass" : "endinterface";
385 if (checkforcmd(&p, end_name, is_class ? 4 : 5))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000386 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000387 if (STRNCMP(line, end_name, is_class ? 8 : 12) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000388 semsg(_(e_command_cannot_be_shortened_str), line);
389 else if (*p == '|' || !ends_excmd2(line, p))
390 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +0000391 else
392 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000393 break;
394 }
Bram Moolenaar554d0312023-01-05 19:59:18 +0000395 char *wrong_name = is_class ? "endinterface" : "endclass";
396 if (checkforcmd(&p, wrong_name, is_class ? 5 : 4))
397 {
398 semsg(_(e_invalid_command_str), line);
399 break;
400 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000401
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000402 int has_public = FALSE;
403 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000404 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000405 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000406 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000407 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000408 break;
409 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000410 has_public = TRUE;
411 p = skipwhite(line + 6);
412
Bram Moolenaard505d172022-12-18 21:42:55 +0000413 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000414 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000415 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000416 break;
417 }
418 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000419
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000420 int has_static = FALSE;
421 char_u *ps = p;
422 if (checkforcmd(&p, "static", 4))
423 {
424 if (STRNCMP(ps, "static", 6) != 0)
425 {
426 semsg(_(e_command_cannot_be_shortened_str), ps);
427 break;
428 }
429 has_static = TRUE;
430 p = skipwhite(ps + 6);
431 }
432
Bram Moolenaard505d172022-12-18 21:42:55 +0000433 // object members (public, read access, private):
434 // "this._varname"
435 // "this.varname"
436 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000437 if (STRNCMP(p, "this", 4) == 0)
438 {
439 if (p[4] != '.' || !eval_isnamec1(p[5]))
440 {
441 semsg(_(e_invalid_object_member_declaration_str), p);
442 break;
443 }
444 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +0000445 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +0000446 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +0000447 char_u *init_expr = NULL;
448 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000449 &varname_end, &type_list, &type,
450 is_class ? &init_expr: NULL) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000451 break;
452 if (add_member(&objmembers, varname, varname_end,
453 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000454 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000455 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000456 break;
457 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000458 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000459
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000460 // constructors:
461 // def new()
462 // enddef
463 // def newOther()
464 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000465 // object methods and class functions:
466 // def SomeMethod()
467 // enddef
468 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000469 // enddef
470 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000471 // def <Tval> someMethod()
472 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000473 else if (checkforcmd(&p, "def", 3))
474 {
475 exarg_T ea;
476 garray_T lines_to_free;
477
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000478 // TODO: error for "public static def Func()"?
479
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000480 CLEAR_FIELD(ea);
481 ea.cmd = line;
482 ea.arg = p;
483 ea.cmdidx = CMD_def;
484 ea.getline = eap->getline;
485 ea.cookie = eap->cookie;
486
487 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000488 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free,
489 is_class ? CF_CLASS : CF_INTERFACE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000490 ga_clear_strings(&lines_to_free);
491
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000492 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000493 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000494 char_u *name = uf->uf_name;
495 int is_new = STRNCMP(name, "new", 3) == 0;
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000496 garray_T *fgap = has_static || is_new
497 ? &classfunctions : &objmethods;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000498 // Check the name isn't used already.
499 for (int i = 0; i < fgap->ga_len; ++i)
500 {
501 char_u *n = ((ufunc_T **)fgap->ga_data)[i]->uf_name;
502 if (STRCMP(name, n) == 0)
503 {
504 semsg(_(e_duplicate_function_str), name);
505 break;
506 }
507 }
508
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000509 if (ga_grow(fgap, 1) == OK)
510 {
511 if (is_new)
512 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000513
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000514 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
515 ++fgap->ga_len;
516 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000517 }
518 }
519
520 // class members
521 else if (has_static)
522 {
523 // class members (public, read access, private):
524 // "static _varname"
525 // "static varname"
526 // "public static varname"
527 char_u *varname = p;
528 char_u *varname_end = NULL;
529 type_T *type = NULL;
530 char_u *init_expr = NULL;
531 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000532 &varname_end, &type_list, &type,
533 is_class ? &init_expr : NULL) == FAIL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000534 break;
535 if (add_member(&classmembers, varname, varname_end,
536 has_public, type, init_expr) == FAIL)
537 {
538 vim_free(init_expr);
539 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000540 }
541 }
542
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000543 else
544 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000545 if (is_class)
546 semsg(_(e_not_valid_command_in_class_str), line);
547 else
548 semsg(_(e_not_valid_command_in_interface_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000549 break;
550 }
551 }
552 vim_free(theline);
553
Bram Moolenaar83677162023-01-08 19:54:10 +0000554 class_T *extends_cl = NULL; // class from "extends" argument
555
556 /*
557 * Check a few things before defining the class.
558 */
559
560 // Check the "extends" class is valid.
561 if (success && extends != NULL)
562 {
563 typval_T tv;
564 tv.v_type = VAR_UNKNOWN;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000565 if (eval_variable_import(extends, &tv) == FAIL)
Bram Moolenaar83677162023-01-08 19:54:10 +0000566 {
567 semsg(_(e_class_name_not_found_str), extends);
568 success = FALSE;
569 }
570 else
571 {
572 if (tv.v_type != VAR_CLASS
573 || tv.vval.v_class == NULL
574 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) != 0)
575 {
576 semsg(_(e_cannot_extend_str), extends);
577 success = FALSE;
578 }
579 else
580 {
581 extends_cl = tv.vval.v_class;
582 ++extends_cl->class_refcount;
583 }
584 clear_tv(&tv);
585 }
586 }
587 VIM_CLEAR(extends);
588
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000589 class_T **intf_classes = NULL;
590
Bram Moolenaar83677162023-01-08 19:54:10 +0000591 // Check all "implements" entries are valid.
Bram Moolenaar94674f22023-01-06 18:42:20 +0000592 if (success && ga_impl.ga_len > 0)
593 {
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000594 intf_classes = ALLOC_CLEAR_MULT(class_T *, ga_impl.ga_len);
595
Bram Moolenaar94674f22023-01-06 18:42:20 +0000596 for (int i = 0; i < ga_impl.ga_len && success; ++i)
597 {
598 char_u *impl = ((char_u **)ga_impl.ga_data)[i];
599 typval_T tv;
600 tv.v_type = VAR_UNKNOWN;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000601 if (eval_variable_import(impl, &tv) == FAIL)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000602 {
603 semsg(_(e_interface_name_not_found_str), impl);
604 success = FALSE;
605 break;
606 }
607
608 if (tv.v_type != VAR_CLASS
609 || tv.vval.v_class == NULL
610 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0)
611 {
612 semsg(_(e_not_valid_interface_str), impl);
613 success = FALSE;
614 }
615
Bram Moolenaar94674f22023-01-06 18:42:20 +0000616 class_T *ifcl = tv.vval.v_class;
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000617 intf_classes[i] = ifcl;
618 ++ifcl->class_refcount;
619
620 // check the members of the interface match the members of the class
Bram Moolenaar94674f22023-01-06 18:42:20 +0000621 for (int loop = 1; loop <= 2 && success; ++loop)
622 {
623 // loop == 1: check class members
624 // loop == 2: check object members
625 int if_count = loop == 1 ? ifcl->class_class_member_count
626 : ifcl->class_obj_member_count;
627 if (if_count == 0)
628 continue;
629 ocmember_T *if_ms = loop == 1 ? ifcl->class_class_members
630 : ifcl->class_obj_members;
631 ocmember_T *cl_ms = (ocmember_T *)(loop == 1
632 ? classmembers.ga_data
633 : objmembers.ga_data);
634 int cl_count = loop == 1 ? classmembers.ga_len
635 : objmembers.ga_len;
636 for (int if_i = 0; if_i < if_count; ++if_i)
637 {
638 int cl_i;
639 for (cl_i = 0; cl_i < cl_count; ++cl_i)
640 {
641 ocmember_T *m = &cl_ms[cl_i];
642 if (STRCMP(if_ms[if_i].ocm_name, m->ocm_name) == 0)
643 {
644 // TODO: check type
645 break;
646 }
647 }
648 if (cl_i == cl_count)
649 {
650 semsg(_(e_member_str_of_interface_str_not_implemented),
651 if_ms[if_i].ocm_name, impl);
652 success = FALSE;
653 break;
654 }
655 }
656 }
657
658 // check the functions/methods of the interface match the
659 // functions/methods of the class
660 for (int loop = 1; loop <= 2 && success; ++loop)
661 {
662 // loop == 1: check class functions
663 // loop == 2: check object methods
664 int if_count = loop == 1 ? ifcl->class_class_function_count
665 : ifcl->class_obj_method_count;
666 if (if_count == 0)
667 continue;
668 ufunc_T **if_fp = loop == 1 ? ifcl->class_class_functions
669 : ifcl->class_obj_methods;
670 ufunc_T **cl_fp = (ufunc_T **)(loop == 1
671 ? classfunctions.ga_data
672 : objmethods.ga_data);
673 int cl_count = loop == 1 ? classfunctions.ga_len
674 : objmethods.ga_len;
675 for (int if_i = 0; if_i < if_count; ++if_i)
676 {
677 char_u *if_name = if_fp[if_i]->uf_name;
678 int cl_i;
679 for (cl_i = 0; cl_i < cl_count; ++cl_i)
680 {
681 char_u *cl_name = cl_fp[cl_i]->uf_name;
682 if (STRCMP(if_name, cl_name) == 0)
683 {
684 // TODO: check return and argument types
685 break;
686 }
687 }
688 if (cl_i == cl_count)
689 {
690 semsg(_(e_function_str_of_interface_str_not_implemented),
691 if_name, impl);
692 success = FALSE;
693 break;
694 }
695 }
696 }
697
698 clear_tv(&tv);
699 }
700 }
701
Bram Moolenaareb533502022-12-14 15:06:11 +0000702 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000703 if (success)
704 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000705 // "endclass" encountered without failures: Create the class.
706
Bram Moolenaareb533502022-12-14 15:06:11 +0000707 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000708 if (cl == NULL)
709 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000710 if (!is_class)
711 cl->class_flags = CLASS_INTERFACE;
712
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000713 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +0000714 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +0000715 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000716 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000717
Bram Moolenaar83677162023-01-08 19:54:10 +0000718 cl->class_extends = extends_cl;
719
Bram Moolenaar94674f22023-01-06 18:42:20 +0000720 if (ga_impl.ga_len > 0)
721 {
722 // Move the "implements" names into the class.
723 cl->class_interface_count = ga_impl.ga_len;
724 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
725 if (cl->class_interfaces == NULL)
726 goto cleanup;
727 for (int i = 0; i < ga_impl.ga_len; ++i)
728 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
Bram Moolenaar7d4d87b2023-01-06 18:59:08 +0000729 VIM_CLEAR(ga_impl.ga_data);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000730 ga_impl.ga_len = 0;
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000731
732 cl->class_interfaces_cl = intf_classes;
733 intf_classes = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +0000734 }
735
Bram Moolenaard505d172022-12-18 21:42:55 +0000736 // Add class and object members to "cl".
737 if (add_members_to_class(&classmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000738 extends_cl == NULL ? NULL
739 : extends_cl->class_class_members,
740 extends_cl == NULL ? 0
741 : extends_cl->class_class_member_count,
742 &cl->class_class_members,
743 &cl->class_class_member_count) == FAIL
Bram Moolenaard505d172022-12-18 21:42:55 +0000744 || add_members_to_class(&objmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000745 extends_cl == NULL ? NULL
746 : extends_cl->class_obj_members,
747 extends_cl == NULL ? 0
748 : extends_cl->class_obj_member_count,
749 &cl->class_obj_members,
750 &cl->class_obj_member_count) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000751 goto cleanup;
752
Bram Moolenaar554d0312023-01-05 19:59:18 +0000753 if (is_class && cl->class_class_member_count > 0)
Bram Moolenaard505d172022-12-18 21:42:55 +0000754 {
755 // Allocate a typval for each class member and initialize it.
756 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
757 cl->class_class_member_count);
758 if (cl->class_members_tv != NULL)
759 for (int i = 0; i < cl->class_class_member_count; ++i)
760 {
761 ocmember_T *m = &cl->class_class_members[i];
762 typval_T *tv = &cl->class_members_tv[i];
763 if (m->ocm_init != NULL)
764 {
765 typval_T *etv = eval_expr(m->ocm_init, eap);
766 if (etv != NULL)
767 {
768 *tv = *etv;
769 vim_free(etv);
770 }
771 }
772 else
773 {
774 // TODO: proper default value
775 tv->v_type = m->ocm_type->tt_type;
776 tv->vval.v_string = NULL;
777 }
778 }
779 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000780
781 int have_new = FALSE;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000782 for (int i = 0; i < classfunctions.ga_len; ++i)
783 if (STRCMP(((ufunc_T **)classfunctions.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000784 "new") == 0)
785 {
786 have_new = TRUE;
787 break;
788 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000789 if (is_class && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000790 {
791 // No new() method was defined, add the default constructor.
792 garray_T fga;
793 ga_init2(&fga, 1, 1000);
794 ga_concat(&fga, (char_u *)"new(");
795 for (int i = 0; i < cl->class_obj_member_count; ++i)
796 {
797 if (i > 0)
798 ga_concat(&fga, (char_u *)", ");
799 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000800 ocmember_T *m = cl->class_obj_members + i;
801 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000802 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000803 }
804 ga_concat(&fga, (char_u *)")\nenddef\n");
805 ga_append(&fga, NUL);
806
807 exarg_T fea;
808 CLEAR_FIELD(fea);
809 fea.cmdidx = CMD_def;
810 fea.cmd = fea.arg = fga.ga_data;
811
812 garray_T lines_to_free;
813 ga_init2(&lines_to_free, sizeof(char_u *), 50);
814
Bram Moolenaar2c011312023-01-07 10:51:30 +0000815 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000816
817 ga_clear_strings(&lines_to_free);
818 vim_free(fga.ga_data);
819
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000820 if (nf != NULL && ga_grow(&classfunctions, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000821 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000822 ((ufunc_T **)classfunctions.ga_data)[classfunctions.ga_len]
823 = nf;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000824 ++classfunctions.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000825
826 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000827 nf->uf_ret_type = get_type_ptr(&type_list);
828 if (nf->uf_ret_type != NULL)
829 {
830 nf->uf_ret_type->tt_type = VAR_OBJECT;
831 nf->uf_ret_type->tt_member = (type_T *)cl;
832 nf->uf_ret_type->tt_argcount = 0;
833 nf->uf_ret_type->tt_args = NULL;
834 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000835 }
836 }
837
Bram Moolenaar58b40092023-01-11 15:59:05 +0000838 // Move all the functions into the created class.
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000839 // loop 1: class functions, loop 2: object methods
840 for (int loop = 1; loop <= 2; ++loop)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000841 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000842 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
843 int *fcount = loop == 1 ? &cl->class_class_function_count
844 : &cl->class_obj_method_count;
845 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
846 : &cl->class_obj_methods;
847
Bram Moolenaar83677162023-01-08 19:54:10 +0000848 int parent_count = 0;
849 if (extends_cl != NULL)
850 // Include functions from the parent.
851 parent_count = loop == 1
852 ? extends_cl->class_class_function_count
853 : extends_cl->class_obj_method_count;
854
855 *fcount = parent_count + gap->ga_len;
856 if (*fcount == 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000857 {
858 *fup = NULL;
859 continue;
860 }
Bram Moolenaar83677162023-01-08 19:54:10 +0000861 *fup = ALLOC_MULT(ufunc_T *, *fcount);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000862 if (*fup == NULL)
863 goto cleanup;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000864
Bram Moolenaar58b40092023-01-11 15:59:05 +0000865 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
866 vim_free(gap->ga_data);
867 if (loop == 1)
868 cl->class_class_function_count_child = gap->ga_len;
869 else
870 cl->class_obj_method_count_child = gap->ga_len;
871
Bram Moolenaar83677162023-01-08 19:54:10 +0000872 int skipped = 0;
873 for (int i = 0; i < parent_count; ++i)
874 {
875 // Copy functions from the parent. Can't use the same
876 // function, because "uf_class" is different and compilation
877 // will have a different result.
Bram Moolenaar58b40092023-01-11 15:59:05 +0000878 // Put them after the functions in the current class, object
879 // methods may be overruled, then "super.Method()" is used to
880 // find a method from the parent.
Bram Moolenaar83677162023-01-08 19:54:10 +0000881 // Skip "new" functions. TODO: not all of them.
882 if (loop == 1 && STRNCMP(
883 extends_cl->class_class_functions[i]->uf_name,
884 "new", 3) == 0)
885 ++skipped;
886 else
Bram Moolenaar58b40092023-01-11 15:59:05 +0000887 {
888 ufunc_T *pf = (loop == 1
Bram Moolenaar83677162023-01-08 19:54:10 +0000889 ? extends_cl->class_class_functions
Bram Moolenaar58b40092023-01-11 15:59:05 +0000890 : extends_cl->class_obj_methods)[i];
891 (*fup)[gap->ga_len + i - skipped] = copy_function(pf);
892
893 // If the child class overrides a function from the parent
894 // the signature must be equal.
895 char_u *pname = pf->uf_name;
896 for (int ci = 0; ci < gap->ga_len; ++ci)
897 {
898 ufunc_T *cf = (*fup)[ci];
899 char_u *cname = cf->uf_name;
900 if (STRCMP(pname, cname) == 0)
901 {
902 where_T where = WHERE_INIT;
903 where.wt_func_name = (char *)pname;
904 (void)check_type(pf->uf_func_type, cf->uf_func_type,
905 TRUE, where);
906 }
907 }
908 }
Bram Moolenaar83677162023-01-08 19:54:10 +0000909 }
910
Bram Moolenaar83677162023-01-08 19:54:10 +0000911 *fcount -= skipped;
912
913 // Set the class pointer on all the functions and object methods.
914 for (int i = 0; i < *fcount; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000915 {
916 ufunc_T *fp = (*fup)[i];
917 fp->uf_class = cl;
918 if (loop == 2)
919 fp->uf_flags |= FC_OBJECT;
920 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000921 }
922
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000923 cl->class_type.tt_type = VAR_CLASS;
924 cl->class_type.tt_member = (type_T *)cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000925 cl->class_object_type.tt_type = VAR_OBJECT;
926 cl->class_object_type.tt_member = (type_T *)cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000927 cl->class_type_list = type_list;
928
929 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +0000930 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000931
932 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +0000933 // TODO: handle other context, e.g. in a function
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000934 typval_T tv;
935 tv.v_type = VAR_CLASS;
936 tv.vval.v_class = cl;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000937 is_export = class_export;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000938 set_var_const(cl->class_name, current_sctx.sc_sid,
939 NULL, &tv, FALSE, ASSIGN_DECL, 0);
940 return;
941 }
942
943cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +0000944 if (cl != NULL)
945 {
946 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000947 vim_free(cl->class_class_functions);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000948 if (cl->class_interfaces != NULL)
949 {
950 for (int i = 0; i < cl->class_interface_count; ++i)
951 vim_free(cl->class_interfaces[i]);
952 vim_free(cl->class_interfaces);
953 }
954 if (cl->class_interfaces_cl != NULL)
955 {
956 for (int i = 0; i < cl->class_interface_count; ++i)
957 class_unref(cl->class_interfaces_cl[i]);
958 vim_free(cl->class_interfaces_cl);
959 }
Bram Moolenaareb533502022-12-14 15:06:11 +0000960 vim_free(cl->class_obj_members);
961 vim_free(cl->class_obj_methods);
962 vim_free(cl);
963 }
964
Bram Moolenaar83677162023-01-08 19:54:10 +0000965 vim_free(extends);
966 class_unref(extends_cl);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000967
968 if (intf_classes != NULL)
969 {
970 for (int i = 0; i < ga_impl.ga_len; ++i)
971 class_unref(intf_classes[i]);
972 vim_free(intf_classes);
973 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000974 ga_clear_strings(&ga_impl);
975
Bram Moolenaard505d172022-12-18 21:42:55 +0000976 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000977 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000978 garray_T *gap = round == 1 ? &classmembers : &objmembers;
979 if (gap->ga_len == 0 || gap->ga_data == NULL)
980 continue;
981
982 for (int i = 0; i < gap->ga_len; ++i)
983 {
984 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
985 vim_free(m->ocm_name);
986 vim_free(m->ocm_init);
987 }
988 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000989 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000990
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000991 for (int i = 0; i < objmethods.ga_len; ++i)
992 {
993 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
994 func_clear_free(uf, FALSE);
995 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000996 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000997
998 for (int i = 0; i < classfunctions.ga_len; ++i)
999 {
1000 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
1001 func_clear_free(uf, FALSE);
1002 }
1003 ga_clear(&classfunctions);
1004
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001005 clear_type_list(&type_list);
1006}
1007
1008/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001009 * Find member "name" in class "cl", set "member_idx" to the member index and
1010 * return its type.
1011 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001012 */
1013 type_T *
1014class_member_type(
1015 class_T *cl,
1016 char_u *name,
1017 char_u *name_end,
1018 int *member_idx)
1019{
1020 *member_idx = -1; // not found (yet)
1021 size_t len = name_end - name;
1022
1023 for (int i = 0; i < cl->class_obj_member_count; ++i)
1024 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001025 ocmember_T *m = cl->class_obj_members + i;
1026 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001027 {
1028 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001029 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001030 }
1031 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001032
1033 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001034 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001035}
1036
1037/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001038 * Handle ":enum" up to ":endenum".
1039 */
1040 void
1041ex_enum(exarg_T *eap UNUSED)
1042{
1043 // TODO
1044}
1045
1046/*
1047 * Handle ":type".
1048 */
1049 void
1050ex_type(exarg_T *eap UNUSED)
1051{
1052 // TODO
1053}
1054
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001055/*
1056 * Evaluate what comes after a class:
1057 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001058 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001059 * - class constructor: SomeClass.new()
1060 * - object member: someObject.varname
1061 * - object method: someObject.SomeMethod()
1062 *
1063 * "*arg" points to the '.'.
1064 * "*arg" is advanced to after the member name or method call.
1065 *
1066 * Returns FAIL or OK.
1067 */
1068 int
1069class_object_index(
1070 char_u **arg,
1071 typval_T *rettv,
1072 evalarg_T *evalarg,
1073 int verbose UNUSED) // give error messages
1074{
1075 // int evaluate = evalarg != NULL
1076 // && (evalarg->eval_flags & EVAL_EVALUATE);
1077
1078 if (VIM_ISWHITE((*arg)[1]))
1079 {
1080 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
1081 return FAIL;
1082 }
1083
1084 ++*arg;
1085 char_u *name = *arg;
1086 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1087 if (name_end == name)
1088 return FAIL;
1089 size_t len = name_end - name;
1090
1091 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
1092 : rettv->vval.v_object->obj_class;
1093 if (*name_end == '(')
1094 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001095 int on_class = rettv->v_type == VAR_CLASS;
1096 int count = on_class ? cl->class_class_function_count
1097 : cl->class_obj_method_count;
1098 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001099 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001100 ufunc_T *fp = on_class ? cl->class_class_functions[i]
1101 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +00001102 // Use a separate pointer to avoid that ASAN complains about
1103 // uf_name[] only being 4 characters.
1104 char_u *ufname = (char_u *)fp->uf_name;
1105 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001106 {
1107 typval_T argvars[MAX_FUNC_ARGS + 1];
1108 int argcount = 0;
1109
1110 char_u *argp = name_end;
1111 int ret = get_func_arguments(&argp, evalarg, 0,
1112 argvars, &argcount);
1113 if (ret == FAIL)
1114 return FAIL;
1115
1116 funcexe_T funcexe;
1117 CLEAR_FIELD(funcexe);
1118 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001119 if (rettv->v_type == VAR_OBJECT)
1120 {
1121 funcexe.fe_object = rettv->vval.v_object;
1122 ++funcexe.fe_object->obj_refcount;
1123 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001124
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001125 // Clear the class or object after calling the function, in
1126 // case the refcount is one.
1127 typval_T tv_tofree = *rettv;
1128 rettv->v_type = VAR_UNKNOWN;
1129
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001130 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001131 int error = call_user_func_check(fp, argcount, argvars,
1132 rettv, &funcexe, NULL);
1133
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001134 // Clear the previous rettv and the arguments.
1135 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001136 for (int idx = 0; idx < argcount; ++idx)
1137 clear_tv(&argvars[idx]);
1138
1139 if (error != FCERR_NONE)
1140 {
1141 user_func_error(error, printable_func_name(fp),
1142 funcexe.fe_found_var);
1143 return FAIL;
1144 }
1145 *arg = argp;
1146 return OK;
1147 }
1148 }
1149
1150 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
1151 }
1152
1153 else if (rettv->v_type == VAR_OBJECT)
1154 {
1155 for (int i = 0; i < cl->class_obj_member_count; ++i)
1156 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001157 ocmember_T *m = &cl->class_obj_members[i];
1158 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001159 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001160 if (*name == '_')
1161 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001162 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001163 return FAIL;
1164 }
1165
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001166 // The object only contains a pointer to the class, the member
1167 // values array follows right after that.
1168 object_T *obj = rettv->vval.v_object;
1169 typval_T *tv = (typval_T *)(obj + 1) + i;
1170 copy_tv(tv, rettv);
1171 object_unref(obj);
1172
1173 *arg = name_end;
1174 return OK;
1175 }
1176 }
1177
1178 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
1179 }
1180
Bram Moolenaard505d172022-12-18 21:42:55 +00001181 else if (rettv->v_type == VAR_CLASS)
1182 {
1183 // class member
1184 for (int i = 0; i < cl->class_class_member_count; ++i)
1185 {
1186 ocmember_T *m = &cl->class_class_members[i];
1187 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
1188 {
1189 if (*name == '_')
1190 {
1191 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
1192 return FAIL;
1193 }
1194
1195 typval_T *tv = &cl->class_members_tv[i];
1196 copy_tv(tv, rettv);
1197 class_unref(cl);
1198
1199 *arg = name_end;
1200 return OK;
1201 }
1202 }
1203
1204 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
1205 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001206
1207 return FAIL;
1208}
1209
1210/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001211 * If "arg" points to a class or object method, return it.
1212 * Otherwise return NULL.
1213 */
1214 ufunc_T *
1215find_class_func(char_u **arg)
1216{
1217 char_u *name = *arg;
1218 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1219 if (name_end == name || *name_end != '.')
1220 return NULL;
1221
1222 size_t len = name_end - name;
1223 typval_T tv;
1224 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001225 if (eval_variable(name, (int)len,
1226 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001227 return NULL;
1228 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001229 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001230
1231 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1232 : tv.vval.v_object->obj_class;
1233 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001234 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001235 char_u *fname = name_end + 1;
1236 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1237 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001238 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001239 len = fname_end - fname;
1240
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001241 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1242 : cl->class_obj_method_count;
1243 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1244 : cl->class_obj_methods;
1245 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001246 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001247 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001248 // Use a separate pointer to avoid that ASAN complains about
1249 // uf_name[] only being 4 characters.
1250 char_u *ufname = (char_u *)fp->uf_name;
1251 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001252 {
1253 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001254 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001255 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001256 }
1257
Bram Moolenaareb533502022-12-14 15:06:11 +00001258fail_after_eval:
1259 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001260 return NULL;
1261}
1262
1263/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001264 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1265 * index in class.class_class_members[].
1266 * If "cl_ret" is not NULL set it to the class.
1267 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001268 */
1269 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001270class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001271{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001272 if (cctx == NULL || cctx->ctx_ufunc == NULL
1273 || cctx->ctx_ufunc->uf_class == NULL)
1274 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001275 class_T *cl = cctx->ctx_ufunc->uf_class;
1276
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001277 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001278 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001279 ocmember_T *m = &cl->class_class_members[i];
1280 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001281 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001282 if (cl_ret != NULL)
1283 *cl_ret = cl;
1284 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001285 }
1286 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001287 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001288}
1289
1290/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001291 * Make a copy of an object.
1292 */
1293 void
1294copy_object(typval_T *from, typval_T *to)
1295{
1296 *to = *from;
1297 if (to->vval.v_object != NULL)
1298 ++to->vval.v_object->obj_refcount;
1299}
1300
1301/*
1302 * Free an object.
1303 */
1304 static void
1305object_clear(object_T *obj)
1306{
1307 class_T *cl = obj->obj_class;
1308
1309 // the member values are just after the object structure
1310 typval_T *tv = (typval_T *)(obj + 1);
1311 for (int i = 0; i < cl->class_obj_member_count; ++i)
1312 clear_tv(tv + i);
1313
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001314 // Remove from the list headed by "first_object".
1315 object_cleared(obj);
1316
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001317 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001318 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001319}
1320
1321/*
1322 * Unreference an object.
1323 */
1324 void
1325object_unref(object_T *obj)
1326{
1327 if (obj != NULL && --obj->obj_refcount <= 0)
1328 object_clear(obj);
1329}
1330
1331/*
1332 * Make a copy of a class.
1333 */
1334 void
1335copy_class(typval_T *from, typval_T *to)
1336{
1337 *to = *from;
1338 if (to->vval.v_class != NULL)
1339 ++to->vval.v_class->class_refcount;
1340}
1341
1342/*
1343 * Unreference a class. Free it when the reference count goes down to zero.
1344 */
1345 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001346class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001347{
Bram Moolenaard505d172022-12-18 21:42:55 +00001348 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001349 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001350 // Freeing what the class contains may recursively come back here.
1351 // Clear "class_name" first, if it is NULL the class does not need to
1352 // be freed.
1353 VIM_CLEAR(cl->class_name);
1354
Bram Moolenaar83677162023-01-08 19:54:10 +00001355 class_unref(cl->class_extends);
1356
Bram Moolenaar94674f22023-01-06 18:42:20 +00001357 for (int i = 0; i < cl->class_interface_count; ++i)
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001358 {
Bram Moolenaar94674f22023-01-06 18:42:20 +00001359 vim_free(((char_u **)cl->class_interfaces)[i]);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001360 if (cl->class_interfaces_cl[i] != NULL)
1361 class_unref(cl->class_interfaces_cl[i]);
1362 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001363 vim_free(cl->class_interfaces);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001364 vim_free(cl->class_interfaces_cl);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001365
Bram Moolenaard505d172022-12-18 21:42:55 +00001366 for (int i = 0; i < cl->class_class_member_count; ++i)
1367 {
1368 ocmember_T *m = &cl->class_class_members[i];
1369 vim_free(m->ocm_name);
1370 vim_free(m->ocm_init);
1371 if (cl->class_members_tv != NULL)
1372 clear_tv(&cl->class_members_tv[i]);
1373 }
1374 vim_free(cl->class_class_members);
1375 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001376
1377 for (int i = 0; i < cl->class_obj_member_count; ++i)
1378 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001379 ocmember_T *m = &cl->class_obj_members[i];
1380 vim_free(m->ocm_name);
1381 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001382 }
1383 vim_free(cl->class_obj_members);
1384
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001385 for (int i = 0; i < cl->class_class_function_count; ++i)
1386 {
1387 ufunc_T *uf = cl->class_class_functions[i];
1388 func_clear_free(uf, FALSE);
1389 }
1390 vim_free(cl->class_class_functions);
1391
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001392 for (int i = 0; i < cl->class_obj_method_count; ++i)
1393 {
1394 ufunc_T *uf = cl->class_obj_methods[i];
1395 func_clear_free(uf, FALSE);
1396 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001397 vim_free(cl->class_obj_methods);
1398
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001399 clear_type_list(&cl->class_type_list);
1400
1401 vim_free(cl);
1402 }
1403}
1404
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001405static object_T *first_object = NULL;
1406
1407/*
1408 * Call this function when an object has been created. It will be added to the
1409 * list headed by "first_object".
1410 */
1411 void
1412object_created(object_T *obj)
1413{
1414 if (first_object != NULL)
1415 {
1416 obj->obj_next_used = first_object;
1417 first_object->obj_prev_used = obj;
1418 }
1419 first_object = obj;
1420}
1421
1422/*
1423 * Call this function when an object has been cleared and is about to be freed.
1424 * It is removed from the list headed by "first_object".
1425 */
1426 void
1427object_cleared(object_T *obj)
1428{
1429 if (obj->obj_next_used != NULL)
1430 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1431 if (obj->obj_prev_used != NULL)
1432 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1433 else if (first_object == obj)
1434 first_object = obj->obj_next_used;
1435}
1436
1437/*
1438 * Go through the list of all objects and free items without "copyID".
1439 */
1440 int
1441object_free_nonref(int copyID)
1442{
1443 int did_free = FALSE;
1444 object_T *next_obj;
1445
1446 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
1447 {
1448 next_obj = obj->obj_next_used;
1449 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1450 {
1451 // Free the object and items it contains.
1452 object_clear(obj);
1453 did_free = TRUE;
1454 }
1455 }
1456
1457 return did_free;
1458}
1459
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001460
1461#endif // FEAT_EVAL