blob: 94fe9e227c207be5e840c4c5ec99f3eded84a086 [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/*
572 * Find member "name" in class "cl" and return its type.
573 * When not found t_any is returned.
574 */
575 type_T *
576class_member_type(
577 class_T *cl,
578 char_u *name,
579 char_u *name_end,
580 int *member_idx)
581{
582 *member_idx = -1; // not found (yet)
583 size_t len = name_end - name;
584
585 for (int i = 0; i < cl->class_obj_member_count; ++i)
586 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000587 ocmember_T *m = cl->class_obj_members + i;
588 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000589 {
590 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +0000591 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000592 }
593 }
594 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000595}
596
597/*
598 * Handle ":interface" up to ":endinterface".
599 */
600 void
601ex_interface(exarg_T *eap UNUSED)
602{
603 // TODO
604}
605
606/*
607 * Handle ":enum" up to ":endenum".
608 */
609 void
610ex_enum(exarg_T *eap UNUSED)
611{
612 // TODO
613}
614
615/*
616 * Handle ":type".
617 */
618 void
619ex_type(exarg_T *eap UNUSED)
620{
621 // TODO
622}
623
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000624/*
625 * Evaluate what comes after a class:
626 * - class member: SomeClass.varname
627 * - class method: SomeClass.SomeMethod()
628 * - class constructor: SomeClass.new()
629 * - object member: someObject.varname
630 * - object method: someObject.SomeMethod()
631 *
632 * "*arg" points to the '.'.
633 * "*arg" is advanced to after the member name or method call.
634 *
635 * Returns FAIL or OK.
636 */
637 int
638class_object_index(
639 char_u **arg,
640 typval_T *rettv,
641 evalarg_T *evalarg,
642 int verbose UNUSED) // give error messages
643{
644 // int evaluate = evalarg != NULL
645 // && (evalarg->eval_flags & EVAL_EVALUATE);
646
647 if (VIM_ISWHITE((*arg)[1]))
648 {
649 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
650 return FAIL;
651 }
652
653 ++*arg;
654 char_u *name = *arg;
655 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
656 if (name_end == name)
657 return FAIL;
658 size_t len = name_end - name;
659
660 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
661 : rettv->vval.v_object->obj_class;
662 if (*name_end == '(')
663 {
664 for (int i = 0; i < cl->class_obj_method_count; ++i)
665 {
666 ufunc_T *fp = cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +0000667 // Use a separate pointer to avoid that ASAN complains about
668 // uf_name[] only being 4 characters.
669 char_u *ufname = (char_u *)fp->uf_name;
670 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000671 {
672 typval_T argvars[MAX_FUNC_ARGS + 1];
673 int argcount = 0;
674
675 char_u *argp = name_end;
676 int ret = get_func_arguments(&argp, evalarg, 0,
677 argvars, &argcount);
678 if (ret == FAIL)
679 return FAIL;
680
681 funcexe_T funcexe;
682 CLEAR_FIELD(funcexe);
683 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000684 if (rettv->v_type == VAR_OBJECT)
685 {
686 funcexe.fe_object = rettv->vval.v_object;
687 ++funcexe.fe_object->obj_refcount;
688 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000689
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000690 // Clear the class or object after calling the function, in
691 // case the refcount is one.
692 typval_T tv_tofree = *rettv;
693 rettv->v_type = VAR_UNKNOWN;
694
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000695 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000696 int error = call_user_func_check(fp, argcount, argvars,
697 rettv, &funcexe, NULL);
698
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000699 // Clear the previous rettv and the arguments.
700 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000701 for (int idx = 0; idx < argcount; ++idx)
702 clear_tv(&argvars[idx]);
703
704 if (error != FCERR_NONE)
705 {
706 user_func_error(error, printable_func_name(fp),
707 funcexe.fe_found_var);
708 return FAIL;
709 }
710 *arg = argp;
711 return OK;
712 }
713 }
714
715 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
716 }
717
718 else if (rettv->v_type == VAR_OBJECT)
719 {
720 for (int i = 0; i < cl->class_obj_member_count; ++i)
721 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000722 ocmember_T *m = &cl->class_obj_members[i];
723 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000724 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000725 if (*name == '_')
726 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000727 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000728 return FAIL;
729 }
730
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000731 // The object only contains a pointer to the class, the member
732 // values array follows right after that.
733 object_T *obj = rettv->vval.v_object;
734 typval_T *tv = (typval_T *)(obj + 1) + i;
735 copy_tv(tv, rettv);
736 object_unref(obj);
737
738 *arg = name_end;
739 return OK;
740 }
741 }
742
743 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
744 }
745
Bram Moolenaard505d172022-12-18 21:42:55 +0000746 else if (rettv->v_type == VAR_CLASS)
747 {
748 // class member
749 for (int i = 0; i < cl->class_class_member_count; ++i)
750 {
751 ocmember_T *m = &cl->class_class_members[i];
752 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
753 {
754 if (*name == '_')
755 {
756 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
757 return FAIL;
758 }
759
760 typval_T *tv = &cl->class_members_tv[i];
761 copy_tv(tv, rettv);
762 class_unref(cl);
763
764 *arg = name_end;
765 return OK;
766 }
767 }
768
769 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
770 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000771
772 return FAIL;
773}
774
775/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000776 * If "arg" points to a class or object method, return it.
777 * Otherwise return NULL.
778 */
779 ufunc_T *
780find_class_func(char_u **arg)
781{
782 char_u *name = *arg;
783 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
784 if (name_end == name || *name_end != '.')
785 return NULL;
786
787 size_t len = name_end - name;
788 typval_T tv;
789 tv.v_type = VAR_UNKNOWN;
790 if (eval_variable(name, len, 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
791 return NULL;
792 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +0000793 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000794
795 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
796 : tv.vval.v_object->obj_class;
797 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +0000798 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000799 char_u *fname = name_end + 1;
800 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
801 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +0000802 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000803 len = fname_end - fname;
804
805 for (int i = 0; i < cl->class_obj_method_count; ++i)
806 {
807 ufunc_T *fp = cl->class_obj_methods[i];
808 // Use a separate pointer to avoid that ASAN complains about
809 // uf_name[] only being 4 characters.
810 char_u *ufname = (char_u *)fp->uf_name;
811 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +0000812 {
813 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000814 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +0000815 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000816 }
817
Bram Moolenaareb533502022-12-14 15:06:11 +0000818fail_after_eval:
819 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000820 return NULL;
821}
822
823/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000824 * Make a copy of an object.
825 */
826 void
827copy_object(typval_T *from, typval_T *to)
828{
829 *to = *from;
830 if (to->vval.v_object != NULL)
831 ++to->vval.v_object->obj_refcount;
832}
833
834/*
835 * Free an object.
836 */
837 static void
838object_clear(object_T *obj)
839{
840 class_T *cl = obj->obj_class;
841
842 // the member values are just after the object structure
843 typval_T *tv = (typval_T *)(obj + 1);
844 for (int i = 0; i < cl->class_obj_member_count; ++i)
845 clear_tv(tv + i);
846
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000847 // Remove from the list headed by "first_object".
848 object_cleared(obj);
849
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000850 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000851 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000852}
853
854/*
855 * Unreference an object.
856 */
857 void
858object_unref(object_T *obj)
859{
860 if (obj != NULL && --obj->obj_refcount <= 0)
861 object_clear(obj);
862}
863
864/*
865 * Make a copy of a class.
866 */
867 void
868copy_class(typval_T *from, typval_T *to)
869{
870 *to = *from;
871 if (to->vval.v_class != NULL)
872 ++to->vval.v_class->class_refcount;
873}
874
875/*
876 * Unreference a class. Free it when the reference count goes down to zero.
877 */
878 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000879class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000880{
Bram Moolenaard505d172022-12-18 21:42:55 +0000881 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000882 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000883 // Freeing what the class contains may recursively come back here.
884 // Clear "class_name" first, if it is NULL the class does not need to
885 // be freed.
886 VIM_CLEAR(cl->class_name);
887
888 for (int i = 0; i < cl->class_class_member_count; ++i)
889 {
890 ocmember_T *m = &cl->class_class_members[i];
891 vim_free(m->ocm_name);
892 vim_free(m->ocm_init);
893 if (cl->class_members_tv != NULL)
894 clear_tv(&cl->class_members_tv[i]);
895 }
896 vim_free(cl->class_class_members);
897 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000898
899 for (int i = 0; i < cl->class_obj_member_count; ++i)
900 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000901 ocmember_T *m = &cl->class_obj_members[i];
902 vim_free(m->ocm_name);
903 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000904 }
905 vim_free(cl->class_obj_members);
906
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000907 for (int i = 0; i < cl->class_obj_method_count; ++i)
908 {
909 ufunc_T *uf = cl->class_obj_methods[i];
910 func_clear_free(uf, FALSE);
911 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000912 vim_free(cl->class_obj_methods);
913
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000914 clear_type_list(&cl->class_type_list);
915
916 vim_free(cl);
917 }
918}
919
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000920static object_T *first_object = NULL;
921
922/*
923 * Call this function when an object has been created. It will be added to the
924 * list headed by "first_object".
925 */
926 void
927object_created(object_T *obj)
928{
929 if (first_object != NULL)
930 {
931 obj->obj_next_used = first_object;
932 first_object->obj_prev_used = obj;
933 }
934 first_object = obj;
935}
936
937/*
938 * Call this function when an object has been cleared and is about to be freed.
939 * It is removed from the list headed by "first_object".
940 */
941 void
942object_cleared(object_T *obj)
943{
944 if (obj->obj_next_used != NULL)
945 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
946 if (obj->obj_prev_used != NULL)
947 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
948 else if (first_object == obj)
949 first_object = obj->obj_next_used;
950}
951
952/*
953 * Go through the list of all objects and free items without "copyID".
954 */
955 int
956object_free_nonref(int copyID)
957{
958 int did_free = FALSE;
959 object_T *next_obj;
960
961 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
962 {
963 next_obj = obj->obj_next_used;
964 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
965 {
966 // Free the object and items it contains.
967 object_clear(obj);
968 did_free = TRUE;
969 }
970 }
971
972 return did_free;
973}
974
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000975
976#endif // FEAT_EVAL