blob: 3169f6220c5826e994dfc7d666d385ead3e5cc86 [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 Moolenaard40f00c2023-01-13 17:36:49 +0000702 if (success)
703 {
704 // Check no function argument name is used as an object/class member.
705 for (int loop = 1; loop <= 2 && success; ++loop)
706 {
707 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
708
709 for (int fi = 0; fi < gap->ga_len && success; ++fi)
710 {
711 ufunc_T *uf = ((ufunc_T **)gap->ga_data)[fi];
712
713 for (int i = 0; i < uf->uf_args.ga_len && success; ++i)
714 {
715 char_u *aname = ((char_u **)uf->uf_args.ga_data)[i];
716 for (int il = 1; il <= 2 && success; ++il)
717 {
718 // For a "new()" function "this.member" arguments are
719 // OK. TODO: check for the "this." prefix.
720 if (STRNCMP(uf->uf_name, "new", 3) == NULL && il == 2)
721 continue;
722 garray_T *mgap = il == 1 ? &classmembers : &objmembers;
723 for (int mi = 0; mi < mgap->ga_len; ++mi)
724 {
725 char_u *mname = ((ocmember_T *)mgap->ga_data
726 + mi)->ocm_name;
727 if (STRCMP(aname, mname) == 0)
728 {
729 success = FALSE;
730 semsg(_(e_argument_already_declared_in_class_str),
731 aname);
732 break;
733 }
734 }
735 }
736 }
737 }
738 }
739 }
740
741
Bram Moolenaareb533502022-12-14 15:06:11 +0000742 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000743 if (success)
744 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000745 // "endclass" encountered without failures: Create the class.
746
Bram Moolenaareb533502022-12-14 15:06:11 +0000747 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000748 if (cl == NULL)
749 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000750 if (!is_class)
751 cl->class_flags = CLASS_INTERFACE;
752
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000753 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +0000754 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +0000755 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000756 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000757
Bram Moolenaar83677162023-01-08 19:54:10 +0000758 cl->class_extends = extends_cl;
759
Bram Moolenaar94674f22023-01-06 18:42:20 +0000760 if (ga_impl.ga_len > 0)
761 {
762 // Move the "implements" names into the class.
763 cl->class_interface_count = ga_impl.ga_len;
764 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
765 if (cl->class_interfaces == NULL)
766 goto cleanup;
767 for (int i = 0; i < ga_impl.ga_len; ++i)
768 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
Bram Moolenaar7d4d87b2023-01-06 18:59:08 +0000769 VIM_CLEAR(ga_impl.ga_data);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000770 ga_impl.ga_len = 0;
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000771
772 cl->class_interfaces_cl = intf_classes;
773 intf_classes = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +0000774 }
775
Bram Moolenaard505d172022-12-18 21:42:55 +0000776 // Add class and object members to "cl".
777 if (add_members_to_class(&classmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000778 extends_cl == NULL ? NULL
779 : extends_cl->class_class_members,
780 extends_cl == NULL ? 0
781 : extends_cl->class_class_member_count,
782 &cl->class_class_members,
783 &cl->class_class_member_count) == FAIL
Bram Moolenaard505d172022-12-18 21:42:55 +0000784 || add_members_to_class(&objmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000785 extends_cl == NULL ? NULL
786 : extends_cl->class_obj_members,
787 extends_cl == NULL ? 0
788 : extends_cl->class_obj_member_count,
789 &cl->class_obj_members,
790 &cl->class_obj_member_count) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000791 goto cleanup;
792
Bram Moolenaar554d0312023-01-05 19:59:18 +0000793 if (is_class && cl->class_class_member_count > 0)
Bram Moolenaard505d172022-12-18 21:42:55 +0000794 {
795 // Allocate a typval for each class member and initialize it.
796 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
797 cl->class_class_member_count);
798 if (cl->class_members_tv != NULL)
799 for (int i = 0; i < cl->class_class_member_count; ++i)
800 {
801 ocmember_T *m = &cl->class_class_members[i];
802 typval_T *tv = &cl->class_members_tv[i];
803 if (m->ocm_init != NULL)
804 {
805 typval_T *etv = eval_expr(m->ocm_init, eap);
806 if (etv != NULL)
807 {
808 *tv = *etv;
809 vim_free(etv);
810 }
811 }
812 else
813 {
814 // TODO: proper default value
815 tv->v_type = m->ocm_type->tt_type;
816 tv->vval.v_string = NULL;
817 }
818 }
819 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000820
821 int have_new = FALSE;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000822 for (int i = 0; i < classfunctions.ga_len; ++i)
823 if (STRCMP(((ufunc_T **)classfunctions.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000824 "new") == 0)
825 {
826 have_new = TRUE;
827 break;
828 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000829 if (is_class && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000830 {
831 // No new() method was defined, add the default constructor.
832 garray_T fga;
833 ga_init2(&fga, 1, 1000);
834 ga_concat(&fga, (char_u *)"new(");
835 for (int i = 0; i < cl->class_obj_member_count; ++i)
836 {
837 if (i > 0)
838 ga_concat(&fga, (char_u *)", ");
839 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000840 ocmember_T *m = cl->class_obj_members + i;
841 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000842 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000843 }
844 ga_concat(&fga, (char_u *)")\nenddef\n");
845 ga_append(&fga, NUL);
846
847 exarg_T fea;
848 CLEAR_FIELD(fea);
849 fea.cmdidx = CMD_def;
850 fea.cmd = fea.arg = fga.ga_data;
851
852 garray_T lines_to_free;
853 ga_init2(&lines_to_free, sizeof(char_u *), 50);
854
Bram Moolenaar2c011312023-01-07 10:51:30 +0000855 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000856
857 ga_clear_strings(&lines_to_free);
858 vim_free(fga.ga_data);
859
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000860 if (nf != NULL && ga_grow(&classfunctions, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000861 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000862 ((ufunc_T **)classfunctions.ga_data)[classfunctions.ga_len]
863 = nf;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000864 ++classfunctions.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000865
866 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000867 nf->uf_ret_type = get_type_ptr(&type_list);
868 if (nf->uf_ret_type != NULL)
869 {
870 nf->uf_ret_type->tt_type = VAR_OBJECT;
871 nf->uf_ret_type->tt_member = (type_T *)cl;
872 nf->uf_ret_type->tt_argcount = 0;
873 nf->uf_ret_type->tt_args = NULL;
874 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000875 }
876 }
877
Bram Moolenaar58b40092023-01-11 15:59:05 +0000878 // Move all the functions into the created class.
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000879 // loop 1: class functions, loop 2: object methods
880 for (int loop = 1; loop <= 2; ++loop)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000881 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000882 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
883 int *fcount = loop == 1 ? &cl->class_class_function_count
884 : &cl->class_obj_method_count;
885 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
886 : &cl->class_obj_methods;
887
Bram Moolenaar83677162023-01-08 19:54:10 +0000888 int parent_count = 0;
889 if (extends_cl != NULL)
890 // Include functions from the parent.
891 parent_count = loop == 1
892 ? extends_cl->class_class_function_count
893 : extends_cl->class_obj_method_count;
894
895 *fcount = parent_count + gap->ga_len;
896 if (*fcount == 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000897 {
898 *fup = NULL;
899 continue;
900 }
Bram Moolenaar83677162023-01-08 19:54:10 +0000901 *fup = ALLOC_MULT(ufunc_T *, *fcount);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000902 if (*fup == NULL)
903 goto cleanup;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000904
Bram Moolenaar58b40092023-01-11 15:59:05 +0000905 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
906 vim_free(gap->ga_data);
907 if (loop == 1)
908 cl->class_class_function_count_child = gap->ga_len;
909 else
910 cl->class_obj_method_count_child = gap->ga_len;
911
Bram Moolenaar83677162023-01-08 19:54:10 +0000912 int skipped = 0;
913 for (int i = 0; i < parent_count; ++i)
914 {
915 // Copy functions from the parent. Can't use the same
916 // function, because "uf_class" is different and compilation
917 // will have a different result.
Bram Moolenaar58b40092023-01-11 15:59:05 +0000918 // Put them after the functions in the current class, object
919 // methods may be overruled, then "super.Method()" is used to
920 // find a method from the parent.
Bram Moolenaar83677162023-01-08 19:54:10 +0000921 // Skip "new" functions. TODO: not all of them.
922 if (loop == 1 && STRNCMP(
923 extends_cl->class_class_functions[i]->uf_name,
924 "new", 3) == 0)
925 ++skipped;
926 else
Bram Moolenaar58b40092023-01-11 15:59:05 +0000927 {
928 ufunc_T *pf = (loop == 1
Bram Moolenaar83677162023-01-08 19:54:10 +0000929 ? extends_cl->class_class_functions
Bram Moolenaar58b40092023-01-11 15:59:05 +0000930 : extends_cl->class_obj_methods)[i];
931 (*fup)[gap->ga_len + i - skipped] = copy_function(pf);
932
933 // If the child class overrides a function from the parent
934 // the signature must be equal.
935 char_u *pname = pf->uf_name;
936 for (int ci = 0; ci < gap->ga_len; ++ci)
937 {
938 ufunc_T *cf = (*fup)[ci];
939 char_u *cname = cf->uf_name;
940 if (STRCMP(pname, cname) == 0)
941 {
942 where_T where = WHERE_INIT;
943 where.wt_func_name = (char *)pname;
944 (void)check_type(pf->uf_func_type, cf->uf_func_type,
945 TRUE, where);
946 }
947 }
948 }
Bram Moolenaar83677162023-01-08 19:54:10 +0000949 }
950
Bram Moolenaar83677162023-01-08 19:54:10 +0000951 *fcount -= skipped;
952
953 // Set the class pointer on all the functions and object methods.
954 for (int i = 0; i < *fcount; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000955 {
956 ufunc_T *fp = (*fup)[i];
957 fp->uf_class = cl;
958 if (loop == 2)
959 fp->uf_flags |= FC_OBJECT;
960 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000961 }
962
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000963 cl->class_type.tt_type = VAR_CLASS;
964 cl->class_type.tt_member = (type_T *)cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000965 cl->class_object_type.tt_type = VAR_OBJECT;
966 cl->class_object_type.tt_member = (type_T *)cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000967 cl->class_type_list = type_list;
968
969 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +0000970 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000971
972 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +0000973 // TODO: handle other context, e.g. in a function
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000974 typval_T tv;
975 tv.v_type = VAR_CLASS;
976 tv.vval.v_class = cl;
Bram Moolenaara86655a2023-01-12 17:06:27 +0000977 is_export = class_export;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000978 set_var_const(cl->class_name, current_sctx.sc_sid,
979 NULL, &tv, FALSE, ASSIGN_DECL, 0);
980 return;
981 }
982
983cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +0000984 if (cl != NULL)
985 {
986 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000987 vim_free(cl->class_class_functions);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000988 if (cl->class_interfaces != NULL)
989 {
990 for (int i = 0; i < cl->class_interface_count; ++i)
991 vim_free(cl->class_interfaces[i]);
992 vim_free(cl->class_interfaces);
993 }
994 if (cl->class_interfaces_cl != NULL)
995 {
996 for (int i = 0; i < cl->class_interface_count; ++i)
997 class_unref(cl->class_interfaces_cl[i]);
998 vim_free(cl->class_interfaces_cl);
999 }
Bram Moolenaareb533502022-12-14 15:06:11 +00001000 vim_free(cl->class_obj_members);
1001 vim_free(cl->class_obj_methods);
1002 vim_free(cl);
1003 }
1004
Bram Moolenaar83677162023-01-08 19:54:10 +00001005 vim_free(extends);
1006 class_unref(extends_cl);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001007
1008 if (intf_classes != NULL)
1009 {
1010 for (int i = 0; i < ga_impl.ga_len; ++i)
1011 class_unref(intf_classes[i]);
1012 vim_free(intf_classes);
1013 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001014 ga_clear_strings(&ga_impl);
1015
Bram Moolenaard505d172022-12-18 21:42:55 +00001016 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001017 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001018 garray_T *gap = round == 1 ? &classmembers : &objmembers;
1019 if (gap->ga_len == 0 || gap->ga_data == NULL)
1020 continue;
1021
1022 for (int i = 0; i < gap->ga_len; ++i)
1023 {
1024 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
1025 vim_free(m->ocm_name);
1026 vim_free(m->ocm_init);
1027 }
1028 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001029 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001030
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001031 for (int i = 0; i < objmethods.ga_len; ++i)
1032 {
1033 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
1034 func_clear_free(uf, FALSE);
1035 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001036 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001037
1038 for (int i = 0; i < classfunctions.ga_len; ++i)
1039 {
1040 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
1041 func_clear_free(uf, FALSE);
1042 }
1043 ga_clear(&classfunctions);
1044
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001045 clear_type_list(&type_list);
1046}
1047
1048/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001049 * Find member "name" in class "cl", set "member_idx" to the member index and
1050 * return its type.
1051 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001052 */
1053 type_T *
1054class_member_type(
1055 class_T *cl,
1056 char_u *name,
1057 char_u *name_end,
1058 int *member_idx)
1059{
1060 *member_idx = -1; // not found (yet)
1061 size_t len = name_end - name;
1062
1063 for (int i = 0; i < cl->class_obj_member_count; ++i)
1064 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001065 ocmember_T *m = cl->class_obj_members + i;
1066 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001067 {
1068 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001069 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001070 }
1071 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001072
1073 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001074 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001075}
1076
1077/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001078 * Handle ":enum" up to ":endenum".
1079 */
1080 void
1081ex_enum(exarg_T *eap UNUSED)
1082{
1083 // TODO
1084}
1085
1086/*
1087 * Handle ":type".
1088 */
1089 void
1090ex_type(exarg_T *eap UNUSED)
1091{
1092 // TODO
1093}
1094
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001095/*
1096 * Evaluate what comes after a class:
1097 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001098 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001099 * - class constructor: SomeClass.new()
1100 * - object member: someObject.varname
1101 * - object method: someObject.SomeMethod()
1102 *
1103 * "*arg" points to the '.'.
1104 * "*arg" is advanced to after the member name or method call.
1105 *
1106 * Returns FAIL or OK.
1107 */
1108 int
1109class_object_index(
1110 char_u **arg,
1111 typval_T *rettv,
1112 evalarg_T *evalarg,
1113 int verbose UNUSED) // give error messages
1114{
1115 // int evaluate = evalarg != NULL
1116 // && (evalarg->eval_flags & EVAL_EVALUATE);
1117
1118 if (VIM_ISWHITE((*arg)[1]))
1119 {
1120 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
1121 return FAIL;
1122 }
1123
1124 ++*arg;
1125 char_u *name = *arg;
1126 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1127 if (name_end == name)
1128 return FAIL;
1129 size_t len = name_end - name;
1130
1131 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
1132 : rettv->vval.v_object->obj_class;
1133 if (*name_end == '(')
1134 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001135 int on_class = rettv->v_type == VAR_CLASS;
1136 int count = on_class ? cl->class_class_function_count
1137 : cl->class_obj_method_count;
1138 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001139 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001140 ufunc_T *fp = on_class ? cl->class_class_functions[i]
1141 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +00001142 // Use a separate pointer to avoid that ASAN complains about
1143 // uf_name[] only being 4 characters.
1144 char_u *ufname = (char_u *)fp->uf_name;
1145 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001146 {
1147 typval_T argvars[MAX_FUNC_ARGS + 1];
1148 int argcount = 0;
1149
1150 char_u *argp = name_end;
1151 int ret = get_func_arguments(&argp, evalarg, 0,
1152 argvars, &argcount);
1153 if (ret == FAIL)
1154 return FAIL;
1155
1156 funcexe_T funcexe;
1157 CLEAR_FIELD(funcexe);
1158 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001159 if (rettv->v_type == VAR_OBJECT)
1160 {
1161 funcexe.fe_object = rettv->vval.v_object;
1162 ++funcexe.fe_object->obj_refcount;
1163 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001164
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001165 // Clear the class or object after calling the function, in
1166 // case the refcount is one.
1167 typval_T tv_tofree = *rettv;
1168 rettv->v_type = VAR_UNKNOWN;
1169
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001170 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001171 int error = call_user_func_check(fp, argcount, argvars,
1172 rettv, &funcexe, NULL);
1173
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001174 // Clear the previous rettv and the arguments.
1175 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001176 for (int idx = 0; idx < argcount; ++idx)
1177 clear_tv(&argvars[idx]);
1178
1179 if (error != FCERR_NONE)
1180 {
1181 user_func_error(error, printable_func_name(fp),
1182 funcexe.fe_found_var);
1183 return FAIL;
1184 }
1185 *arg = argp;
1186 return OK;
1187 }
1188 }
1189
1190 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
1191 }
1192
1193 else if (rettv->v_type == VAR_OBJECT)
1194 {
1195 for (int i = 0; i < cl->class_obj_member_count; ++i)
1196 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001197 ocmember_T *m = &cl->class_obj_members[i];
1198 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001199 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001200 if (*name == '_')
1201 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001202 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001203 return FAIL;
1204 }
1205
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001206 // The object only contains a pointer to the class, the member
1207 // values array follows right after that.
1208 object_T *obj = rettv->vval.v_object;
1209 typval_T *tv = (typval_T *)(obj + 1) + i;
1210 copy_tv(tv, rettv);
1211 object_unref(obj);
1212
1213 *arg = name_end;
1214 return OK;
1215 }
1216 }
1217
1218 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
1219 }
1220
Bram Moolenaard505d172022-12-18 21:42:55 +00001221 else if (rettv->v_type == VAR_CLASS)
1222 {
1223 // class member
1224 for (int i = 0; i < cl->class_class_member_count; ++i)
1225 {
1226 ocmember_T *m = &cl->class_class_members[i];
1227 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
1228 {
1229 if (*name == '_')
1230 {
1231 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
1232 return FAIL;
1233 }
1234
1235 typval_T *tv = &cl->class_members_tv[i];
1236 copy_tv(tv, rettv);
1237 class_unref(cl);
1238
1239 *arg = name_end;
1240 return OK;
1241 }
1242 }
1243
1244 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
1245 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001246
1247 return FAIL;
1248}
1249
1250/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001251 * If "arg" points to a class or object method, return it.
1252 * Otherwise return NULL.
1253 */
1254 ufunc_T *
1255find_class_func(char_u **arg)
1256{
1257 char_u *name = *arg;
1258 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1259 if (name_end == name || *name_end != '.')
1260 return NULL;
1261
1262 size_t len = name_end - name;
1263 typval_T tv;
1264 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001265 if (eval_variable(name, (int)len,
1266 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001267 return NULL;
1268 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001269 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001270
1271 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1272 : tv.vval.v_object->obj_class;
1273 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001274 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001275 char_u *fname = name_end + 1;
1276 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1277 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001278 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001279 len = fname_end - fname;
1280
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001281 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1282 : cl->class_obj_method_count;
1283 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1284 : cl->class_obj_methods;
1285 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001286 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001287 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001288 // Use a separate pointer to avoid that ASAN complains about
1289 // uf_name[] only being 4 characters.
1290 char_u *ufname = (char_u *)fp->uf_name;
1291 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001292 {
1293 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001294 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001295 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001296 }
1297
Bram Moolenaareb533502022-12-14 15:06:11 +00001298fail_after_eval:
1299 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001300 return NULL;
1301}
1302
1303/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001304 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1305 * index in class.class_class_members[].
1306 * If "cl_ret" is not NULL set it to the class.
1307 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001308 */
1309 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001310class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001311{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001312 if (cctx == NULL || cctx->ctx_ufunc == NULL
1313 || cctx->ctx_ufunc->uf_class == NULL)
1314 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001315 class_T *cl = cctx->ctx_ufunc->uf_class;
1316
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001317 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001318 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001319 ocmember_T *m = &cl->class_class_members[i];
1320 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001321 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001322 if (cl_ret != NULL)
1323 *cl_ret = cl;
1324 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001325 }
1326 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001327 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001328}
1329
1330/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001331 * Make a copy of an object.
1332 */
1333 void
1334copy_object(typval_T *from, typval_T *to)
1335{
1336 *to = *from;
1337 if (to->vval.v_object != NULL)
1338 ++to->vval.v_object->obj_refcount;
1339}
1340
1341/*
1342 * Free an object.
1343 */
1344 static void
1345object_clear(object_T *obj)
1346{
1347 class_T *cl = obj->obj_class;
1348
1349 // the member values are just after the object structure
1350 typval_T *tv = (typval_T *)(obj + 1);
1351 for (int i = 0; i < cl->class_obj_member_count; ++i)
1352 clear_tv(tv + i);
1353
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001354 // Remove from the list headed by "first_object".
1355 object_cleared(obj);
1356
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001357 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001358 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001359}
1360
1361/*
1362 * Unreference an object.
1363 */
1364 void
1365object_unref(object_T *obj)
1366{
1367 if (obj != NULL && --obj->obj_refcount <= 0)
1368 object_clear(obj);
1369}
1370
1371/*
1372 * Make a copy of a class.
1373 */
1374 void
1375copy_class(typval_T *from, typval_T *to)
1376{
1377 *to = *from;
1378 if (to->vval.v_class != NULL)
1379 ++to->vval.v_class->class_refcount;
1380}
1381
1382/*
1383 * Unreference a class. Free it when the reference count goes down to zero.
1384 */
1385 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001386class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001387{
Bram Moolenaard505d172022-12-18 21:42:55 +00001388 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001389 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001390 // Freeing what the class contains may recursively come back here.
1391 // Clear "class_name" first, if it is NULL the class does not need to
1392 // be freed.
1393 VIM_CLEAR(cl->class_name);
1394
Bram Moolenaar83677162023-01-08 19:54:10 +00001395 class_unref(cl->class_extends);
1396
Bram Moolenaar94674f22023-01-06 18:42:20 +00001397 for (int i = 0; i < cl->class_interface_count; ++i)
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001398 {
Bram Moolenaar94674f22023-01-06 18:42:20 +00001399 vim_free(((char_u **)cl->class_interfaces)[i]);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001400 if (cl->class_interfaces_cl[i] != NULL)
1401 class_unref(cl->class_interfaces_cl[i]);
1402 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001403 vim_free(cl->class_interfaces);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001404 vim_free(cl->class_interfaces_cl);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001405
Bram Moolenaard505d172022-12-18 21:42:55 +00001406 for (int i = 0; i < cl->class_class_member_count; ++i)
1407 {
1408 ocmember_T *m = &cl->class_class_members[i];
1409 vim_free(m->ocm_name);
1410 vim_free(m->ocm_init);
1411 if (cl->class_members_tv != NULL)
1412 clear_tv(&cl->class_members_tv[i]);
1413 }
1414 vim_free(cl->class_class_members);
1415 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001416
1417 for (int i = 0; i < cl->class_obj_member_count; ++i)
1418 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001419 ocmember_T *m = &cl->class_obj_members[i];
1420 vim_free(m->ocm_name);
1421 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001422 }
1423 vim_free(cl->class_obj_members);
1424
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001425 for (int i = 0; i < cl->class_class_function_count; ++i)
1426 {
1427 ufunc_T *uf = cl->class_class_functions[i];
1428 func_clear_free(uf, FALSE);
1429 }
1430 vim_free(cl->class_class_functions);
1431
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001432 for (int i = 0; i < cl->class_obj_method_count; ++i)
1433 {
1434 ufunc_T *uf = cl->class_obj_methods[i];
1435 func_clear_free(uf, FALSE);
1436 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001437 vim_free(cl->class_obj_methods);
1438
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001439 clear_type_list(&cl->class_type_list);
1440
1441 vim_free(cl);
1442 }
1443}
1444
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001445static object_T *first_object = NULL;
1446
1447/*
1448 * Call this function when an object has been created. It will be added to the
1449 * list headed by "first_object".
1450 */
1451 void
1452object_created(object_T *obj)
1453{
1454 if (first_object != NULL)
1455 {
1456 obj->obj_next_used = first_object;
1457 first_object->obj_prev_used = obj;
1458 }
1459 first_object = obj;
1460}
1461
1462/*
1463 * Call this function when an object has been cleared and is about to be freed.
1464 * It is removed from the list headed by "first_object".
1465 */
1466 void
1467object_cleared(object_T *obj)
1468{
1469 if (obj->obj_next_used != NULL)
1470 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1471 if (obj->obj_prev_used != NULL)
1472 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1473 else if (first_object == obj)
1474 first_object = obj->obj_next_used;
1475}
1476
1477/*
1478 * Go through the list of all objects and free items without "copyID".
1479 */
1480 int
1481object_free_nonref(int copyID)
1482{
1483 int did_free = FALSE;
1484 object_T *next_obj;
1485
1486 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
1487 {
1488 next_obj = obj->obj_next_used;
1489 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1490 {
1491 // Free the object and items it contains.
1492 object_clear(obj);
1493 did_free = TRUE;
1494 }
1495 }
1496
1497 return did_free;
1498}
1499
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001500
1501#endif // FEAT_EVAL