blob: 2e1cac0c441eecdcffbb65eb74852c3ad66eca45 [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
226 // Growarray with object methods declared in the class.
227 garray_T classmethods;
228 ga_init2(&classmethods, sizeof(ufunc_T *), 10);
229
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
291 // object members (public, read access, private):
292 // "this._varname"
293 // "this.varname"
294 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000295 if (STRNCMP(p, "this", 4) == 0)
296 {
297 if (p[4] != '.' || !eval_isnamec1(p[5]))
298 {
299 semsg(_(e_invalid_object_member_declaration_str), p);
300 break;
301 }
302 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +0000303 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +0000304 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +0000305 char_u *init_expr = NULL;
306 if (parse_member(eap, line, varname, has_public,
307 &varname_end, &type_list, &type, &init_expr) == FAIL)
308 break;
309 if (add_member(&objmembers, varname, varname_end,
310 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000311 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000312 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000313 break;
314 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000315 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000316
Bram Moolenaard505d172022-12-18 21:42:55 +0000317 // class members and methods
318 else if (checkforcmd(&p, "static", 6))
319 {
320 p = skipwhite(p);
321 if (checkforcmd(&p, "def", 3))
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000322 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000323 // TODO: class method
324 // static def someMethod()
325 // enddef
326 // static def <Tval> someMethod()
327 // enddef
328 }
329 else
330 {
331 // class members (public, read access, private):
332 // "static _varname"
333 // "static varname"
334 // "public static varname"
335 char_u *varname = p;
336 char_u *varname_end = NULL;
337 type_T *type = NULL;
338 char_u *init_expr = NULL;
339 if (parse_member(eap, line, varname, has_public,
340 &varname_end, &type_list, &type, &init_expr) == FAIL)
341 break;
342 if (add_member(&classmembers, varname, varname_end,
343 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000344 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000345 vim_free(init_expr);
Bram Moolenaar74e12742022-12-13 21:14:28 +0000346 break;
347 }
Bram Moolenaar74e12742022-12-13 21:14:28 +0000348 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000349 }
350
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000351 // constructors:
352 // def new()
353 // enddef
354 // def newOther()
355 // enddef
356 // methods:
357 // def someMethod()
358 // enddef
359 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000360 // def <Tval> someMethod()
361 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000362 else if (checkforcmd(&p, "def", 3))
363 {
364 exarg_T ea;
365 garray_T lines_to_free;
366
367 CLEAR_FIELD(ea);
368 ea.cmd = line;
369 ea.arg = p;
370 ea.cmdidx = CMD_def;
371 ea.getline = eap->getline;
372 ea.cookie = eap->cookie;
373
374 ga_init2(&lines_to_free, sizeof(char_u *), 50);
375 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free, TRUE);
376 ga_clear_strings(&lines_to_free);
377
378 // TODO: how about errors?
379 if (uf != NULL && ga_grow(&objmethods, 1) == OK)
380 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000381 if (STRNCMP(uf->uf_name, "new", 3) == 0)
382 uf->uf_flags |= FC_NEW;
383
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000384 ((ufunc_T **)objmethods.ga_data)[objmethods.ga_len] = uf;
385 ++objmethods.ga_len;
386 }
387 }
388
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000389 else
390 {
391 semsg(_(e_not_valid_command_in_class_str), line);
392 break;
393 }
394 }
395 vim_free(theline);
396
Bram Moolenaareb533502022-12-14 15:06:11 +0000397 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000398 if (success)
399 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000400 // "endclass" encountered without failures: Create the class.
401
Bram Moolenaareb533502022-12-14 15:06:11 +0000402 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000403 if (cl == NULL)
404 goto cleanup;
405 cl->class_refcount = 1;
406 cl->class_name = vim_strnsave(arg, name_end - arg);
Bram Moolenaard505d172022-12-18 21:42:55 +0000407 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000408 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000409
410 // Add class and object members to "cl".
411 if (add_members_to_class(&classmembers,
412 &cl->class_class_members,
413 &cl->class_class_member_count) == FAIL
414 || add_members_to_class(&objmembers,
415 &cl->class_obj_members,
416 &cl->class_obj_member_count) == FAIL)
417 goto cleanup;
418
419 if (cl->class_class_member_count > 0)
420 {
421 // Allocate a typval for each class member and initialize it.
422 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
423 cl->class_class_member_count);
424 if (cl->class_members_tv != NULL)
425 for (int i = 0; i < cl->class_class_member_count; ++i)
426 {
427 ocmember_T *m = &cl->class_class_members[i];
428 typval_T *tv = &cl->class_members_tv[i];
429 if (m->ocm_init != NULL)
430 {
431 typval_T *etv = eval_expr(m->ocm_init, eap);
432 if (etv != NULL)
433 {
434 *tv = *etv;
435 vim_free(etv);
436 }
437 }
438 else
439 {
440 // TODO: proper default value
441 tv->v_type = m->ocm_type->tt_type;
442 tv->vval.v_string = NULL;
443 }
444 }
445 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000446
447 int have_new = FALSE;
448 for (int i = 0; i < objmethods.ga_len; ++i)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000449 if (STRCMP(((ufunc_T **)objmethods.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000450 "new") == 0)
451 {
452 have_new = TRUE;
453 break;
454 }
455 if (!have_new)
456 {
457 // No new() method was defined, add the default constructor.
458 garray_T fga;
459 ga_init2(&fga, 1, 1000);
460 ga_concat(&fga, (char_u *)"new(");
461 for (int i = 0; i < cl->class_obj_member_count; ++i)
462 {
463 if (i > 0)
464 ga_concat(&fga, (char_u *)", ");
465 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000466 ocmember_T *m = cl->class_obj_members + i;
467 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000468 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000469 }
470 ga_concat(&fga, (char_u *)")\nenddef\n");
471 ga_append(&fga, NUL);
472
473 exarg_T fea;
474 CLEAR_FIELD(fea);
475 fea.cmdidx = CMD_def;
476 fea.cmd = fea.arg = fga.ga_data;
477
478 garray_T lines_to_free;
479 ga_init2(&lines_to_free, sizeof(char_u *), 50);
480
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000481 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, TRUE);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000482
483 ga_clear_strings(&lines_to_free);
484 vim_free(fga.ga_data);
485
486 if (nf != NULL && ga_grow(&objmethods, 1) == OK)
487 {
488 ((ufunc_T **)objmethods.ga_data)[objmethods.ga_len] = nf;
489 ++objmethods.ga_len;
490
491 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000492 nf->uf_ret_type = get_type_ptr(&type_list);
493 if (nf->uf_ret_type != NULL)
494 {
495 nf->uf_ret_type->tt_type = VAR_OBJECT;
496 nf->uf_ret_type->tt_member = (type_T *)cl;
497 nf->uf_ret_type->tt_argcount = 0;
498 nf->uf_ret_type->tt_args = NULL;
499 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000500 }
501 }
502
Bram Moolenaard505d172022-12-18 21:42:55 +0000503 // TODO: class methods
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000504 cl->class_obj_method_count = objmethods.ga_len;
505 cl->class_obj_methods = ALLOC_MULT(ufunc_T *, objmethods.ga_len);
506 if (cl->class_obj_methods == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000507 goto cleanup;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000508 mch_memmove(cl->class_obj_methods, objmethods.ga_data,
509 sizeof(ufunc_T *) * objmethods.ga_len);
510 vim_free(objmethods.ga_data);
511
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000512 // Set the class pointer on all the object methods.
513 for (int i = 0; i < objmethods.ga_len; ++i)
514 {
515 ufunc_T *fp = cl->class_obj_methods[i];
516 fp->uf_class = cl;
517 fp->uf_flags |= FC_OBJECT; // TODO: not for class method
518 }
519
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000520 cl->class_type.tt_type = VAR_CLASS;
521 cl->class_type.tt_member = (type_T *)cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000522 cl->class_object_type.tt_type = VAR_OBJECT;
523 cl->class_object_type.tt_member = (type_T *)cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000524 cl->class_type_list = type_list;
525
526 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +0000527 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000528
529 // Add the class to the script-local variables.
530 typval_T tv;
531 tv.v_type = VAR_CLASS;
532 tv.vval.v_class = cl;
533 set_var_const(cl->class_name, current_sctx.sc_sid,
534 NULL, &tv, FALSE, ASSIGN_DECL, 0);
535 return;
536 }
537
538cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +0000539 if (cl != NULL)
540 {
541 vim_free(cl->class_name);
542 vim_free(cl->class_obj_members);
543 vim_free(cl->class_obj_methods);
544 vim_free(cl);
545 }
546
Bram Moolenaard505d172022-12-18 21:42:55 +0000547 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000548 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000549 garray_T *gap = round == 1 ? &classmembers : &objmembers;
550 if (gap->ga_len == 0 || gap->ga_data == NULL)
551 continue;
552
553 for (int i = 0; i < gap->ga_len; ++i)
554 {
555 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
556 vim_free(m->ocm_name);
557 vim_free(m->ocm_init);
558 }
559 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000560 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000561
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000562 for (int i = 0; i < objmethods.ga_len; ++i)
563 {
564 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
565 func_clear_free(uf, FALSE);
566 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000567 ga_clear(&objmethods);
568 clear_type_list(&type_list);
569}
570
571/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000572 * Find member "name" in class "cl", set "member_idx" to the member index and
573 * return its type.
574 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000575 */
576 type_T *
577class_member_type(
578 class_T *cl,
579 char_u *name,
580 char_u *name_end,
581 int *member_idx)
582{
583 *member_idx = -1; // not found (yet)
584 size_t len = name_end - name;
585
586 for (int i = 0; i < cl->class_obj_member_count; ++i)
587 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000588 ocmember_T *m = cl->class_obj_members + i;
589 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000590 {
591 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +0000592 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000593 }
594 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000595
596 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000597 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000598}
599
600/*
601 * Handle ":interface" up to ":endinterface".
602 */
603 void
604ex_interface(exarg_T *eap UNUSED)
605{
606 // TODO
607}
608
609/*
610 * Handle ":enum" up to ":endenum".
611 */
612 void
613ex_enum(exarg_T *eap UNUSED)
614{
615 // TODO
616}
617
618/*
619 * Handle ":type".
620 */
621 void
622ex_type(exarg_T *eap UNUSED)
623{
624 // TODO
625}
626
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000627/*
628 * Evaluate what comes after a class:
629 * - class member: SomeClass.varname
630 * - class method: SomeClass.SomeMethod()
631 * - class constructor: SomeClass.new()
632 * - object member: someObject.varname
633 * - object method: someObject.SomeMethod()
634 *
635 * "*arg" points to the '.'.
636 * "*arg" is advanced to after the member name or method call.
637 *
638 * Returns FAIL or OK.
639 */
640 int
641class_object_index(
642 char_u **arg,
643 typval_T *rettv,
644 evalarg_T *evalarg,
645 int verbose UNUSED) // give error messages
646{
647 // int evaluate = evalarg != NULL
648 // && (evalarg->eval_flags & EVAL_EVALUATE);
649
650 if (VIM_ISWHITE((*arg)[1]))
651 {
652 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
653 return FAIL;
654 }
655
656 ++*arg;
657 char_u *name = *arg;
658 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
659 if (name_end == name)
660 return FAIL;
661 size_t len = name_end - name;
662
663 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
664 : rettv->vval.v_object->obj_class;
665 if (*name_end == '(')
666 {
667 for (int i = 0; i < cl->class_obj_method_count; ++i)
668 {
669 ufunc_T *fp = cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +0000670 // Use a separate pointer to avoid that ASAN complains about
671 // uf_name[] only being 4 characters.
672 char_u *ufname = (char_u *)fp->uf_name;
673 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000674 {
675 typval_T argvars[MAX_FUNC_ARGS + 1];
676 int argcount = 0;
677
678 char_u *argp = name_end;
679 int ret = get_func_arguments(&argp, evalarg, 0,
680 argvars, &argcount);
681 if (ret == FAIL)
682 return FAIL;
683
684 funcexe_T funcexe;
685 CLEAR_FIELD(funcexe);
686 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000687 if (rettv->v_type == VAR_OBJECT)
688 {
689 funcexe.fe_object = rettv->vval.v_object;
690 ++funcexe.fe_object->obj_refcount;
691 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000692
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000693 // Clear the class or object after calling the function, in
694 // case the refcount is one.
695 typval_T tv_tofree = *rettv;
696 rettv->v_type = VAR_UNKNOWN;
697
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000698 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000699 int error = call_user_func_check(fp, argcount, argvars,
700 rettv, &funcexe, NULL);
701
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000702 // Clear the previous rettv and the arguments.
703 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000704 for (int idx = 0; idx < argcount; ++idx)
705 clear_tv(&argvars[idx]);
706
707 if (error != FCERR_NONE)
708 {
709 user_func_error(error, printable_func_name(fp),
710 funcexe.fe_found_var);
711 return FAIL;
712 }
713 *arg = argp;
714 return OK;
715 }
716 }
717
718 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
719 }
720
721 else if (rettv->v_type == VAR_OBJECT)
722 {
723 for (int i = 0; i < cl->class_obj_member_count; ++i)
724 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000725 ocmember_T *m = &cl->class_obj_members[i];
726 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000727 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000728 if (*name == '_')
729 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000730 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000731 return FAIL;
732 }
733
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000734 // The object only contains a pointer to the class, the member
735 // values array follows right after that.
736 object_T *obj = rettv->vval.v_object;
737 typval_T *tv = (typval_T *)(obj + 1) + i;
738 copy_tv(tv, rettv);
739 object_unref(obj);
740
741 *arg = name_end;
742 return OK;
743 }
744 }
745
746 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
747 }
748
Bram Moolenaard505d172022-12-18 21:42:55 +0000749 else if (rettv->v_type == VAR_CLASS)
750 {
751 // class member
752 for (int i = 0; i < cl->class_class_member_count; ++i)
753 {
754 ocmember_T *m = &cl->class_class_members[i];
755 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
756 {
757 if (*name == '_')
758 {
759 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
760 return FAIL;
761 }
762
763 typval_T *tv = &cl->class_members_tv[i];
764 copy_tv(tv, rettv);
765 class_unref(cl);
766
767 *arg = name_end;
768 return OK;
769 }
770 }
771
772 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
773 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000774
775 return FAIL;
776}
777
778/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000779 * If "arg" points to a class or object method, return it.
780 * Otherwise return NULL.
781 */
782 ufunc_T *
783find_class_func(char_u **arg)
784{
785 char_u *name = *arg;
786 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
787 if (name_end == name || *name_end != '.')
788 return NULL;
789
790 size_t len = name_end - name;
791 typval_T tv;
792 tv.v_type = VAR_UNKNOWN;
793 if (eval_variable(name, len, 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
794 return NULL;
795 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +0000796 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000797
798 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
799 : tv.vval.v_object->obj_class;
800 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +0000801 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000802 char_u *fname = name_end + 1;
803 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
804 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +0000805 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000806 len = fname_end - fname;
807
808 for (int i = 0; i < cl->class_obj_method_count; ++i)
809 {
810 ufunc_T *fp = cl->class_obj_methods[i];
811 // Use a separate pointer to avoid that ASAN complains about
812 // uf_name[] only being 4 characters.
813 char_u *ufname = (char_u *)fp->uf_name;
814 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +0000815 {
816 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000817 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +0000818 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000819 }
820
Bram Moolenaareb533502022-12-14 15:06:11 +0000821fail_after_eval:
822 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000823 return NULL;
824}
825
826/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000827 * Make a copy of an object.
828 */
829 void
830copy_object(typval_T *from, typval_T *to)
831{
832 *to = *from;
833 if (to->vval.v_object != NULL)
834 ++to->vval.v_object->obj_refcount;
835}
836
837/*
838 * Free an object.
839 */
840 static void
841object_clear(object_T *obj)
842{
843 class_T *cl = obj->obj_class;
844
845 // the member values are just after the object structure
846 typval_T *tv = (typval_T *)(obj + 1);
847 for (int i = 0; i < cl->class_obj_member_count; ++i)
848 clear_tv(tv + i);
849
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000850 // Remove from the list headed by "first_object".
851 object_cleared(obj);
852
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000853 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000854 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000855}
856
857/*
858 * Unreference an object.
859 */
860 void
861object_unref(object_T *obj)
862{
863 if (obj != NULL && --obj->obj_refcount <= 0)
864 object_clear(obj);
865}
866
867/*
868 * Make a copy of a class.
869 */
870 void
871copy_class(typval_T *from, typval_T *to)
872{
873 *to = *from;
874 if (to->vval.v_class != NULL)
875 ++to->vval.v_class->class_refcount;
876}
877
878/*
879 * Unreference a class. Free it when the reference count goes down to zero.
880 */
881 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000882class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000883{
Bram Moolenaard505d172022-12-18 21:42:55 +0000884 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000885 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000886 // Freeing what the class contains may recursively come back here.
887 // Clear "class_name" first, if it is NULL the class does not need to
888 // be freed.
889 VIM_CLEAR(cl->class_name);
890
891 for (int i = 0; i < cl->class_class_member_count; ++i)
892 {
893 ocmember_T *m = &cl->class_class_members[i];
894 vim_free(m->ocm_name);
895 vim_free(m->ocm_init);
896 if (cl->class_members_tv != NULL)
897 clear_tv(&cl->class_members_tv[i]);
898 }
899 vim_free(cl->class_class_members);
900 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000901
902 for (int i = 0; i < cl->class_obj_member_count; ++i)
903 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000904 ocmember_T *m = &cl->class_obj_members[i];
905 vim_free(m->ocm_name);
906 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000907 }
908 vim_free(cl->class_obj_members);
909
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000910 for (int i = 0; i < cl->class_obj_method_count; ++i)
911 {
912 ufunc_T *uf = cl->class_obj_methods[i];
913 func_clear_free(uf, FALSE);
914 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000915 vim_free(cl->class_obj_methods);
916
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000917 clear_type_list(&cl->class_type_list);
918
919 vim_free(cl);
920 }
921}
922
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000923static object_T *first_object = NULL;
924
925/*
926 * Call this function when an object has been created. It will be added to the
927 * list headed by "first_object".
928 */
929 void
930object_created(object_T *obj)
931{
932 if (first_object != NULL)
933 {
934 obj->obj_next_used = first_object;
935 first_object->obj_prev_used = obj;
936 }
937 first_object = obj;
938}
939
940/*
941 * Call this function when an object has been cleared and is about to be freed.
942 * It is removed from the list headed by "first_object".
943 */
944 void
945object_cleared(object_T *obj)
946{
947 if (obj->obj_next_used != NULL)
948 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
949 if (obj->obj_prev_used != NULL)
950 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
951 else if (first_object == obj)
952 first_object = obj->obj_next_used;
953}
954
955/*
956 * Go through the list of all objects and free items without "copyID".
957 */
958 int
959object_free_nonref(int copyID)
960{
961 int did_free = FALSE;
962 object_T *next_obj;
963
964 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
965 {
966 next_obj = obj->obj_next_used;
967 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
968 {
969 // Free the object and items it contains.
970 object_clear(obj);
971 did_free = TRUE;
972 }
973 }
974
975 return did_free;
976}
977
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000978
979#endif // FEAT_EVAL