blob: a91a4a5d5c92dfcb998c054d81280765f093d80e [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 Moolenaar00b28d62022-12-08 15:32:33 +0000251 char_u *p = line;
252 if (checkforcmd(&p, "endclass", 4))
253 {
254 if (STRNCMP(line, "endclass", 8) != 0)
255 semsg(_(e_command_cannot_be_shortened_str), line);
256 else if (*p == '|' || !ends_excmd2(line, p))
257 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +0000258 else
259 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000260 break;
261 }
262
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000263 int has_public = FALSE;
264 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000265 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000266 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000267 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000268 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000269 break;
270 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000271 has_public = TRUE;
272 p = skipwhite(line + 6);
273
Bram Moolenaard505d172022-12-18 21:42:55 +0000274 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000275 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000276 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000277 break;
278 }
279 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000280
281 // object members (public, read access, private):
282 // "this._varname"
283 // "this.varname"
284 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000285 if (STRNCMP(p, "this", 4) == 0)
286 {
287 if (p[4] != '.' || !eval_isnamec1(p[5]))
288 {
289 semsg(_(e_invalid_object_member_declaration_str), p);
290 break;
291 }
292 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +0000293 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +0000294 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +0000295 char_u *init_expr = NULL;
296 if (parse_member(eap, line, varname, has_public,
297 &varname_end, &type_list, &type, &init_expr) == FAIL)
298 break;
299 if (add_member(&objmembers, varname, varname_end,
300 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000301 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000302 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000303 break;
304 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000305 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000306
Bram Moolenaard505d172022-12-18 21:42:55 +0000307 // class members and methods
308 else if (checkforcmd(&p, "static", 6))
309 {
310 p = skipwhite(p);
311 if (checkforcmd(&p, "def", 3))
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000312 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000313 // TODO: class method
314 // static def someMethod()
315 // enddef
316 // static def <Tval> someMethod()
317 // enddef
318 }
319 else
320 {
321 // class members (public, read access, private):
322 // "static _varname"
323 // "static varname"
324 // "public static varname"
325 char_u *varname = p;
326 char_u *varname_end = NULL;
327 type_T *type = NULL;
328 char_u *init_expr = NULL;
329 if (parse_member(eap, line, varname, has_public,
330 &varname_end, &type_list, &type, &init_expr) == FAIL)
331 break;
332 if (add_member(&classmembers, varname, varname_end,
333 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000334 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000335 vim_free(init_expr);
Bram Moolenaar74e12742022-12-13 21:14:28 +0000336 break;
337 }
Bram Moolenaar74e12742022-12-13 21:14:28 +0000338 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000339 }
340
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000341 // constructors:
342 // def new()
343 // enddef
344 // def newOther()
345 // enddef
346 // methods:
347 // def someMethod()
348 // enddef
349 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000350 // def <Tval> someMethod()
351 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000352 else if (checkforcmd(&p, "def", 3))
353 {
354 exarg_T ea;
355 garray_T lines_to_free;
356
357 CLEAR_FIELD(ea);
358 ea.cmd = line;
359 ea.arg = p;
360 ea.cmdidx = CMD_def;
361 ea.getline = eap->getline;
362 ea.cookie = eap->cookie;
363
364 ga_init2(&lines_to_free, sizeof(char_u *), 50);
365 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free, TRUE);
366 ga_clear_strings(&lines_to_free);
367
368 // TODO: how about errors?
369 if (uf != NULL && ga_grow(&objmethods, 1) == OK)
370 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000371 if (STRNCMP(uf->uf_name, "new", 3) == 0)
372 uf->uf_flags |= FC_NEW;
373
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000374 ((ufunc_T **)objmethods.ga_data)[objmethods.ga_len] = uf;
375 ++objmethods.ga_len;
376 }
377 }
378
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000379 else
380 {
381 semsg(_(e_not_valid_command_in_class_str), line);
382 break;
383 }
384 }
385 vim_free(theline);
386
Bram Moolenaareb533502022-12-14 15:06:11 +0000387 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000388 if (success)
389 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000390 // "endclass" encountered without failures: Create the class.
391
Bram Moolenaareb533502022-12-14 15:06:11 +0000392 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000393 if (cl == NULL)
394 goto cleanup;
395 cl->class_refcount = 1;
396 cl->class_name = vim_strnsave(arg, name_end - arg);
Bram Moolenaard505d172022-12-18 21:42:55 +0000397 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000398 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000399
400 // Add class and object members to "cl".
401 if (add_members_to_class(&classmembers,
402 &cl->class_class_members,
403 &cl->class_class_member_count) == FAIL
404 || add_members_to_class(&objmembers,
405 &cl->class_obj_members,
406 &cl->class_obj_member_count) == FAIL)
407 goto cleanup;
408
409 if (cl->class_class_member_count > 0)
410 {
411 // Allocate a typval for each class member and initialize it.
412 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
413 cl->class_class_member_count);
414 if (cl->class_members_tv != NULL)
415 for (int i = 0; i < cl->class_class_member_count; ++i)
416 {
417 ocmember_T *m = &cl->class_class_members[i];
418 typval_T *tv = &cl->class_members_tv[i];
419 if (m->ocm_init != NULL)
420 {
421 typval_T *etv = eval_expr(m->ocm_init, eap);
422 if (etv != NULL)
423 {
424 *tv = *etv;
425 vim_free(etv);
426 }
427 }
428 else
429 {
430 // TODO: proper default value
431 tv->v_type = m->ocm_type->tt_type;
432 tv->vval.v_string = NULL;
433 }
434 }
435 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000436
437 int have_new = FALSE;
438 for (int i = 0; i < objmethods.ga_len; ++i)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000439 if (STRCMP(((ufunc_T **)objmethods.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000440 "new") == 0)
441 {
442 have_new = TRUE;
443 break;
444 }
445 if (!have_new)
446 {
447 // No new() method was defined, add the default constructor.
448 garray_T fga;
449 ga_init2(&fga, 1, 1000);
450 ga_concat(&fga, (char_u *)"new(");
451 for (int i = 0; i < cl->class_obj_member_count; ++i)
452 {
453 if (i > 0)
454 ga_concat(&fga, (char_u *)", ");
455 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000456 ocmember_T *m = cl->class_obj_members + i;
457 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000458 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000459 }
460 ga_concat(&fga, (char_u *)")\nenddef\n");
461 ga_append(&fga, NUL);
462
463 exarg_T fea;
464 CLEAR_FIELD(fea);
465 fea.cmdidx = CMD_def;
466 fea.cmd = fea.arg = fga.ga_data;
467
468 garray_T lines_to_free;
469 ga_init2(&lines_to_free, sizeof(char_u *), 50);
470
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000471 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, TRUE);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000472
473 ga_clear_strings(&lines_to_free);
474 vim_free(fga.ga_data);
475
476 if (nf != NULL && ga_grow(&objmethods, 1) == OK)
477 {
478 ((ufunc_T **)objmethods.ga_data)[objmethods.ga_len] = nf;
479 ++objmethods.ga_len;
480
481 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000482 nf->uf_ret_type = get_type_ptr(&type_list);
483 if (nf->uf_ret_type != NULL)
484 {
485 nf->uf_ret_type->tt_type = VAR_OBJECT;
486 nf->uf_ret_type->tt_member = (type_T *)cl;
487 nf->uf_ret_type->tt_argcount = 0;
488 nf->uf_ret_type->tt_args = NULL;
489 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000490 }
491 }
492
Bram Moolenaard505d172022-12-18 21:42:55 +0000493 // TODO: class methods
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000494 cl->class_obj_method_count = objmethods.ga_len;
495 cl->class_obj_methods = ALLOC_MULT(ufunc_T *, objmethods.ga_len);
496 if (cl->class_obj_methods == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000497 goto cleanup;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000498 mch_memmove(cl->class_obj_methods, objmethods.ga_data,
499 sizeof(ufunc_T *) * objmethods.ga_len);
500 vim_free(objmethods.ga_data);
501
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000502 // Set the class pointer on all the object methods.
503 for (int i = 0; i < objmethods.ga_len; ++i)
504 {
505 ufunc_T *fp = cl->class_obj_methods[i];
506 fp->uf_class = cl;
507 fp->uf_flags |= FC_OBJECT; // TODO: not for class method
508 }
509
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000510 cl->class_type.tt_type = VAR_CLASS;
511 cl->class_type.tt_member = (type_T *)cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000512 cl->class_object_type.tt_type = VAR_OBJECT;
513 cl->class_object_type.tt_member = (type_T *)cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000514 cl->class_type_list = type_list;
515
516 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +0000517 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000518
519 // Add the class to the script-local variables.
520 typval_T tv;
521 tv.v_type = VAR_CLASS;
522 tv.vval.v_class = cl;
523 set_var_const(cl->class_name, current_sctx.sc_sid,
524 NULL, &tv, FALSE, ASSIGN_DECL, 0);
525 return;
526 }
527
528cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +0000529 if (cl != NULL)
530 {
531 vim_free(cl->class_name);
532 vim_free(cl->class_obj_members);
533 vim_free(cl->class_obj_methods);
534 vim_free(cl);
535 }
536
Bram Moolenaard505d172022-12-18 21:42:55 +0000537 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000538 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000539 garray_T *gap = round == 1 ? &classmembers : &objmembers;
540 if (gap->ga_len == 0 || gap->ga_data == NULL)
541 continue;
542
543 for (int i = 0; i < gap->ga_len; ++i)
544 {
545 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
546 vim_free(m->ocm_name);
547 vim_free(m->ocm_init);
548 }
549 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000550 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000551
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000552 for (int i = 0; i < objmethods.ga_len; ++i)
553 {
554 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
555 func_clear_free(uf, FALSE);
556 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000557 ga_clear(&objmethods);
558 clear_type_list(&type_list);
559}
560
561/*
562 * Find member "name" in class "cl" and return its type.
563 * When not found t_any is returned.
564 */
565 type_T *
566class_member_type(
567 class_T *cl,
568 char_u *name,
569 char_u *name_end,
570 int *member_idx)
571{
572 *member_idx = -1; // not found (yet)
573 size_t len = name_end - name;
574
575 for (int i = 0; i < cl->class_obj_member_count; ++i)
576 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000577 ocmember_T *m = cl->class_obj_members + i;
578 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000579 {
580 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +0000581 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000582 }
583 }
584 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000585}
586
587/*
588 * Handle ":interface" up to ":endinterface".
589 */
590 void
591ex_interface(exarg_T *eap UNUSED)
592{
593 // TODO
594}
595
596/*
597 * Handle ":enum" up to ":endenum".
598 */
599 void
600ex_enum(exarg_T *eap UNUSED)
601{
602 // TODO
603}
604
605/*
606 * Handle ":type".
607 */
608 void
609ex_type(exarg_T *eap UNUSED)
610{
611 // TODO
612}
613
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000614/*
615 * Evaluate what comes after a class:
616 * - class member: SomeClass.varname
617 * - class method: SomeClass.SomeMethod()
618 * - class constructor: SomeClass.new()
619 * - object member: someObject.varname
620 * - object method: someObject.SomeMethod()
621 *
622 * "*arg" points to the '.'.
623 * "*arg" is advanced to after the member name or method call.
624 *
625 * Returns FAIL or OK.
626 */
627 int
628class_object_index(
629 char_u **arg,
630 typval_T *rettv,
631 evalarg_T *evalarg,
632 int verbose UNUSED) // give error messages
633{
634 // int evaluate = evalarg != NULL
635 // && (evalarg->eval_flags & EVAL_EVALUATE);
636
637 if (VIM_ISWHITE((*arg)[1]))
638 {
639 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
640 return FAIL;
641 }
642
643 ++*arg;
644 char_u *name = *arg;
645 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
646 if (name_end == name)
647 return FAIL;
648 size_t len = name_end - name;
649
650 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
651 : rettv->vval.v_object->obj_class;
652 if (*name_end == '(')
653 {
654 for (int i = 0; i < cl->class_obj_method_count; ++i)
655 {
656 ufunc_T *fp = cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +0000657 // Use a separate pointer to avoid that ASAN complains about
658 // uf_name[] only being 4 characters.
659 char_u *ufname = (char_u *)fp->uf_name;
660 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000661 {
662 typval_T argvars[MAX_FUNC_ARGS + 1];
663 int argcount = 0;
664
665 char_u *argp = name_end;
666 int ret = get_func_arguments(&argp, evalarg, 0,
667 argvars, &argcount);
668 if (ret == FAIL)
669 return FAIL;
670
671 funcexe_T funcexe;
672 CLEAR_FIELD(funcexe);
673 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000674 if (rettv->v_type == VAR_OBJECT)
675 {
676 funcexe.fe_object = rettv->vval.v_object;
677 ++funcexe.fe_object->obj_refcount;
678 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000679
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000680 // Clear the class or object after calling the function, in
681 // case the refcount is one.
682 typval_T tv_tofree = *rettv;
683 rettv->v_type = VAR_UNKNOWN;
684
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000685 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000686 int error = call_user_func_check(fp, argcount, argvars,
687 rettv, &funcexe, NULL);
688
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000689 // Clear the previous rettv and the arguments.
690 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000691 for (int idx = 0; idx < argcount; ++idx)
692 clear_tv(&argvars[idx]);
693
694 if (error != FCERR_NONE)
695 {
696 user_func_error(error, printable_func_name(fp),
697 funcexe.fe_found_var);
698 return FAIL;
699 }
700 *arg = argp;
701 return OK;
702 }
703 }
704
705 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
706 }
707
708 else if (rettv->v_type == VAR_OBJECT)
709 {
710 for (int i = 0; i < cl->class_obj_member_count; ++i)
711 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000712 ocmember_T *m = &cl->class_obj_members[i];
713 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000714 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000715 if (*name == '_')
716 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000717 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000718 return FAIL;
719 }
720
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000721 // The object only contains a pointer to the class, the member
722 // values array follows right after that.
723 object_T *obj = rettv->vval.v_object;
724 typval_T *tv = (typval_T *)(obj + 1) + i;
725 copy_tv(tv, rettv);
726 object_unref(obj);
727
728 *arg = name_end;
729 return OK;
730 }
731 }
732
733 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
734 }
735
Bram Moolenaard505d172022-12-18 21:42:55 +0000736 else if (rettv->v_type == VAR_CLASS)
737 {
738 // class member
739 for (int i = 0; i < cl->class_class_member_count; ++i)
740 {
741 ocmember_T *m = &cl->class_class_members[i];
742 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
743 {
744 if (*name == '_')
745 {
746 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
747 return FAIL;
748 }
749
750 typval_T *tv = &cl->class_members_tv[i];
751 copy_tv(tv, rettv);
752 class_unref(cl);
753
754 *arg = name_end;
755 return OK;
756 }
757 }
758
759 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
760 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000761
762 return FAIL;
763}
764
765/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000766 * If "arg" points to a class or object method, return it.
767 * Otherwise return NULL.
768 */
769 ufunc_T *
770find_class_func(char_u **arg)
771{
772 char_u *name = *arg;
773 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
774 if (name_end == name || *name_end != '.')
775 return NULL;
776
777 size_t len = name_end - name;
778 typval_T tv;
779 tv.v_type = VAR_UNKNOWN;
780 if (eval_variable(name, len, 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
781 return NULL;
782 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +0000783 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000784
785 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
786 : tv.vval.v_object->obj_class;
787 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +0000788 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000789 char_u *fname = name_end + 1;
790 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
791 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +0000792 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000793 len = fname_end - fname;
794
795 for (int i = 0; i < cl->class_obj_method_count; ++i)
796 {
797 ufunc_T *fp = cl->class_obj_methods[i];
798 // Use a separate pointer to avoid that ASAN complains about
799 // uf_name[] only being 4 characters.
800 char_u *ufname = (char_u *)fp->uf_name;
801 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +0000802 {
803 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000804 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +0000805 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000806 }
807
Bram Moolenaareb533502022-12-14 15:06:11 +0000808fail_after_eval:
809 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000810 return NULL;
811}
812
813/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000814 * Make a copy of an object.
815 */
816 void
817copy_object(typval_T *from, typval_T *to)
818{
819 *to = *from;
820 if (to->vval.v_object != NULL)
821 ++to->vval.v_object->obj_refcount;
822}
823
824/*
825 * Free an object.
826 */
827 static void
828object_clear(object_T *obj)
829{
830 class_T *cl = obj->obj_class;
831
832 // the member values are just after the object structure
833 typval_T *tv = (typval_T *)(obj + 1);
834 for (int i = 0; i < cl->class_obj_member_count; ++i)
835 clear_tv(tv + i);
836
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000837 // Remove from the list headed by "first_object".
838 object_cleared(obj);
839
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000840 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000841 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000842}
843
844/*
845 * Unreference an object.
846 */
847 void
848object_unref(object_T *obj)
849{
850 if (obj != NULL && --obj->obj_refcount <= 0)
851 object_clear(obj);
852}
853
854/*
855 * Make a copy of a class.
856 */
857 void
858copy_class(typval_T *from, typval_T *to)
859{
860 *to = *from;
861 if (to->vval.v_class != NULL)
862 ++to->vval.v_class->class_refcount;
863}
864
865/*
866 * Unreference a class. Free it when the reference count goes down to zero.
867 */
868 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000869class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000870{
Bram Moolenaard505d172022-12-18 21:42:55 +0000871 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000872 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000873 // Freeing what the class contains may recursively come back here.
874 // Clear "class_name" first, if it is NULL the class does not need to
875 // be freed.
876 VIM_CLEAR(cl->class_name);
877
878 for (int i = 0; i < cl->class_class_member_count; ++i)
879 {
880 ocmember_T *m = &cl->class_class_members[i];
881 vim_free(m->ocm_name);
882 vim_free(m->ocm_init);
883 if (cl->class_members_tv != NULL)
884 clear_tv(&cl->class_members_tv[i]);
885 }
886 vim_free(cl->class_class_members);
887 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000888
889 for (int i = 0; i < cl->class_obj_member_count; ++i)
890 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000891 ocmember_T *m = &cl->class_obj_members[i];
892 vim_free(m->ocm_name);
893 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000894 }
895 vim_free(cl->class_obj_members);
896
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000897 for (int i = 0; i < cl->class_obj_method_count; ++i)
898 {
899 ufunc_T *uf = cl->class_obj_methods[i];
900 func_clear_free(uf, FALSE);
901 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000902 vim_free(cl->class_obj_methods);
903
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000904 clear_type_list(&cl->class_type_list);
905
906 vim_free(cl);
907 }
908}
909
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000910static object_T *first_object = NULL;
911
912/*
913 * Call this function when an object has been created. It will be added to the
914 * list headed by "first_object".
915 */
916 void
917object_created(object_T *obj)
918{
919 if (first_object != NULL)
920 {
921 obj->obj_next_used = first_object;
922 first_object->obj_prev_used = obj;
923 }
924 first_object = obj;
925}
926
927/*
928 * Call this function when an object has been cleared and is about to be freed.
929 * It is removed from the list headed by "first_object".
930 */
931 void
932object_cleared(object_T *obj)
933{
934 if (obj->obj_next_used != NULL)
935 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
936 if (obj->obj_prev_used != NULL)
937 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
938 else if (first_object == obj)
939 first_object = obj->obj_next_used;
940}
941
942/*
943 * Go through the list of all objects and free items without "copyID".
944 */
945 int
946object_free_nonref(int copyID)
947{
948 int did_free = FALSE;
949 object_T *next_obj;
950
951 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
952 {
953 next_obj = obj->obj_next_used;
954 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
955 {
956 // Free the object and items it contains.
957 object_clear(obj);
958 did_free = TRUE;
959 }
960 }
961
962 return did_free;
963}
964
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000965
966#endif // FEAT_EVAL