blob: 2717a7905a60bcbf4615600a598f49dae6edbf50 [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)
Bram Moolenaare83c1332023-01-02 21:04:04 +0000102 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000103 type = typval2type(&tv, get_copyID(), type_list,
104 TVTT_DO_MEMBER);
Bram Moolenaare83c1332023-01-02 21:04:04 +0000105 clear_tv(&tv);
106 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000107 if (type == NULL)
108 {
109 semsg(_(e_cannot_get_object_member_type_from_initializer_str),
110 expr_start);
111 clear_evalarg(&evalarg, NULL);
112 return FAIL;
113 }
114 }
115 clear_evalarg(&evalarg, NULL);
116 }
117 if (!valid_declaration_type(type))
118 return FAIL;
119
120 *type_ret = type;
121 if (expr_end > expr_start)
122 *init_expr = vim_strnsave(expr_start, expr_end - expr_start);
123 return OK;
124}
125
126/*
127 * Add a member to an object or a class.
128 * Returns OK when successful, "init_expr" will be consumed then.
129 * Returns FAIL otherwise, caller might need to free "init_expr".
130 */
131 static int
132add_member(
133 garray_T *gap,
134 char_u *varname,
135 char_u *varname_end,
136 int has_public,
137 type_T *type,
138 char_u *init_expr)
139{
140 if (ga_grow(gap, 1) == FAIL)
141 return FAIL;
142 ocmember_T *m = ((ocmember_T *)gap->ga_data) + gap->ga_len;
143 m->ocm_name = vim_strnsave(varname, varname_end - varname);
144 m->ocm_access = has_public ? ACCESS_ALL
145 : *varname == '_' ? ACCESS_PRIVATE : ACCESS_READ;
146 m->ocm_type = type;
147 if (init_expr != NULL)
148 m->ocm_init = init_expr;
149 ++gap->ga_len;
150 return OK;
151}
152
153/*
154 * Move the class or object members found while parsing a class into the class.
155 * "gap" contains the found members.
156 * "members" will be set to the newly allocated array of members and
157 * "member_count" set to the number of members.
158 * Returns OK or FAIL.
159 */
160 static int
161add_members_to_class(
162 garray_T *gap,
163 ocmember_T **members,
164 int *member_count)
165{
166 *member_count = gap->ga_len;
167 *members = gap->ga_len == 0 ? NULL : ALLOC_MULT(ocmember_T, gap->ga_len);
168 if (gap->ga_len > 0 && *members == NULL)
169 return FAIL;
Bram Moolenaar8efdcee2022-12-19 12:18:09 +0000170 if (gap->ga_len > 0)
171 mch_memmove(*members, gap->ga_data, sizeof(ocmember_T) * gap->ga_len);
Bram Moolenaard505d172022-12-18 21:42:55 +0000172 VIM_CLEAR(gap->ga_data);
173 return OK;
174}
175
176/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000177 * Handle ":class" and ":abstract class" up to ":endclass".
178 */
179 void
180ex_class(exarg_T *eap)
181{
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000182 if (!current_script_is_vim9()
183 || (cmdmod.cmod_flags & CMOD_LEGACY)
184 || !getline_equal(eap->getline, eap->cookie, getsourceline))
185 {
186 emsg(_(e_class_can_only_be_defined_in_vim9_script));
187 return;
188 }
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000189
190 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000191 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000192 if (is_abstract)
193 {
194 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
195 {
196 semsg(_(e_invalid_argument_str), arg);
197 return;
198 }
199 arg = skipwhite(arg + 5);
200 }
201
202 if (!ASCII_ISUPPER(*arg))
203 {
204 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
205 return;
206 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000207 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
208 if (!IS_WHITE_OR_NUL(*name_end))
209 {
210 semsg(_(e_white_space_required_after_class_name_str), arg);
211 return;
212 }
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000213
214 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000215 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000216 // extends SomeClass
217 // implements SomeInterface
218 // specifies SomeInterface
Bram Moolenaard505d172022-12-18 21:42:55 +0000219 // check that nothing follows
220 // handle "is_export" if it is set
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000221
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000222 garray_T type_list; // list of pointers to allocated types
223 ga_init2(&type_list, sizeof(type_T *), 10);
224
Bram Moolenaard505d172022-12-18 21:42:55 +0000225 // Growarray with class members declared in the class.
226 garray_T classmembers;
227 ga_init2(&classmembers, sizeof(ocmember_T), 10);
228
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000229 // Growarray with functions declared in the class.
230 garray_T classfunctions;
231 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +0000232
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000233 // Growarray with object members declared in the class.
234 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +0000235 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000236
237 // Growarray with object methods declared in the class.
238 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000239 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000240
241 /*
242 * Go over the body of the class until "endclass" is found.
243 */
244 char_u *theline = NULL;
245 int success = FALSE;
246 for (;;)
247 {
248 vim_free(theline);
249 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
250 if (theline == NULL)
251 break;
252 char_u *line = skipwhite(theline);
253
Bram Moolenaar418b5472022-12-20 13:38:22 +0000254 // Skip empty and comment lines.
255 if (*line == NUL)
256 continue;
257 if (*line == '#')
258 {
259 if (vim9_bad_comment(line))
260 break;
261 continue;
262 }
263
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000264 char_u *p = line;
265 if (checkforcmd(&p, "endclass", 4))
266 {
267 if (STRNCMP(line, "endclass", 8) != 0)
268 semsg(_(e_command_cannot_be_shortened_str), line);
269 else if (*p == '|' || !ends_excmd2(line, p))
270 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +0000271 else
272 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000273 break;
274 }
275
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000276 int has_public = FALSE;
277 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000278 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000279 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000280 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000281 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000282 break;
283 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000284 has_public = TRUE;
285 p = skipwhite(line + 6);
286
Bram Moolenaard505d172022-12-18 21:42:55 +0000287 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000288 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000289 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000290 break;
291 }
292 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000293
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000294 int has_static = FALSE;
295 char_u *ps = p;
296 if (checkforcmd(&p, "static", 4))
297 {
298 if (STRNCMP(ps, "static", 6) != 0)
299 {
300 semsg(_(e_command_cannot_be_shortened_str), ps);
301 break;
302 }
303 has_static = TRUE;
304 p = skipwhite(ps + 6);
305 }
306
Bram Moolenaard505d172022-12-18 21:42:55 +0000307 // object members (public, read access, private):
308 // "this._varname"
309 // "this.varname"
310 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000311 if (STRNCMP(p, "this", 4) == 0)
312 {
313 if (p[4] != '.' || !eval_isnamec1(p[5]))
314 {
315 semsg(_(e_invalid_object_member_declaration_str), p);
316 break;
317 }
318 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +0000319 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +0000320 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +0000321 char_u *init_expr = NULL;
322 if (parse_member(eap, line, varname, has_public,
323 &varname_end, &type_list, &type, &init_expr) == FAIL)
324 break;
325 if (add_member(&objmembers, varname, varname_end,
326 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000327 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000328 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000329 break;
330 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000331 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000332
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000333 // constructors:
334 // def new()
335 // enddef
336 // def newOther()
337 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000338 // object methods and class functions:
339 // def SomeMethod()
340 // enddef
341 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000342 // enddef
343 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000344 // def <Tval> someMethod()
345 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000346 else if (checkforcmd(&p, "def", 3))
347 {
348 exarg_T ea;
349 garray_T lines_to_free;
350
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000351 // TODO: error for "public static def Func()"?
352
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000353 CLEAR_FIELD(ea);
354 ea.cmd = line;
355 ea.arg = p;
356 ea.cmdidx = CMD_def;
357 ea.getline = eap->getline;
358 ea.cookie = eap->cookie;
359
360 ga_init2(&lines_to_free, sizeof(char_u *), 50);
361 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free, TRUE);
362 ga_clear_strings(&lines_to_free);
363
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000364 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000365 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000366 int is_new = STRNCMP(uf->uf_name, "new", 3) == 0;
367 garray_T *fgap = has_static || is_new
368 ? &classfunctions : &objmethods;
369 if (ga_grow(fgap, 1) == OK)
370 {
371 if (is_new)
372 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000373
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000374 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
375 ++fgap->ga_len;
376 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000377 }
378 }
379
380 // class members
381 else if (has_static)
382 {
383 // class members (public, read access, private):
384 // "static _varname"
385 // "static varname"
386 // "public static varname"
387 char_u *varname = p;
388 char_u *varname_end = NULL;
389 type_T *type = NULL;
390 char_u *init_expr = NULL;
391 if (parse_member(eap, line, varname, has_public,
392 &varname_end, &type_list, &type, &init_expr) == FAIL)
393 break;
394 if (add_member(&classmembers, varname, varname_end,
395 has_public, type, init_expr) == FAIL)
396 {
397 vim_free(init_expr);
398 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000399 }
400 }
401
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000402 else
403 {
404 semsg(_(e_not_valid_command_in_class_str), line);
405 break;
406 }
407 }
408 vim_free(theline);
409
Bram Moolenaareb533502022-12-14 15:06:11 +0000410 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000411 if (success)
412 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000413 // "endclass" encountered without failures: Create the class.
414
Bram Moolenaareb533502022-12-14 15:06:11 +0000415 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000416 if (cl == NULL)
417 goto cleanup;
418 cl->class_refcount = 1;
419 cl->class_name = vim_strnsave(arg, name_end - arg);
Bram Moolenaard505d172022-12-18 21:42:55 +0000420 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000421 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000422
423 // Add class and object members to "cl".
424 if (add_members_to_class(&classmembers,
425 &cl->class_class_members,
426 &cl->class_class_member_count) == FAIL
427 || add_members_to_class(&objmembers,
428 &cl->class_obj_members,
429 &cl->class_obj_member_count) == FAIL)
430 goto cleanup;
431
432 if (cl->class_class_member_count > 0)
433 {
434 // Allocate a typval for each class member and initialize it.
435 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
436 cl->class_class_member_count);
437 if (cl->class_members_tv != NULL)
438 for (int i = 0; i < cl->class_class_member_count; ++i)
439 {
440 ocmember_T *m = &cl->class_class_members[i];
441 typval_T *tv = &cl->class_members_tv[i];
442 if (m->ocm_init != NULL)
443 {
444 typval_T *etv = eval_expr(m->ocm_init, eap);
445 if (etv != NULL)
446 {
447 *tv = *etv;
448 vim_free(etv);
449 }
450 }
451 else
452 {
453 // TODO: proper default value
454 tv->v_type = m->ocm_type->tt_type;
455 tv->vval.v_string = NULL;
456 }
457 }
458 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000459
460 int have_new = FALSE;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000461 for (int i = 0; i < classfunctions.ga_len; ++i)
462 if (STRCMP(((ufunc_T **)classfunctions.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000463 "new") == 0)
464 {
465 have_new = TRUE;
466 break;
467 }
468 if (!have_new)
469 {
470 // No new() method was defined, add the default constructor.
471 garray_T fga;
472 ga_init2(&fga, 1, 1000);
473 ga_concat(&fga, (char_u *)"new(");
474 for (int i = 0; i < cl->class_obj_member_count; ++i)
475 {
476 if (i > 0)
477 ga_concat(&fga, (char_u *)", ");
478 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000479 ocmember_T *m = cl->class_obj_members + i;
480 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000481 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000482 }
483 ga_concat(&fga, (char_u *)")\nenddef\n");
484 ga_append(&fga, NUL);
485
486 exarg_T fea;
487 CLEAR_FIELD(fea);
488 fea.cmdidx = CMD_def;
489 fea.cmd = fea.arg = fga.ga_data;
490
491 garray_T lines_to_free;
492 ga_init2(&lines_to_free, sizeof(char_u *), 50);
493
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000494 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, TRUE);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000495
496 ga_clear_strings(&lines_to_free);
497 vim_free(fga.ga_data);
498
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000499 if (nf != NULL && ga_grow(&classfunctions, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000500 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000501 ((ufunc_T **)classfunctions.ga_data)[classfunctions.ga_len] = nf;
502 ++classfunctions.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000503
504 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000505 nf->uf_ret_type = get_type_ptr(&type_list);
506 if (nf->uf_ret_type != NULL)
507 {
508 nf->uf_ret_type->tt_type = VAR_OBJECT;
509 nf->uf_ret_type->tt_member = (type_T *)cl;
510 nf->uf_ret_type->tt_argcount = 0;
511 nf->uf_ret_type->tt_args = NULL;
512 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000513 }
514 }
515
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000516 // loop 1: class functions, loop 2: object methods
517 for (int loop = 1; loop <= 2; ++loop)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000518 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000519 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
520 int *fcount = loop == 1 ? &cl->class_class_function_count
521 : &cl->class_obj_method_count;
522 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
523 : &cl->class_obj_methods;
524
525 *fcount = gap->ga_len;
526 if (gap->ga_len == 0)
527 {
528 *fup = NULL;
529 continue;
530 }
531 *fup = ALLOC_MULT(ufunc_T *, gap->ga_len);
532 if (*fup == NULL)
533 goto cleanup;
534 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
535 vim_free(gap->ga_data);
536
537 // Set the class pointer on all the object methods.
538 for (int i = 0; i < gap->ga_len; ++i)
539 {
540 ufunc_T *fp = (*fup)[i];
541 fp->uf_class = cl;
542 if (loop == 2)
543 fp->uf_flags |= FC_OBJECT;
544 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000545 }
546
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000547 cl->class_type.tt_type = VAR_CLASS;
548 cl->class_type.tt_member = (type_T *)cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000549 cl->class_object_type.tt_type = VAR_OBJECT;
550 cl->class_object_type.tt_member = (type_T *)cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000551 cl->class_type_list = type_list;
552
553 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +0000554 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000555
556 // Add the class to the script-local variables.
557 typval_T tv;
558 tv.v_type = VAR_CLASS;
559 tv.vval.v_class = cl;
560 set_var_const(cl->class_name, current_sctx.sc_sid,
561 NULL, &tv, FALSE, ASSIGN_DECL, 0);
562 return;
563 }
564
565cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +0000566 if (cl != NULL)
567 {
568 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000569 vim_free(cl->class_class_functions);
Bram Moolenaareb533502022-12-14 15:06:11 +0000570 vim_free(cl->class_obj_members);
571 vim_free(cl->class_obj_methods);
572 vim_free(cl);
573 }
574
Bram Moolenaard505d172022-12-18 21:42:55 +0000575 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000576 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000577 garray_T *gap = round == 1 ? &classmembers : &objmembers;
578 if (gap->ga_len == 0 || gap->ga_data == NULL)
579 continue;
580
581 for (int i = 0; i < gap->ga_len; ++i)
582 {
583 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
584 vim_free(m->ocm_name);
585 vim_free(m->ocm_init);
586 }
587 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000588 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000589
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000590 for (int i = 0; i < objmethods.ga_len; ++i)
591 {
592 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
593 func_clear_free(uf, FALSE);
594 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000595 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000596
597 for (int i = 0; i < classfunctions.ga_len; ++i)
598 {
599 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
600 func_clear_free(uf, FALSE);
601 }
602 ga_clear(&classfunctions);
603
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000604 clear_type_list(&type_list);
605}
606
607/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000608 * Find member "name" in class "cl", set "member_idx" to the member index and
609 * return its type.
610 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000611 */
612 type_T *
613class_member_type(
614 class_T *cl,
615 char_u *name,
616 char_u *name_end,
617 int *member_idx)
618{
619 *member_idx = -1; // not found (yet)
620 size_t len = name_end - name;
621
622 for (int i = 0; i < cl->class_obj_member_count; ++i)
623 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000624 ocmember_T *m = cl->class_obj_members + i;
625 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000626 {
627 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +0000628 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000629 }
630 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000631
632 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000633 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000634}
635
636/*
637 * Handle ":interface" up to ":endinterface".
638 */
639 void
640ex_interface(exarg_T *eap UNUSED)
641{
642 // TODO
643}
644
645/*
646 * Handle ":enum" up to ":endenum".
647 */
648 void
649ex_enum(exarg_T *eap UNUSED)
650{
651 // TODO
652}
653
654/*
655 * Handle ":type".
656 */
657 void
658ex_type(exarg_T *eap UNUSED)
659{
660 // TODO
661}
662
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000663/*
664 * Evaluate what comes after a class:
665 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000666 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000667 * - class constructor: SomeClass.new()
668 * - object member: someObject.varname
669 * - object method: someObject.SomeMethod()
670 *
671 * "*arg" points to the '.'.
672 * "*arg" is advanced to after the member name or method call.
673 *
674 * Returns FAIL or OK.
675 */
676 int
677class_object_index(
678 char_u **arg,
679 typval_T *rettv,
680 evalarg_T *evalarg,
681 int verbose UNUSED) // give error messages
682{
683 // int evaluate = evalarg != NULL
684 // && (evalarg->eval_flags & EVAL_EVALUATE);
685
686 if (VIM_ISWHITE((*arg)[1]))
687 {
688 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
689 return FAIL;
690 }
691
692 ++*arg;
693 char_u *name = *arg;
694 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
695 if (name_end == name)
696 return FAIL;
697 size_t len = name_end - name;
698
699 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
700 : rettv->vval.v_object->obj_class;
701 if (*name_end == '(')
702 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000703 int on_class = rettv->v_type == VAR_CLASS;
704 int count = on_class ? cl->class_class_function_count
705 : cl->class_obj_method_count;
706 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000707 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000708 ufunc_T *fp = on_class ? cl->class_class_functions[i]
709 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +0000710 // Use a separate pointer to avoid that ASAN complains about
711 // uf_name[] only being 4 characters.
712 char_u *ufname = (char_u *)fp->uf_name;
713 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000714 {
715 typval_T argvars[MAX_FUNC_ARGS + 1];
716 int argcount = 0;
717
718 char_u *argp = name_end;
719 int ret = get_func_arguments(&argp, evalarg, 0,
720 argvars, &argcount);
721 if (ret == FAIL)
722 return FAIL;
723
724 funcexe_T funcexe;
725 CLEAR_FIELD(funcexe);
726 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000727 if (rettv->v_type == VAR_OBJECT)
728 {
729 funcexe.fe_object = rettv->vval.v_object;
730 ++funcexe.fe_object->obj_refcount;
731 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000732
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000733 // Clear the class or object after calling the function, in
734 // case the refcount is one.
735 typval_T tv_tofree = *rettv;
736 rettv->v_type = VAR_UNKNOWN;
737
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000738 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000739 int error = call_user_func_check(fp, argcount, argvars,
740 rettv, &funcexe, NULL);
741
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000742 // Clear the previous rettv and the arguments.
743 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000744 for (int idx = 0; idx < argcount; ++idx)
745 clear_tv(&argvars[idx]);
746
747 if (error != FCERR_NONE)
748 {
749 user_func_error(error, printable_func_name(fp),
750 funcexe.fe_found_var);
751 return FAIL;
752 }
753 *arg = argp;
754 return OK;
755 }
756 }
757
758 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
759 }
760
761 else if (rettv->v_type == VAR_OBJECT)
762 {
763 for (int i = 0; i < cl->class_obj_member_count; ++i)
764 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000765 ocmember_T *m = &cl->class_obj_members[i];
766 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000767 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000768 if (*name == '_')
769 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000770 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000771 return FAIL;
772 }
773
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000774 // The object only contains a pointer to the class, the member
775 // values array follows right after that.
776 object_T *obj = rettv->vval.v_object;
777 typval_T *tv = (typval_T *)(obj + 1) + i;
778 copy_tv(tv, rettv);
779 object_unref(obj);
780
781 *arg = name_end;
782 return OK;
783 }
784 }
785
786 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
787 }
788
Bram Moolenaard505d172022-12-18 21:42:55 +0000789 else if (rettv->v_type == VAR_CLASS)
790 {
791 // class member
792 for (int i = 0; i < cl->class_class_member_count; ++i)
793 {
794 ocmember_T *m = &cl->class_class_members[i];
795 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
796 {
797 if (*name == '_')
798 {
799 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
800 return FAIL;
801 }
802
803 typval_T *tv = &cl->class_members_tv[i];
804 copy_tv(tv, rettv);
805 class_unref(cl);
806
807 *arg = name_end;
808 return OK;
809 }
810 }
811
812 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
813 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000814
815 return FAIL;
816}
817
818/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000819 * If "arg" points to a class or object method, return it.
820 * Otherwise return NULL.
821 */
822 ufunc_T *
823find_class_func(char_u **arg)
824{
825 char_u *name = *arg;
826 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
827 if (name_end == name || *name_end != '.')
828 return NULL;
829
830 size_t len = name_end - name;
831 typval_T tv;
832 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +0000833 if (eval_variable(name, (int)len,
834 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000835 return NULL;
836 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +0000837 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000838
839 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
840 : tv.vval.v_object->obj_class;
841 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +0000842 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000843 char_u *fname = name_end + 1;
844 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
845 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +0000846 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000847 len = fname_end - fname;
848
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000849 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
850 : cl->class_obj_method_count;
851 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
852 : cl->class_obj_methods;
853 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000854 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000855 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000856 // Use a separate pointer to avoid that ASAN complains about
857 // uf_name[] only being 4 characters.
858 char_u *ufname = (char_u *)fp->uf_name;
859 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +0000860 {
861 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000862 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +0000863 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000864 }
865
Bram Moolenaareb533502022-12-14 15:06:11 +0000866fail_after_eval:
867 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000868 return NULL;
869}
870
871/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000872 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
873 * index in class.class_class_members[].
874 * If "cl_ret" is not NULL set it to the class.
875 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000876 */
877 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000878class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000879{
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000880 if (cctx == NULL || cctx->ctx_ufunc == NULL
881 || cctx->ctx_ufunc->uf_class == NULL)
882 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000883 class_T *cl = cctx->ctx_ufunc->uf_class;
884
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000885 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000886 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000887 ocmember_T *m = &cl->class_class_members[i];
888 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000889 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000890 if (cl_ret != NULL)
891 *cl_ret = cl;
892 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000893 }
894 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000895 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000896}
897
898/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000899 * Make a copy of an object.
900 */
901 void
902copy_object(typval_T *from, typval_T *to)
903{
904 *to = *from;
905 if (to->vval.v_object != NULL)
906 ++to->vval.v_object->obj_refcount;
907}
908
909/*
910 * Free an object.
911 */
912 static void
913object_clear(object_T *obj)
914{
915 class_T *cl = obj->obj_class;
916
917 // the member values are just after the object structure
918 typval_T *tv = (typval_T *)(obj + 1);
919 for (int i = 0; i < cl->class_obj_member_count; ++i)
920 clear_tv(tv + i);
921
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000922 // Remove from the list headed by "first_object".
923 object_cleared(obj);
924
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000925 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000926 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000927}
928
929/*
930 * Unreference an object.
931 */
932 void
933object_unref(object_T *obj)
934{
935 if (obj != NULL && --obj->obj_refcount <= 0)
936 object_clear(obj);
937}
938
939/*
940 * Make a copy of a class.
941 */
942 void
943copy_class(typval_T *from, typval_T *to)
944{
945 *to = *from;
946 if (to->vval.v_class != NULL)
947 ++to->vval.v_class->class_refcount;
948}
949
950/*
951 * Unreference a class. Free it when the reference count goes down to zero.
952 */
953 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000954class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000955{
Bram Moolenaard505d172022-12-18 21:42:55 +0000956 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000957 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000958 // Freeing what the class contains may recursively come back here.
959 // Clear "class_name" first, if it is NULL the class does not need to
960 // be freed.
961 VIM_CLEAR(cl->class_name);
962
963 for (int i = 0; i < cl->class_class_member_count; ++i)
964 {
965 ocmember_T *m = &cl->class_class_members[i];
966 vim_free(m->ocm_name);
967 vim_free(m->ocm_init);
968 if (cl->class_members_tv != NULL)
969 clear_tv(&cl->class_members_tv[i]);
970 }
971 vim_free(cl->class_class_members);
972 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000973
974 for (int i = 0; i < cl->class_obj_member_count; ++i)
975 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000976 ocmember_T *m = &cl->class_obj_members[i];
977 vim_free(m->ocm_name);
978 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000979 }
980 vim_free(cl->class_obj_members);
981
Bram Moolenaarec8b74f2023-01-01 14:11:27 +0000982 for (int i = 0; i < cl->class_class_function_count; ++i)
983 {
984 ufunc_T *uf = cl->class_class_functions[i];
985 func_clear_free(uf, FALSE);
986 }
987 vim_free(cl->class_class_functions);
988
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000989 for (int i = 0; i < cl->class_obj_method_count; ++i)
990 {
991 ufunc_T *uf = cl->class_obj_methods[i];
992 func_clear_free(uf, FALSE);
993 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000994 vim_free(cl->class_obj_methods);
995
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000996 clear_type_list(&cl->class_type_list);
997
998 vim_free(cl);
999 }
1000}
1001
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001002static object_T *first_object = NULL;
1003
1004/*
1005 * Call this function when an object has been created. It will be added to the
1006 * list headed by "first_object".
1007 */
1008 void
1009object_created(object_T *obj)
1010{
1011 if (first_object != NULL)
1012 {
1013 obj->obj_next_used = first_object;
1014 first_object->obj_prev_used = obj;
1015 }
1016 first_object = obj;
1017}
1018
1019/*
1020 * Call this function when an object has been cleared and is about to be freed.
1021 * It is removed from the list headed by "first_object".
1022 */
1023 void
1024object_cleared(object_T *obj)
1025{
1026 if (obj->obj_next_used != NULL)
1027 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1028 if (obj->obj_prev_used != NULL)
1029 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1030 else if (first_object == obj)
1031 first_object = obj->obj_next_used;
1032}
1033
1034/*
1035 * Go through the list of all objects and free items without "copyID".
1036 */
1037 int
1038object_free_nonref(int copyID)
1039{
1040 int did_free = FALSE;
1041 object_T *next_obj;
1042
1043 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
1044 {
1045 next_obj = obj->obj_next_used;
1046 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1047 {
1048 // Free the object and items it contains.
1049 object_clear(obj);
1050 did_free = TRUE;
1051 }
1052 }
1053
1054 return did_free;
1055}
1056
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001057
1058#endif // FEAT_EVAL