blob: 0970619b91f5d6362e3be6c283d03e9a6d99c8c1 [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
585 // Check all "implements" entries are valid.
Bram Moolenaar94674f22023-01-06 18:42:20 +0000586 if (success && ga_impl.ga_len > 0)
587 {
Bram Moolenaar94674f22023-01-06 18:42:20 +0000588 for (int i = 0; i < ga_impl.ga_len && success; ++i)
589 {
590 char_u *impl = ((char_u **)ga_impl.ga_data)[i];
591 typval_T tv;
592 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar83677162023-01-08 19:54:10 +0000593 if (eval_variable(impl, 0, 0, &tv, NULL, EVAL_VAR_IMPORT) == FAIL)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000594 {
595 semsg(_(e_interface_name_not_found_str), impl);
596 success = FALSE;
597 break;
598 }
599
600 if (tv.v_type != VAR_CLASS
601 || tv.vval.v_class == NULL
602 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0)
603 {
604 semsg(_(e_not_valid_interface_str), impl);
605 success = FALSE;
606 }
607
608 // check the members of the interface match the members of the class
609 class_T *ifcl = tv.vval.v_class;
610 for (int loop = 1; loop <= 2 && success; ++loop)
611 {
612 // loop == 1: check class members
613 // loop == 2: check object members
614 int if_count = loop == 1 ? ifcl->class_class_member_count
615 : ifcl->class_obj_member_count;
616 if (if_count == 0)
617 continue;
618 ocmember_T *if_ms = loop == 1 ? ifcl->class_class_members
619 : ifcl->class_obj_members;
620 ocmember_T *cl_ms = (ocmember_T *)(loop == 1
621 ? classmembers.ga_data
622 : objmembers.ga_data);
623 int cl_count = loop == 1 ? classmembers.ga_len
624 : objmembers.ga_len;
625 for (int if_i = 0; if_i < if_count; ++if_i)
626 {
627 int cl_i;
628 for (cl_i = 0; cl_i < cl_count; ++cl_i)
629 {
630 ocmember_T *m = &cl_ms[cl_i];
631 if (STRCMP(if_ms[if_i].ocm_name, m->ocm_name) == 0)
632 {
633 // TODO: check type
634 break;
635 }
636 }
637 if (cl_i == cl_count)
638 {
639 semsg(_(e_member_str_of_interface_str_not_implemented),
640 if_ms[if_i].ocm_name, impl);
641 success = FALSE;
642 break;
643 }
644 }
645 }
646
647 // check the functions/methods of the interface match the
648 // functions/methods of the class
649 for (int loop = 1; loop <= 2 && success; ++loop)
650 {
651 // loop == 1: check class functions
652 // loop == 2: check object methods
653 int if_count = loop == 1 ? ifcl->class_class_function_count
654 : ifcl->class_obj_method_count;
655 if (if_count == 0)
656 continue;
657 ufunc_T **if_fp = loop == 1 ? ifcl->class_class_functions
658 : ifcl->class_obj_methods;
659 ufunc_T **cl_fp = (ufunc_T **)(loop == 1
660 ? classfunctions.ga_data
661 : objmethods.ga_data);
662 int cl_count = loop == 1 ? classfunctions.ga_len
663 : objmethods.ga_len;
664 for (int if_i = 0; if_i < if_count; ++if_i)
665 {
666 char_u *if_name = if_fp[if_i]->uf_name;
667 int cl_i;
668 for (cl_i = 0; cl_i < cl_count; ++cl_i)
669 {
670 char_u *cl_name = cl_fp[cl_i]->uf_name;
671 if (STRCMP(if_name, cl_name) == 0)
672 {
673 // TODO: check return and argument types
674 break;
675 }
676 }
677 if (cl_i == cl_count)
678 {
679 semsg(_(e_function_str_of_interface_str_not_implemented),
680 if_name, impl);
681 success = FALSE;
682 break;
683 }
684 }
685 }
686
687 clear_tv(&tv);
688 }
689 }
690
Bram Moolenaareb533502022-12-14 15:06:11 +0000691 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000692 if (success)
693 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000694 // "endclass" encountered without failures: Create the class.
695
Bram Moolenaareb533502022-12-14 15:06:11 +0000696 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000697 if (cl == NULL)
698 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000699 if (!is_class)
700 cl->class_flags = CLASS_INTERFACE;
701
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000702 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +0000703 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +0000704 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000705 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000706
Bram Moolenaar83677162023-01-08 19:54:10 +0000707 cl->class_extends = extends_cl;
708
Bram Moolenaar94674f22023-01-06 18:42:20 +0000709 if (ga_impl.ga_len > 0)
710 {
711 // Move the "implements" names into the class.
712 cl->class_interface_count = ga_impl.ga_len;
713 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
714 if (cl->class_interfaces == NULL)
715 goto cleanup;
716 for (int i = 0; i < ga_impl.ga_len; ++i)
717 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
Bram Moolenaar7d4d87b2023-01-06 18:59:08 +0000718 VIM_CLEAR(ga_impl.ga_data);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000719 ga_impl.ga_len = 0;
720 }
721
Bram Moolenaard505d172022-12-18 21:42:55 +0000722 // Add class and object members to "cl".
723 if (add_members_to_class(&classmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000724 extends_cl == NULL ? NULL
725 : extends_cl->class_class_members,
726 extends_cl == NULL ? 0
727 : extends_cl->class_class_member_count,
728 &cl->class_class_members,
729 &cl->class_class_member_count) == FAIL
Bram Moolenaard505d172022-12-18 21:42:55 +0000730 || add_members_to_class(&objmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +0000731 extends_cl == NULL ? NULL
732 : extends_cl->class_obj_members,
733 extends_cl == NULL ? 0
734 : extends_cl->class_obj_member_count,
735 &cl->class_obj_members,
736 &cl->class_obj_member_count) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000737 goto cleanup;
738
Bram Moolenaar554d0312023-01-05 19:59:18 +0000739 if (is_class && cl->class_class_member_count > 0)
Bram Moolenaard505d172022-12-18 21:42:55 +0000740 {
741 // Allocate a typval for each class member and initialize it.
742 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
743 cl->class_class_member_count);
744 if (cl->class_members_tv != NULL)
745 for (int i = 0; i < cl->class_class_member_count; ++i)
746 {
747 ocmember_T *m = &cl->class_class_members[i];
748 typval_T *tv = &cl->class_members_tv[i];
749 if (m->ocm_init != NULL)
750 {
751 typval_T *etv = eval_expr(m->ocm_init, eap);
752 if (etv != NULL)
753 {
754 *tv = *etv;
755 vim_free(etv);
756 }
757 }
758 else
759 {
760 // TODO: proper default value
761 tv->v_type = m->ocm_type->tt_type;
762 tv->vval.v_string = NULL;
763 }
764 }
765 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000766
767 int have_new = FALSE;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000768 for (int i = 0; i < classfunctions.ga_len; ++i)
769 if (STRCMP(((ufunc_T **)classfunctions.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000770 "new") == 0)
771 {
772 have_new = TRUE;
773 break;
774 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000775 if (is_class && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000776 {
777 // No new() method was defined, add the default constructor.
778 garray_T fga;
779 ga_init2(&fga, 1, 1000);
780 ga_concat(&fga, (char_u *)"new(");
781 for (int i = 0; i < cl->class_obj_member_count; ++i)
782 {
783 if (i > 0)
784 ga_concat(&fga, (char_u *)", ");
785 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000786 ocmember_T *m = cl->class_obj_members + i;
787 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000788 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000789 }
790 ga_concat(&fga, (char_u *)")\nenddef\n");
791 ga_append(&fga, NUL);
792
793 exarg_T fea;
794 CLEAR_FIELD(fea);
795 fea.cmdidx = CMD_def;
796 fea.cmd = fea.arg = fga.ga_data;
797
798 garray_T lines_to_free;
799 ga_init2(&lines_to_free, sizeof(char_u *), 50);
800
Bram Moolenaar2c011312023-01-07 10:51:30 +0000801 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000802
803 ga_clear_strings(&lines_to_free);
804 vim_free(fga.ga_data);
805
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000806 if (nf != NULL && ga_grow(&classfunctions, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000807 {
Bram Moolenaar58b40092023-01-11 15:59:05 +0000808 ((ufunc_T **)classfunctions.ga_data)[classfunctions.ga_len]
809 = nf;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000810 ++classfunctions.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000811
812 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000813 nf->uf_ret_type = get_type_ptr(&type_list);
814 if (nf->uf_ret_type != NULL)
815 {
816 nf->uf_ret_type->tt_type = VAR_OBJECT;
817 nf->uf_ret_type->tt_member = (type_T *)cl;
818 nf->uf_ret_type->tt_argcount = 0;
819 nf->uf_ret_type->tt_args = NULL;
820 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000821 }
822 }
823
Bram Moolenaar58b40092023-01-11 15:59:05 +0000824 // Move all the functions into the created class.
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000825 // loop 1: class functions, loop 2: object methods
826 for (int loop = 1; loop <= 2; ++loop)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000827 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000828 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
829 int *fcount = loop == 1 ? &cl->class_class_function_count
830 : &cl->class_obj_method_count;
831 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
832 : &cl->class_obj_methods;
833
Bram Moolenaar83677162023-01-08 19:54:10 +0000834 int parent_count = 0;
835 if (extends_cl != NULL)
836 // Include functions from the parent.
837 parent_count = loop == 1
838 ? extends_cl->class_class_function_count
839 : extends_cl->class_obj_method_count;
840
841 *fcount = parent_count + gap->ga_len;
842 if (*fcount == 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000843 {
844 *fup = NULL;
845 continue;
846 }
Bram Moolenaar83677162023-01-08 19:54:10 +0000847 *fup = ALLOC_MULT(ufunc_T *, *fcount);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000848 if (*fup == NULL)
849 goto cleanup;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000850
Bram Moolenaar58b40092023-01-11 15:59:05 +0000851 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
852 vim_free(gap->ga_data);
853 if (loop == 1)
854 cl->class_class_function_count_child = gap->ga_len;
855 else
856 cl->class_obj_method_count_child = gap->ga_len;
857
Bram Moolenaar83677162023-01-08 19:54:10 +0000858 int skipped = 0;
859 for (int i = 0; i < parent_count; ++i)
860 {
861 // Copy functions from the parent. Can't use the same
862 // function, because "uf_class" is different and compilation
863 // will have a different result.
Bram Moolenaar58b40092023-01-11 15:59:05 +0000864 // Put them after the functions in the current class, object
865 // methods may be overruled, then "super.Method()" is used to
866 // find a method from the parent.
Bram Moolenaar83677162023-01-08 19:54:10 +0000867 // Skip "new" functions. TODO: not all of them.
868 if (loop == 1 && STRNCMP(
869 extends_cl->class_class_functions[i]->uf_name,
870 "new", 3) == 0)
871 ++skipped;
872 else
Bram Moolenaar58b40092023-01-11 15:59:05 +0000873 {
874 ufunc_T *pf = (loop == 1
Bram Moolenaar83677162023-01-08 19:54:10 +0000875 ? extends_cl->class_class_functions
Bram Moolenaar58b40092023-01-11 15:59:05 +0000876 : extends_cl->class_obj_methods)[i];
877 (*fup)[gap->ga_len + i - skipped] = copy_function(pf);
878
879 // If the child class overrides a function from the parent
880 // the signature must be equal.
881 char_u *pname = pf->uf_name;
882 for (int ci = 0; ci < gap->ga_len; ++ci)
883 {
884 ufunc_T *cf = (*fup)[ci];
885 char_u *cname = cf->uf_name;
886 if (STRCMP(pname, cname) == 0)
887 {
888 where_T where = WHERE_INIT;
889 where.wt_func_name = (char *)pname;
890 (void)check_type(pf->uf_func_type, cf->uf_func_type,
891 TRUE, where);
892 }
893 }
894 }
Bram Moolenaar83677162023-01-08 19:54:10 +0000895 }
896
Bram Moolenaar83677162023-01-08 19:54:10 +0000897 *fcount -= skipped;
898
899 // Set the class pointer on all the functions and object methods.
900 for (int i = 0; i < *fcount; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000901 {
902 ufunc_T *fp = (*fup)[i];
903 fp->uf_class = cl;
904 if (loop == 2)
905 fp->uf_flags |= FC_OBJECT;
906 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000907 }
908
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000909 cl->class_type.tt_type = VAR_CLASS;
910 cl->class_type.tt_member = (type_T *)cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000911 cl->class_object_type.tt_type = VAR_OBJECT;
912 cl->class_object_type.tt_member = (type_T *)cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000913 cl->class_type_list = type_list;
914
915 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +0000916 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000917
918 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +0000919 // TODO: handle other context, e.g. in a function
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000920 typval_T tv;
921 tv.v_type = VAR_CLASS;
922 tv.vval.v_class = cl;
923 set_var_const(cl->class_name, current_sctx.sc_sid,
924 NULL, &tv, FALSE, ASSIGN_DECL, 0);
925 return;
926 }
927
928cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +0000929 if (cl != NULL)
930 {
931 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000932 vim_free(cl->class_class_functions);
Bram Moolenaareb533502022-12-14 15:06:11 +0000933 vim_free(cl->class_obj_members);
934 vim_free(cl->class_obj_methods);
935 vim_free(cl);
936 }
937
Bram Moolenaar83677162023-01-08 19:54:10 +0000938 vim_free(extends);
939 class_unref(extends_cl);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000940 ga_clear_strings(&ga_impl);
941
Bram Moolenaard505d172022-12-18 21:42:55 +0000942 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000943 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000944 garray_T *gap = round == 1 ? &classmembers : &objmembers;
945 if (gap->ga_len == 0 || gap->ga_data == NULL)
946 continue;
947
948 for (int i = 0; i < gap->ga_len; ++i)
949 {
950 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
951 vim_free(m->ocm_name);
952 vim_free(m->ocm_init);
953 }
954 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000955 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000956
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000957 for (int i = 0; i < objmethods.ga_len; ++i)
958 {
959 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
960 func_clear_free(uf, FALSE);
961 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000962 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000963
964 for (int i = 0; i < classfunctions.ga_len; ++i)
965 {
966 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
967 func_clear_free(uf, FALSE);
968 }
969 ga_clear(&classfunctions);
970
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000971 clear_type_list(&type_list);
972}
973
974/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000975 * Find member "name" in class "cl", set "member_idx" to the member index and
976 * return its type.
977 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000978 */
979 type_T *
980class_member_type(
981 class_T *cl,
982 char_u *name,
983 char_u *name_end,
984 int *member_idx)
985{
986 *member_idx = -1; // not found (yet)
987 size_t len = name_end - name;
988
989 for (int i = 0; i < cl->class_obj_member_count; ++i)
990 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000991 ocmember_T *m = cl->class_obj_members + i;
992 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000993 {
994 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +0000995 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000996 }
997 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000998
999 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001000 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001001}
1002
1003/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001004 * Handle ":enum" up to ":endenum".
1005 */
1006 void
1007ex_enum(exarg_T *eap UNUSED)
1008{
1009 // TODO
1010}
1011
1012/*
1013 * Handle ":type".
1014 */
1015 void
1016ex_type(exarg_T *eap UNUSED)
1017{
1018 // TODO
1019}
1020
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001021/*
1022 * Evaluate what comes after a class:
1023 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001024 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001025 * - class constructor: SomeClass.new()
1026 * - object member: someObject.varname
1027 * - object method: someObject.SomeMethod()
1028 *
1029 * "*arg" points to the '.'.
1030 * "*arg" is advanced to after the member name or method call.
1031 *
1032 * Returns FAIL or OK.
1033 */
1034 int
1035class_object_index(
1036 char_u **arg,
1037 typval_T *rettv,
1038 evalarg_T *evalarg,
1039 int verbose UNUSED) // give error messages
1040{
1041 // int evaluate = evalarg != NULL
1042 // && (evalarg->eval_flags & EVAL_EVALUATE);
1043
1044 if (VIM_ISWHITE((*arg)[1]))
1045 {
1046 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
1047 return FAIL;
1048 }
1049
1050 ++*arg;
1051 char_u *name = *arg;
1052 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1053 if (name_end == name)
1054 return FAIL;
1055 size_t len = name_end - name;
1056
1057 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
1058 : rettv->vval.v_object->obj_class;
1059 if (*name_end == '(')
1060 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001061 int on_class = rettv->v_type == VAR_CLASS;
1062 int count = on_class ? cl->class_class_function_count
1063 : cl->class_obj_method_count;
1064 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001065 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001066 ufunc_T *fp = on_class ? cl->class_class_functions[i]
1067 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +00001068 // Use a separate pointer to avoid that ASAN complains about
1069 // uf_name[] only being 4 characters.
1070 char_u *ufname = (char_u *)fp->uf_name;
1071 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001072 {
1073 typval_T argvars[MAX_FUNC_ARGS + 1];
1074 int argcount = 0;
1075
1076 char_u *argp = name_end;
1077 int ret = get_func_arguments(&argp, evalarg, 0,
1078 argvars, &argcount);
1079 if (ret == FAIL)
1080 return FAIL;
1081
1082 funcexe_T funcexe;
1083 CLEAR_FIELD(funcexe);
1084 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001085 if (rettv->v_type == VAR_OBJECT)
1086 {
1087 funcexe.fe_object = rettv->vval.v_object;
1088 ++funcexe.fe_object->obj_refcount;
1089 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001090
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001091 // Clear the class or object after calling the function, in
1092 // case the refcount is one.
1093 typval_T tv_tofree = *rettv;
1094 rettv->v_type = VAR_UNKNOWN;
1095
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001096 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001097 int error = call_user_func_check(fp, argcount, argvars,
1098 rettv, &funcexe, NULL);
1099
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001100 // Clear the previous rettv and the arguments.
1101 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001102 for (int idx = 0; idx < argcount; ++idx)
1103 clear_tv(&argvars[idx]);
1104
1105 if (error != FCERR_NONE)
1106 {
1107 user_func_error(error, printable_func_name(fp),
1108 funcexe.fe_found_var);
1109 return FAIL;
1110 }
1111 *arg = argp;
1112 return OK;
1113 }
1114 }
1115
1116 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
1117 }
1118
1119 else if (rettv->v_type == VAR_OBJECT)
1120 {
1121 for (int i = 0; i < cl->class_obj_member_count; ++i)
1122 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001123 ocmember_T *m = &cl->class_obj_members[i];
1124 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001125 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001126 if (*name == '_')
1127 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001128 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001129 return FAIL;
1130 }
1131
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001132 // The object only contains a pointer to the class, the member
1133 // values array follows right after that.
1134 object_T *obj = rettv->vval.v_object;
1135 typval_T *tv = (typval_T *)(obj + 1) + i;
1136 copy_tv(tv, rettv);
1137 object_unref(obj);
1138
1139 *arg = name_end;
1140 return OK;
1141 }
1142 }
1143
1144 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
1145 }
1146
Bram Moolenaard505d172022-12-18 21:42:55 +00001147 else if (rettv->v_type == VAR_CLASS)
1148 {
1149 // class member
1150 for (int i = 0; i < cl->class_class_member_count; ++i)
1151 {
1152 ocmember_T *m = &cl->class_class_members[i];
1153 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
1154 {
1155 if (*name == '_')
1156 {
1157 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
1158 return FAIL;
1159 }
1160
1161 typval_T *tv = &cl->class_members_tv[i];
1162 copy_tv(tv, rettv);
1163 class_unref(cl);
1164
1165 *arg = name_end;
1166 return OK;
1167 }
1168 }
1169
1170 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
1171 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001172
1173 return FAIL;
1174}
1175
1176/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001177 * If "arg" points to a class or object method, return it.
1178 * Otherwise return NULL.
1179 */
1180 ufunc_T *
1181find_class_func(char_u **arg)
1182{
1183 char_u *name = *arg;
1184 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1185 if (name_end == name || *name_end != '.')
1186 return NULL;
1187
1188 size_t len = name_end - name;
1189 typval_T tv;
1190 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001191 if (eval_variable(name, (int)len,
1192 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001193 return NULL;
1194 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001195 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001196
1197 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1198 : tv.vval.v_object->obj_class;
1199 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001200 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001201 char_u *fname = name_end + 1;
1202 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1203 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001204 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001205 len = fname_end - fname;
1206
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001207 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1208 : cl->class_obj_method_count;
1209 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1210 : cl->class_obj_methods;
1211 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001212 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001213 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001214 // Use a separate pointer to avoid that ASAN complains about
1215 // uf_name[] only being 4 characters.
1216 char_u *ufname = (char_u *)fp->uf_name;
1217 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001218 {
1219 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001220 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001221 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001222 }
1223
Bram Moolenaareb533502022-12-14 15:06:11 +00001224fail_after_eval:
1225 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001226 return NULL;
1227}
1228
1229/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001230 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1231 * index in class.class_class_members[].
1232 * If "cl_ret" is not NULL set it to the class.
1233 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001234 */
1235 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001236class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001237{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001238 if (cctx == NULL || cctx->ctx_ufunc == NULL
1239 || cctx->ctx_ufunc->uf_class == NULL)
1240 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001241 class_T *cl = cctx->ctx_ufunc->uf_class;
1242
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001243 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001244 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001245 ocmember_T *m = &cl->class_class_members[i];
1246 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001247 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001248 if (cl_ret != NULL)
1249 *cl_ret = cl;
1250 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001251 }
1252 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001253 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001254}
1255
1256/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001257 * Make a copy of an object.
1258 */
1259 void
1260copy_object(typval_T *from, typval_T *to)
1261{
1262 *to = *from;
1263 if (to->vval.v_object != NULL)
1264 ++to->vval.v_object->obj_refcount;
1265}
1266
1267/*
1268 * Free an object.
1269 */
1270 static void
1271object_clear(object_T *obj)
1272{
1273 class_T *cl = obj->obj_class;
1274
1275 // the member values are just after the object structure
1276 typval_T *tv = (typval_T *)(obj + 1);
1277 for (int i = 0; i < cl->class_obj_member_count; ++i)
1278 clear_tv(tv + i);
1279
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001280 // Remove from the list headed by "first_object".
1281 object_cleared(obj);
1282
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001283 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001284 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001285}
1286
1287/*
1288 * Unreference an object.
1289 */
1290 void
1291object_unref(object_T *obj)
1292{
1293 if (obj != NULL && --obj->obj_refcount <= 0)
1294 object_clear(obj);
1295}
1296
1297/*
1298 * Make a copy of a class.
1299 */
1300 void
1301copy_class(typval_T *from, typval_T *to)
1302{
1303 *to = *from;
1304 if (to->vval.v_class != NULL)
1305 ++to->vval.v_class->class_refcount;
1306}
1307
1308/*
1309 * Unreference a class. Free it when the reference count goes down to zero.
1310 */
1311 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001312class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001313{
Bram Moolenaard505d172022-12-18 21:42:55 +00001314 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001315 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001316 // Freeing what the class contains may recursively come back here.
1317 // Clear "class_name" first, if it is NULL the class does not need to
1318 // be freed.
1319 VIM_CLEAR(cl->class_name);
1320
Bram Moolenaar83677162023-01-08 19:54:10 +00001321 class_unref(cl->class_extends);
1322
Bram Moolenaar94674f22023-01-06 18:42:20 +00001323 for (int i = 0; i < cl->class_interface_count; ++i)
1324 vim_free(((char_u **)cl->class_interfaces)[i]);
1325 vim_free(cl->class_interfaces);
1326
Bram Moolenaard505d172022-12-18 21:42:55 +00001327 for (int i = 0; i < cl->class_class_member_count; ++i)
1328 {
1329 ocmember_T *m = &cl->class_class_members[i];
1330 vim_free(m->ocm_name);
1331 vim_free(m->ocm_init);
1332 if (cl->class_members_tv != NULL)
1333 clear_tv(&cl->class_members_tv[i]);
1334 }
1335 vim_free(cl->class_class_members);
1336 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001337
1338 for (int i = 0; i < cl->class_obj_member_count; ++i)
1339 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001340 ocmember_T *m = &cl->class_obj_members[i];
1341 vim_free(m->ocm_name);
1342 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001343 }
1344 vim_free(cl->class_obj_members);
1345
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001346 for (int i = 0; i < cl->class_class_function_count; ++i)
1347 {
1348 ufunc_T *uf = cl->class_class_functions[i];
1349 func_clear_free(uf, FALSE);
1350 }
1351 vim_free(cl->class_class_functions);
1352
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001353 for (int i = 0; i < cl->class_obj_method_count; ++i)
1354 {
1355 ufunc_T *uf = cl->class_obj_methods[i];
1356 func_clear_free(uf, FALSE);
1357 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001358 vim_free(cl->class_obj_methods);
1359
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001360 clear_type_list(&cl->class_type_list);
1361
1362 vim_free(cl);
1363 }
1364}
1365
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001366static object_T *first_object = NULL;
1367
1368/*
1369 * Call this function when an object has been created. It will be added to the
1370 * list headed by "first_object".
1371 */
1372 void
1373object_created(object_T *obj)
1374{
1375 if (first_object != NULL)
1376 {
1377 obj->obj_next_used = first_object;
1378 first_object->obj_prev_used = obj;
1379 }
1380 first_object = obj;
1381}
1382
1383/*
1384 * Call this function when an object has been cleared and is about to be freed.
1385 * It is removed from the list headed by "first_object".
1386 */
1387 void
1388object_cleared(object_T *obj)
1389{
1390 if (obj->obj_next_used != NULL)
1391 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1392 if (obj->obj_prev_used != NULL)
1393 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1394 else if (first_object == obj)
1395 first_object = obj->obj_next_used;
1396}
1397
1398/*
1399 * Go through the list of all objects and free items without "copyID".
1400 */
1401 int
1402object_free_nonref(int copyID)
1403{
1404 int did_free = FALSE;
1405 object_T *next_obj;
1406
1407 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
1408 {
1409 next_obj = obj->obj_next_used;
1410 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1411 {
1412 // Free the object and items it contains.
1413 object_clear(obj);
1414 did_free = TRUE;
1415 }
1416 }
1417
1418 return did_free;
1419}
1420
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001421
1422#endif // FEAT_EVAL