blob: 955bd0cbcd6ddb3186616ccfc99e2ac30e453709 [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/*
252 * Check the members of the interface class "ifcl" match the class members
253 * ("classmembers_gap") and object members ("objmembers_gap") of a class.
254 * Returns TRUE if the class and object member names are valid.
255 */
256 static int
257validate_interface_members(
258 char_u *intf_class_name,
259 class_T *ifcl,
260 garray_T *classmembers_gap,
261 garray_T *objmembers_gap)
262{
263 int success = TRUE;
264
265 for (int loop = 1; loop <= 2 && success; ++loop)
266 {
267 // loop == 1: check class members
268 // loop == 2: check object members
269 int if_count = loop == 1 ? ifcl->class_class_member_count
270 : ifcl->class_obj_member_count;
271 if (if_count == 0)
272 continue;
273 ocmember_T *if_ms = loop == 1 ? ifcl->class_class_members
274 : ifcl->class_obj_members;
275 ocmember_T *cl_ms = (ocmember_T *)(loop == 1
276 ? classmembers_gap->ga_data
277 : objmembers_gap->ga_data);
278 int cl_count = loop == 1 ? classmembers_gap->ga_len
279 : objmembers_gap->ga_len;
280 for (int if_i = 0; if_i < if_count; ++if_i)
281 {
282 int cl_i;
283 for (cl_i = 0; cl_i < cl_count; ++cl_i)
284 {
285 ocmember_T *m = &cl_ms[cl_i];
286 where_T where = WHERE_INIT;
287
288 if (STRCMP(if_ms[if_i].ocm_name, m->ocm_name) != 0)
289 continue;
290
291 // Ensure the type is matching.
292 where.wt_func_name = (char *)m->ocm_name;
293 where.wt_kind = WT_MEMBER;
294 if (check_type_maybe(if_ms[if_i].ocm_type, m->ocm_type, TRUE,
295 where) != OK)
296 success = FALSE;
297
298 break;
299 }
300 if (cl_i == cl_count)
301 {
302 semsg(_(e_member_str_of_interface_str_not_implemented),
303 if_ms[if_i].ocm_name, intf_class_name);
304 success = FALSE;
305 break;
306 }
307 }
308 }
309
310 return success;
311}
312
313/*
314 * Check the functions/methods of the interface class "ifcl" match the class
315 * methods ("classfunctions_gap") and object functions ("objmemthods_gap") of a
316 * class.
317 * Returns TRUE if the class and object member names are valid.
318 */
319 static int
320validate_interface_methods(
321 char_u *intf_class_name,
322 class_T *ifcl,
323 garray_T *classfunctions_gap,
324 garray_T *objmethods_gap)
325{
326 int success = TRUE;
327
328 for (int loop = 1; loop <= 2 && success; ++loop)
329 {
330 // loop == 1: check class functions
331 // loop == 2: check object methods
332 int if_count = loop == 1 ? ifcl->class_class_function_count
333 : ifcl->class_obj_method_count;
334 if (if_count == 0)
335 continue;
336 ufunc_T **if_fp = loop == 1 ? ifcl->class_class_functions
337 : ifcl->class_obj_methods;
338 ufunc_T **cl_fp = (ufunc_T **)(loop == 1
339 ? classfunctions_gap->ga_data
340 : objmethods_gap->ga_data);
341 int cl_count = loop == 1 ? classfunctions_gap->ga_len
342 : objmethods_gap->ga_len;
343 for (int if_i = 0; if_i < if_count; ++if_i)
344 {
345 char_u *if_name = if_fp[if_i]->uf_name;
346 int cl_i;
347 for (cl_i = 0; cl_i < cl_count; ++cl_i)
348 {
349 char_u *cl_name = cl_fp[cl_i]->uf_name;
350 if (STRCMP(if_name, cl_name) == 0)
351 {
352 where_T where = WHERE_INIT;
353
354 // Ensure the type is matching.
355 where.wt_func_name = (char *)if_name;
356 where.wt_kind = WT_METHOD;
357 if (check_type_maybe(if_fp[if_i]->uf_func_type,
358 cl_fp[cl_i]->uf_func_type, TRUE, where) != OK)
359 success = FALSE;
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200360 // Ensure the public/private access level is matching.
361 if (if_fp[if_i]->uf_private != cl_fp[cl_i]->uf_private)
362 {
363 semsg(_(e_interface_str_and_class_str_function_access_not_same),
364 cl_name, if_name);
365 success = FALSE;
366 }
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200367 break;
368 }
369 }
370 if (cl_i == cl_count)
371 {
372 semsg(_(e_function_str_of_interface_str_not_implemented),
373 if_name, intf_class_name);
374 success = FALSE;
375 break;
376 }
377 }
378 }
379
380 return success;
381}
382
383/*
384 * Validate all the "implements" classes when creating a new class. The
385 * classes are returned in "intf_classes". The class functions, class methods,
386 * object methods and object members in the new class are in
387 * "classfunctions_gap", "classmembers_gap", "objmethods_gap", and
388 * "objmembers_gap" respectively.
389 */
390 static int
391validate_implements_classes(
392 garray_T *impl_gap,
393 class_T **intf_classes,
394 garray_T *classfunctions_gap,
395 garray_T *classmembers_gap,
396 garray_T *objmethods_gap,
397 garray_T *objmembers_gap)
398{
399 int success = TRUE;
400
401 for (int i = 0; i < impl_gap->ga_len && success; ++i)
402 {
403 char_u *impl = ((char_u **)impl_gap->ga_data)[i];
404 typval_T tv;
405 tv.v_type = VAR_UNKNOWN;
406 if (eval_variable_import(impl, &tv) == FAIL)
407 {
408 semsg(_(e_interface_name_not_found_str), impl);
409 success = FALSE;
410 break;
411 }
412
413 if (tv.v_type != VAR_CLASS
414 || tv.vval.v_class == NULL
415 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0)
416 {
417 semsg(_(e_not_valid_interface_str), impl);
418 success = FALSE;
419 clear_tv(&tv);
420 break;
421 }
422
423 class_T *ifcl = tv.vval.v_class;
424 intf_classes[i] = ifcl;
425 ++ifcl->class_refcount;
426
427 // check the members of the interface match the members of the class
428 success = validate_interface_members(impl, ifcl, classmembers_gap,
429 objmembers_gap);
430
431 // check the functions/methods of the interface match the
432 // functions/methods of the class
433 success = validate_interface_methods(impl, ifcl, classfunctions_gap,
434 objmethods_gap);
435 clear_tv(&tv);
436 }
437
438 return success;
439}
440
441/*
442 * Check no function argument name is used as a class member.
443 * (Object members are always accessed with "this." prefix, so no need
444 * to check them.)
445 */
446 static int
447check_func_arg_names(
448 garray_T *classfunctions_gap,
449 garray_T *objmethods_gap,
450 garray_T *classmembers_gap)
451{
452 int success = TRUE;
453
454 // loop 1: class functions, loop 2: object methods
455 for (int loop = 1; loop <= 2 && success; ++loop)
456 {
457 garray_T *gap = loop == 1 ? classfunctions_gap : objmethods_gap;
458
459 for (int fi = 0; fi < gap->ga_len && success; ++fi)
460 {
461 ufunc_T *uf = ((ufunc_T **)gap->ga_data)[fi];
462
463 for (int i = 0; i < uf->uf_args.ga_len && success; ++i)
464 {
465 char_u *aname = ((char_u **)uf->uf_args.ga_data)[i];
466 garray_T *mgap = classmembers_gap;
467
468 // Check all the class member names
469 for (int mi = 0; mi < mgap->ga_len; ++mi)
470 {
471 char_u *mname = ((ocmember_T *)mgap->ga_data + mi)
472 ->ocm_name;
473 if (STRCMP(aname, mname) == 0)
474 {
475 success = FALSE;
476
477 if (uf->uf_script_ctx.sc_sid > 0)
478 SOURCING_LNUM = uf->uf_script_ctx.sc_lnum;
479
480 semsg(_(e_argument_already_declared_in_class_str),
481 aname);
482 break;
483 }
484 }
485 }
486 }
487 }
488
489 return success;
490}
491
492/*
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +0200493 * Returns TRUE if the member "varname" is already defined.
494 */
495 static int
496is_duplicate_member(garray_T *mgap, char_u *varname, char_u *varname_end)
497{
498 char_u *pstr = (*varname == '_') ? varname + 1 : varname;
499
500 for (int i = 0; i < mgap->ga_len; ++i)
501 {
502 ocmember_T *m = ((ocmember_T *)mgap->ga_data) + i;
503 char_u *qstr = *m->ocm_name == '_' ? m->ocm_name + 1 : m->ocm_name;
504 if (STRNCMP(pstr, qstr, varname_end - pstr) == 0)
505 {
506 char_u *name = vim_strnsave(varname, varname_end - varname);
507 semsg(_(e_duplicate_member_str), name);
508 vim_free(name);
509 return TRUE;
510 }
511 }
512
513 return FALSE;
514}
515
516/*
517 * Returns TRUE if the method "name" is already defined.
518 */
519 static int
520is_duplicate_method(garray_T *fgap, char_u *name)
521{
522 char_u *pstr = (*name == '_') ? name + 1 : name;
523
524 for (int i = 0; i < fgap->ga_len; ++i)
525 {
526 char_u *n = ((ufunc_T **)fgap->ga_data)[i]->uf_name;
527 char_u *qstr = *n == '_' ? n + 1 : n;
528 if (STRCMP(pstr, qstr) == 0)
529 {
530 semsg(_(e_duplicate_function_str), name);
531 return TRUE;
532 }
533 }
534
535 return FALSE;
536}
537
538/*
Yegappan Lakshmananb1027282023-08-19 11:26:42 +0200539 * Update the interface class lookup table for the member index on the
540 * interface to the member index in the class implementing the interface.
541 * And a lookup table for the object method index on the interface
542 * to the object method index in the class implementing the interface.
543 * This is also used for updating the lookup table for the extended class
544 * hierarchy.
545 */
546 static int
547update_member_method_lookup_table(
548 class_T *ifcl,
549 class_T *cl,
550 garray_T *objmethods,
551 int pobj_method_offset,
552 int is_interface)
553{
554 if (ifcl == NULL)
555 return OK;
556
557 // Table for members.
558 itf2class_T *if2cl = alloc_clear(sizeof(itf2class_T)
559 + ifcl->class_obj_member_count * sizeof(int));
560 if (if2cl == NULL)
561 return FAIL;
562 if2cl->i2c_next = ifcl->class_itf2class;
563 ifcl->class_itf2class = if2cl;
564 if2cl->i2c_class = cl;
565 if2cl->i2c_is_method = FALSE;
566
567 for (int if_i = 0; if_i < ifcl->class_obj_member_count; ++if_i)
568 for (int cl_i = 0; cl_i < cl->class_obj_member_count; ++cl_i)
569 {
570 if (STRCMP(ifcl->class_obj_members[if_i].ocm_name,
571 cl->class_obj_members[cl_i].ocm_name) == 0)
572 {
573 int *table = (int *)(if2cl + 1);
574 table[if_i] = cl_i;
575 break;
576 }
577 }
578
579 // Table for methods.
580 if2cl = alloc_clear(sizeof(itf2class_T)
581 + ifcl->class_obj_method_count * sizeof(int));
582 if (if2cl == NULL)
583 return FAIL;
584 if2cl->i2c_next = ifcl->class_itf2class;
585 ifcl->class_itf2class = if2cl;
586 if2cl->i2c_class = cl;
587 if2cl->i2c_is_method = TRUE;
588
589 for (int if_i = 0; if_i < ifcl->class_obj_method_count; ++if_i)
590 {
591 int done = FALSE;
592 for (int cl_i = 0; cl_i < objmethods->ga_len; ++cl_i)
593 {
594 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
595 ((ufunc_T **)objmethods->ga_data)[cl_i]->uf_name)
596 == 0)
597 {
598 int *table = (int *)(if2cl + 1);
599 table[if_i] = cl_i;
600 done = TRUE;
601 break;
602 }
603 }
604
605 // extended class object method is not overridden by the child class.
606 // Keep the method declared in one of the parent classes in the
607 // lineage.
608 if (!done && !is_interface)
609 {
610 // If "ifcl" is not the immediate parent of "cl", then search in
611 // the intermediate parent classes.
612 if (cl->class_extends != ifcl)
613 {
614 class_T *parent = cl->class_extends;
615 int method_offset = objmethods->ga_len;
616
617 while (!done && parent != NULL && parent != ifcl)
618 {
619
620 for (int cl_i = 0;
621 cl_i < parent->class_obj_method_count_child; ++cl_i)
622 {
623 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
624 parent->class_obj_methods[cl_i]->uf_name)
625 == 0)
626 {
627 int *table = (int *)(if2cl + 1);
628 table[if_i] = method_offset + cl_i;
629 done = TRUE;
630 break;
631 }
632 }
633 method_offset += parent->class_obj_method_count_child;
634 parent = parent->class_extends;
635 }
636 }
637
638 if (!done)
639 {
640 int *table = (int *)(if2cl + 1);
641 table[if_i] = pobj_method_offset + if_i;
642 }
643 }
644 }
645
646 return OK;
647}
648
649/*
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200650 * Update the member and object method lookup tables for a new class in the
651 * interface class.
652 * For each interface add a lookup table for the member index on the interface
653 * to the member index in the new class. And a lookup table for the object
654 * method index on the interface to the object method index in the new class.
655 */
656 static int
657add_lookup_tables(class_T *cl, class_T *extends_cl, garray_T *objmethods_gap)
658{
659 for (int i = 0; i < cl->class_interface_count; ++i)
660 {
661 class_T *ifcl = cl->class_interfaces_cl[i];
662
663 if (update_member_method_lookup_table(ifcl, cl, objmethods_gap,
664 0, TRUE) == FAIL)
665 return FAIL;
666 }
667
668 // Update the lookup table for the extended class, if nay
669 if (extends_cl != NULL)
670 {
671 class_T *pclass = extends_cl;
672 int pobj_method_offset = objmethods_gap->ga_len;
673
674 // Update the entire lineage of extended classes.
675 while (pclass != NULL)
676 {
677 if (update_member_method_lookup_table(pclass, cl,
678 objmethods_gap, pobj_method_offset, FALSE) == FAIL)
679 return FAIL;
680
681 pobj_method_offset += pclass->class_obj_method_count_child;
682 pclass = pclass->class_extends;
683 }
684 }
685
686 return OK;
687}
688
689/*
690 * Add class members to a new class. Allocate a typval for each class member
691 * and initialize it.
692 */
693 static void
694add_class_members(class_T *cl, exarg_T *eap)
695{
696 // Allocate a typval for each class member and initialize it.
697 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
698 cl->class_class_member_count);
699 if (cl->class_members_tv == NULL)
700 return;
701
702 for (int i = 0; i < cl->class_class_member_count; ++i)
703 {
704 ocmember_T *m = &cl->class_class_members[i];
705 typval_T *tv = &cl->class_members_tv[i];
706 if (m->ocm_init != NULL)
707 {
708 typval_T *etv = eval_expr(m->ocm_init, eap);
709 if (etv != NULL)
710 {
711 *tv = *etv;
712 vim_free(etv);
713 }
714 }
715 else
716 {
717 // TODO: proper default value
718 tv->v_type = m->ocm_type->tt_type;
719 tv->vval.v_string = NULL;
720 }
721 }
722}
723
724/*
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +0200725 * Add a default constructor method (new()) to the class "cl".
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200726 */
727 static void
728add_default_constructor(
729 class_T *cl,
730 garray_T *classfunctions_gap,
731 garray_T *type_list_gap)
732{
733 garray_T fga;
734
735 ga_init2(&fga, 1, 1000);
736 ga_concat(&fga, (char_u *)"new(");
737 for (int i = 0; i < cl->class_obj_member_count; ++i)
738 {
739 if (i > 0)
740 ga_concat(&fga, (char_u *)", ");
741 ga_concat(&fga, (char_u *)"this.");
742 ocmember_T *m = cl->class_obj_members + i;
743 ga_concat(&fga, (char_u *)m->ocm_name);
744 ga_concat(&fga, (char_u *)" = v:none");
745 }
746 ga_concat(&fga, (char_u *)")\nenddef\n");
747 ga_append(&fga, NUL);
748
749 exarg_T fea;
750 CLEAR_FIELD(fea);
751 fea.cmdidx = CMD_def;
752 fea.cmd = fea.arg = fga.ga_data;
753
754 garray_T lines_to_free;
755 ga_init2(&lines_to_free, sizeof(char_u *), 50);
756
757 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS);
758
759 ga_clear_strings(&lines_to_free);
760 vim_free(fga.ga_data);
761
762 if (nf != NULL && ga_grow(classfunctions_gap, 1) == OK)
763 {
764 ((ufunc_T **)classfunctions_gap->ga_data)[classfunctions_gap->ga_len]
765 = nf;
766 ++classfunctions_gap->ga_len;
767
768 nf->uf_flags |= FC_NEW;
769 nf->uf_ret_type = get_type_ptr(type_list_gap);
770 if (nf->uf_ret_type != NULL)
771 {
772 nf->uf_ret_type->tt_type = VAR_OBJECT;
773 nf->uf_ret_type->tt_class = cl;
774 nf->uf_ret_type->tt_argcount = 0;
775 nf->uf_ret_type->tt_args = NULL;
776 }
777 }
778}
779
780/*
781 * Add the class functions and object methods to the new class "cl".
782 * When extending a class, add the functions and methods from the parent class
783 * also.
784 */
785 static int
786add_classfuncs_objmethods(
787 class_T *cl,
788 class_T *extends_cl,
789 garray_T *classfunctions_gap,
790 garray_T *objmethods_gap)
791{
792 // loop 1: class functions, loop 2: object methods
793 for (int loop = 1; loop <= 2; ++loop)
794 {
795 garray_T *gap = loop == 1 ? classfunctions_gap : objmethods_gap;
796 int *fcount = loop == 1 ? &cl->class_class_function_count
797 : &cl->class_obj_method_count;
798 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
799 : &cl->class_obj_methods;
800
801 int parent_count = 0;
802 if (extends_cl != NULL)
803 // Include functions from the parent.
804 parent_count = loop == 1
805 ? extends_cl->class_class_function_count
806 : extends_cl->class_obj_method_count;
807
808 *fcount = parent_count + gap->ga_len;
809 if (*fcount == 0)
810 {
811 *fup = NULL;
812 continue;
813 }
814 *fup = ALLOC_MULT(ufunc_T *, *fcount);
815 if (*fup == NULL)
816 return FAIL;
817
818 if (gap->ga_len != 0)
819 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
820 vim_free(gap->ga_data);
821 if (loop == 1)
822 cl->class_class_function_count_child = gap->ga_len;
823 else
824 cl->class_obj_method_count_child = gap->ga_len;
825
826 int skipped = 0;
827 for (int i = 0; i < parent_count; ++i)
828 {
829 // Copy functions from the parent. Can't use the same
830 // function, because "uf_class" is different and compilation
831 // will have a different result.
832 // Put them after the functions in the current class, object
833 // methods may be overruled, then "super.Method()" is used to
834 // find a method from the parent.
835 // Skip "new" functions. TODO: not all of them.
836 if (loop == 1 && STRNCMP(
837 extends_cl->class_class_functions[i]->uf_name,
838 "new", 3) == 0)
839 ++skipped;
840 else
841 {
842 ufunc_T *pf = (loop == 1
843 ? extends_cl->class_class_functions
844 : extends_cl->class_obj_methods)[i];
845 (*fup)[gap->ga_len + i - skipped] = copy_function(pf);
846
847 // If the child class overrides a function from the parent
848 // the signature must be equal.
849 char_u *pname = pf->uf_name;
850 for (int ci = 0; ci < gap->ga_len; ++ci)
851 {
852 ufunc_T *cf = (*fup)[ci];
853 char_u *cname = cf->uf_name;
854 if (STRCMP(pname, cname) == 0)
855 {
856 where_T where = WHERE_INIT;
857 where.wt_func_name = (char *)pname;
858 where.wt_kind = WT_METHOD;
859 (void)check_type(pf->uf_func_type, cf->uf_func_type,
860 TRUE, where);
861 }
862 }
863 }
864 }
865
866 *fcount -= skipped;
867
868 // Set the class pointer on all the functions and object methods.
869 for (int i = 0; i < *fcount; ++i)
870 {
871 ufunc_T *fp = (*fup)[i];
872 fp->uf_class = cl;
873 if (loop == 2)
874 fp->uf_flags |= FC_OBJECT;
875 }
876 }
877
878 return OK;
879}
880
881/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000882 * Handle ":class" and ":abstract class" up to ":endclass".
Bram Moolenaar554d0312023-01-05 19:59:18 +0000883 * Handle ":interface" up to ":endinterface".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000884 */
885 void
886ex_class(exarg_T *eap)
887{
Bram Moolenaar83ae6152023-02-25 19:59:31 +0000888 int is_class = eap->cmdidx == CMD_class; // FALSE for :interface
889 long start_lnum = SOURCING_LNUM;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000890
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000891 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000892 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000893 if (is_abstract)
894 {
895 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
896 {
897 semsg(_(e_invalid_argument_str), arg);
898 return;
899 }
900 arg = skipwhite(arg + 5);
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000901 is_class = TRUE;
902 }
903
904 if (!current_script_is_vim9()
905 || (cmdmod.cmod_flags & CMOD_LEGACY)
906 || !getline_equal(eap->getline, eap->cookie, getsourceline))
907 {
908 if (is_class)
909 emsg(_(e_class_can_only_be_defined_in_vim9_script));
910 else
911 emsg(_(e_interface_can_only_be_defined_in_vim9_script));
912 return;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000913 }
914
915 if (!ASCII_ISUPPER(*arg))
916 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000917 if (is_class)
918 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
919 else
920 semsg(_(e_interface_name_must_start_with_uppercase_letter_str),
921 arg);
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000922 return;
923 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000924 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
925 if (!IS_WHITE_OR_NUL(*name_end))
926 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000927 semsg(_(e_white_space_required_after_name_str), arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000928 return;
929 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000930 char_u *name_start = arg;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000931
Bram Moolenaara86655a2023-01-12 17:06:27 +0000932 // "export class" gets used when creating the class, don't use "is_export"
933 // for the items inside the class.
934 int class_export = is_export;
935 is_export = FALSE;
936
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000937 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000938 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000939
Bram Moolenaar83677162023-01-08 19:54:10 +0000940 // Name for "extends BaseClass"
941 char_u *extends = NULL;
942
Bram Moolenaar94674f22023-01-06 18:42:20 +0000943 // Names for "implements SomeInterface"
944 garray_T ga_impl;
945 ga_init2(&ga_impl, sizeof(char_u *), 5);
946
947 arg = skipwhite(name_end);
948 while (*arg != NUL && *arg != '#' && *arg != '\n')
949 {
950 // TODO:
Bram Moolenaar94674f22023-01-06 18:42:20 +0000951 // specifies SomeInterface
Bram Moolenaar83677162023-01-08 19:54:10 +0000952 if (STRNCMP(arg, "extends", 7) == 0 && IS_WHITE_OR_NUL(arg[7]))
953 {
954 if (extends != NULL)
955 {
956 emsg(_(e_duplicate_extends));
957 goto early_ret;
958 }
959 arg = skipwhite(arg + 7);
960 char_u *end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
961 if (!IS_WHITE_OR_NUL(*end))
962 {
963 semsg(_(e_white_space_required_after_name_str), arg);
964 goto early_ret;
965 }
966 extends = vim_strnsave(arg, end - arg);
967 if (extends == NULL)
968 goto early_ret;
969
970 arg = skipwhite(end + 1);
971 }
972 else if (STRNCMP(arg, "implements", 10) == 0
973 && IS_WHITE_OR_NUL(arg[10]))
Bram Moolenaar94674f22023-01-06 18:42:20 +0000974 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000975 if (ga_impl.ga_len > 0)
976 {
977 emsg(_(e_duplicate_implements));
978 goto early_ret;
979 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000980 arg = skipwhite(arg + 10);
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000981
982 for (;;)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000983 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000984 char_u *impl_end = find_name_end(arg, NULL, NULL,
985 FNE_CHECK_START);
986 if (!IS_WHITE_OR_NUL(*impl_end) && *impl_end != ',')
987 {
988 semsg(_(e_white_space_required_after_name_str), arg);
989 goto early_ret;
990 }
991 char_u *iname = vim_strnsave(arg, impl_end - arg);
992 if (iname == NULL)
993 goto early_ret;
994 for (int i = 0; i < ga_impl.ga_len; ++i)
995 if (STRCMP(((char_u **)ga_impl.ga_data)[i], iname) == 0)
996 {
997 semsg(_(e_duplicate_interface_after_implements_str),
998 iname);
999 vim_free(iname);
1000 goto early_ret;
1001 }
1002 if (ga_add_string(&ga_impl, iname) == FAIL)
1003 {
1004 vim_free(iname);
1005 goto early_ret;
1006 }
1007 if (*impl_end != ',')
1008 {
1009 arg = skipwhite(impl_end);
1010 break;
1011 }
1012 arg = skipwhite(impl_end + 1);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001013 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001014 }
1015 else
1016 {
1017 semsg(_(e_trailing_characters_str), arg);
1018early_ret:
Bram Moolenaar83677162023-01-08 19:54:10 +00001019 vim_free(extends);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001020 ga_clear_strings(&ga_impl);
1021 return;
1022 }
1023 }
1024
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001025 garray_T type_list; // list of pointers to allocated types
1026 ga_init2(&type_list, sizeof(type_T *), 10);
1027
Bram Moolenaard505d172022-12-18 21:42:55 +00001028 // Growarray with class members declared in the class.
1029 garray_T classmembers;
1030 ga_init2(&classmembers, sizeof(ocmember_T), 10);
1031
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001032 // Growarray with functions declared in the class.
1033 garray_T classfunctions;
1034 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +00001035
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001036 // Growarray with object members declared in the class.
1037 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +00001038 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001039
1040 // Growarray with object methods declared in the class.
1041 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001042 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001043
1044 /*
Bram Moolenaar554d0312023-01-05 19:59:18 +00001045 * Go over the body of the class/interface until "endclass" or
1046 * "endinterface" is found.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001047 */
1048 char_u *theline = NULL;
1049 int success = FALSE;
1050 for (;;)
1051 {
1052 vim_free(theline);
1053 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
1054 if (theline == NULL)
1055 break;
1056 char_u *line = skipwhite(theline);
1057
Bram Moolenaar418b5472022-12-20 13:38:22 +00001058 // Skip empty and comment lines.
1059 if (*line == NUL)
1060 continue;
1061 if (*line == '#')
1062 {
1063 if (vim9_bad_comment(line))
1064 break;
1065 continue;
1066 }
1067
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001068 char_u *p = line;
Bram Moolenaar554d0312023-01-05 19:59:18 +00001069 char *end_name = is_class ? "endclass" : "endinterface";
1070 if (checkforcmd(&p, end_name, is_class ? 4 : 5))
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001071 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001072 if (STRNCMP(line, end_name, is_class ? 8 : 12) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001073 semsg(_(e_command_cannot_be_shortened_str), line);
1074 else if (*p == '|' || !ends_excmd2(line, p))
1075 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +00001076 else
1077 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001078 break;
1079 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00001080 char *wrong_name = is_class ? "endinterface" : "endclass";
1081 if (checkforcmd(&p, wrong_name, is_class ? 5 : 4))
1082 {
Bram Moolenaar657aea72023-01-27 13:16:19 +00001083 semsg(_(e_invalid_command_str_expected_str), line, end_name);
Bram Moolenaar554d0312023-01-05 19:59:18 +00001084 break;
1085 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001086
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001087 int has_public = FALSE;
1088 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001089 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001090 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001091 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001092 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001093 break;
1094 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001095 has_public = TRUE;
1096 p = skipwhite(line + 6);
1097
Bram Moolenaard505d172022-12-18 21:42:55 +00001098 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001099 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001100 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001101 break;
1102 }
1103 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001104
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001105 int has_static = FALSE;
1106 char_u *ps = p;
1107 if (checkforcmd(&p, "static", 4))
1108 {
1109 if (STRNCMP(ps, "static", 6) != 0)
1110 {
1111 semsg(_(e_command_cannot_be_shortened_str), ps);
1112 break;
1113 }
1114 has_static = TRUE;
1115 p = skipwhite(ps + 6);
1116 }
1117
Bram Moolenaard505d172022-12-18 21:42:55 +00001118 // object members (public, read access, private):
1119 // "this._varname"
1120 // "this.varname"
1121 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001122 if (STRNCMP(p, "this", 4) == 0)
1123 {
1124 if (p[4] != '.' || !eval_isnamec1(p[5]))
1125 {
1126 semsg(_(e_invalid_object_member_declaration_str), p);
1127 break;
1128 }
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001129 if (has_static)
1130 {
1131 emsg(_(e_static_cannot_be_followed_by_this));
1132 break;
1133 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001134 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +00001135 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +00001136 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +00001137 char_u *init_expr = NULL;
1138 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +00001139 &varname_end, &type_list, &type,
1140 is_class ? &init_expr: NULL) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +00001141 break;
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001142 if (is_duplicate_member(&objmembers, varname, varname_end))
1143 {
1144 vim_free(init_expr);
1145 break;
1146 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001147 if (add_member(&objmembers, varname, varname_end,
1148 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +00001149 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001150 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001151 break;
1152 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001153 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001154
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001155 // constructors:
1156 // def new()
1157 // enddef
1158 // def newOther()
1159 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001160 // object methods and class functions:
1161 // def SomeMethod()
1162 // enddef
1163 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001164 // enddef
1165 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001166 // def <Tval> someMethod()
1167 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001168 else if (checkforcmd(&p, "def", 3))
1169 {
1170 exarg_T ea;
1171 garray_T lines_to_free;
1172
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001173 // TODO: error for "public static def Func()"?
1174
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001175 CLEAR_FIELD(ea);
1176 ea.cmd = line;
1177 ea.arg = p;
1178 ea.cmdidx = CMD_def;
1179 ea.getline = eap->getline;
1180 ea.cookie = eap->cookie;
1181
1182 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +00001183 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free,
1184 is_class ? CF_CLASS : CF_INTERFACE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001185 ga_clear_strings(&lines_to_free);
1186
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001187 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001188 {
Bram Moolenaar58b40092023-01-11 15:59:05 +00001189 char_u *name = uf->uf_name;
1190 int is_new = STRNCMP(name, "new", 3) == 0;
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001191 if (is_new && is_abstract)
1192 {
1193 emsg(_(e_cannot_define_new_function_in_abstract_class));
1194 success = FALSE;
Yegappan Lakshmananb1027282023-08-19 11:26:42 +02001195 func_clear_free(uf, FALSE);
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001196 break;
1197 }
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001198 if (is_new)
1199 {
1200 // A return type should not be specified for the new()
1201 // constructor method.
1202 if (uf->uf_ret_type->tt_type != VAR_VOID)
1203 {
1204 emsg(_(e_cannot_use_a_return_type_with_new));
1205 success = FALSE;
1206 func_clear_free(uf, FALSE);
1207 break;
1208 }
1209 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001210 garray_T *fgap = has_static || is_new
1211 ? &classfunctions : &objmethods;
Bram Moolenaar58b40092023-01-11 15:59:05 +00001212 // Check the name isn't used already.
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001213 if (is_duplicate_method(fgap, name))
1214 break;
Bram Moolenaar58b40092023-01-11 15:59:05 +00001215
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001216 if (ga_grow(fgap, 1) == OK)
1217 {
1218 if (is_new)
1219 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001220
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02001221 // If the method name starts with '_', then it a private
1222 // method.
1223 if (*name == '_')
1224 uf->uf_private = TRUE;
1225
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001226 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
1227 ++fgap->ga_len;
1228 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001229 }
1230 }
1231
1232 // class members
1233 else if (has_static)
1234 {
1235 // class members (public, read access, private):
1236 // "static _varname"
1237 // "static varname"
1238 // "public static varname"
1239 char_u *varname = p;
1240 char_u *varname_end = NULL;
1241 type_T *type = NULL;
1242 char_u *init_expr = NULL;
1243 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +00001244 &varname_end, &type_list, &type,
1245 is_class ? &init_expr : NULL) == FAIL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001246 break;
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001247 if (is_duplicate_member(&classmembers, varname, varname_end))
1248 {
1249 vim_free(init_expr);
1250 break;
1251 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001252 if (add_member(&classmembers, varname, varname_end,
1253 has_public, type, init_expr) == FAIL)
1254 {
1255 vim_free(init_expr);
1256 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001257 }
1258 }
1259
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001260 else
1261 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001262 if (is_class)
1263 semsg(_(e_not_valid_command_in_class_str), line);
1264 else
1265 semsg(_(e_not_valid_command_in_interface_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001266 break;
1267 }
1268 }
1269 vim_free(theline);
1270
Bram Moolenaar83677162023-01-08 19:54:10 +00001271 class_T *extends_cl = NULL; // class from "extends" argument
1272
1273 /*
1274 * Check a few things before defining the class.
1275 */
1276
1277 // Check the "extends" class is valid.
1278 if (success && extends != NULL)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001279 success = validate_extends_class(extends, &extends_cl);
Bram Moolenaar83677162023-01-08 19:54:10 +00001280 VIM_CLEAR(extends);
1281
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001282 class_T **intf_classes = NULL;
1283
Bram Moolenaar83677162023-01-08 19:54:10 +00001284 // Check all "implements" entries are valid.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001285 if (success && ga_impl.ga_len > 0)
1286 {
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001287 intf_classes = ALLOC_CLEAR_MULT(class_T *, ga_impl.ga_len);
1288
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001289 success = validate_implements_classes(&ga_impl, intf_classes,
1290 &classfunctions, &classmembers,
1291 &objmethods, &objmembers);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001292 }
1293
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001294 // Check no function argument name is used as a class member.
Bram Moolenaard40f00c2023-01-13 17:36:49 +00001295 if (success)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001296 success = check_func_arg_names(&classfunctions, &objmethods,
1297 &classmembers);
Bram Moolenaard40f00c2023-01-13 17:36:49 +00001298
Bram Moolenaareb533502022-12-14 15:06:11 +00001299 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001300 if (success)
1301 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001302 // "endclass" encountered without failures: Create the class.
1303
Bram Moolenaareb533502022-12-14 15:06:11 +00001304 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001305 if (cl == NULL)
1306 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +00001307 if (!is_class)
1308 cl->class_flags = CLASS_INTERFACE;
1309
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001310 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +00001311 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +00001312 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001313 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +00001314
Bram Moolenaard0200c82023-01-28 15:19:40 +00001315 if (extends_cl != NULL)
1316 {
1317 cl->class_extends = extends_cl;
1318 extends_cl->class_flags |= CLASS_EXTENDED;
1319 }
Bram Moolenaar83677162023-01-08 19:54:10 +00001320
Bram Moolenaard505d172022-12-18 21:42:55 +00001321 // Add class and object members to "cl".
1322 if (add_members_to_class(&classmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +00001323 extends_cl == NULL ? NULL
1324 : extends_cl->class_class_members,
1325 extends_cl == NULL ? 0
1326 : extends_cl->class_class_member_count,
1327 &cl->class_class_members,
1328 &cl->class_class_member_count) == FAIL
Bram Moolenaard505d172022-12-18 21:42:55 +00001329 || add_members_to_class(&objmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +00001330 extends_cl == NULL ? NULL
1331 : extends_cl->class_obj_members,
1332 extends_cl == NULL ? 0
1333 : extends_cl->class_obj_member_count,
1334 &cl->class_obj_members,
1335 &cl->class_obj_member_count) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +00001336 goto cleanup;
1337
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001338 if (ga_impl.ga_len > 0)
1339 {
1340 // Move the "implements" names into the class.
1341 cl->class_interface_count = ga_impl.ga_len;
1342 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
1343 if (cl->class_interfaces == NULL)
1344 goto cleanup;
1345 for (int i = 0; i < ga_impl.ga_len; ++i)
1346 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
1347 VIM_CLEAR(ga_impl.ga_data);
1348 ga_impl.ga_len = 0;
1349
Bram Moolenaard0200c82023-01-28 15:19:40 +00001350 cl->class_interfaces_cl = intf_classes;
1351 intf_classes = NULL;
1352 }
1353
1354 if (cl->class_interface_count > 0 || extends_cl != NULL)
1355 {
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001356 // Add a method and member lookup table to each of the interface
1357 // classes.
1358 if (add_lookup_tables(cl, extends_cl, &objmethods) == FAIL)
1359 goto cleanup;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001360 }
1361
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001362 // Allocate a typval for each class member and initialize it.
Bram Moolenaar554d0312023-01-05 19:59:18 +00001363 if (is_class && cl->class_class_member_count > 0)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001364 add_class_members(cl, eap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001365
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001366 int have_new = FALSE;
1367 ufunc_T *class_func = NULL;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001368 for (int i = 0; i < classfunctions.ga_len; ++i)
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001369 {
1370 class_func = ((ufunc_T **)classfunctions.ga_data)[i];
1371 if (STRCMP(class_func->uf_name, "new") == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001372 {
1373 have_new = TRUE;
1374 break;
1375 }
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001376 }
1377
1378 if (have_new)
1379 // The return type of new() is an object of class "cl"
1380 class_func->uf_ret_type->tt_class = cl;
1381 else if (is_class && !is_abstract && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001382 // No new() method was defined, add the default constructor.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001383 add_default_constructor(cl, &classfunctions, &type_list);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001384
Bram Moolenaar58b40092023-01-11 15:59:05 +00001385 // Move all the functions into the created class.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001386 if (add_classfuncs_objmethods(cl, extends_cl, &classfunctions,
1387 &objmethods) == FAIL)
1388 goto cleanup;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001389
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001390 cl->class_type.tt_type = VAR_CLASS;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001391 cl->class_type.tt_class = cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001392 cl->class_object_type.tt_type = VAR_OBJECT;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001393 cl->class_object_type.tt_class = cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001394 cl->class_type_list = type_list;
1395
1396 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +00001397 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001398
1399 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001400 // TODO: handle other context, e.g. in a function
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001401 typval_T tv;
1402 tv.v_type = VAR_CLASS;
1403 tv.vval.v_class = cl;
Bram Moolenaara86655a2023-01-12 17:06:27 +00001404 is_export = class_export;
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001405 SOURCING_LNUM = start_lnum;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001406 set_var_const(cl->class_name, current_sctx.sc_sid,
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001407 NULL, &tv, FALSE, 0, 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001408 return;
1409 }
1410
1411cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +00001412 if (cl != NULL)
1413 {
1414 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001415 vim_free(cl->class_class_functions);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001416 if (cl->class_interfaces != NULL)
1417 {
1418 for (int i = 0; i < cl->class_interface_count; ++i)
1419 vim_free(cl->class_interfaces[i]);
1420 vim_free(cl->class_interfaces);
1421 }
1422 if (cl->class_interfaces_cl != NULL)
1423 {
1424 for (int i = 0; i < cl->class_interface_count; ++i)
1425 class_unref(cl->class_interfaces_cl[i]);
1426 vim_free(cl->class_interfaces_cl);
1427 }
Bram Moolenaareb533502022-12-14 15:06:11 +00001428 vim_free(cl->class_obj_members);
1429 vim_free(cl->class_obj_methods);
1430 vim_free(cl);
1431 }
1432
Bram Moolenaar83677162023-01-08 19:54:10 +00001433 vim_free(extends);
1434 class_unref(extends_cl);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001435
1436 if (intf_classes != NULL)
1437 {
1438 for (int i = 0; i < ga_impl.ga_len; ++i)
1439 class_unref(intf_classes[i]);
1440 vim_free(intf_classes);
1441 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001442 ga_clear_strings(&ga_impl);
1443
Bram Moolenaard505d172022-12-18 21:42:55 +00001444 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001445 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001446 garray_T *gap = round == 1 ? &classmembers : &objmembers;
1447 if (gap->ga_len == 0 || gap->ga_data == NULL)
1448 continue;
1449
1450 for (int i = 0; i < gap->ga_len; ++i)
1451 {
1452 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
1453 vim_free(m->ocm_name);
1454 vim_free(m->ocm_init);
1455 }
1456 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001457 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001458
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001459 for (int i = 0; i < objmethods.ga_len; ++i)
1460 {
1461 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
1462 func_clear_free(uf, FALSE);
1463 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001464 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001465
1466 for (int i = 0; i < classfunctions.ga_len; ++i)
1467 {
1468 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
1469 func_clear_free(uf, FALSE);
1470 }
1471 ga_clear(&classfunctions);
1472
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001473 clear_type_list(&type_list);
1474}
1475
1476/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001477 * Find member "name" in class "cl", set "member_idx" to the member index and
1478 * return its type.
1479 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001480 */
1481 type_T *
1482class_member_type(
1483 class_T *cl,
1484 char_u *name,
1485 char_u *name_end,
1486 int *member_idx)
1487{
1488 *member_idx = -1; // not found (yet)
1489 size_t len = name_end - name;
1490
1491 for (int i = 0; i < cl->class_obj_member_count; ++i)
1492 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001493 ocmember_T *m = cl->class_obj_members + i;
1494 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001495 {
1496 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001497 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001498 }
1499 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001500
1501 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001502 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001503}
1504
1505/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001506 * Handle ":enum" up to ":endenum".
1507 */
1508 void
1509ex_enum(exarg_T *eap UNUSED)
1510{
1511 // TODO
1512}
1513
1514/*
1515 * Handle ":type".
1516 */
1517 void
1518ex_type(exarg_T *eap UNUSED)
1519{
1520 // TODO
1521}
1522
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001523/*
1524 * Evaluate what comes after a class:
1525 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001526 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001527 * - class constructor: SomeClass.new()
1528 * - object member: someObject.varname
1529 * - object method: someObject.SomeMethod()
1530 *
1531 * "*arg" points to the '.'.
1532 * "*arg" is advanced to after the member name or method call.
1533 *
1534 * Returns FAIL or OK.
1535 */
1536 int
1537class_object_index(
1538 char_u **arg,
1539 typval_T *rettv,
1540 evalarg_T *evalarg,
1541 int verbose UNUSED) // give error messages
1542{
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001543 if (VIM_ISWHITE((*arg)[1]))
1544 {
1545 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
1546 return FAIL;
1547 }
1548
1549 ++*arg;
1550 char_u *name = *arg;
1551 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1552 if (name_end == name)
1553 return FAIL;
1554 size_t len = name_end - name;
1555
Bram Moolenaar552bdca2023-02-17 21:08:50 +00001556 class_T *cl;
1557 if (rettv->v_type == VAR_CLASS)
1558 cl = rettv->vval.v_class;
1559 else // VAR_OBJECT
1560 {
1561 if (rettv->vval.v_object == NULL)
1562 {
1563 emsg(_(e_using_null_object));
1564 return FAIL;
1565 }
1566 cl = rettv->vval.v_object->obj_class;
1567 }
1568
Bram Moolenaard13dd302023-03-11 20:56:35 +00001569 if (cl == NULL)
1570 {
1571 emsg(_(e_incomplete_type));
1572 return FAIL;
1573 }
1574
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001575 if (*name_end == '(')
1576 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001577 int on_class = rettv->v_type == VAR_CLASS;
1578 int count = on_class ? cl->class_class_function_count
1579 : cl->class_obj_method_count;
1580 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001581 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001582 ufunc_T *fp = on_class ? cl->class_class_functions[i]
1583 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +00001584 // Use a separate pointer to avoid that ASAN complains about
1585 // uf_name[] only being 4 characters.
1586 char_u *ufname = (char_u *)fp->uf_name;
1587 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001588 {
1589 typval_T argvars[MAX_FUNC_ARGS + 1];
1590 int argcount = 0;
1591
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02001592 if (fp->uf_private)
1593 {
1594 // Cannot access a private method outside of a class
1595 semsg(_(e_cannot_access_private_method_str), name);
1596 return FAIL;
1597 }
1598
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001599 char_u *argp = name_end;
1600 int ret = get_func_arguments(&argp, evalarg, 0,
1601 argvars, &argcount);
1602 if (ret == FAIL)
1603 return FAIL;
1604
1605 funcexe_T funcexe;
1606 CLEAR_FIELD(funcexe);
1607 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001608 if (rettv->v_type == VAR_OBJECT)
1609 {
1610 funcexe.fe_object = rettv->vval.v_object;
1611 ++funcexe.fe_object->obj_refcount;
1612 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001613
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001614 // Clear the class or object after calling the function, in
1615 // case the refcount is one.
1616 typval_T tv_tofree = *rettv;
1617 rettv->v_type = VAR_UNKNOWN;
1618
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001619 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001620 int error = call_user_func_check(fp, argcount, argvars,
1621 rettv, &funcexe, NULL);
1622
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001623 // Clear the previous rettv and the arguments.
1624 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001625 for (int idx = 0; idx < argcount; ++idx)
1626 clear_tv(&argvars[idx]);
1627
1628 if (error != FCERR_NONE)
1629 {
1630 user_func_error(error, printable_func_name(fp),
1631 funcexe.fe_found_var);
1632 return FAIL;
1633 }
1634 *arg = argp;
1635 return OK;
1636 }
1637 }
1638
1639 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
1640 }
1641
1642 else if (rettv->v_type == VAR_OBJECT)
1643 {
1644 for (int i = 0; i < cl->class_obj_member_count; ++i)
1645 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001646 ocmember_T *m = &cl->class_obj_members[i];
1647 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001648 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001649 if (*name == '_')
1650 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001651 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001652 return FAIL;
1653 }
1654
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001655 // The object only contains a pointer to the class, the member
1656 // values array follows right after that.
1657 object_T *obj = rettv->vval.v_object;
1658 typval_T *tv = (typval_T *)(obj + 1) + i;
1659 copy_tv(tv, rettv);
1660 object_unref(obj);
1661
1662 *arg = name_end;
1663 return OK;
1664 }
1665 }
1666
1667 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
1668 }
1669
Bram Moolenaard505d172022-12-18 21:42:55 +00001670 else if (rettv->v_type == VAR_CLASS)
1671 {
1672 // class member
1673 for (int i = 0; i < cl->class_class_member_count; ++i)
1674 {
1675 ocmember_T *m = &cl->class_class_members[i];
1676 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
1677 {
1678 if (*name == '_')
1679 {
1680 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
1681 return FAIL;
1682 }
1683
1684 typval_T *tv = &cl->class_members_tv[i];
1685 copy_tv(tv, rettv);
1686 class_unref(cl);
1687
1688 *arg = name_end;
1689 return OK;
1690 }
1691 }
1692
1693 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
1694 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001695
1696 return FAIL;
1697}
1698
1699/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001700 * If "arg" points to a class or object method, return it.
1701 * Otherwise return NULL.
1702 */
1703 ufunc_T *
1704find_class_func(char_u **arg)
1705{
1706 char_u *name = *arg;
1707 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1708 if (name_end == name || *name_end != '.')
1709 return NULL;
1710
1711 size_t len = name_end - name;
1712 typval_T tv;
1713 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001714 if (eval_variable(name, (int)len,
1715 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001716 return NULL;
1717 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001718 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001719
1720 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1721 : tv.vval.v_object->obj_class;
1722 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001723 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001724 char_u *fname = name_end + 1;
1725 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1726 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001727 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001728 len = fname_end - fname;
1729
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001730 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1731 : cl->class_obj_method_count;
1732 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1733 : cl->class_obj_methods;
1734 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001735 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001736 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001737 // Use a separate pointer to avoid that ASAN complains about
1738 // uf_name[] only being 4 characters.
1739 char_u *ufname = (char_u *)fp->uf_name;
1740 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001741 {
1742 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001743 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001744 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001745 }
1746
Bram Moolenaareb533502022-12-14 15:06:11 +00001747fail_after_eval:
1748 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001749 return NULL;
1750}
1751
1752/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001753 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1754 * index in class.class_class_members[].
1755 * If "cl_ret" is not NULL set it to the class.
1756 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001757 */
1758 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001759class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001760{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001761 if (cctx == NULL || cctx->ctx_ufunc == NULL
1762 || cctx->ctx_ufunc->uf_class == NULL)
1763 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001764 class_T *cl = cctx->ctx_ufunc->uf_class;
1765
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001766 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001767 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001768 ocmember_T *m = &cl->class_class_members[i];
1769 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001770 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001771 if (cl_ret != NULL)
1772 *cl_ret = cl;
1773 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001774 }
1775 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001776 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001777}
1778
1779/*
Bram Moolenaar62a69232023-01-24 15:07:04 +00001780 * Return TRUE if current context "cctx_arg" is inside class "cl".
1781 * Return FALSE if not.
1782 */
1783 int
1784inside_class(cctx_T *cctx_arg, class_T *cl)
1785{
1786 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
1787 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class == cl)
1788 return TRUE;
1789 return FALSE;
1790}
1791
1792/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001793 * Make a copy of an object.
1794 */
1795 void
1796copy_object(typval_T *from, typval_T *to)
1797{
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02001798 if (from->vval.v_object == NULL)
1799 to->vval.v_object = NULL;
1800 else
1801 {
1802 to->vval.v_object = from->vval.v_object;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001803 ++to->vval.v_object->obj_refcount;
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02001804 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001805}
1806
1807/*
1808 * Free an object.
1809 */
1810 static void
1811object_clear(object_T *obj)
1812{
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001813 // Avoid a recursive call, it can happen if "obj" has a circular reference.
1814 obj->obj_refcount = INT_MAX;
1815
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001816 class_T *cl = obj->obj_class;
1817
Jia-Ju Bai5b0889b2023-08-13 20:04:04 +02001818 if (!cl)
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02001819 return;
Jia-Ju Bai5b0889b2023-08-13 20:04:04 +02001820
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001821 // the member values are just after the object structure
1822 typval_T *tv = (typval_T *)(obj + 1);
1823 for (int i = 0; i < cl->class_obj_member_count; ++i)
1824 clear_tv(tv + i);
1825
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001826 // Remove from the list headed by "first_object".
1827 object_cleared(obj);
1828
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001829 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001830 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001831}
1832
1833/*
1834 * Unreference an object.
1835 */
1836 void
1837object_unref(object_T *obj)
1838{
1839 if (obj != NULL && --obj->obj_refcount <= 0)
1840 object_clear(obj);
1841}
1842
1843/*
1844 * Make a copy of a class.
1845 */
1846 void
1847copy_class(typval_T *from, typval_T *to)
1848{
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02001849 if (from->vval.v_class == NULL)
1850 to->vval.v_class = NULL;
1851 else
1852 {
1853 to->vval.v_class = from->vval.v_class;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001854 ++to->vval.v_class->class_refcount;
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02001855 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001856}
1857
1858/*
1859 * Unreference a class. Free it when the reference count goes down to zero.
1860 */
1861 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001862class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001863{
Bram Moolenaard505d172022-12-18 21:42:55 +00001864 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001865 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001866 // Freeing what the class contains may recursively come back here.
1867 // Clear "class_name" first, if it is NULL the class does not need to
1868 // be freed.
1869 VIM_CLEAR(cl->class_name);
1870
Bram Moolenaar83677162023-01-08 19:54:10 +00001871 class_unref(cl->class_extends);
1872
Bram Moolenaar94674f22023-01-06 18:42:20 +00001873 for (int i = 0; i < cl->class_interface_count; ++i)
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001874 {
Bram Moolenaar94674f22023-01-06 18:42:20 +00001875 vim_free(((char_u **)cl->class_interfaces)[i]);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001876 if (cl->class_interfaces_cl[i] != NULL)
1877 class_unref(cl->class_interfaces_cl[i]);
1878 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001879 vim_free(cl->class_interfaces);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001880 vim_free(cl->class_interfaces_cl);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001881
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001882 itf2class_T *next;
1883 for (itf2class_T *i2c = cl->class_itf2class; i2c != NULL; i2c = next)
1884 {
1885 next = i2c->i2c_next;
1886 vim_free(i2c);
1887 }
1888
Bram Moolenaard505d172022-12-18 21:42:55 +00001889 for (int i = 0; i < cl->class_class_member_count; ++i)
1890 {
1891 ocmember_T *m = &cl->class_class_members[i];
1892 vim_free(m->ocm_name);
1893 vim_free(m->ocm_init);
1894 if (cl->class_members_tv != NULL)
1895 clear_tv(&cl->class_members_tv[i]);
1896 }
1897 vim_free(cl->class_class_members);
1898 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001899
1900 for (int i = 0; i < cl->class_obj_member_count; ++i)
1901 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001902 ocmember_T *m = &cl->class_obj_members[i];
1903 vim_free(m->ocm_name);
1904 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001905 }
1906 vim_free(cl->class_obj_members);
1907
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001908 for (int i = 0; i < cl->class_class_function_count; ++i)
1909 {
1910 ufunc_T *uf = cl->class_class_functions[i];
1911 func_clear_free(uf, FALSE);
1912 }
1913 vim_free(cl->class_class_functions);
1914
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001915 for (int i = 0; i < cl->class_obj_method_count; ++i)
1916 {
1917 ufunc_T *uf = cl->class_obj_methods[i];
1918 func_clear_free(uf, FALSE);
1919 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001920 vim_free(cl->class_obj_methods);
1921
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001922 clear_type_list(&cl->class_type_list);
1923
1924 vim_free(cl);
1925 }
1926}
1927
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001928static object_T *first_object = NULL;
1929
1930/*
1931 * Call this function when an object has been created. It will be added to the
1932 * list headed by "first_object".
1933 */
1934 void
1935object_created(object_T *obj)
1936{
1937 if (first_object != NULL)
1938 {
1939 obj->obj_next_used = first_object;
1940 first_object->obj_prev_used = obj;
1941 }
1942 first_object = obj;
1943}
1944
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001945static object_T *next_nonref_obj = NULL;
1946
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001947/*
1948 * Call this function when an object has been cleared and is about to be freed.
1949 * It is removed from the list headed by "first_object".
1950 */
1951 void
1952object_cleared(object_T *obj)
1953{
1954 if (obj->obj_next_used != NULL)
1955 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1956 if (obj->obj_prev_used != NULL)
1957 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1958 else if (first_object == obj)
1959 first_object = obj->obj_next_used;
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001960
1961 // update the next object to check if needed
1962 if (obj == next_nonref_obj)
1963 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001964}
1965
1966/*
1967 * Go through the list of all objects and free items without "copyID".
1968 */
1969 int
1970object_free_nonref(int copyID)
1971{
1972 int did_free = FALSE;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001973
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001974 for (object_T *obj = first_object; obj != NULL; obj = next_nonref_obj)
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001975 {
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001976 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001977 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1978 {
1979 // Free the object and items it contains.
1980 object_clear(obj);
1981 did_free = TRUE;
1982 }
1983 }
1984
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001985 next_nonref_obj = NULL;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001986 return did_free;
1987}
1988
LemonBoyafe04662023-08-23 21:08:11 +02001989/*
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02001990 * Return TRUE when the class "cl", its base class or one of the implemented
1991 * interfaces matches the class "other_cl".
LemonBoyafe04662023-08-23 21:08:11 +02001992 */
1993 int
1994class_instance_of(class_T *cl, class_T *other_cl)
1995{
1996 if (cl == other_cl)
1997 return TRUE;
1998
1999 // Recursively check the base classes.
2000 for (; cl != NULL; cl = cl->class_extends)
2001 {
2002 if (cl == other_cl)
2003 return TRUE;
2004 // Check the implemented interfaces.
2005 for (int i = cl->class_interface_count - 1; i >= 0; --i)
2006 if (cl->class_interfaces_cl[i] == other_cl)
2007 return TRUE;
2008 }
2009
2010 return FALSE;
2011}
2012
2013/*
2014 * "instanceof(object, classinfo)" function
2015 */
2016 void
2017f_instanceof(typval_T *argvars, typval_T *rettv)
2018{
2019 typval_T *object_tv = &argvars[0];
2020 typval_T *classinfo_tv = &argvars[1];
2021 listitem_T *li;
2022
2023 rettv->vval.v_number = VVAL_FALSE;
2024
2025 if (check_for_object_arg(argvars, 0) == FAIL
2026 || check_for_class_or_list_arg(argvars, 1) == FAIL)
2027 return;
2028
2029 if (classinfo_tv->v_type == VAR_LIST)
2030 {
2031 FOR_ALL_LIST_ITEMS(classinfo_tv->vval.v_list, li)
2032 {
2033 if (li->li_tv.v_type != VAR_CLASS)
2034 {
2035 emsg(_(e_class_required));
2036 return;
2037 }
2038
2039 if (class_instance_of(object_tv->vval.v_object->obj_class,
2040 li->li_tv.vval.v_class) == TRUE)
2041 {
2042 rettv->vval.v_number = VVAL_TRUE;
2043 return;
2044 }
2045 }
2046 }
2047 else if (classinfo_tv->v_type == VAR_CLASS)
2048 {
2049 rettv->vval.v_number = class_instance_of(object_tv->vval.v_object->obj_class,
2050 classinfo_tv->vval.v_class);
2051 }
2052}
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00002053
2054#endif // FEAT_EVAL