blob: 9e5bb56bf1db8cdbd17b93f093afc00a9b906c53 [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/*
690 * Add a default constructor to the class "cl".
691 */
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 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001153 garray_T *fgap = has_static || is_new
1154 ? &classfunctions : &objmethods;
Bram Moolenaar58b40092023-01-11 15:59:05 +00001155 // Check the name isn't used already.
1156 for (int i = 0; i < fgap->ga_len; ++i)
1157 {
1158 char_u *n = ((ufunc_T **)fgap->ga_data)[i]->uf_name;
1159 if (STRCMP(name, n) == 0)
1160 {
1161 semsg(_(e_duplicate_function_str), name);
1162 break;
1163 }
1164 }
1165
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001166 if (ga_grow(fgap, 1) == OK)
1167 {
1168 if (is_new)
1169 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001170
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001171 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
1172 ++fgap->ga_len;
1173 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001174 }
1175 }
1176
1177 // class members
1178 else if (has_static)
1179 {
1180 // class members (public, read access, private):
1181 // "static _varname"
1182 // "static varname"
1183 // "public static varname"
1184 char_u *varname = p;
1185 char_u *varname_end = NULL;
1186 type_T *type = NULL;
1187 char_u *init_expr = NULL;
1188 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +00001189 &varname_end, &type_list, &type,
1190 is_class ? &init_expr : NULL) == FAIL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001191 break;
1192 if (add_member(&classmembers, varname, varname_end,
1193 has_public, type, init_expr) == FAIL)
1194 {
1195 vim_free(init_expr);
1196 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001197 }
1198 }
1199
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001200 else
1201 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001202 if (is_class)
1203 semsg(_(e_not_valid_command_in_class_str), line);
1204 else
1205 semsg(_(e_not_valid_command_in_interface_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001206 break;
1207 }
1208 }
1209 vim_free(theline);
1210
Bram Moolenaar83677162023-01-08 19:54:10 +00001211 class_T *extends_cl = NULL; // class from "extends" argument
1212
1213 /*
1214 * Check a few things before defining the class.
1215 */
1216
1217 // Check the "extends" class is valid.
1218 if (success && extends != NULL)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001219 success = validate_extends_class(extends, &extends_cl);
Bram Moolenaar83677162023-01-08 19:54:10 +00001220 VIM_CLEAR(extends);
1221
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001222 class_T **intf_classes = NULL;
1223
Bram Moolenaar83677162023-01-08 19:54:10 +00001224 // Check all "implements" entries are valid.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001225 if (success && ga_impl.ga_len > 0)
1226 {
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001227 intf_classes = ALLOC_CLEAR_MULT(class_T *, ga_impl.ga_len);
1228
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001229 success = validate_implements_classes(&ga_impl, intf_classes,
1230 &classfunctions, &classmembers,
1231 &objmethods, &objmembers);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001232 }
1233
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001234 // Check no function argument name is used as a class member.
Bram Moolenaard40f00c2023-01-13 17:36:49 +00001235 if (success)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001236 success = check_func_arg_names(&classfunctions, &objmethods,
1237 &classmembers);
Bram Moolenaard40f00c2023-01-13 17:36:49 +00001238
Bram Moolenaareb533502022-12-14 15:06:11 +00001239 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001240 if (success)
1241 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001242 // "endclass" encountered without failures: Create the class.
1243
Bram Moolenaareb533502022-12-14 15:06:11 +00001244 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001245 if (cl == NULL)
1246 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +00001247 if (!is_class)
1248 cl->class_flags = CLASS_INTERFACE;
1249
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001250 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +00001251 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +00001252 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001253 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +00001254
Bram Moolenaard0200c82023-01-28 15:19:40 +00001255 if (extends_cl != NULL)
1256 {
1257 cl->class_extends = extends_cl;
1258 extends_cl->class_flags |= CLASS_EXTENDED;
1259 }
Bram Moolenaar83677162023-01-08 19:54:10 +00001260
Bram Moolenaard505d172022-12-18 21:42:55 +00001261 // Add class and object members to "cl".
1262 if (add_members_to_class(&classmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +00001263 extends_cl == NULL ? NULL
1264 : extends_cl->class_class_members,
1265 extends_cl == NULL ? 0
1266 : extends_cl->class_class_member_count,
1267 &cl->class_class_members,
1268 &cl->class_class_member_count) == FAIL
Bram Moolenaard505d172022-12-18 21:42:55 +00001269 || add_members_to_class(&objmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +00001270 extends_cl == NULL ? NULL
1271 : extends_cl->class_obj_members,
1272 extends_cl == NULL ? 0
1273 : extends_cl->class_obj_member_count,
1274 &cl->class_obj_members,
1275 &cl->class_obj_member_count) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +00001276 goto cleanup;
1277
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001278 if (ga_impl.ga_len > 0)
1279 {
1280 // Move the "implements" names into the class.
1281 cl->class_interface_count = ga_impl.ga_len;
1282 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
1283 if (cl->class_interfaces == NULL)
1284 goto cleanup;
1285 for (int i = 0; i < ga_impl.ga_len; ++i)
1286 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
1287 VIM_CLEAR(ga_impl.ga_data);
1288 ga_impl.ga_len = 0;
1289
Bram Moolenaard0200c82023-01-28 15:19:40 +00001290 cl->class_interfaces_cl = intf_classes;
1291 intf_classes = NULL;
1292 }
1293
1294 if (cl->class_interface_count > 0 || extends_cl != NULL)
1295 {
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001296 // Add a method and member lookup table to each of the interface
1297 // classes.
1298 if (add_lookup_tables(cl, extends_cl, &objmethods) == FAIL)
1299 goto cleanup;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001300 }
1301
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001302 // Allocate a typval for each class member and initialize it.
Bram Moolenaar554d0312023-01-05 19:59:18 +00001303 if (is_class && cl->class_class_member_count > 0)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001304 add_class_members(cl, eap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001305
1306 int have_new = FALSE;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001307 for (int i = 0; i < classfunctions.ga_len; ++i)
1308 if (STRCMP(((ufunc_T **)classfunctions.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001309 "new") == 0)
1310 {
1311 have_new = TRUE;
1312 break;
1313 }
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001314 if (is_class && !is_abstract && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001315 // No new() method was defined, add the default constructor.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001316 add_default_constructor(cl, &classfunctions, &type_list);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001317
Bram Moolenaar58b40092023-01-11 15:59:05 +00001318 // Move all the functions into the created class.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001319 if (add_classfuncs_objmethods(cl, extends_cl, &classfunctions,
1320 &objmethods) == FAIL)
1321 goto cleanup;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001322
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001323 cl->class_type.tt_type = VAR_CLASS;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001324 cl->class_type.tt_class = cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001325 cl->class_object_type.tt_type = VAR_OBJECT;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001326 cl->class_object_type.tt_class = cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001327 cl->class_type_list = type_list;
1328
1329 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +00001330 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001331
1332 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001333 // TODO: handle other context, e.g. in a function
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001334 typval_T tv;
1335 tv.v_type = VAR_CLASS;
1336 tv.vval.v_class = cl;
Bram Moolenaara86655a2023-01-12 17:06:27 +00001337 is_export = class_export;
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001338 SOURCING_LNUM = start_lnum;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001339 set_var_const(cl->class_name, current_sctx.sc_sid,
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001340 NULL, &tv, FALSE, 0, 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001341 return;
1342 }
1343
1344cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +00001345 if (cl != NULL)
1346 {
1347 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001348 vim_free(cl->class_class_functions);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001349 if (cl->class_interfaces != NULL)
1350 {
1351 for (int i = 0; i < cl->class_interface_count; ++i)
1352 vim_free(cl->class_interfaces[i]);
1353 vim_free(cl->class_interfaces);
1354 }
1355 if (cl->class_interfaces_cl != NULL)
1356 {
1357 for (int i = 0; i < cl->class_interface_count; ++i)
1358 class_unref(cl->class_interfaces_cl[i]);
1359 vim_free(cl->class_interfaces_cl);
1360 }
Bram Moolenaareb533502022-12-14 15:06:11 +00001361 vim_free(cl->class_obj_members);
1362 vim_free(cl->class_obj_methods);
1363 vim_free(cl);
1364 }
1365
Bram Moolenaar83677162023-01-08 19:54:10 +00001366 vim_free(extends);
1367 class_unref(extends_cl);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001368
1369 if (intf_classes != NULL)
1370 {
1371 for (int i = 0; i < ga_impl.ga_len; ++i)
1372 class_unref(intf_classes[i]);
1373 vim_free(intf_classes);
1374 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001375 ga_clear_strings(&ga_impl);
1376
Bram Moolenaard505d172022-12-18 21:42:55 +00001377 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001378 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001379 garray_T *gap = round == 1 ? &classmembers : &objmembers;
1380 if (gap->ga_len == 0 || gap->ga_data == NULL)
1381 continue;
1382
1383 for (int i = 0; i < gap->ga_len; ++i)
1384 {
1385 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
1386 vim_free(m->ocm_name);
1387 vim_free(m->ocm_init);
1388 }
1389 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001390 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001391
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001392 for (int i = 0; i < objmethods.ga_len; ++i)
1393 {
1394 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
1395 func_clear_free(uf, FALSE);
1396 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001397 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001398
1399 for (int i = 0; i < classfunctions.ga_len; ++i)
1400 {
1401 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
1402 func_clear_free(uf, FALSE);
1403 }
1404 ga_clear(&classfunctions);
1405
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001406 clear_type_list(&type_list);
1407}
1408
1409/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001410 * Find member "name" in class "cl", set "member_idx" to the member index and
1411 * return its type.
1412 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001413 */
1414 type_T *
1415class_member_type(
1416 class_T *cl,
1417 char_u *name,
1418 char_u *name_end,
1419 int *member_idx)
1420{
1421 *member_idx = -1; // not found (yet)
1422 size_t len = name_end - name;
1423
1424 for (int i = 0; i < cl->class_obj_member_count; ++i)
1425 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001426 ocmember_T *m = cl->class_obj_members + i;
1427 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001428 {
1429 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001430 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001431 }
1432 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001433
1434 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001435 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001436}
1437
1438/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001439 * Handle ":enum" up to ":endenum".
1440 */
1441 void
1442ex_enum(exarg_T *eap UNUSED)
1443{
1444 // TODO
1445}
1446
1447/*
1448 * Handle ":type".
1449 */
1450 void
1451ex_type(exarg_T *eap UNUSED)
1452{
1453 // TODO
1454}
1455
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001456/*
1457 * Evaluate what comes after a class:
1458 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001459 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001460 * - class constructor: SomeClass.new()
1461 * - object member: someObject.varname
1462 * - object method: someObject.SomeMethod()
1463 *
1464 * "*arg" points to the '.'.
1465 * "*arg" is advanced to after the member name or method call.
1466 *
1467 * Returns FAIL or OK.
1468 */
1469 int
1470class_object_index(
1471 char_u **arg,
1472 typval_T *rettv,
1473 evalarg_T *evalarg,
1474 int verbose UNUSED) // give error messages
1475{
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001476 if (VIM_ISWHITE((*arg)[1]))
1477 {
1478 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
1479 return FAIL;
1480 }
1481
1482 ++*arg;
1483 char_u *name = *arg;
1484 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1485 if (name_end == name)
1486 return FAIL;
1487 size_t len = name_end - name;
1488
Bram Moolenaar552bdca2023-02-17 21:08:50 +00001489 class_T *cl;
1490 if (rettv->v_type == VAR_CLASS)
1491 cl = rettv->vval.v_class;
1492 else // VAR_OBJECT
1493 {
1494 if (rettv->vval.v_object == NULL)
1495 {
1496 emsg(_(e_using_null_object));
1497 return FAIL;
1498 }
1499 cl = rettv->vval.v_object->obj_class;
1500 }
1501
Bram Moolenaard13dd302023-03-11 20:56:35 +00001502 if (cl == NULL)
1503 {
1504 emsg(_(e_incomplete_type));
1505 return FAIL;
1506 }
1507
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001508 if (*name_end == '(')
1509 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001510 int on_class = rettv->v_type == VAR_CLASS;
1511 int count = on_class ? cl->class_class_function_count
1512 : cl->class_obj_method_count;
1513 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001514 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001515 ufunc_T *fp = on_class ? cl->class_class_functions[i]
1516 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +00001517 // Use a separate pointer to avoid that ASAN complains about
1518 // uf_name[] only being 4 characters.
1519 char_u *ufname = (char_u *)fp->uf_name;
1520 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001521 {
1522 typval_T argvars[MAX_FUNC_ARGS + 1];
1523 int argcount = 0;
1524
1525 char_u *argp = name_end;
1526 int ret = get_func_arguments(&argp, evalarg, 0,
1527 argvars, &argcount);
1528 if (ret == FAIL)
1529 return FAIL;
1530
1531 funcexe_T funcexe;
1532 CLEAR_FIELD(funcexe);
1533 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001534 if (rettv->v_type == VAR_OBJECT)
1535 {
1536 funcexe.fe_object = rettv->vval.v_object;
1537 ++funcexe.fe_object->obj_refcount;
1538 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001539
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001540 // Clear the class or object after calling the function, in
1541 // case the refcount is one.
1542 typval_T tv_tofree = *rettv;
1543 rettv->v_type = VAR_UNKNOWN;
1544
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001545 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001546 int error = call_user_func_check(fp, argcount, argvars,
1547 rettv, &funcexe, NULL);
1548
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001549 // Clear the previous rettv and the arguments.
1550 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001551 for (int idx = 0; idx < argcount; ++idx)
1552 clear_tv(&argvars[idx]);
1553
1554 if (error != FCERR_NONE)
1555 {
1556 user_func_error(error, printable_func_name(fp),
1557 funcexe.fe_found_var);
1558 return FAIL;
1559 }
1560 *arg = argp;
1561 return OK;
1562 }
1563 }
1564
1565 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
1566 }
1567
1568 else if (rettv->v_type == VAR_OBJECT)
1569 {
1570 for (int i = 0; i < cl->class_obj_member_count; ++i)
1571 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001572 ocmember_T *m = &cl->class_obj_members[i];
1573 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001574 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001575 if (*name == '_')
1576 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001577 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001578 return FAIL;
1579 }
1580
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001581 // The object only contains a pointer to the class, the member
1582 // values array follows right after that.
1583 object_T *obj = rettv->vval.v_object;
1584 typval_T *tv = (typval_T *)(obj + 1) + i;
1585 copy_tv(tv, rettv);
1586 object_unref(obj);
1587
1588 *arg = name_end;
1589 return OK;
1590 }
1591 }
1592
1593 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
1594 }
1595
Bram Moolenaard505d172022-12-18 21:42:55 +00001596 else if (rettv->v_type == VAR_CLASS)
1597 {
1598 // class member
1599 for (int i = 0; i < cl->class_class_member_count; ++i)
1600 {
1601 ocmember_T *m = &cl->class_class_members[i];
1602 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
1603 {
1604 if (*name == '_')
1605 {
1606 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
1607 return FAIL;
1608 }
1609
1610 typval_T *tv = &cl->class_members_tv[i];
1611 copy_tv(tv, rettv);
1612 class_unref(cl);
1613
1614 *arg = name_end;
1615 return OK;
1616 }
1617 }
1618
1619 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
1620 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001621
1622 return FAIL;
1623}
1624
1625/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001626 * If "arg" points to a class or object method, return it.
1627 * Otherwise return NULL.
1628 */
1629 ufunc_T *
1630find_class_func(char_u **arg)
1631{
1632 char_u *name = *arg;
1633 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1634 if (name_end == name || *name_end != '.')
1635 return NULL;
1636
1637 size_t len = name_end - name;
1638 typval_T tv;
1639 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001640 if (eval_variable(name, (int)len,
1641 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001642 return NULL;
1643 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001644 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001645
1646 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1647 : tv.vval.v_object->obj_class;
1648 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001649 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001650 char_u *fname = name_end + 1;
1651 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1652 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001653 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001654 len = fname_end - fname;
1655
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001656 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1657 : cl->class_obj_method_count;
1658 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1659 : cl->class_obj_methods;
1660 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001661 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001662 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001663 // Use a separate pointer to avoid that ASAN complains about
1664 // uf_name[] only being 4 characters.
1665 char_u *ufname = (char_u *)fp->uf_name;
1666 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001667 {
1668 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001669 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001670 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001671 }
1672
Bram Moolenaareb533502022-12-14 15:06:11 +00001673fail_after_eval:
1674 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001675 return NULL;
1676}
1677
1678/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001679 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1680 * index in class.class_class_members[].
1681 * If "cl_ret" is not NULL set it to the class.
1682 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001683 */
1684 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001685class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001686{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001687 if (cctx == NULL || cctx->ctx_ufunc == NULL
1688 || cctx->ctx_ufunc->uf_class == NULL)
1689 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001690 class_T *cl = cctx->ctx_ufunc->uf_class;
1691
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001692 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001693 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001694 ocmember_T *m = &cl->class_class_members[i];
1695 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001696 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001697 if (cl_ret != NULL)
1698 *cl_ret = cl;
1699 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001700 }
1701 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001702 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001703}
1704
1705/*
Bram Moolenaar62a69232023-01-24 15:07:04 +00001706 * Return TRUE if current context "cctx_arg" is inside class "cl".
1707 * Return FALSE if not.
1708 */
1709 int
1710inside_class(cctx_T *cctx_arg, class_T *cl)
1711{
1712 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
1713 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class == cl)
1714 return TRUE;
1715 return FALSE;
1716}
1717
1718/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001719 * Make a copy of an object.
1720 */
1721 void
1722copy_object(typval_T *from, typval_T *to)
1723{
1724 *to = *from;
1725 if (to->vval.v_object != NULL)
1726 ++to->vval.v_object->obj_refcount;
1727}
1728
1729/*
1730 * Free an object.
1731 */
1732 static void
1733object_clear(object_T *obj)
1734{
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001735 // Avoid a recursive call, it can happen if "obj" has a circular reference.
1736 obj->obj_refcount = INT_MAX;
1737
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001738 class_T *cl = obj->obj_class;
1739
Jia-Ju Bai5b0889b2023-08-13 20:04:04 +02001740 if (!cl)
1741 return;
1742
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001743 // the member values are just after the object structure
1744 typval_T *tv = (typval_T *)(obj + 1);
1745 for (int i = 0; i < cl->class_obj_member_count; ++i)
1746 clear_tv(tv + i);
1747
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001748 // Remove from the list headed by "first_object".
1749 object_cleared(obj);
1750
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001751 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001752 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001753}
1754
1755/*
1756 * Unreference an object.
1757 */
1758 void
1759object_unref(object_T *obj)
1760{
1761 if (obj != NULL && --obj->obj_refcount <= 0)
1762 object_clear(obj);
1763}
1764
1765/*
1766 * Make a copy of a class.
1767 */
1768 void
1769copy_class(typval_T *from, typval_T *to)
1770{
1771 *to = *from;
1772 if (to->vval.v_class != NULL)
1773 ++to->vval.v_class->class_refcount;
1774}
1775
1776/*
1777 * Unreference a class. Free it when the reference count goes down to zero.
1778 */
1779 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001780class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001781{
Bram Moolenaard505d172022-12-18 21:42:55 +00001782 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001783 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001784 // Freeing what the class contains may recursively come back here.
1785 // Clear "class_name" first, if it is NULL the class does not need to
1786 // be freed.
1787 VIM_CLEAR(cl->class_name);
1788
Bram Moolenaar83677162023-01-08 19:54:10 +00001789 class_unref(cl->class_extends);
1790
Bram Moolenaar94674f22023-01-06 18:42:20 +00001791 for (int i = 0; i < cl->class_interface_count; ++i)
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001792 {
Bram Moolenaar94674f22023-01-06 18:42:20 +00001793 vim_free(((char_u **)cl->class_interfaces)[i]);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001794 if (cl->class_interfaces_cl[i] != NULL)
1795 class_unref(cl->class_interfaces_cl[i]);
1796 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001797 vim_free(cl->class_interfaces);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001798 vim_free(cl->class_interfaces_cl);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001799
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001800 itf2class_T *next;
1801 for (itf2class_T *i2c = cl->class_itf2class; i2c != NULL; i2c = next)
1802 {
1803 next = i2c->i2c_next;
1804 vim_free(i2c);
1805 }
1806
Bram Moolenaard505d172022-12-18 21:42:55 +00001807 for (int i = 0; i < cl->class_class_member_count; ++i)
1808 {
1809 ocmember_T *m = &cl->class_class_members[i];
1810 vim_free(m->ocm_name);
1811 vim_free(m->ocm_init);
1812 if (cl->class_members_tv != NULL)
1813 clear_tv(&cl->class_members_tv[i]);
1814 }
1815 vim_free(cl->class_class_members);
1816 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001817
1818 for (int i = 0; i < cl->class_obj_member_count; ++i)
1819 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001820 ocmember_T *m = &cl->class_obj_members[i];
1821 vim_free(m->ocm_name);
1822 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001823 }
1824 vim_free(cl->class_obj_members);
1825
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001826 for (int i = 0; i < cl->class_class_function_count; ++i)
1827 {
1828 ufunc_T *uf = cl->class_class_functions[i];
1829 func_clear_free(uf, FALSE);
1830 }
1831 vim_free(cl->class_class_functions);
1832
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001833 for (int i = 0; i < cl->class_obj_method_count; ++i)
1834 {
1835 ufunc_T *uf = cl->class_obj_methods[i];
1836 func_clear_free(uf, FALSE);
1837 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001838 vim_free(cl->class_obj_methods);
1839
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001840 clear_type_list(&cl->class_type_list);
1841
1842 vim_free(cl);
1843 }
1844}
1845
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001846static object_T *first_object = NULL;
1847
1848/*
1849 * Call this function when an object has been created. It will be added to the
1850 * list headed by "first_object".
1851 */
1852 void
1853object_created(object_T *obj)
1854{
1855 if (first_object != NULL)
1856 {
1857 obj->obj_next_used = first_object;
1858 first_object->obj_prev_used = obj;
1859 }
1860 first_object = obj;
1861}
1862
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001863static object_T *next_nonref_obj = NULL;
1864
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001865/*
1866 * Call this function when an object has been cleared and is about to be freed.
1867 * It is removed from the list headed by "first_object".
1868 */
1869 void
1870object_cleared(object_T *obj)
1871{
1872 if (obj->obj_next_used != NULL)
1873 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1874 if (obj->obj_prev_used != NULL)
1875 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1876 else if (first_object == obj)
1877 first_object = obj->obj_next_used;
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001878
1879 // update the next object to check if needed
1880 if (obj == next_nonref_obj)
1881 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001882}
1883
1884/*
1885 * Go through the list of all objects and free items without "copyID".
1886 */
1887 int
1888object_free_nonref(int copyID)
1889{
1890 int did_free = FALSE;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001891
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001892 for (object_T *obj = first_object; obj != NULL; obj = next_nonref_obj)
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001893 {
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001894 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001895 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1896 {
1897 // Free the object and items it contains.
1898 object_clear(obj);
1899 did_free = TRUE;
1900 }
1901 }
1902
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001903 next_nonref_obj = NULL;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001904 return did_free;
1905}
1906
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001907
1908#endif // FEAT_EVAL