blob: 92e3c6316b69691faa6fe5185554469930833137 [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
Bram Moolenaar554d0312023-01-05 19:59:18 +000029 * one. For an interface "init_expr" is NULL.
Bram Moolenaard505d172022-12-18 21:42:55 +000030 */
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)
Bram Moolenaar554d0312023-01-05 19:59:18 +0000122 {
123 if (init_expr == NULL)
124 {
125 emsg(_(e_cannot_initialize_member_in_interface));
126 return FAIL;
127 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000128 *init_expr = vim_strnsave(expr_start, expr_end - expr_start);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000129 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000130 return OK;
131}
132
133/*
134 * Add a member to an object or a class.
135 * Returns OK when successful, "init_expr" will be consumed then.
136 * Returns FAIL otherwise, caller might need to free "init_expr".
137 */
138 static int
139add_member(
140 garray_T *gap,
141 char_u *varname,
142 char_u *varname_end,
143 int has_public,
144 type_T *type,
145 char_u *init_expr)
146{
147 if (ga_grow(gap, 1) == FAIL)
148 return FAIL;
149 ocmember_T *m = ((ocmember_T *)gap->ga_data) + gap->ga_len;
150 m->ocm_name = vim_strnsave(varname, varname_end - varname);
151 m->ocm_access = has_public ? ACCESS_ALL
152 : *varname == '_' ? ACCESS_PRIVATE : ACCESS_READ;
153 m->ocm_type = type;
154 if (init_expr != NULL)
155 m->ocm_init = init_expr;
156 ++gap->ga_len;
157 return OK;
158}
159
160/*
161 * Move the class or object members found while parsing a class into the class.
162 * "gap" contains the found members.
163 * "members" will be set to the newly allocated array of members and
164 * "member_count" set to the number of members.
165 * Returns OK or FAIL.
166 */
167 static int
168add_members_to_class(
169 garray_T *gap,
170 ocmember_T **members,
171 int *member_count)
172{
173 *member_count = gap->ga_len;
174 *members = gap->ga_len == 0 ? NULL : ALLOC_MULT(ocmember_T, gap->ga_len);
175 if (gap->ga_len > 0 && *members == NULL)
176 return FAIL;
Bram Moolenaar8efdcee2022-12-19 12:18:09 +0000177 if (gap->ga_len > 0)
178 mch_memmove(*members, gap->ga_data, sizeof(ocmember_T) * gap->ga_len);
Bram Moolenaard505d172022-12-18 21:42:55 +0000179 VIM_CLEAR(gap->ga_data);
180 return OK;
181}
182
183/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000184 * Handle ":class" and ":abstract class" up to ":endclass".
Bram Moolenaar554d0312023-01-05 19:59:18 +0000185 * Handle ":interface" up to ":endinterface".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000186 */
187 void
188ex_class(exarg_T *eap)
189{
Bram Moolenaar554d0312023-01-05 19:59:18 +0000190 int is_class = eap->cmdidx == CMD_class; // FALSE for :interface
191
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000192 if (!current_script_is_vim9()
193 || (cmdmod.cmod_flags & CMOD_LEGACY)
194 || !getline_equal(eap->getline, eap->cookie, getsourceline))
195 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000196 if (is_class)
197 emsg(_(e_class_can_only_be_defined_in_vim9_script));
198 else
199 emsg(_(e_interface_can_only_be_defined_in_vim9_script));
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000200 return;
201 }
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000202
203 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000204 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000205 if (is_abstract)
206 {
207 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
208 {
209 semsg(_(e_invalid_argument_str), arg);
210 return;
211 }
212 arg = skipwhite(arg + 5);
213 }
214
215 if (!ASCII_ISUPPER(*arg))
216 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000217 if (is_class)
218 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
219 else
220 semsg(_(e_interface_name_must_start_with_uppercase_letter_str),
221 arg);
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000222 return;
223 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000224 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
225 if (!IS_WHITE_OR_NUL(*name_end))
226 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000227 semsg(_(e_white_space_required_after_name_str), arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000228 return;
229 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000230 char_u *name_start = arg;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000231
232 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000233 // generics: <Tkey, Tentry>
Bram Moolenaard505d172022-12-18 21:42:55 +0000234 // handle "is_export" if it is set
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000235
Bram Moolenaar94674f22023-01-06 18:42:20 +0000236 // Names for "implements SomeInterface"
237 garray_T ga_impl;
238 ga_init2(&ga_impl, sizeof(char_u *), 5);
239
240 arg = skipwhite(name_end);
241 while (*arg != NUL && *arg != '#' && *arg != '\n')
242 {
243 // TODO:
244 // extends SomeClass
245 // specifies SomeInterface
246 if (STRNCMP(arg, "implements", 10) == 0 && IS_WHITE_OR_NUL(arg[10]))
247 {
248 arg = skipwhite(arg + 10);
249 char_u *impl_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
250 if (!IS_WHITE_OR_NUL(*impl_end))
251 {
252 semsg(_(e_white_space_required_after_name_str), arg);
253 goto early_ret;
254 }
255 char_u *iname = vim_strnsave(arg, impl_end - arg);
256 if (iname == NULL)
257 goto early_ret;
258 if (ga_add_string(&ga_impl, iname) == FAIL)
259 {
260 vim_free(iname);
261 goto early_ret;
262 }
263 arg = skipwhite(impl_end);
264 }
265 else
266 {
267 semsg(_(e_trailing_characters_str), arg);
268early_ret:
269 ga_clear_strings(&ga_impl);
270 return;
271 }
272 }
273
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000274 garray_T type_list; // list of pointers to allocated types
275 ga_init2(&type_list, sizeof(type_T *), 10);
276
Bram Moolenaard505d172022-12-18 21:42:55 +0000277 // Growarray with class members declared in the class.
278 garray_T classmembers;
279 ga_init2(&classmembers, sizeof(ocmember_T), 10);
280
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000281 // Growarray with functions declared in the class.
282 garray_T classfunctions;
283 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +0000284
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000285 // Growarray with object members declared in the class.
286 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +0000287 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000288
289 // Growarray with object methods declared in the class.
290 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000291 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000292
293 /*
Bram Moolenaar554d0312023-01-05 19:59:18 +0000294 * Go over the body of the class/interface until "endclass" or
295 * "endinterface" is found.
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000296 */
297 char_u *theline = NULL;
298 int success = FALSE;
299 for (;;)
300 {
301 vim_free(theline);
302 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
303 if (theline == NULL)
304 break;
305 char_u *line = skipwhite(theline);
306
Bram Moolenaar418b5472022-12-20 13:38:22 +0000307 // Skip empty and comment lines.
308 if (*line == NUL)
309 continue;
310 if (*line == '#')
311 {
312 if (vim9_bad_comment(line))
313 break;
314 continue;
315 }
316
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000317 char_u *p = line;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000318 char *end_name = is_class ? "endclass" : "endinterface";
319 if (checkforcmd(&p, end_name, is_class ? 4 : 5))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000320 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000321 if (STRNCMP(line, end_name, is_class ? 8 : 12) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000322 semsg(_(e_command_cannot_be_shortened_str), line);
323 else if (*p == '|' || !ends_excmd2(line, p))
324 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +0000325 else
326 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000327 break;
328 }
Bram Moolenaar554d0312023-01-05 19:59:18 +0000329 char *wrong_name = is_class ? "endinterface" : "endclass";
330 if (checkforcmd(&p, wrong_name, is_class ? 5 : 4))
331 {
332 semsg(_(e_invalid_command_str), line);
333 break;
334 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000335
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000336 int has_public = FALSE;
337 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000338 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000339 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000340 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000341 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000342 break;
343 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000344 has_public = TRUE;
345 p = skipwhite(line + 6);
346
Bram Moolenaard505d172022-12-18 21:42:55 +0000347 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000348 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000349 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000350 break;
351 }
352 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000353
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000354 int has_static = FALSE;
355 char_u *ps = p;
356 if (checkforcmd(&p, "static", 4))
357 {
358 if (STRNCMP(ps, "static", 6) != 0)
359 {
360 semsg(_(e_command_cannot_be_shortened_str), ps);
361 break;
362 }
363 has_static = TRUE;
364 p = skipwhite(ps + 6);
365 }
366
Bram Moolenaard505d172022-12-18 21:42:55 +0000367 // object members (public, read access, private):
368 // "this._varname"
369 // "this.varname"
370 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000371 if (STRNCMP(p, "this", 4) == 0)
372 {
373 if (p[4] != '.' || !eval_isnamec1(p[5]))
374 {
375 semsg(_(e_invalid_object_member_declaration_str), p);
376 break;
377 }
378 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +0000379 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +0000380 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +0000381 char_u *init_expr = NULL;
382 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000383 &varname_end, &type_list, &type,
384 is_class ? &init_expr: NULL) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000385 break;
386 if (add_member(&objmembers, varname, varname_end,
387 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000388 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000389 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000390 break;
391 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000392 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000393
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000394 // constructors:
395 // def new()
396 // enddef
397 // def newOther()
398 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000399 // object methods and class functions:
400 // def SomeMethod()
401 // enddef
402 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000403 // enddef
404 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000405 // def <Tval> someMethod()
406 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000407 else if (checkforcmd(&p, "def", 3))
408 {
409 exarg_T ea;
410 garray_T lines_to_free;
411
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000412 // TODO: error for "public static def Func()"?
413
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000414 CLEAR_FIELD(ea);
415 ea.cmd = line;
416 ea.arg = p;
417 ea.cmdidx = CMD_def;
418 ea.getline = eap->getline;
419 ea.cookie = eap->cookie;
420
421 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000422 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free,
423 is_class ? CF_CLASS : CF_INTERFACE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000424 ga_clear_strings(&lines_to_free);
425
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000426 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000427 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000428 int is_new = STRNCMP(uf->uf_name, "new", 3) == 0;
429 garray_T *fgap = has_static || is_new
430 ? &classfunctions : &objmethods;
431 if (ga_grow(fgap, 1) == OK)
432 {
433 if (is_new)
434 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000435
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000436 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
437 ++fgap->ga_len;
438 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000439 }
440 }
441
442 // class members
443 else if (has_static)
444 {
445 // class members (public, read access, private):
446 // "static _varname"
447 // "static varname"
448 // "public static varname"
449 char_u *varname = p;
450 char_u *varname_end = NULL;
451 type_T *type = NULL;
452 char_u *init_expr = NULL;
453 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000454 &varname_end, &type_list, &type,
455 is_class ? &init_expr : NULL) == FAIL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000456 break;
457 if (add_member(&classmembers, varname, varname_end,
458 has_public, type, init_expr) == FAIL)
459 {
460 vim_free(init_expr);
461 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000462 }
463 }
464
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000465 else
466 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000467 if (is_class)
468 semsg(_(e_not_valid_command_in_class_str), line);
469 else
470 semsg(_(e_not_valid_command_in_interface_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000471 break;
472 }
473 }
474 vim_free(theline);
475
Bram Moolenaar94674f22023-01-06 18:42:20 +0000476 // Check a few things before defining the class.
477 if (success && ga_impl.ga_len > 0)
478 {
479 // Check all "implements" entries are valid and correct.
480 for (int i = 0; i < ga_impl.ga_len && success; ++i)
481 {
482 char_u *impl = ((char_u **)ga_impl.ga_data)[i];
483 typval_T tv;
484 tv.v_type = VAR_UNKNOWN;
485 if (eval_variable(impl, 0, 0, &tv, NULL,
486 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT) == FAIL)
487 {
488 semsg(_(e_interface_name_not_found_str), impl);
489 success = FALSE;
490 break;
491 }
492
493 if (tv.v_type != VAR_CLASS
494 || tv.vval.v_class == NULL
495 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0)
496 {
497 semsg(_(e_not_valid_interface_str), impl);
498 success = FALSE;
499 }
500
501 // check the members of the interface match the members of the class
502 class_T *ifcl = tv.vval.v_class;
503 for (int loop = 1; loop <= 2 && success; ++loop)
504 {
505 // loop == 1: check class members
506 // loop == 2: check object members
507 int if_count = loop == 1 ? ifcl->class_class_member_count
508 : ifcl->class_obj_member_count;
509 if (if_count == 0)
510 continue;
511 ocmember_T *if_ms = loop == 1 ? ifcl->class_class_members
512 : ifcl->class_obj_members;
513 ocmember_T *cl_ms = (ocmember_T *)(loop == 1
514 ? classmembers.ga_data
515 : objmembers.ga_data);
516 int cl_count = loop == 1 ? classmembers.ga_len
517 : objmembers.ga_len;
518 for (int if_i = 0; if_i < if_count; ++if_i)
519 {
520 int cl_i;
521 for (cl_i = 0; cl_i < cl_count; ++cl_i)
522 {
523 ocmember_T *m = &cl_ms[cl_i];
524 if (STRCMP(if_ms[if_i].ocm_name, m->ocm_name) == 0)
525 {
526 // TODO: check type
527 break;
528 }
529 }
530 if (cl_i == cl_count)
531 {
532 semsg(_(e_member_str_of_interface_str_not_implemented),
533 if_ms[if_i].ocm_name, impl);
534 success = FALSE;
535 break;
536 }
537 }
538 }
539
540 // check the functions/methods of the interface match the
541 // functions/methods of the class
542 for (int loop = 1; loop <= 2 && success; ++loop)
543 {
544 // loop == 1: check class functions
545 // loop == 2: check object methods
546 int if_count = loop == 1 ? ifcl->class_class_function_count
547 : ifcl->class_obj_method_count;
548 if (if_count == 0)
549 continue;
550 ufunc_T **if_fp = loop == 1 ? ifcl->class_class_functions
551 : ifcl->class_obj_methods;
552 ufunc_T **cl_fp = (ufunc_T **)(loop == 1
553 ? classfunctions.ga_data
554 : objmethods.ga_data);
555 int cl_count = loop == 1 ? classfunctions.ga_len
556 : objmethods.ga_len;
557 for (int if_i = 0; if_i < if_count; ++if_i)
558 {
559 char_u *if_name = if_fp[if_i]->uf_name;
560 int cl_i;
561 for (cl_i = 0; cl_i < cl_count; ++cl_i)
562 {
563 char_u *cl_name = cl_fp[cl_i]->uf_name;
564 if (STRCMP(if_name, cl_name) == 0)
565 {
566 // TODO: check return and argument types
567 break;
568 }
569 }
570 if (cl_i == cl_count)
571 {
572 semsg(_(e_function_str_of_interface_str_not_implemented),
573 if_name, impl);
574 success = FALSE;
575 break;
576 }
577 }
578 }
579
580 clear_tv(&tv);
581 }
582 }
583
Bram Moolenaareb533502022-12-14 15:06:11 +0000584 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000585 if (success)
586 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000587 // "endclass" encountered without failures: Create the class.
588
Bram Moolenaareb533502022-12-14 15:06:11 +0000589 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000590 if (cl == NULL)
591 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000592 if (!is_class)
593 cl->class_flags = CLASS_INTERFACE;
594
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000595 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +0000596 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +0000597 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000598 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000599
Bram Moolenaar94674f22023-01-06 18:42:20 +0000600 if (ga_impl.ga_len > 0)
601 {
602 // Move the "implements" names into the class.
603 cl->class_interface_count = ga_impl.ga_len;
604 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
605 if (cl->class_interfaces == NULL)
606 goto cleanup;
607 for (int i = 0; i < ga_impl.ga_len; ++i)
608 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
Bram Moolenaar7d4d87b2023-01-06 18:59:08 +0000609 VIM_CLEAR(ga_impl.ga_data);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000610 ga_impl.ga_len = 0;
611 }
612
Bram Moolenaard505d172022-12-18 21:42:55 +0000613 // Add class and object members to "cl".
614 if (add_members_to_class(&classmembers,
615 &cl->class_class_members,
616 &cl->class_class_member_count) == FAIL
617 || add_members_to_class(&objmembers,
618 &cl->class_obj_members,
619 &cl->class_obj_member_count) == FAIL)
620 goto cleanup;
621
Bram Moolenaar554d0312023-01-05 19:59:18 +0000622 if (is_class && cl->class_class_member_count > 0)
Bram Moolenaard505d172022-12-18 21:42:55 +0000623 {
624 // Allocate a typval for each class member and initialize it.
625 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
626 cl->class_class_member_count);
627 if (cl->class_members_tv != NULL)
628 for (int i = 0; i < cl->class_class_member_count; ++i)
629 {
630 ocmember_T *m = &cl->class_class_members[i];
631 typval_T *tv = &cl->class_members_tv[i];
632 if (m->ocm_init != NULL)
633 {
634 typval_T *etv = eval_expr(m->ocm_init, eap);
635 if (etv != NULL)
636 {
637 *tv = *etv;
638 vim_free(etv);
639 }
640 }
641 else
642 {
643 // TODO: proper default value
644 tv->v_type = m->ocm_type->tt_type;
645 tv->vval.v_string = NULL;
646 }
647 }
648 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000649
650 int have_new = FALSE;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000651 for (int i = 0; i < classfunctions.ga_len; ++i)
652 if (STRCMP(((ufunc_T **)classfunctions.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000653 "new") == 0)
654 {
655 have_new = TRUE;
656 break;
657 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000658 if (is_class && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000659 {
660 // No new() method was defined, add the default constructor.
661 garray_T fga;
662 ga_init2(&fga, 1, 1000);
663 ga_concat(&fga, (char_u *)"new(");
664 for (int i = 0; i < cl->class_obj_member_count; ++i)
665 {
666 if (i > 0)
667 ga_concat(&fga, (char_u *)", ");
668 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000669 ocmember_T *m = cl->class_obj_members + i;
670 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000671 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000672 }
673 ga_concat(&fga, (char_u *)")\nenddef\n");
674 ga_append(&fga, NUL);
675
676 exarg_T fea;
677 CLEAR_FIELD(fea);
678 fea.cmdidx = CMD_def;
679 fea.cmd = fea.arg = fga.ga_data;
680
681 garray_T lines_to_free;
682 ga_init2(&lines_to_free, sizeof(char_u *), 50);
683
Bram Moolenaar2c011312023-01-07 10:51:30 +0000684 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000685
686 ga_clear_strings(&lines_to_free);
687 vim_free(fga.ga_data);
688
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000689 if (nf != NULL && ga_grow(&classfunctions, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000690 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000691 ((ufunc_T **)classfunctions.ga_data)[classfunctions.ga_len] = nf;
692 ++classfunctions.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000693
694 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000695 nf->uf_ret_type = get_type_ptr(&type_list);
696 if (nf->uf_ret_type != NULL)
697 {
698 nf->uf_ret_type->tt_type = VAR_OBJECT;
699 nf->uf_ret_type->tt_member = (type_T *)cl;
700 nf->uf_ret_type->tt_argcount = 0;
701 nf->uf_ret_type->tt_args = NULL;
702 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000703 }
704 }
705
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000706 // loop 1: class functions, loop 2: object methods
707 for (int loop = 1; loop <= 2; ++loop)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000708 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000709 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
710 int *fcount = loop == 1 ? &cl->class_class_function_count
711 : &cl->class_obj_method_count;
712 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
713 : &cl->class_obj_methods;
714
715 *fcount = gap->ga_len;
716 if (gap->ga_len == 0)
717 {
718 *fup = NULL;
719 continue;
720 }
721 *fup = ALLOC_MULT(ufunc_T *, gap->ga_len);
722 if (*fup == NULL)
723 goto cleanup;
724 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
725 vim_free(gap->ga_data);
726
727 // Set the class pointer on all the object methods.
728 for (int i = 0; i < gap->ga_len; ++i)
729 {
730 ufunc_T *fp = (*fup)[i];
731 fp->uf_class = cl;
732 if (loop == 2)
733 fp->uf_flags |= FC_OBJECT;
734 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000735 }
736
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000737 cl->class_type.tt_type = VAR_CLASS;
738 cl->class_type.tt_member = (type_T *)cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000739 cl->class_object_type.tt_type = VAR_OBJECT;
740 cl->class_object_type.tt_member = (type_T *)cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000741 cl->class_type_list = type_list;
742
743 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +0000744 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000745
746 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +0000747 // TODO: handle other context, e.g. in a function
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000748 typval_T tv;
749 tv.v_type = VAR_CLASS;
750 tv.vval.v_class = cl;
751 set_var_const(cl->class_name, current_sctx.sc_sid,
752 NULL, &tv, FALSE, ASSIGN_DECL, 0);
753 return;
754 }
755
756cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +0000757 if (cl != NULL)
758 {
759 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000760 vim_free(cl->class_class_functions);
Bram Moolenaareb533502022-12-14 15:06:11 +0000761 vim_free(cl->class_obj_members);
762 vim_free(cl->class_obj_methods);
763 vim_free(cl);
764 }
765
Bram Moolenaar94674f22023-01-06 18:42:20 +0000766 ga_clear_strings(&ga_impl);
767
Bram Moolenaard505d172022-12-18 21:42:55 +0000768 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000769 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000770 garray_T *gap = round == 1 ? &classmembers : &objmembers;
771 if (gap->ga_len == 0 || gap->ga_data == NULL)
772 continue;
773
774 for (int i = 0; i < gap->ga_len; ++i)
775 {
776 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
777 vim_free(m->ocm_name);
778 vim_free(m->ocm_init);
779 }
780 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000781 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000782
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000783 for (int i = 0; i < objmethods.ga_len; ++i)
784 {
785 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
786 func_clear_free(uf, FALSE);
787 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000788 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000789
790 for (int i = 0; i < classfunctions.ga_len; ++i)
791 {
792 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
793 func_clear_free(uf, FALSE);
794 }
795 ga_clear(&classfunctions);
796
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000797 clear_type_list(&type_list);
798}
799
800/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000801 * Find member "name" in class "cl", set "member_idx" to the member index and
802 * return its type.
803 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000804 */
805 type_T *
806class_member_type(
807 class_T *cl,
808 char_u *name,
809 char_u *name_end,
810 int *member_idx)
811{
812 *member_idx = -1; // not found (yet)
813 size_t len = name_end - name;
814
815 for (int i = 0; i < cl->class_obj_member_count; ++i)
816 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000817 ocmember_T *m = cl->class_obj_members + i;
818 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000819 {
820 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +0000821 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000822 }
823 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000824
825 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000826 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000827}
828
829/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000830 * Handle ":enum" up to ":endenum".
831 */
832 void
833ex_enum(exarg_T *eap UNUSED)
834{
835 // TODO
836}
837
838/*
839 * Handle ":type".
840 */
841 void
842ex_type(exarg_T *eap UNUSED)
843{
844 // TODO
845}
846
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000847/*
848 * Evaluate what comes after a class:
849 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000850 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000851 * - class constructor: SomeClass.new()
852 * - object member: someObject.varname
853 * - object method: someObject.SomeMethod()
854 *
855 * "*arg" points to the '.'.
856 * "*arg" is advanced to after the member name or method call.
857 *
858 * Returns FAIL or OK.
859 */
860 int
861class_object_index(
862 char_u **arg,
863 typval_T *rettv,
864 evalarg_T *evalarg,
865 int verbose UNUSED) // give error messages
866{
867 // int evaluate = evalarg != NULL
868 // && (evalarg->eval_flags & EVAL_EVALUATE);
869
870 if (VIM_ISWHITE((*arg)[1]))
871 {
872 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
873 return FAIL;
874 }
875
876 ++*arg;
877 char_u *name = *arg;
878 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
879 if (name_end == name)
880 return FAIL;
881 size_t len = name_end - name;
882
883 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
884 : rettv->vval.v_object->obj_class;
885 if (*name_end == '(')
886 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000887 int on_class = rettv->v_type == VAR_CLASS;
888 int count = on_class ? cl->class_class_function_count
889 : cl->class_obj_method_count;
890 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000891 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000892 ufunc_T *fp = on_class ? cl->class_class_functions[i]
893 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +0000894 // Use a separate pointer to avoid that ASAN complains about
895 // uf_name[] only being 4 characters.
896 char_u *ufname = (char_u *)fp->uf_name;
897 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000898 {
899 typval_T argvars[MAX_FUNC_ARGS + 1];
900 int argcount = 0;
901
902 char_u *argp = name_end;
903 int ret = get_func_arguments(&argp, evalarg, 0,
904 argvars, &argcount);
905 if (ret == FAIL)
906 return FAIL;
907
908 funcexe_T funcexe;
909 CLEAR_FIELD(funcexe);
910 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000911 if (rettv->v_type == VAR_OBJECT)
912 {
913 funcexe.fe_object = rettv->vval.v_object;
914 ++funcexe.fe_object->obj_refcount;
915 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000916
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000917 // Clear the class or object after calling the function, in
918 // case the refcount is one.
919 typval_T tv_tofree = *rettv;
920 rettv->v_type = VAR_UNKNOWN;
921
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000922 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000923 int error = call_user_func_check(fp, argcount, argvars,
924 rettv, &funcexe, NULL);
925
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000926 // Clear the previous rettv and the arguments.
927 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000928 for (int idx = 0; idx < argcount; ++idx)
929 clear_tv(&argvars[idx]);
930
931 if (error != FCERR_NONE)
932 {
933 user_func_error(error, printable_func_name(fp),
934 funcexe.fe_found_var);
935 return FAIL;
936 }
937 *arg = argp;
938 return OK;
939 }
940 }
941
942 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
943 }
944
945 else if (rettv->v_type == VAR_OBJECT)
946 {
947 for (int i = 0; i < cl->class_obj_member_count; ++i)
948 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000949 ocmember_T *m = &cl->class_obj_members[i];
950 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000951 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000952 if (*name == '_')
953 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000954 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000955 return FAIL;
956 }
957
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000958 // The object only contains a pointer to the class, the member
959 // values array follows right after that.
960 object_T *obj = rettv->vval.v_object;
961 typval_T *tv = (typval_T *)(obj + 1) + i;
962 copy_tv(tv, rettv);
963 object_unref(obj);
964
965 *arg = name_end;
966 return OK;
967 }
968 }
969
970 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
971 }
972
Bram Moolenaard505d172022-12-18 21:42:55 +0000973 else if (rettv->v_type == VAR_CLASS)
974 {
975 // class member
976 for (int i = 0; i < cl->class_class_member_count; ++i)
977 {
978 ocmember_T *m = &cl->class_class_members[i];
979 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
980 {
981 if (*name == '_')
982 {
983 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
984 return FAIL;
985 }
986
987 typval_T *tv = &cl->class_members_tv[i];
988 copy_tv(tv, rettv);
989 class_unref(cl);
990
991 *arg = name_end;
992 return OK;
993 }
994 }
995
996 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
997 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000998
999 return FAIL;
1000}
1001
1002/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001003 * If "arg" points to a class or object method, return it.
1004 * Otherwise return NULL.
1005 */
1006 ufunc_T *
1007find_class_func(char_u **arg)
1008{
1009 char_u *name = *arg;
1010 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1011 if (name_end == name || *name_end != '.')
1012 return NULL;
1013
1014 size_t len = name_end - name;
1015 typval_T tv;
1016 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001017 if (eval_variable(name, (int)len,
1018 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001019 return NULL;
1020 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001021 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001022
1023 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1024 : tv.vval.v_object->obj_class;
1025 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001026 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001027 char_u *fname = name_end + 1;
1028 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1029 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001030 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001031 len = fname_end - fname;
1032
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001033 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1034 : cl->class_obj_method_count;
1035 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1036 : cl->class_obj_methods;
1037 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001038 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001039 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001040 // Use a separate pointer to avoid that ASAN complains about
1041 // uf_name[] only being 4 characters.
1042 char_u *ufname = (char_u *)fp->uf_name;
1043 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001044 {
1045 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001046 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001047 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001048 }
1049
Bram Moolenaareb533502022-12-14 15:06:11 +00001050fail_after_eval:
1051 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001052 return NULL;
1053}
1054
1055/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001056 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1057 * index in class.class_class_members[].
1058 * If "cl_ret" is not NULL set it to the class.
1059 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001060 */
1061 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001062class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001063{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001064 if (cctx == NULL || cctx->ctx_ufunc == NULL
1065 || cctx->ctx_ufunc->uf_class == NULL)
1066 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001067 class_T *cl = cctx->ctx_ufunc->uf_class;
1068
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001069 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001070 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001071 ocmember_T *m = &cl->class_class_members[i];
1072 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001073 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001074 if (cl_ret != NULL)
1075 *cl_ret = cl;
1076 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001077 }
1078 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001079 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001080}
1081
1082/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001083 * Make a copy of an object.
1084 */
1085 void
1086copy_object(typval_T *from, typval_T *to)
1087{
1088 *to = *from;
1089 if (to->vval.v_object != NULL)
1090 ++to->vval.v_object->obj_refcount;
1091}
1092
1093/*
1094 * Free an object.
1095 */
1096 static void
1097object_clear(object_T *obj)
1098{
1099 class_T *cl = obj->obj_class;
1100
1101 // the member values are just after the object structure
1102 typval_T *tv = (typval_T *)(obj + 1);
1103 for (int i = 0; i < cl->class_obj_member_count; ++i)
1104 clear_tv(tv + i);
1105
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001106 // Remove from the list headed by "first_object".
1107 object_cleared(obj);
1108
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001109 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001110 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001111}
1112
1113/*
1114 * Unreference an object.
1115 */
1116 void
1117object_unref(object_T *obj)
1118{
1119 if (obj != NULL && --obj->obj_refcount <= 0)
1120 object_clear(obj);
1121}
1122
1123/*
1124 * Make a copy of a class.
1125 */
1126 void
1127copy_class(typval_T *from, typval_T *to)
1128{
1129 *to = *from;
1130 if (to->vval.v_class != NULL)
1131 ++to->vval.v_class->class_refcount;
1132}
1133
1134/*
1135 * Unreference a class. Free it when the reference count goes down to zero.
1136 */
1137 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001138class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001139{
Bram Moolenaard505d172022-12-18 21:42:55 +00001140 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001141 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001142 // Freeing what the class contains may recursively come back here.
1143 // Clear "class_name" first, if it is NULL the class does not need to
1144 // be freed.
1145 VIM_CLEAR(cl->class_name);
1146
Bram Moolenaar94674f22023-01-06 18:42:20 +00001147 for (int i = 0; i < cl->class_interface_count; ++i)
1148 vim_free(((char_u **)cl->class_interfaces)[i]);
1149 vim_free(cl->class_interfaces);
1150
Bram Moolenaard505d172022-12-18 21:42:55 +00001151 for (int i = 0; i < cl->class_class_member_count; ++i)
1152 {
1153 ocmember_T *m = &cl->class_class_members[i];
1154 vim_free(m->ocm_name);
1155 vim_free(m->ocm_init);
1156 if (cl->class_members_tv != NULL)
1157 clear_tv(&cl->class_members_tv[i]);
1158 }
1159 vim_free(cl->class_class_members);
1160 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001161
1162 for (int i = 0; i < cl->class_obj_member_count; ++i)
1163 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001164 ocmember_T *m = &cl->class_obj_members[i];
1165 vim_free(m->ocm_name);
1166 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001167 }
1168 vim_free(cl->class_obj_members);
1169
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001170 for (int i = 0; i < cl->class_class_function_count; ++i)
1171 {
1172 ufunc_T *uf = cl->class_class_functions[i];
1173 func_clear_free(uf, FALSE);
1174 }
1175 vim_free(cl->class_class_functions);
1176
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001177 for (int i = 0; i < cl->class_obj_method_count; ++i)
1178 {
1179 ufunc_T *uf = cl->class_obj_methods[i];
1180 func_clear_free(uf, FALSE);
1181 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001182 vim_free(cl->class_obj_methods);
1183
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001184 clear_type_list(&cl->class_type_list);
1185
1186 vim_free(cl);
1187 }
1188}
1189
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001190static object_T *first_object = NULL;
1191
1192/*
1193 * Call this function when an object has been created. It will be added to the
1194 * list headed by "first_object".
1195 */
1196 void
1197object_created(object_T *obj)
1198{
1199 if (first_object != NULL)
1200 {
1201 obj->obj_next_used = first_object;
1202 first_object->obj_prev_used = obj;
1203 }
1204 first_object = obj;
1205}
1206
1207/*
1208 * Call this function when an object has been cleared and is about to be freed.
1209 * It is removed from the list headed by "first_object".
1210 */
1211 void
1212object_cleared(object_T *obj)
1213{
1214 if (obj->obj_next_used != NULL)
1215 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1216 if (obj->obj_prev_used != NULL)
1217 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1218 else if (first_object == obj)
1219 first_object = obj->obj_next_used;
1220}
1221
1222/*
1223 * Go through the list of all objects and free items without "copyID".
1224 */
1225 int
1226object_free_nonref(int copyID)
1227{
1228 int did_free = FALSE;
1229 object_T *next_obj;
1230
1231 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
1232 {
1233 next_obj = obj->obj_next_used;
1234 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1235 {
1236 // Free the object and items it contains.
1237 object_clear(obj);
1238 did_free = TRUE;
1239 }
1240 }
1241
1242 return did_free;
1243}
1244
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001245
1246#endif // FEAT_EVAL