blob: e4130459b25df211ec41445bbddbd18f7bac633f [file] [log] [blame]
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9class.c: Vim9 script class support
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
24/*
Bram Moolenaard505d172022-12-18 21:42:55 +000025 * Parse a member declaration, both object and class member.
26 * Returns OK or FAIL. When OK then "varname_end" is set to just after the
27 * variable name and "type_ret" is set to the decleared or detected type.
28 * "init_expr" is set to the initialisation expression (allocated), if there is
Bram Moolenaar554d0312023-01-05 19:59:18 +000029 * one. For an interface "init_expr" is NULL.
Bram Moolenaard505d172022-12-18 21:42:55 +000030 */
31 static int
32parse_member(
33 exarg_T *eap,
34 char_u *line,
35 char_u *varname,
36 int has_public, // TRUE if "public" seen before "varname"
37 char_u **varname_end,
38 garray_T *type_list,
39 type_T **type_ret,
40 char_u **init_expr)
41{
42 *varname_end = to_name_end(varname, FALSE);
43 if (*varname == '_' && has_public)
44 {
45 semsg(_(e_public_member_name_cannot_start_with_underscore_str), line);
46 return FAIL;
47 }
48
49 char_u *colon = skipwhite(*varname_end);
50 char_u *type_arg = colon;
51 type_T *type = NULL;
52 if (*colon == ':')
53 {
54 if (VIM_ISWHITE(**varname_end))
55 {
56 semsg(_(e_no_white_space_allowed_before_colon_str), varname);
57 return FAIL;
58 }
59 if (!VIM_ISWHITE(colon[1]))
60 {
61 semsg(_(e_white_space_required_after_str_str), ":", varname);
62 return FAIL;
63 }
64 type_arg = skipwhite(colon + 1);
65 type = parse_type(&type_arg, type_list, TRUE);
66 if (type == NULL)
67 return FAIL;
68 }
69
70 char_u *expr_start = skipwhite(type_arg);
71 char_u *expr_end = expr_start;
72 if (type == NULL && *expr_start != '=')
73 {
74 emsg(_(e_type_or_initialization_required));
75 return FAIL;
76 }
77
78 if (*expr_start == '=')
79 {
80 if (!VIM_ISWHITE(expr_start[-1]) || !VIM_ISWHITE(expr_start[1]))
81 {
82 semsg(_(e_white_space_required_before_and_after_str_at_str),
83 "=", type_arg);
84 return FAIL;
85 }
86 expr_start = skipwhite(expr_start + 1);
87
88 expr_end = expr_start;
89 evalarg_T evalarg;
90 fill_evalarg_from_eap(&evalarg, eap, FALSE);
91 skip_expr(&expr_end, NULL);
92
93 if (type == NULL)
94 {
95 // No type specified, use the type of the initializer.
96 typval_T tv;
97 tv.v_type = VAR_UNKNOWN;
98 char_u *expr = expr_start;
99 int res = eval0(expr, &tv, eap, &evalarg);
100
101 if (res == OK)
Bram Moolenaare83c1332023-01-02 21:04:04 +0000102 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000103 type = typval2type(&tv, get_copyID(), type_list,
104 TVTT_DO_MEMBER);
Bram Moolenaare83c1332023-01-02 21:04:04 +0000105 clear_tv(&tv);
106 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000107 if (type == NULL)
108 {
109 semsg(_(e_cannot_get_object_member_type_from_initializer_str),
110 expr_start);
111 clear_evalarg(&evalarg, NULL);
112 return FAIL;
113 }
114 }
115 clear_evalarg(&evalarg, NULL);
116 }
117 if (!valid_declaration_type(type))
118 return FAIL;
119
120 *type_ret = type;
121 if (expr_end > expr_start)
Bram Moolenaar554d0312023-01-05 19:59:18 +0000122 {
123 if (init_expr == NULL)
124 {
125 emsg(_(e_cannot_initialize_member_in_interface));
126 return FAIL;
127 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000128 *init_expr = vim_strnsave(expr_start, expr_end - expr_start);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000129 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000130 return OK;
131}
132
133/*
134 * Add a member to an object or a class.
135 * Returns OK when successful, "init_expr" will be consumed then.
136 * Returns FAIL otherwise, caller might need to free "init_expr".
137 */
138 static int
139add_member(
140 garray_T *gap,
141 char_u *varname,
142 char_u *varname_end,
143 int has_public,
144 type_T *type,
145 char_u *init_expr)
146{
147 if (ga_grow(gap, 1) == FAIL)
148 return FAIL;
149 ocmember_T *m = ((ocmember_T *)gap->ga_data) + gap->ga_len;
150 m->ocm_name = vim_strnsave(varname, varname_end - varname);
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +0000151 m->ocm_access = has_public ? VIM_ACCESS_ALL
152 : *varname == '_' ? VIM_ACCESS_PRIVATE : VIM_ACCESS_READ;
Bram Moolenaard505d172022-12-18 21:42:55 +0000153 m->ocm_type = type;
154 if (init_expr != NULL)
155 m->ocm_init = init_expr;
156 ++gap->ga_len;
157 return OK;
158}
159
160/*
161 * Move the class or object members found while parsing a class into the class.
162 * "gap" contains the found members.
Bram Moolenaar83677162023-01-08 19:54:10 +0000163 * "parent_members" points to the members in the parent class (if any)
164 * "parent_count" is the number of members in the parent class
Bram Moolenaard505d172022-12-18 21:42:55 +0000165 * "members" will be set to the newly allocated array of members and
166 * "member_count" set to the number of members.
167 * Returns OK or FAIL.
168 */
169 static int
170add_members_to_class(
171 garray_T *gap,
Bram Moolenaar83677162023-01-08 19:54:10 +0000172 ocmember_T *parent_members,
173 int parent_count,
Bram Moolenaard505d172022-12-18 21:42:55 +0000174 ocmember_T **members,
175 int *member_count)
176{
Bram Moolenaar83677162023-01-08 19:54:10 +0000177 *member_count = parent_count + gap->ga_len;
178 *members = *member_count == 0 ? NULL
179 : ALLOC_MULT(ocmember_T, *member_count);
180 if (*member_count > 0 && *members == NULL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000181 return FAIL;
Bram Moolenaar83677162023-01-08 19:54:10 +0000182 for (int i = 0; i < parent_count; ++i)
183 {
184 // parent members need to be copied
Bram Moolenaarae3205a2023-01-15 20:49:00 +0000185 ocmember_T *m = *members + i;
186 *m = parent_members[i];
187 m->ocm_name = vim_strsave(m->ocm_name);
188 if (m->ocm_init != NULL)
189 m->ocm_init = vim_strsave(m->ocm_init);
Bram Moolenaar83677162023-01-08 19:54:10 +0000190 }
Bram Moolenaar8efdcee2022-12-19 12:18:09 +0000191 if (gap->ga_len > 0)
Bram Moolenaar83677162023-01-08 19:54:10 +0000192 // new members are moved
193 mch_memmove(*members + parent_count,
194 gap->ga_data, sizeof(ocmember_T) * gap->ga_len);
Bram Moolenaard505d172022-12-18 21:42:55 +0000195 VIM_CLEAR(gap->ga_data);
196 return OK;
197}
198
199/*
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000200 * Convert a member index "idx" of interface "itf" to the member index of class
201 * "cl" implementing that interface.
202 */
203 int
Bram Moolenaard0200c82023-01-28 15:19:40 +0000204object_index_from_itf_index(class_T *itf, int is_method, int idx, class_T *cl)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000205{
Bram Moolenaard0200c82023-01-28 15:19:40 +0000206 if (idx > (is_method ? itf->class_obj_method_count
207 : itf->class_obj_member_count))
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000208 {
209 siemsg("index %d out of range for interface %s", idx, itf->class_name);
210 return 0;
211 }
Yegappan Lakshmanan74cc13c2023-08-13 17:41:26 +0200212
213 // If "cl" is the interface or the class that is extended, then the method
214 // index can be used directly and there is no need to search for the method
215 // index in one of the child classes.
216 if (cl == itf)
217 return idx;
218
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000219 itf2class_T *i2c;
220 for (i2c = itf->class_itf2class; i2c != NULL; i2c = i2c->i2c_next)
Bram Moolenaard0200c82023-01-28 15:19:40 +0000221 if (i2c->i2c_class == cl && i2c->i2c_is_method == is_method)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000222 break;
223 if (i2c == NULL)
224 {
225 siemsg("class %s not found on interface %s",
226 cl->class_name, itf->class_name);
227 return 0;
228 }
229 int *table = (int *)(i2c + 1);
230 return table[idx];
231}
232
233/*
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200234 * Check whether a class named "extends_name" is present. If the class is
235 * valid, then "extends_clp" is set with the class pointer.
236 * Returns TRUE if the class name "extends_names" is a valid class.
237 */
238 static int
239validate_extends_class(char_u *extends_name, class_T **extends_clp)
240{
241 typval_T tv;
242 int success = FALSE;
243
244 tv.v_type = VAR_UNKNOWN;
245 if (eval_variable_import(extends_name, &tv) == FAIL)
246 {
247 semsg(_(e_class_name_not_found_str), extends_name);
248 return success;
249 }
250 else
251 {
252 if (tv.v_type != VAR_CLASS
253 || tv.vval.v_class == NULL
254 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) != 0)
255 semsg(_(e_cannot_extend_str), extends_name);
256 else
257 {
258 class_T *extends_cl = tv.vval.v_class;
259 ++extends_cl->class_refcount;
260 *extends_clp = extends_cl;
261 success = TRUE;
262 }
263 clear_tv(&tv);
264 }
265
266 return success;
267}
268
269/*
270 * Check the members of the interface class "ifcl" match the class members
271 * ("classmembers_gap") and object members ("objmembers_gap") of a class.
272 * Returns TRUE if the class and object member names are valid.
273 */
274 static int
275validate_interface_members(
276 char_u *intf_class_name,
277 class_T *ifcl,
278 garray_T *classmembers_gap,
279 garray_T *objmembers_gap)
280{
281 int success = TRUE;
282
283 for (int loop = 1; loop <= 2 && success; ++loop)
284 {
285 // loop == 1: check class members
286 // loop == 2: check object members
287 int if_count = loop == 1 ? ifcl->class_class_member_count
288 : ifcl->class_obj_member_count;
289 if (if_count == 0)
290 continue;
291 ocmember_T *if_ms = loop == 1 ? ifcl->class_class_members
292 : ifcl->class_obj_members;
293 ocmember_T *cl_ms = (ocmember_T *)(loop == 1
294 ? classmembers_gap->ga_data
295 : objmembers_gap->ga_data);
296 int cl_count = loop == 1 ? classmembers_gap->ga_len
297 : objmembers_gap->ga_len;
298 for (int if_i = 0; if_i < if_count; ++if_i)
299 {
300 int cl_i;
301 for (cl_i = 0; cl_i < cl_count; ++cl_i)
302 {
303 ocmember_T *m = &cl_ms[cl_i];
304 where_T where = WHERE_INIT;
305
306 if (STRCMP(if_ms[if_i].ocm_name, m->ocm_name) != 0)
307 continue;
308
309 // Ensure the type is matching.
310 where.wt_func_name = (char *)m->ocm_name;
311 where.wt_kind = WT_MEMBER;
312 if (check_type_maybe(if_ms[if_i].ocm_type, m->ocm_type, TRUE,
313 where) != OK)
314 success = FALSE;
315
316 break;
317 }
318 if (cl_i == cl_count)
319 {
320 semsg(_(e_member_str_of_interface_str_not_implemented),
321 if_ms[if_i].ocm_name, intf_class_name);
322 success = FALSE;
323 break;
324 }
325 }
326 }
327
328 return success;
329}
330
331/*
332 * Check the functions/methods of the interface class "ifcl" match the class
333 * methods ("classfunctions_gap") and object functions ("objmemthods_gap") of a
334 * class.
335 * Returns TRUE if the class and object member names are valid.
336 */
337 static int
338validate_interface_methods(
339 char_u *intf_class_name,
340 class_T *ifcl,
341 garray_T *classfunctions_gap,
342 garray_T *objmethods_gap)
343{
344 int success = TRUE;
345
346 for (int loop = 1; loop <= 2 && success; ++loop)
347 {
348 // loop == 1: check class functions
349 // loop == 2: check object methods
350 int if_count = loop == 1 ? ifcl->class_class_function_count
351 : ifcl->class_obj_method_count;
352 if (if_count == 0)
353 continue;
354 ufunc_T **if_fp = loop == 1 ? ifcl->class_class_functions
355 : ifcl->class_obj_methods;
356 ufunc_T **cl_fp = (ufunc_T **)(loop == 1
357 ? classfunctions_gap->ga_data
358 : objmethods_gap->ga_data);
359 int cl_count = loop == 1 ? classfunctions_gap->ga_len
360 : objmethods_gap->ga_len;
361 for (int if_i = 0; if_i < if_count; ++if_i)
362 {
363 char_u *if_name = if_fp[if_i]->uf_name;
364 int cl_i;
365 for (cl_i = 0; cl_i < cl_count; ++cl_i)
366 {
367 char_u *cl_name = cl_fp[cl_i]->uf_name;
368 if (STRCMP(if_name, cl_name) == 0)
369 {
370 where_T where = WHERE_INIT;
371
372 // Ensure the type is matching.
373 where.wt_func_name = (char *)if_name;
374 where.wt_kind = WT_METHOD;
375 if (check_type_maybe(if_fp[if_i]->uf_func_type,
376 cl_fp[cl_i]->uf_func_type, TRUE, where) != OK)
377 success = FALSE;
378 break;
379 }
380 }
381 if (cl_i == cl_count)
382 {
383 semsg(_(e_function_str_of_interface_str_not_implemented),
384 if_name, intf_class_name);
385 success = FALSE;
386 break;
387 }
388 }
389 }
390
391 return success;
392}
393
394/*
395 * Validate all the "implements" classes when creating a new class. The
396 * classes are returned in "intf_classes". The class functions, class methods,
397 * object methods and object members in the new class are in
398 * "classfunctions_gap", "classmembers_gap", "objmethods_gap", and
399 * "objmembers_gap" respectively.
400 */
401 static int
402validate_implements_classes(
403 garray_T *impl_gap,
404 class_T **intf_classes,
405 garray_T *classfunctions_gap,
406 garray_T *classmembers_gap,
407 garray_T *objmethods_gap,
408 garray_T *objmembers_gap)
409{
410 int success = TRUE;
411
412 for (int i = 0; i < impl_gap->ga_len && success; ++i)
413 {
414 char_u *impl = ((char_u **)impl_gap->ga_data)[i];
415 typval_T tv;
416 tv.v_type = VAR_UNKNOWN;
417 if (eval_variable_import(impl, &tv) == FAIL)
418 {
419 semsg(_(e_interface_name_not_found_str), impl);
420 success = FALSE;
421 break;
422 }
423
424 if (tv.v_type != VAR_CLASS
425 || tv.vval.v_class == NULL
426 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0)
427 {
428 semsg(_(e_not_valid_interface_str), impl);
429 success = FALSE;
430 clear_tv(&tv);
431 break;
432 }
433
434 class_T *ifcl = tv.vval.v_class;
435 intf_classes[i] = ifcl;
436 ++ifcl->class_refcount;
437
438 // check the members of the interface match the members of the class
439 success = validate_interface_members(impl, ifcl, classmembers_gap,
440 objmembers_gap);
441
442 // check the functions/methods of the interface match the
443 // functions/methods of the class
444 success = validate_interface_methods(impl, ifcl, classfunctions_gap,
445 objmethods_gap);
446 clear_tv(&tv);
447 }
448
449 return success;
450}
451
452/*
453 * Check no function argument name is used as a class member.
454 * (Object members are always accessed with "this." prefix, so no need
455 * to check them.)
456 */
457 static int
458check_func_arg_names(
459 garray_T *classfunctions_gap,
460 garray_T *objmethods_gap,
461 garray_T *classmembers_gap)
462{
463 int success = TRUE;
464
465 // loop 1: class functions, loop 2: object methods
466 for (int loop = 1; loop <= 2 && success; ++loop)
467 {
468 garray_T *gap = loop == 1 ? classfunctions_gap : objmethods_gap;
469
470 for (int fi = 0; fi < gap->ga_len && success; ++fi)
471 {
472 ufunc_T *uf = ((ufunc_T **)gap->ga_data)[fi];
473
474 for (int i = 0; i < uf->uf_args.ga_len && success; ++i)
475 {
476 char_u *aname = ((char_u **)uf->uf_args.ga_data)[i];
477 garray_T *mgap = classmembers_gap;
478
479 // Check all the class member names
480 for (int mi = 0; mi < mgap->ga_len; ++mi)
481 {
482 char_u *mname = ((ocmember_T *)mgap->ga_data + mi)
483 ->ocm_name;
484 if (STRCMP(aname, mname) == 0)
485 {
486 success = FALSE;
487
488 if (uf->uf_script_ctx.sc_sid > 0)
489 SOURCING_LNUM = uf->uf_script_ctx.sc_lnum;
490
491 semsg(_(e_argument_already_declared_in_class_str),
492 aname);
493 break;
494 }
495 }
496 }
497 }
498 }
499
500 return success;
501}
502
503/*
Yegappan Lakshmananb1027282023-08-19 11:26:42 +0200504 * Update the interface class lookup table for the member index on the
505 * interface to the member index in the class implementing the interface.
506 * And a lookup table for the object method index on the interface
507 * to the object method index in the class implementing the interface.
508 * This is also used for updating the lookup table for the extended class
509 * hierarchy.
510 */
511 static int
512update_member_method_lookup_table(
513 class_T *ifcl,
514 class_T *cl,
515 garray_T *objmethods,
516 int pobj_method_offset,
517 int is_interface)
518{
519 if (ifcl == NULL)
520 return OK;
521
522 // Table for members.
523 itf2class_T *if2cl = alloc_clear(sizeof(itf2class_T)
524 + ifcl->class_obj_member_count * sizeof(int));
525 if (if2cl == NULL)
526 return FAIL;
527 if2cl->i2c_next = ifcl->class_itf2class;
528 ifcl->class_itf2class = if2cl;
529 if2cl->i2c_class = cl;
530 if2cl->i2c_is_method = FALSE;
531
532 for (int if_i = 0; if_i < ifcl->class_obj_member_count; ++if_i)
533 for (int cl_i = 0; cl_i < cl->class_obj_member_count; ++cl_i)
534 {
535 if (STRCMP(ifcl->class_obj_members[if_i].ocm_name,
536 cl->class_obj_members[cl_i].ocm_name) == 0)
537 {
538 int *table = (int *)(if2cl + 1);
539 table[if_i] = cl_i;
540 break;
541 }
542 }
543
544 // Table for methods.
545 if2cl = alloc_clear(sizeof(itf2class_T)
546 + ifcl->class_obj_method_count * sizeof(int));
547 if (if2cl == NULL)
548 return FAIL;
549 if2cl->i2c_next = ifcl->class_itf2class;
550 ifcl->class_itf2class = if2cl;
551 if2cl->i2c_class = cl;
552 if2cl->i2c_is_method = TRUE;
553
554 for (int if_i = 0; if_i < ifcl->class_obj_method_count; ++if_i)
555 {
556 int done = FALSE;
557 for (int cl_i = 0; cl_i < objmethods->ga_len; ++cl_i)
558 {
559 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
560 ((ufunc_T **)objmethods->ga_data)[cl_i]->uf_name)
561 == 0)
562 {
563 int *table = (int *)(if2cl + 1);
564 table[if_i] = cl_i;
565 done = TRUE;
566 break;
567 }
568 }
569
570 // extended class object method is not overridden by the child class.
571 // Keep the method declared in one of the parent classes in the
572 // lineage.
573 if (!done && !is_interface)
574 {
575 // If "ifcl" is not the immediate parent of "cl", then search in
576 // the intermediate parent classes.
577 if (cl->class_extends != ifcl)
578 {
579 class_T *parent = cl->class_extends;
580 int method_offset = objmethods->ga_len;
581
582 while (!done && parent != NULL && parent != ifcl)
583 {
584
585 for (int cl_i = 0;
586 cl_i < parent->class_obj_method_count_child; ++cl_i)
587 {
588 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
589 parent->class_obj_methods[cl_i]->uf_name)
590 == 0)
591 {
592 int *table = (int *)(if2cl + 1);
593 table[if_i] = method_offset + cl_i;
594 done = TRUE;
595 break;
596 }
597 }
598 method_offset += parent->class_obj_method_count_child;
599 parent = parent->class_extends;
600 }
601 }
602
603 if (!done)
604 {
605 int *table = (int *)(if2cl + 1);
606 table[if_i] = pobj_method_offset + if_i;
607 }
608 }
609 }
610
611 return OK;
612}
613
614/*
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200615 * Update the member and object method lookup tables for a new class in the
616 * interface class.
617 * For each interface add a lookup table for the member index on the interface
618 * to the member index in the new class. And a lookup table for the object
619 * method index on the interface to the object method index in the new class.
620 */
621 static int
622add_lookup_tables(class_T *cl, class_T *extends_cl, garray_T *objmethods_gap)
623{
624 for (int i = 0; i < cl->class_interface_count; ++i)
625 {
626 class_T *ifcl = cl->class_interfaces_cl[i];
627
628 if (update_member_method_lookup_table(ifcl, cl, objmethods_gap,
629 0, TRUE) == FAIL)
630 return FAIL;
631 }
632
633 // Update the lookup table for the extended class, if nay
634 if (extends_cl != NULL)
635 {
636 class_T *pclass = extends_cl;
637 int pobj_method_offset = objmethods_gap->ga_len;
638
639 // Update the entire lineage of extended classes.
640 while (pclass != NULL)
641 {
642 if (update_member_method_lookup_table(pclass, cl,
643 objmethods_gap, pobj_method_offset, FALSE) == FAIL)
644 return FAIL;
645
646 pobj_method_offset += pclass->class_obj_method_count_child;
647 pclass = pclass->class_extends;
648 }
649 }
650
651 return OK;
652}
653
654/*
655 * Add class members to a new class. Allocate a typval for each class member
656 * and initialize it.
657 */
658 static void
659add_class_members(class_T *cl, exarg_T *eap)
660{
661 // Allocate a typval for each class member and initialize it.
662 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
663 cl->class_class_member_count);
664 if (cl->class_members_tv == NULL)
665 return;
666
667 for (int i = 0; i < cl->class_class_member_count; ++i)
668 {
669 ocmember_T *m = &cl->class_class_members[i];
670 typval_T *tv = &cl->class_members_tv[i];
671 if (m->ocm_init != NULL)
672 {
673 typval_T *etv = eval_expr(m->ocm_init, eap);
674 if (etv != NULL)
675 {
676 *tv = *etv;
677 vim_free(etv);
678 }
679 }
680 else
681 {
682 // TODO: proper default value
683 tv->v_type = m->ocm_type->tt_type;
684 tv->vval.v_string = NULL;
685 }
686 }
687}
688
689/*
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +0200690 * Add a default constructor method (new()) to the class "cl".
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200691 */
692 static void
693add_default_constructor(
694 class_T *cl,
695 garray_T *classfunctions_gap,
696 garray_T *type_list_gap)
697{
698 garray_T fga;
699
700 ga_init2(&fga, 1, 1000);
701 ga_concat(&fga, (char_u *)"new(");
702 for (int i = 0; i < cl->class_obj_member_count; ++i)
703 {
704 if (i > 0)
705 ga_concat(&fga, (char_u *)", ");
706 ga_concat(&fga, (char_u *)"this.");
707 ocmember_T *m = cl->class_obj_members + i;
708 ga_concat(&fga, (char_u *)m->ocm_name);
709 ga_concat(&fga, (char_u *)" = v:none");
710 }
711 ga_concat(&fga, (char_u *)")\nenddef\n");
712 ga_append(&fga, NUL);
713
714 exarg_T fea;
715 CLEAR_FIELD(fea);
716 fea.cmdidx = CMD_def;
717 fea.cmd = fea.arg = fga.ga_data;
718
719 garray_T lines_to_free;
720 ga_init2(&lines_to_free, sizeof(char_u *), 50);
721
722 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS);
723
724 ga_clear_strings(&lines_to_free);
725 vim_free(fga.ga_data);
726
727 if (nf != NULL && ga_grow(classfunctions_gap, 1) == OK)
728 {
729 ((ufunc_T **)classfunctions_gap->ga_data)[classfunctions_gap->ga_len]
730 = nf;
731 ++classfunctions_gap->ga_len;
732
733 nf->uf_flags |= FC_NEW;
734 nf->uf_ret_type = get_type_ptr(type_list_gap);
735 if (nf->uf_ret_type != NULL)
736 {
737 nf->uf_ret_type->tt_type = VAR_OBJECT;
738 nf->uf_ret_type->tt_class = cl;
739 nf->uf_ret_type->tt_argcount = 0;
740 nf->uf_ret_type->tt_args = NULL;
741 }
742 }
743}
744
745/*
746 * Add the class functions and object methods to the new class "cl".
747 * When extending a class, add the functions and methods from the parent class
748 * also.
749 */
750 static int
751add_classfuncs_objmethods(
752 class_T *cl,
753 class_T *extends_cl,
754 garray_T *classfunctions_gap,
755 garray_T *objmethods_gap)
756{
757 // loop 1: class functions, loop 2: object methods
758 for (int loop = 1; loop <= 2; ++loop)
759 {
760 garray_T *gap = loop == 1 ? classfunctions_gap : objmethods_gap;
761 int *fcount = loop == 1 ? &cl->class_class_function_count
762 : &cl->class_obj_method_count;
763 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
764 : &cl->class_obj_methods;
765
766 int parent_count = 0;
767 if (extends_cl != NULL)
768 // Include functions from the parent.
769 parent_count = loop == 1
770 ? extends_cl->class_class_function_count
771 : extends_cl->class_obj_method_count;
772
773 *fcount = parent_count + gap->ga_len;
774 if (*fcount == 0)
775 {
776 *fup = NULL;
777 continue;
778 }
779 *fup = ALLOC_MULT(ufunc_T *, *fcount);
780 if (*fup == NULL)
781 return FAIL;
782
783 if (gap->ga_len != 0)
784 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
785 vim_free(gap->ga_data);
786 if (loop == 1)
787 cl->class_class_function_count_child = gap->ga_len;
788 else
789 cl->class_obj_method_count_child = gap->ga_len;
790
791 int skipped = 0;
792 for (int i = 0; i < parent_count; ++i)
793 {
794 // Copy functions from the parent. Can't use the same
795 // function, because "uf_class" is different and compilation
796 // will have a different result.
797 // Put them after the functions in the current class, object
798 // methods may be overruled, then "super.Method()" is used to
799 // find a method from the parent.
800 // Skip "new" functions. TODO: not all of them.
801 if (loop == 1 && STRNCMP(
802 extends_cl->class_class_functions[i]->uf_name,
803 "new", 3) == 0)
804 ++skipped;
805 else
806 {
807 ufunc_T *pf = (loop == 1
808 ? extends_cl->class_class_functions
809 : extends_cl->class_obj_methods)[i];
810 (*fup)[gap->ga_len + i - skipped] = copy_function(pf);
811
812 // If the child class overrides a function from the parent
813 // the signature must be equal.
814 char_u *pname = pf->uf_name;
815 for (int ci = 0; ci < gap->ga_len; ++ci)
816 {
817 ufunc_T *cf = (*fup)[ci];
818 char_u *cname = cf->uf_name;
819 if (STRCMP(pname, cname) == 0)
820 {
821 where_T where = WHERE_INIT;
822 where.wt_func_name = (char *)pname;
823 where.wt_kind = WT_METHOD;
824 (void)check_type(pf->uf_func_type, cf->uf_func_type,
825 TRUE, where);
826 }
827 }
828 }
829 }
830
831 *fcount -= skipped;
832
833 // Set the class pointer on all the functions and object methods.
834 for (int i = 0; i < *fcount; ++i)
835 {
836 ufunc_T *fp = (*fup)[i];
837 fp->uf_class = cl;
838 if (loop == 2)
839 fp->uf_flags |= FC_OBJECT;
840 }
841 }
842
843 return OK;
844}
845
846/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000847 * Handle ":class" and ":abstract class" up to ":endclass".
Bram Moolenaar554d0312023-01-05 19:59:18 +0000848 * Handle ":interface" up to ":endinterface".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000849 */
850 void
851ex_class(exarg_T *eap)
852{
Bram Moolenaar83ae6152023-02-25 19:59:31 +0000853 int is_class = eap->cmdidx == CMD_class; // FALSE for :interface
854 long start_lnum = SOURCING_LNUM;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000855
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000856 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000857 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000858 if (is_abstract)
859 {
860 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
861 {
862 semsg(_(e_invalid_argument_str), arg);
863 return;
864 }
865 arg = skipwhite(arg + 5);
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000866 is_class = TRUE;
867 }
868
869 if (!current_script_is_vim9()
870 || (cmdmod.cmod_flags & CMOD_LEGACY)
871 || !getline_equal(eap->getline, eap->cookie, getsourceline))
872 {
873 if (is_class)
874 emsg(_(e_class_can_only_be_defined_in_vim9_script));
875 else
876 emsg(_(e_interface_can_only_be_defined_in_vim9_script));
877 return;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000878 }
879
880 if (!ASCII_ISUPPER(*arg))
881 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000882 if (is_class)
883 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
884 else
885 semsg(_(e_interface_name_must_start_with_uppercase_letter_str),
886 arg);
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000887 return;
888 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000889 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
890 if (!IS_WHITE_OR_NUL(*name_end))
891 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000892 semsg(_(e_white_space_required_after_name_str), arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000893 return;
894 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000895 char_u *name_start = arg;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000896
Bram Moolenaara86655a2023-01-12 17:06:27 +0000897 // "export class" gets used when creating the class, don't use "is_export"
898 // for the items inside the class.
899 int class_export = is_export;
900 is_export = FALSE;
901
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000902 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000903 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000904
Bram Moolenaar83677162023-01-08 19:54:10 +0000905 // Name for "extends BaseClass"
906 char_u *extends = NULL;
907
Bram Moolenaar94674f22023-01-06 18:42:20 +0000908 // Names for "implements SomeInterface"
909 garray_T ga_impl;
910 ga_init2(&ga_impl, sizeof(char_u *), 5);
911
912 arg = skipwhite(name_end);
913 while (*arg != NUL && *arg != '#' && *arg != '\n')
914 {
915 // TODO:
Bram Moolenaar94674f22023-01-06 18:42:20 +0000916 // specifies SomeInterface
Bram Moolenaar83677162023-01-08 19:54:10 +0000917 if (STRNCMP(arg, "extends", 7) == 0 && IS_WHITE_OR_NUL(arg[7]))
918 {
919 if (extends != NULL)
920 {
921 emsg(_(e_duplicate_extends));
922 goto early_ret;
923 }
924 arg = skipwhite(arg + 7);
925 char_u *end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
926 if (!IS_WHITE_OR_NUL(*end))
927 {
928 semsg(_(e_white_space_required_after_name_str), arg);
929 goto early_ret;
930 }
931 extends = vim_strnsave(arg, end - arg);
932 if (extends == NULL)
933 goto early_ret;
934
935 arg = skipwhite(end + 1);
936 }
937 else if (STRNCMP(arg, "implements", 10) == 0
938 && IS_WHITE_OR_NUL(arg[10]))
Bram Moolenaar94674f22023-01-06 18:42:20 +0000939 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000940 if (ga_impl.ga_len > 0)
941 {
942 emsg(_(e_duplicate_implements));
943 goto early_ret;
944 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000945 arg = skipwhite(arg + 10);
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000946
947 for (;;)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000948 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000949 char_u *impl_end = find_name_end(arg, NULL, NULL,
950 FNE_CHECK_START);
951 if (!IS_WHITE_OR_NUL(*impl_end) && *impl_end != ',')
952 {
953 semsg(_(e_white_space_required_after_name_str), arg);
954 goto early_ret;
955 }
956 char_u *iname = vim_strnsave(arg, impl_end - arg);
957 if (iname == NULL)
958 goto early_ret;
959 for (int i = 0; i < ga_impl.ga_len; ++i)
960 if (STRCMP(((char_u **)ga_impl.ga_data)[i], iname) == 0)
961 {
962 semsg(_(e_duplicate_interface_after_implements_str),
963 iname);
964 vim_free(iname);
965 goto early_ret;
966 }
967 if (ga_add_string(&ga_impl, iname) == FAIL)
968 {
969 vim_free(iname);
970 goto early_ret;
971 }
972 if (*impl_end != ',')
973 {
974 arg = skipwhite(impl_end);
975 break;
976 }
977 arg = skipwhite(impl_end + 1);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000978 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000979 }
980 else
981 {
982 semsg(_(e_trailing_characters_str), arg);
983early_ret:
Bram Moolenaar83677162023-01-08 19:54:10 +0000984 vim_free(extends);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000985 ga_clear_strings(&ga_impl);
986 return;
987 }
988 }
989
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000990 garray_T type_list; // list of pointers to allocated types
991 ga_init2(&type_list, sizeof(type_T *), 10);
992
Bram Moolenaard505d172022-12-18 21:42:55 +0000993 // Growarray with class members declared in the class.
994 garray_T classmembers;
995 ga_init2(&classmembers, sizeof(ocmember_T), 10);
996
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000997 // Growarray with functions declared in the class.
998 garray_T classfunctions;
999 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +00001000
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001001 // Growarray with object members declared in the class.
1002 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +00001003 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001004
1005 // Growarray with object methods declared in the class.
1006 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001007 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001008
1009 /*
Bram Moolenaar554d0312023-01-05 19:59:18 +00001010 * Go over the body of the class/interface until "endclass" or
1011 * "endinterface" is found.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001012 */
1013 char_u *theline = NULL;
1014 int success = FALSE;
1015 for (;;)
1016 {
1017 vim_free(theline);
1018 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
1019 if (theline == NULL)
1020 break;
1021 char_u *line = skipwhite(theline);
1022
Bram Moolenaar418b5472022-12-20 13:38:22 +00001023 // Skip empty and comment lines.
1024 if (*line == NUL)
1025 continue;
1026 if (*line == '#')
1027 {
1028 if (vim9_bad_comment(line))
1029 break;
1030 continue;
1031 }
1032
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001033 char_u *p = line;
Bram Moolenaar554d0312023-01-05 19:59:18 +00001034 char *end_name = is_class ? "endclass" : "endinterface";
1035 if (checkforcmd(&p, end_name, is_class ? 4 : 5))
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001036 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001037 if (STRNCMP(line, end_name, is_class ? 8 : 12) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001038 semsg(_(e_command_cannot_be_shortened_str), line);
1039 else if (*p == '|' || !ends_excmd2(line, p))
1040 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +00001041 else
1042 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001043 break;
1044 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00001045 char *wrong_name = is_class ? "endinterface" : "endclass";
1046 if (checkforcmd(&p, wrong_name, is_class ? 5 : 4))
1047 {
Bram Moolenaar657aea72023-01-27 13:16:19 +00001048 semsg(_(e_invalid_command_str_expected_str), line, end_name);
Bram Moolenaar554d0312023-01-05 19:59:18 +00001049 break;
1050 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001051
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001052 int has_public = FALSE;
1053 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001054 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001055 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001056 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001057 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001058 break;
1059 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001060 has_public = TRUE;
1061 p = skipwhite(line + 6);
1062
Bram Moolenaard505d172022-12-18 21:42:55 +00001063 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001064 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001065 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001066 break;
1067 }
1068 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001069
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001070 int has_static = FALSE;
1071 char_u *ps = p;
1072 if (checkforcmd(&p, "static", 4))
1073 {
1074 if (STRNCMP(ps, "static", 6) != 0)
1075 {
1076 semsg(_(e_command_cannot_be_shortened_str), ps);
1077 break;
1078 }
1079 has_static = TRUE;
1080 p = skipwhite(ps + 6);
1081 }
1082
Bram Moolenaard505d172022-12-18 21:42:55 +00001083 // object members (public, read access, private):
1084 // "this._varname"
1085 // "this.varname"
1086 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001087 if (STRNCMP(p, "this", 4) == 0)
1088 {
1089 if (p[4] != '.' || !eval_isnamec1(p[5]))
1090 {
1091 semsg(_(e_invalid_object_member_declaration_str), p);
1092 break;
1093 }
1094 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +00001095 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +00001096 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +00001097 char_u *init_expr = NULL;
1098 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +00001099 &varname_end, &type_list, &type,
1100 is_class ? &init_expr: NULL) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +00001101 break;
1102 if (add_member(&objmembers, varname, varname_end,
1103 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +00001104 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001105 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001106 break;
1107 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001108 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001109
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001110 // constructors:
1111 // def new()
1112 // enddef
1113 // def newOther()
1114 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001115 // object methods and class functions:
1116 // def SomeMethod()
1117 // enddef
1118 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001119 // enddef
1120 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001121 // def <Tval> someMethod()
1122 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001123 else if (checkforcmd(&p, "def", 3))
1124 {
1125 exarg_T ea;
1126 garray_T lines_to_free;
1127
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001128 // TODO: error for "public static def Func()"?
1129
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001130 CLEAR_FIELD(ea);
1131 ea.cmd = line;
1132 ea.arg = p;
1133 ea.cmdidx = CMD_def;
1134 ea.getline = eap->getline;
1135 ea.cookie = eap->cookie;
1136
1137 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +00001138 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free,
1139 is_class ? CF_CLASS : CF_INTERFACE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001140 ga_clear_strings(&lines_to_free);
1141
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001142 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001143 {
Bram Moolenaar58b40092023-01-11 15:59:05 +00001144 char_u *name = uf->uf_name;
1145 int is_new = STRNCMP(name, "new", 3) == 0;
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001146 if (is_new && is_abstract)
1147 {
1148 emsg(_(e_cannot_define_new_function_in_abstract_class));
1149 success = FALSE;
Yegappan Lakshmananb1027282023-08-19 11:26:42 +02001150 func_clear_free(uf, FALSE);
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001151 break;
1152 }
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001153 if (is_new)
1154 {
1155 // A return type should not be specified for the new()
1156 // constructor method.
1157 if (uf->uf_ret_type->tt_type != VAR_VOID)
1158 {
1159 emsg(_(e_cannot_use_a_return_type_with_new));
1160 success = FALSE;
1161 func_clear_free(uf, FALSE);
1162 break;
1163 }
1164 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001165 garray_T *fgap = has_static || is_new
1166 ? &classfunctions : &objmethods;
Bram Moolenaar58b40092023-01-11 15:59:05 +00001167 // Check the name isn't used already.
1168 for (int i = 0; i < fgap->ga_len; ++i)
1169 {
1170 char_u *n = ((ufunc_T **)fgap->ga_data)[i]->uf_name;
1171 if (STRCMP(name, n) == 0)
1172 {
1173 semsg(_(e_duplicate_function_str), name);
1174 break;
1175 }
1176 }
1177
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001178 if (ga_grow(fgap, 1) == OK)
1179 {
1180 if (is_new)
1181 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001182
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001183 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
1184 ++fgap->ga_len;
1185 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001186 }
1187 }
1188
1189 // class members
1190 else if (has_static)
1191 {
1192 // class members (public, read access, private):
1193 // "static _varname"
1194 // "static varname"
1195 // "public static varname"
1196 char_u *varname = p;
1197 char_u *varname_end = NULL;
1198 type_T *type = NULL;
1199 char_u *init_expr = NULL;
1200 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +00001201 &varname_end, &type_list, &type,
1202 is_class ? &init_expr : NULL) == FAIL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001203 break;
1204 if (add_member(&classmembers, varname, varname_end,
1205 has_public, type, init_expr) == FAIL)
1206 {
1207 vim_free(init_expr);
1208 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001209 }
1210 }
1211
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001212 else
1213 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001214 if (is_class)
1215 semsg(_(e_not_valid_command_in_class_str), line);
1216 else
1217 semsg(_(e_not_valid_command_in_interface_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001218 break;
1219 }
1220 }
1221 vim_free(theline);
1222
Bram Moolenaar83677162023-01-08 19:54:10 +00001223 class_T *extends_cl = NULL; // class from "extends" argument
1224
1225 /*
1226 * Check a few things before defining the class.
1227 */
1228
1229 // Check the "extends" class is valid.
1230 if (success && extends != NULL)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001231 success = validate_extends_class(extends, &extends_cl);
Bram Moolenaar83677162023-01-08 19:54:10 +00001232 VIM_CLEAR(extends);
1233
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001234 class_T **intf_classes = NULL;
1235
Bram Moolenaar83677162023-01-08 19:54:10 +00001236 // Check all "implements" entries are valid.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001237 if (success && ga_impl.ga_len > 0)
1238 {
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001239 intf_classes = ALLOC_CLEAR_MULT(class_T *, ga_impl.ga_len);
1240
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001241 success = validate_implements_classes(&ga_impl, intf_classes,
1242 &classfunctions, &classmembers,
1243 &objmethods, &objmembers);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001244 }
1245
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001246 // Check no function argument name is used as a class member.
Bram Moolenaard40f00c2023-01-13 17:36:49 +00001247 if (success)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001248 success = check_func_arg_names(&classfunctions, &objmethods,
1249 &classmembers);
Bram Moolenaard40f00c2023-01-13 17:36:49 +00001250
Bram Moolenaareb533502022-12-14 15:06:11 +00001251 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001252 if (success)
1253 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001254 // "endclass" encountered without failures: Create the class.
1255
Bram Moolenaareb533502022-12-14 15:06:11 +00001256 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001257 if (cl == NULL)
1258 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +00001259 if (!is_class)
1260 cl->class_flags = CLASS_INTERFACE;
1261
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001262 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +00001263 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +00001264 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001265 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +00001266
Bram Moolenaard0200c82023-01-28 15:19:40 +00001267 if (extends_cl != NULL)
1268 {
1269 cl->class_extends = extends_cl;
1270 extends_cl->class_flags |= CLASS_EXTENDED;
1271 }
Bram Moolenaar83677162023-01-08 19:54:10 +00001272
Bram Moolenaard505d172022-12-18 21:42:55 +00001273 // Add class and object members to "cl".
1274 if (add_members_to_class(&classmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +00001275 extends_cl == NULL ? NULL
1276 : extends_cl->class_class_members,
1277 extends_cl == NULL ? 0
1278 : extends_cl->class_class_member_count,
1279 &cl->class_class_members,
1280 &cl->class_class_member_count) == FAIL
Bram Moolenaard505d172022-12-18 21:42:55 +00001281 || add_members_to_class(&objmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +00001282 extends_cl == NULL ? NULL
1283 : extends_cl->class_obj_members,
1284 extends_cl == NULL ? 0
1285 : extends_cl->class_obj_member_count,
1286 &cl->class_obj_members,
1287 &cl->class_obj_member_count) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +00001288 goto cleanup;
1289
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001290 if (ga_impl.ga_len > 0)
1291 {
1292 // Move the "implements" names into the class.
1293 cl->class_interface_count = ga_impl.ga_len;
1294 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
1295 if (cl->class_interfaces == NULL)
1296 goto cleanup;
1297 for (int i = 0; i < ga_impl.ga_len; ++i)
1298 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
1299 VIM_CLEAR(ga_impl.ga_data);
1300 ga_impl.ga_len = 0;
1301
Bram Moolenaard0200c82023-01-28 15:19:40 +00001302 cl->class_interfaces_cl = intf_classes;
1303 intf_classes = NULL;
1304 }
1305
1306 if (cl->class_interface_count > 0 || extends_cl != NULL)
1307 {
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001308 // Add a method and member lookup table to each of the interface
1309 // classes.
1310 if (add_lookup_tables(cl, extends_cl, &objmethods) == FAIL)
1311 goto cleanup;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001312 }
1313
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001314 // Allocate a typval for each class member and initialize it.
Bram Moolenaar554d0312023-01-05 19:59:18 +00001315 if (is_class && cl->class_class_member_count > 0)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001316 add_class_members(cl, eap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001317
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001318 int have_new = FALSE;
1319 ufunc_T *class_func = NULL;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001320 for (int i = 0; i < classfunctions.ga_len; ++i)
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001321 {
1322 class_func = ((ufunc_T **)classfunctions.ga_data)[i];
1323 if (STRCMP(class_func->uf_name, "new") == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001324 {
1325 have_new = TRUE;
1326 break;
1327 }
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001328 }
1329
1330 if (have_new)
1331 // The return type of new() is an object of class "cl"
1332 class_func->uf_ret_type->tt_class = cl;
1333 else if (is_class && !is_abstract && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001334 // No new() method was defined, add the default constructor.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001335 add_default_constructor(cl, &classfunctions, &type_list);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001336
Bram Moolenaar58b40092023-01-11 15:59:05 +00001337 // Move all the functions into the created class.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001338 if (add_classfuncs_objmethods(cl, extends_cl, &classfunctions,
1339 &objmethods) == FAIL)
1340 goto cleanup;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001341
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001342 cl->class_type.tt_type = VAR_CLASS;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001343 cl->class_type.tt_class = cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001344 cl->class_object_type.tt_type = VAR_OBJECT;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001345 cl->class_object_type.tt_class = cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001346 cl->class_type_list = type_list;
1347
1348 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +00001349 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001350
1351 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001352 // TODO: handle other context, e.g. in a function
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001353 typval_T tv;
1354 tv.v_type = VAR_CLASS;
1355 tv.vval.v_class = cl;
Bram Moolenaara86655a2023-01-12 17:06:27 +00001356 is_export = class_export;
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001357 SOURCING_LNUM = start_lnum;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001358 set_var_const(cl->class_name, current_sctx.sc_sid,
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001359 NULL, &tv, FALSE, 0, 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001360 return;
1361 }
1362
1363cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +00001364 if (cl != NULL)
1365 {
1366 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001367 vim_free(cl->class_class_functions);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001368 if (cl->class_interfaces != NULL)
1369 {
1370 for (int i = 0; i < cl->class_interface_count; ++i)
1371 vim_free(cl->class_interfaces[i]);
1372 vim_free(cl->class_interfaces);
1373 }
1374 if (cl->class_interfaces_cl != NULL)
1375 {
1376 for (int i = 0; i < cl->class_interface_count; ++i)
1377 class_unref(cl->class_interfaces_cl[i]);
1378 vim_free(cl->class_interfaces_cl);
1379 }
Bram Moolenaareb533502022-12-14 15:06:11 +00001380 vim_free(cl->class_obj_members);
1381 vim_free(cl->class_obj_methods);
1382 vim_free(cl);
1383 }
1384
Bram Moolenaar83677162023-01-08 19:54:10 +00001385 vim_free(extends);
1386 class_unref(extends_cl);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001387
1388 if (intf_classes != NULL)
1389 {
1390 for (int i = 0; i < ga_impl.ga_len; ++i)
1391 class_unref(intf_classes[i]);
1392 vim_free(intf_classes);
1393 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001394 ga_clear_strings(&ga_impl);
1395
Bram Moolenaard505d172022-12-18 21:42:55 +00001396 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001397 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001398 garray_T *gap = round == 1 ? &classmembers : &objmembers;
1399 if (gap->ga_len == 0 || gap->ga_data == NULL)
1400 continue;
1401
1402 for (int i = 0; i < gap->ga_len; ++i)
1403 {
1404 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
1405 vim_free(m->ocm_name);
1406 vim_free(m->ocm_init);
1407 }
1408 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001409 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001410
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001411 for (int i = 0; i < objmethods.ga_len; ++i)
1412 {
1413 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
1414 func_clear_free(uf, FALSE);
1415 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001416 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001417
1418 for (int i = 0; i < classfunctions.ga_len; ++i)
1419 {
1420 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
1421 func_clear_free(uf, FALSE);
1422 }
1423 ga_clear(&classfunctions);
1424
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001425 clear_type_list(&type_list);
1426}
1427
1428/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001429 * Find member "name" in class "cl", set "member_idx" to the member index and
1430 * return its type.
1431 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001432 */
1433 type_T *
1434class_member_type(
1435 class_T *cl,
1436 char_u *name,
1437 char_u *name_end,
1438 int *member_idx)
1439{
1440 *member_idx = -1; // not found (yet)
1441 size_t len = name_end - name;
1442
1443 for (int i = 0; i < cl->class_obj_member_count; ++i)
1444 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001445 ocmember_T *m = cl->class_obj_members + i;
1446 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001447 {
1448 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001449 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001450 }
1451 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001452
1453 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001454 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001455}
1456
1457/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001458 * Handle ":enum" up to ":endenum".
1459 */
1460 void
1461ex_enum(exarg_T *eap UNUSED)
1462{
1463 // TODO
1464}
1465
1466/*
1467 * Handle ":type".
1468 */
1469 void
1470ex_type(exarg_T *eap UNUSED)
1471{
1472 // TODO
1473}
1474
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001475/*
1476 * Evaluate what comes after a class:
1477 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001478 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001479 * - class constructor: SomeClass.new()
1480 * - object member: someObject.varname
1481 * - object method: someObject.SomeMethod()
1482 *
1483 * "*arg" points to the '.'.
1484 * "*arg" is advanced to after the member name or method call.
1485 *
1486 * Returns FAIL or OK.
1487 */
1488 int
1489class_object_index(
1490 char_u **arg,
1491 typval_T *rettv,
1492 evalarg_T *evalarg,
1493 int verbose UNUSED) // give error messages
1494{
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001495 if (VIM_ISWHITE((*arg)[1]))
1496 {
1497 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
1498 return FAIL;
1499 }
1500
1501 ++*arg;
1502 char_u *name = *arg;
1503 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1504 if (name_end == name)
1505 return FAIL;
1506 size_t len = name_end - name;
1507
Bram Moolenaar552bdca2023-02-17 21:08:50 +00001508 class_T *cl;
1509 if (rettv->v_type == VAR_CLASS)
1510 cl = rettv->vval.v_class;
1511 else // VAR_OBJECT
1512 {
1513 if (rettv->vval.v_object == NULL)
1514 {
1515 emsg(_(e_using_null_object));
1516 return FAIL;
1517 }
1518 cl = rettv->vval.v_object->obj_class;
1519 }
1520
Bram Moolenaard13dd302023-03-11 20:56:35 +00001521 if (cl == NULL)
1522 {
1523 emsg(_(e_incomplete_type));
1524 return FAIL;
1525 }
1526
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001527 if (*name_end == '(')
1528 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001529 int on_class = rettv->v_type == VAR_CLASS;
1530 int count = on_class ? cl->class_class_function_count
1531 : cl->class_obj_method_count;
1532 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001533 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001534 ufunc_T *fp = on_class ? cl->class_class_functions[i]
1535 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +00001536 // Use a separate pointer to avoid that ASAN complains about
1537 // uf_name[] only being 4 characters.
1538 char_u *ufname = (char_u *)fp->uf_name;
1539 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001540 {
1541 typval_T argvars[MAX_FUNC_ARGS + 1];
1542 int argcount = 0;
1543
1544 char_u *argp = name_end;
1545 int ret = get_func_arguments(&argp, evalarg, 0,
1546 argvars, &argcount);
1547 if (ret == FAIL)
1548 return FAIL;
1549
1550 funcexe_T funcexe;
1551 CLEAR_FIELD(funcexe);
1552 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001553 if (rettv->v_type == VAR_OBJECT)
1554 {
1555 funcexe.fe_object = rettv->vval.v_object;
1556 ++funcexe.fe_object->obj_refcount;
1557 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001558
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001559 // Clear the class or object after calling the function, in
1560 // case the refcount is one.
1561 typval_T tv_tofree = *rettv;
1562 rettv->v_type = VAR_UNKNOWN;
1563
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001564 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001565 int error = call_user_func_check(fp, argcount, argvars,
1566 rettv, &funcexe, NULL);
1567
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001568 // Clear the previous rettv and the arguments.
1569 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001570 for (int idx = 0; idx < argcount; ++idx)
1571 clear_tv(&argvars[idx]);
1572
1573 if (error != FCERR_NONE)
1574 {
1575 user_func_error(error, printable_func_name(fp),
1576 funcexe.fe_found_var);
1577 return FAIL;
1578 }
1579 *arg = argp;
1580 return OK;
1581 }
1582 }
1583
1584 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
1585 }
1586
1587 else if (rettv->v_type == VAR_OBJECT)
1588 {
1589 for (int i = 0; i < cl->class_obj_member_count; ++i)
1590 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001591 ocmember_T *m = &cl->class_obj_members[i];
1592 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001593 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001594 if (*name == '_')
1595 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001596 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001597 return FAIL;
1598 }
1599
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001600 // The object only contains a pointer to the class, the member
1601 // values array follows right after that.
1602 object_T *obj = rettv->vval.v_object;
1603 typval_T *tv = (typval_T *)(obj + 1) + i;
1604 copy_tv(tv, rettv);
1605 object_unref(obj);
1606
1607 *arg = name_end;
1608 return OK;
1609 }
1610 }
1611
1612 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
1613 }
1614
Bram Moolenaard505d172022-12-18 21:42:55 +00001615 else if (rettv->v_type == VAR_CLASS)
1616 {
1617 // class member
1618 for (int i = 0; i < cl->class_class_member_count; ++i)
1619 {
1620 ocmember_T *m = &cl->class_class_members[i];
1621 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
1622 {
1623 if (*name == '_')
1624 {
1625 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
1626 return FAIL;
1627 }
1628
1629 typval_T *tv = &cl->class_members_tv[i];
1630 copy_tv(tv, rettv);
1631 class_unref(cl);
1632
1633 *arg = name_end;
1634 return OK;
1635 }
1636 }
1637
1638 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
1639 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001640
1641 return FAIL;
1642}
1643
1644/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001645 * If "arg" points to a class or object method, return it.
1646 * Otherwise return NULL.
1647 */
1648 ufunc_T *
1649find_class_func(char_u **arg)
1650{
1651 char_u *name = *arg;
1652 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1653 if (name_end == name || *name_end != '.')
1654 return NULL;
1655
1656 size_t len = name_end - name;
1657 typval_T tv;
1658 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001659 if (eval_variable(name, (int)len,
1660 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001661 return NULL;
1662 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001663 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001664
1665 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1666 : tv.vval.v_object->obj_class;
1667 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001668 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001669 char_u *fname = name_end + 1;
1670 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1671 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001672 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001673 len = fname_end - fname;
1674
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001675 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1676 : cl->class_obj_method_count;
1677 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1678 : cl->class_obj_methods;
1679 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001680 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001681 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001682 // Use a separate pointer to avoid that ASAN complains about
1683 // uf_name[] only being 4 characters.
1684 char_u *ufname = (char_u *)fp->uf_name;
1685 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001686 {
1687 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001688 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001689 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001690 }
1691
Bram Moolenaareb533502022-12-14 15:06:11 +00001692fail_after_eval:
1693 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001694 return NULL;
1695}
1696
1697/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001698 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1699 * index in class.class_class_members[].
1700 * If "cl_ret" is not NULL set it to the class.
1701 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001702 */
1703 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001704class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001705{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001706 if (cctx == NULL || cctx->ctx_ufunc == NULL
1707 || cctx->ctx_ufunc->uf_class == NULL)
1708 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001709 class_T *cl = cctx->ctx_ufunc->uf_class;
1710
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001711 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001712 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001713 ocmember_T *m = &cl->class_class_members[i];
1714 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001715 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001716 if (cl_ret != NULL)
1717 *cl_ret = cl;
1718 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001719 }
1720 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001721 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001722}
1723
1724/*
Bram Moolenaar62a69232023-01-24 15:07:04 +00001725 * Return TRUE if current context "cctx_arg" is inside class "cl".
1726 * Return FALSE if not.
1727 */
1728 int
1729inside_class(cctx_T *cctx_arg, class_T *cl)
1730{
1731 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
1732 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class == cl)
1733 return TRUE;
1734 return FALSE;
1735}
1736
1737/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001738 * Make a copy of an object.
1739 */
1740 void
1741copy_object(typval_T *from, typval_T *to)
1742{
1743 *to = *from;
1744 if (to->vval.v_object != NULL)
1745 ++to->vval.v_object->obj_refcount;
1746}
1747
1748/*
1749 * Free an object.
1750 */
1751 static void
1752object_clear(object_T *obj)
1753{
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001754 // Avoid a recursive call, it can happen if "obj" has a circular reference.
1755 obj->obj_refcount = INT_MAX;
1756
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001757 class_T *cl = obj->obj_class;
1758
Jia-Ju Bai5b0889b2023-08-13 20:04:04 +02001759 if (!cl)
1760 return;
1761
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001762 // the member values are just after the object structure
1763 typval_T *tv = (typval_T *)(obj + 1);
1764 for (int i = 0; i < cl->class_obj_member_count; ++i)
1765 clear_tv(tv + i);
1766
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001767 // Remove from the list headed by "first_object".
1768 object_cleared(obj);
1769
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001770 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001771 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001772}
1773
1774/*
1775 * Unreference an object.
1776 */
1777 void
1778object_unref(object_T *obj)
1779{
1780 if (obj != NULL && --obj->obj_refcount <= 0)
1781 object_clear(obj);
1782}
1783
1784/*
1785 * Make a copy of a class.
1786 */
1787 void
1788copy_class(typval_T *from, typval_T *to)
1789{
1790 *to = *from;
1791 if (to->vval.v_class != NULL)
1792 ++to->vval.v_class->class_refcount;
1793}
1794
1795/*
1796 * Unreference a class. Free it when the reference count goes down to zero.
1797 */
1798 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001799class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001800{
Bram Moolenaard505d172022-12-18 21:42:55 +00001801 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001802 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001803 // Freeing what the class contains may recursively come back here.
1804 // Clear "class_name" first, if it is NULL the class does not need to
1805 // be freed.
1806 VIM_CLEAR(cl->class_name);
1807
Bram Moolenaar83677162023-01-08 19:54:10 +00001808 class_unref(cl->class_extends);
1809
Bram Moolenaar94674f22023-01-06 18:42:20 +00001810 for (int i = 0; i < cl->class_interface_count; ++i)
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001811 {
Bram Moolenaar94674f22023-01-06 18:42:20 +00001812 vim_free(((char_u **)cl->class_interfaces)[i]);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001813 if (cl->class_interfaces_cl[i] != NULL)
1814 class_unref(cl->class_interfaces_cl[i]);
1815 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001816 vim_free(cl->class_interfaces);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001817 vim_free(cl->class_interfaces_cl);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001818
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001819 itf2class_T *next;
1820 for (itf2class_T *i2c = cl->class_itf2class; i2c != NULL; i2c = next)
1821 {
1822 next = i2c->i2c_next;
1823 vim_free(i2c);
1824 }
1825
Bram Moolenaard505d172022-12-18 21:42:55 +00001826 for (int i = 0; i < cl->class_class_member_count; ++i)
1827 {
1828 ocmember_T *m = &cl->class_class_members[i];
1829 vim_free(m->ocm_name);
1830 vim_free(m->ocm_init);
1831 if (cl->class_members_tv != NULL)
1832 clear_tv(&cl->class_members_tv[i]);
1833 }
1834 vim_free(cl->class_class_members);
1835 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001836
1837 for (int i = 0; i < cl->class_obj_member_count; ++i)
1838 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001839 ocmember_T *m = &cl->class_obj_members[i];
1840 vim_free(m->ocm_name);
1841 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001842 }
1843 vim_free(cl->class_obj_members);
1844
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001845 for (int i = 0; i < cl->class_class_function_count; ++i)
1846 {
1847 ufunc_T *uf = cl->class_class_functions[i];
1848 func_clear_free(uf, FALSE);
1849 }
1850 vim_free(cl->class_class_functions);
1851
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001852 for (int i = 0; i < cl->class_obj_method_count; ++i)
1853 {
1854 ufunc_T *uf = cl->class_obj_methods[i];
1855 func_clear_free(uf, FALSE);
1856 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001857 vim_free(cl->class_obj_methods);
1858
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001859 clear_type_list(&cl->class_type_list);
1860
1861 vim_free(cl);
1862 }
1863}
1864
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001865static object_T *first_object = NULL;
1866
1867/*
1868 * Call this function when an object has been created. It will be added to the
1869 * list headed by "first_object".
1870 */
1871 void
1872object_created(object_T *obj)
1873{
1874 if (first_object != NULL)
1875 {
1876 obj->obj_next_used = first_object;
1877 first_object->obj_prev_used = obj;
1878 }
1879 first_object = obj;
1880}
1881
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001882static object_T *next_nonref_obj = NULL;
1883
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001884/*
1885 * Call this function when an object has been cleared and is about to be freed.
1886 * It is removed from the list headed by "first_object".
1887 */
1888 void
1889object_cleared(object_T *obj)
1890{
1891 if (obj->obj_next_used != NULL)
1892 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1893 if (obj->obj_prev_used != NULL)
1894 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1895 else if (first_object == obj)
1896 first_object = obj->obj_next_used;
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001897
1898 // update the next object to check if needed
1899 if (obj == next_nonref_obj)
1900 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001901}
1902
1903/*
1904 * Go through the list of all objects and free items without "copyID".
1905 */
1906 int
1907object_free_nonref(int copyID)
1908{
1909 int did_free = FALSE;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001910
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001911 for (object_T *obj = first_object; obj != NULL; obj = next_nonref_obj)
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001912 {
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001913 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001914 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1915 {
1916 // Free the object and items it contains.
1917 object_clear(obj);
1918 did_free = TRUE;
1919 }
1920 }
1921
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001922 next_nonref_obj = NULL;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001923 return did_free;
1924}
1925
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001926
1927#endif // FEAT_EVAL