blob: 4c0c13fd383e6962aa6d3d7c53002a9cae96aef8 [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
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +020027 * variable name and "type_ret" is set to the declared or detected type.
Bram Moolenaard505d172022-12-18 21:42:55 +000028 * "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
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +020070 char_u *init_arg = skipwhite(type_arg);
71 if (type == NULL && *init_arg != '=')
Bram Moolenaard505d172022-12-18 21:42:55 +000072 {
73 emsg(_(e_type_or_initialization_required));
74 return FAIL;
75 }
76
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +020077 if (init_expr == NULL && *init_arg == '=')
Bram Moolenaard505d172022-12-18 21:42:55 +000078 {
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +020079 emsg(_(e_cannot_initialize_member_in_interface));
80 return FAIL;
81 }
82
83 if (*init_arg == '=')
84 {
85 evalarg_T evalarg;
86 char_u *expr_start, *expr_end;
87
88 if (!VIM_ISWHITE(init_arg[-1]) || !VIM_ISWHITE(init_arg[1]))
Bram Moolenaard505d172022-12-18 21:42:55 +000089 {
90 semsg(_(e_white_space_required_before_and_after_str_at_str),
91 "=", type_arg);
92 return FAIL;
93 }
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +020094 init_arg = skipwhite(init_arg + 1);
Bram Moolenaard505d172022-12-18 21:42:55 +000095
Bram Moolenaard505d172022-12-18 21:42:55 +000096 fill_evalarg_from_eap(&evalarg, eap, FALSE);
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +020097 (void)skip_expr_concatenate(&init_arg, &expr_start, &expr_end, &evalarg);
Bram Moolenaard505d172022-12-18 21:42:55 +000098
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +020099 // No type specified for the member. Set it to "any" and the correct
100 // type will be set when the object is instantiated.
Bram Moolenaard505d172022-12-18 21:42:55 +0000101 if (type == NULL)
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200102 type = &t_any;
Bram Moolenaard505d172022-12-18 21:42:55 +0000103
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200104 *init_expr = vim_strnsave(expr_start, expr_end - expr_start);
105 // Free the memory pointed by expr_start.
Bram Moolenaard505d172022-12-18 21:42:55 +0000106 clear_evalarg(&evalarg, NULL);
107 }
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200108 else if (!valid_declaration_type(type))
Bram Moolenaard505d172022-12-18 21:42:55 +0000109 return FAIL;
110
111 *type_ret = type;
Bram Moolenaard505d172022-12-18 21:42:55 +0000112 return OK;
113}
114
115/*
116 * Add a member to an object or a class.
117 * Returns OK when successful, "init_expr" will be consumed then.
118 * Returns FAIL otherwise, caller might need to free "init_expr".
119 */
120 static int
121add_member(
122 garray_T *gap,
123 char_u *varname,
124 char_u *varname_end,
125 int has_public,
126 type_T *type,
127 char_u *init_expr)
128{
129 if (ga_grow(gap, 1) == FAIL)
130 return FAIL;
131 ocmember_T *m = ((ocmember_T *)gap->ga_data) + gap->ga_len;
132 m->ocm_name = vim_strnsave(varname, varname_end - varname);
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +0000133 m->ocm_access = has_public ? VIM_ACCESS_ALL
134 : *varname == '_' ? VIM_ACCESS_PRIVATE : VIM_ACCESS_READ;
Bram Moolenaard505d172022-12-18 21:42:55 +0000135 m->ocm_type = type;
136 if (init_expr != NULL)
137 m->ocm_init = init_expr;
138 ++gap->ga_len;
139 return OK;
140}
141
142/*
143 * Move the class or object members found while parsing a class into the class.
144 * "gap" contains the found members.
Bram Moolenaar83677162023-01-08 19:54:10 +0000145 * "parent_members" points to the members in the parent class (if any)
146 * "parent_count" is the number of members in the parent class
Bram Moolenaard505d172022-12-18 21:42:55 +0000147 * "members" will be set to the newly allocated array of members and
148 * "member_count" set to the number of members.
149 * Returns OK or FAIL.
150 */
151 static int
152add_members_to_class(
153 garray_T *gap,
Bram Moolenaar83677162023-01-08 19:54:10 +0000154 ocmember_T *parent_members,
155 int parent_count,
Bram Moolenaard505d172022-12-18 21:42:55 +0000156 ocmember_T **members,
157 int *member_count)
158{
Bram Moolenaar83677162023-01-08 19:54:10 +0000159 *member_count = parent_count + gap->ga_len;
160 *members = *member_count == 0 ? NULL
161 : ALLOC_MULT(ocmember_T, *member_count);
162 if (*member_count > 0 && *members == NULL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000163 return FAIL;
Bram Moolenaar83677162023-01-08 19:54:10 +0000164 for (int i = 0; i < parent_count; ++i)
165 {
166 // parent members need to be copied
Bram Moolenaarae3205a2023-01-15 20:49:00 +0000167 ocmember_T *m = *members + i;
168 *m = parent_members[i];
169 m->ocm_name = vim_strsave(m->ocm_name);
170 if (m->ocm_init != NULL)
171 m->ocm_init = vim_strsave(m->ocm_init);
Bram Moolenaar83677162023-01-08 19:54:10 +0000172 }
Bram Moolenaar8efdcee2022-12-19 12:18:09 +0000173 if (gap->ga_len > 0)
Bram Moolenaar83677162023-01-08 19:54:10 +0000174 // new members are moved
175 mch_memmove(*members + parent_count,
176 gap->ga_data, sizeof(ocmember_T) * gap->ga_len);
Bram Moolenaard505d172022-12-18 21:42:55 +0000177 VIM_CLEAR(gap->ga_data);
178 return OK;
179}
180
181/*
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000182 * Convert a member index "idx" of interface "itf" to the member index of class
183 * "cl" implementing that interface.
184 */
185 int
Bram Moolenaard0200c82023-01-28 15:19:40 +0000186object_index_from_itf_index(class_T *itf, int is_method, int idx, class_T *cl)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000187{
Bram Moolenaard0200c82023-01-28 15:19:40 +0000188 if (idx > (is_method ? itf->class_obj_method_count
189 : itf->class_obj_member_count))
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000190 {
191 siemsg("index %d out of range for interface %s", idx, itf->class_name);
192 return 0;
193 }
Yegappan Lakshmanan74cc13c2023-08-13 17:41:26 +0200194
195 // If "cl" is the interface or the class that is extended, then the method
196 // index can be used directly and there is no need to search for the method
197 // index in one of the child classes.
198 if (cl == itf)
199 return idx;
200
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000201 itf2class_T *i2c;
202 for (i2c = itf->class_itf2class; i2c != NULL; i2c = i2c->i2c_next)
Bram Moolenaard0200c82023-01-28 15:19:40 +0000203 if (i2c->i2c_class == cl && i2c->i2c_is_method == is_method)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000204 break;
205 if (i2c == NULL)
206 {
207 siemsg("class %s not found on interface %s",
208 cl->class_name, itf->class_name);
209 return 0;
210 }
211 int *table = (int *)(i2c + 1);
212 return table[idx];
213}
214
215/*
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200216 * Check whether a class named "extends_name" is present. If the class is
217 * valid, then "extends_clp" is set with the class pointer.
218 * Returns TRUE if the class name "extends_names" is a valid class.
219 */
220 static int
221validate_extends_class(char_u *extends_name, class_T **extends_clp)
222{
223 typval_T tv;
224 int success = FALSE;
225
226 tv.v_type = VAR_UNKNOWN;
227 if (eval_variable_import(extends_name, &tv) == FAIL)
228 {
229 semsg(_(e_class_name_not_found_str), extends_name);
230 return success;
231 }
232 else
233 {
234 if (tv.v_type != VAR_CLASS
235 || tv.vval.v_class == NULL
236 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) != 0)
237 semsg(_(e_cannot_extend_str), extends_name);
238 else
239 {
240 class_T *extends_cl = tv.vval.v_class;
241 ++extends_cl->class_refcount;
242 *extends_clp = extends_cl;
243 success = TRUE;
244 }
245 clear_tv(&tv);
246 }
247
248 return success;
249}
250
251/*
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200252 * Check whether a class/object member variable in "classmembers_gap" /
253 * "objmembers_gap" is a duplicate of a member in any of the extended parent
254 * class lineage. Returns TRUE if there are no duplicates.
255 */
256 static int
257validate_extends_members(
258 garray_T *classmembers_gap,
259 garray_T *objmembers_gap,
260 class_T *extends_cl)
261{
262 for (int loop = 1; loop <= 2; ++loop)
263 {
264 // loop == 1: check class members
265 // loop == 2: check object members
266 int member_count = loop == 1 ? classmembers_gap->ga_len
267 : objmembers_gap->ga_len;
268 if (member_count == 0)
269 continue;
270 ocmember_T *members = (ocmember_T *)(loop == 1
271 ? classmembers_gap->ga_data
272 : objmembers_gap->ga_data);
273
274 // Validate each member variable
275 for (int c_i = 0; c_i < member_count; c_i++)
276 {
277 class_T *p_cl = extends_cl;
278 ocmember_T *c_m = members + c_i;
279 char_u *pstr = (*c_m->ocm_name == '_')
280 ? c_m->ocm_name + 1 : c_m->ocm_name;
281
282 // Check in all the parent classes in the lineage
283 while (p_cl != NULL)
284 {
285 int p_member_count = loop == 1
286 ? p_cl->class_class_member_count
287 : p_cl->class_obj_member_count;
288 if (p_member_count == 0)
289 continue;
290 ocmember_T *p_members = (loop == 1
291 ? p_cl->class_class_members
292 : p_cl->class_obj_members);
293
294 // Compare against all the members in the parent class
295 for (int p_i = 0; p_i < p_member_count; p_i++)
296 {
297 ocmember_T *p_m = p_members + p_i;
298 char_u *qstr = (*p_m->ocm_name == '_')
299 ? p_m->ocm_name + 1 : p_m->ocm_name;
300 if (STRCMP(pstr, qstr) == 0)
301 {
302 semsg(_(e_duplicate_member_str), c_m->ocm_name);
303 return FALSE;
304 }
305 }
306
307 p_cl = p_cl->class_extends;
308 }
309 }
310 }
311
312 return TRUE;
313}
314
315/*
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200316 * Check the members of the interface class "ifcl" match the class members
317 * ("classmembers_gap") and object members ("objmembers_gap") of a class.
318 * Returns TRUE if the class and object member names are valid.
319 */
320 static int
321validate_interface_members(
322 char_u *intf_class_name,
323 class_T *ifcl,
324 garray_T *classmembers_gap,
325 garray_T *objmembers_gap)
326{
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200327 for (int loop = 1; loop <= 2; ++loop)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200328 {
329 // loop == 1: check class members
330 // loop == 2: check object members
331 int if_count = loop == 1 ? ifcl->class_class_member_count
332 : ifcl->class_obj_member_count;
333 if (if_count == 0)
334 continue;
335 ocmember_T *if_ms = loop == 1 ? ifcl->class_class_members
336 : ifcl->class_obj_members;
337 ocmember_T *cl_ms = (ocmember_T *)(loop == 1
338 ? classmembers_gap->ga_data
339 : objmembers_gap->ga_data);
340 int cl_count = loop == 1 ? classmembers_gap->ga_len
341 : objmembers_gap->ga_len;
342 for (int if_i = 0; if_i < if_count; ++if_i)
343 {
344 int cl_i;
345 for (cl_i = 0; cl_i < cl_count; ++cl_i)
346 {
347 ocmember_T *m = &cl_ms[cl_i];
348 where_T where = WHERE_INIT;
349
350 if (STRCMP(if_ms[if_i].ocm_name, m->ocm_name) != 0)
351 continue;
352
353 // Ensure the type is matching.
354 where.wt_func_name = (char *)m->ocm_name;
355 where.wt_kind = WT_MEMBER;
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200356 if (check_type(if_ms[if_i].ocm_type, m->ocm_type, TRUE,
357 where) == FAIL)
358 return FALSE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200359
360 break;
361 }
362 if (cl_i == cl_count)
363 {
364 semsg(_(e_member_str_of_interface_str_not_implemented),
365 if_ms[if_i].ocm_name, intf_class_name);
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200366 return FALSE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200367 }
368 }
369 }
370
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200371 return TRUE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200372}
373
374/*
375 * Check the functions/methods of the interface class "ifcl" match the class
376 * methods ("classfunctions_gap") and object functions ("objmemthods_gap") of a
377 * class.
378 * Returns TRUE if the class and object member names are valid.
379 */
380 static int
381validate_interface_methods(
382 char_u *intf_class_name,
383 class_T *ifcl,
384 garray_T *classfunctions_gap,
385 garray_T *objmethods_gap)
386{
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200387 for (int loop = 1; loop <= 2; ++loop)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200388 {
389 // loop == 1: check class functions
390 // loop == 2: check object methods
391 int if_count = loop == 1 ? ifcl->class_class_function_count
392 : ifcl->class_obj_method_count;
393 if (if_count == 0)
394 continue;
395 ufunc_T **if_fp = loop == 1 ? ifcl->class_class_functions
396 : ifcl->class_obj_methods;
397 ufunc_T **cl_fp = (ufunc_T **)(loop == 1
398 ? classfunctions_gap->ga_data
399 : objmethods_gap->ga_data);
400 int cl_count = loop == 1 ? classfunctions_gap->ga_len
401 : objmethods_gap->ga_len;
402 for (int if_i = 0; if_i < if_count; ++if_i)
403 {
404 char_u *if_name = if_fp[if_i]->uf_name;
405 int cl_i;
406 for (cl_i = 0; cl_i < cl_count; ++cl_i)
407 {
408 char_u *cl_name = cl_fp[cl_i]->uf_name;
409 if (STRCMP(if_name, cl_name) == 0)
410 {
411 where_T where = WHERE_INIT;
412
413 // Ensure the type is matching.
414 where.wt_func_name = (char *)if_name;
415 where.wt_kind = WT_METHOD;
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200416 if (check_type(if_fp[if_i]->uf_func_type,
417 cl_fp[cl_i]->uf_func_type, TRUE, where) == FAIL)
418 return FALSE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200419 break;
420 }
421 }
422 if (cl_i == cl_count)
423 {
424 semsg(_(e_function_str_of_interface_str_not_implemented),
425 if_name, intf_class_name);
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200426 return FALSE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200427 }
428 }
429 }
430
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200431 return TRUE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200432}
433
434/*
435 * Validate all the "implements" classes when creating a new class. The
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200436 * classes are returned in "intf_classes". The class functions, class members,
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200437 * object methods and object members in the new class are in
438 * "classfunctions_gap", "classmembers_gap", "objmethods_gap", and
439 * "objmembers_gap" respectively.
440 */
441 static int
442validate_implements_classes(
443 garray_T *impl_gap,
444 class_T **intf_classes,
445 garray_T *classfunctions_gap,
446 garray_T *classmembers_gap,
447 garray_T *objmethods_gap,
448 garray_T *objmembers_gap)
449{
450 int success = TRUE;
451
452 for (int i = 0; i < impl_gap->ga_len && success; ++i)
453 {
454 char_u *impl = ((char_u **)impl_gap->ga_data)[i];
455 typval_T tv;
456 tv.v_type = VAR_UNKNOWN;
457 if (eval_variable_import(impl, &tv) == FAIL)
458 {
459 semsg(_(e_interface_name_not_found_str), impl);
460 success = FALSE;
461 break;
462 }
463
464 if (tv.v_type != VAR_CLASS
465 || tv.vval.v_class == NULL
466 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0)
467 {
468 semsg(_(e_not_valid_interface_str), impl);
469 success = FALSE;
470 clear_tv(&tv);
471 break;
472 }
473
474 class_T *ifcl = tv.vval.v_class;
475 intf_classes[i] = ifcl;
476 ++ifcl->class_refcount;
477
478 // check the members of the interface match the members of the class
479 success = validate_interface_members(impl, ifcl, classmembers_gap,
480 objmembers_gap);
481
482 // check the functions/methods of the interface match the
483 // functions/methods of the class
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200484 if (success)
485 success = validate_interface_methods(impl, ifcl,
486 classfunctions_gap, objmethods_gap);
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200487 clear_tv(&tv);
488 }
489
490 return success;
491}
492
493/*
494 * Check no function argument name is used as a class member.
495 * (Object members are always accessed with "this." prefix, so no need
496 * to check them.)
497 */
498 static int
499check_func_arg_names(
500 garray_T *classfunctions_gap,
501 garray_T *objmethods_gap,
502 garray_T *classmembers_gap)
503{
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200504 // loop 1: class functions, loop 2: object methods
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200505 for (int loop = 1; loop <= 2; ++loop)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200506 {
507 garray_T *gap = loop == 1 ? classfunctions_gap : objmethods_gap;
508
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200509 for (int fi = 0; fi < gap->ga_len; ++fi)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200510 {
511 ufunc_T *uf = ((ufunc_T **)gap->ga_data)[fi];
512
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200513 for (int i = 0; i < uf->uf_args.ga_len; ++i)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200514 {
515 char_u *aname = ((char_u **)uf->uf_args.ga_data)[i];
516 garray_T *mgap = classmembers_gap;
517
518 // Check all the class member names
519 for (int mi = 0; mi < mgap->ga_len; ++mi)
520 {
521 char_u *mname = ((ocmember_T *)mgap->ga_data + mi)
522 ->ocm_name;
523 if (STRCMP(aname, mname) == 0)
524 {
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200525 if (uf->uf_script_ctx.sc_sid > 0)
526 SOURCING_LNUM = uf->uf_script_ctx.sc_lnum;
527
528 semsg(_(e_argument_already_declared_in_class_str),
529 aname);
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200530
531 return FALSE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200532 }
533 }
534 }
535 }
536 }
537
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200538 return TRUE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200539}
540
541/*
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +0200542 * Returns TRUE if the member "varname" is already defined.
543 */
544 static int
545is_duplicate_member(garray_T *mgap, char_u *varname, char_u *varname_end)
546{
547 char_u *pstr = (*varname == '_') ? varname + 1 : varname;
548
549 for (int i = 0; i < mgap->ga_len; ++i)
550 {
551 ocmember_T *m = ((ocmember_T *)mgap->ga_data) + i;
552 char_u *qstr = *m->ocm_name == '_' ? m->ocm_name + 1 : m->ocm_name;
553 if (STRNCMP(pstr, qstr, varname_end - pstr) == 0)
554 {
555 char_u *name = vim_strnsave(varname, varname_end - varname);
556 semsg(_(e_duplicate_member_str), name);
557 vim_free(name);
558 return TRUE;
559 }
560 }
561
562 return FALSE;
563}
564
565/*
566 * Returns TRUE if the method "name" is already defined.
567 */
568 static int
569is_duplicate_method(garray_T *fgap, char_u *name)
570{
571 char_u *pstr = (*name == '_') ? name + 1 : name;
572
573 for (int i = 0; i < fgap->ga_len; ++i)
574 {
575 char_u *n = ((ufunc_T **)fgap->ga_data)[i]->uf_name;
576 char_u *qstr = *n == '_' ? n + 1 : n;
577 if (STRCMP(pstr, qstr) == 0)
578 {
579 semsg(_(e_duplicate_function_str), name);
580 return TRUE;
581 }
582 }
583
584 return FALSE;
585}
586
587/*
Gianmaria Bajo4b9777a2023-08-29 22:26:30 +0200588 * Returns TRUE if the constructor is valid.
589 */
590 static int
591is_valid_constructor(ufunc_T *uf, int is_abstract, int has_static)
592{
593 // Constructors are not allowed in abstract classes.
594 if (is_abstract)
595 {
596 emsg(_(e_cannot_define_new_function_in_abstract_class));
597 return FALSE;
598 }
599 // A constructor is always static, no need to define it so.
600 if (has_static)
601 {
602 emsg(_(e_cannot_define_new_function_as_static));
603 return FALSE;
604 }
605 // A return type should not be specified for the new()
606 // constructor method.
607 if (uf->uf_ret_type->tt_type != VAR_VOID)
608 {
609 emsg(_(e_cannot_use_a_return_type_with_new));
610 return FALSE;
611 }
612 return TRUE;
613}
614
615/*
Yegappan Lakshmananb1027282023-08-19 11:26:42 +0200616 * Update the interface class lookup table for the member index on the
617 * interface to the member index in the class implementing the interface.
618 * And a lookup table for the object method index on the interface
619 * to the object method index in the class implementing the interface.
620 * This is also used for updating the lookup table for the extended class
621 * hierarchy.
622 */
623 static int
624update_member_method_lookup_table(
625 class_T *ifcl,
626 class_T *cl,
627 garray_T *objmethods,
628 int pobj_method_offset,
629 int is_interface)
630{
631 if (ifcl == NULL)
632 return OK;
633
634 // Table for members.
635 itf2class_T *if2cl = alloc_clear(sizeof(itf2class_T)
636 + ifcl->class_obj_member_count * sizeof(int));
637 if (if2cl == NULL)
638 return FAIL;
639 if2cl->i2c_next = ifcl->class_itf2class;
640 ifcl->class_itf2class = if2cl;
641 if2cl->i2c_class = cl;
642 if2cl->i2c_is_method = FALSE;
643
644 for (int if_i = 0; if_i < ifcl->class_obj_member_count; ++if_i)
645 for (int cl_i = 0; cl_i < cl->class_obj_member_count; ++cl_i)
646 {
647 if (STRCMP(ifcl->class_obj_members[if_i].ocm_name,
648 cl->class_obj_members[cl_i].ocm_name) == 0)
649 {
650 int *table = (int *)(if2cl + 1);
651 table[if_i] = cl_i;
652 break;
653 }
654 }
655
656 // Table for methods.
657 if2cl = alloc_clear(sizeof(itf2class_T)
658 + ifcl->class_obj_method_count * sizeof(int));
659 if (if2cl == NULL)
660 return FAIL;
661 if2cl->i2c_next = ifcl->class_itf2class;
662 ifcl->class_itf2class = if2cl;
663 if2cl->i2c_class = cl;
664 if2cl->i2c_is_method = TRUE;
665
666 for (int if_i = 0; if_i < ifcl->class_obj_method_count; ++if_i)
667 {
668 int done = FALSE;
669 for (int cl_i = 0; cl_i < objmethods->ga_len; ++cl_i)
670 {
671 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
672 ((ufunc_T **)objmethods->ga_data)[cl_i]->uf_name)
673 == 0)
674 {
675 int *table = (int *)(if2cl + 1);
676 table[if_i] = cl_i;
677 done = TRUE;
678 break;
679 }
680 }
681
682 // extended class object method is not overridden by the child class.
683 // Keep the method declared in one of the parent classes in the
684 // lineage.
685 if (!done && !is_interface)
686 {
687 // If "ifcl" is not the immediate parent of "cl", then search in
688 // the intermediate parent classes.
689 if (cl->class_extends != ifcl)
690 {
691 class_T *parent = cl->class_extends;
692 int method_offset = objmethods->ga_len;
693
694 while (!done && parent != NULL && parent != ifcl)
695 {
696
697 for (int cl_i = 0;
698 cl_i < parent->class_obj_method_count_child; ++cl_i)
699 {
700 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
701 parent->class_obj_methods[cl_i]->uf_name)
702 == 0)
703 {
704 int *table = (int *)(if2cl + 1);
705 table[if_i] = method_offset + cl_i;
706 done = TRUE;
707 break;
708 }
709 }
710 method_offset += parent->class_obj_method_count_child;
711 parent = parent->class_extends;
712 }
713 }
714
715 if (!done)
716 {
717 int *table = (int *)(if2cl + 1);
718 table[if_i] = pobj_method_offset + if_i;
719 }
720 }
721 }
722
723 return OK;
724}
725
726/*
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200727 * Update the member and object method lookup tables for a new class in the
728 * interface class.
729 * For each interface add a lookup table for the member index on the interface
730 * to the member index in the new class. And a lookup table for the object
731 * method index on the interface to the object method index in the new class.
732 */
733 static int
734add_lookup_tables(class_T *cl, class_T *extends_cl, garray_T *objmethods_gap)
735{
736 for (int i = 0; i < cl->class_interface_count; ++i)
737 {
738 class_T *ifcl = cl->class_interfaces_cl[i];
739
740 if (update_member_method_lookup_table(ifcl, cl, objmethods_gap,
741 0, TRUE) == FAIL)
742 return FAIL;
743 }
744
745 // Update the lookup table for the extended class, if nay
746 if (extends_cl != NULL)
747 {
748 class_T *pclass = extends_cl;
749 int pobj_method_offset = objmethods_gap->ga_len;
750
751 // Update the entire lineage of extended classes.
752 while (pclass != NULL)
753 {
754 if (update_member_method_lookup_table(pclass, cl,
755 objmethods_gap, pobj_method_offset, FALSE) == FAIL)
756 return FAIL;
757
758 pobj_method_offset += pclass->class_obj_method_count_child;
759 pclass = pclass->class_extends;
760 }
761 }
762
763 return OK;
764}
765
766/*
767 * Add class members to a new class. Allocate a typval for each class member
768 * and initialize it.
769 */
770 static void
771add_class_members(class_T *cl, exarg_T *eap)
772{
773 // Allocate a typval for each class member and initialize it.
774 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
775 cl->class_class_member_count);
776 if (cl->class_members_tv == NULL)
777 return;
778
779 for (int i = 0; i < cl->class_class_member_count; ++i)
780 {
781 ocmember_T *m = &cl->class_class_members[i];
782 typval_T *tv = &cl->class_members_tv[i];
783 if (m->ocm_init != NULL)
784 {
785 typval_T *etv = eval_expr(m->ocm_init, eap);
786 if (etv != NULL)
787 {
788 *tv = *etv;
789 vim_free(etv);
790 }
791 }
792 else
793 {
794 // TODO: proper default value
795 tv->v_type = m->ocm_type->tt_type;
796 tv->vval.v_string = NULL;
797 }
798 }
799}
800
801/*
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +0200802 * Add a default constructor method (new()) to the class "cl".
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200803 */
804 static void
805add_default_constructor(
806 class_T *cl,
807 garray_T *classfunctions_gap,
808 garray_T *type_list_gap)
809{
810 garray_T fga;
811
812 ga_init2(&fga, 1, 1000);
813 ga_concat(&fga, (char_u *)"new(");
814 for (int i = 0; i < cl->class_obj_member_count; ++i)
815 {
816 if (i > 0)
817 ga_concat(&fga, (char_u *)", ");
818 ga_concat(&fga, (char_u *)"this.");
819 ocmember_T *m = cl->class_obj_members + i;
820 ga_concat(&fga, (char_u *)m->ocm_name);
821 ga_concat(&fga, (char_u *)" = v:none");
822 }
823 ga_concat(&fga, (char_u *)")\nenddef\n");
824 ga_append(&fga, NUL);
825
826 exarg_T fea;
827 CLEAR_FIELD(fea);
828 fea.cmdidx = CMD_def;
829 fea.cmd = fea.arg = fga.ga_data;
830
831 garray_T lines_to_free;
832 ga_init2(&lines_to_free, sizeof(char_u *), 50);
833
834 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS);
835
836 ga_clear_strings(&lines_to_free);
837 vim_free(fga.ga_data);
838
839 if (nf != NULL && ga_grow(classfunctions_gap, 1) == OK)
840 {
841 ((ufunc_T **)classfunctions_gap->ga_data)[classfunctions_gap->ga_len]
842 = nf;
843 ++classfunctions_gap->ga_len;
844
845 nf->uf_flags |= FC_NEW;
846 nf->uf_ret_type = get_type_ptr(type_list_gap);
847 if (nf->uf_ret_type != NULL)
848 {
849 nf->uf_ret_type->tt_type = VAR_OBJECT;
850 nf->uf_ret_type->tt_class = cl;
851 nf->uf_ret_type->tt_argcount = 0;
852 nf->uf_ret_type->tt_args = NULL;
853 }
854 }
855}
856
857/*
858 * Add the class functions and object methods to the new class "cl".
859 * When extending a class, add the functions and methods from the parent class
860 * also.
861 */
862 static int
863add_classfuncs_objmethods(
864 class_T *cl,
865 class_T *extends_cl,
866 garray_T *classfunctions_gap,
867 garray_T *objmethods_gap)
868{
869 // loop 1: class functions, loop 2: object methods
870 for (int loop = 1; loop <= 2; ++loop)
871 {
872 garray_T *gap = loop == 1 ? classfunctions_gap : objmethods_gap;
873 int *fcount = loop == 1 ? &cl->class_class_function_count
874 : &cl->class_obj_method_count;
875 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
876 : &cl->class_obj_methods;
877
878 int parent_count = 0;
879 if (extends_cl != NULL)
880 // Include functions from the parent.
881 parent_count = loop == 1
882 ? extends_cl->class_class_function_count
883 : extends_cl->class_obj_method_count;
884
885 *fcount = parent_count + gap->ga_len;
886 if (*fcount == 0)
887 {
888 *fup = NULL;
889 continue;
890 }
891 *fup = ALLOC_MULT(ufunc_T *, *fcount);
892 if (*fup == NULL)
893 return FAIL;
894
895 if (gap->ga_len != 0)
896 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
897 vim_free(gap->ga_data);
898 if (loop == 1)
899 cl->class_class_function_count_child = gap->ga_len;
900 else
901 cl->class_obj_method_count_child = gap->ga_len;
902
903 int skipped = 0;
904 for (int i = 0; i < parent_count; ++i)
905 {
906 // Copy functions from the parent. Can't use the same
907 // function, because "uf_class" is different and compilation
908 // will have a different result.
909 // Put them after the functions in the current class, object
910 // methods may be overruled, then "super.Method()" is used to
911 // find a method from the parent.
912 // Skip "new" functions. TODO: not all of them.
913 if (loop == 1 && STRNCMP(
914 extends_cl->class_class_functions[i]->uf_name,
915 "new", 3) == 0)
916 ++skipped;
917 else
918 {
919 ufunc_T *pf = (loop == 1
920 ? extends_cl->class_class_functions
921 : extends_cl->class_obj_methods)[i];
922 (*fup)[gap->ga_len + i - skipped] = copy_function(pf);
923
924 // If the child class overrides a function from the parent
925 // the signature must be equal.
926 char_u *pname = pf->uf_name;
927 for (int ci = 0; ci < gap->ga_len; ++ci)
928 {
929 ufunc_T *cf = (*fup)[ci];
930 char_u *cname = cf->uf_name;
931 if (STRCMP(pname, cname) == 0)
932 {
933 where_T where = WHERE_INIT;
934 where.wt_func_name = (char *)pname;
935 where.wt_kind = WT_METHOD;
936 (void)check_type(pf->uf_func_type, cf->uf_func_type,
937 TRUE, where);
938 }
939 }
940 }
941 }
942
943 *fcount -= skipped;
944
945 // Set the class pointer on all the functions and object methods.
946 for (int i = 0; i < *fcount; ++i)
947 {
948 ufunc_T *fp = (*fup)[i];
949 fp->uf_class = cl;
950 if (loop == 2)
951 fp->uf_flags |= FC_OBJECT;
952 }
953 }
954
955 return OK;
956}
957
958/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000959 * Handle ":class" and ":abstract class" up to ":endclass".
Bram Moolenaar554d0312023-01-05 19:59:18 +0000960 * Handle ":interface" up to ":endinterface".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000961 */
962 void
963ex_class(exarg_T *eap)
964{
Bram Moolenaar83ae6152023-02-25 19:59:31 +0000965 int is_class = eap->cmdidx == CMD_class; // FALSE for :interface
966 long start_lnum = SOURCING_LNUM;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000967
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000968 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000969 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000970 if (is_abstract)
971 {
972 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
973 {
974 semsg(_(e_invalid_argument_str), arg);
975 return;
976 }
977 arg = skipwhite(arg + 5);
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000978 is_class = TRUE;
979 }
980
981 if (!current_script_is_vim9()
982 || (cmdmod.cmod_flags & CMOD_LEGACY)
983 || !getline_equal(eap->getline, eap->cookie, getsourceline))
984 {
985 if (is_class)
986 emsg(_(e_class_can_only_be_defined_in_vim9_script));
987 else
988 emsg(_(e_interface_can_only_be_defined_in_vim9_script));
989 return;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000990 }
991
992 if (!ASCII_ISUPPER(*arg))
993 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000994 if (is_class)
995 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
996 else
997 semsg(_(e_interface_name_must_start_with_uppercase_letter_str),
998 arg);
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000999 return;
1000 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001001 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
1002 if (!IS_WHITE_OR_NUL(*name_end))
1003 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001004 semsg(_(e_white_space_required_after_name_str), arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001005 return;
1006 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001007 char_u *name_start = arg;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001008
Bram Moolenaara86655a2023-01-12 17:06:27 +00001009 // "export class" gets used when creating the class, don't use "is_export"
1010 // for the items inside the class.
1011 int class_export = is_export;
1012 is_export = FALSE;
1013
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001014 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001015 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001016
Bram Moolenaar83677162023-01-08 19:54:10 +00001017 // Name for "extends BaseClass"
1018 char_u *extends = NULL;
1019
Bram Moolenaar94674f22023-01-06 18:42:20 +00001020 // Names for "implements SomeInterface"
1021 garray_T ga_impl;
1022 ga_init2(&ga_impl, sizeof(char_u *), 5);
1023
1024 arg = skipwhite(name_end);
1025 while (*arg != NUL && *arg != '#' && *arg != '\n')
1026 {
1027 // TODO:
Bram Moolenaar94674f22023-01-06 18:42:20 +00001028 // specifies SomeInterface
Bram Moolenaar83677162023-01-08 19:54:10 +00001029 if (STRNCMP(arg, "extends", 7) == 0 && IS_WHITE_OR_NUL(arg[7]))
1030 {
1031 if (extends != NULL)
1032 {
1033 emsg(_(e_duplicate_extends));
1034 goto early_ret;
1035 }
1036 arg = skipwhite(arg + 7);
1037 char_u *end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
1038 if (!IS_WHITE_OR_NUL(*end))
1039 {
1040 semsg(_(e_white_space_required_after_name_str), arg);
1041 goto early_ret;
1042 }
1043 extends = vim_strnsave(arg, end - arg);
1044 if (extends == NULL)
1045 goto early_ret;
1046
1047 arg = skipwhite(end + 1);
1048 }
1049 else if (STRNCMP(arg, "implements", 10) == 0
1050 && IS_WHITE_OR_NUL(arg[10]))
Bram Moolenaar94674f22023-01-06 18:42:20 +00001051 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001052 if (ga_impl.ga_len > 0)
1053 {
1054 emsg(_(e_duplicate_implements));
1055 goto early_ret;
1056 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001057 arg = skipwhite(arg + 10);
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001058
1059 for (;;)
Bram Moolenaar94674f22023-01-06 18:42:20 +00001060 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001061 char_u *impl_end = find_name_end(arg, NULL, NULL,
1062 FNE_CHECK_START);
1063 if (!IS_WHITE_OR_NUL(*impl_end) && *impl_end != ',')
1064 {
1065 semsg(_(e_white_space_required_after_name_str), arg);
1066 goto early_ret;
1067 }
1068 char_u *iname = vim_strnsave(arg, impl_end - arg);
1069 if (iname == NULL)
1070 goto early_ret;
1071 for (int i = 0; i < ga_impl.ga_len; ++i)
1072 if (STRCMP(((char_u **)ga_impl.ga_data)[i], iname) == 0)
1073 {
1074 semsg(_(e_duplicate_interface_after_implements_str),
1075 iname);
1076 vim_free(iname);
1077 goto early_ret;
1078 }
1079 if (ga_add_string(&ga_impl, iname) == FAIL)
1080 {
1081 vim_free(iname);
1082 goto early_ret;
1083 }
1084 if (*impl_end != ',')
1085 {
1086 arg = skipwhite(impl_end);
1087 break;
1088 }
1089 arg = skipwhite(impl_end + 1);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001090 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001091 }
1092 else
1093 {
1094 semsg(_(e_trailing_characters_str), arg);
1095early_ret:
Bram Moolenaar83677162023-01-08 19:54:10 +00001096 vim_free(extends);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001097 ga_clear_strings(&ga_impl);
1098 return;
1099 }
1100 }
1101
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001102 garray_T type_list; // list of pointers to allocated types
1103 ga_init2(&type_list, sizeof(type_T *), 10);
1104
Bram Moolenaard505d172022-12-18 21:42:55 +00001105 // Growarray with class members declared in the class.
1106 garray_T classmembers;
1107 ga_init2(&classmembers, sizeof(ocmember_T), 10);
1108
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001109 // Growarray with functions declared in the class.
1110 garray_T classfunctions;
1111 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +00001112
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001113 // Growarray with object members declared in the class.
1114 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +00001115 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001116
1117 // Growarray with object methods declared in the class.
1118 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001119 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001120
1121 /*
Bram Moolenaar554d0312023-01-05 19:59:18 +00001122 * Go over the body of the class/interface until "endclass" or
1123 * "endinterface" is found.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001124 */
1125 char_u *theline = NULL;
1126 int success = FALSE;
1127 for (;;)
1128 {
1129 vim_free(theline);
1130 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
1131 if (theline == NULL)
1132 break;
1133 char_u *line = skipwhite(theline);
1134
Bram Moolenaar418b5472022-12-20 13:38:22 +00001135 // Skip empty and comment lines.
1136 if (*line == NUL)
1137 continue;
1138 if (*line == '#')
1139 {
1140 if (vim9_bad_comment(line))
1141 break;
1142 continue;
1143 }
1144
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001145 char_u *p = line;
Bram Moolenaar554d0312023-01-05 19:59:18 +00001146 char *end_name = is_class ? "endclass" : "endinterface";
1147 if (checkforcmd(&p, end_name, is_class ? 4 : 5))
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001148 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001149 if (STRNCMP(line, end_name, is_class ? 8 : 12) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001150 semsg(_(e_command_cannot_be_shortened_str), line);
1151 else if (*p == '|' || !ends_excmd2(line, p))
1152 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +00001153 else
1154 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001155 break;
1156 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00001157 char *wrong_name = is_class ? "endinterface" : "endclass";
1158 if (checkforcmd(&p, wrong_name, is_class ? 5 : 4))
1159 {
Bram Moolenaar657aea72023-01-27 13:16:19 +00001160 semsg(_(e_invalid_command_str_expected_str), line, end_name);
Bram Moolenaar554d0312023-01-05 19:59:18 +00001161 break;
1162 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001163
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001164 int has_public = FALSE;
1165 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001166 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001167 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001168 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001169 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001170 break;
1171 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001172 has_public = TRUE;
1173 p = skipwhite(line + 6);
1174
Bram Moolenaard505d172022-12-18 21:42:55 +00001175 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001176 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001177 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001178 break;
1179 }
1180 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001181
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001182 int has_static = FALSE;
1183 char_u *ps = p;
1184 if (checkforcmd(&p, "static", 4))
1185 {
1186 if (STRNCMP(ps, "static", 6) != 0)
1187 {
1188 semsg(_(e_command_cannot_be_shortened_str), ps);
1189 break;
1190 }
1191 has_static = TRUE;
1192 p = skipwhite(ps + 6);
1193 }
1194
Bram Moolenaard505d172022-12-18 21:42:55 +00001195 // object members (public, read access, private):
1196 // "this._varname"
1197 // "this.varname"
1198 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001199 if (STRNCMP(p, "this", 4) == 0)
1200 {
1201 if (p[4] != '.' || !eval_isnamec1(p[5]))
1202 {
1203 semsg(_(e_invalid_object_member_declaration_str), p);
1204 break;
1205 }
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001206 if (has_static)
1207 {
1208 emsg(_(e_static_cannot_be_followed_by_this));
1209 break;
1210 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001211 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +00001212 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +00001213 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +00001214 char_u *init_expr = NULL;
1215 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +00001216 &varname_end, &type_list, &type,
1217 is_class ? &init_expr: NULL) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +00001218 break;
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001219 if (is_duplicate_member(&objmembers, varname, varname_end))
1220 {
1221 vim_free(init_expr);
1222 break;
1223 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001224 if (add_member(&objmembers, varname, varname_end,
1225 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +00001226 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001227 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001228 break;
1229 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001230 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001231
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001232 // constructors:
1233 // def new()
1234 // enddef
1235 // def newOther()
1236 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001237 // object methods and class functions:
1238 // def SomeMethod()
1239 // enddef
1240 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001241 // enddef
1242 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001243 // def <Tval> someMethod()
1244 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001245 else if (checkforcmd(&p, "def", 3))
1246 {
1247 exarg_T ea;
1248 garray_T lines_to_free;
1249
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001250 // TODO: error for "public static def Func()"?
1251
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001252 CLEAR_FIELD(ea);
1253 ea.cmd = line;
1254 ea.arg = p;
1255 ea.cmdidx = CMD_def;
1256 ea.getline = eap->getline;
1257 ea.cookie = eap->cookie;
1258
1259 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +00001260 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free,
1261 is_class ? CF_CLASS : CF_INTERFACE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001262 ga_clear_strings(&lines_to_free);
1263
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001264 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001265 {
Bram Moolenaar58b40092023-01-11 15:59:05 +00001266 char_u *name = uf->uf_name;
1267 int is_new = STRNCMP(name, "new", 3) == 0;
Gianmaria Bajo4b9777a2023-08-29 22:26:30 +02001268
1269 if (is_new && !is_valid_constructor(uf, is_abstract, has_static))
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001270 {
Yegappan Lakshmananb1027282023-08-19 11:26:42 +02001271 func_clear_free(uf, FALSE);
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001272 break;
1273 }
Gianmaria Bajo4b9777a2023-08-29 22:26:30 +02001274
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001275 garray_T *fgap = has_static || is_new
1276 ? &classfunctions : &objmethods;
Bram Moolenaar58b40092023-01-11 15:59:05 +00001277 // Check the name isn't used already.
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001278 if (is_duplicate_method(fgap, name))
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +02001279 {
1280 success = FALSE;
1281 func_clear_free(uf, FALSE);
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001282 break;
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +02001283 }
Bram Moolenaar58b40092023-01-11 15:59:05 +00001284
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001285 if (ga_grow(fgap, 1) == OK)
1286 {
1287 if (is_new)
1288 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001289
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001290 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
1291 ++fgap->ga_len;
1292 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001293 }
1294 }
1295
1296 // class members
1297 else if (has_static)
1298 {
1299 // class members (public, read access, private):
1300 // "static _varname"
1301 // "static varname"
1302 // "public static varname"
1303 char_u *varname = p;
1304 char_u *varname_end = NULL;
1305 type_T *type = NULL;
1306 char_u *init_expr = NULL;
1307 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +00001308 &varname_end, &type_list, &type,
1309 is_class ? &init_expr : NULL) == FAIL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001310 break;
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001311 if (is_duplicate_member(&classmembers, varname, varname_end))
1312 {
1313 vim_free(init_expr);
1314 break;
1315 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001316 if (add_member(&classmembers, varname, varname_end,
1317 has_public, type, init_expr) == FAIL)
1318 {
1319 vim_free(init_expr);
1320 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001321 }
1322 }
1323
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001324 else
1325 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001326 if (is_class)
1327 semsg(_(e_not_valid_command_in_class_str), line);
1328 else
1329 semsg(_(e_not_valid_command_in_interface_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001330 break;
1331 }
1332 }
1333 vim_free(theline);
1334
Bram Moolenaar83677162023-01-08 19:54:10 +00001335 class_T *extends_cl = NULL; // class from "extends" argument
1336
1337 /*
1338 * Check a few things before defining the class.
1339 */
1340
1341 // Check the "extends" class is valid.
1342 if (success && extends != NULL)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001343 success = validate_extends_class(extends, &extends_cl);
Bram Moolenaar83677162023-01-08 19:54:10 +00001344 VIM_CLEAR(extends);
1345
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +02001346 // Check the new class members and object members doesn't duplicate the
1347 // members in the extended class lineage.
1348 if (success && extends_cl != NULL)
1349 success = validate_extends_members(&classmembers, &objmembers,
1350 extends_cl);
1351
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001352 class_T **intf_classes = NULL;
1353
Bram Moolenaar83677162023-01-08 19:54:10 +00001354 // Check all "implements" entries are valid.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001355 if (success && ga_impl.ga_len > 0)
1356 {
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001357 intf_classes = ALLOC_CLEAR_MULT(class_T *, ga_impl.ga_len);
1358
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001359 success = validate_implements_classes(&ga_impl, intf_classes,
1360 &classfunctions, &classmembers,
1361 &objmethods, &objmembers);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001362 }
1363
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001364 // Check no function argument name is used as a class member.
Bram Moolenaard40f00c2023-01-13 17:36:49 +00001365 if (success)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001366 success = check_func_arg_names(&classfunctions, &objmethods,
1367 &classmembers);
Bram Moolenaard40f00c2023-01-13 17:36:49 +00001368
Bram Moolenaareb533502022-12-14 15:06:11 +00001369 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001370 if (success)
1371 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001372 // "endclass" encountered without failures: Create the class.
1373
Bram Moolenaareb533502022-12-14 15:06:11 +00001374 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001375 if (cl == NULL)
1376 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +00001377 if (!is_class)
1378 cl->class_flags = CLASS_INTERFACE;
1379
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001380 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +00001381 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +00001382 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001383 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +00001384
Bram Moolenaard0200c82023-01-28 15:19:40 +00001385 if (extends_cl != NULL)
1386 {
1387 cl->class_extends = extends_cl;
1388 extends_cl->class_flags |= CLASS_EXTENDED;
1389 }
Bram Moolenaar83677162023-01-08 19:54:10 +00001390
Bram Moolenaard505d172022-12-18 21:42:55 +00001391 // Add class and object members to "cl".
1392 if (add_members_to_class(&classmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +00001393 extends_cl == NULL ? NULL
1394 : extends_cl->class_class_members,
1395 extends_cl == NULL ? 0
1396 : extends_cl->class_class_member_count,
1397 &cl->class_class_members,
1398 &cl->class_class_member_count) == FAIL
Bram Moolenaard505d172022-12-18 21:42:55 +00001399 || add_members_to_class(&objmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +00001400 extends_cl == NULL ? NULL
1401 : extends_cl->class_obj_members,
1402 extends_cl == NULL ? 0
1403 : extends_cl->class_obj_member_count,
1404 &cl->class_obj_members,
1405 &cl->class_obj_member_count) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +00001406 goto cleanup;
1407
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001408 if (ga_impl.ga_len > 0)
1409 {
1410 // Move the "implements" names into the class.
1411 cl->class_interface_count = ga_impl.ga_len;
1412 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
1413 if (cl->class_interfaces == NULL)
1414 goto cleanup;
1415 for (int i = 0; i < ga_impl.ga_len; ++i)
1416 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
1417 VIM_CLEAR(ga_impl.ga_data);
1418 ga_impl.ga_len = 0;
1419
Bram Moolenaard0200c82023-01-28 15:19:40 +00001420 cl->class_interfaces_cl = intf_classes;
1421 intf_classes = NULL;
1422 }
1423
1424 if (cl->class_interface_count > 0 || extends_cl != NULL)
1425 {
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001426 // Add a method and member lookup table to each of the interface
1427 // classes.
1428 if (add_lookup_tables(cl, extends_cl, &objmethods) == FAIL)
1429 goto cleanup;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001430 }
1431
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001432 // Allocate a typval for each class member and initialize it.
Bram Moolenaar554d0312023-01-05 19:59:18 +00001433 if (is_class && cl->class_class_member_count > 0)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001434 add_class_members(cl, eap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001435
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001436 int have_new = FALSE;
1437 ufunc_T *class_func = NULL;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001438 for (int i = 0; i < classfunctions.ga_len; ++i)
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001439 {
1440 class_func = ((ufunc_T **)classfunctions.ga_data)[i];
1441 if (STRCMP(class_func->uf_name, "new") == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001442 {
1443 have_new = TRUE;
1444 break;
1445 }
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001446 }
1447
1448 if (have_new)
1449 // The return type of new() is an object of class "cl"
1450 class_func->uf_ret_type->tt_class = cl;
1451 else if (is_class && !is_abstract && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001452 // No new() method was defined, add the default constructor.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001453 add_default_constructor(cl, &classfunctions, &type_list);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001454
Bram Moolenaar58b40092023-01-11 15:59:05 +00001455 // Move all the functions into the created class.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001456 if (add_classfuncs_objmethods(cl, extends_cl, &classfunctions,
1457 &objmethods) == FAIL)
1458 goto cleanup;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001459
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001460 cl->class_type.tt_type = VAR_CLASS;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001461 cl->class_type.tt_class = cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001462 cl->class_object_type.tt_type = VAR_OBJECT;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001463 cl->class_object_type.tt_class = cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001464 cl->class_type_list = type_list;
1465
1466 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +00001467 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001468
1469 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001470 // TODO: handle other context, e.g. in a function
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001471 typval_T tv;
1472 tv.v_type = VAR_CLASS;
1473 tv.vval.v_class = cl;
Bram Moolenaara86655a2023-01-12 17:06:27 +00001474 is_export = class_export;
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001475 SOURCING_LNUM = start_lnum;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001476 set_var_const(cl->class_name, current_sctx.sc_sid,
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001477 NULL, &tv, FALSE, 0, 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001478 return;
1479 }
1480
1481cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +00001482 if (cl != NULL)
1483 {
1484 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001485 vim_free(cl->class_class_functions);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001486 if (cl->class_interfaces != NULL)
1487 {
1488 for (int i = 0; i < cl->class_interface_count; ++i)
1489 vim_free(cl->class_interfaces[i]);
1490 vim_free(cl->class_interfaces);
1491 }
1492 if (cl->class_interfaces_cl != NULL)
1493 {
1494 for (int i = 0; i < cl->class_interface_count; ++i)
1495 class_unref(cl->class_interfaces_cl[i]);
1496 vim_free(cl->class_interfaces_cl);
1497 }
Bram Moolenaareb533502022-12-14 15:06:11 +00001498 vim_free(cl->class_obj_members);
1499 vim_free(cl->class_obj_methods);
1500 vim_free(cl);
1501 }
1502
Bram Moolenaar83677162023-01-08 19:54:10 +00001503 vim_free(extends);
1504 class_unref(extends_cl);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001505
1506 if (intf_classes != NULL)
1507 {
1508 for (int i = 0; i < ga_impl.ga_len; ++i)
1509 class_unref(intf_classes[i]);
1510 vim_free(intf_classes);
1511 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001512 ga_clear_strings(&ga_impl);
1513
Bram Moolenaard505d172022-12-18 21:42:55 +00001514 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001515 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001516 garray_T *gap = round == 1 ? &classmembers : &objmembers;
1517 if (gap->ga_len == 0 || gap->ga_data == NULL)
1518 continue;
1519
1520 for (int i = 0; i < gap->ga_len; ++i)
1521 {
1522 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
1523 vim_free(m->ocm_name);
1524 vim_free(m->ocm_init);
1525 }
1526 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001527 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001528
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001529 for (int i = 0; i < objmethods.ga_len; ++i)
1530 {
1531 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
1532 func_clear_free(uf, FALSE);
1533 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001534 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001535
1536 for (int i = 0; i < classfunctions.ga_len; ++i)
1537 {
1538 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
1539 func_clear_free(uf, FALSE);
1540 }
1541 ga_clear(&classfunctions);
1542
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001543 clear_type_list(&type_list);
1544}
1545
1546/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001547 * Find member "name" in class "cl", set "member_idx" to the member index and
1548 * return its type.
1549 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001550 */
1551 type_T *
1552class_member_type(
1553 class_T *cl,
1554 char_u *name,
1555 char_u *name_end,
1556 int *member_idx)
1557{
1558 *member_idx = -1; // not found (yet)
1559 size_t len = name_end - name;
1560
1561 for (int i = 0; i < cl->class_obj_member_count; ++i)
1562 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001563 ocmember_T *m = cl->class_obj_members + i;
1564 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001565 {
1566 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001567 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001568 }
1569 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001570
1571 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001572 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001573}
1574
1575/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001576 * Handle ":enum" up to ":endenum".
1577 */
1578 void
1579ex_enum(exarg_T *eap UNUSED)
1580{
1581 // TODO
1582}
1583
1584/*
1585 * Handle ":type".
1586 */
1587 void
1588ex_type(exarg_T *eap UNUSED)
1589{
1590 // TODO
1591}
1592
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001593/*
1594 * Evaluate what comes after a class:
1595 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001596 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001597 * - class constructor: SomeClass.new()
1598 * - object member: someObject.varname
1599 * - object method: someObject.SomeMethod()
1600 *
1601 * "*arg" points to the '.'.
1602 * "*arg" is advanced to after the member name or method call.
1603 *
1604 * Returns FAIL or OK.
1605 */
1606 int
1607class_object_index(
1608 char_u **arg,
1609 typval_T *rettv,
1610 evalarg_T *evalarg,
1611 int verbose UNUSED) // give error messages
1612{
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001613 if (VIM_ISWHITE((*arg)[1]))
1614 {
1615 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
1616 return FAIL;
1617 }
1618
1619 ++*arg;
1620 char_u *name = *arg;
1621 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1622 if (name_end == name)
1623 return FAIL;
1624 size_t len = name_end - name;
1625
Bram Moolenaar552bdca2023-02-17 21:08:50 +00001626 class_T *cl;
1627 if (rettv->v_type == VAR_CLASS)
1628 cl = rettv->vval.v_class;
1629 else // VAR_OBJECT
1630 {
1631 if (rettv->vval.v_object == NULL)
1632 {
1633 emsg(_(e_using_null_object));
1634 return FAIL;
1635 }
1636 cl = rettv->vval.v_object->obj_class;
1637 }
1638
Bram Moolenaard13dd302023-03-11 20:56:35 +00001639 if (cl == NULL)
1640 {
1641 emsg(_(e_incomplete_type));
1642 return FAIL;
1643 }
1644
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001645 if (*name_end == '(')
1646 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001647 int on_class = rettv->v_type == VAR_CLASS;
1648 int count = on_class ? cl->class_class_function_count
1649 : cl->class_obj_method_count;
1650 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001651 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001652 ufunc_T *fp = on_class ? cl->class_class_functions[i]
1653 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +00001654 // Use a separate pointer to avoid that ASAN complains about
1655 // uf_name[] only being 4 characters.
1656 char_u *ufname = (char_u *)fp->uf_name;
1657 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001658 {
1659 typval_T argvars[MAX_FUNC_ARGS + 1];
1660 int argcount = 0;
1661
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +02001662 if (*ufname == '_')
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02001663 {
1664 // Cannot access a private method outside of a class
1665 semsg(_(e_cannot_access_private_method_str), name);
1666 return FAIL;
1667 }
1668
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001669 char_u *argp = name_end;
1670 int ret = get_func_arguments(&argp, evalarg, 0,
1671 argvars, &argcount);
1672 if (ret == FAIL)
1673 return FAIL;
1674
1675 funcexe_T funcexe;
1676 CLEAR_FIELD(funcexe);
1677 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001678 if (rettv->v_type == VAR_OBJECT)
1679 {
1680 funcexe.fe_object = rettv->vval.v_object;
1681 ++funcexe.fe_object->obj_refcount;
1682 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001683
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001684 // Clear the class or object after calling the function, in
1685 // case the refcount is one.
1686 typval_T tv_tofree = *rettv;
1687 rettv->v_type = VAR_UNKNOWN;
1688
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001689 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001690 int error = call_user_func_check(fp, argcount, argvars,
1691 rettv, &funcexe, NULL);
1692
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001693 // Clear the previous rettv and the arguments.
1694 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001695 for (int idx = 0; idx < argcount; ++idx)
1696 clear_tv(&argvars[idx]);
1697
1698 if (error != FCERR_NONE)
1699 {
1700 user_func_error(error, printable_func_name(fp),
1701 funcexe.fe_found_var);
1702 return FAIL;
1703 }
1704 *arg = argp;
1705 return OK;
1706 }
1707 }
1708
1709 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
1710 }
1711
1712 else if (rettv->v_type == VAR_OBJECT)
1713 {
1714 for (int i = 0; i < cl->class_obj_member_count; ++i)
1715 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001716 ocmember_T *m = &cl->class_obj_members[i];
1717 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001718 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001719 if (*name == '_')
1720 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001721 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001722 return FAIL;
1723 }
1724
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001725 // The object only contains a pointer to the class, the member
1726 // values array follows right after that.
1727 object_T *obj = rettv->vval.v_object;
1728 typval_T *tv = (typval_T *)(obj + 1) + i;
1729 copy_tv(tv, rettv);
1730 object_unref(obj);
1731
1732 *arg = name_end;
1733 return OK;
1734 }
1735 }
1736
1737 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
1738 }
1739
Bram Moolenaard505d172022-12-18 21:42:55 +00001740 else if (rettv->v_type == VAR_CLASS)
1741 {
1742 // class member
1743 for (int i = 0; i < cl->class_class_member_count; ++i)
1744 {
1745 ocmember_T *m = &cl->class_class_members[i];
1746 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
1747 {
1748 if (*name == '_')
1749 {
1750 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
1751 return FAIL;
1752 }
1753
1754 typval_T *tv = &cl->class_members_tv[i];
1755 copy_tv(tv, rettv);
1756 class_unref(cl);
1757
1758 *arg = name_end;
1759 return OK;
1760 }
1761 }
1762
1763 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
1764 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001765
1766 return FAIL;
1767}
1768
1769/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001770 * If "arg" points to a class or object method, return it.
1771 * Otherwise return NULL.
1772 */
1773 ufunc_T *
1774find_class_func(char_u **arg)
1775{
1776 char_u *name = *arg;
1777 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1778 if (name_end == name || *name_end != '.')
1779 return NULL;
1780
1781 size_t len = name_end - name;
1782 typval_T tv;
1783 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001784 if (eval_variable(name, (int)len,
1785 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001786 return NULL;
1787 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001788 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001789
1790 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1791 : tv.vval.v_object->obj_class;
1792 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001793 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001794 char_u *fname = name_end + 1;
1795 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1796 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001797 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001798 len = fname_end - fname;
1799
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001800 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1801 : cl->class_obj_method_count;
1802 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1803 : cl->class_obj_methods;
1804 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001805 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001806 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001807 // Use a separate pointer to avoid that ASAN complains about
1808 // uf_name[] only being 4 characters.
1809 char_u *ufname = (char_u *)fp->uf_name;
1810 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001811 {
1812 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001813 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001814 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001815 }
1816
Bram Moolenaareb533502022-12-14 15:06:11 +00001817fail_after_eval:
1818 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001819 return NULL;
1820}
1821
1822/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001823 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1824 * index in class.class_class_members[].
1825 * If "cl_ret" is not NULL set it to the class.
1826 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001827 */
1828 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001829class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001830{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001831 if (cctx == NULL || cctx->ctx_ufunc == NULL
1832 || cctx->ctx_ufunc->uf_class == NULL)
1833 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001834 class_T *cl = cctx->ctx_ufunc->uf_class;
1835
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001836 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001837 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001838 ocmember_T *m = &cl->class_class_members[i];
1839 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001840 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001841 if (cl_ret != NULL)
1842 *cl_ret = cl;
1843 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001844 }
1845 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001846 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001847}
1848
1849/*
Bram Moolenaar62a69232023-01-24 15:07:04 +00001850 * Return TRUE if current context "cctx_arg" is inside class "cl".
1851 * Return FALSE if not.
1852 */
1853 int
1854inside_class(cctx_T *cctx_arg, class_T *cl)
1855{
1856 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
1857 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class == cl)
1858 return TRUE;
1859 return FALSE;
1860}
1861
1862/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001863 * Make a copy of an object.
1864 */
1865 void
1866copy_object(typval_T *from, typval_T *to)
1867{
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02001868 if (from->vval.v_object == NULL)
1869 to->vval.v_object = NULL;
1870 else
1871 {
1872 to->vval.v_object = from->vval.v_object;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001873 ++to->vval.v_object->obj_refcount;
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02001874 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001875}
1876
1877/*
1878 * Free an object.
1879 */
1880 static void
1881object_clear(object_T *obj)
1882{
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001883 // Avoid a recursive call, it can happen if "obj" has a circular reference.
1884 obj->obj_refcount = INT_MAX;
1885
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001886 class_T *cl = obj->obj_class;
1887
Jia-Ju Bai5b0889b2023-08-13 20:04:04 +02001888 if (!cl)
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02001889 return;
Jia-Ju Bai5b0889b2023-08-13 20:04:04 +02001890
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001891 // the member values are just after the object structure
1892 typval_T *tv = (typval_T *)(obj + 1);
1893 for (int i = 0; i < cl->class_obj_member_count; ++i)
1894 clear_tv(tv + i);
1895
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001896 // Remove from the list headed by "first_object".
1897 object_cleared(obj);
1898
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001899 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001900 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001901}
1902
1903/*
1904 * Unreference an object.
1905 */
1906 void
1907object_unref(object_T *obj)
1908{
1909 if (obj != NULL && --obj->obj_refcount <= 0)
1910 object_clear(obj);
1911}
1912
1913/*
1914 * Make a copy of a class.
1915 */
1916 void
1917copy_class(typval_T *from, typval_T *to)
1918{
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02001919 if (from->vval.v_class == NULL)
1920 to->vval.v_class = NULL;
1921 else
1922 {
1923 to->vval.v_class = from->vval.v_class;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001924 ++to->vval.v_class->class_refcount;
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02001925 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001926}
1927
1928/*
1929 * Unreference a class. Free it when the reference count goes down to zero.
1930 */
1931 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001932class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001933{
Bram Moolenaard505d172022-12-18 21:42:55 +00001934 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001935 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001936 // Freeing what the class contains may recursively come back here.
1937 // Clear "class_name" first, if it is NULL the class does not need to
1938 // be freed.
1939 VIM_CLEAR(cl->class_name);
1940
Bram Moolenaar83677162023-01-08 19:54:10 +00001941 class_unref(cl->class_extends);
1942
Bram Moolenaar94674f22023-01-06 18:42:20 +00001943 for (int i = 0; i < cl->class_interface_count; ++i)
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001944 {
Bram Moolenaar94674f22023-01-06 18:42:20 +00001945 vim_free(((char_u **)cl->class_interfaces)[i]);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001946 if (cl->class_interfaces_cl[i] != NULL)
1947 class_unref(cl->class_interfaces_cl[i]);
1948 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001949 vim_free(cl->class_interfaces);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001950 vim_free(cl->class_interfaces_cl);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001951
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001952 itf2class_T *next;
1953 for (itf2class_T *i2c = cl->class_itf2class; i2c != NULL; i2c = next)
1954 {
1955 next = i2c->i2c_next;
1956 vim_free(i2c);
1957 }
1958
Bram Moolenaard505d172022-12-18 21:42:55 +00001959 for (int i = 0; i < cl->class_class_member_count; ++i)
1960 {
1961 ocmember_T *m = &cl->class_class_members[i];
1962 vim_free(m->ocm_name);
1963 vim_free(m->ocm_init);
1964 if (cl->class_members_tv != NULL)
1965 clear_tv(&cl->class_members_tv[i]);
1966 }
1967 vim_free(cl->class_class_members);
1968 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001969
1970 for (int i = 0; i < cl->class_obj_member_count; ++i)
1971 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001972 ocmember_T *m = &cl->class_obj_members[i];
1973 vim_free(m->ocm_name);
1974 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001975 }
1976 vim_free(cl->class_obj_members);
1977
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001978 for (int i = 0; i < cl->class_class_function_count; ++i)
1979 {
1980 ufunc_T *uf = cl->class_class_functions[i];
1981 func_clear_free(uf, FALSE);
1982 }
1983 vim_free(cl->class_class_functions);
1984
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001985 for (int i = 0; i < cl->class_obj_method_count; ++i)
1986 {
1987 ufunc_T *uf = cl->class_obj_methods[i];
1988 func_clear_free(uf, FALSE);
1989 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001990 vim_free(cl->class_obj_methods);
1991
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001992 clear_type_list(&cl->class_type_list);
1993
1994 vim_free(cl);
1995 }
1996}
1997
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001998static object_T *first_object = NULL;
1999
2000/*
2001 * Call this function when an object has been created. It will be added to the
2002 * list headed by "first_object".
2003 */
2004 void
2005object_created(object_T *obj)
2006{
2007 if (first_object != NULL)
2008 {
2009 obj->obj_next_used = first_object;
2010 first_object->obj_prev_used = obj;
2011 }
2012 first_object = obj;
2013}
2014
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01002015static object_T *next_nonref_obj = NULL;
2016
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002017/*
2018 * Call this function when an object has been cleared and is about to be freed.
2019 * It is removed from the list headed by "first_object".
2020 */
2021 void
2022object_cleared(object_T *obj)
2023{
2024 if (obj->obj_next_used != NULL)
2025 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
2026 if (obj->obj_prev_used != NULL)
2027 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
2028 else if (first_object == obj)
2029 first_object = obj->obj_next_used;
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01002030
2031 // update the next object to check if needed
2032 if (obj == next_nonref_obj)
2033 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002034}
2035
2036/*
2037 * Go through the list of all objects and free items without "copyID".
2038 */
2039 int
2040object_free_nonref(int copyID)
2041{
2042 int did_free = FALSE;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002043
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01002044 for (object_T *obj = first_object; obj != NULL; obj = next_nonref_obj)
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002045 {
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01002046 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002047 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
2048 {
2049 // Free the object and items it contains.
2050 object_clear(obj);
2051 did_free = TRUE;
2052 }
2053 }
2054
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01002055 next_nonref_obj = NULL;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002056 return did_free;
2057}
2058
LemonBoyafe04662023-08-23 21:08:11 +02002059/*
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02002060 * Return TRUE when the class "cl", its base class or one of the implemented
2061 * interfaces matches the class "other_cl".
LemonBoyafe04662023-08-23 21:08:11 +02002062 */
2063 int
2064class_instance_of(class_T *cl, class_T *other_cl)
2065{
2066 if (cl == other_cl)
2067 return TRUE;
2068
2069 // Recursively check the base classes.
2070 for (; cl != NULL; cl = cl->class_extends)
2071 {
2072 if (cl == other_cl)
2073 return TRUE;
2074 // Check the implemented interfaces.
2075 for (int i = cl->class_interface_count - 1; i >= 0; --i)
2076 if (cl->class_interfaces_cl[i] == other_cl)
2077 return TRUE;
2078 }
2079
2080 return FALSE;
2081}
2082
2083/*
2084 * "instanceof(object, classinfo)" function
2085 */
2086 void
2087f_instanceof(typval_T *argvars, typval_T *rettv)
2088{
2089 typval_T *object_tv = &argvars[0];
2090 typval_T *classinfo_tv = &argvars[1];
2091 listitem_T *li;
2092
2093 rettv->vval.v_number = VVAL_FALSE;
2094
2095 if (check_for_object_arg(argvars, 0) == FAIL
2096 || check_for_class_or_list_arg(argvars, 1) == FAIL)
2097 return;
2098
2099 if (classinfo_tv->v_type == VAR_LIST)
2100 {
2101 FOR_ALL_LIST_ITEMS(classinfo_tv->vval.v_list, li)
2102 {
2103 if (li->li_tv.v_type != VAR_CLASS)
2104 {
2105 emsg(_(e_class_required));
2106 return;
2107 }
2108
2109 if (class_instance_of(object_tv->vval.v_object->obj_class,
2110 li->li_tv.vval.v_class) == TRUE)
2111 {
2112 rettv->vval.v_number = VVAL_TRUE;
2113 return;
2114 }
2115 }
2116 }
2117 else if (classinfo_tv->v_type == VAR_CLASS)
2118 {
2119 rettv->vval.v_number = class_instance_of(object_tv->vval.v_object->obj_class,
2120 classinfo_tv->vval.v_class);
2121 }
2122}
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00002123
2124#endif // FEAT_EVAL