blob: a60e70d2a05844680be097efefb2f80ab8e876bc [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
247 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000248 // generics: <Tkey, Tentry>
Bram Moolenaard505d172022-12-18 21:42:55 +0000249 // handle "is_export" if it is set
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000250
Bram Moolenaar83677162023-01-08 19:54:10 +0000251 // Name for "extends BaseClass"
252 char_u *extends = NULL;
253
Bram Moolenaar94674f22023-01-06 18:42:20 +0000254 // Names for "implements SomeInterface"
255 garray_T ga_impl;
256 ga_init2(&ga_impl, sizeof(char_u *), 5);
257
258 arg = skipwhite(name_end);
259 while (*arg != NUL && *arg != '#' && *arg != '\n')
260 {
261 // TODO:
Bram Moolenaar94674f22023-01-06 18:42:20 +0000262 // specifies SomeInterface
Bram Moolenaar83677162023-01-08 19:54:10 +0000263 if (STRNCMP(arg, "extends", 7) == 0 && IS_WHITE_OR_NUL(arg[7]))
264 {
265 if (extends != NULL)
266 {
267 emsg(_(e_duplicate_extends));
268 goto early_ret;
269 }
270 arg = skipwhite(arg + 7);
271 char_u *end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
272 if (!IS_WHITE_OR_NUL(*end))
273 {
274 semsg(_(e_white_space_required_after_name_str), arg);
275 goto early_ret;
276 }
277 extends = vim_strnsave(arg, end - arg);
278 if (extends == NULL)
279 goto early_ret;
280
281 arg = skipwhite(end + 1);
282 }
283 else if (STRNCMP(arg, "implements", 10) == 0
284 && IS_WHITE_OR_NUL(arg[10]))
Bram Moolenaar94674f22023-01-06 18:42:20 +0000285 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000286 if (ga_impl.ga_len > 0)
287 {
288 emsg(_(e_duplicate_implements));
289 goto early_ret;
290 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000291 arg = skipwhite(arg + 10);
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000292
293 for (;;)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000294 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000295 char_u *impl_end = find_name_end(arg, NULL, NULL,
296 FNE_CHECK_START);
297 if (!IS_WHITE_OR_NUL(*impl_end) && *impl_end != ',')
298 {
299 semsg(_(e_white_space_required_after_name_str), arg);
300 goto early_ret;
301 }
302 char_u *iname = vim_strnsave(arg, impl_end - arg);
303 if (iname == NULL)
304 goto early_ret;
305 for (int i = 0; i < ga_impl.ga_len; ++i)
306 if (STRCMP(((char_u **)ga_impl.ga_data)[i], iname) == 0)
307 {
308 semsg(_(e_duplicate_interface_after_implements_str),
309 iname);
310 vim_free(iname);
311 goto early_ret;
312 }
313 if (ga_add_string(&ga_impl, iname) == FAIL)
314 {
315 vim_free(iname);
316 goto early_ret;
317 }
318 if (*impl_end != ',')
319 {
320 arg = skipwhite(impl_end);
321 break;
322 }
323 arg = skipwhite(impl_end + 1);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000324 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000325 }
326 else
327 {
328 semsg(_(e_trailing_characters_str), arg);
329early_ret:
Bram Moolenaar83677162023-01-08 19:54:10 +0000330 vim_free(extends);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000331 ga_clear_strings(&ga_impl);
332 return;
333 }
334 }
335
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000336 garray_T type_list; // list of pointers to allocated types
337 ga_init2(&type_list, sizeof(type_T *), 10);
338
Bram Moolenaard505d172022-12-18 21:42:55 +0000339 // Growarray with class members declared in the class.
340 garray_T classmembers;
341 ga_init2(&classmembers, sizeof(ocmember_T), 10);
342
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000343 // Growarray with functions declared in the class.
344 garray_T classfunctions;
345 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +0000346
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000347 // Growarray with object members declared in the class.
348 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +0000349 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000350
351 // Growarray with object methods declared in the class.
352 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000353 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000354
355 /*
Bram Moolenaar554d0312023-01-05 19:59:18 +0000356 * Go over the body of the class/interface until "endclass" or
357 * "endinterface" is found.
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000358 */
359 char_u *theline = NULL;
360 int success = FALSE;
361 for (;;)
362 {
363 vim_free(theline);
364 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
365 if (theline == NULL)
366 break;
367 char_u *line = skipwhite(theline);
368
Bram Moolenaar418b5472022-12-20 13:38:22 +0000369 // Skip empty and comment lines.
370 if (*line == NUL)
371 continue;
372 if (*line == '#')
373 {
374 if (vim9_bad_comment(line))
375 break;
376 continue;
377 }
378
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000379 char_u *p = line;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000380 char *end_name = is_class ? "endclass" : "endinterface";
381 if (checkforcmd(&p, end_name, is_class ? 4 : 5))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000382 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000383 if (STRNCMP(line, end_name, is_class ? 8 : 12) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000384 semsg(_(e_command_cannot_be_shortened_str), line);
385 else if (*p == '|' || !ends_excmd2(line, p))
386 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +0000387 else
388 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000389 break;
390 }
Bram Moolenaar554d0312023-01-05 19:59:18 +0000391 char *wrong_name = is_class ? "endinterface" : "endclass";
392 if (checkforcmd(&p, wrong_name, is_class ? 5 : 4))
393 {
394 semsg(_(e_invalid_command_str), line);
395 break;
396 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000397
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000398 int has_public = FALSE;
399 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000400 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000401 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000402 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000403 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000404 break;
405 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000406 has_public = TRUE;
407 p = skipwhite(line + 6);
408
Bram Moolenaard505d172022-12-18 21:42:55 +0000409 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000410 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000411 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000412 break;
413 }
414 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000415
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000416 int has_static = FALSE;
417 char_u *ps = p;
418 if (checkforcmd(&p, "static", 4))
419 {
420 if (STRNCMP(ps, "static", 6) != 0)
421 {
422 semsg(_(e_command_cannot_be_shortened_str), ps);
423 break;
424 }
425 has_static = TRUE;
426 p = skipwhite(ps + 6);
427 }
428
Bram Moolenaard505d172022-12-18 21:42:55 +0000429 // object members (public, read access, private):
430 // "this._varname"
431 // "this.varname"
432 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000433 if (STRNCMP(p, "this", 4) == 0)
434 {
435 if (p[4] != '.' || !eval_isnamec1(p[5]))
436 {
437 semsg(_(e_invalid_object_member_declaration_str), p);
438 break;
439 }
440 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +0000441 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +0000442 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +0000443 char_u *init_expr = NULL;
444 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000445 &varname_end, &type_list, &type,
446 is_class ? &init_expr: NULL) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000447 break;
448 if (add_member(&objmembers, varname, varname_end,
449 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000450 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000451 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000452 break;
453 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000454 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000455
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000456 // constructors:
457 // def new()
458 // enddef
459 // def newOther()
460 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000461 // object methods and class functions:
462 // def SomeMethod()
463 // enddef
464 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000465 // enddef
466 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000467 // def <Tval> someMethod()
468 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000469 else if (checkforcmd(&p, "def", 3))
470 {
471 exarg_T ea;
472 garray_T lines_to_free;
473
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000474 // TODO: error for "public static def Func()"?
475
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000476 CLEAR_FIELD(ea);
477 ea.cmd = line;
478 ea.arg = p;
479 ea.cmdidx = CMD_def;
480 ea.getline = eap->getline;
481 ea.cookie = eap->cookie;
482
483 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000484 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free,
485 is_class ? CF_CLASS : CF_INTERFACE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000486 ga_clear_strings(&lines_to_free);
487
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000488 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000489 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000490 char_u *name = uf->uf_name;
491 int is_new = STRNCMP(name, "new", 3) == 0;
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000492 garray_T *fgap = has_static || is_new
493 ? &classfunctions : &objmethods;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000494 // Check the name isn't used already.
495 for (int i = 0; i < fgap->ga_len; ++i)
496 {
497 char_u *n = ((ufunc_T **)fgap->ga_data)[i]->uf_name;
498 if (STRCMP(name, n) == 0)
499 {
500 semsg(_(e_duplicate_function_str), name);
501 break;
502 }
503 }
504
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000505 if (ga_grow(fgap, 1) == OK)
506 {
507 if (is_new)
508 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000509
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000510 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
511 ++fgap->ga_len;
512 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000513 }
514 }
515
516 // class members
517 else if (has_static)
518 {
519 // class members (public, read access, private):
520 // "static _varname"
521 // "static varname"
522 // "public static varname"
523 char_u *varname = p;
524 char_u *varname_end = NULL;
525 type_T *type = NULL;
526 char_u *init_expr = NULL;
527 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000528 &varname_end, &type_list, &type,
529 is_class ? &init_expr : NULL) == FAIL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000530 break;
531 if (add_member(&classmembers, varname, varname_end,
532 has_public, type, init_expr) == FAIL)
533 {
534 vim_free(init_expr);
535 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000536 }
537 }
538
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000539 else
540 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000541 if (is_class)
542 semsg(_(e_not_valid_command_in_class_str), line);
543 else
544 semsg(_(e_not_valid_command_in_interface_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000545 break;
546 }
547 }
548 vim_free(theline);
549
Bram Moolenaar83677162023-01-08 19:54:10 +0000550 class_T *extends_cl = NULL; // class from "extends" argument
551
552 /*
553 * Check a few things before defining the class.
554 */
555
556 // Check the "extends" class is valid.
557 if (success && extends != NULL)
558 {
559 typval_T tv;
560 tv.v_type = VAR_UNKNOWN;
561 if (eval_variable(extends, 0, 0, &tv, NULL, EVAL_VAR_IMPORT) == FAIL)
562 {
563 semsg(_(e_class_name_not_found_str), extends);
564 success = FALSE;
565 }
566 else
567 {
568 if (tv.v_type != VAR_CLASS
569 || tv.vval.v_class == NULL
570 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) != 0)
571 {
572 semsg(_(e_cannot_extend_str), extends);
573 success = FALSE;
574 }
575 else
576 {
577 extends_cl = tv.vval.v_class;
578 ++extends_cl->class_refcount;
579 }
580 clear_tv(&tv);
581 }
582 }
583 VIM_CLEAR(extends);
584
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000585 class_T **intf_classes = NULL;
586
Bram Moolenaar83677162023-01-08 19:54:10 +0000587 // Check all "implements" entries are valid.
Bram Moolenaar94674f22023-01-06 18:42:20 +0000588 if (success && ga_impl.ga_len > 0)
589 {
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000590 intf_classes = ALLOC_CLEAR_MULT(class_T *, ga_impl.ga_len);
591
Bram Moolenaar94674f22023-01-06 18:42:20 +0000592 for (int i = 0; i < ga_impl.ga_len && success; ++i)
593 {
594 char_u *impl = ((char_u **)ga_impl.ga_data)[i];
595 typval_T tv;
596 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar83677162023-01-08 19:54:10 +0000597 if (eval_variable(impl, 0, 0, &tv, NULL, EVAL_VAR_IMPORT) == FAIL)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000598 {
599 semsg(_(e_interface_name_not_found_str), impl);
600 success = FALSE;
601 break;
602 }
603
604 if (tv.v_type != VAR_CLASS
605 || tv.vval.v_class == NULL
606 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0)
607 {
608 semsg(_(e_not_valid_interface_str), impl);
609 success = FALSE;
610 }
611
Bram Moolenaar94674f22023-01-06 18:42:20 +0000612 class_T *ifcl = tv.vval.v_class;
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000613 intf_classes[i] = ifcl;
614 ++ifcl->class_refcount;
615
616 // check the members of the interface match the members of the class
Bram Moolenaar94674f22023-01-06 18:42:20 +0000617 for (int loop = 1; loop <= 2 && success; ++loop)
618 {
619 // loop == 1: check class members
620 // loop == 2: check object members
621 int if_count = loop == 1 ? ifcl->class_class_member_count
622 : ifcl->class_obj_member_count;
623 if (if_count == 0)
624 continue;
625 ocmember_T *if_ms = loop == 1 ? ifcl->class_class_members
626 : ifcl->class_obj_members;
627 ocmember_T *cl_ms = (ocmember_T *)(loop == 1
628 ? classmembers.ga_data
629 : objmembers.ga_data);
630 int cl_count = loop == 1 ? classmembers.ga_len
631 : objmembers.ga_len;
632 for (int if_i = 0; if_i < if_count; ++if_i)
633 {
634 int cl_i;
635 for (cl_i = 0; cl_i < cl_count; ++cl_i)
636 {
637 ocmember_T *m = &cl_ms[cl_i];
638 if (STRCMP(if_ms[if_i].ocm_name, m->ocm_name) == 0)
639 {
640 // TODO: check type
641 break;
642 }
643 }
644 if (cl_i == cl_count)
645 {
646 semsg(_(e_member_str_of_interface_str_not_implemented),
647 if_ms[if_i].ocm_name, impl);
648 success = FALSE;
649 break;
650 }
651 }
652 }
653
654 // check the functions/methods of the interface match the
655 // functions/methods of the class
656 for (int loop = 1; loop <= 2 && success; ++loop)
657 {
658 // loop == 1: check class functions
659 // loop == 2: check object methods
660 int if_count = loop == 1 ? ifcl->class_class_function_count
661 : ifcl->class_obj_method_count;
662 if (if_count == 0)
663 continue;
664 ufunc_T **if_fp = loop == 1 ? ifcl->class_class_functions
665 : ifcl->class_obj_methods;
666 ufunc_T **cl_fp = (ufunc_T **)(loop == 1
667 ? classfunctions.ga_data
668 : objmethods.ga_data);
669 int cl_count = loop == 1 ? classfunctions.ga_len
670 : objmethods.ga_len;
671 for (int if_i = 0; if_i < if_count; ++if_i)
672 {
673 char_u *if_name = if_fp[if_i]->uf_name;
674 int cl_i;
675 for (cl_i = 0; cl_i < cl_count; ++cl_i)
676 {
677 char_u *cl_name = cl_fp[cl_i]->uf_name;
678 if (STRCMP(if_name, cl_name) == 0)
679 {
680 // TODO: check return and argument types
681 break;
682 }
683 }
684 if (cl_i == cl_count)
685 {
686 semsg(_(e_function_str_of_interface_str_not_implemented),
687 if_name, impl);
688 success = FALSE;
689 break;
690 }
691 }
692 }
693
694 clear_tv(&tv);
695 }
696 }
697
Bram Moolenaareb533502022-12-14 15:06:11 +0000698 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000699 if (success)
700 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000701 // "endclass" encountered without failures: Create the class.
702
Bram Moolenaareb533502022-12-14 15:06:11 +0000703 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000704 if (cl == NULL)
705 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000706 if (!is_class)
707 cl->class_flags = CLASS_INTERFACE;
708
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000709 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +0000710 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +0000711 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000712 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000713
Bram Moolenaar83677162023-01-08 19:54:10 +0000714 cl->class_extends = extends_cl;
715
Bram Moolenaar94674f22023-01-06 18:42:20 +0000716 if (ga_impl.ga_len > 0)
717 {
718 // Move the "implements" names into the class.
719 cl->class_interface_count = ga_impl.ga_len;
720 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
721 if (cl->class_interfaces == NULL)
722 goto cleanup;
723 for (int i = 0; i < ga_impl.ga_len; ++i)
724 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
Bram Moolenaar7d4d87b2023-01-06 18:59:08 +0000725 VIM_CLEAR(ga_impl.ga_data);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000726 ga_impl.ga_len = 0;
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000727
728 cl->class_interfaces_cl = intf_classes;
729 intf_classes = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +0000730 }
731
Bram Moolenaard505d172022-12-18 21:42:55 +0000732 // Add class and object members to "cl".
733 if (add_members_to_class(&classmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000734 extends_cl == NULL ? NULL
735 : extends_cl->class_class_members,
736 extends_cl == NULL ? 0
737 : extends_cl->class_class_member_count,
738 &cl->class_class_members,
739 &cl->class_class_member_count) == FAIL
Bram Moolenaard505d172022-12-18 21:42:55 +0000740 || add_members_to_class(&objmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000741 extends_cl == NULL ? NULL
742 : extends_cl->class_obj_members,
743 extends_cl == NULL ? 0
744 : extends_cl->class_obj_member_count,
745 &cl->class_obj_members,
746 &cl->class_obj_member_count) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000747 goto cleanup;
748
Bram Moolenaar554d0312023-01-05 19:59:18 +0000749 if (is_class && cl->class_class_member_count > 0)
Bram Moolenaard505d172022-12-18 21:42:55 +0000750 {
751 // Allocate a typval for each class member and initialize it.
752 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
753 cl->class_class_member_count);
754 if (cl->class_members_tv != NULL)
755 for (int i = 0; i < cl->class_class_member_count; ++i)
756 {
757 ocmember_T *m = &cl->class_class_members[i];
758 typval_T *tv = &cl->class_members_tv[i];
759 if (m->ocm_init != NULL)
760 {
761 typval_T *etv = eval_expr(m->ocm_init, eap);
762 if (etv != NULL)
763 {
764 *tv = *etv;
765 vim_free(etv);
766 }
767 }
768 else
769 {
770 // TODO: proper default value
771 tv->v_type = m->ocm_type->tt_type;
772 tv->vval.v_string = NULL;
773 }
774 }
775 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000776
777 int have_new = FALSE;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000778 for (int i = 0; i < classfunctions.ga_len; ++i)
779 if (STRCMP(((ufunc_T **)classfunctions.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000780 "new") == 0)
781 {
782 have_new = TRUE;
783 break;
784 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000785 if (is_class && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000786 {
787 // No new() method was defined, add the default constructor.
788 garray_T fga;
789 ga_init2(&fga, 1, 1000);
790 ga_concat(&fga, (char_u *)"new(");
791 for (int i = 0; i < cl->class_obj_member_count; ++i)
792 {
793 if (i > 0)
794 ga_concat(&fga, (char_u *)", ");
795 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000796 ocmember_T *m = cl->class_obj_members + i;
797 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000798 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000799 }
800 ga_concat(&fga, (char_u *)")\nenddef\n");
801 ga_append(&fga, NUL);
802
803 exarg_T fea;
804 CLEAR_FIELD(fea);
805 fea.cmdidx = CMD_def;
806 fea.cmd = fea.arg = fga.ga_data;
807
808 garray_T lines_to_free;
809 ga_init2(&lines_to_free, sizeof(char_u *), 50);
810
Bram Moolenaar2c011312023-01-07 10:51:30 +0000811 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000812
813 ga_clear_strings(&lines_to_free);
814 vim_free(fga.ga_data);
815
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000816 if (nf != NULL && ga_grow(&classfunctions, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000817 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000818 ((ufunc_T **)classfunctions.ga_data)[classfunctions.ga_len]
819 = nf;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000820 ++classfunctions.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000821
822 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000823 nf->uf_ret_type = get_type_ptr(&type_list);
824 if (nf->uf_ret_type != NULL)
825 {
826 nf->uf_ret_type->tt_type = VAR_OBJECT;
827 nf->uf_ret_type->tt_member = (type_T *)cl;
828 nf->uf_ret_type->tt_argcount = 0;
829 nf->uf_ret_type->tt_args = NULL;
830 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000831 }
832 }
833
Bram Moolenaar58b40092023-01-11 15:59:05 +0000834 // Move all the functions into the created class.
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000835 // loop 1: class functions, loop 2: object methods
836 for (int loop = 1; loop <= 2; ++loop)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000837 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000838 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
839 int *fcount = loop == 1 ? &cl->class_class_function_count
840 : &cl->class_obj_method_count;
841 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
842 : &cl->class_obj_methods;
843
Bram Moolenaar83677162023-01-08 19:54:10 +0000844 int parent_count = 0;
845 if (extends_cl != NULL)
846 // Include functions from the parent.
847 parent_count = loop == 1
848 ? extends_cl->class_class_function_count
849 : extends_cl->class_obj_method_count;
850
851 *fcount = parent_count + gap->ga_len;
852 if (*fcount == 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000853 {
854 *fup = NULL;
855 continue;
856 }
Bram Moolenaar83677162023-01-08 19:54:10 +0000857 *fup = ALLOC_MULT(ufunc_T *, *fcount);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000858 if (*fup == NULL)
859 goto cleanup;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000860
Bram Moolenaar58b40092023-01-11 15:59:05 +0000861 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
862 vim_free(gap->ga_data);
863 if (loop == 1)
864 cl->class_class_function_count_child = gap->ga_len;
865 else
866 cl->class_obj_method_count_child = gap->ga_len;
867
Bram Moolenaar83677162023-01-08 19:54:10 +0000868 int skipped = 0;
869 for (int i = 0; i < parent_count; ++i)
870 {
871 // Copy functions from the parent. Can't use the same
872 // function, because "uf_class" is different and compilation
873 // will have a different result.
Bram Moolenaar58b40092023-01-11 15:59:05 +0000874 // Put them after the functions in the current class, object
875 // methods may be overruled, then "super.Method()" is used to
876 // find a method from the parent.
Bram Moolenaar83677162023-01-08 19:54:10 +0000877 // Skip "new" functions. TODO: not all of them.
878 if (loop == 1 && STRNCMP(
879 extends_cl->class_class_functions[i]->uf_name,
880 "new", 3) == 0)
881 ++skipped;
882 else
Bram Moolenaar58b40092023-01-11 15:59:05 +0000883 {
884 ufunc_T *pf = (loop == 1
Bram Moolenaar83677162023-01-08 19:54:10 +0000885 ? extends_cl->class_class_functions
Bram Moolenaar58b40092023-01-11 15:59:05 +0000886 : extends_cl->class_obj_methods)[i];
887 (*fup)[gap->ga_len + i - skipped] = copy_function(pf);
888
889 // If the child class overrides a function from the parent
890 // the signature must be equal.
891 char_u *pname = pf->uf_name;
892 for (int ci = 0; ci < gap->ga_len; ++ci)
893 {
894 ufunc_T *cf = (*fup)[ci];
895 char_u *cname = cf->uf_name;
896 if (STRCMP(pname, cname) == 0)
897 {
898 where_T where = WHERE_INIT;
899 where.wt_func_name = (char *)pname;
900 (void)check_type(pf->uf_func_type, cf->uf_func_type,
901 TRUE, where);
902 }
903 }
904 }
Bram Moolenaar83677162023-01-08 19:54:10 +0000905 }
906
Bram Moolenaar83677162023-01-08 19:54:10 +0000907 *fcount -= skipped;
908
909 // Set the class pointer on all the functions and object methods.
910 for (int i = 0; i < *fcount; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000911 {
912 ufunc_T *fp = (*fup)[i];
913 fp->uf_class = cl;
914 if (loop == 2)
915 fp->uf_flags |= FC_OBJECT;
916 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000917 }
918
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000919 cl->class_type.tt_type = VAR_CLASS;
920 cl->class_type.tt_member = (type_T *)cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000921 cl->class_object_type.tt_type = VAR_OBJECT;
922 cl->class_object_type.tt_member = (type_T *)cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000923 cl->class_type_list = type_list;
924
925 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +0000926 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000927
928 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +0000929 // TODO: handle other context, e.g. in a function
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000930 typval_T tv;
931 tv.v_type = VAR_CLASS;
932 tv.vval.v_class = cl;
933 set_var_const(cl->class_name, current_sctx.sc_sid,
934 NULL, &tv, FALSE, ASSIGN_DECL, 0);
935 return;
936 }
937
938cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +0000939 if (cl != NULL)
940 {
941 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000942 vim_free(cl->class_class_functions);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000943 if (cl->class_interfaces != NULL)
944 {
945 for (int i = 0; i < cl->class_interface_count; ++i)
946 vim_free(cl->class_interfaces[i]);
947 vim_free(cl->class_interfaces);
948 }
949 if (cl->class_interfaces_cl != NULL)
950 {
951 for (int i = 0; i < cl->class_interface_count; ++i)
952 class_unref(cl->class_interfaces_cl[i]);
953 vim_free(cl->class_interfaces_cl);
954 }
Bram Moolenaareb533502022-12-14 15:06:11 +0000955 vim_free(cl->class_obj_members);
956 vim_free(cl->class_obj_methods);
957 vim_free(cl);
958 }
959
Bram Moolenaar83677162023-01-08 19:54:10 +0000960 vim_free(extends);
961 class_unref(extends_cl);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +0000962
963 if (intf_classes != NULL)
964 {
965 for (int i = 0; i < ga_impl.ga_len; ++i)
966 class_unref(intf_classes[i]);
967 vim_free(intf_classes);
968 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000969 ga_clear_strings(&ga_impl);
970
Bram Moolenaard505d172022-12-18 21:42:55 +0000971 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000972 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000973 garray_T *gap = round == 1 ? &classmembers : &objmembers;
974 if (gap->ga_len == 0 || gap->ga_data == NULL)
975 continue;
976
977 for (int i = 0; i < gap->ga_len; ++i)
978 {
979 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
980 vim_free(m->ocm_name);
981 vim_free(m->ocm_init);
982 }
983 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000984 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000985
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000986 for (int i = 0; i < objmethods.ga_len; ++i)
987 {
988 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
989 func_clear_free(uf, FALSE);
990 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000991 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000992
993 for (int i = 0; i < classfunctions.ga_len; ++i)
994 {
995 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
996 func_clear_free(uf, FALSE);
997 }
998 ga_clear(&classfunctions);
999
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001000 clear_type_list(&type_list);
1001}
1002
1003/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001004 * Find member "name" in class "cl", set "member_idx" to the member index and
1005 * return its type.
1006 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001007 */
1008 type_T *
1009class_member_type(
1010 class_T *cl,
1011 char_u *name,
1012 char_u *name_end,
1013 int *member_idx)
1014{
1015 *member_idx = -1; // not found (yet)
1016 size_t len = name_end - name;
1017
1018 for (int i = 0; i < cl->class_obj_member_count; ++i)
1019 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001020 ocmember_T *m = cl->class_obj_members + i;
1021 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001022 {
1023 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001024 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001025 }
1026 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001027
1028 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001029 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001030}
1031
1032/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001033 * Handle ":enum" up to ":endenum".
1034 */
1035 void
1036ex_enum(exarg_T *eap UNUSED)
1037{
1038 // TODO
1039}
1040
1041/*
1042 * Handle ":type".
1043 */
1044 void
1045ex_type(exarg_T *eap UNUSED)
1046{
1047 // TODO
1048}
1049
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001050/*
1051 * Evaluate what comes after a class:
1052 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001053 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001054 * - class constructor: SomeClass.new()
1055 * - object member: someObject.varname
1056 * - object method: someObject.SomeMethod()
1057 *
1058 * "*arg" points to the '.'.
1059 * "*arg" is advanced to after the member name or method call.
1060 *
1061 * Returns FAIL or OK.
1062 */
1063 int
1064class_object_index(
1065 char_u **arg,
1066 typval_T *rettv,
1067 evalarg_T *evalarg,
1068 int verbose UNUSED) // give error messages
1069{
1070 // int evaluate = evalarg != NULL
1071 // && (evalarg->eval_flags & EVAL_EVALUATE);
1072
1073 if (VIM_ISWHITE((*arg)[1]))
1074 {
1075 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
1076 return FAIL;
1077 }
1078
1079 ++*arg;
1080 char_u *name = *arg;
1081 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1082 if (name_end == name)
1083 return FAIL;
1084 size_t len = name_end - name;
1085
1086 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
1087 : rettv->vval.v_object->obj_class;
1088 if (*name_end == '(')
1089 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001090 int on_class = rettv->v_type == VAR_CLASS;
1091 int count = on_class ? cl->class_class_function_count
1092 : cl->class_obj_method_count;
1093 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001094 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001095 ufunc_T *fp = on_class ? cl->class_class_functions[i]
1096 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +00001097 // Use a separate pointer to avoid that ASAN complains about
1098 // uf_name[] only being 4 characters.
1099 char_u *ufname = (char_u *)fp->uf_name;
1100 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001101 {
1102 typval_T argvars[MAX_FUNC_ARGS + 1];
1103 int argcount = 0;
1104
1105 char_u *argp = name_end;
1106 int ret = get_func_arguments(&argp, evalarg, 0,
1107 argvars, &argcount);
1108 if (ret == FAIL)
1109 return FAIL;
1110
1111 funcexe_T funcexe;
1112 CLEAR_FIELD(funcexe);
1113 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001114 if (rettv->v_type == VAR_OBJECT)
1115 {
1116 funcexe.fe_object = rettv->vval.v_object;
1117 ++funcexe.fe_object->obj_refcount;
1118 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001119
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001120 // Clear the class or object after calling the function, in
1121 // case the refcount is one.
1122 typval_T tv_tofree = *rettv;
1123 rettv->v_type = VAR_UNKNOWN;
1124
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001125 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001126 int error = call_user_func_check(fp, argcount, argvars,
1127 rettv, &funcexe, NULL);
1128
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001129 // Clear the previous rettv and the arguments.
1130 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001131 for (int idx = 0; idx < argcount; ++idx)
1132 clear_tv(&argvars[idx]);
1133
1134 if (error != FCERR_NONE)
1135 {
1136 user_func_error(error, printable_func_name(fp),
1137 funcexe.fe_found_var);
1138 return FAIL;
1139 }
1140 *arg = argp;
1141 return OK;
1142 }
1143 }
1144
1145 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
1146 }
1147
1148 else if (rettv->v_type == VAR_OBJECT)
1149 {
1150 for (int i = 0; i < cl->class_obj_member_count; ++i)
1151 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001152 ocmember_T *m = &cl->class_obj_members[i];
1153 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001154 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001155 if (*name == '_')
1156 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001157 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001158 return FAIL;
1159 }
1160
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001161 // The object only contains a pointer to the class, the member
1162 // values array follows right after that.
1163 object_T *obj = rettv->vval.v_object;
1164 typval_T *tv = (typval_T *)(obj + 1) + i;
1165 copy_tv(tv, rettv);
1166 object_unref(obj);
1167
1168 *arg = name_end;
1169 return OK;
1170 }
1171 }
1172
1173 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
1174 }
1175
Bram Moolenaard505d172022-12-18 21:42:55 +00001176 else if (rettv->v_type == VAR_CLASS)
1177 {
1178 // class member
1179 for (int i = 0; i < cl->class_class_member_count; ++i)
1180 {
1181 ocmember_T *m = &cl->class_class_members[i];
1182 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
1183 {
1184 if (*name == '_')
1185 {
1186 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
1187 return FAIL;
1188 }
1189
1190 typval_T *tv = &cl->class_members_tv[i];
1191 copy_tv(tv, rettv);
1192 class_unref(cl);
1193
1194 *arg = name_end;
1195 return OK;
1196 }
1197 }
1198
1199 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
1200 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001201
1202 return FAIL;
1203}
1204
1205/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001206 * If "arg" points to a class or object method, return it.
1207 * Otherwise return NULL.
1208 */
1209 ufunc_T *
1210find_class_func(char_u **arg)
1211{
1212 char_u *name = *arg;
1213 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1214 if (name_end == name || *name_end != '.')
1215 return NULL;
1216
1217 size_t len = name_end - name;
1218 typval_T tv;
1219 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001220 if (eval_variable(name, (int)len,
1221 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001222 return NULL;
1223 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001224 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001225
1226 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1227 : tv.vval.v_object->obj_class;
1228 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001229 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001230 char_u *fname = name_end + 1;
1231 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1232 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001233 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001234 len = fname_end - fname;
1235
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001236 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1237 : cl->class_obj_method_count;
1238 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1239 : cl->class_obj_methods;
1240 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001241 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001242 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001243 // Use a separate pointer to avoid that ASAN complains about
1244 // uf_name[] only being 4 characters.
1245 char_u *ufname = (char_u *)fp->uf_name;
1246 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001247 {
1248 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001249 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001250 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001251 }
1252
Bram Moolenaareb533502022-12-14 15:06:11 +00001253fail_after_eval:
1254 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001255 return NULL;
1256}
1257
1258/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001259 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1260 * index in class.class_class_members[].
1261 * If "cl_ret" is not NULL set it to the class.
1262 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001263 */
1264 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001265class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001266{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001267 if (cctx == NULL || cctx->ctx_ufunc == NULL
1268 || cctx->ctx_ufunc->uf_class == NULL)
1269 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001270 class_T *cl = cctx->ctx_ufunc->uf_class;
1271
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001272 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001273 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001274 ocmember_T *m = &cl->class_class_members[i];
1275 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001276 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001277 if (cl_ret != NULL)
1278 *cl_ret = cl;
1279 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001280 }
1281 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001282 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001283}
1284
1285/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001286 * Make a copy of an object.
1287 */
1288 void
1289copy_object(typval_T *from, typval_T *to)
1290{
1291 *to = *from;
1292 if (to->vval.v_object != NULL)
1293 ++to->vval.v_object->obj_refcount;
1294}
1295
1296/*
1297 * Free an object.
1298 */
1299 static void
1300object_clear(object_T *obj)
1301{
1302 class_T *cl = obj->obj_class;
1303
1304 // the member values are just after the object structure
1305 typval_T *tv = (typval_T *)(obj + 1);
1306 for (int i = 0; i < cl->class_obj_member_count; ++i)
1307 clear_tv(tv + i);
1308
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001309 // Remove from the list headed by "first_object".
1310 object_cleared(obj);
1311
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001312 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001313 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001314}
1315
1316/*
1317 * Unreference an object.
1318 */
1319 void
1320object_unref(object_T *obj)
1321{
1322 if (obj != NULL && --obj->obj_refcount <= 0)
1323 object_clear(obj);
1324}
1325
1326/*
1327 * Make a copy of a class.
1328 */
1329 void
1330copy_class(typval_T *from, typval_T *to)
1331{
1332 *to = *from;
1333 if (to->vval.v_class != NULL)
1334 ++to->vval.v_class->class_refcount;
1335}
1336
1337/*
1338 * Unreference a class. Free it when the reference count goes down to zero.
1339 */
1340 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001341class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001342{
Bram Moolenaard505d172022-12-18 21:42:55 +00001343 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001344 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001345 // Freeing what the class contains may recursively come back here.
1346 // Clear "class_name" first, if it is NULL the class does not need to
1347 // be freed.
1348 VIM_CLEAR(cl->class_name);
1349
Bram Moolenaar83677162023-01-08 19:54:10 +00001350 class_unref(cl->class_extends);
1351
Bram Moolenaar94674f22023-01-06 18:42:20 +00001352 for (int i = 0; i < cl->class_interface_count; ++i)
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001353 {
Bram Moolenaar94674f22023-01-06 18:42:20 +00001354 vim_free(((char_u **)cl->class_interfaces)[i]);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001355 if (cl->class_interfaces_cl[i] != NULL)
1356 class_unref(cl->class_interfaces_cl[i]);
1357 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001358 vim_free(cl->class_interfaces);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001359 vim_free(cl->class_interfaces_cl);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001360
Bram Moolenaard505d172022-12-18 21:42:55 +00001361 for (int i = 0; i < cl->class_class_member_count; ++i)
1362 {
1363 ocmember_T *m = &cl->class_class_members[i];
1364 vim_free(m->ocm_name);
1365 vim_free(m->ocm_init);
1366 if (cl->class_members_tv != NULL)
1367 clear_tv(&cl->class_members_tv[i]);
1368 }
1369 vim_free(cl->class_class_members);
1370 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001371
1372 for (int i = 0; i < cl->class_obj_member_count; ++i)
1373 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001374 ocmember_T *m = &cl->class_obj_members[i];
1375 vim_free(m->ocm_name);
1376 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001377 }
1378 vim_free(cl->class_obj_members);
1379
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001380 for (int i = 0; i < cl->class_class_function_count; ++i)
1381 {
1382 ufunc_T *uf = cl->class_class_functions[i];
1383 func_clear_free(uf, FALSE);
1384 }
1385 vim_free(cl->class_class_functions);
1386
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001387 for (int i = 0; i < cl->class_obj_method_count; ++i)
1388 {
1389 ufunc_T *uf = cl->class_obj_methods[i];
1390 func_clear_free(uf, FALSE);
1391 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001392 vim_free(cl->class_obj_methods);
1393
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001394 clear_type_list(&cl->class_type_list);
1395
1396 vim_free(cl);
1397 }
1398}
1399
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001400static object_T *first_object = NULL;
1401
1402/*
1403 * Call this function when an object has been created. It will be added to the
1404 * list headed by "first_object".
1405 */
1406 void
1407object_created(object_T *obj)
1408{
1409 if (first_object != NULL)
1410 {
1411 obj->obj_next_used = first_object;
1412 first_object->obj_prev_used = obj;
1413 }
1414 first_object = obj;
1415}
1416
1417/*
1418 * Call this function when an object has been cleared and is about to be freed.
1419 * It is removed from the list headed by "first_object".
1420 */
1421 void
1422object_cleared(object_T *obj)
1423{
1424 if (obj->obj_next_used != NULL)
1425 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1426 if (obj->obj_prev_used != NULL)
1427 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1428 else if (first_object == obj)
1429 first_object = obj->obj_next_used;
1430}
1431
1432/*
1433 * Go through the list of all objects and free items without "copyID".
1434 */
1435 int
1436object_free_nonref(int copyID)
1437{
1438 int did_free = FALSE;
1439 object_T *next_obj;
1440
1441 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
1442 {
1443 next_obj = obj->obj_next_used;
1444 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1445 {
1446 // Free the object and items it contains.
1447 object_clear(obj);
1448 did_free = TRUE;
1449 }
1450 }
1451
1452 return did_free;
1453}
1454
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001455
1456#endif // FEAT_EVAL