blob: 30a3b664719c017bb438216e6f95efdcd0c89e4f [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
Yegappan Lakshmanane651e112023-09-04 07:51:01 +020024static class_T *first_class = NULL;
25static class_T *next_nonref_class = NULL;
26
27/*
28 * Call this function when a class has been created. It will be added to the
29 * list headed by "first_class".
30 */
31 static void
32class_created(class_T *cl)
33{
34 if (first_class != NULL)
35 {
36 cl->class_next_used = first_class;
37 first_class->class_prev_used = cl;
38 }
39 first_class = cl;
40}
41
42/*
43 * Call this function when a class has been cleared and is about to be freed.
44 * It is removed from the list headed by "first_class".
45 */
46 static void
47class_cleared(class_T *cl)
48{
49 if (cl->class_next_used != NULL)
50 cl->class_next_used->class_prev_used = cl->class_prev_used;
51 if (cl->class_prev_used != NULL)
52 cl->class_prev_used->class_next_used = cl->class_next_used;
53 else if (first_class == cl)
54 first_class = cl->class_next_used;
55
56 // update the next class to check if needed
57 if (cl == next_nonref_class)
58 next_nonref_class = cl->class_next_used;
59}
60
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000061/*
Bram Moolenaard505d172022-12-18 21:42:55 +000062 * Parse a member declaration, both object and class member.
63 * Returns OK or FAIL. When OK then "varname_end" is set to just after the
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +020064 * variable name and "type_ret" is set to the declared or detected type.
Bram Moolenaard505d172022-12-18 21:42:55 +000065 * "init_expr" is set to the initialisation expression (allocated), if there is
Bram Moolenaar554d0312023-01-05 19:59:18 +000066 * one. For an interface "init_expr" is NULL.
Bram Moolenaard505d172022-12-18 21:42:55 +000067 */
68 static int
69parse_member(
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +020070 exarg_T *eap,
71 char_u *line,
72 char_u *varname,
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +020073 int has_public, // TRUE if "public" seen before "varname"
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +020074 char_u **varname_end,
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +020075 garray_T *type_list,
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +020076 type_T **type_ret,
77 char_u **init_expr)
Bram Moolenaard505d172022-12-18 21:42:55 +000078{
79 *varname_end = to_name_end(varname, FALSE);
80 if (*varname == '_' && has_public)
81 {
82 semsg(_(e_public_member_name_cannot_start_with_underscore_str), line);
83 return FAIL;
84 }
85
86 char_u *colon = skipwhite(*varname_end);
87 char_u *type_arg = colon;
88 type_T *type = NULL;
89 if (*colon == ':')
90 {
91 if (VIM_ISWHITE(**varname_end))
92 {
93 semsg(_(e_no_white_space_allowed_before_colon_str), varname);
94 return FAIL;
95 }
96 if (!VIM_ISWHITE(colon[1]))
97 {
98 semsg(_(e_white_space_required_after_str_str), ":", varname);
99 return FAIL;
100 }
101 type_arg = skipwhite(colon + 1);
102 type = parse_type(&type_arg, type_list, TRUE);
103 if (type == NULL)
104 return FAIL;
105 }
106
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200107 char_u *init_arg = skipwhite(type_arg);
108 if (type == NULL && *init_arg != '=')
Bram Moolenaard505d172022-12-18 21:42:55 +0000109 {
110 emsg(_(e_type_or_initialization_required));
111 return FAIL;
112 }
113
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200114 if (init_expr == NULL && *init_arg == '=')
Bram Moolenaard505d172022-12-18 21:42:55 +0000115 {
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200116 emsg(_(e_cannot_initialize_member_in_interface));
117 return FAIL;
118 }
119
120 if (*init_arg == '=')
121 {
122 evalarg_T evalarg;
123 char_u *expr_start, *expr_end;
124
125 if (!VIM_ISWHITE(init_arg[-1]) || !VIM_ISWHITE(init_arg[1]))
Bram Moolenaard505d172022-12-18 21:42:55 +0000126 {
127 semsg(_(e_white_space_required_before_and_after_str_at_str),
128 "=", type_arg);
129 return FAIL;
130 }
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200131 init_arg = skipwhite(init_arg + 1);
Bram Moolenaard505d172022-12-18 21:42:55 +0000132
Bram Moolenaard505d172022-12-18 21:42:55 +0000133 fill_evalarg_from_eap(&evalarg, eap, FALSE);
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200134 (void)skip_expr_concatenate(&init_arg, &expr_start, &expr_end, &evalarg);
Bram Moolenaard505d172022-12-18 21:42:55 +0000135
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +0200136 // No type specified for the member. Set it to "any" and the correct
137 // type will be set when the object is instantiated.
Bram Moolenaard505d172022-12-18 21:42:55 +0000138 if (type == NULL)
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200139 type = &t_any;
Bram Moolenaard505d172022-12-18 21:42:55 +0000140
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200141 *init_expr = vim_strnsave(expr_start, expr_end - expr_start);
142 // Free the memory pointed by expr_start.
Bram Moolenaard505d172022-12-18 21:42:55 +0000143 clear_evalarg(&evalarg, NULL);
144 }
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200145 else if (!valid_declaration_type(type))
Bram Moolenaard505d172022-12-18 21:42:55 +0000146 return FAIL;
147
148 *type_ret = type;
Bram Moolenaard505d172022-12-18 21:42:55 +0000149 return OK;
150}
151
152/*
153 * Add a member to an object or a class.
154 * Returns OK when successful, "init_expr" will be consumed then.
155 * Returns FAIL otherwise, caller might need to free "init_expr".
156 */
157 static int
158add_member(
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +0200159 garray_T *gap,
160 char_u *varname,
161 char_u *varname_end,
162 int has_public,
163 type_T *type,
164 char_u *init_expr)
Bram Moolenaard505d172022-12-18 21:42:55 +0000165{
166 if (ga_grow(gap, 1) == FAIL)
167 return FAIL;
168 ocmember_T *m = ((ocmember_T *)gap->ga_data) + gap->ga_len;
169 m->ocm_name = vim_strnsave(varname, varname_end - varname);
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +0000170 m->ocm_access = has_public ? VIM_ACCESS_ALL
171 : *varname == '_' ? VIM_ACCESS_PRIVATE : VIM_ACCESS_READ;
Bram Moolenaard505d172022-12-18 21:42:55 +0000172 m->ocm_type = type;
173 if (init_expr != NULL)
174 m->ocm_init = init_expr;
175 ++gap->ga_len;
176 return OK;
177}
178
179/*
180 * Move the class or object members found while parsing a class into the class.
181 * "gap" contains the found members.
Bram Moolenaar83677162023-01-08 19:54:10 +0000182 * "parent_members" points to the members in the parent class (if any)
183 * "parent_count" is the number of members in the parent class
Bram Moolenaard505d172022-12-18 21:42:55 +0000184 * "members" will be set to the newly allocated array of members and
185 * "member_count" set to the number of members.
186 * Returns OK or FAIL.
187 */
188 static int
189add_members_to_class(
190 garray_T *gap,
Bram Moolenaar83677162023-01-08 19:54:10 +0000191 ocmember_T *parent_members,
192 int parent_count,
Bram Moolenaard505d172022-12-18 21:42:55 +0000193 ocmember_T **members,
194 int *member_count)
195{
Bram Moolenaar83677162023-01-08 19:54:10 +0000196 *member_count = parent_count + gap->ga_len;
197 *members = *member_count == 0 ? NULL
198 : ALLOC_MULT(ocmember_T, *member_count);
199 if (*member_count > 0 && *members == NULL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000200 return FAIL;
Bram Moolenaar83677162023-01-08 19:54:10 +0000201 for (int i = 0; i < parent_count; ++i)
202 {
203 // parent members need to be copied
Bram Moolenaarae3205a2023-01-15 20:49:00 +0000204 ocmember_T *m = *members + i;
205 *m = parent_members[i];
206 m->ocm_name = vim_strsave(m->ocm_name);
207 if (m->ocm_init != NULL)
208 m->ocm_init = vim_strsave(m->ocm_init);
Bram Moolenaar83677162023-01-08 19:54:10 +0000209 }
Bram Moolenaar8efdcee2022-12-19 12:18:09 +0000210 if (gap->ga_len > 0)
Bram Moolenaar83677162023-01-08 19:54:10 +0000211 // new members are moved
212 mch_memmove(*members + parent_count,
213 gap->ga_data, sizeof(ocmember_T) * gap->ga_len);
Bram Moolenaard505d172022-12-18 21:42:55 +0000214 VIM_CLEAR(gap->ga_data);
215 return OK;
216}
217
218/*
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000219 * Convert a member index "idx" of interface "itf" to the member index of class
220 * "cl" implementing that interface.
221 */
222 int
Ernie Rael18143d32023-09-04 22:30:41 +0200223object_index_from_itf_index(class_T *itf, int is_method, int idx, class_T *cl,
224 int is_static)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000225{
Ernie Rael18143d32023-09-04 22:30:41 +0200226 if (idx >= (is_method ? itf->class_obj_method_count
227 : is_static ? itf->class_class_member_count
Bram Moolenaard0200c82023-01-28 15:19:40 +0000228 : itf->class_obj_member_count))
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000229 {
230 siemsg("index %d out of range for interface %s", idx, itf->class_name);
231 return 0;
232 }
Yegappan Lakshmanan74cc13c2023-08-13 17:41:26 +0200233
234 // If "cl" is the interface or the class that is extended, then the method
235 // index can be used directly and there is no need to search for the method
236 // index in one of the child classes.
237 if (cl == itf)
238 return idx;
239
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000240 itf2class_T *i2c;
241 for (i2c = itf->class_itf2class; i2c != NULL; i2c = i2c->i2c_next)
Bram Moolenaard0200c82023-01-28 15:19:40 +0000242 if (i2c->i2c_class == cl && i2c->i2c_is_method == is_method)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000243 break;
244 if (i2c == NULL)
245 {
246 siemsg("class %s not found on interface %s",
247 cl->class_name, itf->class_name);
248 return 0;
249 }
Ernie Rael18143d32023-09-04 22:30:41 +0200250 if (is_static)
251 {
252 // TODO: Need a table for fast lookup?
253 char_u *name = itf->class_class_members[idx].ocm_name;
254 for (int i = 0; i < i2c->i2c_class->class_class_member_count; ++i)
255 {
256 ocmember_T *m = &i2c->i2c_class->class_class_members[i];
257 if (STRCMP(name, m->ocm_name) == 0)
258 {
259 return i;
260 }
261 }
262 siemsg("class %s, interface %s, static %s not found",
263 cl->class_name, itf->class_name, name);
264 return 0;
265 }
266 else
267 {
268 // A table follows the i2c for the class
269 int *table = (int *)(i2c + 1);
270 return table[idx];
271 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000272}
273
274/*
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200275 * Check whether a class named "extends_name" is present. If the class is
276 * valid, then "extends_clp" is set with the class pointer.
277 * Returns TRUE if the class name "extends_names" is a valid class.
278 */
279 static int
280validate_extends_class(char_u *extends_name, class_T **extends_clp)
281{
282 typval_T tv;
283 int success = FALSE;
284
285 tv.v_type = VAR_UNKNOWN;
286 if (eval_variable_import(extends_name, &tv) == FAIL)
287 {
288 semsg(_(e_class_name_not_found_str), extends_name);
289 return success;
290 }
291 else
292 {
293 if (tv.v_type != VAR_CLASS
294 || tv.vval.v_class == NULL
295 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) != 0)
296 semsg(_(e_cannot_extend_str), extends_name);
297 else
298 {
299 class_T *extends_cl = tv.vval.v_class;
300 ++extends_cl->class_refcount;
301 *extends_clp = extends_cl;
302 success = TRUE;
303 }
304 clear_tv(&tv);
305 }
306
307 return success;
308}
309
310/*
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200311 * Check whether a class/object member variable in "classmembers_gap" /
312 * "objmembers_gap" is a duplicate of a member in any of the extended parent
313 * class lineage. Returns TRUE if there are no duplicates.
314 */
315 static int
316validate_extends_members(
317 garray_T *classmembers_gap,
318 garray_T *objmembers_gap,
319 class_T *extends_cl)
320{
321 for (int loop = 1; loop <= 2; ++loop)
322 {
323 // loop == 1: check class members
324 // loop == 2: check object members
325 int member_count = loop == 1 ? classmembers_gap->ga_len
326 : objmembers_gap->ga_len;
327 if (member_count == 0)
328 continue;
329 ocmember_T *members = (ocmember_T *)(loop == 1
330 ? classmembers_gap->ga_data
331 : objmembers_gap->ga_data);
332
333 // Validate each member variable
334 for (int c_i = 0; c_i < member_count; c_i++)
335 {
336 class_T *p_cl = extends_cl;
337 ocmember_T *c_m = members + c_i;
338 char_u *pstr = (*c_m->ocm_name == '_')
339 ? c_m->ocm_name + 1 : c_m->ocm_name;
340
341 // Check in all the parent classes in the lineage
342 while (p_cl != NULL)
343 {
344 int p_member_count = loop == 1
345 ? p_cl->class_class_member_count
346 : p_cl->class_obj_member_count;
347 if (p_member_count == 0)
348 continue;
349 ocmember_T *p_members = (loop == 1
350 ? p_cl->class_class_members
351 : p_cl->class_obj_members);
352
353 // Compare against all the members in the parent class
354 for (int p_i = 0; p_i < p_member_count; p_i++)
355 {
356 ocmember_T *p_m = p_members + p_i;
357 char_u *qstr = (*p_m->ocm_name == '_')
358 ? p_m->ocm_name + 1 : p_m->ocm_name;
359 if (STRCMP(pstr, qstr) == 0)
360 {
361 semsg(_(e_duplicate_member_str), c_m->ocm_name);
362 return FALSE;
363 }
364 }
365
366 p_cl = p_cl->class_extends;
367 }
368 }
369 }
370
371 return TRUE;
372}
373
374/*
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200375 * Check the members of the interface class "ifcl" match the class members
376 * ("classmembers_gap") and object members ("objmembers_gap") of a class.
377 * Returns TRUE if the class and object member names are valid.
378 */
379 static int
380validate_interface_members(
381 char_u *intf_class_name,
382 class_T *ifcl,
383 garray_T *classmembers_gap,
384 garray_T *objmembers_gap)
385{
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200386 for (int loop = 1; loop <= 2; ++loop)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200387 {
388 // loop == 1: check class members
389 // loop == 2: check object members
390 int if_count = loop == 1 ? ifcl->class_class_member_count
391 : ifcl->class_obj_member_count;
392 if (if_count == 0)
393 continue;
394 ocmember_T *if_ms = loop == 1 ? ifcl->class_class_members
395 : ifcl->class_obj_members;
396 ocmember_T *cl_ms = (ocmember_T *)(loop == 1
397 ? classmembers_gap->ga_data
398 : objmembers_gap->ga_data);
399 int cl_count = loop == 1 ? classmembers_gap->ga_len
400 : objmembers_gap->ga_len;
401 for (int if_i = 0; if_i < if_count; ++if_i)
402 {
403 int cl_i;
404 for (cl_i = 0; cl_i < cl_count; ++cl_i)
405 {
406 ocmember_T *m = &cl_ms[cl_i];
407 where_T where = WHERE_INIT;
408
409 if (STRCMP(if_ms[if_i].ocm_name, m->ocm_name) != 0)
410 continue;
411
412 // Ensure the type is matching.
413 where.wt_func_name = (char *)m->ocm_name;
414 where.wt_kind = WT_MEMBER;
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200415 if (check_type(if_ms[if_i].ocm_type, m->ocm_type, TRUE,
416 where) == FAIL)
417 return FALSE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200418
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +0200419 if (if_ms[if_i].ocm_access != m->ocm_access)
420 {
421 semsg(_(e_member_str_of_interface_str_has_different_access),
422 if_ms[if_i].ocm_name, intf_class_name);
423 return FALSE;
424 }
425
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200426 break;
427 }
428 if (cl_i == cl_count)
429 {
430 semsg(_(e_member_str_of_interface_str_not_implemented),
431 if_ms[if_i].ocm_name, intf_class_name);
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200432 return FALSE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200433 }
434 }
435 }
436
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200437 return TRUE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200438}
439
440/*
441 * Check the functions/methods of the interface class "ifcl" match the class
442 * methods ("classfunctions_gap") and object functions ("objmemthods_gap") of a
443 * class.
444 * Returns TRUE if the class and object member names are valid.
445 */
446 static int
447validate_interface_methods(
448 char_u *intf_class_name,
449 class_T *ifcl,
450 garray_T *classfunctions_gap,
451 garray_T *objmethods_gap)
452{
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200453 for (int loop = 1; loop <= 2; ++loop)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200454 {
455 // loop == 1: check class functions
456 // loop == 2: check object methods
457 int if_count = loop == 1 ? ifcl->class_class_function_count
458 : ifcl->class_obj_method_count;
459 if (if_count == 0)
460 continue;
461 ufunc_T **if_fp = loop == 1 ? ifcl->class_class_functions
462 : ifcl->class_obj_methods;
463 ufunc_T **cl_fp = (ufunc_T **)(loop == 1
464 ? classfunctions_gap->ga_data
465 : objmethods_gap->ga_data);
466 int cl_count = loop == 1 ? classfunctions_gap->ga_len
467 : objmethods_gap->ga_len;
468 for (int if_i = 0; if_i < if_count; ++if_i)
469 {
470 char_u *if_name = if_fp[if_i]->uf_name;
471 int cl_i;
472 for (cl_i = 0; cl_i < cl_count; ++cl_i)
473 {
474 char_u *cl_name = cl_fp[cl_i]->uf_name;
475 if (STRCMP(if_name, cl_name) == 0)
476 {
477 where_T where = WHERE_INIT;
478
479 // Ensure the type is matching.
480 where.wt_func_name = (char *)if_name;
481 where.wt_kind = WT_METHOD;
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200482 if (check_type(if_fp[if_i]->uf_func_type,
483 cl_fp[cl_i]->uf_func_type, TRUE, where) == FAIL)
484 return FALSE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200485 break;
486 }
487 }
488 if (cl_i == cl_count)
489 {
490 semsg(_(e_function_str_of_interface_str_not_implemented),
491 if_name, intf_class_name);
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200492 return FALSE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200493 }
494 }
495 }
496
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200497 return TRUE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200498}
499
500/*
501 * Validate all the "implements" classes when creating a new class. The
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200502 * classes are returned in "intf_classes". The class functions, class members,
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200503 * object methods and object members in the new class are in
504 * "classfunctions_gap", "classmembers_gap", "objmethods_gap", and
505 * "objmembers_gap" respectively.
506 */
507 static int
508validate_implements_classes(
509 garray_T *impl_gap,
510 class_T **intf_classes,
511 garray_T *classfunctions_gap,
512 garray_T *classmembers_gap,
513 garray_T *objmethods_gap,
514 garray_T *objmembers_gap)
515{
516 int success = TRUE;
517
518 for (int i = 0; i < impl_gap->ga_len && success; ++i)
519 {
520 char_u *impl = ((char_u **)impl_gap->ga_data)[i];
521 typval_T tv;
522 tv.v_type = VAR_UNKNOWN;
523 if (eval_variable_import(impl, &tv) == FAIL)
524 {
525 semsg(_(e_interface_name_not_found_str), impl);
526 success = FALSE;
527 break;
528 }
529
530 if (tv.v_type != VAR_CLASS
531 || tv.vval.v_class == NULL
532 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0)
533 {
534 semsg(_(e_not_valid_interface_str), impl);
535 success = FALSE;
536 clear_tv(&tv);
537 break;
538 }
539
540 class_T *ifcl = tv.vval.v_class;
541 intf_classes[i] = ifcl;
542 ++ifcl->class_refcount;
543
544 // check the members of the interface match the members of the class
545 success = validate_interface_members(impl, ifcl, classmembers_gap,
546 objmembers_gap);
547
548 // check the functions/methods of the interface match the
549 // functions/methods of the class
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200550 if (success)
551 success = validate_interface_methods(impl, ifcl,
552 classfunctions_gap, objmethods_gap);
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200553 clear_tv(&tv);
554 }
555
556 return success;
557}
558
559/*
560 * Check no function argument name is used as a class member.
561 * (Object members are always accessed with "this." prefix, so no need
562 * to check them.)
563 */
564 static int
565check_func_arg_names(
566 garray_T *classfunctions_gap,
567 garray_T *objmethods_gap,
568 garray_T *classmembers_gap)
569{
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200570 // loop 1: class functions, loop 2: object methods
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200571 for (int loop = 1; loop <= 2; ++loop)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200572 {
573 garray_T *gap = loop == 1 ? classfunctions_gap : objmethods_gap;
574
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200575 for (int fi = 0; fi < gap->ga_len; ++fi)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200576 {
577 ufunc_T *uf = ((ufunc_T **)gap->ga_data)[fi];
578
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200579 for (int i = 0; i < uf->uf_args.ga_len; ++i)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200580 {
581 char_u *aname = ((char_u **)uf->uf_args.ga_data)[i];
582 garray_T *mgap = classmembers_gap;
583
584 // Check all the class member names
585 for (int mi = 0; mi < mgap->ga_len; ++mi)
586 {
587 char_u *mname = ((ocmember_T *)mgap->ga_data + mi)
588 ->ocm_name;
589 if (STRCMP(aname, mname) == 0)
590 {
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200591 if (uf->uf_script_ctx.sc_sid > 0)
592 SOURCING_LNUM = uf->uf_script_ctx.sc_lnum;
593
594 semsg(_(e_argument_already_declared_in_class_str),
595 aname);
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200596
597 return FALSE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200598 }
599 }
600 }
601 }
602 }
603
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200604 return TRUE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200605}
606
607/*
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +0200608 * Returns TRUE if the member "varname" is already defined.
609 */
610 static int
611is_duplicate_member(garray_T *mgap, char_u *varname, char_u *varname_end)
612{
613 char_u *pstr = (*varname == '_') ? varname + 1 : varname;
614
615 for (int i = 0; i < mgap->ga_len; ++i)
616 {
617 ocmember_T *m = ((ocmember_T *)mgap->ga_data) + i;
618 char_u *qstr = *m->ocm_name == '_' ? m->ocm_name + 1 : m->ocm_name;
619 if (STRNCMP(pstr, qstr, varname_end - pstr) == 0)
620 {
621 char_u *name = vim_strnsave(varname, varname_end - varname);
622 semsg(_(e_duplicate_member_str), name);
623 vim_free(name);
624 return TRUE;
625 }
626 }
627
628 return FALSE;
629}
630
631/*
632 * Returns TRUE if the method "name" is already defined.
633 */
634 static int
635is_duplicate_method(garray_T *fgap, char_u *name)
636{
637 char_u *pstr = (*name == '_') ? name + 1 : name;
638
639 for (int i = 0; i < fgap->ga_len; ++i)
640 {
641 char_u *n = ((ufunc_T **)fgap->ga_data)[i]->uf_name;
642 char_u *qstr = *n == '_' ? n + 1 : n;
643 if (STRCMP(pstr, qstr) == 0)
644 {
645 semsg(_(e_duplicate_function_str), name);
646 return TRUE;
647 }
648 }
649
650 return FALSE;
651}
652
653/*
Gianmaria Bajo4b9777a2023-08-29 22:26:30 +0200654 * Returns TRUE if the constructor is valid.
655 */
656 static int
657is_valid_constructor(ufunc_T *uf, int is_abstract, int has_static)
658{
659 // Constructors are not allowed in abstract classes.
660 if (is_abstract)
661 {
662 emsg(_(e_cannot_define_new_function_in_abstract_class));
663 return FALSE;
664 }
665 // A constructor is always static, no need to define it so.
666 if (has_static)
667 {
668 emsg(_(e_cannot_define_new_function_as_static));
669 return FALSE;
670 }
671 // A return type should not be specified for the new()
672 // constructor method.
673 if (uf->uf_ret_type->tt_type != VAR_VOID)
674 {
675 emsg(_(e_cannot_use_a_return_type_with_new));
676 return FALSE;
677 }
678 return TRUE;
679}
680
681/*
Yegappan Lakshmananb1027282023-08-19 11:26:42 +0200682 * Update the interface class lookup table for the member index on the
683 * interface to the member index in the class implementing the interface.
684 * And a lookup table for the object method index on the interface
685 * to the object method index in the class implementing the interface.
686 * This is also used for updating the lookup table for the extended class
687 * hierarchy.
688 */
689 static int
690update_member_method_lookup_table(
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +0200691 class_T *ifcl,
692 class_T *cl,
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +0200693 garray_T *objmethods,
694 int pobj_method_offset,
695 int is_interface)
Yegappan Lakshmananb1027282023-08-19 11:26:42 +0200696{
697 if (ifcl == NULL)
698 return OK;
699
700 // Table for members.
701 itf2class_T *if2cl = alloc_clear(sizeof(itf2class_T)
702 + ifcl->class_obj_member_count * sizeof(int));
703 if (if2cl == NULL)
704 return FAIL;
705 if2cl->i2c_next = ifcl->class_itf2class;
706 ifcl->class_itf2class = if2cl;
707 if2cl->i2c_class = cl;
708 if2cl->i2c_is_method = FALSE;
709
710 for (int if_i = 0; if_i < ifcl->class_obj_member_count; ++if_i)
711 for (int cl_i = 0; cl_i < cl->class_obj_member_count; ++cl_i)
712 {
713 if (STRCMP(ifcl->class_obj_members[if_i].ocm_name,
714 cl->class_obj_members[cl_i].ocm_name) == 0)
715 {
716 int *table = (int *)(if2cl + 1);
717 table[if_i] = cl_i;
718 break;
719 }
720 }
721
722 // Table for methods.
723 if2cl = alloc_clear(sizeof(itf2class_T)
724 + ifcl->class_obj_method_count * sizeof(int));
725 if (if2cl == NULL)
726 return FAIL;
727 if2cl->i2c_next = ifcl->class_itf2class;
728 ifcl->class_itf2class = if2cl;
729 if2cl->i2c_class = cl;
730 if2cl->i2c_is_method = TRUE;
731
732 for (int if_i = 0; if_i < ifcl->class_obj_method_count; ++if_i)
733 {
734 int done = FALSE;
735 for (int cl_i = 0; cl_i < objmethods->ga_len; ++cl_i)
736 {
737 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
738 ((ufunc_T **)objmethods->ga_data)[cl_i]->uf_name)
739 == 0)
740 {
741 int *table = (int *)(if2cl + 1);
742 table[if_i] = cl_i;
743 done = TRUE;
744 break;
745 }
746 }
747
748 // extended class object method is not overridden by the child class.
749 // Keep the method declared in one of the parent classes in the
750 // lineage.
751 if (!done && !is_interface)
752 {
753 // If "ifcl" is not the immediate parent of "cl", then search in
754 // the intermediate parent classes.
755 if (cl->class_extends != ifcl)
756 {
757 class_T *parent = cl->class_extends;
758 int method_offset = objmethods->ga_len;
759
760 while (!done && parent != NULL && parent != ifcl)
761 {
762
763 for (int cl_i = 0;
764 cl_i < parent->class_obj_method_count_child; ++cl_i)
765 {
766 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
767 parent->class_obj_methods[cl_i]->uf_name)
768 == 0)
769 {
770 int *table = (int *)(if2cl + 1);
771 table[if_i] = method_offset + cl_i;
772 done = TRUE;
773 break;
774 }
775 }
776 method_offset += parent->class_obj_method_count_child;
777 parent = parent->class_extends;
778 }
779 }
780
781 if (!done)
782 {
783 int *table = (int *)(if2cl + 1);
784 table[if_i] = pobj_method_offset + if_i;
785 }
786 }
787 }
788
789 return OK;
790}
791
792/*
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200793 * Update the member and object method lookup tables for a new class in the
794 * interface class.
795 * For each interface add a lookup table for the member index on the interface
796 * to the member index in the new class. And a lookup table for the object
797 * method index on the interface to the object method index in the new class.
798 */
799 static int
800add_lookup_tables(class_T *cl, class_T *extends_cl, garray_T *objmethods_gap)
801{
802 for (int i = 0; i < cl->class_interface_count; ++i)
803 {
804 class_T *ifcl = cl->class_interfaces_cl[i];
805
806 if (update_member_method_lookup_table(ifcl, cl, objmethods_gap,
807 0, TRUE) == FAIL)
808 return FAIL;
809 }
810
811 // Update the lookup table for the extended class, if nay
812 if (extends_cl != NULL)
813 {
814 class_T *pclass = extends_cl;
815 int pobj_method_offset = objmethods_gap->ga_len;
816
817 // Update the entire lineage of extended classes.
818 while (pclass != NULL)
819 {
820 if (update_member_method_lookup_table(pclass, cl,
821 objmethods_gap, pobj_method_offset, FALSE) == FAIL)
822 return FAIL;
823
824 pobj_method_offset += pclass->class_obj_method_count_child;
825 pclass = pclass->class_extends;
826 }
827 }
828
829 return OK;
830}
831
832/*
833 * Add class members to a new class. Allocate a typval for each class member
834 * and initialize it.
835 */
836 static void
837add_class_members(class_T *cl, exarg_T *eap)
838{
839 // Allocate a typval for each class member and initialize it.
840 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
841 cl->class_class_member_count);
842 if (cl->class_members_tv == NULL)
843 return;
844
845 for (int i = 0; i < cl->class_class_member_count; ++i)
846 {
847 ocmember_T *m = &cl->class_class_members[i];
848 typval_T *tv = &cl->class_members_tv[i];
849 if (m->ocm_init != NULL)
850 {
851 typval_T *etv = eval_expr(m->ocm_init, eap);
852 if (etv != NULL)
853 {
854 *tv = *etv;
855 vim_free(etv);
856 }
857 }
858 else
859 {
860 // TODO: proper default value
861 tv->v_type = m->ocm_type->tt_type;
862 tv->vval.v_string = NULL;
863 }
864 }
865}
866
867/*
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +0200868 * Add a default constructor method (new()) to the class "cl".
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200869 */
870 static void
871add_default_constructor(
872 class_T *cl,
873 garray_T *classfunctions_gap,
874 garray_T *type_list_gap)
875{
876 garray_T fga;
877
878 ga_init2(&fga, 1, 1000);
879 ga_concat(&fga, (char_u *)"new(");
880 for (int i = 0; i < cl->class_obj_member_count; ++i)
881 {
882 if (i > 0)
883 ga_concat(&fga, (char_u *)", ");
884 ga_concat(&fga, (char_u *)"this.");
885 ocmember_T *m = cl->class_obj_members + i;
886 ga_concat(&fga, (char_u *)m->ocm_name);
887 ga_concat(&fga, (char_u *)" = v:none");
888 }
889 ga_concat(&fga, (char_u *)")\nenddef\n");
890 ga_append(&fga, NUL);
891
892 exarg_T fea;
893 CLEAR_FIELD(fea);
894 fea.cmdidx = CMD_def;
895 fea.cmd = fea.arg = fga.ga_data;
896
897 garray_T lines_to_free;
898 ga_init2(&lines_to_free, sizeof(char_u *), 50);
899
900 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS);
901
902 ga_clear_strings(&lines_to_free);
903 vim_free(fga.ga_data);
904
905 if (nf != NULL && ga_grow(classfunctions_gap, 1) == OK)
906 {
907 ((ufunc_T **)classfunctions_gap->ga_data)[classfunctions_gap->ga_len]
908 = nf;
909 ++classfunctions_gap->ga_len;
910
911 nf->uf_flags |= FC_NEW;
912 nf->uf_ret_type = get_type_ptr(type_list_gap);
913 if (nf->uf_ret_type != NULL)
914 {
915 nf->uf_ret_type->tt_type = VAR_OBJECT;
916 nf->uf_ret_type->tt_class = cl;
917 nf->uf_ret_type->tt_argcount = 0;
918 nf->uf_ret_type->tt_args = NULL;
919 }
920 }
921}
922
923/*
924 * Add the class functions and object methods to the new class "cl".
925 * When extending a class, add the functions and methods from the parent class
926 * also.
927 */
928 static int
929add_classfuncs_objmethods(
930 class_T *cl,
931 class_T *extends_cl,
932 garray_T *classfunctions_gap,
933 garray_T *objmethods_gap)
934{
935 // loop 1: class functions, loop 2: object methods
936 for (int loop = 1; loop <= 2; ++loop)
937 {
938 garray_T *gap = loop == 1 ? classfunctions_gap : objmethods_gap;
939 int *fcount = loop == 1 ? &cl->class_class_function_count
940 : &cl->class_obj_method_count;
941 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
942 : &cl->class_obj_methods;
943
944 int parent_count = 0;
945 if (extends_cl != NULL)
946 // Include functions from the parent.
947 parent_count = loop == 1
948 ? extends_cl->class_class_function_count
949 : extends_cl->class_obj_method_count;
950
951 *fcount = parent_count + gap->ga_len;
952 if (*fcount == 0)
953 {
954 *fup = NULL;
955 continue;
956 }
957 *fup = ALLOC_MULT(ufunc_T *, *fcount);
958 if (*fup == NULL)
959 return FAIL;
960
961 if (gap->ga_len != 0)
962 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
963 vim_free(gap->ga_data);
964 if (loop == 1)
965 cl->class_class_function_count_child = gap->ga_len;
966 else
967 cl->class_obj_method_count_child = gap->ga_len;
968
969 int skipped = 0;
970 for (int i = 0; i < parent_count; ++i)
971 {
972 // Copy functions from the parent. Can't use the same
973 // function, because "uf_class" is different and compilation
974 // will have a different result.
975 // Put them after the functions in the current class, object
976 // methods may be overruled, then "super.Method()" is used to
977 // find a method from the parent.
978 // Skip "new" functions. TODO: not all of them.
979 if (loop == 1 && STRNCMP(
980 extends_cl->class_class_functions[i]->uf_name,
981 "new", 3) == 0)
982 ++skipped;
983 else
984 {
985 ufunc_T *pf = (loop == 1
986 ? extends_cl->class_class_functions
987 : extends_cl->class_obj_methods)[i];
988 (*fup)[gap->ga_len + i - skipped] = copy_function(pf);
989
990 // If the child class overrides a function from the parent
991 // the signature must be equal.
992 char_u *pname = pf->uf_name;
993 for (int ci = 0; ci < gap->ga_len; ++ci)
994 {
995 ufunc_T *cf = (*fup)[ci];
996 char_u *cname = cf->uf_name;
997 if (STRCMP(pname, cname) == 0)
998 {
999 where_T where = WHERE_INIT;
1000 where.wt_func_name = (char *)pname;
1001 where.wt_kind = WT_METHOD;
1002 (void)check_type(pf->uf_func_type, cf->uf_func_type,
1003 TRUE, where);
1004 }
1005 }
1006 }
1007 }
1008
1009 *fcount -= skipped;
1010
1011 // Set the class pointer on all the functions and object methods.
1012 for (int i = 0; i < *fcount; ++i)
1013 {
1014 ufunc_T *fp = (*fup)[i];
1015 fp->uf_class = cl;
1016 if (loop == 2)
1017 fp->uf_flags |= FC_OBJECT;
1018 }
1019 }
1020
1021 return OK;
1022}
1023
1024/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001025 * Handle ":class" and ":abstract class" up to ":endclass".
Bram Moolenaar554d0312023-01-05 19:59:18 +00001026 * Handle ":interface" up to ":endinterface".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001027 */
1028 void
1029ex_class(exarg_T *eap)
1030{
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001031 int is_class = eap->cmdidx == CMD_class; // FALSE for :interface
1032 long start_lnum = SOURCING_LNUM;
Bram Moolenaar554d0312023-01-05 19:59:18 +00001033
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001034 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001035 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001036 if (is_abstract)
1037 {
1038 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
1039 {
1040 semsg(_(e_invalid_argument_str), arg);
1041 return;
1042 }
1043 arg = skipwhite(arg + 5);
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001044 is_class = TRUE;
1045 }
1046
1047 if (!current_script_is_vim9()
1048 || (cmdmod.cmod_flags & CMOD_LEGACY)
1049 || !getline_equal(eap->getline, eap->cookie, getsourceline))
1050 {
1051 if (is_class)
1052 emsg(_(e_class_can_only_be_defined_in_vim9_script));
1053 else
1054 emsg(_(e_interface_can_only_be_defined_in_vim9_script));
1055 return;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001056 }
1057
1058 if (!ASCII_ISUPPER(*arg))
1059 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001060 if (is_class)
1061 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
1062 else
1063 semsg(_(e_interface_name_must_start_with_uppercase_letter_str),
1064 arg);
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001065 return;
1066 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001067 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
1068 if (!IS_WHITE_OR_NUL(*name_end))
1069 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001070 semsg(_(e_white_space_required_after_name_str), arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001071 return;
1072 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001073 char_u *name_start = arg;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001074
Bram Moolenaara86655a2023-01-12 17:06:27 +00001075 // "export class" gets used when creating the class, don't use "is_export"
1076 // for the items inside the class.
1077 int class_export = is_export;
1078 is_export = FALSE;
1079
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001080 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001081 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001082
Bram Moolenaar83677162023-01-08 19:54:10 +00001083 // Name for "extends BaseClass"
1084 char_u *extends = NULL;
1085
Bram Moolenaar94674f22023-01-06 18:42:20 +00001086 // Names for "implements SomeInterface"
1087 garray_T ga_impl;
1088 ga_init2(&ga_impl, sizeof(char_u *), 5);
1089
1090 arg = skipwhite(name_end);
1091 while (*arg != NUL && *arg != '#' && *arg != '\n')
1092 {
1093 // TODO:
Bram Moolenaar94674f22023-01-06 18:42:20 +00001094 // specifies SomeInterface
Bram Moolenaar83677162023-01-08 19:54:10 +00001095 if (STRNCMP(arg, "extends", 7) == 0 && IS_WHITE_OR_NUL(arg[7]))
1096 {
1097 if (extends != NULL)
1098 {
1099 emsg(_(e_duplicate_extends));
1100 goto early_ret;
1101 }
1102 arg = skipwhite(arg + 7);
1103 char_u *end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
1104 if (!IS_WHITE_OR_NUL(*end))
1105 {
1106 semsg(_(e_white_space_required_after_name_str), arg);
1107 goto early_ret;
1108 }
1109 extends = vim_strnsave(arg, end - arg);
1110 if (extends == NULL)
1111 goto early_ret;
1112
1113 arg = skipwhite(end + 1);
1114 }
1115 else if (STRNCMP(arg, "implements", 10) == 0
1116 && IS_WHITE_OR_NUL(arg[10]))
Bram Moolenaar94674f22023-01-06 18:42:20 +00001117 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001118 if (ga_impl.ga_len > 0)
1119 {
1120 emsg(_(e_duplicate_implements));
1121 goto early_ret;
1122 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001123 arg = skipwhite(arg + 10);
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001124
1125 for (;;)
Bram Moolenaar94674f22023-01-06 18:42:20 +00001126 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001127 char_u *impl_end = find_name_end(arg, NULL, NULL,
1128 FNE_CHECK_START);
1129 if (!IS_WHITE_OR_NUL(*impl_end) && *impl_end != ',')
1130 {
1131 semsg(_(e_white_space_required_after_name_str), arg);
1132 goto early_ret;
1133 }
1134 char_u *iname = vim_strnsave(arg, impl_end - arg);
1135 if (iname == NULL)
1136 goto early_ret;
1137 for (int i = 0; i < ga_impl.ga_len; ++i)
1138 if (STRCMP(((char_u **)ga_impl.ga_data)[i], iname) == 0)
1139 {
1140 semsg(_(e_duplicate_interface_after_implements_str),
1141 iname);
1142 vim_free(iname);
1143 goto early_ret;
1144 }
1145 if (ga_add_string(&ga_impl, iname) == FAIL)
1146 {
1147 vim_free(iname);
1148 goto early_ret;
1149 }
1150 if (*impl_end != ',')
1151 {
1152 arg = skipwhite(impl_end);
1153 break;
1154 }
1155 arg = skipwhite(impl_end + 1);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001156 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001157 }
1158 else
1159 {
1160 semsg(_(e_trailing_characters_str), arg);
1161early_ret:
Bram Moolenaar83677162023-01-08 19:54:10 +00001162 vim_free(extends);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001163 ga_clear_strings(&ga_impl);
1164 return;
1165 }
1166 }
1167
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001168 garray_T type_list; // list of pointers to allocated types
1169 ga_init2(&type_list, sizeof(type_T *), 10);
1170
Bram Moolenaard505d172022-12-18 21:42:55 +00001171 // Growarray with class members declared in the class.
1172 garray_T classmembers;
1173 ga_init2(&classmembers, sizeof(ocmember_T), 10);
1174
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001175 // Growarray with functions declared in the class.
1176 garray_T classfunctions;
1177 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +00001178
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001179 // Growarray with object members declared in the class.
1180 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +00001181 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001182
1183 // Growarray with object methods declared in the class.
1184 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001185 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001186
1187 /*
Bram Moolenaar554d0312023-01-05 19:59:18 +00001188 * Go over the body of the class/interface until "endclass" or
1189 * "endinterface" is found.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001190 */
1191 char_u *theline = NULL;
1192 int success = FALSE;
1193 for (;;)
1194 {
1195 vim_free(theline);
1196 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
1197 if (theline == NULL)
1198 break;
1199 char_u *line = skipwhite(theline);
1200
Bram Moolenaar418b5472022-12-20 13:38:22 +00001201 // Skip empty and comment lines.
1202 if (*line == NUL)
1203 continue;
1204 if (*line == '#')
1205 {
1206 if (vim9_bad_comment(line))
1207 break;
1208 continue;
1209 }
1210
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001211 char_u *p = line;
Bram Moolenaar554d0312023-01-05 19:59:18 +00001212 char *end_name = is_class ? "endclass" : "endinterface";
1213 if (checkforcmd(&p, end_name, is_class ? 4 : 5))
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001214 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001215 if (STRNCMP(line, end_name, is_class ? 8 : 12) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001216 semsg(_(e_command_cannot_be_shortened_str), line);
1217 else if (*p == '|' || !ends_excmd2(line, p))
1218 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +00001219 else
1220 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001221 break;
1222 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00001223 char *wrong_name = is_class ? "endinterface" : "endclass";
1224 if (checkforcmd(&p, wrong_name, is_class ? 5 : 4))
1225 {
Bram Moolenaar657aea72023-01-27 13:16:19 +00001226 semsg(_(e_invalid_command_str_expected_str), line, end_name);
Bram Moolenaar554d0312023-01-05 19:59:18 +00001227 break;
1228 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001229
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001230 int has_public = FALSE;
1231 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001232 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001233 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001234 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001235 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001236 break;
1237 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001238 has_public = TRUE;
1239 p = skipwhite(line + 6);
1240
Bram Moolenaard505d172022-12-18 21:42:55 +00001241 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001242 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001243 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001244 break;
1245 }
1246 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001247
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001248 int has_static = FALSE;
1249 char_u *ps = p;
1250 if (checkforcmd(&p, "static", 4))
1251 {
1252 if (STRNCMP(ps, "static", 6) != 0)
1253 {
1254 semsg(_(e_command_cannot_be_shortened_str), ps);
1255 break;
1256 }
1257 has_static = TRUE;
1258 p = skipwhite(ps + 6);
1259 }
1260
Bram Moolenaard505d172022-12-18 21:42:55 +00001261 // object members (public, read access, private):
1262 // "this._varname"
1263 // "this.varname"
1264 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001265 if (STRNCMP(p, "this", 4) == 0)
1266 {
1267 if (p[4] != '.' || !eval_isnamec1(p[5]))
1268 {
1269 semsg(_(e_invalid_object_member_declaration_str), p);
1270 break;
1271 }
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001272 if (has_static)
1273 {
1274 emsg(_(e_static_cannot_be_followed_by_this));
1275 break;
1276 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001277 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +00001278 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +00001279 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +00001280 char_u *init_expr = NULL;
1281 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +00001282 &varname_end, &type_list, &type,
1283 is_class ? &init_expr: NULL) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +00001284 break;
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001285 if (is_duplicate_member(&objmembers, varname, varname_end))
1286 {
1287 vim_free(init_expr);
1288 break;
1289 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001290 if (add_member(&objmembers, varname, varname_end,
1291 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +00001292 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001293 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001294 break;
1295 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001296 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001297
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001298 // constructors:
1299 // def new()
1300 // enddef
1301 // def newOther()
1302 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001303 // object methods and class functions:
1304 // def SomeMethod()
1305 // enddef
1306 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001307 // enddef
1308 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001309 // def <Tval> someMethod()
1310 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001311 else if (checkforcmd(&p, "def", 3))
1312 {
1313 exarg_T ea;
1314 garray_T lines_to_free;
1315
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001316 // TODO: error for "public static def Func()"?
1317
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001318 CLEAR_FIELD(ea);
1319 ea.cmd = line;
1320 ea.arg = p;
1321 ea.cmdidx = CMD_def;
1322 ea.getline = eap->getline;
1323 ea.cookie = eap->cookie;
1324
1325 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +00001326 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free,
1327 is_class ? CF_CLASS : CF_INTERFACE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001328 ga_clear_strings(&lines_to_free);
1329
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001330 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001331 {
Bram Moolenaar58b40092023-01-11 15:59:05 +00001332 char_u *name = uf->uf_name;
1333 int is_new = STRNCMP(name, "new", 3) == 0;
Gianmaria Bajo4b9777a2023-08-29 22:26:30 +02001334
1335 if (is_new && !is_valid_constructor(uf, is_abstract, has_static))
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001336 {
Yegappan Lakshmananb1027282023-08-19 11:26:42 +02001337 func_clear_free(uf, FALSE);
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001338 break;
1339 }
Gianmaria Bajo4b9777a2023-08-29 22:26:30 +02001340
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001341 garray_T *fgap = has_static || is_new
1342 ? &classfunctions : &objmethods;
Bram Moolenaar58b40092023-01-11 15:59:05 +00001343 // Check the name isn't used already.
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001344 if (is_duplicate_method(fgap, name))
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +02001345 {
1346 success = FALSE;
1347 func_clear_free(uf, FALSE);
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001348 break;
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +02001349 }
Bram Moolenaar58b40092023-01-11 15:59:05 +00001350
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001351 if (ga_grow(fgap, 1) == OK)
1352 {
1353 if (is_new)
1354 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001355
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001356 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
1357 ++fgap->ga_len;
1358 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001359 }
1360 }
1361
1362 // class members
1363 else if (has_static)
1364 {
1365 // class members (public, read access, private):
1366 // "static _varname"
1367 // "static varname"
1368 // "public static varname"
1369 char_u *varname = p;
1370 char_u *varname_end = NULL;
1371 type_T *type = NULL;
1372 char_u *init_expr = NULL;
1373 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +00001374 &varname_end, &type_list, &type,
1375 is_class ? &init_expr : NULL) == FAIL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001376 break;
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001377 if (is_duplicate_member(&classmembers, varname, varname_end))
1378 {
1379 vim_free(init_expr);
1380 break;
1381 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001382 if (add_member(&classmembers, varname, varname_end,
1383 has_public, type, init_expr) == FAIL)
1384 {
1385 vim_free(init_expr);
1386 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001387 }
1388 }
1389
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001390 else
1391 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001392 if (is_class)
1393 semsg(_(e_not_valid_command_in_class_str), line);
1394 else
1395 semsg(_(e_not_valid_command_in_interface_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001396 break;
1397 }
1398 }
1399 vim_free(theline);
1400
Bram Moolenaar83677162023-01-08 19:54:10 +00001401 class_T *extends_cl = NULL; // class from "extends" argument
1402
1403 /*
1404 * Check a few things before defining the class.
1405 */
1406
1407 // Check the "extends" class is valid.
1408 if (success && extends != NULL)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001409 success = validate_extends_class(extends, &extends_cl);
Bram Moolenaar83677162023-01-08 19:54:10 +00001410 VIM_CLEAR(extends);
1411
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +02001412 // Check the new class members and object members doesn't duplicate the
1413 // members in the extended class lineage.
1414 if (success && extends_cl != NULL)
1415 success = validate_extends_members(&classmembers, &objmembers,
1416 extends_cl);
1417
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001418 class_T **intf_classes = NULL;
1419
Bram Moolenaar83677162023-01-08 19:54:10 +00001420 // Check all "implements" entries are valid.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001421 if (success && ga_impl.ga_len > 0)
1422 {
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001423 intf_classes = ALLOC_CLEAR_MULT(class_T *, ga_impl.ga_len);
1424
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001425 success = validate_implements_classes(&ga_impl, intf_classes,
1426 &classfunctions, &classmembers,
1427 &objmethods, &objmembers);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001428 }
1429
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001430 // Check no function argument name is used as a class member.
Bram Moolenaard40f00c2023-01-13 17:36:49 +00001431 if (success)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001432 success = check_func_arg_names(&classfunctions, &objmethods,
1433 &classmembers);
Bram Moolenaard40f00c2023-01-13 17:36:49 +00001434
Bram Moolenaareb533502022-12-14 15:06:11 +00001435 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001436 if (success)
1437 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001438 // "endclass" encountered without failures: Create the class.
1439
Bram Moolenaareb533502022-12-14 15:06:11 +00001440 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001441 if (cl == NULL)
1442 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +00001443 if (!is_class)
1444 cl->class_flags = CLASS_INTERFACE;
1445
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001446 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +00001447 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +00001448 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001449 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +00001450
Bram Moolenaard0200c82023-01-28 15:19:40 +00001451 if (extends_cl != NULL)
1452 {
1453 cl->class_extends = extends_cl;
1454 extends_cl->class_flags |= CLASS_EXTENDED;
1455 }
Bram Moolenaar83677162023-01-08 19:54:10 +00001456
Bram Moolenaard505d172022-12-18 21:42:55 +00001457 // Add class and object members to "cl".
1458 if (add_members_to_class(&classmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +00001459 extends_cl == NULL ? NULL
1460 : extends_cl->class_class_members,
1461 extends_cl == NULL ? 0
1462 : extends_cl->class_class_member_count,
1463 &cl->class_class_members,
1464 &cl->class_class_member_count) == FAIL
Bram Moolenaard505d172022-12-18 21:42:55 +00001465 || add_members_to_class(&objmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +00001466 extends_cl == NULL ? NULL
1467 : extends_cl->class_obj_members,
1468 extends_cl == NULL ? 0
1469 : extends_cl->class_obj_member_count,
1470 &cl->class_obj_members,
1471 &cl->class_obj_member_count) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +00001472 goto cleanup;
1473
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001474 if (ga_impl.ga_len > 0)
1475 {
1476 // Move the "implements" names into the class.
1477 cl->class_interface_count = ga_impl.ga_len;
1478 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
1479 if (cl->class_interfaces == NULL)
1480 goto cleanup;
1481 for (int i = 0; i < ga_impl.ga_len; ++i)
1482 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
1483 VIM_CLEAR(ga_impl.ga_data);
1484 ga_impl.ga_len = 0;
1485
Bram Moolenaard0200c82023-01-28 15:19:40 +00001486 cl->class_interfaces_cl = intf_classes;
1487 intf_classes = NULL;
1488 }
1489
1490 if (cl->class_interface_count > 0 || extends_cl != NULL)
1491 {
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001492 // Add a method and member lookup table to each of the interface
1493 // classes.
1494 if (add_lookup_tables(cl, extends_cl, &objmethods) == FAIL)
1495 goto cleanup;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001496 }
1497
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001498 // Allocate a typval for each class member and initialize it.
Bram Moolenaar554d0312023-01-05 19:59:18 +00001499 if (is_class && cl->class_class_member_count > 0)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001500 add_class_members(cl, eap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001501
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001502 int have_new = FALSE;
1503 ufunc_T *class_func = NULL;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001504 for (int i = 0; i < classfunctions.ga_len; ++i)
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001505 {
1506 class_func = ((ufunc_T **)classfunctions.ga_data)[i];
1507 if (STRCMP(class_func->uf_name, "new") == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001508 {
1509 have_new = TRUE;
1510 break;
1511 }
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001512 }
1513
1514 if (have_new)
1515 // The return type of new() is an object of class "cl"
1516 class_func->uf_ret_type->tt_class = cl;
1517 else if (is_class && !is_abstract && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001518 // No new() method was defined, add the default constructor.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001519 add_default_constructor(cl, &classfunctions, &type_list);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001520
Bram Moolenaar58b40092023-01-11 15:59:05 +00001521 // Move all the functions into the created class.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001522 if (add_classfuncs_objmethods(cl, extends_cl, &classfunctions,
1523 &objmethods) == FAIL)
1524 goto cleanup;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001525
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001526 cl->class_type.tt_type = VAR_CLASS;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001527 cl->class_type.tt_class = cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001528 cl->class_object_type.tt_type = VAR_OBJECT;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001529 cl->class_object_type.tt_class = cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001530 cl->class_type_list = type_list;
1531
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02001532 class_created(cl);
1533
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001534 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +00001535 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001536
1537 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001538 // TODO: handle other context, e.g. in a function
Ernie Rael21d32122023-09-02 15:09:18 +02001539 // TODO: does uf_hash need to be cleared?
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001540 typval_T tv;
1541 tv.v_type = VAR_CLASS;
1542 tv.vval.v_class = cl;
Bram Moolenaara86655a2023-01-12 17:06:27 +00001543 is_export = class_export;
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001544 SOURCING_LNUM = start_lnum;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001545 set_var_const(cl->class_name, current_sctx.sc_sid,
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001546 NULL, &tv, FALSE, 0, 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001547 return;
1548 }
1549
1550cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +00001551 if (cl != NULL)
1552 {
1553 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001554 vim_free(cl->class_class_functions);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001555 if (cl->class_interfaces != NULL)
1556 {
1557 for (int i = 0; i < cl->class_interface_count; ++i)
1558 vim_free(cl->class_interfaces[i]);
1559 vim_free(cl->class_interfaces);
1560 }
1561 if (cl->class_interfaces_cl != NULL)
1562 {
1563 for (int i = 0; i < cl->class_interface_count; ++i)
1564 class_unref(cl->class_interfaces_cl[i]);
1565 vim_free(cl->class_interfaces_cl);
1566 }
Bram Moolenaareb533502022-12-14 15:06:11 +00001567 vim_free(cl->class_obj_members);
1568 vim_free(cl->class_obj_methods);
1569 vim_free(cl);
1570 }
1571
Bram Moolenaar83677162023-01-08 19:54:10 +00001572 vim_free(extends);
1573 class_unref(extends_cl);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001574
1575 if (intf_classes != NULL)
1576 {
1577 for (int i = 0; i < ga_impl.ga_len; ++i)
1578 class_unref(intf_classes[i]);
1579 vim_free(intf_classes);
1580 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001581 ga_clear_strings(&ga_impl);
1582
Bram Moolenaard505d172022-12-18 21:42:55 +00001583 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001584 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001585 garray_T *gap = round == 1 ? &classmembers : &objmembers;
1586 if (gap->ga_len == 0 || gap->ga_data == NULL)
1587 continue;
1588
1589 for (int i = 0; i < gap->ga_len; ++i)
1590 {
1591 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
1592 vim_free(m->ocm_name);
1593 vim_free(m->ocm_init);
1594 }
1595 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001596 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001597
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001598 for (int i = 0; i < objmethods.ga_len; ++i)
1599 {
1600 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
1601 func_clear_free(uf, FALSE);
1602 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001603 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001604
1605 for (int i = 0; i < classfunctions.ga_len; ++i)
1606 {
1607 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
1608 func_clear_free(uf, FALSE);
1609 }
1610 ga_clear(&classfunctions);
1611
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001612 clear_type_list(&type_list);
1613}
1614
1615/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001616 * Find member "name" in class "cl", set "member_idx" to the member index and
1617 * return its type.
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02001618 * When "is_object" is TRUE, then look for object members. Otherwise look for
1619 * class members.
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001620 * When not found "member_idx" is set to -1 and t_any is returned.
Ernie Rael456ae552023-09-01 18:54:54 +02001621 * Set *p_m ocmmember_T if not NULL
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001622 */
1623 type_T *
1624class_member_type(
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02001625 class_T *cl,
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02001626 int is_object,
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02001627 char_u *name,
1628 char_u *name_end,
1629 int *member_idx,
Ernie Rael456ae552023-09-01 18:54:54 +02001630 ocmember_T **p_m)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001631{
1632 *member_idx = -1; // not found (yet)
1633 size_t len = name_end - name;
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02001634 int member_count = is_object ? cl->class_obj_member_count
1635 : cl->class_class_member_count;
1636 ocmember_T *members = is_object ? cl->class_obj_members
1637 : cl->class_class_members;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001638
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02001639 for (int i = 0; i < member_count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001640 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02001641 ocmember_T *m = members + i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001642 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001643 {
1644 *member_idx = i;
Ernie Rael456ae552023-09-01 18:54:54 +02001645 if (p_m != NULL)
1646 *p_m = m;
Bram Moolenaard505d172022-12-18 21:42:55 +00001647 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001648 }
1649 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001650
1651 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001652 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001653}
1654
1655/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001656 * Handle ":enum" up to ":endenum".
1657 */
1658 void
1659ex_enum(exarg_T *eap UNUSED)
1660{
1661 // TODO
1662}
1663
1664/*
1665 * Handle ":type".
1666 */
1667 void
1668ex_type(exarg_T *eap UNUSED)
1669{
1670 // TODO
1671}
1672
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001673/*
1674 * Evaluate what comes after a class:
1675 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001676 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001677 * - class constructor: SomeClass.new()
1678 * - object member: someObject.varname
1679 * - object method: someObject.SomeMethod()
1680 *
1681 * "*arg" points to the '.'.
1682 * "*arg" is advanced to after the member name or method call.
1683 *
1684 * Returns FAIL or OK.
1685 */
1686 int
1687class_object_index(
1688 char_u **arg,
1689 typval_T *rettv,
1690 evalarg_T *evalarg,
1691 int verbose UNUSED) // give error messages
1692{
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001693 if (VIM_ISWHITE((*arg)[1]))
1694 {
1695 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
1696 return FAIL;
1697 }
1698
1699 ++*arg;
1700 char_u *name = *arg;
1701 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1702 if (name_end == name)
1703 return FAIL;
1704 size_t len = name_end - name;
1705
Bram Moolenaar552bdca2023-02-17 21:08:50 +00001706 class_T *cl;
1707 if (rettv->v_type == VAR_CLASS)
1708 cl = rettv->vval.v_class;
1709 else // VAR_OBJECT
1710 {
1711 if (rettv->vval.v_object == NULL)
1712 {
1713 emsg(_(e_using_null_object));
1714 return FAIL;
1715 }
1716 cl = rettv->vval.v_object->obj_class;
1717 }
1718
Bram Moolenaard13dd302023-03-11 20:56:35 +00001719 if (cl == NULL)
1720 {
1721 emsg(_(e_incomplete_type));
1722 return FAIL;
1723 }
1724
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001725 if (*name_end == '(')
1726 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001727 int on_class = rettv->v_type == VAR_CLASS;
1728 int count = on_class ? cl->class_class_function_count
1729 : cl->class_obj_method_count;
1730 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001731 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001732 ufunc_T *fp = on_class ? cl->class_class_functions[i]
1733 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +00001734 // Use a separate pointer to avoid that ASAN complains about
1735 // uf_name[] only being 4 characters.
1736 char_u *ufname = (char_u *)fp->uf_name;
1737 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001738 {
1739 typval_T argvars[MAX_FUNC_ARGS + 1];
1740 int argcount = 0;
1741
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +02001742 if (*ufname == '_')
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02001743 {
1744 // Cannot access a private method outside of a class
1745 semsg(_(e_cannot_access_private_method_str), name);
1746 return FAIL;
1747 }
1748
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001749 char_u *argp = name_end;
1750 int ret = get_func_arguments(&argp, evalarg, 0,
1751 argvars, &argcount);
1752 if (ret == FAIL)
1753 return FAIL;
1754
1755 funcexe_T funcexe;
1756 CLEAR_FIELD(funcexe);
1757 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001758 if (rettv->v_type == VAR_OBJECT)
1759 {
1760 funcexe.fe_object = rettv->vval.v_object;
1761 ++funcexe.fe_object->obj_refcount;
1762 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001763
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001764 // Clear the class or object after calling the function, in
1765 // case the refcount is one.
1766 typval_T tv_tofree = *rettv;
1767 rettv->v_type = VAR_UNKNOWN;
1768
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001769 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001770 int error = call_user_func_check(fp, argcount, argvars,
1771 rettv, &funcexe, NULL);
1772
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001773 // Clear the previous rettv and the arguments.
1774 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001775 for (int idx = 0; idx < argcount; ++idx)
1776 clear_tv(&argvars[idx]);
1777
1778 if (error != FCERR_NONE)
1779 {
1780 user_func_error(error, printable_func_name(fp),
1781 funcexe.fe_found_var);
1782 return FAIL;
1783 }
1784 *arg = argp;
1785 return OK;
1786 }
1787 }
1788
1789 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
1790 }
1791
1792 else if (rettv->v_type == VAR_OBJECT)
1793 {
1794 for (int i = 0; i < cl->class_obj_member_count; ++i)
1795 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001796 ocmember_T *m = &cl->class_obj_members[i];
1797 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001798 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001799 if (*name == '_')
1800 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001801 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001802 return FAIL;
1803 }
1804
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001805 // The object only contains a pointer to the class, the member
1806 // values array follows right after that.
1807 object_T *obj = rettv->vval.v_object;
1808 typval_T *tv = (typval_T *)(obj + 1) + i;
1809 copy_tv(tv, rettv);
1810 object_unref(obj);
1811
1812 *arg = name_end;
1813 return OK;
1814 }
1815 }
1816
1817 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
1818 }
1819
Bram Moolenaard505d172022-12-18 21:42:55 +00001820 else if (rettv->v_type == VAR_CLASS)
1821 {
1822 // class member
1823 for (int i = 0; i < cl->class_class_member_count; ++i)
1824 {
1825 ocmember_T *m = &cl->class_class_members[i];
1826 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
1827 {
1828 if (*name == '_')
1829 {
1830 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
1831 return FAIL;
1832 }
Ernie Rael18143d32023-09-04 22:30:41 +02001833 if ((cl->class_flags & CLASS_INTERFACE) != 0)
1834 {
1835 semsg(_(e_interface_static_direct_access_str),
1836 cl->class_name, m->ocm_name);
1837 return FAIL;
1838 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001839
1840 typval_T *tv = &cl->class_members_tv[i];
1841 copy_tv(tv, rettv);
1842 class_unref(cl);
1843
1844 *arg = name_end;
1845 return OK;
1846 }
1847 }
1848
1849 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
1850 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001851
1852 return FAIL;
1853}
1854
1855/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001856 * If "arg" points to a class or object method, return it.
1857 * Otherwise return NULL.
1858 */
1859 ufunc_T *
1860find_class_func(char_u **arg)
1861{
1862 char_u *name = *arg;
1863 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1864 if (name_end == name || *name_end != '.')
1865 return NULL;
1866
1867 size_t len = name_end - name;
1868 typval_T tv;
1869 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001870 if (eval_variable(name, (int)len,
1871 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001872 return NULL;
1873 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001874 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001875
1876 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1877 : tv.vval.v_object->obj_class;
1878 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001879 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001880 char_u *fname = name_end + 1;
1881 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1882 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001883 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001884 len = fname_end - fname;
1885
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001886 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1887 : cl->class_obj_method_count;
1888 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1889 : cl->class_obj_methods;
1890 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001891 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001892 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001893 // Use a separate pointer to avoid that ASAN complains about
1894 // uf_name[] only being 4 characters.
1895 char_u *ufname = (char_u *)fp->uf_name;
1896 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001897 {
1898 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001899 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001900 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001901 }
1902
Bram Moolenaareb533502022-12-14 15:06:11 +00001903fail_after_eval:
1904 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001905 return NULL;
1906}
1907
1908/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001909 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1910 * index in class.class_class_members[].
1911 * If "cl_ret" is not NULL set it to the class.
1912 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001913 */
1914 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001915class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001916{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001917 if (cctx == NULL || cctx->ctx_ufunc == NULL
1918 || cctx->ctx_ufunc->uf_class == NULL)
1919 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001920 class_T *cl = cctx->ctx_ufunc->uf_class;
1921
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001922 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001923 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001924 ocmember_T *m = &cl->class_class_members[i];
1925 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001926 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001927 if (cl_ret != NULL)
1928 *cl_ret = cl;
1929 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001930 }
1931 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001932 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001933}
1934
1935/*
Bram Moolenaar62a69232023-01-24 15:07:04 +00001936 * Return TRUE if current context "cctx_arg" is inside class "cl".
1937 * Return FALSE if not.
1938 */
1939 int
1940inside_class(cctx_T *cctx_arg, class_T *cl)
1941{
1942 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
1943 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class == cl)
1944 return TRUE;
1945 return FALSE;
1946}
1947
1948/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001949 * Make a copy of an object.
1950 */
1951 void
1952copy_object(typval_T *from, typval_T *to)
1953{
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02001954 if (from->vval.v_object == NULL)
1955 to->vval.v_object = NULL;
1956 else
1957 {
1958 to->vval.v_object = from->vval.v_object;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001959 ++to->vval.v_object->obj_refcount;
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02001960 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001961}
1962
1963/*
1964 * Free an object.
1965 */
1966 static void
1967object_clear(object_T *obj)
1968{
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001969 // Avoid a recursive call, it can happen if "obj" has a circular reference.
1970 obj->obj_refcount = INT_MAX;
1971
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001972 class_T *cl = obj->obj_class;
1973
Jia-Ju Bai5b0889b2023-08-13 20:04:04 +02001974 if (!cl)
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02001975 return;
Jia-Ju Bai5b0889b2023-08-13 20:04:04 +02001976
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001977 // the member values are just after the object structure
1978 typval_T *tv = (typval_T *)(obj + 1);
1979 for (int i = 0; i < cl->class_obj_member_count; ++i)
1980 clear_tv(tv + i);
1981
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001982 // Remove from the list headed by "first_object".
1983 object_cleared(obj);
1984
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001985 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001986 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001987}
1988
1989/*
1990 * Unreference an object.
1991 */
1992 void
1993object_unref(object_T *obj)
1994{
1995 if (obj != NULL && --obj->obj_refcount <= 0)
1996 object_clear(obj);
1997}
1998
1999/*
2000 * Make a copy of a class.
2001 */
2002 void
2003copy_class(typval_T *from, typval_T *to)
2004{
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02002005 if (from->vval.v_class == NULL)
2006 to->vval.v_class = NULL;
2007 else
2008 {
2009 to->vval.v_class = from->vval.v_class;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002010 ++to->vval.v_class->class_refcount;
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02002011 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002012}
2013
2014/*
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02002015 * Free the class "cl" and its contents.
2016 */
2017 static void
2018class_free(class_T *cl)
2019{
2020 // Freeing what the class contains may recursively come back here.
2021 // Clear "class_name" first, if it is NULL the class does not need to
2022 // be freed.
2023 VIM_CLEAR(cl->class_name);
2024
2025 class_unref(cl->class_extends);
2026
2027 for (int i = 0; i < cl->class_interface_count; ++i)
2028 {
2029 vim_free(((char_u **)cl->class_interfaces)[i]);
2030 if (cl->class_interfaces_cl[i] != NULL)
2031 class_unref(cl->class_interfaces_cl[i]);
2032 }
2033 vim_free(cl->class_interfaces);
2034 vim_free(cl->class_interfaces_cl);
2035
2036 itf2class_T *next;
2037 for (itf2class_T *i2c = cl->class_itf2class; i2c != NULL; i2c = next)
2038 {
2039 next = i2c->i2c_next;
2040 vim_free(i2c);
2041 }
2042
2043 for (int i = 0; i < cl->class_class_member_count; ++i)
2044 {
2045 ocmember_T *m = &cl->class_class_members[i];
2046 vim_free(m->ocm_name);
2047 vim_free(m->ocm_init);
2048 if (cl->class_members_tv != NULL)
2049 clear_tv(&cl->class_members_tv[i]);
2050 }
2051 vim_free(cl->class_class_members);
2052 vim_free(cl->class_members_tv);
2053
2054 for (int i = 0; i < cl->class_obj_member_count; ++i)
2055 {
2056 ocmember_T *m = &cl->class_obj_members[i];
2057 vim_free(m->ocm_name);
2058 vim_free(m->ocm_init);
2059 }
2060 vim_free(cl->class_obj_members);
2061
2062 for (int i = 0; i < cl->class_class_function_count; ++i)
2063 {
2064 ufunc_T *uf = cl->class_class_functions[i];
2065 func_clear_free(uf, FALSE);
2066 }
2067 vim_free(cl->class_class_functions);
2068
2069 for (int i = 0; i < cl->class_obj_method_count; ++i)
2070 {
2071 ufunc_T *uf = cl->class_obj_methods[i];
2072 func_clear_free(uf, FALSE);
2073 }
2074 vim_free(cl->class_obj_methods);
2075
2076 clear_type_list(&cl->class_type_list);
2077
2078 class_cleared(cl);
2079
2080 vim_free(cl);
2081}
2082
2083/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002084 * Unreference a class. Free it when the reference count goes down to zero.
2085 */
2086 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002087class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002088{
Bram Moolenaard505d172022-12-18 21:42:55 +00002089 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02002090 class_free(cl);
2091}
2092
2093/*
2094 * Go through the list of all classes and free items without "copyID".
2095 */
2096 int
2097class_free_nonref(int copyID)
2098{
2099 int did_free = FALSE;
2100
2101 for (class_T *cl = first_class; cl != NULL; cl = next_nonref_class)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002102 {
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02002103 next_nonref_class = cl->class_next_used;
2104 if ((cl->class_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00002105 {
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02002106 // Free the class and items it contains.
2107 class_free(cl);
2108 did_free = TRUE;
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00002109 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002110 }
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02002111
2112 next_nonref_class = NULL;
2113 return did_free;
2114}
2115
2116 int
2117set_ref_in_classes(int copyID)
2118{
2119 for (class_T *cl = first_class; cl != NULL; cl = cl->class_next_used)
2120 set_ref_in_item_class(cl, copyID, NULL, NULL);
2121
2122 return FALSE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002123}
2124
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002125static object_T *first_object = NULL;
2126
2127/*
2128 * Call this function when an object has been created. It will be added to the
2129 * list headed by "first_object".
2130 */
2131 void
2132object_created(object_T *obj)
2133{
2134 if (first_object != NULL)
2135 {
2136 obj->obj_next_used = first_object;
2137 first_object->obj_prev_used = obj;
2138 }
2139 first_object = obj;
2140}
2141
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01002142static object_T *next_nonref_obj = NULL;
2143
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002144/*
2145 * Call this function when an object has been cleared and is about to be freed.
2146 * It is removed from the list headed by "first_object".
2147 */
2148 void
2149object_cleared(object_T *obj)
2150{
2151 if (obj->obj_next_used != NULL)
2152 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
2153 if (obj->obj_prev_used != NULL)
2154 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
2155 else if (first_object == obj)
2156 first_object = obj->obj_next_used;
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01002157
2158 // update the next object to check if needed
2159 if (obj == next_nonref_obj)
2160 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002161}
2162
2163/*
2164 * Go through the list of all objects and free items without "copyID".
2165 */
2166 int
2167object_free_nonref(int copyID)
2168{
2169 int did_free = FALSE;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002170
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01002171 for (object_T *obj = first_object; obj != NULL; obj = next_nonref_obj)
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002172 {
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01002173 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002174 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
2175 {
2176 // Free the object and items it contains.
2177 object_clear(obj);
2178 did_free = TRUE;
2179 }
2180 }
2181
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01002182 next_nonref_obj = NULL;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002183 return did_free;
2184}
2185
LemonBoyafe04662023-08-23 21:08:11 +02002186/*
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02002187 * Return TRUE when the class "cl", its base class or one of the implemented
2188 * interfaces matches the class "other_cl".
LemonBoyafe04662023-08-23 21:08:11 +02002189 */
2190 int
2191class_instance_of(class_T *cl, class_T *other_cl)
2192{
2193 if (cl == other_cl)
2194 return TRUE;
2195
2196 // Recursively check the base classes.
2197 for (; cl != NULL; cl = cl->class_extends)
2198 {
2199 if (cl == other_cl)
2200 return TRUE;
2201 // Check the implemented interfaces.
2202 for (int i = cl->class_interface_count - 1; i >= 0; --i)
2203 if (cl->class_interfaces_cl[i] == other_cl)
2204 return TRUE;
2205 }
2206
2207 return FALSE;
2208}
2209
2210/*
2211 * "instanceof(object, classinfo)" function
2212 */
2213 void
2214f_instanceof(typval_T *argvars, typval_T *rettv)
2215{
2216 typval_T *object_tv = &argvars[0];
2217 typval_T *classinfo_tv = &argvars[1];
2218 listitem_T *li;
2219
2220 rettv->vval.v_number = VVAL_FALSE;
2221
2222 if (check_for_object_arg(argvars, 0) == FAIL
2223 || check_for_class_or_list_arg(argvars, 1) == FAIL)
2224 return;
2225
2226 if (classinfo_tv->v_type == VAR_LIST)
2227 {
2228 FOR_ALL_LIST_ITEMS(classinfo_tv->vval.v_list, li)
2229 {
2230 if (li->li_tv.v_type != VAR_CLASS)
2231 {
2232 emsg(_(e_class_required));
2233 return;
2234 }
2235
2236 if (class_instance_of(object_tv->vval.v_object->obj_class,
2237 li->li_tv.vval.v_class) == TRUE)
2238 {
2239 rettv->vval.v_number = VVAL_TRUE;
2240 return;
2241 }
2242 }
2243 }
2244 else if (classinfo_tv->v_type == VAR_CLASS)
2245 {
2246 rettv->vval.v_number = class_instance_of(object_tv->vval.v_object->obj_class,
2247 classinfo_tv->vval.v_class);
2248 }
2249}
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00002250
2251#endif // FEAT_EVAL