blob: 4a975858190a5d06f9167735565750d78fb38c96 [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;
167 mch_memmove(*members, gap->ga_data, sizeof(ocmember_T) * gap->ga_len);
168 VIM_CLEAR(gap->ga_data);
169 return OK;
170}
171
172/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000173 * Handle ":class" and ":abstract class" up to ":endclass".
174 */
175 void
176ex_class(exarg_T *eap)
177{
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000178 if (!current_script_is_vim9()
179 || (cmdmod.cmod_flags & CMOD_LEGACY)
180 || !getline_equal(eap->getline, eap->cookie, getsourceline))
181 {
182 emsg(_(e_class_can_only_be_defined_in_vim9_script));
183 return;
184 }
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000185
186 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000187 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000188 if (is_abstract)
189 {
190 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
191 {
192 semsg(_(e_invalid_argument_str), arg);
193 return;
194 }
195 arg = skipwhite(arg + 5);
196 }
197
198 if (!ASCII_ISUPPER(*arg))
199 {
200 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
201 return;
202 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000203 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
204 if (!IS_WHITE_OR_NUL(*name_end))
205 {
206 semsg(_(e_white_space_required_after_class_name_str), arg);
207 return;
208 }
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000209
210 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000211 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000212 // extends SomeClass
213 // implements SomeInterface
214 // specifies SomeInterface
Bram Moolenaard505d172022-12-18 21:42:55 +0000215 // check that nothing follows
216 // handle "is_export" if it is set
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000217
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000218 garray_T type_list; // list of pointers to allocated types
219 ga_init2(&type_list, sizeof(type_T *), 10);
220
Bram Moolenaard505d172022-12-18 21:42:55 +0000221 // Growarray with class members declared in the class.
222 garray_T classmembers;
223 ga_init2(&classmembers, sizeof(ocmember_T), 10);
224
225 // Growarray with object methods declared in the class.
226 garray_T classmethods;
227 ga_init2(&classmethods, sizeof(ufunc_T *), 10);
228
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000229 // Growarray with object members declared in the class.
230 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +0000231 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000232
233 // Growarray with object methods declared in the class.
234 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000235 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000236
237 /*
238 * Go over the body of the class until "endclass" is found.
239 */
240 char_u *theline = NULL;
241 int success = FALSE;
242 for (;;)
243 {
244 vim_free(theline);
245 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
246 if (theline == NULL)
247 break;
248 char_u *line = skipwhite(theline);
249
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000250 char_u *p = line;
251 if (checkforcmd(&p, "endclass", 4))
252 {
253 if (STRNCMP(line, "endclass", 8) != 0)
254 semsg(_(e_command_cannot_be_shortened_str), line);
255 else if (*p == '|' || !ends_excmd2(line, p))
256 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +0000257 else
258 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000259 break;
260 }
261
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000262 int has_public = FALSE;
263 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000264 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000265 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000266 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000267 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000268 break;
269 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000270 has_public = TRUE;
271 p = skipwhite(line + 6);
272
Bram Moolenaard505d172022-12-18 21:42:55 +0000273 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000274 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000275 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000276 break;
277 }
278 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000279
280 // object members (public, read access, private):
281 // "this._varname"
282 // "this.varname"
283 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000284 if (STRNCMP(p, "this", 4) == 0)
285 {
286 if (p[4] != '.' || !eval_isnamec1(p[5]))
287 {
288 semsg(_(e_invalid_object_member_declaration_str), p);
289 break;
290 }
291 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +0000292 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +0000293 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +0000294 char_u *init_expr = NULL;
295 if (parse_member(eap, line, varname, has_public,
296 &varname_end, &type_list, &type, &init_expr) == FAIL)
297 break;
298 if (add_member(&objmembers, varname, varname_end,
299 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000300 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000301 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000302 break;
303 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000304 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000305
Bram Moolenaard505d172022-12-18 21:42:55 +0000306 // class members and methods
307 else if (checkforcmd(&p, "static", 6))
308 {
309 p = skipwhite(p);
310 if (checkforcmd(&p, "def", 3))
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000311 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000312 // TODO: class method
313 // static def someMethod()
314 // enddef
315 // static def <Tval> someMethod()
316 // enddef
317 }
318 else
319 {
320 // class members (public, read access, private):
321 // "static _varname"
322 // "static varname"
323 // "public static varname"
324 char_u *varname = p;
325 char_u *varname_end = NULL;
326 type_T *type = NULL;
327 char_u *init_expr = NULL;
328 if (parse_member(eap, line, varname, has_public,
329 &varname_end, &type_list, &type, &init_expr) == FAIL)
330 break;
331 if (add_member(&classmembers, varname, varname_end,
332 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000333 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000334 vim_free(init_expr);
Bram Moolenaar74e12742022-12-13 21:14:28 +0000335 break;
336 }
Bram Moolenaar74e12742022-12-13 21:14:28 +0000337 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000338 }
339
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000340 // constructors:
341 // def new()
342 // enddef
343 // def newOther()
344 // enddef
345 // methods:
346 // def someMethod()
347 // enddef
348 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000349 // def <Tval> someMethod()
350 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000351 else if (checkforcmd(&p, "def", 3))
352 {
353 exarg_T ea;
354 garray_T lines_to_free;
355
356 CLEAR_FIELD(ea);
357 ea.cmd = line;
358 ea.arg = p;
359 ea.cmdidx = CMD_def;
360 ea.getline = eap->getline;
361 ea.cookie = eap->cookie;
362
363 ga_init2(&lines_to_free, sizeof(char_u *), 50);
364 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free, TRUE);
365 ga_clear_strings(&lines_to_free);
366
367 // TODO: how about errors?
368 if (uf != NULL && ga_grow(&objmethods, 1) == OK)
369 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000370 if (STRNCMP(uf->uf_name, "new", 3) == 0)
371 uf->uf_flags |= FC_NEW;
372
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000373 ((ufunc_T **)objmethods.ga_data)[objmethods.ga_len] = uf;
374 ++objmethods.ga_len;
375 }
376 }
377
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000378 else
379 {
380 semsg(_(e_not_valid_command_in_class_str), line);
381 break;
382 }
383 }
384 vim_free(theline);
385
Bram Moolenaareb533502022-12-14 15:06:11 +0000386 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000387 if (success)
388 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000389 // "endclass" encountered without failures: Create the class.
390
Bram Moolenaareb533502022-12-14 15:06:11 +0000391 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000392 if (cl == NULL)
393 goto cleanup;
394 cl->class_refcount = 1;
395 cl->class_name = vim_strnsave(arg, name_end - arg);
Bram Moolenaard505d172022-12-18 21:42:55 +0000396 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000397 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000398
399 // Add class and object members to "cl".
400 if (add_members_to_class(&classmembers,
401 &cl->class_class_members,
402 &cl->class_class_member_count) == FAIL
403 || add_members_to_class(&objmembers,
404 &cl->class_obj_members,
405 &cl->class_obj_member_count) == FAIL)
406 goto cleanup;
407
408 if (cl->class_class_member_count > 0)
409 {
410 // Allocate a typval for each class member and initialize it.
411 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
412 cl->class_class_member_count);
413 if (cl->class_members_tv != NULL)
414 for (int i = 0; i < cl->class_class_member_count; ++i)
415 {
416 ocmember_T *m = &cl->class_class_members[i];
417 typval_T *tv = &cl->class_members_tv[i];
418 if (m->ocm_init != NULL)
419 {
420 typval_T *etv = eval_expr(m->ocm_init, eap);
421 if (etv != NULL)
422 {
423 *tv = *etv;
424 vim_free(etv);
425 }
426 }
427 else
428 {
429 // TODO: proper default value
430 tv->v_type = m->ocm_type->tt_type;
431 tv->vval.v_string = NULL;
432 }
433 }
434 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000435
436 int have_new = FALSE;
437 for (int i = 0; i < objmethods.ga_len; ++i)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000438 if (STRCMP(((ufunc_T **)objmethods.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000439 "new") == 0)
440 {
441 have_new = TRUE;
442 break;
443 }
444 if (!have_new)
445 {
446 // No new() method was defined, add the default constructor.
447 garray_T fga;
448 ga_init2(&fga, 1, 1000);
449 ga_concat(&fga, (char_u *)"new(");
450 for (int i = 0; i < cl->class_obj_member_count; ++i)
451 {
452 if (i > 0)
453 ga_concat(&fga, (char_u *)", ");
454 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000455 ocmember_T *m = cl->class_obj_members + i;
456 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000457 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000458 }
459 ga_concat(&fga, (char_u *)")\nenddef\n");
460 ga_append(&fga, NUL);
461
462 exarg_T fea;
463 CLEAR_FIELD(fea);
464 fea.cmdidx = CMD_def;
465 fea.cmd = fea.arg = fga.ga_data;
466
467 garray_T lines_to_free;
468 ga_init2(&lines_to_free, sizeof(char_u *), 50);
469
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000470 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, TRUE);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000471
472 ga_clear_strings(&lines_to_free);
473 vim_free(fga.ga_data);
474
475 if (nf != NULL && ga_grow(&objmethods, 1) == OK)
476 {
477 ((ufunc_T **)objmethods.ga_data)[objmethods.ga_len] = nf;
478 ++objmethods.ga_len;
479
480 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000481 nf->uf_ret_type = get_type_ptr(&type_list);
482 if (nf->uf_ret_type != NULL)
483 {
484 nf->uf_ret_type->tt_type = VAR_OBJECT;
485 nf->uf_ret_type->tt_member = (type_T *)cl;
486 nf->uf_ret_type->tt_argcount = 0;
487 nf->uf_ret_type->tt_args = NULL;
488 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000489 }
490 }
491
Bram Moolenaard505d172022-12-18 21:42:55 +0000492 // TODO: class methods
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000493 cl->class_obj_method_count = objmethods.ga_len;
494 cl->class_obj_methods = ALLOC_MULT(ufunc_T *, objmethods.ga_len);
495 if (cl->class_obj_methods == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000496 goto cleanup;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000497 mch_memmove(cl->class_obj_methods, objmethods.ga_data,
498 sizeof(ufunc_T *) * objmethods.ga_len);
499 vim_free(objmethods.ga_data);
500
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000501 // Set the class pointer on all the object methods.
502 for (int i = 0; i < objmethods.ga_len; ++i)
503 {
504 ufunc_T *fp = cl->class_obj_methods[i];
505 fp->uf_class = cl;
506 fp->uf_flags |= FC_OBJECT; // TODO: not for class method
507 }
508
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000509 cl->class_type.tt_type = VAR_CLASS;
510 cl->class_type.tt_member = (type_T *)cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000511 cl->class_object_type.tt_type = VAR_OBJECT;
512 cl->class_object_type.tt_member = (type_T *)cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000513 cl->class_type_list = type_list;
514
515 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +0000516 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000517
518 // Add the class to the script-local variables.
519 typval_T tv;
520 tv.v_type = VAR_CLASS;
521 tv.vval.v_class = cl;
522 set_var_const(cl->class_name, current_sctx.sc_sid,
523 NULL, &tv, FALSE, ASSIGN_DECL, 0);
524 return;
525 }
526
527cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +0000528 if (cl != NULL)
529 {
530 vim_free(cl->class_name);
531 vim_free(cl->class_obj_members);
532 vim_free(cl->class_obj_methods);
533 vim_free(cl);
534 }
535
Bram Moolenaard505d172022-12-18 21:42:55 +0000536 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000537 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000538 garray_T *gap = round == 1 ? &classmembers : &objmembers;
539 if (gap->ga_len == 0 || gap->ga_data == NULL)
540 continue;
541
542 for (int i = 0; i < gap->ga_len; ++i)
543 {
544 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
545 vim_free(m->ocm_name);
546 vim_free(m->ocm_init);
547 }
548 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000549 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000550
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000551 for (int i = 0; i < objmethods.ga_len; ++i)
552 {
553 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
554 func_clear_free(uf, FALSE);
555 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000556 ga_clear(&objmethods);
557 clear_type_list(&type_list);
558}
559
560/*
561 * Find member "name" in class "cl" and return its type.
562 * When not found t_any is returned.
563 */
564 type_T *
565class_member_type(
566 class_T *cl,
567 char_u *name,
568 char_u *name_end,
569 int *member_idx)
570{
571 *member_idx = -1; // not found (yet)
572 size_t len = name_end - name;
573
574 for (int i = 0; i < cl->class_obj_member_count; ++i)
575 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000576 ocmember_T *m = cl->class_obj_members + i;
577 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000578 {
579 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +0000580 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000581 }
582 }
583 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000584}
585
586/*
587 * Handle ":interface" up to ":endinterface".
588 */
589 void
590ex_interface(exarg_T *eap UNUSED)
591{
592 // TODO
593}
594
595/*
596 * Handle ":enum" up to ":endenum".
597 */
598 void
599ex_enum(exarg_T *eap UNUSED)
600{
601 // TODO
602}
603
604/*
605 * Handle ":type".
606 */
607 void
608ex_type(exarg_T *eap UNUSED)
609{
610 // TODO
611}
612
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000613/*
614 * Evaluate what comes after a class:
615 * - class member: SomeClass.varname
616 * - class method: SomeClass.SomeMethod()
617 * - class constructor: SomeClass.new()
618 * - object member: someObject.varname
619 * - object method: someObject.SomeMethod()
620 *
621 * "*arg" points to the '.'.
622 * "*arg" is advanced to after the member name or method call.
623 *
624 * Returns FAIL or OK.
625 */
626 int
627class_object_index(
628 char_u **arg,
629 typval_T *rettv,
630 evalarg_T *evalarg,
631 int verbose UNUSED) // give error messages
632{
633 // int evaluate = evalarg != NULL
634 // && (evalarg->eval_flags & EVAL_EVALUATE);
635
636 if (VIM_ISWHITE((*arg)[1]))
637 {
638 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
639 return FAIL;
640 }
641
642 ++*arg;
643 char_u *name = *arg;
644 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
645 if (name_end == name)
646 return FAIL;
647 size_t len = name_end - name;
648
649 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
650 : rettv->vval.v_object->obj_class;
651 if (*name_end == '(')
652 {
653 for (int i = 0; i < cl->class_obj_method_count; ++i)
654 {
655 ufunc_T *fp = cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +0000656 // Use a separate pointer to avoid that ASAN complains about
657 // uf_name[] only being 4 characters.
658 char_u *ufname = (char_u *)fp->uf_name;
659 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000660 {
661 typval_T argvars[MAX_FUNC_ARGS + 1];
662 int argcount = 0;
663
664 char_u *argp = name_end;
665 int ret = get_func_arguments(&argp, evalarg, 0,
666 argvars, &argcount);
667 if (ret == FAIL)
668 return FAIL;
669
670 funcexe_T funcexe;
671 CLEAR_FIELD(funcexe);
672 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000673 if (rettv->v_type == VAR_OBJECT)
674 {
675 funcexe.fe_object = rettv->vval.v_object;
676 ++funcexe.fe_object->obj_refcount;
677 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000678
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000679 // Clear the class or object after calling the function, in
680 // case the refcount is one.
681 typval_T tv_tofree = *rettv;
682 rettv->v_type = VAR_UNKNOWN;
683
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000684 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000685 int error = call_user_func_check(fp, argcount, argvars,
686 rettv, &funcexe, NULL);
687
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000688 // Clear the previous rettv and the arguments.
689 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000690 for (int idx = 0; idx < argcount; ++idx)
691 clear_tv(&argvars[idx]);
692
693 if (error != FCERR_NONE)
694 {
695 user_func_error(error, printable_func_name(fp),
696 funcexe.fe_found_var);
697 return FAIL;
698 }
699 *arg = argp;
700 return OK;
701 }
702 }
703
704 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
705 }
706
707 else if (rettv->v_type == VAR_OBJECT)
708 {
709 for (int i = 0; i < cl->class_obj_member_count; ++i)
710 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000711 ocmember_T *m = &cl->class_obj_members[i];
712 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000713 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000714 if (*name == '_')
715 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000716 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000717 return FAIL;
718 }
719
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000720 // The object only contains a pointer to the class, the member
721 // values array follows right after that.
722 object_T *obj = rettv->vval.v_object;
723 typval_T *tv = (typval_T *)(obj + 1) + i;
724 copy_tv(tv, rettv);
725 object_unref(obj);
726
727 *arg = name_end;
728 return OK;
729 }
730 }
731
732 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
733 }
734
Bram Moolenaard505d172022-12-18 21:42:55 +0000735 else if (rettv->v_type == VAR_CLASS)
736 {
737 // class member
738 for (int i = 0; i < cl->class_class_member_count; ++i)
739 {
740 ocmember_T *m = &cl->class_class_members[i];
741 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
742 {
743 if (*name == '_')
744 {
745 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
746 return FAIL;
747 }
748
749 typval_T *tv = &cl->class_members_tv[i];
750 copy_tv(tv, rettv);
751 class_unref(cl);
752
753 *arg = name_end;
754 return OK;
755 }
756 }
757
758 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
759 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000760
761 return FAIL;
762}
763
764/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000765 * If "arg" points to a class or object method, return it.
766 * Otherwise return NULL.
767 */
768 ufunc_T *
769find_class_func(char_u **arg)
770{
771 char_u *name = *arg;
772 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
773 if (name_end == name || *name_end != '.')
774 return NULL;
775
776 size_t len = name_end - name;
777 typval_T tv;
778 tv.v_type = VAR_UNKNOWN;
779 if (eval_variable(name, len, 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
780 return NULL;
781 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +0000782 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000783
784 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
785 : tv.vval.v_object->obj_class;
786 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +0000787 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000788 char_u *fname = name_end + 1;
789 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
790 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +0000791 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000792 len = fname_end - fname;
793
794 for (int i = 0; i < cl->class_obj_method_count; ++i)
795 {
796 ufunc_T *fp = cl->class_obj_methods[i];
797 // Use a separate pointer to avoid that ASAN complains about
798 // uf_name[] only being 4 characters.
799 char_u *ufname = (char_u *)fp->uf_name;
800 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +0000801 {
802 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000803 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +0000804 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000805 }
806
Bram Moolenaareb533502022-12-14 15:06:11 +0000807fail_after_eval:
808 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000809 return NULL;
810}
811
812/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000813 * Make a copy of an object.
814 */
815 void
816copy_object(typval_T *from, typval_T *to)
817{
818 *to = *from;
819 if (to->vval.v_object != NULL)
820 ++to->vval.v_object->obj_refcount;
821}
822
823/*
824 * Free an object.
825 */
826 static void
827object_clear(object_T *obj)
828{
829 class_T *cl = obj->obj_class;
830
831 // the member values are just after the object structure
832 typval_T *tv = (typval_T *)(obj + 1);
833 for (int i = 0; i < cl->class_obj_member_count; ++i)
834 clear_tv(tv + i);
835
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000836 // Remove from the list headed by "first_object".
837 object_cleared(obj);
838
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000839 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000840 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000841}
842
843/*
844 * Unreference an object.
845 */
846 void
847object_unref(object_T *obj)
848{
849 if (obj != NULL && --obj->obj_refcount <= 0)
850 object_clear(obj);
851}
852
853/*
854 * Make a copy of a class.
855 */
856 void
857copy_class(typval_T *from, typval_T *to)
858{
859 *to = *from;
860 if (to->vval.v_class != NULL)
861 ++to->vval.v_class->class_refcount;
862}
863
864/*
865 * Unreference a class. Free it when the reference count goes down to zero.
866 */
867 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000868class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000869{
Bram Moolenaard505d172022-12-18 21:42:55 +0000870 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000871 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000872 // Freeing what the class contains may recursively come back here.
873 // Clear "class_name" first, if it is NULL the class does not need to
874 // be freed.
875 VIM_CLEAR(cl->class_name);
876
877 for (int i = 0; i < cl->class_class_member_count; ++i)
878 {
879 ocmember_T *m = &cl->class_class_members[i];
880 vim_free(m->ocm_name);
881 vim_free(m->ocm_init);
882 if (cl->class_members_tv != NULL)
883 clear_tv(&cl->class_members_tv[i]);
884 }
885 vim_free(cl->class_class_members);
886 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000887
888 for (int i = 0; i < cl->class_obj_member_count; ++i)
889 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000890 ocmember_T *m = &cl->class_obj_members[i];
891 vim_free(m->ocm_name);
892 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000893 }
894 vim_free(cl->class_obj_members);
895
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000896 for (int i = 0; i < cl->class_obj_method_count; ++i)
897 {
898 ufunc_T *uf = cl->class_obj_methods[i];
899 func_clear_free(uf, FALSE);
900 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000901 vim_free(cl->class_obj_methods);
902
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000903 clear_type_list(&cl->class_type_list);
904
905 vim_free(cl);
906 }
907}
908
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000909static object_T *first_object = NULL;
910
911/*
912 * Call this function when an object has been created. It will be added to the
913 * list headed by "first_object".
914 */
915 void
916object_created(object_T *obj)
917{
918 if (first_object != NULL)
919 {
920 obj->obj_next_used = first_object;
921 first_object->obj_prev_used = obj;
922 }
923 first_object = obj;
924}
925
926/*
927 * Call this function when an object has been cleared and is about to be freed.
928 * It is removed from the list headed by "first_object".
929 */
930 void
931object_cleared(object_T *obj)
932{
933 if (obj->obj_next_used != NULL)
934 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
935 if (obj->obj_prev_used != NULL)
936 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
937 else if (first_object == obj)
938 first_object = obj->obj_next_used;
939}
940
941/*
942 * Go through the list of all objects and free items without "copyID".
943 */
944 int
945object_free_nonref(int copyID)
946{
947 int did_free = FALSE;
948 object_T *next_obj;
949
950 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
951 {
952 next_obj = obj->obj_next_used;
953 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
954 {
955 // Free the object and items it contains.
956 object_clear(obj);
957 did_free = TRUE;
958 }
959 }
960
961 return did_free;
962}
963
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000964
965#endif // FEAT_EVAL