blob: b1de90b8b7a3e3c81c9270c9db4e9382a716b3ac [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
29 * one.
30 */
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)
102 type = typval2type(&tv, get_copyID(), type_list,
103 TVTT_DO_MEMBER);
104 if (type == NULL)
105 {
106 semsg(_(e_cannot_get_object_member_type_from_initializer_str),
107 expr_start);
108 clear_evalarg(&evalarg, NULL);
109 return FAIL;
110 }
111 }
112 clear_evalarg(&evalarg, NULL);
113 }
114 if (!valid_declaration_type(type))
115 return FAIL;
116
117 *type_ret = type;
118 if (expr_end > expr_start)
119 *init_expr = vim_strnsave(expr_start, expr_end - expr_start);
120 return OK;
121}
122
123/*
124 * Add a member to an object or a class.
125 * Returns OK when successful, "init_expr" will be consumed then.
126 * Returns FAIL otherwise, caller might need to free "init_expr".
127 */
128 static int
129add_member(
130 garray_T *gap,
131 char_u *varname,
132 char_u *varname_end,
133 int has_public,
134 type_T *type,
135 char_u *init_expr)
136{
137 if (ga_grow(gap, 1) == FAIL)
138 return FAIL;
139 ocmember_T *m = ((ocmember_T *)gap->ga_data) + gap->ga_len;
140 m->ocm_name = vim_strnsave(varname, varname_end - varname);
141 m->ocm_access = has_public ? ACCESS_ALL
142 : *varname == '_' ? ACCESS_PRIVATE : ACCESS_READ;
143 m->ocm_type = type;
144 if (init_expr != NULL)
145 m->ocm_init = init_expr;
146 ++gap->ga_len;
147 return OK;
148}
149
150/*
151 * Move the class or object members found while parsing a class into the class.
152 * "gap" contains the found members.
153 * "members" will be set to the newly allocated array of members and
154 * "member_count" set to the number of members.
155 * Returns OK or FAIL.
156 */
157 static int
158add_members_to_class(
159 garray_T *gap,
160 ocmember_T **members,
161 int *member_count)
162{
163 *member_count = gap->ga_len;
164 *members = gap->ga_len == 0 ? NULL : ALLOC_MULT(ocmember_T, gap->ga_len);
165 if (gap->ga_len > 0 && *members == NULL)
166 return FAIL;
Bram Moolenaar8efdcee2022-12-19 12:18:09 +0000167 if (gap->ga_len > 0)
168 mch_memmove(*members, gap->ga_data, sizeof(ocmember_T) * gap->ga_len);
Bram Moolenaard505d172022-12-18 21:42:55 +0000169 VIM_CLEAR(gap->ga_data);
170 return OK;
171}
172
173/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000174 * Handle ":class" and ":abstract class" up to ":endclass".
175 */
176 void
177ex_class(exarg_T *eap)
178{
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000179 if (!current_script_is_vim9()
180 || (cmdmod.cmod_flags & CMOD_LEGACY)
181 || !getline_equal(eap->getline, eap->cookie, getsourceline))
182 {
183 emsg(_(e_class_can_only_be_defined_in_vim9_script));
184 return;
185 }
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000186
187 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000188 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000189 if (is_abstract)
190 {
191 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
192 {
193 semsg(_(e_invalid_argument_str), arg);
194 return;
195 }
196 arg = skipwhite(arg + 5);
197 }
198
199 if (!ASCII_ISUPPER(*arg))
200 {
201 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
202 return;
203 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000204 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
205 if (!IS_WHITE_OR_NUL(*name_end))
206 {
207 semsg(_(e_white_space_required_after_class_name_str), arg);
208 return;
209 }
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000210
211 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000212 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000213 // extends SomeClass
214 // implements SomeInterface
215 // specifies SomeInterface
Bram Moolenaard505d172022-12-18 21:42:55 +0000216 // check that nothing follows
217 // handle "is_export" if it is set
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000218
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000219 garray_T type_list; // list of pointers to allocated types
220 ga_init2(&type_list, sizeof(type_T *), 10);
221
Bram Moolenaard505d172022-12-18 21:42:55 +0000222 // Growarray with class members declared in the class.
223 garray_T classmembers;
224 ga_init2(&classmembers, sizeof(ocmember_T), 10);
225
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000226 // Growarray with functions declared in the class.
227 garray_T classfunctions;
228 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +0000229
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000230 // Growarray with object members declared in the class.
231 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +0000232 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000233
234 // Growarray with object methods declared in the class.
235 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000236 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000237
238 /*
239 * Go over the body of the class until "endclass" is found.
240 */
241 char_u *theline = NULL;
242 int success = FALSE;
243 for (;;)
244 {
245 vim_free(theline);
246 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
247 if (theline == NULL)
248 break;
249 char_u *line = skipwhite(theline);
250
Bram Moolenaar418b5472022-12-20 13:38:22 +0000251 // Skip empty and comment lines.
252 if (*line == NUL)
253 continue;
254 if (*line == '#')
255 {
256 if (vim9_bad_comment(line))
257 break;
258 continue;
259 }
260
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000261 char_u *p = line;
262 if (checkforcmd(&p, "endclass", 4))
263 {
264 if (STRNCMP(line, "endclass", 8) != 0)
265 semsg(_(e_command_cannot_be_shortened_str), line);
266 else if (*p == '|' || !ends_excmd2(line, p))
267 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +0000268 else
269 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000270 break;
271 }
272
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000273 int has_public = FALSE;
274 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000275 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000276 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000277 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000278 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000279 break;
280 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000281 has_public = TRUE;
282 p = skipwhite(line + 6);
283
Bram Moolenaard505d172022-12-18 21:42:55 +0000284 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000285 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000286 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000287 break;
288 }
289 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000290
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000291 int has_static = FALSE;
292 char_u *ps = p;
293 if (checkforcmd(&p, "static", 4))
294 {
295 if (STRNCMP(ps, "static", 6) != 0)
296 {
297 semsg(_(e_command_cannot_be_shortened_str), ps);
298 break;
299 }
300 has_static = TRUE;
301 p = skipwhite(ps + 6);
302 }
303
Bram Moolenaard505d172022-12-18 21:42:55 +0000304 // object members (public, read access, private):
305 // "this._varname"
306 // "this.varname"
307 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000308 if (STRNCMP(p, "this", 4) == 0)
309 {
310 if (p[4] != '.' || !eval_isnamec1(p[5]))
311 {
312 semsg(_(e_invalid_object_member_declaration_str), p);
313 break;
314 }
315 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +0000316 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +0000317 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +0000318 char_u *init_expr = NULL;
319 if (parse_member(eap, line, varname, has_public,
320 &varname_end, &type_list, &type, &init_expr) == FAIL)
321 break;
322 if (add_member(&objmembers, varname, varname_end,
323 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000324 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000325 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000326 break;
327 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000328 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000329
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000330 // constructors:
331 // def new()
332 // enddef
333 // def newOther()
334 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000335 // object methods and class functions:
336 // def SomeMethod()
337 // enddef
338 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000339 // enddef
340 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000341 // def <Tval> someMethod()
342 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000343 else if (checkforcmd(&p, "def", 3))
344 {
345 exarg_T ea;
346 garray_T lines_to_free;
347
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000348 // TODO: error for "public static def Func()"?
349
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000350 CLEAR_FIELD(ea);
351 ea.cmd = line;
352 ea.arg = p;
353 ea.cmdidx = CMD_def;
354 ea.getline = eap->getline;
355 ea.cookie = eap->cookie;
356
357 ga_init2(&lines_to_free, sizeof(char_u *), 50);
358 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free, TRUE);
359 ga_clear_strings(&lines_to_free);
360
361 // TODO: how about errors?
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000362 int is_new = STRNCMP(uf->uf_name, "new", 3) == 0;
363 garray_T *fgap = has_static || is_new
364 ? &classfunctions : &objmethods;
365 if (uf != NULL && ga_grow(fgap, 1) == OK)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000366 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000367 if (is_new)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000368 uf->uf_flags |= FC_NEW;
369
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000370 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
371 ++fgap->ga_len;
372 }
373 }
374
375 // class members
376 else if (has_static)
377 {
378 // class members (public, read access, private):
379 // "static _varname"
380 // "static varname"
381 // "public static varname"
382 char_u *varname = p;
383 char_u *varname_end = NULL;
384 type_T *type = NULL;
385 char_u *init_expr = NULL;
386 if (parse_member(eap, line, varname, has_public,
387 &varname_end, &type_list, &type, &init_expr) == FAIL)
388 break;
389 if (add_member(&classmembers, varname, varname_end,
390 has_public, type, init_expr) == FAIL)
391 {
392 vim_free(init_expr);
393 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000394 }
395 }
396
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000397 else
398 {
399 semsg(_(e_not_valid_command_in_class_str), line);
400 break;
401 }
402 }
403 vim_free(theline);
404
Bram Moolenaareb533502022-12-14 15:06:11 +0000405 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000406 if (success)
407 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000408 // "endclass" encountered without failures: Create the class.
409
Bram Moolenaareb533502022-12-14 15:06:11 +0000410 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000411 if (cl == NULL)
412 goto cleanup;
413 cl->class_refcount = 1;
414 cl->class_name = vim_strnsave(arg, name_end - arg);
Bram Moolenaard505d172022-12-18 21:42:55 +0000415 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000416 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000417
418 // Add class and object members to "cl".
419 if (add_members_to_class(&classmembers,
420 &cl->class_class_members,
421 &cl->class_class_member_count) == FAIL
422 || add_members_to_class(&objmembers,
423 &cl->class_obj_members,
424 &cl->class_obj_member_count) == FAIL)
425 goto cleanup;
426
427 if (cl->class_class_member_count > 0)
428 {
429 // Allocate a typval for each class member and initialize it.
430 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
431 cl->class_class_member_count);
432 if (cl->class_members_tv != NULL)
433 for (int i = 0; i < cl->class_class_member_count; ++i)
434 {
435 ocmember_T *m = &cl->class_class_members[i];
436 typval_T *tv = &cl->class_members_tv[i];
437 if (m->ocm_init != NULL)
438 {
439 typval_T *etv = eval_expr(m->ocm_init, eap);
440 if (etv != NULL)
441 {
442 *tv = *etv;
443 vim_free(etv);
444 }
445 }
446 else
447 {
448 // TODO: proper default value
449 tv->v_type = m->ocm_type->tt_type;
450 tv->vval.v_string = NULL;
451 }
452 }
453 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000454
455 int have_new = FALSE;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000456 for (int i = 0; i < classfunctions.ga_len; ++i)
457 if (STRCMP(((ufunc_T **)classfunctions.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000458 "new") == 0)
459 {
460 have_new = TRUE;
461 break;
462 }
463 if (!have_new)
464 {
465 // No new() method was defined, add the default constructor.
466 garray_T fga;
467 ga_init2(&fga, 1, 1000);
468 ga_concat(&fga, (char_u *)"new(");
469 for (int i = 0; i < cl->class_obj_member_count; ++i)
470 {
471 if (i > 0)
472 ga_concat(&fga, (char_u *)", ");
473 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000474 ocmember_T *m = cl->class_obj_members + i;
475 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000476 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000477 }
478 ga_concat(&fga, (char_u *)")\nenddef\n");
479 ga_append(&fga, NUL);
480
481 exarg_T fea;
482 CLEAR_FIELD(fea);
483 fea.cmdidx = CMD_def;
484 fea.cmd = fea.arg = fga.ga_data;
485
486 garray_T lines_to_free;
487 ga_init2(&lines_to_free, sizeof(char_u *), 50);
488
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000489 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, TRUE);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000490
491 ga_clear_strings(&lines_to_free);
492 vim_free(fga.ga_data);
493
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000494 if (nf != NULL && ga_grow(&classfunctions, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000495 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000496 ((ufunc_T **)classfunctions.ga_data)[classfunctions.ga_len] = nf;
497 ++classfunctions.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000498
499 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000500 nf->uf_ret_type = get_type_ptr(&type_list);
501 if (nf->uf_ret_type != NULL)
502 {
503 nf->uf_ret_type->tt_type = VAR_OBJECT;
504 nf->uf_ret_type->tt_member = (type_T *)cl;
505 nf->uf_ret_type->tt_argcount = 0;
506 nf->uf_ret_type->tt_args = NULL;
507 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000508 }
509 }
510
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000511 // loop 1: class functions, loop 2: object methods
512 for (int loop = 1; loop <= 2; ++loop)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000513 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000514 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
515 int *fcount = loop == 1 ? &cl->class_class_function_count
516 : &cl->class_obj_method_count;
517 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
518 : &cl->class_obj_methods;
519
520 *fcount = gap->ga_len;
521 if (gap->ga_len == 0)
522 {
523 *fup = NULL;
524 continue;
525 }
526 *fup = ALLOC_MULT(ufunc_T *, gap->ga_len);
527 if (*fup == NULL)
528 goto cleanup;
529 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
530 vim_free(gap->ga_data);
531
532 // Set the class pointer on all the object methods.
533 for (int i = 0; i < gap->ga_len; ++i)
534 {
535 ufunc_T *fp = (*fup)[i];
536 fp->uf_class = cl;
537 if (loop == 2)
538 fp->uf_flags |= FC_OBJECT;
539 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000540 }
541
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000542 cl->class_type.tt_type = VAR_CLASS;
543 cl->class_type.tt_member = (type_T *)cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000544 cl->class_object_type.tt_type = VAR_OBJECT;
545 cl->class_object_type.tt_member = (type_T *)cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000546 cl->class_type_list = type_list;
547
548 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +0000549 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000550
551 // Add the class to the script-local variables.
552 typval_T tv;
553 tv.v_type = VAR_CLASS;
554 tv.vval.v_class = cl;
555 set_var_const(cl->class_name, current_sctx.sc_sid,
556 NULL, &tv, FALSE, ASSIGN_DECL, 0);
557 return;
558 }
559
560cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +0000561 if (cl != NULL)
562 {
563 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000564 vim_free(cl->class_class_functions);
Bram Moolenaareb533502022-12-14 15:06:11 +0000565 vim_free(cl->class_obj_members);
566 vim_free(cl->class_obj_methods);
567 vim_free(cl);
568 }
569
Bram Moolenaard505d172022-12-18 21:42:55 +0000570 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000571 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000572 garray_T *gap = round == 1 ? &classmembers : &objmembers;
573 if (gap->ga_len == 0 || gap->ga_data == NULL)
574 continue;
575
576 for (int i = 0; i < gap->ga_len; ++i)
577 {
578 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
579 vim_free(m->ocm_name);
580 vim_free(m->ocm_init);
581 }
582 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000583 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000584
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000585 for (int i = 0; i < objmethods.ga_len; ++i)
586 {
587 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
588 func_clear_free(uf, FALSE);
589 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000590 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000591
592 for (int i = 0; i < classfunctions.ga_len; ++i)
593 {
594 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
595 func_clear_free(uf, FALSE);
596 }
597 ga_clear(&classfunctions);
598
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000599 clear_type_list(&type_list);
600}
601
602/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000603 * Find member "name" in class "cl", set "member_idx" to the member index and
604 * return its type.
605 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000606 */
607 type_T *
608class_member_type(
609 class_T *cl,
610 char_u *name,
611 char_u *name_end,
612 int *member_idx)
613{
614 *member_idx = -1; // not found (yet)
615 size_t len = name_end - name;
616
617 for (int i = 0; i < cl->class_obj_member_count; ++i)
618 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000619 ocmember_T *m = cl->class_obj_members + i;
620 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000621 {
622 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +0000623 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000624 }
625 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000626
627 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000628 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000629}
630
631/*
632 * Handle ":interface" up to ":endinterface".
633 */
634 void
635ex_interface(exarg_T *eap UNUSED)
636{
637 // TODO
638}
639
640/*
641 * Handle ":enum" up to ":endenum".
642 */
643 void
644ex_enum(exarg_T *eap UNUSED)
645{
646 // TODO
647}
648
649/*
650 * Handle ":type".
651 */
652 void
653ex_type(exarg_T *eap UNUSED)
654{
655 // TODO
656}
657
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000658/*
659 * Evaluate what comes after a class:
660 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000661 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000662 * - class constructor: SomeClass.new()
663 * - object member: someObject.varname
664 * - object method: someObject.SomeMethod()
665 *
666 * "*arg" points to the '.'.
667 * "*arg" is advanced to after the member name or method call.
668 *
669 * Returns FAIL or OK.
670 */
671 int
672class_object_index(
673 char_u **arg,
674 typval_T *rettv,
675 evalarg_T *evalarg,
676 int verbose UNUSED) // give error messages
677{
678 // int evaluate = evalarg != NULL
679 // && (evalarg->eval_flags & EVAL_EVALUATE);
680
681 if (VIM_ISWHITE((*arg)[1]))
682 {
683 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
684 return FAIL;
685 }
686
687 ++*arg;
688 char_u *name = *arg;
689 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
690 if (name_end == name)
691 return FAIL;
692 size_t len = name_end - name;
693
694 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
695 : rettv->vval.v_object->obj_class;
696 if (*name_end == '(')
697 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000698 int on_class = rettv->v_type == VAR_CLASS;
699 int count = on_class ? cl->class_class_function_count
700 : cl->class_obj_method_count;
701 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000702 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000703 ufunc_T *fp = on_class ? cl->class_class_functions[i]
704 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +0000705 // Use a separate pointer to avoid that ASAN complains about
706 // uf_name[] only being 4 characters.
707 char_u *ufname = (char_u *)fp->uf_name;
708 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000709 {
710 typval_T argvars[MAX_FUNC_ARGS + 1];
711 int argcount = 0;
712
713 char_u *argp = name_end;
714 int ret = get_func_arguments(&argp, evalarg, 0,
715 argvars, &argcount);
716 if (ret == FAIL)
717 return FAIL;
718
719 funcexe_T funcexe;
720 CLEAR_FIELD(funcexe);
721 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000722 if (rettv->v_type == VAR_OBJECT)
723 {
724 funcexe.fe_object = rettv->vval.v_object;
725 ++funcexe.fe_object->obj_refcount;
726 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000727
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000728 // Clear the class or object after calling the function, in
729 // case the refcount is one.
730 typval_T tv_tofree = *rettv;
731 rettv->v_type = VAR_UNKNOWN;
732
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000733 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000734 int error = call_user_func_check(fp, argcount, argvars,
735 rettv, &funcexe, NULL);
736
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000737 // Clear the previous rettv and the arguments.
738 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000739 for (int idx = 0; idx < argcount; ++idx)
740 clear_tv(&argvars[idx]);
741
742 if (error != FCERR_NONE)
743 {
744 user_func_error(error, printable_func_name(fp),
745 funcexe.fe_found_var);
746 return FAIL;
747 }
748 *arg = argp;
749 return OK;
750 }
751 }
752
753 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
754 }
755
756 else if (rettv->v_type == VAR_OBJECT)
757 {
758 for (int i = 0; i < cl->class_obj_member_count; ++i)
759 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000760 ocmember_T *m = &cl->class_obj_members[i];
761 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000762 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000763 if (*name == '_')
764 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000765 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000766 return FAIL;
767 }
768
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000769 // The object only contains a pointer to the class, the member
770 // values array follows right after that.
771 object_T *obj = rettv->vval.v_object;
772 typval_T *tv = (typval_T *)(obj + 1) + i;
773 copy_tv(tv, rettv);
774 object_unref(obj);
775
776 *arg = name_end;
777 return OK;
778 }
779 }
780
781 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
782 }
783
Bram Moolenaard505d172022-12-18 21:42:55 +0000784 else if (rettv->v_type == VAR_CLASS)
785 {
786 // class member
787 for (int i = 0; i < cl->class_class_member_count; ++i)
788 {
789 ocmember_T *m = &cl->class_class_members[i];
790 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
791 {
792 if (*name == '_')
793 {
794 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
795 return FAIL;
796 }
797
798 typval_T *tv = &cl->class_members_tv[i];
799 copy_tv(tv, rettv);
800 class_unref(cl);
801
802 *arg = name_end;
803 return OK;
804 }
805 }
806
807 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
808 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000809
810 return FAIL;
811}
812
813/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000814 * If "arg" points to a class or object method, return it.
815 * Otherwise return NULL.
816 */
817 ufunc_T *
818find_class_func(char_u **arg)
819{
820 char_u *name = *arg;
821 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
822 if (name_end == name || *name_end != '.')
823 return NULL;
824
825 size_t len = name_end - name;
826 typval_T tv;
827 tv.v_type = VAR_UNKNOWN;
828 if (eval_variable(name, len, 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
829 return NULL;
830 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +0000831 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000832
833 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
834 : tv.vval.v_object->obj_class;
835 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +0000836 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000837 char_u *fname = name_end + 1;
838 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
839 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +0000840 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000841 len = fname_end - fname;
842
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000843 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
844 : cl->class_obj_method_count;
845 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
846 : cl->class_obj_methods;
847 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000848 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000849 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000850 // Use a separate pointer to avoid that ASAN complains about
851 // uf_name[] only being 4 characters.
852 char_u *ufname = (char_u *)fp->uf_name;
853 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +0000854 {
855 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000856 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +0000857 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000858 }
859
Bram Moolenaareb533502022-12-14 15:06:11 +0000860fail_after_eval:
861 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000862 return NULL;
863}
864
865/*
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000866 * If "cctx->ctx_ufunc" indicates we are in a class, check if "name" is a class
867 * member. If it is then return TRUE and set "cl_ret" and "idx_ret".
868 */
869 int
870class_member_exists(
871 char_u *name,
872 class_T **cl_ret,
873 int *idx_ret,
874 cctx_T *cctx)
875{
876 if (cctx->ctx_ufunc == NULL || cctx->ctx_ufunc->uf_class == NULL)
877 return FALSE;
878 class_T *cl = cctx->ctx_ufunc->uf_class;
879
880 for (int idx = 0; idx < cl->class_class_member_count; ++idx)
881 {
882 ocmember_T *m = &cl->class_class_members[idx];
883 if (STRCMP(m->ocm_name, name) == 0)
884 {
885 *cl_ret = cl;
886 *idx_ret = idx;
887 return TRUE;
888 }
889 }
890
891 return FALSE;
892}
893
894/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000895 * Make a copy of an object.
896 */
897 void
898copy_object(typval_T *from, typval_T *to)
899{
900 *to = *from;
901 if (to->vval.v_object != NULL)
902 ++to->vval.v_object->obj_refcount;
903}
904
905/*
906 * Free an object.
907 */
908 static void
909object_clear(object_T *obj)
910{
911 class_T *cl = obj->obj_class;
912
913 // the member values are just after the object structure
914 typval_T *tv = (typval_T *)(obj + 1);
915 for (int i = 0; i < cl->class_obj_member_count; ++i)
916 clear_tv(tv + i);
917
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000918 // Remove from the list headed by "first_object".
919 object_cleared(obj);
920
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000921 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000922 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000923}
924
925/*
926 * Unreference an object.
927 */
928 void
929object_unref(object_T *obj)
930{
931 if (obj != NULL && --obj->obj_refcount <= 0)
932 object_clear(obj);
933}
934
935/*
936 * Make a copy of a class.
937 */
938 void
939copy_class(typval_T *from, typval_T *to)
940{
941 *to = *from;
942 if (to->vval.v_class != NULL)
943 ++to->vval.v_class->class_refcount;
944}
945
946/*
947 * Unreference a class. Free it when the reference count goes down to zero.
948 */
949 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000950class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000951{
Bram Moolenaard505d172022-12-18 21:42:55 +0000952 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000953 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000954 // Freeing what the class contains may recursively come back here.
955 // Clear "class_name" first, if it is NULL the class does not need to
956 // be freed.
957 VIM_CLEAR(cl->class_name);
958
959 for (int i = 0; i < cl->class_class_member_count; ++i)
960 {
961 ocmember_T *m = &cl->class_class_members[i];
962 vim_free(m->ocm_name);
963 vim_free(m->ocm_init);
964 if (cl->class_members_tv != NULL)
965 clear_tv(&cl->class_members_tv[i]);
966 }
967 vim_free(cl->class_class_members);
968 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000969
970 for (int i = 0; i < cl->class_obj_member_count; ++i)
971 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000972 ocmember_T *m = &cl->class_obj_members[i];
973 vim_free(m->ocm_name);
974 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000975 }
976 vim_free(cl->class_obj_members);
977
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000978 for (int i = 0; i < cl->class_obj_method_count; ++i)
979 {
980 ufunc_T *uf = cl->class_obj_methods[i];
981 func_clear_free(uf, FALSE);
982 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000983 vim_free(cl->class_obj_methods);
984
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000985 clear_type_list(&cl->class_type_list);
986
987 vim_free(cl);
988 }
989}
990
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000991static object_T *first_object = NULL;
992
993/*
994 * Call this function when an object has been created. It will be added to the
995 * list headed by "first_object".
996 */
997 void
998object_created(object_T *obj)
999{
1000 if (first_object != NULL)
1001 {
1002 obj->obj_next_used = first_object;
1003 first_object->obj_prev_used = obj;
1004 }
1005 first_object = obj;
1006}
1007
1008/*
1009 * Call this function when an object has been cleared and is about to be freed.
1010 * It is removed from the list headed by "first_object".
1011 */
1012 void
1013object_cleared(object_T *obj)
1014{
1015 if (obj->obj_next_used != NULL)
1016 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1017 if (obj->obj_prev_used != NULL)
1018 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1019 else if (first_object == obj)
1020 first_object = obj->obj_next_used;
1021}
1022
1023/*
1024 * Go through the list of all objects and free items without "copyID".
1025 */
1026 int
1027object_free_nonref(int copyID)
1028{
1029 int did_free = FALSE;
1030 object_T *next_obj;
1031
1032 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
1033 {
1034 next_obj = obj->obj_next_used;
1035 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1036 {
1037 // Free the object and items it contains.
1038 object_clear(obj);
1039 did_free = TRUE;
1040 }
1041 }
1042
1043 return did_free;
1044}
1045
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001046
1047#endif // FEAT_EVAL