blob: 36a5d0a3584e3861f1b9374c08cdbf385aa19f55 [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
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +020070 char_u *init_arg = skipwhite(type_arg);
71 if (type == NULL && *init_arg != '=')
Bram Moolenaard505d172022-12-18 21:42:55 +000072 {
73 emsg(_(e_type_or_initialization_required));
74 return FAIL;
75 }
76
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +020077 if (init_expr == NULL && *init_arg == '=')
Bram Moolenaard505d172022-12-18 21:42:55 +000078 {
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +020079 emsg(_(e_cannot_initialize_member_in_interface));
80 return FAIL;
81 }
82
83 if (*init_arg == '=')
84 {
85 evalarg_T evalarg;
86 char_u *expr_start, *expr_end;
87
88 if (!VIM_ISWHITE(init_arg[-1]) || !VIM_ISWHITE(init_arg[1]))
Bram Moolenaard505d172022-12-18 21:42:55 +000089 {
90 semsg(_(e_white_space_required_before_and_after_str_at_str),
91 "=", type_arg);
92 return FAIL;
93 }
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +020094 init_arg = skipwhite(init_arg + 1);
Bram Moolenaard505d172022-12-18 21:42:55 +000095
Bram Moolenaard505d172022-12-18 21:42:55 +000096 fill_evalarg_from_eap(&evalarg, eap, FALSE);
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +020097 (void)skip_expr_concatenate(&init_arg, &expr_start, &expr_end, &evalarg);
Bram Moolenaard505d172022-12-18 21:42:55 +000098
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +020099 // No type specified for the member. Set it to "any" and the correct
100 // type will be set when the object is instantiated.
Bram Moolenaard505d172022-12-18 21:42:55 +0000101 if (type == NULL)
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200102 type = &t_any;
Bram Moolenaard505d172022-12-18 21:42:55 +0000103
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200104 *init_expr = vim_strnsave(expr_start, expr_end - expr_start);
105 // Free the memory pointed by expr_start.
Bram Moolenaard505d172022-12-18 21:42:55 +0000106 clear_evalarg(&evalarg, NULL);
107 }
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200108 else if (!valid_declaration_type(type))
Bram Moolenaard505d172022-12-18 21:42:55 +0000109 return FAIL;
110
111 *type_ret = type;
Bram Moolenaard505d172022-12-18 21:42:55 +0000112 return OK;
113}
114
115/*
116 * Add a member to an object or a class.
117 * Returns OK when successful, "init_expr" will be consumed then.
118 * Returns FAIL otherwise, caller might need to free "init_expr".
119 */
120 static int
121add_member(
122 garray_T *gap,
123 char_u *varname,
124 char_u *varname_end,
125 int has_public,
126 type_T *type,
127 char_u *init_expr)
128{
129 if (ga_grow(gap, 1) == FAIL)
130 return FAIL;
131 ocmember_T *m = ((ocmember_T *)gap->ga_data) + gap->ga_len;
132 m->ocm_name = vim_strnsave(varname, varname_end - varname);
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +0000133 m->ocm_access = has_public ? VIM_ACCESS_ALL
134 : *varname == '_' ? VIM_ACCESS_PRIVATE : VIM_ACCESS_READ;
Bram Moolenaard505d172022-12-18 21:42:55 +0000135 m->ocm_type = type;
136 if (init_expr != NULL)
137 m->ocm_init = init_expr;
138 ++gap->ga_len;
139 return OK;
140}
141
142/*
143 * Move the class or object members found while parsing a class into the class.
144 * "gap" contains the found members.
Bram Moolenaar83677162023-01-08 19:54:10 +0000145 * "parent_members" points to the members in the parent class (if any)
146 * "parent_count" is the number of members in the parent class
Bram Moolenaard505d172022-12-18 21:42:55 +0000147 * "members" will be set to the newly allocated array of members and
148 * "member_count" set to the number of members.
149 * Returns OK or FAIL.
150 */
151 static int
152add_members_to_class(
153 garray_T *gap,
Bram Moolenaar83677162023-01-08 19:54:10 +0000154 ocmember_T *parent_members,
155 int parent_count,
Bram Moolenaard505d172022-12-18 21:42:55 +0000156 ocmember_T **members,
157 int *member_count)
158{
Bram Moolenaar83677162023-01-08 19:54:10 +0000159 *member_count = parent_count + gap->ga_len;
160 *members = *member_count == 0 ? NULL
161 : ALLOC_MULT(ocmember_T, *member_count);
162 if (*member_count > 0 && *members == NULL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000163 return FAIL;
Bram Moolenaar83677162023-01-08 19:54:10 +0000164 for (int i = 0; i < parent_count; ++i)
165 {
166 // parent members need to be copied
Bram Moolenaarae3205a2023-01-15 20:49:00 +0000167 ocmember_T *m = *members + i;
168 *m = parent_members[i];
169 m->ocm_name = vim_strsave(m->ocm_name);
170 if (m->ocm_init != NULL)
171 m->ocm_init = vim_strsave(m->ocm_init);
Bram Moolenaar83677162023-01-08 19:54:10 +0000172 }
Bram Moolenaar8efdcee2022-12-19 12:18:09 +0000173 if (gap->ga_len > 0)
Bram Moolenaar83677162023-01-08 19:54:10 +0000174 // new members are moved
175 mch_memmove(*members + parent_count,
176 gap->ga_data, sizeof(ocmember_T) * gap->ga_len);
Bram Moolenaard505d172022-12-18 21:42:55 +0000177 VIM_CLEAR(gap->ga_data);
178 return OK;
179}
180
181/*
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000182 * Convert a member index "idx" of interface "itf" to the member index of class
183 * "cl" implementing that interface.
184 */
185 int
Bram Moolenaard0200c82023-01-28 15:19:40 +0000186object_index_from_itf_index(class_T *itf, int is_method, int idx, class_T *cl)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000187{
Bram Moolenaard0200c82023-01-28 15:19:40 +0000188 if (idx > (is_method ? itf->class_obj_method_count
189 : itf->class_obj_member_count))
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000190 {
191 siemsg("index %d out of range for interface %s", idx, itf->class_name);
192 return 0;
193 }
Yegappan Lakshmanan74cc13c2023-08-13 17:41:26 +0200194
195 // If "cl" is the interface or the class that is extended, then the method
196 // index can be used directly and there is no need to search for the method
197 // index in one of the child classes.
198 if (cl == itf)
199 return idx;
200
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000201 itf2class_T *i2c;
202 for (i2c = itf->class_itf2class; i2c != NULL; i2c = i2c->i2c_next)
Bram Moolenaard0200c82023-01-28 15:19:40 +0000203 if (i2c->i2c_class == cl && i2c->i2c_is_method == is_method)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000204 break;
205 if (i2c == NULL)
206 {
207 siemsg("class %s not found on interface %s",
208 cl->class_name, itf->class_name);
209 return 0;
210 }
211 int *table = (int *)(i2c + 1);
212 return table[idx];
213}
214
215/*
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200216 * Check whether a class named "extends_name" is present. If the class is
217 * valid, then "extends_clp" is set with the class pointer.
218 * Returns TRUE if the class name "extends_names" is a valid class.
219 */
220 static int
221validate_extends_class(char_u *extends_name, class_T **extends_clp)
222{
223 typval_T tv;
224 int success = FALSE;
225
226 tv.v_type = VAR_UNKNOWN;
227 if (eval_variable_import(extends_name, &tv) == FAIL)
228 {
229 semsg(_(e_class_name_not_found_str), extends_name);
230 return success;
231 }
232 else
233 {
234 if (tv.v_type != VAR_CLASS
235 || tv.vval.v_class == NULL
236 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) != 0)
237 semsg(_(e_cannot_extend_str), extends_name);
238 else
239 {
240 class_T *extends_cl = tv.vval.v_class;
241 ++extends_cl->class_refcount;
242 *extends_clp = extends_cl;
243 success = TRUE;
244 }
245 clear_tv(&tv);
246 }
247
248 return success;
249}
250
251/*
252 * Check the members of the interface class "ifcl" match the class members
253 * ("classmembers_gap") and object members ("objmembers_gap") of a class.
254 * Returns TRUE if the class and object member names are valid.
255 */
256 static int
257validate_interface_members(
258 char_u *intf_class_name,
259 class_T *ifcl,
260 garray_T *classmembers_gap,
261 garray_T *objmembers_gap)
262{
263 int success = TRUE;
264
265 for (int loop = 1; loop <= 2 && success; ++loop)
266 {
267 // loop == 1: check class members
268 // loop == 2: check object members
269 int if_count = loop == 1 ? ifcl->class_class_member_count
270 : ifcl->class_obj_member_count;
271 if (if_count == 0)
272 continue;
273 ocmember_T *if_ms = loop == 1 ? ifcl->class_class_members
274 : ifcl->class_obj_members;
275 ocmember_T *cl_ms = (ocmember_T *)(loop == 1
276 ? classmembers_gap->ga_data
277 : objmembers_gap->ga_data);
278 int cl_count = loop == 1 ? classmembers_gap->ga_len
279 : objmembers_gap->ga_len;
280 for (int if_i = 0; if_i < if_count; ++if_i)
281 {
282 int cl_i;
283 for (cl_i = 0; cl_i < cl_count; ++cl_i)
284 {
285 ocmember_T *m = &cl_ms[cl_i];
286 where_T where = WHERE_INIT;
287
288 if (STRCMP(if_ms[if_i].ocm_name, m->ocm_name) != 0)
289 continue;
290
291 // Ensure the type is matching.
292 where.wt_func_name = (char *)m->ocm_name;
293 where.wt_kind = WT_MEMBER;
294 if (check_type_maybe(if_ms[if_i].ocm_type, m->ocm_type, TRUE,
295 where) != OK)
296 success = FALSE;
297
298 break;
299 }
300 if (cl_i == cl_count)
301 {
302 semsg(_(e_member_str_of_interface_str_not_implemented),
303 if_ms[if_i].ocm_name, intf_class_name);
304 success = FALSE;
305 break;
306 }
307 }
308 }
309
310 return success;
311}
312
313/*
314 * Check the functions/methods of the interface class "ifcl" match the class
315 * methods ("classfunctions_gap") and object functions ("objmemthods_gap") of a
316 * class.
317 * Returns TRUE if the class and object member names are valid.
318 */
319 static int
320validate_interface_methods(
321 char_u *intf_class_name,
322 class_T *ifcl,
323 garray_T *classfunctions_gap,
324 garray_T *objmethods_gap)
325{
326 int success = TRUE;
327
328 for (int loop = 1; loop <= 2 && success; ++loop)
329 {
330 // loop == 1: check class functions
331 // loop == 2: check object methods
332 int if_count = loop == 1 ? ifcl->class_class_function_count
333 : ifcl->class_obj_method_count;
334 if (if_count == 0)
335 continue;
336 ufunc_T **if_fp = loop == 1 ? ifcl->class_class_functions
337 : ifcl->class_obj_methods;
338 ufunc_T **cl_fp = (ufunc_T **)(loop == 1
339 ? classfunctions_gap->ga_data
340 : objmethods_gap->ga_data);
341 int cl_count = loop == 1 ? classfunctions_gap->ga_len
342 : objmethods_gap->ga_len;
343 for (int if_i = 0; if_i < if_count; ++if_i)
344 {
345 char_u *if_name = if_fp[if_i]->uf_name;
346 int cl_i;
347 for (cl_i = 0; cl_i < cl_count; ++cl_i)
348 {
349 char_u *cl_name = cl_fp[cl_i]->uf_name;
350 if (STRCMP(if_name, cl_name) == 0)
351 {
352 where_T where = WHERE_INIT;
353
354 // Ensure the type is matching.
355 where.wt_func_name = (char *)if_name;
356 where.wt_kind = WT_METHOD;
357 if (check_type_maybe(if_fp[if_i]->uf_func_type,
358 cl_fp[cl_i]->uf_func_type, TRUE, where) != OK)
359 success = FALSE;
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200360 // Ensure the public/private access level is matching.
361 if (if_fp[if_i]->uf_private != cl_fp[cl_i]->uf_private)
362 {
363 semsg(_(e_interface_str_and_class_str_function_access_not_same),
364 cl_name, if_name);
365 success = FALSE;
366 }
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200367 break;
368 }
369 }
370 if (cl_i == cl_count)
371 {
372 semsg(_(e_function_str_of_interface_str_not_implemented),
373 if_name, intf_class_name);
374 success = FALSE;
375 break;
376 }
377 }
378 }
379
380 return success;
381}
382
383/*
384 * Validate all the "implements" classes when creating a new class. The
385 * classes are returned in "intf_classes". The class functions, class methods,
386 * object methods and object members in the new class are in
387 * "classfunctions_gap", "classmembers_gap", "objmethods_gap", and
388 * "objmembers_gap" respectively.
389 */
390 static int
391validate_implements_classes(
392 garray_T *impl_gap,
393 class_T **intf_classes,
394 garray_T *classfunctions_gap,
395 garray_T *classmembers_gap,
396 garray_T *objmethods_gap,
397 garray_T *objmembers_gap)
398{
399 int success = TRUE;
400
401 for (int i = 0; i < impl_gap->ga_len && success; ++i)
402 {
403 char_u *impl = ((char_u **)impl_gap->ga_data)[i];
404 typval_T tv;
405 tv.v_type = VAR_UNKNOWN;
406 if (eval_variable_import(impl, &tv) == FAIL)
407 {
408 semsg(_(e_interface_name_not_found_str), impl);
409 success = FALSE;
410 break;
411 }
412
413 if (tv.v_type != VAR_CLASS
414 || tv.vval.v_class == NULL
415 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0)
416 {
417 semsg(_(e_not_valid_interface_str), impl);
418 success = FALSE;
419 clear_tv(&tv);
420 break;
421 }
422
423 class_T *ifcl = tv.vval.v_class;
424 intf_classes[i] = ifcl;
425 ++ifcl->class_refcount;
426
427 // check the members of the interface match the members of the class
428 success = validate_interface_members(impl, ifcl, classmembers_gap,
429 objmembers_gap);
430
431 // check the functions/methods of the interface match the
432 // functions/methods of the class
433 success = validate_interface_methods(impl, ifcl, classfunctions_gap,
434 objmethods_gap);
435 clear_tv(&tv);
436 }
437
438 return success;
439}
440
441/*
442 * Check no function argument name is used as a class member.
443 * (Object members are always accessed with "this." prefix, so no need
444 * to check them.)
445 */
446 static int
447check_func_arg_names(
448 garray_T *classfunctions_gap,
449 garray_T *objmethods_gap,
450 garray_T *classmembers_gap)
451{
452 int success = TRUE;
453
454 // loop 1: class functions, loop 2: object methods
455 for (int loop = 1; loop <= 2 && success; ++loop)
456 {
457 garray_T *gap = loop == 1 ? classfunctions_gap : objmethods_gap;
458
459 for (int fi = 0; fi < gap->ga_len && success; ++fi)
460 {
461 ufunc_T *uf = ((ufunc_T **)gap->ga_data)[fi];
462
463 for (int i = 0; i < uf->uf_args.ga_len && success; ++i)
464 {
465 char_u *aname = ((char_u **)uf->uf_args.ga_data)[i];
466 garray_T *mgap = classmembers_gap;
467
468 // Check all the class member names
469 for (int mi = 0; mi < mgap->ga_len; ++mi)
470 {
471 char_u *mname = ((ocmember_T *)mgap->ga_data + mi)
472 ->ocm_name;
473 if (STRCMP(aname, mname) == 0)
474 {
475 success = FALSE;
476
477 if (uf->uf_script_ctx.sc_sid > 0)
478 SOURCING_LNUM = uf->uf_script_ctx.sc_lnum;
479
480 semsg(_(e_argument_already_declared_in_class_str),
481 aname);
482 break;
483 }
484 }
485 }
486 }
487 }
488
489 return success;
490}
491
492/*
Yegappan Lakshmananb1027282023-08-19 11:26:42 +0200493 * Update the interface class lookup table for the member index on the
494 * interface to the member index in the class implementing the interface.
495 * And a lookup table for the object method index on the interface
496 * to the object method index in the class implementing the interface.
497 * This is also used for updating the lookup table for the extended class
498 * hierarchy.
499 */
500 static int
501update_member_method_lookup_table(
502 class_T *ifcl,
503 class_T *cl,
504 garray_T *objmethods,
505 int pobj_method_offset,
506 int is_interface)
507{
508 if (ifcl == NULL)
509 return OK;
510
511 // Table for members.
512 itf2class_T *if2cl = alloc_clear(sizeof(itf2class_T)
513 + ifcl->class_obj_member_count * sizeof(int));
514 if (if2cl == NULL)
515 return FAIL;
516 if2cl->i2c_next = ifcl->class_itf2class;
517 ifcl->class_itf2class = if2cl;
518 if2cl->i2c_class = cl;
519 if2cl->i2c_is_method = FALSE;
520
521 for (int if_i = 0; if_i < ifcl->class_obj_member_count; ++if_i)
522 for (int cl_i = 0; cl_i < cl->class_obj_member_count; ++cl_i)
523 {
524 if (STRCMP(ifcl->class_obj_members[if_i].ocm_name,
525 cl->class_obj_members[cl_i].ocm_name) == 0)
526 {
527 int *table = (int *)(if2cl + 1);
528 table[if_i] = cl_i;
529 break;
530 }
531 }
532
533 // Table for methods.
534 if2cl = alloc_clear(sizeof(itf2class_T)
535 + ifcl->class_obj_method_count * sizeof(int));
536 if (if2cl == NULL)
537 return FAIL;
538 if2cl->i2c_next = ifcl->class_itf2class;
539 ifcl->class_itf2class = if2cl;
540 if2cl->i2c_class = cl;
541 if2cl->i2c_is_method = TRUE;
542
543 for (int if_i = 0; if_i < ifcl->class_obj_method_count; ++if_i)
544 {
545 int done = FALSE;
546 for (int cl_i = 0; cl_i < objmethods->ga_len; ++cl_i)
547 {
548 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
549 ((ufunc_T **)objmethods->ga_data)[cl_i]->uf_name)
550 == 0)
551 {
552 int *table = (int *)(if2cl + 1);
553 table[if_i] = cl_i;
554 done = TRUE;
555 break;
556 }
557 }
558
559 // extended class object method is not overridden by the child class.
560 // Keep the method declared in one of the parent classes in the
561 // lineage.
562 if (!done && !is_interface)
563 {
564 // If "ifcl" is not the immediate parent of "cl", then search in
565 // the intermediate parent classes.
566 if (cl->class_extends != ifcl)
567 {
568 class_T *parent = cl->class_extends;
569 int method_offset = objmethods->ga_len;
570
571 while (!done && parent != NULL && parent != ifcl)
572 {
573
574 for (int cl_i = 0;
575 cl_i < parent->class_obj_method_count_child; ++cl_i)
576 {
577 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
578 parent->class_obj_methods[cl_i]->uf_name)
579 == 0)
580 {
581 int *table = (int *)(if2cl + 1);
582 table[if_i] = method_offset + cl_i;
583 done = TRUE;
584 break;
585 }
586 }
587 method_offset += parent->class_obj_method_count_child;
588 parent = parent->class_extends;
589 }
590 }
591
592 if (!done)
593 {
594 int *table = (int *)(if2cl + 1);
595 table[if_i] = pobj_method_offset + if_i;
596 }
597 }
598 }
599
600 return OK;
601}
602
603/*
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200604 * Update the member and object method lookup tables for a new class in the
605 * interface class.
606 * For each interface add a lookup table for the member index on the interface
607 * to the member index in the new class. And a lookup table for the object
608 * method index on the interface to the object method index in the new class.
609 */
610 static int
611add_lookup_tables(class_T *cl, class_T *extends_cl, garray_T *objmethods_gap)
612{
613 for (int i = 0; i < cl->class_interface_count; ++i)
614 {
615 class_T *ifcl = cl->class_interfaces_cl[i];
616
617 if (update_member_method_lookup_table(ifcl, cl, objmethods_gap,
618 0, TRUE) == FAIL)
619 return FAIL;
620 }
621
622 // Update the lookup table for the extended class, if nay
623 if (extends_cl != NULL)
624 {
625 class_T *pclass = extends_cl;
626 int pobj_method_offset = objmethods_gap->ga_len;
627
628 // Update the entire lineage of extended classes.
629 while (pclass != NULL)
630 {
631 if (update_member_method_lookup_table(pclass, cl,
632 objmethods_gap, pobj_method_offset, FALSE) == FAIL)
633 return FAIL;
634
635 pobj_method_offset += pclass->class_obj_method_count_child;
636 pclass = pclass->class_extends;
637 }
638 }
639
640 return OK;
641}
642
643/*
644 * Add class members to a new class. Allocate a typval for each class member
645 * and initialize it.
646 */
647 static void
648add_class_members(class_T *cl, exarg_T *eap)
649{
650 // Allocate a typval for each class member and initialize it.
651 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
652 cl->class_class_member_count);
653 if (cl->class_members_tv == NULL)
654 return;
655
656 for (int i = 0; i < cl->class_class_member_count; ++i)
657 {
658 ocmember_T *m = &cl->class_class_members[i];
659 typval_T *tv = &cl->class_members_tv[i];
660 if (m->ocm_init != NULL)
661 {
662 typval_T *etv = eval_expr(m->ocm_init, eap);
663 if (etv != NULL)
664 {
665 *tv = *etv;
666 vim_free(etv);
667 }
668 }
669 else
670 {
671 // TODO: proper default value
672 tv->v_type = m->ocm_type->tt_type;
673 tv->vval.v_string = NULL;
674 }
675 }
676}
677
678/*
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +0200679 * Add a default constructor method (new()) to the class "cl".
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200680 */
681 static void
682add_default_constructor(
683 class_T *cl,
684 garray_T *classfunctions_gap,
685 garray_T *type_list_gap)
686{
687 garray_T fga;
688
689 ga_init2(&fga, 1, 1000);
690 ga_concat(&fga, (char_u *)"new(");
691 for (int i = 0; i < cl->class_obj_member_count; ++i)
692 {
693 if (i > 0)
694 ga_concat(&fga, (char_u *)", ");
695 ga_concat(&fga, (char_u *)"this.");
696 ocmember_T *m = cl->class_obj_members + i;
697 ga_concat(&fga, (char_u *)m->ocm_name);
698 ga_concat(&fga, (char_u *)" = v:none");
699 }
700 ga_concat(&fga, (char_u *)")\nenddef\n");
701 ga_append(&fga, NUL);
702
703 exarg_T fea;
704 CLEAR_FIELD(fea);
705 fea.cmdidx = CMD_def;
706 fea.cmd = fea.arg = fga.ga_data;
707
708 garray_T lines_to_free;
709 ga_init2(&lines_to_free, sizeof(char_u *), 50);
710
711 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS);
712
713 ga_clear_strings(&lines_to_free);
714 vim_free(fga.ga_data);
715
716 if (nf != NULL && ga_grow(classfunctions_gap, 1) == OK)
717 {
718 ((ufunc_T **)classfunctions_gap->ga_data)[classfunctions_gap->ga_len]
719 = nf;
720 ++classfunctions_gap->ga_len;
721
722 nf->uf_flags |= FC_NEW;
723 nf->uf_ret_type = get_type_ptr(type_list_gap);
724 if (nf->uf_ret_type != NULL)
725 {
726 nf->uf_ret_type->tt_type = VAR_OBJECT;
727 nf->uf_ret_type->tt_class = cl;
728 nf->uf_ret_type->tt_argcount = 0;
729 nf->uf_ret_type->tt_args = NULL;
730 }
731 }
732}
733
734/*
735 * Add the class functions and object methods to the new class "cl".
736 * When extending a class, add the functions and methods from the parent class
737 * also.
738 */
739 static int
740add_classfuncs_objmethods(
741 class_T *cl,
742 class_T *extends_cl,
743 garray_T *classfunctions_gap,
744 garray_T *objmethods_gap)
745{
746 // loop 1: class functions, loop 2: object methods
747 for (int loop = 1; loop <= 2; ++loop)
748 {
749 garray_T *gap = loop == 1 ? classfunctions_gap : objmethods_gap;
750 int *fcount = loop == 1 ? &cl->class_class_function_count
751 : &cl->class_obj_method_count;
752 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
753 : &cl->class_obj_methods;
754
755 int parent_count = 0;
756 if (extends_cl != NULL)
757 // Include functions from the parent.
758 parent_count = loop == 1
759 ? extends_cl->class_class_function_count
760 : extends_cl->class_obj_method_count;
761
762 *fcount = parent_count + gap->ga_len;
763 if (*fcount == 0)
764 {
765 *fup = NULL;
766 continue;
767 }
768 *fup = ALLOC_MULT(ufunc_T *, *fcount);
769 if (*fup == NULL)
770 return FAIL;
771
772 if (gap->ga_len != 0)
773 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
774 vim_free(gap->ga_data);
775 if (loop == 1)
776 cl->class_class_function_count_child = gap->ga_len;
777 else
778 cl->class_obj_method_count_child = gap->ga_len;
779
780 int skipped = 0;
781 for (int i = 0; i < parent_count; ++i)
782 {
783 // Copy functions from the parent. Can't use the same
784 // function, because "uf_class" is different and compilation
785 // will have a different result.
786 // Put them after the functions in the current class, object
787 // methods may be overruled, then "super.Method()" is used to
788 // find a method from the parent.
789 // Skip "new" functions. TODO: not all of them.
790 if (loop == 1 && STRNCMP(
791 extends_cl->class_class_functions[i]->uf_name,
792 "new", 3) == 0)
793 ++skipped;
794 else
795 {
796 ufunc_T *pf = (loop == 1
797 ? extends_cl->class_class_functions
798 : extends_cl->class_obj_methods)[i];
799 (*fup)[gap->ga_len + i - skipped] = copy_function(pf);
800
801 // If the child class overrides a function from the parent
802 // the signature must be equal.
803 char_u *pname = pf->uf_name;
804 for (int ci = 0; ci < gap->ga_len; ++ci)
805 {
806 ufunc_T *cf = (*fup)[ci];
807 char_u *cname = cf->uf_name;
808 if (STRCMP(pname, cname) == 0)
809 {
810 where_T where = WHERE_INIT;
811 where.wt_func_name = (char *)pname;
812 where.wt_kind = WT_METHOD;
813 (void)check_type(pf->uf_func_type, cf->uf_func_type,
814 TRUE, where);
815 }
816 }
817 }
818 }
819
820 *fcount -= skipped;
821
822 // Set the class pointer on all the functions and object methods.
823 for (int i = 0; i < *fcount; ++i)
824 {
825 ufunc_T *fp = (*fup)[i];
826 fp->uf_class = cl;
827 if (loop == 2)
828 fp->uf_flags |= FC_OBJECT;
829 }
830 }
831
832 return OK;
833}
834
835/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000836 * Handle ":class" and ":abstract class" up to ":endclass".
Bram Moolenaar554d0312023-01-05 19:59:18 +0000837 * Handle ":interface" up to ":endinterface".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000838 */
839 void
840ex_class(exarg_T *eap)
841{
Bram Moolenaar83ae6152023-02-25 19:59:31 +0000842 int is_class = eap->cmdidx == CMD_class; // FALSE for :interface
843 long start_lnum = SOURCING_LNUM;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000844
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000845 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000846 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000847 if (is_abstract)
848 {
849 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
850 {
851 semsg(_(e_invalid_argument_str), arg);
852 return;
853 }
854 arg = skipwhite(arg + 5);
Bram Moolenaar24a8d062023-01-14 13:12:06 +0000855 is_class = TRUE;
856 }
857
858 if (!current_script_is_vim9()
859 || (cmdmod.cmod_flags & CMOD_LEGACY)
860 || !getline_equal(eap->getline, eap->cookie, getsourceline))
861 {
862 if (is_class)
863 emsg(_(e_class_can_only_be_defined_in_vim9_script));
864 else
865 emsg(_(e_interface_can_only_be_defined_in_vim9_script));
866 return;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000867 }
868
869 if (!ASCII_ISUPPER(*arg))
870 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000871 if (is_class)
872 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
873 else
874 semsg(_(e_interface_name_must_start_with_uppercase_letter_str),
875 arg);
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000876 return;
877 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000878 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
879 if (!IS_WHITE_OR_NUL(*name_end))
880 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000881 semsg(_(e_white_space_required_after_name_str), arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000882 return;
883 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000884 char_u *name_start = arg;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000885
Bram Moolenaara86655a2023-01-12 17:06:27 +0000886 // "export class" gets used when creating the class, don't use "is_export"
887 // for the items inside the class.
888 int class_export = is_export;
889 is_export = FALSE;
890
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000891 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000892 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000893
Bram Moolenaar83677162023-01-08 19:54:10 +0000894 // Name for "extends BaseClass"
895 char_u *extends = NULL;
896
Bram Moolenaar94674f22023-01-06 18:42:20 +0000897 // Names for "implements SomeInterface"
898 garray_T ga_impl;
899 ga_init2(&ga_impl, sizeof(char_u *), 5);
900
901 arg = skipwhite(name_end);
902 while (*arg != NUL && *arg != '#' && *arg != '\n')
903 {
904 // TODO:
Bram Moolenaar94674f22023-01-06 18:42:20 +0000905 // specifies SomeInterface
Bram Moolenaar83677162023-01-08 19:54:10 +0000906 if (STRNCMP(arg, "extends", 7) == 0 && IS_WHITE_OR_NUL(arg[7]))
907 {
908 if (extends != NULL)
909 {
910 emsg(_(e_duplicate_extends));
911 goto early_ret;
912 }
913 arg = skipwhite(arg + 7);
914 char_u *end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
915 if (!IS_WHITE_OR_NUL(*end))
916 {
917 semsg(_(e_white_space_required_after_name_str), arg);
918 goto early_ret;
919 }
920 extends = vim_strnsave(arg, end - arg);
921 if (extends == NULL)
922 goto early_ret;
923
924 arg = skipwhite(end + 1);
925 }
926 else if (STRNCMP(arg, "implements", 10) == 0
927 && IS_WHITE_OR_NUL(arg[10]))
Bram Moolenaar94674f22023-01-06 18:42:20 +0000928 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000929 if (ga_impl.ga_len > 0)
930 {
931 emsg(_(e_duplicate_implements));
932 goto early_ret;
933 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000934 arg = skipwhite(arg + 10);
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000935
936 for (;;)
Bram Moolenaar94674f22023-01-06 18:42:20 +0000937 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +0000938 char_u *impl_end = find_name_end(arg, NULL, NULL,
939 FNE_CHECK_START);
940 if (!IS_WHITE_OR_NUL(*impl_end) && *impl_end != ',')
941 {
942 semsg(_(e_white_space_required_after_name_str), arg);
943 goto early_ret;
944 }
945 char_u *iname = vim_strnsave(arg, impl_end - arg);
946 if (iname == NULL)
947 goto early_ret;
948 for (int i = 0; i < ga_impl.ga_len; ++i)
949 if (STRCMP(((char_u **)ga_impl.ga_data)[i], iname) == 0)
950 {
951 semsg(_(e_duplicate_interface_after_implements_str),
952 iname);
953 vim_free(iname);
954 goto early_ret;
955 }
956 if (ga_add_string(&ga_impl, iname) == FAIL)
957 {
958 vim_free(iname);
959 goto early_ret;
960 }
961 if (*impl_end != ',')
962 {
963 arg = skipwhite(impl_end);
964 break;
965 }
966 arg = skipwhite(impl_end + 1);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000967 }
Bram Moolenaar94674f22023-01-06 18:42:20 +0000968 }
969 else
970 {
971 semsg(_(e_trailing_characters_str), arg);
972early_ret:
Bram Moolenaar83677162023-01-08 19:54:10 +0000973 vim_free(extends);
Bram Moolenaar94674f22023-01-06 18:42:20 +0000974 ga_clear_strings(&ga_impl);
975 return;
976 }
977 }
978
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000979 garray_T type_list; // list of pointers to allocated types
980 ga_init2(&type_list, sizeof(type_T *), 10);
981
Bram Moolenaard505d172022-12-18 21:42:55 +0000982 // Growarray with class members declared in the class.
983 garray_T classmembers;
984 ga_init2(&classmembers, sizeof(ocmember_T), 10);
985
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000986 // Growarray with functions declared in the class.
987 garray_T classfunctions;
988 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +0000989
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000990 // Growarray with object members declared in the class.
991 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +0000992 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000993
994 // Growarray with object methods declared in the class.
995 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000996 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000997
998 /*
Bram Moolenaar554d0312023-01-05 19:59:18 +0000999 * Go over the body of the class/interface until "endclass" or
1000 * "endinterface" is found.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001001 */
1002 char_u *theline = NULL;
1003 int success = FALSE;
1004 for (;;)
1005 {
1006 vim_free(theline);
1007 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
1008 if (theline == NULL)
1009 break;
1010 char_u *line = skipwhite(theline);
1011
Bram Moolenaar418b5472022-12-20 13:38:22 +00001012 // Skip empty and comment lines.
1013 if (*line == NUL)
1014 continue;
1015 if (*line == '#')
1016 {
1017 if (vim9_bad_comment(line))
1018 break;
1019 continue;
1020 }
1021
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001022 char_u *p = line;
Bram Moolenaar554d0312023-01-05 19:59:18 +00001023 char *end_name = is_class ? "endclass" : "endinterface";
1024 if (checkforcmd(&p, end_name, is_class ? 4 : 5))
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001025 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001026 if (STRNCMP(line, end_name, is_class ? 8 : 12) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001027 semsg(_(e_command_cannot_be_shortened_str), line);
1028 else if (*p == '|' || !ends_excmd2(line, p))
1029 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +00001030 else
1031 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001032 break;
1033 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00001034 char *wrong_name = is_class ? "endinterface" : "endclass";
1035 if (checkforcmd(&p, wrong_name, is_class ? 5 : 4))
1036 {
Bram Moolenaar657aea72023-01-27 13:16:19 +00001037 semsg(_(e_invalid_command_str_expected_str), line, end_name);
Bram Moolenaar554d0312023-01-05 19:59:18 +00001038 break;
1039 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001040
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001041 int has_public = FALSE;
1042 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001043 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001044 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001045 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001046 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001047 break;
1048 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001049 has_public = TRUE;
1050 p = skipwhite(line + 6);
1051
Bram Moolenaard505d172022-12-18 21:42:55 +00001052 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001053 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001054 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001055 break;
1056 }
1057 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001058
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001059 int has_static = FALSE;
1060 char_u *ps = p;
1061 if (checkforcmd(&p, "static", 4))
1062 {
1063 if (STRNCMP(ps, "static", 6) != 0)
1064 {
1065 semsg(_(e_command_cannot_be_shortened_str), ps);
1066 break;
1067 }
1068 has_static = TRUE;
1069 p = skipwhite(ps + 6);
1070 }
1071
Bram Moolenaard505d172022-12-18 21:42:55 +00001072 // object members (public, read access, private):
1073 // "this._varname"
1074 // "this.varname"
1075 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001076 if (STRNCMP(p, "this", 4) == 0)
1077 {
1078 if (p[4] != '.' || !eval_isnamec1(p[5]))
1079 {
1080 semsg(_(e_invalid_object_member_declaration_str), p);
1081 break;
1082 }
1083 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +00001084 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +00001085 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +00001086 char_u *init_expr = NULL;
1087 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +00001088 &varname_end, &type_list, &type,
1089 is_class ? &init_expr: NULL) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +00001090 break;
1091 if (add_member(&objmembers, varname, varname_end,
1092 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +00001093 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001094 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001095 break;
1096 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001097 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001098
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001099 // constructors:
1100 // def new()
1101 // enddef
1102 // def newOther()
1103 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001104 // object methods and class functions:
1105 // def SomeMethod()
1106 // enddef
1107 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001108 // enddef
1109 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001110 // def <Tval> someMethod()
1111 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001112 else if (checkforcmd(&p, "def", 3))
1113 {
1114 exarg_T ea;
1115 garray_T lines_to_free;
1116
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001117 // TODO: error for "public static def Func()"?
1118
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001119 CLEAR_FIELD(ea);
1120 ea.cmd = line;
1121 ea.arg = p;
1122 ea.cmdidx = CMD_def;
1123 ea.getline = eap->getline;
1124 ea.cookie = eap->cookie;
1125
1126 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +00001127 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free,
1128 is_class ? CF_CLASS : CF_INTERFACE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001129 ga_clear_strings(&lines_to_free);
1130
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001131 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001132 {
Bram Moolenaar58b40092023-01-11 15:59:05 +00001133 char_u *name = uf->uf_name;
1134 int is_new = STRNCMP(name, "new", 3) == 0;
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001135 if (is_new && is_abstract)
1136 {
1137 emsg(_(e_cannot_define_new_function_in_abstract_class));
1138 success = FALSE;
Yegappan Lakshmananb1027282023-08-19 11:26:42 +02001139 func_clear_free(uf, FALSE);
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001140 break;
1141 }
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001142 if (is_new)
1143 {
1144 // A return type should not be specified for the new()
1145 // constructor method.
1146 if (uf->uf_ret_type->tt_type != VAR_VOID)
1147 {
1148 emsg(_(e_cannot_use_a_return_type_with_new));
1149 success = FALSE;
1150 func_clear_free(uf, FALSE);
1151 break;
1152 }
1153 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001154 garray_T *fgap = has_static || is_new
1155 ? &classfunctions : &objmethods;
Bram Moolenaar58b40092023-01-11 15:59:05 +00001156 // Check the name isn't used already.
1157 for (int i = 0; i < fgap->ga_len; ++i)
1158 {
1159 char_u *n = ((ufunc_T **)fgap->ga_data)[i]->uf_name;
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02001160 char_u *pstr = *name == '_' ? name + 1 : name;
1161 char_u *qstr = *n == '_' ? n + 1 : n;
1162 if (STRCMP(pstr, qstr) == 0)
Bram Moolenaar58b40092023-01-11 15:59:05 +00001163 {
1164 semsg(_(e_duplicate_function_str), name);
1165 break;
1166 }
1167 }
1168
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001169 if (ga_grow(fgap, 1) == OK)
1170 {
1171 if (is_new)
1172 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001173
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02001174 // If the method name starts with '_', then it a private
1175 // method.
1176 if (*name == '_')
1177 uf->uf_private = TRUE;
1178
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001179 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
1180 ++fgap->ga_len;
1181 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001182 }
1183 }
1184
1185 // class members
1186 else if (has_static)
1187 {
1188 // class members (public, read access, private):
1189 // "static _varname"
1190 // "static varname"
1191 // "public static varname"
1192 char_u *varname = p;
1193 char_u *varname_end = NULL;
1194 type_T *type = NULL;
1195 char_u *init_expr = NULL;
1196 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +00001197 &varname_end, &type_list, &type,
1198 is_class ? &init_expr : NULL) == FAIL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001199 break;
1200 if (add_member(&classmembers, varname, varname_end,
1201 has_public, type, init_expr) == FAIL)
1202 {
1203 vim_free(init_expr);
1204 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001205 }
1206 }
1207
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001208 else
1209 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001210 if (is_class)
1211 semsg(_(e_not_valid_command_in_class_str), line);
1212 else
1213 semsg(_(e_not_valid_command_in_interface_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001214 break;
1215 }
1216 }
1217 vim_free(theline);
1218
Bram Moolenaar83677162023-01-08 19:54:10 +00001219 class_T *extends_cl = NULL; // class from "extends" argument
1220
1221 /*
1222 * Check a few things before defining the class.
1223 */
1224
1225 // Check the "extends" class is valid.
1226 if (success && extends != NULL)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001227 success = validate_extends_class(extends, &extends_cl);
Bram Moolenaar83677162023-01-08 19:54:10 +00001228 VIM_CLEAR(extends);
1229
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001230 class_T **intf_classes = NULL;
1231
Bram Moolenaar83677162023-01-08 19:54:10 +00001232 // Check all "implements" entries are valid.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001233 if (success && ga_impl.ga_len > 0)
1234 {
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001235 intf_classes = ALLOC_CLEAR_MULT(class_T *, ga_impl.ga_len);
1236
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001237 success = validate_implements_classes(&ga_impl, intf_classes,
1238 &classfunctions, &classmembers,
1239 &objmethods, &objmembers);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001240 }
1241
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001242 // Check no function argument name is used as a class member.
Bram Moolenaard40f00c2023-01-13 17:36:49 +00001243 if (success)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001244 success = check_func_arg_names(&classfunctions, &objmethods,
1245 &classmembers);
Bram Moolenaard40f00c2023-01-13 17:36:49 +00001246
Bram Moolenaareb533502022-12-14 15:06:11 +00001247 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001248 if (success)
1249 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001250 // "endclass" encountered without failures: Create the class.
1251
Bram Moolenaareb533502022-12-14 15:06:11 +00001252 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001253 if (cl == NULL)
1254 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +00001255 if (!is_class)
1256 cl->class_flags = CLASS_INTERFACE;
1257
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001258 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +00001259 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +00001260 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001261 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +00001262
Bram Moolenaard0200c82023-01-28 15:19:40 +00001263 if (extends_cl != NULL)
1264 {
1265 cl->class_extends = extends_cl;
1266 extends_cl->class_flags |= CLASS_EXTENDED;
1267 }
Bram Moolenaar83677162023-01-08 19:54:10 +00001268
Bram Moolenaard505d172022-12-18 21:42:55 +00001269 // Add class and object members to "cl".
1270 if (add_members_to_class(&classmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +00001271 extends_cl == NULL ? NULL
1272 : extends_cl->class_class_members,
1273 extends_cl == NULL ? 0
1274 : extends_cl->class_class_member_count,
1275 &cl->class_class_members,
1276 &cl->class_class_member_count) == FAIL
Bram Moolenaard505d172022-12-18 21:42:55 +00001277 || add_members_to_class(&objmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +00001278 extends_cl == NULL ? NULL
1279 : extends_cl->class_obj_members,
1280 extends_cl == NULL ? 0
1281 : extends_cl->class_obj_member_count,
1282 &cl->class_obj_members,
1283 &cl->class_obj_member_count) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +00001284 goto cleanup;
1285
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001286 if (ga_impl.ga_len > 0)
1287 {
1288 // Move the "implements" names into the class.
1289 cl->class_interface_count = ga_impl.ga_len;
1290 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
1291 if (cl->class_interfaces == NULL)
1292 goto cleanup;
1293 for (int i = 0; i < ga_impl.ga_len; ++i)
1294 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
1295 VIM_CLEAR(ga_impl.ga_data);
1296 ga_impl.ga_len = 0;
1297
Bram Moolenaard0200c82023-01-28 15:19:40 +00001298 cl->class_interfaces_cl = intf_classes;
1299 intf_classes = NULL;
1300 }
1301
1302 if (cl->class_interface_count > 0 || extends_cl != NULL)
1303 {
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001304 // Add a method and member lookup table to each of the interface
1305 // classes.
1306 if (add_lookup_tables(cl, extends_cl, &objmethods) == FAIL)
1307 goto cleanup;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001308 }
1309
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001310 // Allocate a typval for each class member and initialize it.
Bram Moolenaar554d0312023-01-05 19:59:18 +00001311 if (is_class && cl->class_class_member_count > 0)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001312 add_class_members(cl, eap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001313
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001314 int have_new = FALSE;
1315 ufunc_T *class_func = NULL;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001316 for (int i = 0; i < classfunctions.ga_len; ++i)
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001317 {
1318 class_func = ((ufunc_T **)classfunctions.ga_data)[i];
1319 if (STRCMP(class_func->uf_name, "new") == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001320 {
1321 have_new = TRUE;
1322 break;
1323 }
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001324 }
1325
1326 if (have_new)
1327 // The return type of new() is an object of class "cl"
1328 class_func->uf_ret_type->tt_class = cl;
1329 else if (is_class && !is_abstract && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001330 // No new() method was defined, add the default constructor.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001331 add_default_constructor(cl, &classfunctions, &type_list);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001332
Bram Moolenaar58b40092023-01-11 15:59:05 +00001333 // Move all the functions into the created class.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001334 if (add_classfuncs_objmethods(cl, extends_cl, &classfunctions,
1335 &objmethods) == FAIL)
1336 goto cleanup;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001337
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001338 cl->class_type.tt_type = VAR_CLASS;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001339 cl->class_type.tt_class = cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001340 cl->class_object_type.tt_type = VAR_OBJECT;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001341 cl->class_object_type.tt_class = cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001342 cl->class_type_list = type_list;
1343
1344 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +00001345 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001346
1347 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001348 // TODO: handle other context, e.g. in a function
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001349 typval_T tv;
1350 tv.v_type = VAR_CLASS;
1351 tv.vval.v_class = cl;
Bram Moolenaara86655a2023-01-12 17:06:27 +00001352 is_export = class_export;
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001353 SOURCING_LNUM = start_lnum;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001354 set_var_const(cl->class_name, current_sctx.sc_sid,
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001355 NULL, &tv, FALSE, 0, 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001356 return;
1357 }
1358
1359cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +00001360 if (cl != NULL)
1361 {
1362 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001363 vim_free(cl->class_class_functions);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001364 if (cl->class_interfaces != NULL)
1365 {
1366 for (int i = 0; i < cl->class_interface_count; ++i)
1367 vim_free(cl->class_interfaces[i]);
1368 vim_free(cl->class_interfaces);
1369 }
1370 if (cl->class_interfaces_cl != NULL)
1371 {
1372 for (int i = 0; i < cl->class_interface_count; ++i)
1373 class_unref(cl->class_interfaces_cl[i]);
1374 vim_free(cl->class_interfaces_cl);
1375 }
Bram Moolenaareb533502022-12-14 15:06:11 +00001376 vim_free(cl->class_obj_members);
1377 vim_free(cl->class_obj_methods);
1378 vim_free(cl);
1379 }
1380
Bram Moolenaar83677162023-01-08 19:54:10 +00001381 vim_free(extends);
1382 class_unref(extends_cl);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001383
1384 if (intf_classes != NULL)
1385 {
1386 for (int i = 0; i < ga_impl.ga_len; ++i)
1387 class_unref(intf_classes[i]);
1388 vim_free(intf_classes);
1389 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001390 ga_clear_strings(&ga_impl);
1391
Bram Moolenaard505d172022-12-18 21:42:55 +00001392 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001393 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001394 garray_T *gap = round == 1 ? &classmembers : &objmembers;
1395 if (gap->ga_len == 0 || gap->ga_data == NULL)
1396 continue;
1397
1398 for (int i = 0; i < gap->ga_len; ++i)
1399 {
1400 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
1401 vim_free(m->ocm_name);
1402 vim_free(m->ocm_init);
1403 }
1404 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001405 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001406
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001407 for (int i = 0; i < objmethods.ga_len; ++i)
1408 {
1409 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
1410 func_clear_free(uf, FALSE);
1411 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001412 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001413
1414 for (int i = 0; i < classfunctions.ga_len; ++i)
1415 {
1416 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
1417 func_clear_free(uf, FALSE);
1418 }
1419 ga_clear(&classfunctions);
1420
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001421 clear_type_list(&type_list);
1422}
1423
1424/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001425 * Find member "name" in class "cl", set "member_idx" to the member index and
1426 * return its type.
1427 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001428 */
1429 type_T *
1430class_member_type(
1431 class_T *cl,
1432 char_u *name,
1433 char_u *name_end,
1434 int *member_idx)
1435{
1436 *member_idx = -1; // not found (yet)
1437 size_t len = name_end - name;
1438
1439 for (int i = 0; i < cl->class_obj_member_count; ++i)
1440 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001441 ocmember_T *m = cl->class_obj_members + i;
1442 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001443 {
1444 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001445 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001446 }
1447 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001448
1449 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001450 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001451}
1452
1453/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001454 * Handle ":enum" up to ":endenum".
1455 */
1456 void
1457ex_enum(exarg_T *eap UNUSED)
1458{
1459 // TODO
1460}
1461
1462/*
1463 * Handle ":type".
1464 */
1465 void
1466ex_type(exarg_T *eap UNUSED)
1467{
1468 // TODO
1469}
1470
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001471/*
1472 * Evaluate what comes after a class:
1473 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001474 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001475 * - class constructor: SomeClass.new()
1476 * - object member: someObject.varname
1477 * - object method: someObject.SomeMethod()
1478 *
1479 * "*arg" points to the '.'.
1480 * "*arg" is advanced to after the member name or method call.
1481 *
1482 * Returns FAIL or OK.
1483 */
1484 int
1485class_object_index(
1486 char_u **arg,
1487 typval_T *rettv,
1488 evalarg_T *evalarg,
1489 int verbose UNUSED) // give error messages
1490{
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001491 if (VIM_ISWHITE((*arg)[1]))
1492 {
1493 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
1494 return FAIL;
1495 }
1496
1497 ++*arg;
1498 char_u *name = *arg;
1499 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1500 if (name_end == name)
1501 return FAIL;
1502 size_t len = name_end - name;
1503
Bram Moolenaar552bdca2023-02-17 21:08:50 +00001504 class_T *cl;
1505 if (rettv->v_type == VAR_CLASS)
1506 cl = rettv->vval.v_class;
1507 else // VAR_OBJECT
1508 {
1509 if (rettv->vval.v_object == NULL)
1510 {
1511 emsg(_(e_using_null_object));
1512 return FAIL;
1513 }
1514 cl = rettv->vval.v_object->obj_class;
1515 }
1516
Bram Moolenaard13dd302023-03-11 20:56:35 +00001517 if (cl == NULL)
1518 {
1519 emsg(_(e_incomplete_type));
1520 return FAIL;
1521 }
1522
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001523 if (*name_end == '(')
1524 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001525 int on_class = rettv->v_type == VAR_CLASS;
1526 int count = on_class ? cl->class_class_function_count
1527 : cl->class_obj_method_count;
1528 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001529 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001530 ufunc_T *fp = on_class ? cl->class_class_functions[i]
1531 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +00001532 // Use a separate pointer to avoid that ASAN complains about
1533 // uf_name[] only being 4 characters.
1534 char_u *ufname = (char_u *)fp->uf_name;
1535 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001536 {
1537 typval_T argvars[MAX_FUNC_ARGS + 1];
1538 int argcount = 0;
1539
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02001540 if (fp->uf_private)
1541 {
1542 // Cannot access a private method outside of a class
1543 semsg(_(e_cannot_access_private_method_str), name);
1544 return FAIL;
1545 }
1546
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001547 char_u *argp = name_end;
1548 int ret = get_func_arguments(&argp, evalarg, 0,
1549 argvars, &argcount);
1550 if (ret == FAIL)
1551 return FAIL;
1552
1553 funcexe_T funcexe;
1554 CLEAR_FIELD(funcexe);
1555 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001556 if (rettv->v_type == VAR_OBJECT)
1557 {
1558 funcexe.fe_object = rettv->vval.v_object;
1559 ++funcexe.fe_object->obj_refcount;
1560 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001561
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001562 // Clear the class or object after calling the function, in
1563 // case the refcount is one.
1564 typval_T tv_tofree = *rettv;
1565 rettv->v_type = VAR_UNKNOWN;
1566
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001567 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001568 int error = call_user_func_check(fp, argcount, argvars,
1569 rettv, &funcexe, NULL);
1570
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001571 // Clear the previous rettv and the arguments.
1572 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001573 for (int idx = 0; idx < argcount; ++idx)
1574 clear_tv(&argvars[idx]);
1575
1576 if (error != FCERR_NONE)
1577 {
1578 user_func_error(error, printable_func_name(fp),
1579 funcexe.fe_found_var);
1580 return FAIL;
1581 }
1582 *arg = argp;
1583 return OK;
1584 }
1585 }
1586
1587 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
1588 }
1589
1590 else if (rettv->v_type == VAR_OBJECT)
1591 {
1592 for (int i = 0; i < cl->class_obj_member_count; ++i)
1593 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001594 ocmember_T *m = &cl->class_obj_members[i];
1595 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001596 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001597 if (*name == '_')
1598 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001599 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001600 return FAIL;
1601 }
1602
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001603 // The object only contains a pointer to the class, the member
1604 // values array follows right after that.
1605 object_T *obj = rettv->vval.v_object;
1606 typval_T *tv = (typval_T *)(obj + 1) + i;
1607 copy_tv(tv, rettv);
1608 object_unref(obj);
1609
1610 *arg = name_end;
1611 return OK;
1612 }
1613 }
1614
1615 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
1616 }
1617
Bram Moolenaard505d172022-12-18 21:42:55 +00001618 else if (rettv->v_type == VAR_CLASS)
1619 {
1620 // class member
1621 for (int i = 0; i < cl->class_class_member_count; ++i)
1622 {
1623 ocmember_T *m = &cl->class_class_members[i];
1624 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
1625 {
1626 if (*name == '_')
1627 {
1628 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
1629 return FAIL;
1630 }
1631
1632 typval_T *tv = &cl->class_members_tv[i];
1633 copy_tv(tv, rettv);
1634 class_unref(cl);
1635
1636 *arg = name_end;
1637 return OK;
1638 }
1639 }
1640
1641 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
1642 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001643
1644 return FAIL;
1645}
1646
1647/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001648 * If "arg" points to a class or object method, return it.
1649 * Otherwise return NULL.
1650 */
1651 ufunc_T *
1652find_class_func(char_u **arg)
1653{
1654 char_u *name = *arg;
1655 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
1656 if (name_end == name || *name_end != '.')
1657 return NULL;
1658
1659 size_t len = name_end - name;
1660 typval_T tv;
1661 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00001662 if (eval_variable(name, (int)len,
1663 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001664 return NULL;
1665 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00001666 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001667
1668 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
1669 : tv.vval.v_object->obj_class;
1670 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001671 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001672 char_u *fname = name_end + 1;
1673 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
1674 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00001675 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001676 len = fname_end - fname;
1677
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001678 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
1679 : cl->class_obj_method_count;
1680 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
1681 : cl->class_obj_methods;
1682 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001683 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001684 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001685 // Use a separate pointer to avoid that ASAN complains about
1686 // uf_name[] only being 4 characters.
1687 char_u *ufname = (char_u *)fp->uf_name;
1688 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +00001689 {
1690 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001691 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +00001692 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001693 }
1694
Bram Moolenaareb533502022-12-14 15:06:11 +00001695fail_after_eval:
1696 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001697 return NULL;
1698}
1699
1700/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001701 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
1702 * index in class.class_class_members[].
1703 * If "cl_ret" is not NULL set it to the class.
1704 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001705 */
1706 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001707class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001708{
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001709 if (cctx == NULL || cctx->ctx_ufunc == NULL
1710 || cctx->ctx_ufunc->uf_class == NULL)
1711 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001712 class_T *cl = cctx->ctx_ufunc->uf_class;
1713
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001714 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001715 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001716 ocmember_T *m = &cl->class_class_members[i];
1717 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001718 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001719 if (cl_ret != NULL)
1720 *cl_ret = cl;
1721 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001722 }
1723 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001724 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001725}
1726
1727/*
Bram Moolenaar62a69232023-01-24 15:07:04 +00001728 * Return TRUE if current context "cctx_arg" is inside class "cl".
1729 * Return FALSE if not.
1730 */
1731 int
1732inside_class(cctx_T *cctx_arg, class_T *cl)
1733{
1734 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
1735 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class == cl)
1736 return TRUE;
1737 return FALSE;
1738}
1739
1740/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001741 * Make a copy of an object.
1742 */
1743 void
1744copy_object(typval_T *from, typval_T *to)
1745{
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02001746 if (from->vval.v_object == NULL)
1747 to->vval.v_object = NULL;
1748 else
1749 {
1750 to->vval.v_object = from->vval.v_object;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001751 ++to->vval.v_object->obj_refcount;
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02001752 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001753}
1754
1755/*
1756 * Free an object.
1757 */
1758 static void
1759object_clear(object_T *obj)
1760{
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001761 // Avoid a recursive call, it can happen if "obj" has a circular reference.
1762 obj->obj_refcount = INT_MAX;
1763
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001764 class_T *cl = obj->obj_class;
1765
Jia-Ju Bai5b0889b2023-08-13 20:04:04 +02001766 if (!cl)
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02001767 return;
Jia-Ju Bai5b0889b2023-08-13 20:04:04 +02001768
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001769 // the member values are just after the object structure
1770 typval_T *tv = (typval_T *)(obj + 1);
1771 for (int i = 0; i < cl->class_obj_member_count; ++i)
1772 clear_tv(tv + i);
1773
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001774 // Remove from the list headed by "first_object".
1775 object_cleared(obj);
1776
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001777 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001778 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001779}
1780
1781/*
1782 * Unreference an object.
1783 */
1784 void
1785object_unref(object_T *obj)
1786{
1787 if (obj != NULL && --obj->obj_refcount <= 0)
1788 object_clear(obj);
1789}
1790
1791/*
1792 * Make a copy of a class.
1793 */
1794 void
1795copy_class(typval_T *from, typval_T *to)
1796{
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02001797 if (from->vval.v_class == NULL)
1798 to->vval.v_class = NULL;
1799 else
1800 {
1801 to->vval.v_class = from->vval.v_class;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001802 ++to->vval.v_class->class_refcount;
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02001803 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001804}
1805
1806/*
1807 * Unreference a class. Free it when the reference count goes down to zero.
1808 */
1809 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001810class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001811{
Bram Moolenaard505d172022-12-18 21:42:55 +00001812 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001813 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001814 // Freeing what the class contains may recursively come back here.
1815 // Clear "class_name" first, if it is NULL the class does not need to
1816 // be freed.
1817 VIM_CLEAR(cl->class_name);
1818
Bram Moolenaar83677162023-01-08 19:54:10 +00001819 class_unref(cl->class_extends);
1820
Bram Moolenaar94674f22023-01-06 18:42:20 +00001821 for (int i = 0; i < cl->class_interface_count; ++i)
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001822 {
Bram Moolenaar94674f22023-01-06 18:42:20 +00001823 vim_free(((char_u **)cl->class_interfaces)[i]);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001824 if (cl->class_interfaces_cl[i] != NULL)
1825 class_unref(cl->class_interfaces_cl[i]);
1826 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001827 vim_free(cl->class_interfaces);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001828 vim_free(cl->class_interfaces_cl);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001829
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001830 itf2class_T *next;
1831 for (itf2class_T *i2c = cl->class_itf2class; i2c != NULL; i2c = next)
1832 {
1833 next = i2c->i2c_next;
1834 vim_free(i2c);
1835 }
1836
Bram Moolenaard505d172022-12-18 21:42:55 +00001837 for (int i = 0; i < cl->class_class_member_count; ++i)
1838 {
1839 ocmember_T *m = &cl->class_class_members[i];
1840 vim_free(m->ocm_name);
1841 vim_free(m->ocm_init);
1842 if (cl->class_members_tv != NULL)
1843 clear_tv(&cl->class_members_tv[i]);
1844 }
1845 vim_free(cl->class_class_members);
1846 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001847
1848 for (int i = 0; i < cl->class_obj_member_count; ++i)
1849 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001850 ocmember_T *m = &cl->class_obj_members[i];
1851 vim_free(m->ocm_name);
1852 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001853 }
1854 vim_free(cl->class_obj_members);
1855
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001856 for (int i = 0; i < cl->class_class_function_count; ++i)
1857 {
1858 ufunc_T *uf = cl->class_class_functions[i];
1859 func_clear_free(uf, FALSE);
1860 }
1861 vim_free(cl->class_class_functions);
1862
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001863 for (int i = 0; i < cl->class_obj_method_count; ++i)
1864 {
1865 ufunc_T *uf = cl->class_obj_methods[i];
1866 func_clear_free(uf, FALSE);
1867 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001868 vim_free(cl->class_obj_methods);
1869
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001870 clear_type_list(&cl->class_type_list);
1871
1872 vim_free(cl);
1873 }
1874}
1875
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001876static object_T *first_object = NULL;
1877
1878/*
1879 * Call this function when an object has been created. It will be added to the
1880 * list headed by "first_object".
1881 */
1882 void
1883object_created(object_T *obj)
1884{
1885 if (first_object != NULL)
1886 {
1887 obj->obj_next_used = first_object;
1888 first_object->obj_prev_used = obj;
1889 }
1890 first_object = obj;
1891}
1892
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001893static object_T *next_nonref_obj = NULL;
1894
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001895/*
1896 * Call this function when an object has been cleared and is about to be freed.
1897 * It is removed from the list headed by "first_object".
1898 */
1899 void
1900object_cleared(object_T *obj)
1901{
1902 if (obj->obj_next_used != NULL)
1903 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1904 if (obj->obj_prev_used != NULL)
1905 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1906 else if (first_object == obj)
1907 first_object = obj->obj_next_used;
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001908
1909 // update the next object to check if needed
1910 if (obj == next_nonref_obj)
1911 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001912}
1913
1914/*
1915 * Go through the list of all objects and free items without "copyID".
1916 */
1917 int
1918object_free_nonref(int copyID)
1919{
1920 int did_free = FALSE;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001921
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001922 for (object_T *obj = first_object; obj != NULL; obj = next_nonref_obj)
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001923 {
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001924 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001925 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1926 {
1927 // Free the object and items it contains.
1928 object_clear(obj);
1929 did_free = TRUE;
1930 }
1931 }
1932
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01001933 next_nonref_obj = NULL;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001934 return did_free;
1935}
1936
LemonBoyafe04662023-08-23 21:08:11 +02001937/*
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02001938 * Return TRUE when the class "cl", its base class or one of the implemented
1939 * interfaces matches the class "other_cl".
LemonBoyafe04662023-08-23 21:08:11 +02001940 */
1941 int
1942class_instance_of(class_T *cl, class_T *other_cl)
1943{
1944 if (cl == other_cl)
1945 return TRUE;
1946
1947 // Recursively check the base classes.
1948 for (; cl != NULL; cl = cl->class_extends)
1949 {
1950 if (cl == other_cl)
1951 return TRUE;
1952 // Check the implemented interfaces.
1953 for (int i = cl->class_interface_count - 1; i >= 0; --i)
1954 if (cl->class_interfaces_cl[i] == other_cl)
1955 return TRUE;
1956 }
1957
1958 return FALSE;
1959}
1960
1961/*
1962 * "instanceof(object, classinfo)" function
1963 */
1964 void
1965f_instanceof(typval_T *argvars, typval_T *rettv)
1966{
1967 typval_T *object_tv = &argvars[0];
1968 typval_T *classinfo_tv = &argvars[1];
1969 listitem_T *li;
1970
1971 rettv->vval.v_number = VVAL_FALSE;
1972
1973 if (check_for_object_arg(argvars, 0) == FAIL
1974 || check_for_class_or_list_arg(argvars, 1) == FAIL)
1975 return;
1976
1977 if (classinfo_tv->v_type == VAR_LIST)
1978 {
1979 FOR_ALL_LIST_ITEMS(classinfo_tv->vval.v_list, li)
1980 {
1981 if (li->li_tv.v_type != VAR_CLASS)
1982 {
1983 emsg(_(e_class_required));
1984 return;
1985 }
1986
1987 if (class_instance_of(object_tv->vval.v_object->obj_class,
1988 li->li_tv.vval.v_class) == TRUE)
1989 {
1990 rettv->vval.v_number = VVAL_TRUE;
1991 return;
1992 }
1993 }
1994 }
1995 else if (classinfo_tv->v_type == VAR_CLASS)
1996 {
1997 rettv->vval.v_number = class_instance_of(object_tv->vval.v_object->obj_class,
1998 classinfo_tv->vval.v_class);
1999 }
2000}
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00002001
2002#endif // FEAT_EVAL