blob: 8dcd92b6ebd7c7074acb34138ee5457e8cae80e5 [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 Moolenaarc1c365c2022-12-04 20:13:24 +0000207 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000208 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000209 if (is_abstract)
210 {
211 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
212 {
213 semsg(_(e_invalid_argument_str), arg);
214 return;
215 }
216 arg = skipwhite(arg + 5);
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000217 is_class = TRUE;
218 }
219
220 if (!current_script_is_vim9()
221 || (cmdmod.cmod_flags & CMOD_LEGACY)
222 || !getline_equal(eap->getline, eap->cookie, getsourceline))
223 {
224 if (is_class)
225 emsg(_(e_class_can_only_be_defined_in_vim9_script));
226 else
227 emsg(_(e_interface_can_only_be_defined_in_vim9_script));
228 return;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000229 }
230
231 if (!ASCII_ISUPPER(*arg))
232 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000233 if (is_class)
234 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
235 else
236 semsg(_(e_interface_name_must_start_with_uppercase_letter_str),
237 arg);
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000238 return;
239 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000240 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
241 if (!IS_WHITE_OR_NUL(*name_end))
242 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000243 semsg(_(e_white_space_required_after_name_str), arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000244 return;
245 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000246 char_u *name_start = arg;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000247
Bram Moolenaara86655a2023-01-12 17:06:27 +0000248 // "export class" gets used when creating the class, don't use "is_export"
249 // for the items inside the class.
250 int class_export = is_export;
251 is_export = FALSE;
252
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000253 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000254 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000255
Bram Moolenaar83677162023-01-08 19:54:10 +0000256 // Name for "extends BaseClass"
257 char_u *extends = NULL;
258
Bram Moolenaar94674f22023-01-06 18:42:20 +0000259 // Names for "implements SomeInterface"
260 garray_T ga_impl;
261 ga_init2(&ga_impl, sizeof(char_u *), 5);
262
263 arg = skipwhite(name_end);
264 while (*arg != NUL && *arg != '#' && *arg != '\n')
265 {
266 // TODO:
Bram Moolenaar94674f22023-01-06 18:42:20 +0000267 // specifies SomeInterface
Bram Moolenaar83677162023-01-08 19:54:10 +0000268 if (STRNCMP(arg, "extends", 7) == 0 && IS_WHITE_OR_NUL(arg[7]))
269 {
270 if (extends != NULL)
271 {
272 emsg(_(e_duplicate_extends));
273 goto early_ret;
274 }
275 arg = skipwhite(arg + 7);
276 char_u *end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
277 if (!IS_WHITE_OR_NUL(*end))
278 {
279 semsg(_(e_white_space_required_after_name_str), arg);
280 goto early_ret;
281 }
282 extends = vim_strnsave(arg, end - arg);
283 if (extends == NULL)
284 goto early_ret;
285
286 arg = skipwhite(end + 1);
287 }
288 else if (STRNCMP(arg, "implements", 10) == 0
289 && IS_WHITE_OR_NUL(arg[10]))
Bram Moolenaar94674f22023-01-06 18:42:20 +0000290 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000291 if (ga_impl.ga_len > 0)
292 {
293 emsg(_(e_duplicate_implements));
294 goto early_ret;
295 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000296 arg = skipwhite(arg + 10);
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000297
298 for (;;)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000299 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000300 char_u *impl_end = find_name_end(arg, NULL, NULL,
301 FNE_CHECK_START);
302 if (!IS_WHITE_OR_NUL(*impl_end) && *impl_end != ',')
303 {
304 semsg(_(e_white_space_required_after_name_str), arg);
305 goto early_ret;
306 }
307 char_u *iname = vim_strnsave(arg, impl_end - arg);
308 if (iname == NULL)
309 goto early_ret;
310 for (int i = 0; i < ga_impl.ga_len; ++i)
311 if (STRCMP(((char_u **)ga_impl.ga_data)[i], iname) == 0)
312 {
313 semsg(_(e_duplicate_interface_after_implements_str),
314 iname);
315 vim_free(iname);
316 goto early_ret;
317 }
318 if (ga_add_string(&ga_impl, iname) == FAIL)
319 {
320 vim_free(iname);
321 goto early_ret;
322 }
323 if (*impl_end != ',')
324 {
325 arg = skipwhite(impl_end);
326 break;
327 }
328 arg = skipwhite(impl_end + 1);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000329 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000330 }
331 else
332 {
333 semsg(_(e_trailing_characters_str), arg);
334early_ret:
Bram Moolenaar83677162023-01-08 19:54:10 +0000335 vim_free(extends);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000336 ga_clear_strings(&ga_impl);
337 return;
338 }
339 }
340
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000341 garray_T type_list; // list of pointers to allocated types
342 ga_init2(&type_list, sizeof(type_T *), 10);
343
Bram Moolenaard505d172022-12-18 21:42:55 +0000344 // Growarray with class members declared in the class.
345 garray_T classmembers;
346 ga_init2(&classmembers, sizeof(ocmember_T), 10);
347
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000348 // Growarray with functions declared in the class.
349 garray_T classfunctions;
350 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +0000351
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000352 // Growarray with object members declared in the class.
353 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +0000354 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000355
356 // Growarray with object methods declared in the class.
357 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000358 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000359
360 /*
Bram Moolenaar554d0312023-01-05 19:59:18 +0000361 * Go over the body of the class/interface until "endclass" or
362 * "endinterface" is found.
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000363 */
364 char_u *theline = NULL;
365 int success = FALSE;
366 for (;;)
367 {
368 vim_free(theline);
369 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
370 if (theline == NULL)
371 break;
372 char_u *line = skipwhite(theline);
373
Bram Moolenaar418b5472022-12-20 13:38:22 +0000374 // Skip empty and comment lines.
375 if (*line == NUL)
376 continue;
377 if (*line == '#')
378 {
379 if (vim9_bad_comment(line))
380 break;
381 continue;
382 }
383
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000384 char_u *p = line;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000385 char *end_name = is_class ? "endclass" : "endinterface";
386 if (checkforcmd(&p, end_name, is_class ? 4 : 5))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000387 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000388 if (STRNCMP(line, end_name, is_class ? 8 : 12) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000389 semsg(_(e_command_cannot_be_shortened_str), line);
390 else if (*p == '|' || !ends_excmd2(line, p))
391 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +0000392 else
393 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000394 break;
395 }
Bram Moolenaar554d0312023-01-05 19:59:18 +0000396 char *wrong_name = is_class ? "endinterface" : "endclass";
397 if (checkforcmd(&p, wrong_name, is_class ? 5 : 4))
398 {
399 semsg(_(e_invalid_command_str), line);
400 break;
401 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000402
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000403 int has_public = FALSE;
404 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000405 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000406 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000407 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000408 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000409 break;
410 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000411 has_public = TRUE;
412 p = skipwhite(line + 6);
413
Bram Moolenaard505d172022-12-18 21:42:55 +0000414 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000415 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000416 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000417 break;
418 }
419 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000420
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000421 int has_static = FALSE;
422 char_u *ps = p;
423 if (checkforcmd(&p, "static", 4))
424 {
425 if (STRNCMP(ps, "static", 6) != 0)
426 {
427 semsg(_(e_command_cannot_be_shortened_str), ps);
428 break;
429 }
430 has_static = TRUE;
431 p = skipwhite(ps + 6);
432 }
433
Bram Moolenaard505d172022-12-18 21:42:55 +0000434 // object members (public, read access, private):
435 // "this._varname"
436 // "this.varname"
437 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000438 if (STRNCMP(p, "this", 4) == 0)
439 {
440 if (p[4] != '.' || !eval_isnamec1(p[5]))
441 {
442 semsg(_(e_invalid_object_member_declaration_str), p);
443 break;
444 }
445 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +0000446 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +0000447 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +0000448 char_u *init_expr = NULL;
449 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000450 &varname_end, &type_list, &type,
451 is_class ? &init_expr: NULL) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000452 break;
453 if (add_member(&objmembers, varname, varname_end,
454 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000455 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000456 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000457 break;
458 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000459 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000460
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000461 // constructors:
462 // def new()
463 // enddef
464 // def newOther()
465 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000466 // object methods and class functions:
467 // def SomeMethod()
468 // enddef
469 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000470 // enddef
471 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000472 // def <Tval> someMethod()
473 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000474 else if (checkforcmd(&p, "def", 3))
475 {
476 exarg_T ea;
477 garray_T lines_to_free;
478
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000479 // TODO: error for "public static def Func()"?
480
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000481 CLEAR_FIELD(ea);
482 ea.cmd = line;
483 ea.arg = p;
484 ea.cmdidx = CMD_def;
485 ea.getline = eap->getline;
486 ea.cookie = eap->cookie;
487
488 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000489 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free,
490 is_class ? CF_CLASS : CF_INTERFACE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000491 ga_clear_strings(&lines_to_free);
492
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000493 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000494 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000495 char_u *name = uf->uf_name;
496 int is_new = STRNCMP(name, "new", 3) == 0;
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000497 if (is_new && is_abstract)
498 {
499 emsg(_(e_cannot_define_new_function_in_abstract_class));
500 success = FALSE;
501 break;
502 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000503 garray_T *fgap = has_static || is_new
504 ? &classfunctions : &objmethods;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000505 // Check the name isn't used already.
506 for (int i = 0; i < fgap->ga_len; ++i)
507 {
508 char_u *n = ((ufunc_T **)fgap->ga_data)[i]->uf_name;
509 if (STRCMP(name, n) == 0)
510 {
511 semsg(_(e_duplicate_function_str), name);
512 break;
513 }
514 }
515
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000516 if (ga_grow(fgap, 1) == OK)
517 {
518 if (is_new)
519 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000520
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000521 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
522 ++fgap->ga_len;
523 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000524 }
525 }
526
527 // class members
528 else if (has_static)
529 {
530 // class members (public, read access, private):
531 // "static _varname"
532 // "static varname"
533 // "public static varname"
534 char_u *varname = p;
535 char_u *varname_end = NULL;
536 type_T *type = NULL;
537 char_u *init_expr = NULL;
538 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000539 &varname_end, &type_list, &type,
540 is_class ? &init_expr : NULL) == FAIL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000541 break;
542 if (add_member(&classmembers, varname, varname_end,
543 has_public, type, init_expr) == FAIL)
544 {
545 vim_free(init_expr);
546 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000547 }
548 }
549
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000550 else
551 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000552 if (is_class)
553 semsg(_(e_not_valid_command_in_class_str), line);
554 else
555 semsg(_(e_not_valid_command_in_interface_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000556 break;
557 }
558 }
559 vim_free(theline);
560
Bram Moolenaar83677162023-01-08 19:54:10 +0000561 class_T *extends_cl = NULL; // class from "extends" argument
562
563 /*
564 * Check a few things before defining the class.
565 */
566
567 // Check the "extends" class is valid.
568 if (success && extends != NULL)
569 {
570 typval_T tv;
571 tv.v_type = VAR_UNKNOWN;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000572 if (eval_variable_import(extends, &tv) == FAIL)
Bram Moolenaar83677162023-01-08 19:54:10 +0000573 {
574 semsg(_(e_class_name_not_found_str), extends);
575 success = FALSE;
576 }
577 else
578 {
579 if (tv.v_type != VAR_CLASS
580 || tv.vval.v_class == NULL
581 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) != 0)
582 {
583 semsg(_(e_cannot_extend_str), extends);
584 success = FALSE;
585 }
586 else
587 {
588 extends_cl = tv.vval.v_class;
589 ++extends_cl->class_refcount;
590 }
591 clear_tv(&tv);
592 }
593 }
594 VIM_CLEAR(extends);
595
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000596 class_T **intf_classes = NULL;
597
Bram Moolenaar83677162023-01-08 19:54:10 +0000598 // Check all "implements" entries are valid.
Bram Moolenaar94674f22023-01-06 18:42:20 +0000599 if (success && ga_impl.ga_len > 0)
600 {
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000601 intf_classes = ALLOC_CLEAR_MULT(class_T *, ga_impl.ga_len);
602
Bram Moolenaar94674f22023-01-06 18:42:20 +0000603 for (int i = 0; i < ga_impl.ga_len && success; ++i)
604 {
605 char_u *impl = ((char_u **)ga_impl.ga_data)[i];
606 typval_T tv;
607 tv.v_type = VAR_UNKNOWN;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000608 if (eval_variable_import(impl, &tv) == FAIL)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000609 {
610 semsg(_(e_interface_name_not_found_str), impl);
611 success = FALSE;
612 break;
613 }
614
615 if (tv.v_type != VAR_CLASS
616 || tv.vval.v_class == NULL
617 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0)
618 {
619 semsg(_(e_not_valid_interface_str), impl);
620 success = FALSE;
621 }
622
Bram Moolenaar94674f22023-01-06 18:42:20 +0000623 class_T *ifcl = tv.vval.v_class;
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000624 intf_classes[i] = ifcl;
625 ++ifcl->class_refcount;
626
627 // check the members of the interface match the members of the class
Bram Moolenaar94674f22023-01-06 18:42:20 +0000628 for (int loop = 1; loop <= 2 && success; ++loop)
629 {
630 // loop == 1: check class members
631 // loop == 2: check object members
632 int if_count = loop == 1 ? ifcl->class_class_member_count
633 : ifcl->class_obj_member_count;
634 if (if_count == 0)
635 continue;
636 ocmember_T *if_ms = loop == 1 ? ifcl->class_class_members
637 : ifcl->class_obj_members;
638 ocmember_T *cl_ms = (ocmember_T *)(loop == 1
639 ? classmembers.ga_data
640 : objmembers.ga_data);
641 int cl_count = loop == 1 ? classmembers.ga_len
642 : objmembers.ga_len;
643 for (int if_i = 0; if_i < if_count; ++if_i)
644 {
645 int cl_i;
646 for (cl_i = 0; cl_i < cl_count; ++cl_i)
647 {
648 ocmember_T *m = &cl_ms[cl_i];
649 if (STRCMP(if_ms[if_i].ocm_name, m->ocm_name) == 0)
650 {
651 // TODO: check type
652 break;
653 }
654 }
655 if (cl_i == cl_count)
656 {
657 semsg(_(e_member_str_of_interface_str_not_implemented),
658 if_ms[if_i].ocm_name, impl);
659 success = FALSE;
660 break;
661 }
662 }
663 }
664
665 // check the functions/methods of the interface match the
666 // functions/methods of the class
667 for (int loop = 1; loop <= 2 && success; ++loop)
668 {
669 // loop == 1: check class functions
670 // loop == 2: check object methods
671 int if_count = loop == 1 ? ifcl->class_class_function_count
672 : ifcl->class_obj_method_count;
673 if (if_count == 0)
674 continue;
675 ufunc_T **if_fp = loop == 1 ? ifcl->class_class_functions
676 : ifcl->class_obj_methods;
677 ufunc_T **cl_fp = (ufunc_T **)(loop == 1
678 ? classfunctions.ga_data
679 : objmethods.ga_data);
680 int cl_count = loop == 1 ? classfunctions.ga_len
681 : objmethods.ga_len;
682 for (int if_i = 0; if_i < if_count; ++if_i)
683 {
684 char_u *if_name = if_fp[if_i]->uf_name;
685 int cl_i;
686 for (cl_i = 0; cl_i < cl_count; ++cl_i)
687 {
688 char_u *cl_name = cl_fp[cl_i]->uf_name;
689 if (STRCMP(if_name, cl_name) == 0)
690 {
691 // TODO: check return and argument types
692 break;
693 }
694 }
695 if (cl_i == cl_count)
696 {
697 semsg(_(e_function_str_of_interface_str_not_implemented),
698 if_name, impl);
699 success = FALSE;
700 break;
701 }
702 }
703 }
704
705 clear_tv(&tv);
706 }
707 }
708
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000709 if (success)
710 {
711 // Check no function argument name is used as an object/class member.
712 for (int loop = 1; loop <= 2 && success; ++loop)
713 {
714 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
715
716 for (int fi = 0; fi < gap->ga_len && success; ++fi)
717 {
718 ufunc_T *uf = ((ufunc_T **)gap->ga_data)[fi];
719
720 for (int i = 0; i < uf->uf_args.ga_len && success; ++i)
721 {
722 char_u *aname = ((char_u **)uf->uf_args.ga_data)[i];
723 for (int il = 1; il <= 2 && success; ++il)
724 {
725 // For a "new()" function "this.member" arguments are
726 // OK. TODO: check for the "this." prefix.
Bram Moolenaarb40a2fb2023-01-13 19:18:38 +0000727 if (STRNCMP(uf->uf_name, "new", 3) == 0 && il == 2)
Bram Moolenaard40f00c2023-01-13 17:36:49 +0000728 continue;
729 garray_T *mgap = il == 1 ? &classmembers : &objmembers;
730 for (int mi = 0; mi < mgap->ga_len; ++mi)
731 {
732 char_u *mname = ((ocmember_T *)mgap->ga_data
733 + mi)->ocm_name;
734 if (STRCMP(aname, mname) == 0)
735 {
736 success = FALSE;
737 semsg(_(e_argument_already_declared_in_class_str),
738 aname);
739 break;
740 }
741 }
742 }
743 }
744 }
745 }
746 }
747
748
Bram Moolenaareb533502022-12-14 15:06:11 +0000749 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000750 if (success)
751 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000752 // "endclass" encountered without failures: Create the class.
753
Bram Moolenaareb533502022-12-14 15:06:11 +0000754 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000755 if (cl == NULL)
756 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000757 if (!is_class)
758 cl->class_flags = CLASS_INTERFACE;
759
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000760 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +0000761 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +0000762 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000763 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000764
Bram Moolenaar83677162023-01-08 19:54:10 +0000765 cl->class_extends = extends_cl;
766
Bram Moolenaar94674f22023-01-06 18:42:20 +0000767 if (ga_impl.ga_len > 0)
768 {
769 // Move the "implements" names into the class.
770 cl->class_interface_count = ga_impl.ga_len;
771 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
772 if (cl->class_interfaces == NULL)
773 goto cleanup;
774 for (int i = 0; i < ga_impl.ga_len; ++i)
775 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
Bram Moolenaar7d4d87b2023-01-06 18:59:08 +0000776 VIM_CLEAR(ga_impl.ga_data);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000777 ga_impl.ga_len = 0;
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000778
779 cl->class_interfaces_cl = intf_classes;
780 intf_classes = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +0000781 }
782
Bram Moolenaard505d172022-12-18 21:42:55 +0000783 // Add class and object members to "cl".
784 if (add_members_to_class(&classmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000785 extends_cl == NULL ? NULL
786 : extends_cl->class_class_members,
787 extends_cl == NULL ? 0
788 : extends_cl->class_class_member_count,
789 &cl->class_class_members,
790 &cl->class_class_member_count) == FAIL
Bram Moolenaard505d172022-12-18 21:42:55 +0000791 || add_members_to_class(&objmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000792 extends_cl == NULL ? NULL
793 : extends_cl->class_obj_members,
794 extends_cl == NULL ? 0
795 : extends_cl->class_obj_member_count,
796 &cl->class_obj_members,
797 &cl->class_obj_member_count) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000798 goto cleanup;
799
Bram Moolenaar554d0312023-01-05 19:59:18 +0000800 if (is_class && cl->class_class_member_count > 0)
Bram Moolenaard505d172022-12-18 21:42:55 +0000801 {
802 // Allocate a typval for each class member and initialize it.
803 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
804 cl->class_class_member_count);
805 if (cl->class_members_tv != NULL)
806 for (int i = 0; i < cl->class_class_member_count; ++i)
807 {
808 ocmember_T *m = &cl->class_class_members[i];
809 typval_T *tv = &cl->class_members_tv[i];
810 if (m->ocm_init != NULL)
811 {
812 typval_T *etv = eval_expr(m->ocm_init, eap);
813 if (etv != NULL)
814 {
815 *tv = *etv;
816 vim_free(etv);
817 }
818 }
819 else
820 {
821 // TODO: proper default value
822 tv->v_type = m->ocm_type->tt_type;
823 tv->vval.v_string = NULL;
824 }
825 }
826 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000827
828 int have_new = FALSE;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000829 for (int i = 0; i < classfunctions.ga_len; ++i)
830 if (STRCMP(((ufunc_T **)classfunctions.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000831 "new") == 0)
832 {
833 have_new = TRUE;
834 break;
835 }
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000836 if (is_class && !is_abstract && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000837 {
838 // No new() method was defined, add the default constructor.
839 garray_T fga;
840 ga_init2(&fga, 1, 1000);
841 ga_concat(&fga, (char_u *)"new(");
842 for (int i = 0; i < cl->class_obj_member_count; ++i)
843 {
844 if (i > 0)
845 ga_concat(&fga, (char_u *)", ");
846 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000847 ocmember_T *m = cl->class_obj_members + i;
848 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000849 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000850 }
851 ga_concat(&fga, (char_u *)")\nenddef\n");
852 ga_append(&fga, NUL);
853
854 exarg_T fea;
855 CLEAR_FIELD(fea);
856 fea.cmdidx = CMD_def;
857 fea.cmd = fea.arg = fga.ga_data;
858
859 garray_T lines_to_free;
860 ga_init2(&lines_to_free, sizeof(char_u *), 50);
861
Bram Moolenaar2c011312023-01-07 10:51:30 +0000862 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000863
864 ga_clear_strings(&lines_to_free);
865 vim_free(fga.ga_data);
866
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000867 if (nf != NULL && ga_grow(&classfunctions, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000868 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000869 ((ufunc_T **)classfunctions.ga_data)[classfunctions.ga_len]
870 = nf;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000871 ++classfunctions.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000872
873 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000874 nf->uf_ret_type = get_type_ptr(&type_list);
875 if (nf->uf_ret_type != NULL)
876 {
877 nf->uf_ret_type->tt_type = VAR_OBJECT;
878 nf->uf_ret_type->tt_member = (type_T *)cl;
879 nf->uf_ret_type->tt_argcount = 0;
880 nf->uf_ret_type->tt_args = NULL;
881 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000882 }
883 }
884
Bram Moolenaar58b40092023-01-11 15:59:05 +0000885 // Move all the functions into the created class.
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000886 // loop 1: class functions, loop 2: object methods
887 for (int loop = 1; loop <= 2; ++loop)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000888 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000889 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
890 int *fcount = loop == 1 ? &cl->class_class_function_count
891 : &cl->class_obj_method_count;
892 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
893 : &cl->class_obj_methods;
894
Bram Moolenaar83677162023-01-08 19:54:10 +0000895 int parent_count = 0;
896 if (extends_cl != NULL)
897 // Include functions from the parent.
898 parent_count = loop == 1
899 ? extends_cl->class_class_function_count
900 : extends_cl->class_obj_method_count;
901
902 *fcount = parent_count + gap->ga_len;
903 if (*fcount == 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000904 {
905 *fup = NULL;
906 continue;
907 }
Bram Moolenaar83677162023-01-08 19:54:10 +0000908 *fup = ALLOC_MULT(ufunc_T *, *fcount);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000909 if (*fup == NULL)
910 goto cleanup;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000911
Bram Moolenaar58b40092023-01-11 15:59:05 +0000912 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
913 vim_free(gap->ga_data);
914 if (loop == 1)
915 cl->class_class_function_count_child = gap->ga_len;
916 else
917 cl->class_obj_method_count_child = gap->ga_len;
918
Bram Moolenaar83677162023-01-08 19:54:10 +0000919 int skipped = 0;
920 for (int i = 0; i < parent_count; ++i)
921 {
922 // Copy functions from the parent. Can't use the same
923 // function, because "uf_class" is different and compilation
924 // will have a different result.
Bram Moolenaar58b40092023-01-11 15:59:05 +0000925 // Put them after the functions in the current class, object
926 // methods may be overruled, then "super.Method()" is used to
927 // find a method from the parent.
Bram Moolenaar83677162023-01-08 19:54:10 +0000928 // Skip "new" functions. TODO: not all of them.
929 if (loop == 1 && STRNCMP(
930 extends_cl->class_class_functions[i]->uf_name,
931 "new", 3) == 0)
932 ++skipped;
933 else
Bram Moolenaar58b40092023-01-11 15:59:05 +0000934 {
935 ufunc_T *pf = (loop == 1
Bram Moolenaar83677162023-01-08 19:54:10 +0000936 ? extends_cl->class_class_functions
Bram Moolenaar58b40092023-01-11 15:59:05 +0000937 : extends_cl->class_obj_methods)[i];
938 (*fup)[gap->ga_len + i - skipped] = copy_function(pf);
939
940 // If the child class overrides a function from the parent
941 // the signature must be equal.
942 char_u *pname = pf->uf_name;
943 for (int ci = 0; ci < gap->ga_len; ++ci)
944 {
945 ufunc_T *cf = (*fup)[ci];
946 char_u *cname = cf->uf_name;
947 if (STRCMP(pname, cname) == 0)
948 {
949 where_T where = WHERE_INIT;
950 where.wt_func_name = (char *)pname;
951 (void)check_type(pf->uf_func_type, cf->uf_func_type,
952 TRUE, where);
953 }
954 }
955 }
Bram Moolenaar83677162023-01-08 19:54:10 +0000956 }
957
Bram Moolenaar83677162023-01-08 19:54:10 +0000958 *fcount -= skipped;
959
960 // Set the class pointer on all the functions and object methods.
961 for (int i = 0; i < *fcount; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000962 {
963 ufunc_T *fp = (*fup)[i];
964 fp->uf_class = cl;
965 if (loop == 2)
966 fp->uf_flags |= FC_OBJECT;
967 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000968 }
969
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000970 cl->class_type.tt_type = VAR_CLASS;
971 cl->class_type.tt_member = (type_T *)cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000972 cl->class_object_type.tt_type = VAR_OBJECT;
973 cl->class_object_type.tt_member = (type_T *)cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000974 cl->class_type_list = type_list;
975
976 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +0000977 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000978
979 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +0000980 // TODO: handle other context, e.g. in a function
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000981 typval_T tv;
982 tv.v_type = VAR_CLASS;
983 tv.vval.v_class = cl;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000984 is_export = class_export;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000985 set_var_const(cl->class_name, current_sctx.sc_sid,
986 NULL, &tv, FALSE, ASSIGN_DECL, 0);
987 return;
988 }
989
990cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +0000991 if (cl != NULL)
992 {
993 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000994 vim_free(cl->class_class_functions);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000995 if (cl->class_interfaces != NULL)
996 {
997 for (int i = 0; i < cl->class_interface_count; ++i)
998 vim_free(cl->class_interfaces[i]);
999 vim_free(cl->class_interfaces);
1000 }
1001 if (cl->class_interfaces_cl != NULL)
1002 {
1003 for (int i = 0; i < cl->class_interface_count; ++i)
1004 class_unref(cl->class_interfaces_cl[i]);
1005 vim_free(cl->class_interfaces_cl);
1006 }
Bram Moolenaareb533502022-12-14 15:06:11 +00001007 vim_free(cl->class_obj_members);
1008 vim_free(cl->class_obj_methods);
1009 vim_free(cl);
1010 }
1011
Bram Moolenaar83677162023-01-08 19:54:10 +00001012 vim_free(extends);
1013 class_unref(extends_cl);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001014
1015 if (intf_classes != NULL)
1016 {
1017 for (int i = 0; i < ga_impl.ga_len; ++i)
1018 class_unref(intf_classes[i]);
1019 vim_free(intf_classes);
1020 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001021 ga_clear_strings(&ga_impl);
1022
Bram Moolenaard505d172022-12-18 21:42:55 +00001023 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001024 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001025 garray_T *gap = round == 1 ? &classmembers : &objmembers;
1026 if (gap->ga_len == 0 || gap->ga_data == NULL)
1027 continue;
1028
1029 for (int i = 0; i < gap->ga_len; ++i)
1030 {
1031 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
1032 vim_free(m->ocm_name);
1033 vim_free(m->ocm_init);
1034 }
1035 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001036 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001037
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001038 for (int i = 0; i < objmethods.ga_len; ++i)
1039 {
1040 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
1041 func_clear_free(uf, FALSE);
1042 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001043 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001044
1045 for (int i = 0; i < classfunctions.ga_len; ++i)
1046 {
1047 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
1048 func_clear_free(uf, FALSE);
1049 }
1050 ga_clear(&classfunctions);
1051
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001052 clear_type_list(&type_list);
1053}
1054
1055/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001056 * Find member "name" in class "cl", set "member_idx" to the member index and
1057 * return its type.
1058 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001059 */
1060 type_T *
1061class_member_type(
1062 class_T *cl,
1063 char_u *name,
1064 char_u *name_end,
1065 int *member_idx)
1066{
1067 *member_idx = -1; // not found (yet)
1068 size_t len = name_end - name;
1069
1070 for (int i = 0; i < cl->class_obj_member_count; ++i)
1071 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001072 ocmember_T *m = cl->class_obj_members + i;
1073 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001074 {
1075 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001076 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001077 }
1078 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001079
1080 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001081 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001082}
1083
1084/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001085 * Handle ":enum" up to ":endenum".
1086 */
1087 void
1088ex_enum(exarg_T *eap UNUSED)
1089{
1090 // TODO
1091}
1092
1093/*
1094 * Handle ":type".
1095 */
1096 void
1097ex_type(exarg_T *eap UNUSED)
1098{
1099 // TODO
1100}
1101
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001102/*
1103 * Evaluate what comes after a class:
1104 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001105 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001106 * - class constructor: SomeClass.new()
1107 * - object member: someObject.varname
1108 * - object method: someObject.SomeMethod()
1109 *
1110 * "*arg" points to the '.'.
1111 * "*arg" is advanced to after the member name or method call.
1112 *
1113 * Returns FAIL or OK.
1114 */
1115 int
1116class_object_index(
1117 char_u **arg,
1118 typval_T *rettv,
1119 evalarg_T *evalarg,
1120 int verbose UNUSED) // give error messages
1121{
1122 // int evaluate = evalarg != NULL
1123 // && (evalarg->eval_flags & EVAL_EVALUATE);
1124
1125 if (VIM_ISWHITE((*arg)[1]))
1126 {
1127 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
1128 return FAIL;
1129 }
1130
1131 ++*arg;
1132 char_u *name = *arg;
1133 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1134 if (name_end == name)
1135 return FAIL;
1136 size_t len = name_end - name;
1137
1138 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
1139 : rettv->vval.v_object->obj_class;
1140 if (*name_end == '(')
1141 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001142 int on_class = rettv->v_type == VAR_CLASS;
1143 int count = on_class ? cl->class_class_function_count
1144 : cl->class_obj_method_count;
1145 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001146 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001147 ufunc_T *fp = on_class ? cl->class_class_functions[i]
1148 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +00001149 // Use a separate pointer to avoid that ASAN complains about
1150 // uf_name[] only being 4 characters.
1151 char_u *ufname = (char_u *)fp->uf_name;
1152 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001153 {
1154 typval_T argvars[MAX_FUNC_ARGS + 1];
1155 int argcount = 0;
1156
1157 char_u *argp = name_end;
1158 int ret = get_func_arguments(&argp, evalarg, 0,
1159 argvars, &argcount);
1160 if (ret == FAIL)
1161 return FAIL;
1162
1163 funcexe_T funcexe;
1164 CLEAR_FIELD(funcexe);
1165 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001166 if (rettv->v_type == VAR_OBJECT)
1167 {
1168 funcexe.fe_object = rettv->vval.v_object;
1169 ++funcexe.fe_object->obj_refcount;
1170 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001171
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001172 // Clear the class or object after calling the function, in
1173 // case the refcount is one.
1174 typval_T tv_tofree = *rettv;
1175 rettv->v_type = VAR_UNKNOWN;
1176
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001177 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001178 int error = call_user_func_check(fp, argcount, argvars,
1179 rettv, &funcexe, NULL);
1180
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001181 // Clear the previous rettv and the arguments.
1182 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001183 for (int idx = 0; idx < argcount; ++idx)
1184 clear_tv(&argvars[idx]);
1185
1186 if (error != FCERR_NONE)
1187 {
1188 user_func_error(error, printable_func_name(fp),
1189 funcexe.fe_found_var);
1190 return FAIL;
1191 }
1192 *arg = argp;
1193 return OK;
1194 }
1195 }
1196
1197 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
1198 }
1199
1200 else if (rettv->v_type == VAR_OBJECT)
1201 {
1202 for (int i = 0; i < cl->class_obj_member_count; ++i)
1203 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001204 ocmember_T *m = &cl->class_obj_members[i];
1205 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001206 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001207 if (*name == '_')
1208 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001209 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001210 return FAIL;
1211 }
1212
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001213 // The object only contains a pointer to the class, the member
1214 // values array follows right after that.
1215 object_T *obj = rettv->vval.v_object;
1216 typval_T *tv = (typval_T *)(obj + 1) + i;
1217 copy_tv(tv, rettv);
1218 object_unref(obj);
1219
1220 *arg = name_end;
1221 return OK;
1222 }
1223 }
1224
1225 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
1226 }
1227
Bram Moolenaard505d172022-12-18 21:42:55 +00001228 else if (rettv->v_type == VAR_CLASS)
1229 {
1230 // class member
1231 for (int i = 0; i < cl->class_class_member_count; ++i)
1232 {
1233 ocmember_T *m = &cl->class_class_members[i];
1234 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
1235 {
1236 if (*name == '_')
1237 {
1238 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
1239 return FAIL;
1240 }
1241
1242 typval_T *tv = &cl->class_members_tv[i];
1243 copy_tv(tv, rettv);
1244 class_unref(cl);
1245
1246 *arg = name_end;
1247 return OK;
1248 }
1249 }
1250
1251 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
1252 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001253
1254 return FAIL;
1255}
1256
1257/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001258 * If "arg" points to a class or object method, return it.
1259 * Otherwise return NULL.
1260 */
1261 ufunc_T *
1262find_class_func(char_u **arg)
1263{
1264 char_u *name = *arg;
1265 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1266 if (name_end == name || *name_end != '.')
1267 return NULL;
1268
1269 size_t len = name_end - name;
1270 typval_T tv;
1271 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001272 if (eval_variable(name, (int)len,
1273 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001274 return NULL;
1275 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001276 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001277
1278 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1279 : tv.vval.v_object->obj_class;
1280 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001281 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001282 char_u *fname = name_end + 1;
1283 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1284 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001285 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001286 len = fname_end - fname;
1287
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001288 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1289 : cl->class_obj_method_count;
1290 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1291 : cl->class_obj_methods;
1292 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001293 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001294 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001295 // Use a separate pointer to avoid that ASAN complains about
1296 // uf_name[] only being 4 characters.
1297 char_u *ufname = (char_u *)fp->uf_name;
1298 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001299 {
1300 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001301 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001302 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001303 }
1304
Bram Moolenaareb533502022-12-14 15:06:11 +00001305fail_after_eval:
1306 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001307 return NULL;
1308}
1309
1310/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001311 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1312 * index in class.class_class_members[].
1313 * If "cl_ret" is not NULL set it to the class.
1314 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001315 */
1316 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001317class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001318{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001319 if (cctx == NULL || cctx->ctx_ufunc == NULL
1320 || cctx->ctx_ufunc->uf_class == NULL)
1321 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001322 class_T *cl = cctx->ctx_ufunc->uf_class;
1323
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001324 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001325 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001326 ocmember_T *m = &cl->class_class_members[i];
1327 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001328 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001329 if (cl_ret != NULL)
1330 *cl_ret = cl;
1331 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001332 }
1333 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001334 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001335}
1336
1337/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001338 * Make a copy of an object.
1339 */
1340 void
1341copy_object(typval_T *from, typval_T *to)
1342{
1343 *to = *from;
1344 if (to->vval.v_object != NULL)
1345 ++to->vval.v_object->obj_refcount;
1346}
1347
1348/*
1349 * Free an object.
1350 */
1351 static void
1352object_clear(object_T *obj)
1353{
1354 class_T *cl = obj->obj_class;
1355
1356 // the member values are just after the object structure
1357 typval_T *tv = (typval_T *)(obj + 1);
1358 for (int i = 0; i < cl->class_obj_member_count; ++i)
1359 clear_tv(tv + i);
1360
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001361 // Remove from the list headed by "first_object".
1362 object_cleared(obj);
1363
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001364 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001365 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001366}
1367
1368/*
1369 * Unreference an object.
1370 */
1371 void
1372object_unref(object_T *obj)
1373{
1374 if (obj != NULL && --obj->obj_refcount <= 0)
1375 object_clear(obj);
1376}
1377
1378/*
1379 * Make a copy of a class.
1380 */
1381 void
1382copy_class(typval_T *from, typval_T *to)
1383{
1384 *to = *from;
1385 if (to->vval.v_class != NULL)
1386 ++to->vval.v_class->class_refcount;
1387}
1388
1389/*
1390 * Unreference a class. Free it when the reference count goes down to zero.
1391 */
1392 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001393class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001394{
Bram Moolenaard505d172022-12-18 21:42:55 +00001395 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001396 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001397 // Freeing what the class contains may recursively come back here.
1398 // Clear "class_name" first, if it is NULL the class does not need to
1399 // be freed.
1400 VIM_CLEAR(cl->class_name);
1401
Bram Moolenaar83677162023-01-08 19:54:10 +00001402 class_unref(cl->class_extends);
1403
Bram Moolenaar94674f22023-01-06 18:42:20 +00001404 for (int i = 0; i < cl->class_interface_count; ++i)
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001405 {
Bram Moolenaar94674f22023-01-06 18:42:20 +00001406 vim_free(((char_u **)cl->class_interfaces)[i]);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001407 if (cl->class_interfaces_cl[i] != NULL)
1408 class_unref(cl->class_interfaces_cl[i]);
1409 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001410 vim_free(cl->class_interfaces);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001411 vim_free(cl->class_interfaces_cl);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001412
Bram Moolenaard505d172022-12-18 21:42:55 +00001413 for (int i = 0; i < cl->class_class_member_count; ++i)
1414 {
1415 ocmember_T *m = &cl->class_class_members[i];
1416 vim_free(m->ocm_name);
1417 vim_free(m->ocm_init);
1418 if (cl->class_members_tv != NULL)
1419 clear_tv(&cl->class_members_tv[i]);
1420 }
1421 vim_free(cl->class_class_members);
1422 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001423
1424 for (int i = 0; i < cl->class_obj_member_count; ++i)
1425 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001426 ocmember_T *m = &cl->class_obj_members[i];
1427 vim_free(m->ocm_name);
1428 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001429 }
1430 vim_free(cl->class_obj_members);
1431
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001432 for (int i = 0; i < cl->class_class_function_count; ++i)
1433 {
1434 ufunc_T *uf = cl->class_class_functions[i];
1435 func_clear_free(uf, FALSE);
1436 }
1437 vim_free(cl->class_class_functions);
1438
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001439 for (int i = 0; i < cl->class_obj_method_count; ++i)
1440 {
1441 ufunc_T *uf = cl->class_obj_methods[i];
1442 func_clear_free(uf, FALSE);
1443 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001444 vim_free(cl->class_obj_methods);
1445
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001446 clear_type_list(&cl->class_type_list);
1447
1448 vim_free(cl);
1449 }
1450}
1451
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001452static object_T *first_object = NULL;
1453
1454/*
1455 * Call this function when an object has been created. It will be added to the
1456 * list headed by "first_object".
1457 */
1458 void
1459object_created(object_T *obj)
1460{
1461 if (first_object != NULL)
1462 {
1463 obj->obj_next_used = first_object;
1464 first_object->obj_prev_used = obj;
1465 }
1466 first_object = obj;
1467}
1468
1469/*
1470 * Call this function when an object has been cleared and is about to be freed.
1471 * It is removed from the list headed by "first_object".
1472 */
1473 void
1474object_cleared(object_T *obj)
1475{
1476 if (obj->obj_next_used != NULL)
1477 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1478 if (obj->obj_prev_used != NULL)
1479 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1480 else if (first_object == obj)
1481 first_object = obj->obj_next_used;
1482}
1483
1484/*
1485 * Go through the list of all objects and free items without "copyID".
1486 */
1487 int
1488object_free_nonref(int copyID)
1489{
1490 int did_free = FALSE;
1491 object_T *next_obj;
1492
1493 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
1494 {
1495 next_obj = obj->obj_next_used;
1496 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1497 {
1498 // Free the object and items it contains.
1499 object_clear(obj);
1500 did_free = TRUE;
1501 }
1502 }
1503
1504 return did_free;
1505}
1506
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001507
1508#endif // FEAT_EVAL