blob: 8e05a17f4c2a6067425fdc38bfda67f97ac51f84 [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
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000361 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000362 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000363 int is_new = STRNCMP(uf->uf_name, "new", 3) == 0;
364 garray_T *fgap = has_static || is_new
365 ? &classfunctions : &objmethods;
366 if (ga_grow(fgap, 1) == OK)
367 {
368 if (is_new)
369 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000370
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000371 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
372 ++fgap->ga_len;
373 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000374 }
375 }
376
377 // class members
378 else if (has_static)
379 {
380 // class members (public, read access, private):
381 // "static _varname"
382 // "static varname"
383 // "public static varname"
384 char_u *varname = p;
385 char_u *varname_end = NULL;
386 type_T *type = NULL;
387 char_u *init_expr = NULL;
388 if (parse_member(eap, line, varname, has_public,
389 &varname_end, &type_list, &type, &init_expr) == FAIL)
390 break;
391 if (add_member(&classmembers, varname, varname_end,
392 has_public, type, init_expr) == FAIL)
393 {
394 vim_free(init_expr);
395 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000396 }
397 }
398
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000399 else
400 {
401 semsg(_(e_not_valid_command_in_class_str), line);
402 break;
403 }
404 }
405 vim_free(theline);
406
Bram Moolenaareb533502022-12-14 15:06:11 +0000407 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000408 if (success)
409 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000410 // "endclass" encountered without failures: Create the class.
411
Bram Moolenaareb533502022-12-14 15:06:11 +0000412 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000413 if (cl == NULL)
414 goto cleanup;
415 cl->class_refcount = 1;
416 cl->class_name = vim_strnsave(arg, name_end - arg);
Bram Moolenaard505d172022-12-18 21:42:55 +0000417 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000418 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000419
420 // Add class and object members to "cl".
421 if (add_members_to_class(&classmembers,
422 &cl->class_class_members,
423 &cl->class_class_member_count) == FAIL
424 || add_members_to_class(&objmembers,
425 &cl->class_obj_members,
426 &cl->class_obj_member_count) == FAIL)
427 goto cleanup;
428
429 if (cl->class_class_member_count > 0)
430 {
431 // Allocate a typval for each class member and initialize it.
432 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
433 cl->class_class_member_count);
434 if (cl->class_members_tv != NULL)
435 for (int i = 0; i < cl->class_class_member_count; ++i)
436 {
437 ocmember_T *m = &cl->class_class_members[i];
438 typval_T *tv = &cl->class_members_tv[i];
439 if (m->ocm_init != NULL)
440 {
441 typval_T *etv = eval_expr(m->ocm_init, eap);
442 if (etv != NULL)
443 {
444 *tv = *etv;
445 vim_free(etv);
446 }
447 }
448 else
449 {
450 // TODO: proper default value
451 tv->v_type = m->ocm_type->tt_type;
452 tv->vval.v_string = NULL;
453 }
454 }
455 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000456
457 int have_new = FALSE;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000458 for (int i = 0; i < classfunctions.ga_len; ++i)
459 if (STRCMP(((ufunc_T **)classfunctions.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000460 "new") == 0)
461 {
462 have_new = TRUE;
463 break;
464 }
465 if (!have_new)
466 {
467 // No new() method was defined, add the default constructor.
468 garray_T fga;
469 ga_init2(&fga, 1, 1000);
470 ga_concat(&fga, (char_u *)"new(");
471 for (int i = 0; i < cl->class_obj_member_count; ++i)
472 {
473 if (i > 0)
474 ga_concat(&fga, (char_u *)", ");
475 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000476 ocmember_T *m = cl->class_obj_members + i;
477 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000478 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000479 }
480 ga_concat(&fga, (char_u *)")\nenddef\n");
481 ga_append(&fga, NUL);
482
483 exarg_T fea;
484 CLEAR_FIELD(fea);
485 fea.cmdidx = CMD_def;
486 fea.cmd = fea.arg = fga.ga_data;
487
488 garray_T lines_to_free;
489 ga_init2(&lines_to_free, sizeof(char_u *), 50);
490
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000491 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, TRUE);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000492
493 ga_clear_strings(&lines_to_free);
494 vim_free(fga.ga_data);
495
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000496 if (nf != NULL && ga_grow(&classfunctions, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000497 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000498 ((ufunc_T **)classfunctions.ga_data)[classfunctions.ga_len] = nf;
499 ++classfunctions.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000500
501 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000502 nf->uf_ret_type = get_type_ptr(&type_list);
503 if (nf->uf_ret_type != NULL)
504 {
505 nf->uf_ret_type->tt_type = VAR_OBJECT;
506 nf->uf_ret_type->tt_member = (type_T *)cl;
507 nf->uf_ret_type->tt_argcount = 0;
508 nf->uf_ret_type->tt_args = NULL;
509 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000510 }
511 }
512
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000513 // loop 1: class functions, loop 2: object methods
514 for (int loop = 1; loop <= 2; ++loop)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000515 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000516 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
517 int *fcount = loop == 1 ? &cl->class_class_function_count
518 : &cl->class_obj_method_count;
519 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
520 : &cl->class_obj_methods;
521
522 *fcount = gap->ga_len;
523 if (gap->ga_len == 0)
524 {
525 *fup = NULL;
526 continue;
527 }
528 *fup = ALLOC_MULT(ufunc_T *, gap->ga_len);
529 if (*fup == NULL)
530 goto cleanup;
531 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
532 vim_free(gap->ga_data);
533
534 // Set the class pointer on all the object methods.
535 for (int i = 0; i < gap->ga_len; ++i)
536 {
537 ufunc_T *fp = (*fup)[i];
538 fp->uf_class = cl;
539 if (loop == 2)
540 fp->uf_flags |= FC_OBJECT;
541 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000542 }
543
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000544 cl->class_type.tt_type = VAR_CLASS;
545 cl->class_type.tt_member = (type_T *)cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000546 cl->class_object_type.tt_type = VAR_OBJECT;
547 cl->class_object_type.tt_member = (type_T *)cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000548 cl->class_type_list = type_list;
549
550 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +0000551 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000552
553 // Add the class to the script-local variables.
554 typval_T tv;
555 tv.v_type = VAR_CLASS;
556 tv.vval.v_class = cl;
557 set_var_const(cl->class_name, current_sctx.sc_sid,
558 NULL, &tv, FALSE, ASSIGN_DECL, 0);
559 return;
560 }
561
562cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +0000563 if (cl != NULL)
564 {
565 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000566 vim_free(cl->class_class_functions);
Bram Moolenaareb533502022-12-14 15:06:11 +0000567 vim_free(cl->class_obj_members);
568 vim_free(cl->class_obj_methods);
569 vim_free(cl);
570 }
571
Bram Moolenaard505d172022-12-18 21:42:55 +0000572 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000573 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000574 garray_T *gap = round == 1 ? &classmembers : &objmembers;
575 if (gap->ga_len == 0 || gap->ga_data == NULL)
576 continue;
577
578 for (int i = 0; i < gap->ga_len; ++i)
579 {
580 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
581 vim_free(m->ocm_name);
582 vim_free(m->ocm_init);
583 }
584 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000585 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000586
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000587 for (int i = 0; i < objmethods.ga_len; ++i)
588 {
589 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
590 func_clear_free(uf, FALSE);
591 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000592 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000593
594 for (int i = 0; i < classfunctions.ga_len; ++i)
595 {
596 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
597 func_clear_free(uf, FALSE);
598 }
599 ga_clear(&classfunctions);
600
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000601 clear_type_list(&type_list);
602}
603
604/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000605 * Find member "name" in class "cl", set "member_idx" to the member index and
606 * return its type.
607 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000608 */
609 type_T *
610class_member_type(
611 class_T *cl,
612 char_u *name,
613 char_u *name_end,
614 int *member_idx)
615{
616 *member_idx = -1; // not found (yet)
617 size_t len = name_end - name;
618
619 for (int i = 0; i < cl->class_obj_member_count; ++i)
620 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000621 ocmember_T *m = cl->class_obj_members + i;
622 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000623 {
624 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +0000625 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000626 }
627 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000628
629 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000630 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000631}
632
633/*
634 * Handle ":interface" up to ":endinterface".
635 */
636 void
637ex_interface(exarg_T *eap UNUSED)
638{
639 // TODO
640}
641
642/*
643 * Handle ":enum" up to ":endenum".
644 */
645 void
646ex_enum(exarg_T *eap UNUSED)
647{
648 // TODO
649}
650
651/*
652 * Handle ":type".
653 */
654 void
655ex_type(exarg_T *eap UNUSED)
656{
657 // TODO
658}
659
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000660/*
661 * Evaluate what comes after a class:
662 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000663 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000664 * - class constructor: SomeClass.new()
665 * - object member: someObject.varname
666 * - object method: someObject.SomeMethod()
667 *
668 * "*arg" points to the '.'.
669 * "*arg" is advanced to after the member name or method call.
670 *
671 * Returns FAIL or OK.
672 */
673 int
674class_object_index(
675 char_u **arg,
676 typval_T *rettv,
677 evalarg_T *evalarg,
678 int verbose UNUSED) // give error messages
679{
680 // int evaluate = evalarg != NULL
681 // && (evalarg->eval_flags & EVAL_EVALUATE);
682
683 if (VIM_ISWHITE((*arg)[1]))
684 {
685 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
686 return FAIL;
687 }
688
689 ++*arg;
690 char_u *name = *arg;
691 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
692 if (name_end == name)
693 return FAIL;
694 size_t len = name_end - name;
695
696 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
697 : rettv->vval.v_object->obj_class;
698 if (*name_end == '(')
699 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000700 int on_class = rettv->v_type == VAR_CLASS;
701 int count = on_class ? cl->class_class_function_count
702 : cl->class_obj_method_count;
703 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000704 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000705 ufunc_T *fp = on_class ? cl->class_class_functions[i]
706 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +0000707 // Use a separate pointer to avoid that ASAN complains about
708 // uf_name[] only being 4 characters.
709 char_u *ufname = (char_u *)fp->uf_name;
710 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000711 {
712 typval_T argvars[MAX_FUNC_ARGS + 1];
713 int argcount = 0;
714
715 char_u *argp = name_end;
716 int ret = get_func_arguments(&argp, evalarg, 0,
717 argvars, &argcount);
718 if (ret == FAIL)
719 return FAIL;
720
721 funcexe_T funcexe;
722 CLEAR_FIELD(funcexe);
723 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000724 if (rettv->v_type == VAR_OBJECT)
725 {
726 funcexe.fe_object = rettv->vval.v_object;
727 ++funcexe.fe_object->obj_refcount;
728 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000729
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000730 // Clear the class or object after calling the function, in
731 // case the refcount is one.
732 typval_T tv_tofree = *rettv;
733 rettv->v_type = VAR_UNKNOWN;
734
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000735 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000736 int error = call_user_func_check(fp, argcount, argvars,
737 rettv, &funcexe, NULL);
738
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000739 // Clear the previous rettv and the arguments.
740 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000741 for (int idx = 0; idx < argcount; ++idx)
742 clear_tv(&argvars[idx]);
743
744 if (error != FCERR_NONE)
745 {
746 user_func_error(error, printable_func_name(fp),
747 funcexe.fe_found_var);
748 return FAIL;
749 }
750 *arg = argp;
751 return OK;
752 }
753 }
754
755 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
756 }
757
758 else if (rettv->v_type == VAR_OBJECT)
759 {
760 for (int i = 0; i < cl->class_obj_member_count; ++i)
761 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000762 ocmember_T *m = &cl->class_obj_members[i];
763 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000764 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000765 if (*name == '_')
766 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000767 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000768 return FAIL;
769 }
770
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000771 // The object only contains a pointer to the class, the member
772 // values array follows right after that.
773 object_T *obj = rettv->vval.v_object;
774 typval_T *tv = (typval_T *)(obj + 1) + i;
775 copy_tv(tv, rettv);
776 object_unref(obj);
777
778 *arg = name_end;
779 return OK;
780 }
781 }
782
783 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
784 }
785
Bram Moolenaard505d172022-12-18 21:42:55 +0000786 else if (rettv->v_type == VAR_CLASS)
787 {
788 // class member
789 for (int i = 0; i < cl->class_class_member_count; ++i)
790 {
791 ocmember_T *m = &cl->class_class_members[i];
792 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
793 {
794 if (*name == '_')
795 {
796 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
797 return FAIL;
798 }
799
800 typval_T *tv = &cl->class_members_tv[i];
801 copy_tv(tv, rettv);
802 class_unref(cl);
803
804 *arg = name_end;
805 return OK;
806 }
807 }
808
809 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
810 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000811
812 return FAIL;
813}
814
815/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000816 * If "arg" points to a class or object method, return it.
817 * Otherwise return NULL.
818 */
819 ufunc_T *
820find_class_func(char_u **arg)
821{
822 char_u *name = *arg;
823 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
824 if (name_end == name || *name_end != '.')
825 return NULL;
826
827 size_t len = name_end - name;
828 typval_T tv;
829 tv.v_type = VAR_UNKNOWN;
830 if (eval_variable(name, len, 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
831 return NULL;
832 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +0000833 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000834
835 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
836 : tv.vval.v_object->obj_class;
837 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +0000838 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000839 char_u *fname = name_end + 1;
840 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
841 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +0000842 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000843 len = fname_end - fname;
844
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000845 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
846 : cl->class_obj_method_count;
847 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
848 : cl->class_obj_methods;
849 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000850 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000851 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000852 // Use a separate pointer to avoid that ASAN complains about
853 // uf_name[] only being 4 characters.
854 char_u *ufname = (char_u *)fp->uf_name;
855 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +0000856 {
857 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000858 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +0000859 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000860 }
861
Bram Moolenaareb533502022-12-14 15:06:11 +0000862fail_after_eval:
863 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000864 return NULL;
865}
866
867/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000868 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
869 * index in class.class_class_members[].
870 * If "cl_ret" is not NULL set it to the class.
871 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000872 */
873 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000874class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000875{
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000876 if (cctx == NULL || cctx->ctx_ufunc == NULL
877 || cctx->ctx_ufunc->uf_class == NULL)
878 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000879 class_T *cl = cctx->ctx_ufunc->uf_class;
880
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000881 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000882 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000883 ocmember_T *m = &cl->class_class_members[i];
884 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000885 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000886 if (cl_ret != NULL)
887 *cl_ret = cl;
888 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000889 }
890 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000891 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000892}
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 Moolenaarec8b74f2023-01-01 14:11:27 +0000978 for (int i = 0; i < cl->class_class_function_count; ++i)
979 {
980 ufunc_T *uf = cl->class_class_functions[i];
981 func_clear_free(uf, FALSE);
982 }
983 vim_free(cl->class_class_functions);
984
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000985 for (int i = 0; i < cl->class_obj_method_count; ++i)
986 {
987 ufunc_T *uf = cl->class_obj_methods[i];
988 func_clear_free(uf, FALSE);
989 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000990 vim_free(cl->class_obj_methods);
991
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000992 clear_type_list(&cl->class_type_list);
993
994 vim_free(cl);
995 }
996}
997
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000998static object_T *first_object = NULL;
999
1000/*
1001 * Call this function when an object has been created. It will be added to the
1002 * list headed by "first_object".
1003 */
1004 void
1005object_created(object_T *obj)
1006{
1007 if (first_object != NULL)
1008 {
1009 obj->obj_next_used = first_object;
1010 first_object->obj_prev_used = obj;
1011 }
1012 first_object = obj;
1013}
1014
1015/*
1016 * Call this function when an object has been cleared and is about to be freed.
1017 * It is removed from the list headed by "first_object".
1018 */
1019 void
1020object_cleared(object_T *obj)
1021{
1022 if (obj->obj_next_used != NULL)
1023 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1024 if (obj->obj_prev_used != NULL)
1025 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1026 else if (first_object == obj)
1027 first_object = obj->obj_next_used;
1028}
1029
1030/*
1031 * Go through the list of all objects and free items without "copyID".
1032 */
1033 int
1034object_free_nonref(int copyID)
1035{
1036 int did_free = FALSE;
1037 object_T *next_obj;
1038
1039 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
1040 {
1041 next_obj = obj->obj_next_used;
1042 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1043 {
1044 // Free the object and items it contains.
1045 object_clear(obj);
1046 did_free = TRUE;
1047 }
1048 }
1049
1050 return did_free;
1051}
1052
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001053
1054#endif // FEAT_EVAL