blob: da862f26676060caf5f7b7f067397a5c2bbec44d [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
Yegappan Lakshmanane651e112023-09-04 07:51:01 +020024static class_T *first_class = NULL;
25static class_T *next_nonref_class = NULL;
26
27/*
28 * Call this function when a class has been created. It will be added to the
29 * list headed by "first_class".
30 */
31 static void
32class_created(class_T *cl)
33{
34 if (first_class != NULL)
35 {
36 cl->class_next_used = first_class;
37 first_class->class_prev_used = cl;
38 }
39 first_class = cl;
40}
41
42/*
43 * Call this function when a class has been cleared and is about to be freed.
44 * It is removed from the list headed by "first_class".
45 */
46 static void
47class_cleared(class_T *cl)
48{
49 if (cl->class_next_used != NULL)
50 cl->class_next_used->class_prev_used = cl->class_prev_used;
51 if (cl->class_prev_used != NULL)
52 cl->class_prev_used->class_next_used = cl->class_next_used;
53 else if (first_class == cl)
54 first_class = cl->class_next_used;
55
56 // update the next class to check if needed
57 if (cl == next_nonref_class)
58 next_nonref_class = cl->class_next_used;
59}
60
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000061/*
Bram Moolenaard505d172022-12-18 21:42:55 +000062 * Parse a member declaration, both object and class member.
63 * Returns OK or FAIL. When OK then "varname_end" is set to just after the
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +020064 * variable name and "type_ret" is set to the declared or detected type.
Bram Moolenaard505d172022-12-18 21:42:55 +000065 * "init_expr" is set to the initialisation expression (allocated), if there is
Bram Moolenaar554d0312023-01-05 19:59:18 +000066 * one. For an interface "init_expr" is NULL.
Bram Moolenaard505d172022-12-18 21:42:55 +000067 */
68 static int
69parse_member(
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +020070 exarg_T *eap,
71 char_u *line,
72 char_u *varname,
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +020073 int has_public, // TRUE if "public" seen before "varname"
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +020074 char_u **varname_end,
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +020075 garray_T *type_list,
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +020076 type_T **type_ret,
77 char_u **init_expr)
Bram Moolenaard505d172022-12-18 21:42:55 +000078{
79 *varname_end = to_name_end(varname, FALSE);
80 if (*varname == '_' && has_public)
81 {
82 semsg(_(e_public_member_name_cannot_start_with_underscore_str), line);
83 return FAIL;
84 }
85
86 char_u *colon = skipwhite(*varname_end);
87 char_u *type_arg = colon;
88 type_T *type = NULL;
89 if (*colon == ':')
90 {
91 if (VIM_ISWHITE(**varname_end))
92 {
93 semsg(_(e_no_white_space_allowed_before_colon_str), varname);
94 return FAIL;
95 }
96 if (!VIM_ISWHITE(colon[1]))
97 {
98 semsg(_(e_white_space_required_after_str_str), ":", varname);
99 return FAIL;
100 }
101 type_arg = skipwhite(colon + 1);
102 type = parse_type(&type_arg, type_list, TRUE);
103 if (type == NULL)
104 return FAIL;
105 }
106
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200107 char_u *init_arg = skipwhite(type_arg);
108 if (type == NULL && *init_arg != '=')
Bram Moolenaard505d172022-12-18 21:42:55 +0000109 {
110 emsg(_(e_type_or_initialization_required));
111 return FAIL;
112 }
113
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200114 if (init_expr == NULL && *init_arg == '=')
Bram Moolenaard505d172022-12-18 21:42:55 +0000115 {
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200116 emsg(_(e_cannot_initialize_member_in_interface));
117 return FAIL;
118 }
119
120 if (*init_arg == '=')
121 {
122 evalarg_T evalarg;
123 char_u *expr_start, *expr_end;
124
125 if (!VIM_ISWHITE(init_arg[-1]) || !VIM_ISWHITE(init_arg[1]))
Bram Moolenaard505d172022-12-18 21:42:55 +0000126 {
127 semsg(_(e_white_space_required_before_and_after_str_at_str),
128 "=", type_arg);
129 return FAIL;
130 }
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200131 init_arg = skipwhite(init_arg + 1);
Bram Moolenaard505d172022-12-18 21:42:55 +0000132
Bram Moolenaard505d172022-12-18 21:42:55 +0000133 fill_evalarg_from_eap(&evalarg, eap, FALSE);
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200134 (void)skip_expr_concatenate(&init_arg, &expr_start, &expr_end, &evalarg);
Bram Moolenaard505d172022-12-18 21:42:55 +0000135
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +0200136 // No type specified for the member. Set it to "any" and the correct
137 // type will be set when the object is instantiated.
Bram Moolenaard505d172022-12-18 21:42:55 +0000138 if (type == NULL)
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200139 type = &t_any;
Bram Moolenaard505d172022-12-18 21:42:55 +0000140
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200141 *init_expr = vim_strnsave(expr_start, expr_end - expr_start);
142 // Free the memory pointed by expr_start.
Bram Moolenaard505d172022-12-18 21:42:55 +0000143 clear_evalarg(&evalarg, NULL);
144 }
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200145 else if (!valid_declaration_type(type))
Bram Moolenaard505d172022-12-18 21:42:55 +0000146 return FAIL;
147
148 *type_ret = type;
Bram Moolenaard505d172022-12-18 21:42:55 +0000149 return OK;
150}
151
152/*
153 * Add a member to an object or a class.
154 * Returns OK when successful, "init_expr" will be consumed then.
155 * Returns FAIL otherwise, caller might need to free "init_expr".
156 */
157 static int
158add_member(
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +0200159 garray_T *gap,
160 char_u *varname,
161 char_u *varname_end,
162 int has_public,
163 type_T *type,
164 char_u *init_expr)
Bram Moolenaard505d172022-12-18 21:42:55 +0000165{
166 if (ga_grow(gap, 1) == FAIL)
167 return FAIL;
168 ocmember_T *m = ((ocmember_T *)gap->ga_data) + gap->ga_len;
169 m->ocm_name = vim_strnsave(varname, varname_end - varname);
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +0000170 m->ocm_access = has_public ? VIM_ACCESS_ALL
171 : *varname == '_' ? VIM_ACCESS_PRIVATE : VIM_ACCESS_READ;
Bram Moolenaard505d172022-12-18 21:42:55 +0000172 m->ocm_type = type;
173 if (init_expr != NULL)
174 m->ocm_init = init_expr;
175 ++gap->ga_len;
176 return OK;
177}
178
179/*
180 * Move the class or object members found while parsing a class into the class.
181 * "gap" contains the found members.
Bram Moolenaar83677162023-01-08 19:54:10 +0000182 * "parent_members" points to the members in the parent class (if any)
183 * "parent_count" is the number of members in the parent class
Bram Moolenaard505d172022-12-18 21:42:55 +0000184 * "members" will be set to the newly allocated array of members and
185 * "member_count" set to the number of members.
186 * Returns OK or FAIL.
187 */
188 static int
189add_members_to_class(
190 garray_T *gap,
Bram Moolenaar83677162023-01-08 19:54:10 +0000191 ocmember_T *parent_members,
192 int parent_count,
Bram Moolenaard505d172022-12-18 21:42:55 +0000193 ocmember_T **members,
194 int *member_count)
195{
Bram Moolenaar83677162023-01-08 19:54:10 +0000196 *member_count = parent_count + gap->ga_len;
197 *members = *member_count == 0 ? NULL
198 : ALLOC_MULT(ocmember_T, *member_count);
199 if (*member_count > 0 && *members == NULL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000200 return FAIL;
Bram Moolenaar83677162023-01-08 19:54:10 +0000201 for (int i = 0; i < parent_count; ++i)
202 {
203 // parent members need to be copied
Bram Moolenaarae3205a2023-01-15 20:49:00 +0000204 ocmember_T *m = *members + i;
205 *m = parent_members[i];
206 m->ocm_name = vim_strsave(m->ocm_name);
207 if (m->ocm_init != NULL)
208 m->ocm_init = vim_strsave(m->ocm_init);
Bram Moolenaar83677162023-01-08 19:54:10 +0000209 }
Bram Moolenaar8efdcee2022-12-19 12:18:09 +0000210 if (gap->ga_len > 0)
Bram Moolenaar83677162023-01-08 19:54:10 +0000211 // new members are moved
212 mch_memmove(*members + parent_count,
213 gap->ga_data, sizeof(ocmember_T) * gap->ga_len);
Bram Moolenaard505d172022-12-18 21:42:55 +0000214 VIM_CLEAR(gap->ga_data);
215 return OK;
216}
217
218/*
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000219 * Convert a member index "idx" of interface "itf" to the member index of class
220 * "cl" implementing that interface.
221 */
222 int
Ernie Rael18143d32023-09-04 22:30:41 +0200223object_index_from_itf_index(class_T *itf, int is_method, int idx, class_T *cl,
224 int is_static)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000225{
Ernie Rael18143d32023-09-04 22:30:41 +0200226 if (idx >= (is_method ? itf->class_obj_method_count
227 : is_static ? itf->class_class_member_count
Bram Moolenaard0200c82023-01-28 15:19:40 +0000228 : itf->class_obj_member_count))
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000229 {
230 siemsg("index %d out of range for interface %s", idx, itf->class_name);
231 return 0;
232 }
Yegappan Lakshmanan74cc13c2023-08-13 17:41:26 +0200233
234 // If "cl" is the interface or the class that is extended, then the method
235 // index can be used directly and there is no need to search for the method
236 // index in one of the child classes.
237 if (cl == itf)
238 return idx;
239
Yegappan Lakshmanancc0bcf42023-09-08 19:12:03 +0200240 itf2class_T *i2c = NULL;
241 int searching = TRUE;
242 int method_offset = 0;
243
Ernie Raelcf138d42023-09-06 20:45:03 +0200244 for (class_T *super = cl; super != NULL && searching;
245 super = super->class_extends)
Yegappan Lakshmanancc0bcf42023-09-08 19:12:03 +0200246 {
Ernie Raelcf138d42023-09-06 20:45:03 +0200247 for (i2c = itf->class_itf2class; i2c != NULL; i2c = i2c->i2c_next)
Yegappan Lakshmanancc0bcf42023-09-08 19:12:03 +0200248 {
Ernie Raelcf138d42023-09-06 20:45:03 +0200249 if (i2c->i2c_class == super && i2c->i2c_is_method == is_method)
250 {
251 searching = FALSE;
252 break;
253 }
Yegappan Lakshmanancc0bcf42023-09-08 19:12:03 +0200254 }
255 if (searching && is_method)
256 // The parent class methods are stored after the current class
257 // methods.
258 method_offset += is_static
259 ? super->class_class_function_count_child
260 : super->class_obj_method_count_child;
261 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000262 if (i2c == NULL)
263 {
264 siemsg("class %s not found on interface %s",
265 cl->class_name, itf->class_name);
266 return 0;
267 }
Ernie Rael18143d32023-09-04 22:30:41 +0200268 if (is_static)
269 {
270 // TODO: Need a table for fast lookup?
271 char_u *name = itf->class_class_members[idx].ocm_name;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200272 int m_idx = class_member_idx(i2c->i2c_class, name, 0);
273 if (m_idx >= 0)
274 return m_idx;
275
Ernie Rael18143d32023-09-04 22:30:41 +0200276 siemsg("class %s, interface %s, static %s not found",
277 cl->class_name, itf->class_name, name);
278 return 0;
279 }
280 else
281 {
282 // A table follows the i2c for the class
283 int *table = (int *)(i2c + 1);
Yegappan Lakshmanancc0bcf42023-09-08 19:12:03 +0200284 // "method_offset" is 0, if method is in the current class. If method
285 // is in a parent class, then it is non-zero.
286 return table[idx] + method_offset;
Ernie Rael18143d32023-09-04 22:30:41 +0200287 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000288}
289
290/*
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200291 * Check whether a class named "extends_name" is present. If the class is
292 * valid, then "extends_clp" is set with the class pointer.
293 * Returns TRUE if the class name "extends_names" is a valid class.
294 */
295 static int
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200296validate_extends_class(
297 char_u *extends_name,
298 class_T **extends_clp,
299 int is_class)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200300{
301 typval_T tv;
302 int success = FALSE;
303
304 tv.v_type = VAR_UNKNOWN;
305 if (eval_variable_import(extends_name, &tv) == FAIL)
306 {
307 semsg(_(e_class_name_not_found_str), extends_name);
308 return success;
309 }
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200310
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200311 if (tv.v_type != VAR_CLASS || tv.vval.v_class == NULL
312 || (is_class
313 && (tv.vval.v_class->class_flags & CLASS_INTERFACE) != 0)
314 || (!is_class
315 && (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0))
316 // a interface cannot extend a class and a class cannot extend an
317 // interface.
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200318 semsg(_(e_cannot_extend_str), extends_name);
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200319 else
320 {
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200321 class_T *extends_cl = tv.vval.v_class;
322 ++extends_cl->class_refcount;
323 *extends_clp = extends_cl;
324 success = TRUE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200325 }
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200326 clear_tv(&tv);
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200327
328 return success;
329}
330
331/*
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200332 * Check method names in the parent class lineage to make sure the access is
333 * the same for overridden methods.
334 */
335 static int
336validate_extends_methods(
337 garray_T *objmethods_gap,
338 class_T *extends_cl)
339{
340 class_T *super = extends_cl;
341 int method_count = objmethods_gap->ga_len;
342 ufunc_T **cl_fp = (ufunc_T **)(objmethods_gap->ga_data);
343
344 while (super != NULL)
345 {
346 int extends_method_count = super->class_obj_method_count_child;
347 if (extends_method_count == 0)
348 {
349 super = super->class_extends;
350 continue;
351 }
352
353 ufunc_T **extends_methods = super->class_obj_methods;
354
355 for (int i = 0; i < extends_method_count; i++)
356 {
357 char_u *pstr = extends_methods[i]->uf_name;
358 int extends_private = (*pstr == '_');
359 if (extends_private)
360 pstr++;
361
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200362 // When comparing the method names, ignore the access type (public
363 // and private methods are considered the same).
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200364 for (int j = 0; j < method_count; j++)
365 {
366 char_u *qstr = cl_fp[j]->uf_name;
367 int priv_method = (*qstr == '_');
368 if (priv_method)
369 qstr++;
370 if (STRCMP(pstr, qstr) == 0 && priv_method != extends_private)
371 {
372 // Method access is different between the super class and
373 // the subclass
374 semsg(_(e_method_str_of_class_str_has_different_access),
375 cl_fp[j]->uf_name, super->class_name);
376 return FALSE;
377 }
378 }
379 }
380 super = super->class_extends;
381 }
382
383 return TRUE;
384}
385
386/*
387 * Check whether a object member variable in "objmembers_gap" is a duplicate of
388 * a member in any of the extended parent class lineage. Returns TRUE if there
389 * are no duplicates.
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200390 */
391 static int
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200392extends_check_dup_members(
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200393 garray_T *objmembers_gap,
394 class_T *extends_cl)
395{
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200396 int member_count = objmembers_gap->ga_len;
397 if (member_count == 0)
398 return TRUE;
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200399
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200400 ocmember_T *members = (ocmember_T *)(objmembers_gap->ga_data);
401
402 // Validate each member variable
403 for (int c_i = 0; c_i < member_count; c_i++)
404 {
405 class_T *p_cl = extends_cl;
406 ocmember_T *c_m = members + c_i;
407 char_u *pstr = (*c_m->ocm_name == '_')
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200408 ? c_m->ocm_name + 1 : c_m->ocm_name;
409
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200410 // Check in all the parent classes in the lineage
411 while (p_cl != NULL)
412 {
413 int p_member_count = p_cl->class_obj_member_count;
414 if (p_member_count == 0)
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200415 {
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200416 p_cl = p_cl->class_extends;
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200417 continue;
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200418 }
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200419 ocmember_T *p_members = p_cl->class_obj_members;
420
421 // Compare against all the members in the parent class
422 for (int p_i = 0; p_i < p_member_count; p_i++)
423 {
424 ocmember_T *p_m = p_members + p_i;
425 char_u *qstr = (*p_m->ocm_name == '_')
426 ? p_m->ocm_name + 1 : p_m->ocm_name;
427 if (STRCMP(pstr, qstr) == 0)
428 {
429 semsg(_(e_duplicate_member_str), c_m->ocm_name);
430 return FALSE;
431 }
432 }
433
434 p_cl = p_cl->class_extends;
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200435 }
436 }
437
438 return TRUE;
439}
440
441/*
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200442 * Compare the variable type of interface variables in "objmembers_gap" against
443 * the variable in any of the extended super interface lineage. Used to
444 * compare the variable types when extending interfaces. Returns TRUE if the
445 * variable types are the same.
446 */
447 static int
448extends_check_intf_var_type(
449 garray_T *objmembers_gap,
450 class_T *extends_cl)
451{
452 int member_count = objmembers_gap->ga_len;
453 if (member_count == 0)
454 return TRUE;
455
456 ocmember_T *members = (ocmember_T *)(objmembers_gap->ga_data);
457
458 // Validate each member variable
459 for (int c_i = 0; c_i < member_count; c_i++)
460 {
461 class_T *p_cl = extends_cl;
462 ocmember_T *c_m = members + c_i;
463 int var_found = FALSE;
464
465 // Check in all the parent classes in the lineage
466 while (p_cl != NULL && !var_found)
467 {
468 int p_member_count = p_cl->class_obj_member_count;
469 if (p_member_count == 0)
470 {
471 p_cl = p_cl->class_extends;
472 continue;
473 }
474 ocmember_T *p_members = p_cl->class_obj_members;
475
476 // Compare against all the members in the parent class
477 for (int p_i = 0; p_i < p_member_count; p_i++)
478 {
479 where_T where = WHERE_INIT;
480 ocmember_T *p_m = p_members + p_i;
481
482 if (STRCMP(p_m->ocm_name, c_m->ocm_name) != 0)
483 continue;
484
485 // Ensure the type is matching.
486 where.wt_func_name = (char *)c_m->ocm_name;
487 where.wt_kind = WT_MEMBER;
488
489 if (check_type(p_m->ocm_type, c_m->ocm_type, TRUE,
490 where) == FAIL)
491 return FALSE;
492
493 var_found = TRUE;
494 }
495
496 p_cl = p_cl->class_extends;
497 }
498 }
499
500 return TRUE;
501}
502
503/*
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200504 * When extending an abstract class, check whether all the abstract methods in
505 * the parent class are implemented. Returns TRUE if all the methods are
506 * implemented.
507 */
508 static int
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200509validate_abstract_class_methods(
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200510 garray_T *classmethods_gap,
511 garray_T *objmethods_gap,
512 class_T *extends_cl)
513{
514 for (int loop = 1; loop <= 2; ++loop)
515 {
516 // loop == 1: check class methods
517 // loop == 2: check object methods
518 int extends_method_count = loop == 1
519 ? extends_cl->class_class_function_count
520 : extends_cl->class_obj_method_count;
521 if (extends_method_count == 0)
522 continue;
523
524 ufunc_T **extends_methods = loop == 1
525 ? extends_cl->class_class_functions
526 : extends_cl->class_obj_methods;
527
528 int method_count = loop == 1 ? classmethods_gap->ga_len
529 : objmethods_gap->ga_len;
530 ufunc_T **cl_fp = (ufunc_T **)(loop == 1
531 ? classmethods_gap->ga_data
532 : objmethods_gap->ga_data);
533
534 for (int i = 0; i < extends_method_count; i++)
535 {
536 ufunc_T *uf = extends_methods[i];
537 if ((uf->uf_flags & FC_ABSTRACT) == 0)
538 continue;
539
540 int method_found = FALSE;
541
542 for (int j = 0; j < method_count; j++)
543 {
544 if (STRCMP(uf->uf_name, cl_fp[j]->uf_name) == 0)
545 {
546 method_found = TRUE;
547 break;
548 }
549 }
550
551 if (!method_found)
552 {
553 semsg(_(e_abstract_method_str_not_found), uf->uf_name);
554 return FALSE;
555 }
556 }
557 }
558
559 return TRUE;
560}
561
562/*
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200563 * Returns TRUE if the interface variable "if_var" is present in the list of
564 * variables in "cl_mt" or in the parent lineage of one of the extended classes
565 * in "extends_cl". For a class variable, 'is_class_var' is TRUE.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200566 */
567 static int
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200568intf_variable_present(
569 char_u *intf_class_name,
570 ocmember_T *if_var,
571 int is_class_var,
572 ocmember_T *cl_mt,
573 int cl_member_count,
574 class_T *extends_cl)
575{
576 int variable_present = FALSE;
577
578 for (int cl_i = 0; cl_i < cl_member_count; ++cl_i)
579 {
580 ocmember_T *m = &cl_mt[cl_i];
581 where_T where = WHERE_INIT;
582
583 if (STRCMP(if_var->ocm_name, m->ocm_name) != 0)
584 continue;
585
586 // Ensure the access type is same
587 if (if_var->ocm_access != m->ocm_access)
588 {
589 semsg(_(e_member_str_of_interface_str_has_different_access),
590 if_var->ocm_name, intf_class_name);
591 return FALSE;
592 }
593
594 // Ensure the type is matching.
595 if (m->ocm_type == &t_any)
596 {
597 // variable type is not specified. Use the variable type in the
598 // interface.
599 m->ocm_type = if_var->ocm_type;
600 }
601 else
602 {
603 where.wt_func_name = (char *)m->ocm_name;
604 where.wt_kind = WT_MEMBER;
605 if (check_type(if_var->ocm_type, m->ocm_type, TRUE,
606 where) == FAIL)
607 return FALSE;
608 }
609
610 variable_present = TRUE;
611 break;
612 }
613
614 if (!variable_present && extends_cl != NULL)
615 {
616 int ext_cl_count = is_class_var
617 ? extends_cl->class_class_member_count
618 : extends_cl->class_obj_member_count;
619 ocmember_T *ext_cl_mt = is_class_var
620 ? extends_cl->class_class_members
621 : extends_cl->class_obj_members;
622 return intf_variable_present(intf_class_name, if_var,
623 is_class_var, ext_cl_mt,
624 ext_cl_count,
625 extends_cl->class_extends);
626 }
627
628 return variable_present;
629}
630
631/*
632 * Check the variables of the interface class "ifcl" match the class variables
633 * ("classmembers_gap") and object variables ("objmembers_gap") of a class.
634 * Returns TRUE if the class and object variables names are valid.
635 */
636 static int
637validate_interface_variables(
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200638 char_u *intf_class_name,
639 class_T *ifcl,
640 garray_T *classmembers_gap,
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200641 garray_T *objmembers_gap,
642 class_T *extends_cl)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200643{
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200644 for (int loop = 1; loop <= 2; ++loop)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200645 {
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200646 // loop == 1: check class variables
647 // loop == 2: check object variables
648 int is_class_var = (loop == 1);
649 int if_count = is_class_var ? ifcl->class_class_member_count
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200650 : ifcl->class_obj_member_count;
651 if (if_count == 0)
652 continue;
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200653 ocmember_T *if_ms = is_class_var ? ifcl->class_class_members
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200654 : ifcl->class_obj_members;
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200655 ocmember_T *cl_ms = (ocmember_T *)(is_class_var
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200656 ? classmembers_gap->ga_data
657 : objmembers_gap->ga_data);
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200658 int cl_count = is_class_var ? classmembers_gap->ga_len
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200659 : objmembers_gap->ga_len;
660 for (int if_i = 0; if_i < if_count; ++if_i)
661 {
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200662 if (!intf_variable_present(intf_class_name, &if_ms[if_i],
663 is_class_var, cl_ms, cl_count, extends_cl))
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200664 {
665 semsg(_(e_member_str_of_interface_str_not_implemented),
666 if_ms[if_i].ocm_name, intf_class_name);
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200667 return FALSE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200668 }
669 }
670 }
671
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200672 return TRUE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200673}
674
675/*
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200676 * Returns TRUE if the method signature of "if_method" and "cl_method" matches.
677 */
678 static int
679intf_method_type_matches(ufunc_T *if_method, ufunc_T *cl_method)
680{
681 where_T where = WHERE_INIT;
682
683 // Ensure the type is matching.
684 where.wt_func_name = (char *)if_method->uf_name;
685 where.wt_kind = WT_METHOD;
686 if (check_type(if_method->uf_func_type, cl_method->uf_func_type, TRUE,
687 where) == FAIL)
688 return FALSE;
689
690 return TRUE;
691}
692
693/*
694 * Returns TRUE if the interface method "if_ufunc" is present in the list of
695 * methods in "cl_fp" or in the parent lineage of one of the extended classes
696 * in "extends_cl". For a class method, 'is_class_method' is TRUE.
697 */
698 static int
699intf_method_present(
700 ufunc_T *if_ufunc,
701 int is_class_method,
702 ufunc_T **cl_fp,
703 int cl_count,
704 class_T *extends_cl)
705{
706 int method_present = FALSE;
707
708 for (int cl_i = 0; cl_i < cl_count; ++cl_i)
709 {
710 char_u *cl_name = cl_fp[cl_i]->uf_name;
711 if (STRCMP(if_ufunc->uf_name, cl_name) == 0)
712 {
713 // Ensure the type is matching.
714 if (!intf_method_type_matches(if_ufunc, cl_fp[cl_i]))
715 return FALSE;
716 method_present = TRUE;
717 break;
718 }
719 }
720
721 if (!method_present && extends_cl != NULL)
722 {
723 ufunc_T **ext_cl_fp = (ufunc_T **)(is_class_method
724 ? extends_cl->class_class_functions
725 : extends_cl->class_obj_methods);
726 int ext_cl_count = is_class_method
727 ? extends_cl->class_class_function_count
728 : extends_cl->class_obj_method_count;
729 return intf_method_present(if_ufunc, is_class_method, ext_cl_fp,
730 ext_cl_count,
731 extends_cl->class_extends);
732 }
733
734 return method_present;
735}
736
737/*
738 * Validate that a new class implements all the class/instance methods in the
739 * interface "ifcl". The new class methods are in "classfunctions_gap" and the
740 * new object methods are in "objmemthods_gap". Also validates the method
741 * types.
742 * Returns TRUE if all the interface class/object methods are implemented in
743 * the new class.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200744 */
745 static int
746validate_interface_methods(
747 char_u *intf_class_name,
748 class_T *ifcl,
749 garray_T *classfunctions_gap,
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200750 garray_T *objmethods_gap,
751 class_T *extends_cl)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200752{
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200753 for (int loop = 1; loop <= 2; ++loop)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200754 {
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200755 // loop == 1: check class methods
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200756 // loop == 2: check object methods
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200757 int is_class_method = (loop == 1);
758 int if_count = is_class_method ? ifcl->class_class_function_count
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200759 : ifcl->class_obj_method_count;
760 if (if_count == 0)
761 continue;
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200762 ufunc_T **if_fp = is_class_method ? ifcl->class_class_functions
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200763 : ifcl->class_obj_methods;
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200764 ufunc_T **cl_fp = (ufunc_T **)(is_class_method
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200765 ? classfunctions_gap->ga_data
766 : objmethods_gap->ga_data);
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200767 int cl_count = is_class_method ? classfunctions_gap->ga_len
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200768 : objmethods_gap->ga_len;
769 for (int if_i = 0; if_i < if_count; ++if_i)
770 {
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +0200771 char_u *if_name = if_fp[if_i]->uf_name;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200772
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200773 if (!intf_method_present(if_fp[if_i], is_class_method, cl_fp,
774 cl_count, extends_cl))
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200775 {
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200776 semsg(_(e_method_str_of_interface_str_not_implemented),
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200777 if_name, intf_class_name);
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200778 return FALSE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200779 }
780 }
781 }
782
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200783 return TRUE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200784}
785
786/*
787 * Validate all the "implements" classes when creating a new class. The
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200788 * classes are returned in "intf_classes". The class functions, class members,
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200789 * object methods and object members in the new class are in
790 * "classfunctions_gap", "classmembers_gap", "objmethods_gap", and
791 * "objmembers_gap" respectively.
792 */
793 static int
794validate_implements_classes(
795 garray_T *impl_gap,
796 class_T **intf_classes,
797 garray_T *classfunctions_gap,
798 garray_T *classmembers_gap,
799 garray_T *objmethods_gap,
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200800 garray_T *objmembers_gap,
801 class_T *extends_cl)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200802{
803 int success = TRUE;
804
805 for (int i = 0; i < impl_gap->ga_len && success; ++i)
806 {
807 char_u *impl = ((char_u **)impl_gap->ga_data)[i];
808 typval_T tv;
809 tv.v_type = VAR_UNKNOWN;
810 if (eval_variable_import(impl, &tv) == FAIL)
811 {
812 semsg(_(e_interface_name_not_found_str), impl);
813 success = FALSE;
814 break;
815 }
816
817 if (tv.v_type != VAR_CLASS
818 || tv.vval.v_class == NULL
819 || (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0)
820 {
821 semsg(_(e_not_valid_interface_str), impl);
822 success = FALSE;
823 clear_tv(&tv);
824 break;
825 }
826
827 class_T *ifcl = tv.vval.v_class;
828 intf_classes[i] = ifcl;
829 ++ifcl->class_refcount;
830
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200831 // check the variables of the interface match the members of the class
832 success = validate_interface_variables(impl, ifcl, classmembers_gap,
833 objmembers_gap, extends_cl);
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200834
835 // check the functions/methods of the interface match the
836 // functions/methods of the class
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200837 if (success)
838 success = validate_interface_methods(impl, ifcl,
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200839 classfunctions_gap, objmethods_gap,
840 extends_cl);
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200841 clear_tv(&tv);
842 }
843
844 return success;
845}
846
847/*
848 * Check no function argument name is used as a class member.
849 * (Object members are always accessed with "this." prefix, so no need
850 * to check them.)
851 */
852 static int
853check_func_arg_names(
854 garray_T *classfunctions_gap,
855 garray_T *objmethods_gap,
856 garray_T *classmembers_gap)
857{
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200858 // loop 1: class functions, loop 2: object methods
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200859 for (int loop = 1; loop <= 2; ++loop)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200860 {
861 garray_T *gap = loop == 1 ? classfunctions_gap : objmethods_gap;
862
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200863 for (int fi = 0; fi < gap->ga_len; ++fi)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200864 {
865 ufunc_T *uf = ((ufunc_T **)gap->ga_data)[fi];
866
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200867 for (int i = 0; i < uf->uf_args.ga_len; ++i)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200868 {
869 char_u *aname = ((char_u **)uf->uf_args.ga_data)[i];
870 garray_T *mgap = classmembers_gap;
871
872 // Check all the class member names
873 for (int mi = 0; mi < mgap->ga_len; ++mi)
874 {
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +0200875 char_u *mname =
876 ((ocmember_T *)mgap->ga_data + mi)->ocm_name;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200877 if (STRCMP(aname, mname) == 0)
878 {
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200879 if (uf->uf_script_ctx.sc_sid > 0)
880 SOURCING_LNUM = uf->uf_script_ctx.sc_lnum;
881
882 semsg(_(e_argument_already_declared_in_class_str),
883 aname);
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200884
885 return FALSE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200886 }
887 }
888 }
889 }
890 }
891
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200892 return TRUE;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +0200893}
894
895/*
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +0200896 * Returns TRUE if the member "varname" is already defined.
897 */
898 static int
899is_duplicate_member(garray_T *mgap, char_u *varname, char_u *varname_end)
900{
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +0200901 char_u *name = vim_strnsave(varname, varname_end - varname);
902 char_u *pstr = (*name == '_') ? name + 1 : name;
903 int dup = FALSE;
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +0200904
905 for (int i = 0; i < mgap->ga_len; ++i)
906 {
907 ocmember_T *m = ((ocmember_T *)mgap->ga_data) + i;
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +0200908 char_u *qstr = *m->ocm_name == '_' ? m->ocm_name + 1 : m->ocm_name;
909 if (STRCMP(pstr, qstr) == 0)
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +0200910 {
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +0200911 semsg(_(e_duplicate_member_str), name);
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +0200912 dup = TRUE;
913 break;
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +0200914 }
915 }
916
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +0200917 vim_free(name);
918 return dup;
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +0200919}
920
921/*
922 * Returns TRUE if the method "name" is already defined.
923 */
924 static int
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200925is_duplicate_method(
926 garray_T *classmethods_gap,
927 garray_T *objmethods_gap,
928 char_u *name)
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +0200929{
930 char_u *pstr = (*name == '_') ? name + 1 : name;
931
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200932 // loop 1: class methods, loop 2: object methods
933 for (int loop = 1; loop <= 2; loop++)
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +0200934 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200935 garray_T *fgap = (loop == 1) ? classmethods_gap : objmethods_gap;
936 for (int i = 0; i < fgap->ga_len; ++i)
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +0200937 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200938 char_u *n = ((ufunc_T **)fgap->ga_data)[i]->uf_name;
939 char_u *qstr = *n == '_' ? n + 1 : n;
940 if (STRCMP(pstr, qstr) == 0)
941 {
942 semsg(_(e_duplicate_function_str), name);
943 return TRUE;
944 }
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +0200945 }
946 }
947
948 return FALSE;
949}
950
951/*
Gianmaria Bajo4b9777a2023-08-29 22:26:30 +0200952 * Returns TRUE if the constructor is valid.
953 */
954 static int
955is_valid_constructor(ufunc_T *uf, int is_abstract, int has_static)
956{
957 // Constructors are not allowed in abstract classes.
958 if (is_abstract)
959 {
960 emsg(_(e_cannot_define_new_function_in_abstract_class));
961 return FALSE;
962 }
963 // A constructor is always static, no need to define it so.
964 if (has_static)
965 {
966 emsg(_(e_cannot_define_new_function_as_static));
967 return FALSE;
968 }
969 // A return type should not be specified for the new()
970 // constructor method.
971 if (uf->uf_ret_type->tt_type != VAR_VOID)
972 {
973 emsg(_(e_cannot_use_a_return_type_with_new));
974 return FALSE;
975 }
976 return TRUE;
977}
978
979/*
Yegappan Lakshmananb1027282023-08-19 11:26:42 +0200980 * Update the interface class lookup table for the member index on the
981 * interface to the member index in the class implementing the interface.
982 * And a lookup table for the object method index on the interface
983 * to the object method index in the class implementing the interface.
984 * This is also used for updating the lookup table for the extended class
985 * hierarchy.
986 */
987 static int
988update_member_method_lookup_table(
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +0200989 class_T *ifcl,
990 class_T *cl,
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +0200991 garray_T *objmethods,
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +0200992 int pobj_method_offset)
Yegappan Lakshmananb1027282023-08-19 11:26:42 +0200993{
994 if (ifcl == NULL)
995 return OK;
996
997 // Table for members.
998 itf2class_T *if2cl = alloc_clear(sizeof(itf2class_T)
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +0200999 + ifcl->class_obj_member_count * sizeof(int));
Yegappan Lakshmananb1027282023-08-19 11:26:42 +02001000 if (if2cl == NULL)
1001 return FAIL;
1002 if2cl->i2c_next = ifcl->class_itf2class;
1003 ifcl->class_itf2class = if2cl;
1004 if2cl->i2c_class = cl;
1005 if2cl->i2c_is_method = FALSE;
1006
1007 for (int if_i = 0; if_i < ifcl->class_obj_member_count; ++if_i)
1008 for (int cl_i = 0; cl_i < cl->class_obj_member_count; ++cl_i)
1009 {
1010 if (STRCMP(ifcl->class_obj_members[if_i].ocm_name,
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02001011 cl->class_obj_members[cl_i].ocm_name) == 0)
Yegappan Lakshmananb1027282023-08-19 11:26:42 +02001012 {
1013 int *table = (int *)(if2cl + 1);
1014 table[if_i] = cl_i;
1015 break;
1016 }
1017 }
1018
1019 // Table for methods.
1020 if2cl = alloc_clear(sizeof(itf2class_T)
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02001021 + ifcl->class_obj_method_count * sizeof(int));
Yegappan Lakshmananb1027282023-08-19 11:26:42 +02001022 if (if2cl == NULL)
1023 return FAIL;
1024 if2cl->i2c_next = ifcl->class_itf2class;
1025 ifcl->class_itf2class = if2cl;
1026 if2cl->i2c_class = cl;
1027 if2cl->i2c_is_method = TRUE;
1028
1029 for (int if_i = 0; if_i < ifcl->class_obj_method_count; ++if_i)
1030 {
1031 int done = FALSE;
1032 for (int cl_i = 0; cl_i < objmethods->ga_len; ++cl_i)
1033 {
1034 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02001035 ((ufunc_T **)objmethods->ga_data)[cl_i]->uf_name) == 0)
Yegappan Lakshmananb1027282023-08-19 11:26:42 +02001036 {
1037 int *table = (int *)(if2cl + 1);
1038 table[if_i] = cl_i;
1039 done = TRUE;
1040 break;
1041 }
1042 }
1043
1044 // extended class object method is not overridden by the child class.
1045 // Keep the method declared in one of the parent classes in the
1046 // lineage.
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001047 if (!done)
Yegappan Lakshmananb1027282023-08-19 11:26:42 +02001048 {
1049 // If "ifcl" is not the immediate parent of "cl", then search in
1050 // the intermediate parent classes.
1051 if (cl->class_extends != ifcl)
1052 {
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02001053 class_T *parent = cl->class_extends;
1054 int method_offset = objmethods->ga_len;
Yegappan Lakshmananb1027282023-08-19 11:26:42 +02001055
1056 while (!done && parent != NULL && parent != ifcl)
1057 {
1058
1059 for (int cl_i = 0;
1060 cl_i < parent->class_obj_method_count_child; ++cl_i)
1061 {
1062 if (STRCMP(ifcl->class_obj_methods[if_i]->uf_name,
1063 parent->class_obj_methods[cl_i]->uf_name)
1064 == 0)
1065 {
1066 int *table = (int *)(if2cl + 1);
1067 table[if_i] = method_offset + cl_i;
1068 done = TRUE;
1069 break;
1070 }
1071 }
1072 method_offset += parent->class_obj_method_count_child;
1073 parent = parent->class_extends;
1074 }
1075 }
1076
1077 if (!done)
1078 {
1079 int *table = (int *)(if2cl + 1);
1080 table[if_i] = pobj_method_offset + if_i;
1081 }
1082 }
1083 }
1084
1085 return OK;
1086}
1087
1088/*
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001089 * Update the member and object method lookup tables for a new class in the
1090 * interface class.
1091 * For each interface add a lookup table for the member index on the interface
1092 * to the member index in the new class. And a lookup table for the object
1093 * method index on the interface to the object method index in the new class.
1094 */
1095 static int
1096add_lookup_tables(class_T *cl, class_T *extends_cl, garray_T *objmethods_gap)
1097{
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001098 // update the lookup table for all the implemented interfaces
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001099 for (int i = 0; i < cl->class_interface_count; ++i)
1100 {
1101 class_T *ifcl = cl->class_interfaces_cl[i];
1102
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001103 // update the lookup table for this interface and all its super
1104 // interfaces.
1105 while (ifcl != NULL)
1106 {
1107 if (update_member_method_lookup_table(ifcl, cl, objmethods_gap,
1108 0) == FAIL)
1109 return FAIL;
1110 ifcl = ifcl->class_extends;
1111 }
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001112 }
1113
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001114 // Update the lookup table for the extended class, if any
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001115 if (extends_cl != NULL)
1116 {
1117 class_T *pclass = extends_cl;
1118 int pobj_method_offset = objmethods_gap->ga_len;
1119
1120 // Update the entire lineage of extended classes.
1121 while (pclass != NULL)
1122 {
1123 if (update_member_method_lookup_table(pclass, cl,
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001124 objmethods_gap, pobj_method_offset) == FAIL)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001125 return FAIL;
1126
1127 pobj_method_offset += pclass->class_obj_method_count_child;
1128 pclass = pclass->class_extends;
1129 }
1130 }
1131
1132 return OK;
1133}
1134
1135/*
1136 * Add class members to a new class. Allocate a typval for each class member
1137 * and initialize it.
1138 */
1139 static void
1140add_class_members(class_T *cl, exarg_T *eap)
1141{
1142 // Allocate a typval for each class member and initialize it.
1143 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
1144 cl->class_class_member_count);
1145 if (cl->class_members_tv == NULL)
1146 return;
1147
1148 for (int i = 0; i < cl->class_class_member_count; ++i)
1149 {
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02001150 ocmember_T *m = &cl->class_class_members[i];
1151 typval_T *tv = &cl->class_members_tv[i];
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001152 if (m->ocm_init != NULL)
1153 {
1154 typval_T *etv = eval_expr(m->ocm_init, eap);
1155 if (etv != NULL)
1156 {
1157 *tv = *etv;
1158 vim_free(etv);
1159 }
1160 }
1161 else
1162 {
1163 // TODO: proper default value
1164 tv->v_type = m->ocm_type->tt_type;
1165 tv->vval.v_string = NULL;
1166 }
1167 }
1168}
1169
1170/*
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001171 * Add a default constructor method (new()) to the class "cl".
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001172 */
1173 static void
1174add_default_constructor(
1175 class_T *cl,
1176 garray_T *classfunctions_gap,
1177 garray_T *type_list_gap)
1178{
1179 garray_T fga;
1180
1181 ga_init2(&fga, 1, 1000);
1182 ga_concat(&fga, (char_u *)"new(");
1183 for (int i = 0; i < cl->class_obj_member_count; ++i)
1184 {
1185 if (i > 0)
1186 ga_concat(&fga, (char_u *)", ");
1187 ga_concat(&fga, (char_u *)"this.");
1188 ocmember_T *m = cl->class_obj_members + i;
1189 ga_concat(&fga, (char_u *)m->ocm_name);
1190 ga_concat(&fga, (char_u *)" = v:none");
1191 }
1192 ga_concat(&fga, (char_u *)")\nenddef\n");
1193 ga_append(&fga, NUL);
1194
1195 exarg_T fea;
1196 CLEAR_FIELD(fea);
1197 fea.cmdidx = CMD_def;
1198 fea.cmd = fea.arg = fga.ga_data;
1199
1200 garray_T lines_to_free;
1201 ga_init2(&lines_to_free, sizeof(char_u *), 50);
1202
1203 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS);
1204
1205 ga_clear_strings(&lines_to_free);
1206 vim_free(fga.ga_data);
1207
1208 if (nf != NULL && ga_grow(classfunctions_gap, 1) == OK)
1209 {
1210 ((ufunc_T **)classfunctions_gap->ga_data)[classfunctions_gap->ga_len]
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02001211 = nf;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001212 ++classfunctions_gap->ga_len;
1213
1214 nf->uf_flags |= FC_NEW;
1215 nf->uf_ret_type = get_type_ptr(type_list_gap);
1216 if (nf->uf_ret_type != NULL)
1217 {
1218 nf->uf_ret_type->tt_type = VAR_OBJECT;
1219 nf->uf_ret_type->tt_class = cl;
1220 nf->uf_ret_type->tt_argcount = 0;
1221 nf->uf_ret_type->tt_args = NULL;
1222 }
1223 }
1224}
1225
1226/*
Yegappan Lakshmanane2deb7e2023-09-16 18:05:07 +02001227 * Add the class methods and object methods to the new class "cl".
1228 * When extending a class "extends_cl", add the instance methods from the
1229 * parent class also.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001230 */
1231 static int
1232add_classfuncs_objmethods(
1233 class_T *cl,
1234 class_T *extends_cl,
1235 garray_T *classfunctions_gap,
1236 garray_T *objmethods_gap)
1237{
1238 // loop 1: class functions, loop 2: object methods
1239 for (int loop = 1; loop <= 2; ++loop)
1240 {
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02001241 garray_T *gap = loop == 1 ? classfunctions_gap : objmethods_gap;
1242 int *fcount = loop == 1 ? &cl->class_class_function_count
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001243 : &cl->class_obj_method_count;
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02001244 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001245 : &cl->class_obj_methods;
1246
1247 int parent_count = 0;
1248 if (extends_cl != NULL)
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001249 // Include object methods from the parent.
1250 // Don't include the parent class methods.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001251 parent_count = loop == 1
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001252 ? 0
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001253 : extends_cl->class_obj_method_count;
1254
1255 *fcount = parent_count + gap->ga_len;
1256 if (*fcount == 0)
1257 {
1258 *fup = NULL;
1259 continue;
1260 }
1261 *fup = ALLOC_MULT(ufunc_T *, *fcount);
1262 if (*fup == NULL)
1263 return FAIL;
1264
1265 if (gap->ga_len != 0)
1266 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
1267 vim_free(gap->ga_data);
1268 if (loop == 1)
1269 cl->class_class_function_count_child = gap->ga_len;
1270 else
1271 cl->class_obj_method_count_child = gap->ga_len;
1272
Yegappan Lakshmanane2deb7e2023-09-16 18:05:07 +02001273 if (loop == 2)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001274 {
Yegappan Lakshmanane2deb7e2023-09-16 18:05:07 +02001275 // Copy instance methods from the parent.
1276
1277 for (int i = 0; i < parent_count; ++i)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001278 {
Yegappan Lakshmanane2deb7e2023-09-16 18:05:07 +02001279 // Can't use the same parent function, because "uf_class" is
1280 // different and compilation will have a different result.
1281 // Put them after the functions in the current class, object
1282 // methods may be overruled, then "super.Method()" is used to
1283 // find a method from the parent.
1284 ufunc_T *pf = (extends_cl->class_obj_methods)[i];
1285 (*fup)[gap->ga_len + i] = copy_function(pf);
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001286
1287 // If the child class overrides a function from the parent
1288 // the signature must be equal.
1289 char_u *pname = pf->uf_name;
1290 for (int ci = 0; ci < gap->ga_len; ++ci)
1291 {
1292 ufunc_T *cf = (*fup)[ci];
1293 char_u *cname = cf->uf_name;
1294 if (STRCMP(pname, cname) == 0)
1295 {
1296 where_T where = WHERE_INIT;
1297 where.wt_func_name = (char *)pname;
1298 where.wt_kind = WT_METHOD;
1299 (void)check_type(pf->uf_func_type, cf->uf_func_type,
1300 TRUE, where);
1301 }
1302 }
1303 }
1304 }
1305
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001306 // Set the class pointer on all the functions and object methods.
1307 for (int i = 0; i < *fcount; ++i)
1308 {
1309 ufunc_T *fp = (*fup)[i];
1310 fp->uf_class = cl;
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001311 if (i < gap->ga_len)
1312 fp->uf_defclass = cl;
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001313 if (loop == 2)
1314 fp->uf_flags |= FC_OBJECT;
1315 }
1316 }
1317
1318 return OK;
1319}
1320
1321/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001322 * Handle ":class" and ":abstract class" up to ":endclass".
Bram Moolenaar554d0312023-01-05 19:59:18 +00001323 * Handle ":interface" up to ":endinterface".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001324 */
1325 void
1326ex_class(exarg_T *eap)
1327{
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02001328 int is_class = eap->cmdidx == CMD_class; // FALSE for :interface
1329 long start_lnum = SOURCING_LNUM;
1330 char_u *arg = eap->arg;
1331 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaar554d0312023-01-05 19:59:18 +00001332
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001333 if (is_abstract)
1334 {
1335 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
1336 {
1337 semsg(_(e_invalid_argument_str), arg);
1338 return;
1339 }
1340 arg = skipwhite(arg + 5);
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001341 is_class = TRUE;
1342 }
1343
1344 if (!current_script_is_vim9()
1345 || (cmdmod.cmod_flags & CMOD_LEGACY)
1346 || !getline_equal(eap->getline, eap->cookie, getsourceline))
1347 {
1348 if (is_class)
1349 emsg(_(e_class_can_only_be_defined_in_vim9_script));
1350 else
1351 emsg(_(e_interface_can_only_be_defined_in_vim9_script));
1352 return;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001353 }
1354
1355 if (!ASCII_ISUPPER(*arg))
1356 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001357 if (is_class)
1358 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
1359 else
1360 semsg(_(e_interface_name_must_start_with_uppercase_letter_str),
1361 arg);
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001362 return;
1363 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001364 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
1365 if (!IS_WHITE_OR_NUL(*name_end))
1366 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001367 semsg(_(e_white_space_required_after_name_str), arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001368 return;
1369 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001370 char_u *name_start = arg;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001371
Bram Moolenaara86655a2023-01-12 17:06:27 +00001372 // "export class" gets used when creating the class, don't use "is_export"
1373 // for the items inside the class.
1374 int class_export = is_export;
1375 is_export = FALSE;
1376
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001377 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001378 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001379
Bram Moolenaar83677162023-01-08 19:54:10 +00001380 // Name for "extends BaseClass"
1381 char_u *extends = NULL;
1382
Bram Moolenaar94674f22023-01-06 18:42:20 +00001383 // Names for "implements SomeInterface"
1384 garray_T ga_impl;
1385 ga_init2(&ga_impl, sizeof(char_u *), 5);
1386
1387 arg = skipwhite(name_end);
1388 while (*arg != NUL && *arg != '#' && *arg != '\n')
1389 {
1390 // TODO:
Bram Moolenaar94674f22023-01-06 18:42:20 +00001391 // specifies SomeInterface
Bram Moolenaar83677162023-01-08 19:54:10 +00001392 if (STRNCMP(arg, "extends", 7) == 0 && IS_WHITE_OR_NUL(arg[7]))
1393 {
1394 if (extends != NULL)
1395 {
1396 emsg(_(e_duplicate_extends));
1397 goto early_ret;
1398 }
1399 arg = skipwhite(arg + 7);
1400 char_u *end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
1401 if (!IS_WHITE_OR_NUL(*end))
1402 {
1403 semsg(_(e_white_space_required_after_name_str), arg);
1404 goto early_ret;
1405 }
1406 extends = vim_strnsave(arg, end - arg);
1407 if (extends == NULL)
1408 goto early_ret;
1409
1410 arg = skipwhite(end + 1);
1411 }
1412 else if (STRNCMP(arg, "implements", 10) == 0
1413 && IS_WHITE_OR_NUL(arg[10]))
Bram Moolenaar94674f22023-01-06 18:42:20 +00001414 {
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001415 if (!is_class)
1416 {
1417 emsg(_(e_interface_cannot_use_implements));
1418 goto early_ret;
1419 }
1420
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001421 if (ga_impl.ga_len > 0)
1422 {
1423 emsg(_(e_duplicate_implements));
1424 goto early_ret;
1425 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001426 arg = skipwhite(arg + 10);
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001427
1428 for (;;)
Bram Moolenaar94674f22023-01-06 18:42:20 +00001429 {
Bram Moolenaardf8f9472023-01-07 14:51:03 +00001430 char_u *impl_end = find_name_end(arg, NULL, NULL,
1431 FNE_CHECK_START);
1432 if (!IS_WHITE_OR_NUL(*impl_end) && *impl_end != ',')
1433 {
1434 semsg(_(e_white_space_required_after_name_str), arg);
1435 goto early_ret;
1436 }
1437 char_u *iname = vim_strnsave(arg, impl_end - arg);
1438 if (iname == NULL)
1439 goto early_ret;
1440 for (int i = 0; i < ga_impl.ga_len; ++i)
1441 if (STRCMP(((char_u **)ga_impl.ga_data)[i], iname) == 0)
1442 {
1443 semsg(_(e_duplicate_interface_after_implements_str),
1444 iname);
1445 vim_free(iname);
1446 goto early_ret;
1447 }
1448 if (ga_add_string(&ga_impl, iname) == FAIL)
1449 {
1450 vim_free(iname);
1451 goto early_ret;
1452 }
1453 if (*impl_end != ',')
1454 {
1455 arg = skipwhite(impl_end);
1456 break;
1457 }
1458 arg = skipwhite(impl_end + 1);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001459 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001460 }
1461 else
1462 {
1463 semsg(_(e_trailing_characters_str), arg);
1464early_ret:
Bram Moolenaar83677162023-01-08 19:54:10 +00001465 vim_free(extends);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001466 ga_clear_strings(&ga_impl);
1467 return;
1468 }
1469 }
1470
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001471 garray_T type_list; // list of pointers to allocated types
1472 ga_init2(&type_list, sizeof(type_T *), 10);
1473
Bram Moolenaard505d172022-12-18 21:42:55 +00001474 // Growarray with class members declared in the class.
1475 garray_T classmembers;
1476 ga_init2(&classmembers, sizeof(ocmember_T), 10);
1477
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001478 // Growarray with functions declared in the class.
1479 garray_T classfunctions;
1480 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +00001481
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001482 // Growarray with object members declared in the class.
1483 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +00001484 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001485
1486 // Growarray with object methods declared in the class.
1487 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001488 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001489
1490 /*
Bram Moolenaar554d0312023-01-05 19:59:18 +00001491 * Go over the body of the class/interface until "endclass" or
1492 * "endinterface" is found.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001493 */
1494 char_u *theline = NULL;
1495 int success = FALSE;
1496 for (;;)
1497 {
1498 vim_free(theline);
1499 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
1500 if (theline == NULL)
1501 break;
1502 char_u *line = skipwhite(theline);
1503
Bram Moolenaar418b5472022-12-20 13:38:22 +00001504 // Skip empty and comment lines.
1505 if (*line == NUL)
1506 continue;
1507 if (*line == '#')
1508 {
1509 if (vim9_bad_comment(line))
1510 break;
1511 continue;
1512 }
1513
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001514 char_u *p = line;
Bram Moolenaar554d0312023-01-05 19:59:18 +00001515 char *end_name = is_class ? "endclass" : "endinterface";
1516 if (checkforcmd(&p, end_name, is_class ? 4 : 5))
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001517 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001518 if (STRNCMP(line, end_name, is_class ? 8 : 12) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001519 semsg(_(e_command_cannot_be_shortened_str), line);
1520 else if (*p == '|' || !ends_excmd2(line, p))
1521 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +00001522 else
1523 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001524 break;
1525 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00001526 char *wrong_name = is_class ? "endinterface" : "endclass";
1527 if (checkforcmd(&p, wrong_name, is_class ? 5 : 4))
1528 {
Bram Moolenaar657aea72023-01-27 13:16:19 +00001529 semsg(_(e_invalid_command_str_expected_str), line, end_name);
Bram Moolenaar554d0312023-01-05 19:59:18 +00001530 break;
1531 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001532
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001533 int has_public = FALSE;
1534 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001535 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001536 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001537 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001538 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001539 break;
1540 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001541 has_public = TRUE;
1542 p = skipwhite(line + 6);
1543
Bram Moolenaard505d172022-12-18 21:42:55 +00001544 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001545 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001546 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001547 break;
1548 }
1549 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001550
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02001551 int abstract_method = FALSE;
1552 char_u *pa = p;
1553 if (checkforcmd(&p, "abstract", 3))
1554 {
1555 if (STRNCMP(pa, "abstract", 8) != 0)
1556 {
1557 semsg(_(e_command_cannot_be_shortened_str), pa);
1558 break;
1559 }
1560
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001561 if (!is_class)
1562 // ignore "abstract" in an interface (as all the methods in an
1563 // interface are abstract.
1564 p = skipwhite(pa + 8);
1565 else
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02001566 {
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001567 if (!is_abstract)
1568 {
1569 semsg(_(e_abstract_method_in_concrete_class), pa);
1570 break;
1571 }
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02001572
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001573 abstract_method = TRUE;
1574 p = skipwhite(pa + 8);
1575 if (STRNCMP(p, "def", 3) != 0 && STRNCMP(p, "static", 6) != 0)
1576 {
1577 emsg(_(e_abstract_must_be_followed_by_def_or_static));
1578 break;
1579 }
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02001580 }
1581 }
1582
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001583 int has_static = FALSE;
1584 char_u *ps = p;
1585 if (checkforcmd(&p, "static", 4))
1586 {
1587 if (STRNCMP(ps, "static", 6) != 0)
1588 {
1589 semsg(_(e_command_cannot_be_shortened_str), ps);
1590 break;
1591 }
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001592
1593 if (!is_class)
1594 {
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001595 emsg(_(e_static_member_not_supported_in_interface));
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001596 break;
1597 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001598 has_static = TRUE;
1599 p = skipwhite(ps + 6);
1600 }
1601
Bram Moolenaard505d172022-12-18 21:42:55 +00001602 // object members (public, read access, private):
1603 // "this._varname"
1604 // "this.varname"
1605 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001606 if (STRNCMP(p, "this", 4) == 0)
1607 {
1608 if (p[4] != '.' || !eval_isnamec1(p[5]))
1609 {
1610 semsg(_(e_invalid_object_member_declaration_str), p);
1611 break;
1612 }
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001613 if (has_static)
1614 {
1615 emsg(_(e_static_cannot_be_followed_by_this));
1616 break;
1617 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001618 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +00001619 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +00001620 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +00001621 char_u *init_expr = NULL;
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001622
1623 if (!is_class && *varname == '_')
1624 {
1625 // private variables are not supported in an interface
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001626 semsg(_(e_private_variable_not_supported_in_interface),
1627 varname);
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001628 break;
1629 }
1630
Bram Moolenaard505d172022-12-18 21:42:55 +00001631 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +00001632 &varname_end, &type_list, &type,
1633 is_class ? &init_expr: NULL) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +00001634 break;
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001635 if (is_duplicate_member(&objmembers, varname, varname_end))
1636 {
1637 vim_free(init_expr);
1638 break;
1639 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001640 if (add_member(&objmembers, varname, varname_end,
1641 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +00001642 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001643 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001644 break;
1645 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001646 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001647
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001648 // constructors:
1649 // def new()
1650 // enddef
1651 // def newOther()
1652 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001653 // object methods and class functions:
1654 // def SomeMethod()
1655 // enddef
1656 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001657 // enddef
1658 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001659 // def <Tval> someMethod()
1660 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001661 else if (checkforcmd(&p, "def", 3))
1662 {
1663 exarg_T ea;
1664 garray_T lines_to_free;
1665
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001666 // TODO: error for "public static def Func()"?
1667
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001668 CLEAR_FIELD(ea);
1669 ea.cmd = line;
1670 ea.arg = p;
1671 ea.cmdidx = CMD_def;
1672 ea.getline = eap->getline;
1673 ea.cookie = eap->cookie;
1674
1675 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02001676 int class_flags;
1677 if (is_class)
1678 class_flags = abstract_method ? CF_ABSTRACT_METHOD : CF_CLASS;
1679 else
1680 class_flags = CF_INTERFACE;
Bram Moolenaar554d0312023-01-05 19:59:18 +00001681 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free,
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02001682 class_flags);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001683 ga_clear_strings(&lines_to_free);
1684
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001685 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001686 {
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02001687 char_u *name = uf->uf_name;
1688 int is_new = STRNCMP(name, "new", 3) == 0;
Gianmaria Bajo4b9777a2023-08-29 22:26:30 +02001689
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001690 if (!is_class && *name == '_')
1691 {
1692 // private variables are not supported in an interface
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001693 semsg(_(e_private_method_not_supported_in_interface),
1694 name);
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001695 func_clear_free(uf, FALSE);
1696 break;
1697 }
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02001698 if (is_new && !is_valid_constructor(uf, is_abstract,
1699 has_static))
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001700 {
Yegappan Lakshmananb1027282023-08-19 11:26:42 +02001701 func_clear_free(uf, FALSE);
Bram Moolenaar24a8d062023-01-14 13:12:06 +00001702 break;
1703 }
Gianmaria Bajo4b9777a2023-08-29 22:26:30 +02001704
Bram Moolenaar58b40092023-01-11 15:59:05 +00001705 // Check the name isn't used already.
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001706 if (is_duplicate_method(&classfunctions, &objmethods, name))
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +02001707 {
1708 success = FALSE;
1709 func_clear_free(uf, FALSE);
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001710 break;
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +02001711 }
Bram Moolenaar58b40092023-01-11 15:59:05 +00001712
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001713 garray_T *fgap = has_static || is_new
1714 ? &classfunctions : &objmethods;
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001715 if (ga_grow(fgap, 1) == OK)
1716 {
1717 if (is_new)
1718 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001719
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02001720 if (abstract_method)
1721 uf->uf_flags |= FC_ABSTRACT;
1722
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001723 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
1724 ++fgap->ga_len;
1725 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001726 }
1727 }
1728
1729 // class members
1730 else if (has_static)
1731 {
1732 // class members (public, read access, private):
1733 // "static _varname"
1734 // "static varname"
1735 // "public static varname"
1736 char_u *varname = p;
1737 char_u *varname_end = NULL;
1738 type_T *type = NULL;
1739 char_u *init_expr = NULL;
1740 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +00001741 &varname_end, &type_list, &type,
1742 is_class ? &init_expr : NULL) == FAIL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001743 break;
Yegappan Lakshmanan2ba9d2e2023-08-28 21:26:23 +02001744 if (is_duplicate_member(&classmembers, varname, varname_end))
1745 {
1746 vim_free(init_expr);
1747 break;
1748 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001749 if (add_member(&classmembers, varname, varname_end,
1750 has_public, type, init_expr) == FAIL)
1751 {
1752 vim_free(init_expr);
1753 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001754 }
1755 }
1756
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001757 else
1758 {
Bram Moolenaar554d0312023-01-05 19:59:18 +00001759 if (is_class)
1760 semsg(_(e_not_valid_command_in_class_str), line);
1761 else
1762 semsg(_(e_not_valid_command_in_interface_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001763 break;
1764 }
1765 }
1766 vim_free(theline);
1767
Bram Moolenaar83677162023-01-08 19:54:10 +00001768 class_T *extends_cl = NULL; // class from "extends" argument
1769
1770 /*
1771 * Check a few things before defining the class.
1772 */
1773
1774 // Check the "extends" class is valid.
1775 if (success && extends != NULL)
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001776 success = validate_extends_class(extends, &extends_cl, is_class);
Bram Moolenaar83677162023-01-08 19:54:10 +00001777 VIM_CLEAR(extends);
1778
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001779 // Check the new object methods to make sure their access (public or
1780 // private) is the same as that in the extended class lineage.
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +02001781 if (success && extends_cl != NULL)
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001782 success = validate_extends_methods(&objmethods, extends_cl);
1783
1784 // Check the new class and object variables are not duplicates of the
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001785 // variables in the extended class lineage. If an interface is extending
1786 // another interface, then it can duplicate the member variables.
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001787 if (success && extends_cl != NULL)
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001788 {
1789 if (is_class)
1790 success = extends_check_dup_members(&objmembers, extends_cl);
1791 else
1792 success = extends_check_intf_var_type(&objmembers, extends_cl);
1793 }
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +02001794
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02001795 // When extending an abstract class, make sure all the abstract methods in
1796 // the parent class are implemented. If the current class is an abstract
1797 // class, then there is no need for this check.
1798 if (success && !is_abstract && extends_cl != NULL
1799 && (extends_cl->class_flags & CLASS_ABSTRACT))
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001800 success = validate_abstract_class_methods(&classfunctions,
1801 &objmethods, extends_cl);
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02001802
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001803 class_T **intf_classes = NULL;
1804
Bram Moolenaar83677162023-01-08 19:54:10 +00001805 // Check all "implements" entries are valid.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001806 if (success && ga_impl.ga_len > 0)
1807 {
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001808 intf_classes = ALLOC_CLEAR_MULT(class_T *, ga_impl.ga_len);
1809
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001810 success = validate_implements_classes(&ga_impl, intf_classes,
1811 &classfunctions, &classmembers,
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02001812 &objmethods, &objmembers,
1813 extends_cl);
Bram Moolenaar94674f22023-01-06 18:42:20 +00001814 }
1815
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001816 // Check no function argument name is used as a class member.
Bram Moolenaard40f00c2023-01-13 17:36:49 +00001817 if (success)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001818 success = check_func_arg_names(&classfunctions, &objmethods,
1819 &classmembers);
Bram Moolenaard40f00c2023-01-13 17:36:49 +00001820
Bram Moolenaareb533502022-12-14 15:06:11 +00001821 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001822 if (success)
1823 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001824 // "endclass" encountered without failures: Create the class.
1825
Bram Moolenaareb533502022-12-14 15:06:11 +00001826 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001827 if (cl == NULL)
1828 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +00001829 if (!is_class)
1830 cl->class_flags = CLASS_INTERFACE;
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02001831 else if (is_abstract)
1832 cl->class_flags = CLASS_ABSTRACT;
Bram Moolenaar554d0312023-01-05 19:59:18 +00001833
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001834 cl->class_refcount = 1;
Bram Moolenaar94674f22023-01-06 18:42:20 +00001835 cl->class_name = vim_strnsave(name_start, name_end - name_start);
Bram Moolenaard505d172022-12-18 21:42:55 +00001836 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001837 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +00001838
Bram Moolenaard0200c82023-01-28 15:19:40 +00001839 if (extends_cl != NULL)
1840 {
1841 cl->class_extends = extends_cl;
1842 extends_cl->class_flags |= CLASS_EXTENDED;
1843 }
Bram Moolenaar83677162023-01-08 19:54:10 +00001844
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001845 // Add class and object variables to "cl".
Bram Moolenaard505d172022-12-18 21:42:55 +00001846 if (add_members_to_class(&classmembers,
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001847 NULL,
1848 0,
Bram Moolenaar83677162023-01-08 19:54:10 +00001849 &cl->class_class_members,
1850 &cl->class_class_member_count) == FAIL
Bram Moolenaard505d172022-12-18 21:42:55 +00001851 || add_members_to_class(&objmembers,
Bram Moolenaar83677162023-01-08 19:54:10 +00001852 extends_cl == NULL ? NULL
1853 : extends_cl->class_obj_members,
1854 extends_cl == NULL ? 0
1855 : extends_cl->class_obj_member_count,
1856 &cl->class_obj_members,
1857 &cl->class_obj_member_count) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +00001858 goto cleanup;
1859
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001860 if (ga_impl.ga_len > 0)
1861 {
1862 // Move the "implements" names into the class.
1863 cl->class_interface_count = ga_impl.ga_len;
1864 cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
1865 if (cl->class_interfaces == NULL)
1866 goto cleanup;
1867 for (int i = 0; i < ga_impl.ga_len; ++i)
1868 cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
1869 VIM_CLEAR(ga_impl.ga_data);
1870 ga_impl.ga_len = 0;
1871
Bram Moolenaard0200c82023-01-28 15:19:40 +00001872 cl->class_interfaces_cl = intf_classes;
1873 intf_classes = NULL;
1874 }
1875
1876 if (cl->class_interface_count > 0 || extends_cl != NULL)
1877 {
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001878 // Add a method and member lookup table to each of the interface
1879 // classes.
1880 if (add_lookup_tables(cl, extends_cl, &objmethods) == FAIL)
1881 goto cleanup;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00001882 }
1883
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001884 // Allocate a typval for each class member and initialize it.
Bram Moolenaar554d0312023-01-05 19:59:18 +00001885 if (is_class && cl->class_class_member_count > 0)
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001886 add_class_members(cl, eap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001887
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001888 int have_new = FALSE;
1889 ufunc_T *class_func = NULL;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001890 for (int i = 0; i < classfunctions.ga_len; ++i)
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001891 {
1892 class_func = ((ufunc_T **)classfunctions.ga_data)[i];
1893 if (STRCMP(class_func->uf_name, "new") == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001894 {
1895 have_new = TRUE;
1896 break;
1897 }
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +02001898 }
1899
1900 if (have_new)
1901 // The return type of new() is an object of class "cl"
1902 class_func->uf_ret_type->tt_class = cl;
1903 else if (is_class && !is_abstract && !have_new)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001904 // No new() method was defined, add the default constructor.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001905 add_default_constructor(cl, &classfunctions, &type_list);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001906
Bram Moolenaar58b40092023-01-11 15:59:05 +00001907 // Move all the functions into the created class.
Yegappan Lakshmanan4b1cc792023-08-19 22:39:33 +02001908 if (add_classfuncs_objmethods(cl, extends_cl, &classfunctions,
1909 &objmethods) == FAIL)
1910 goto cleanup;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001911
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001912 cl->class_type.tt_type = VAR_CLASS;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001913 cl->class_type.tt_class = cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001914 cl->class_object_type.tt_type = VAR_OBJECT;
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00001915 cl->class_object_type.tt_class = cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001916 cl->class_type_list = type_list;
1917
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02001918 class_created(cl);
1919
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001920 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +00001921 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001922
1923 // Add the class to the script-local variables.
Bram Moolenaar94674f22023-01-06 18:42:20 +00001924 // TODO: handle other context, e.g. in a function
Ernie Rael21d32122023-09-02 15:09:18 +02001925 // TODO: does uf_hash need to be cleared?
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001926 typval_T tv;
1927 tv.v_type = VAR_CLASS;
1928 tv.vval.v_class = cl;
Bram Moolenaara86655a2023-01-12 17:06:27 +00001929 is_export = class_export;
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001930 SOURCING_LNUM = start_lnum;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001931 set_var_const(cl->class_name, current_sctx.sc_sid,
Bram Moolenaar83ae6152023-02-25 19:59:31 +00001932 NULL, &tv, FALSE, 0, 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001933 return;
1934 }
1935
1936cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +00001937 if (cl != NULL)
1938 {
1939 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001940 vim_free(cl->class_class_functions);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001941 if (cl->class_interfaces != NULL)
1942 {
1943 for (int i = 0; i < cl->class_interface_count; ++i)
1944 vim_free(cl->class_interfaces[i]);
1945 vim_free(cl->class_interfaces);
1946 }
1947 if (cl->class_interfaces_cl != NULL)
1948 {
1949 for (int i = 0; i < cl->class_interface_count; ++i)
1950 class_unref(cl->class_interfaces_cl[i]);
1951 vim_free(cl->class_interfaces_cl);
1952 }
Bram Moolenaareb533502022-12-14 15:06:11 +00001953 vim_free(cl->class_obj_members);
1954 vim_free(cl->class_obj_methods);
1955 vim_free(cl);
1956 }
1957
Bram Moolenaar83677162023-01-08 19:54:10 +00001958 vim_free(extends);
1959 class_unref(extends_cl);
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00001960
1961 if (intf_classes != NULL)
1962 {
1963 for (int i = 0; i < ga_impl.ga_len; ++i)
1964 class_unref(intf_classes[i]);
1965 vim_free(intf_classes);
1966 }
Bram Moolenaar94674f22023-01-06 18:42:20 +00001967 ga_clear_strings(&ga_impl);
1968
Bram Moolenaard505d172022-12-18 21:42:55 +00001969 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001970 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001971 garray_T *gap = round == 1 ? &classmembers : &objmembers;
1972 if (gap->ga_len == 0 || gap->ga_data == NULL)
1973 continue;
1974
1975 for (int i = 0; i < gap->ga_len; ++i)
1976 {
1977 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
1978 vim_free(m->ocm_name);
1979 vim_free(m->ocm_init);
1980 }
1981 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001982 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001983
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001984 for (int i = 0; i < objmethods.ga_len; ++i)
1985 {
1986 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
1987 func_clear_free(uf, FALSE);
1988 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001989 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00001990
1991 for (int i = 0; i < classfunctions.ga_len; ++i)
1992 {
1993 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
1994 func_clear_free(uf, FALSE);
1995 }
1996 ga_clear(&classfunctions);
1997
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001998 clear_type_list(&type_list);
1999}
2000
2001/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00002002 * Find member "name" in class "cl", set "member_idx" to the member index and
2003 * return its type.
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002004 * When "is_object" is TRUE, then look for object members. Otherwise look for
2005 * class members.
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00002006 * When not found "member_idx" is set to -1 and t_any is returned.
Ernie Rael456ae552023-09-01 18:54:54 +02002007 * Set *p_m ocmmember_T if not NULL
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002008 */
2009 type_T *
2010class_member_type(
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02002011 class_T *cl,
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002012 int is_object,
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02002013 char_u *name,
2014 char_u *name_end,
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002015 int *member_idx)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002016{
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02002017 size_t len = name_end - name;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002018 ocmember_T *m;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002019
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002020 *member_idx = -1; // not found (yet)
2021
2022 m = member_lookup(cl, is_object ? VAR_OBJECT : VAR_CLASS, name, len,
2023 member_idx);
2024 if (m == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002025 {
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002026 member_not_found_msg(cl, is_object ? VAR_OBJECT : VAR_CLASS, name,
2027 len);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002028 return &t_any;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002029 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00002030
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002031 return m->ocm_type;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00002032}
2033
2034/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00002035 * Handle ":enum" up to ":endenum".
2036 */
2037 void
2038ex_enum(exarg_T *eap UNUSED)
2039{
2040 // TODO
2041}
2042
2043/*
2044 * Handle ":type".
2045 */
2046 void
2047ex_type(exarg_T *eap UNUSED)
2048{
2049 // TODO
2050}
2051
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002052/*
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02002053 * Returns OK if a member variable named "name" is present in the class "cl".
2054 * Otherwise returns FAIL. If found, the member variable typval is set in
2055 * "rettv". If "is_object" is TRUE, then the object member variable table is
2056 * searched. Otherwise the class member variable table is searched.
2057 */
2058 static int
2059get_member_tv(
2060 class_T *cl,
2061 int is_object,
2062 char_u *name,
2063 size_t namelen,
2064 typval_T *rettv)
2065{
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002066 ocmember_T *m;
2067 int m_idx;
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02002068
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002069 m = member_lookup(cl, is_object ? VAR_OBJECT : VAR_CLASS, name, namelen,
2070 &m_idx);
2071 if (m == NULL)
2072 return FAIL;
2073
2074 if (*name == '_')
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02002075 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002076 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
2077 return FAIL;
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02002078 }
2079
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002080 // The object only contains a pointer to the class, the member
2081 // values array follows right after that.
2082 object_T *obj = rettv->vval.v_object;
2083 if (is_object)
2084 {
2085 typval_T *tv = (typval_T *)(obj + 1) + m_idx;
2086 copy_tv(tv, rettv);
2087 }
2088 else
2089 copy_tv(&cl->class_members_tv[m_idx], rettv);
2090
2091 object_unref(obj);
2092
2093 return OK;
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02002094}
2095
2096/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002097 * Evaluate what comes after a class:
2098 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00002099 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002100 * - class constructor: SomeClass.new()
2101 * - object member: someObject.varname
2102 * - object method: someObject.SomeMethod()
2103 *
2104 * "*arg" points to the '.'.
2105 * "*arg" is advanced to after the member name or method call.
2106 *
2107 * Returns FAIL or OK.
2108 */
2109 int
2110class_object_index(
2111 char_u **arg,
2112 typval_T *rettv,
2113 evalarg_T *evalarg,
2114 int verbose UNUSED) // give error messages
2115{
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002116 if (VIM_ISWHITE((*arg)[1]))
2117 {
2118 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
2119 return FAIL;
2120 }
2121
2122 ++*arg;
2123 char_u *name = *arg;
2124 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
2125 if (name_end == name)
2126 return FAIL;
2127 size_t len = name_end - name;
2128
Bram Moolenaar552bdca2023-02-17 21:08:50 +00002129 class_T *cl;
2130 if (rettv->v_type == VAR_CLASS)
2131 cl = rettv->vval.v_class;
2132 else // VAR_OBJECT
2133 {
2134 if (rettv->vval.v_object == NULL)
2135 {
2136 emsg(_(e_using_null_object));
2137 return FAIL;
2138 }
2139 cl = rettv->vval.v_object->obj_class;
2140 }
2141
Bram Moolenaard13dd302023-03-11 20:56:35 +00002142 if (cl == NULL)
2143 {
2144 emsg(_(e_incomplete_type));
2145 return FAIL;
2146 }
2147
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002148 if (*name_end == '(')
2149 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002150 ufunc_T *fp;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002151
Ernie Rael4d00b832023-09-11 19:54:42 +02002152 fp = method_lookup(cl, rettv->v_type, name, len, NULL);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002153 if (fp == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002154 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002155 method_not_found_msg(cl, rettv->v_type, name, len);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002156 return FAIL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002157 }
2158
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002159 typval_T argvars[MAX_FUNC_ARGS + 1];
2160 int argcount = 0;
2161
2162 if (*fp->uf_name == '_')
2163 {
2164 // Cannot access a private method outside of a class
2165 semsg(_(e_cannot_access_private_method_str), name);
2166 return FAIL;
2167 }
2168
2169 char_u *argp = name_end;
2170 int ret = get_func_arguments(&argp, evalarg, 0,
2171 argvars, &argcount);
2172 if (ret == FAIL)
2173 return FAIL;
2174
2175 funcexe_T funcexe;
2176 CLEAR_FIELD(funcexe);
2177 funcexe.fe_evaluate = TRUE;
2178 if (rettv->v_type == VAR_OBJECT)
2179 {
2180 funcexe.fe_object = rettv->vval.v_object;
2181 ++funcexe.fe_object->obj_refcount;
2182 }
2183
2184 // Clear the class or object after calling the function, in
2185 // case the refcount is one.
2186 typval_T tv_tofree = *rettv;
2187 rettv->v_type = VAR_UNKNOWN;
2188
2189 // Call the user function. Result goes into rettv;
2190 int error = call_user_func_check(fp, argcount, argvars,
2191 rettv, &funcexe, NULL);
2192
2193 // Clear the previous rettv and the arguments.
2194 clear_tv(&tv_tofree);
2195 for (int idx = 0; idx < argcount; ++idx)
2196 clear_tv(&argvars[idx]);
2197
2198 if (error != FCERR_NONE)
2199 {
2200 user_func_error(error, printable_func_name(fp),
2201 funcexe.fe_found_var);
2202 return FAIL;
2203 }
2204 *arg = argp;
2205 return OK;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002206 }
2207
2208 else if (rettv->v_type == VAR_OBJECT)
2209 {
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02002210 // Search in the object member variable table and the class member
2211 // variable table.
Yegappan Lakshmanan23c92d92023-09-09 11:33:29 +02002212 if (get_member_tv(cl, TRUE, name, len, rettv) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002213 {
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02002214 *arg = name_end;
2215 return OK;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002216 }
2217
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002218 member_not_found_msg(cl, VAR_OBJECT, name, len);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002219 }
2220
Bram Moolenaard505d172022-12-18 21:42:55 +00002221 else if (rettv->v_type == VAR_CLASS)
2222 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002223 int m_idx;
2224
Bram Moolenaard505d172022-12-18 21:42:55 +00002225 // class member
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002226 ocmember_T *m = class_member_lookup(cl, name, len, &m_idx);
2227 if (m == NULL)
Bram Moolenaard505d172022-12-18 21:42:55 +00002228 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002229 member_not_found_msg(cl, VAR_CLASS, name, len);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002230 return FAIL;
Bram Moolenaard505d172022-12-18 21:42:55 +00002231 }
2232
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002233 if (*name == '_')
2234 {
2235 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
2236 return FAIL;
2237 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002238
2239 typval_T *tv = &cl->class_members_tv[m_idx];
2240 copy_tv(tv, rettv);
2241 class_unref(cl);
2242
2243 *arg = name_end;
2244 return OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00002245 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002246
2247 return FAIL;
2248}
2249
2250/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002251 * If "arg" points to a class or object method, return it.
2252 * Otherwise return NULL.
2253 */
2254 ufunc_T *
2255find_class_func(char_u **arg)
2256{
2257 char_u *name = *arg;
2258 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
2259 if (name_end == name || *name_end != '.')
2260 return NULL;
2261
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002262 ufunc_T *fp = NULL;
Yegappan Lakshmanan1689e842023-09-06 20:23:23 +02002263 size_t len = name_end - name;
2264 typval_T tv;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002265 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +00002266 if (eval_variable(name, (int)len,
2267 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002268 return NULL;
2269 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +00002270 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002271
2272 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
2273 : tv.vval.v_object->obj_class;
2274 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +00002275 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002276 char_u *fname = name_end + 1;
2277 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
2278 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +00002279 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002280 len = fname_end - fname;
2281
Ernie Rael4d00b832023-09-11 19:54:42 +02002282 fp = method_lookup(cl, tv.v_type, fname, len, NULL);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002283
Bram Moolenaareb533502022-12-14 15:06:11 +00002284fail_after_eval:
2285 clear_tv(&tv);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002286 return fp;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002287}
2288
2289/*
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002290 * Returns the index of class variable "name" in the class "cl".
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002291 * Returns -1, if the variable is not found.
2292 * If "namelen" is zero, then it is assumed that "name" is NUL terminated.
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00002293 */
2294 int
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002295class_member_idx(class_T *cl, char_u *name, size_t namelen)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00002296{
Ernie Rael4d00b832023-09-11 19:54:42 +02002297 int idx;
2298 class_member_lookup(cl, name, namelen, &idx);
2299 return idx;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +00002300}
2301
2302/*
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002303 * Returns a pointer to the class member variable "name" in the class "cl".
2304 * Returns NULL if the variable is not found.
2305 * The member variable index is set in "idx".
2306 */
2307 ocmember_T *
2308class_member_lookup(class_T *cl, char_u *name, size_t namelen, int *idx)
2309{
Ernie Rael4d00b832023-09-11 19:54:42 +02002310 ocmember_T *ret_m = NULL;
2311 int ret_idx = -1;
2312 for (int i = 0; i < cl->class_class_member_count; ++i)
2313 {
2314 ocmember_T *m = &cl->class_class_members[i];
2315 if (namelen)
2316 {
2317 if (STRNCMP(name, m->ocm_name, namelen) == 0
2318 && m->ocm_name[namelen] == NUL)
2319 {
2320 ret_m = m;
2321 ret_idx = i;
2322 break;
2323 }
2324 }
2325 else if (STRCMP(name, m->ocm_name) == 0)
2326 {
2327 ret_m = m;
2328 ret_idx = i;
2329 break;
2330 }
2331 }
2332 if (idx != NULL)
2333 *idx = ret_idx;
2334 return ret_m;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002335}
2336
2337/*
2338 * Returns the index of class method "name" in the class "cl".
2339 * Returns -1, if the method is not found.
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02002340 */
2341 int
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002342class_method_idx(class_T *cl, char_u *name, size_t namelen)
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02002343{
Ernie Rael4d00b832023-09-11 19:54:42 +02002344 int idx;
2345 class_method_lookup(cl, name, namelen, &idx);
2346 return idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002347}
2348
2349/*
2350 * Returns a pointer to the class method "name" in class "cl".
2351 * Returns NULL if the method is not found.
2352 * The method index is set in "idx".
2353 */
2354 ufunc_T *
2355class_method_lookup(class_T *cl, char_u *name, size_t namelen, int *idx)
2356{
Ernie Rael4d00b832023-09-11 19:54:42 +02002357 ufunc_T *ret_fp = NULL;
2358 int ret_idx = -1;
2359 for (int i = 0; i < cl->class_class_function_count; ++i)
2360 {
2361 ufunc_T *fp = cl->class_class_functions[i];
2362 char_u *ufname = (char_u *)fp->uf_name;
2363 if (STRNCMP(name, ufname, namelen) == 0 && ufname[namelen] == NUL)
2364 {
2365 ret_fp = fp;
2366 ret_idx = i;
2367 break;
2368 }
2369 }
2370 if (idx != NULL)
2371 *idx = ret_idx;
2372 return ret_fp;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002373}
2374
2375/*
2376 * Returns the index of object member variable "name" in the class "cl".
2377 * Returns -1, if the variable is not found.
2378 * If "namelen" is zero, then it is assumed that "name" is NUL terminated.
2379 */
2380 int
2381object_member_idx(class_T *cl, char_u *name, size_t namelen)
2382{
Ernie Rael4d00b832023-09-11 19:54:42 +02002383 int idx;
2384 object_member_lookup(cl, name, namelen, &idx);
2385 return idx;
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02002386}
2387
2388/*
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002389 * Returns a pointer to the object member variable "name" in the class "cl".
2390 * Returns NULL if the variable is not found.
2391 * The object member variable index is set in "idx".
2392 */
2393 ocmember_T *
2394object_member_lookup(class_T *cl, char_u *name, size_t namelen, int *idx)
2395{
Ernie Rael4d00b832023-09-11 19:54:42 +02002396 ocmember_T *ret_m = NULL;
2397 int ret_idx = -1;
2398 for (int i = 0; i < cl->class_obj_member_count; ++i)
2399 {
2400 ocmember_T *m = &cl->class_obj_members[i];
2401 if (namelen)
2402 {
2403 if (STRNCMP(name, m->ocm_name, namelen) == 0
2404 && m->ocm_name[namelen] == NUL)
2405 {
2406 ret_m = m;
2407 ret_idx = i;
2408 break;
2409 }
2410 }
2411 else if (STRCMP(name, m->ocm_name) == 0)
2412 {
2413 ret_m = m;
2414 ret_idx = i;
2415 break;
2416 }
2417 }
2418 if (idx != NULL)
2419 *idx = ret_idx;
2420 return ret_m;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002421}
2422
2423/*
2424 * Returns the index of object method "name" in the class "cl".
2425 * Returns -1, if the method is not found.
2426 */
2427 int
2428object_method_idx(class_T *cl, char_u *name, size_t namelen)
2429{
Ernie Rael4d00b832023-09-11 19:54:42 +02002430 int idx;
2431 object_method_lookup(cl, name, namelen, &idx);
2432 return idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002433}
2434
2435/*
2436 * Returns a pointer to the object method "name" in class "cl".
2437 * Returns NULL if the method is not found.
2438 * The object method index is set in "idx".
2439 */
2440 ufunc_T *
2441object_method_lookup(class_T *cl, char_u *name, size_t namelen, int *idx)
2442{
Ernie Rael4d00b832023-09-11 19:54:42 +02002443 ufunc_T *ret_fp = NULL;
2444 int ret_idx = -1;
2445 for (int i = 0; i < cl->class_obj_method_count; ++i)
2446 {
2447 ufunc_T *fp = cl->class_obj_methods[i];
2448 // Use a separate pointer to avoid that ASAN complains about
2449 // uf_name[] only being 4 characters.
2450 char_u *ufname = (char_u *)fp->uf_name;
2451 if (STRNCMP(name, ufname, namelen) == 0 && ufname[namelen] == NUL)
2452 {
2453 ret_fp = fp;
2454 ret_idx = i;
2455 break;
2456 }
2457 }
2458 if (idx != NULL)
2459 *idx = ret_idx;
2460 return ret_fp;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002461}
2462
2463/*
2464 * Lookup a class or object member variable by name. If v_type is VAR_CLASS,
2465 * then lookup a class member variable and if it is VAR_OBJECT, then lookup a
2466 * object member variable.
2467 *
2468 * Returns a pointer to the member variable structure if variable is found.
2469 * Otherwise returns NULL. The member variable index is set in "*idx".
2470 */
2471 ocmember_T *
2472member_lookup(
2473 class_T *cl,
2474 vartype_T v_type,
2475 char_u *name,
2476 size_t namelen,
2477 int *idx)
2478{
2479 if (v_type == VAR_CLASS)
2480 return class_member_lookup(cl, name, namelen, idx);
2481 else
2482 return object_member_lookup(cl, name, namelen, idx);
2483}
2484
2485/*
2486 * Lookup a class or object method by name. If v_type is VAR_CLASS, then
2487 * lookup a class method and if it is VAR_OBJECT, then lookup a object method.
2488 *
2489 * Returns a pointer to the method structure if variable is found.
2490 * Otherwise returns NULL. The method variable index is set in "*idx".
2491 */
2492 ufunc_T *
2493method_lookup(
2494 class_T *cl,
2495 vartype_T v_type,
2496 char_u *name,
2497 size_t namelen,
2498 int *idx)
2499{
2500 if (v_type == VAR_CLASS)
2501 return class_method_lookup(cl, name, namelen, idx);
2502 else
2503 return object_method_lookup(cl, name, namelen, idx);
2504}
2505
2506/*
Bram Moolenaar62a69232023-01-24 15:07:04 +00002507 * Return TRUE if current context "cctx_arg" is inside class "cl".
2508 * Return FALSE if not.
2509 */
2510 int
2511inside_class(cctx_T *cctx_arg, class_T *cl)
2512{
2513 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
Ernie Raelcf138d42023-09-06 20:45:03 +02002514 if (cctx->ctx_ufunc != NULL
2515 && class_instance_of(cctx->ctx_ufunc->uf_class, cl))
Bram Moolenaar62a69232023-01-24 15:07:04 +00002516 return TRUE;
2517 return FALSE;
2518}
2519
2520/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002521 * Make a copy of an object.
2522 */
2523 void
2524copy_object(typval_T *from, typval_T *to)
2525{
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02002526 if (from->vval.v_object == NULL)
2527 to->vval.v_object = NULL;
2528 else
2529 {
2530 to->vval.v_object = from->vval.v_object;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002531 ++to->vval.v_object->obj_refcount;
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02002532 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002533}
2534
2535/*
2536 * Free an object.
2537 */
2538 static void
2539object_clear(object_T *obj)
2540{
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01002541 // Avoid a recursive call, it can happen if "obj" has a circular reference.
2542 obj->obj_refcount = INT_MAX;
2543
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002544 class_T *cl = obj->obj_class;
2545
Jia-Ju Bai5b0889b2023-08-13 20:04:04 +02002546 if (!cl)
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02002547 return;
Jia-Ju Bai5b0889b2023-08-13 20:04:04 +02002548
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002549 // the member values are just after the object structure
2550 typval_T *tv = (typval_T *)(obj + 1);
2551 for (int i = 0; i < cl->class_obj_member_count; ++i)
2552 clear_tv(tv + i);
2553
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002554 // Remove from the list headed by "first_object".
2555 object_cleared(obj);
2556
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002557 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002558 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002559}
2560
2561/*
2562 * Unreference an object.
2563 */
2564 void
2565object_unref(object_T *obj)
2566{
2567 if (obj != NULL && --obj->obj_refcount <= 0)
2568 object_clear(obj);
2569}
2570
2571/*
2572 * Make a copy of a class.
2573 */
2574 void
2575copy_class(typval_T *from, typval_T *to)
2576{
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02002577 if (from->vval.v_class == NULL)
2578 to->vval.v_class = NULL;
2579 else
2580 {
2581 to->vval.v_class = from->vval.v_class;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002582 ++to->vval.v_class->class_refcount;
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +02002583 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002584}
2585
2586/*
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02002587 * Free the class "cl" and its contents.
2588 */
2589 static void
2590class_free(class_T *cl)
2591{
2592 // Freeing what the class contains may recursively come back here.
2593 // Clear "class_name" first, if it is NULL the class does not need to
2594 // be freed.
2595 VIM_CLEAR(cl->class_name);
2596
2597 class_unref(cl->class_extends);
2598
2599 for (int i = 0; i < cl->class_interface_count; ++i)
2600 {
2601 vim_free(((char_u **)cl->class_interfaces)[i]);
2602 if (cl->class_interfaces_cl[i] != NULL)
2603 class_unref(cl->class_interfaces_cl[i]);
2604 }
2605 vim_free(cl->class_interfaces);
2606 vim_free(cl->class_interfaces_cl);
2607
2608 itf2class_T *next;
2609 for (itf2class_T *i2c = cl->class_itf2class; i2c != NULL; i2c = next)
2610 {
2611 next = i2c->i2c_next;
2612 vim_free(i2c);
2613 }
2614
2615 for (int i = 0; i < cl->class_class_member_count; ++i)
2616 {
2617 ocmember_T *m = &cl->class_class_members[i];
2618 vim_free(m->ocm_name);
2619 vim_free(m->ocm_init);
2620 if (cl->class_members_tv != NULL)
2621 clear_tv(&cl->class_members_tv[i]);
2622 }
2623 vim_free(cl->class_class_members);
2624 vim_free(cl->class_members_tv);
2625
2626 for (int i = 0; i < cl->class_obj_member_count; ++i)
2627 {
2628 ocmember_T *m = &cl->class_obj_members[i];
2629 vim_free(m->ocm_name);
2630 vim_free(m->ocm_init);
2631 }
2632 vim_free(cl->class_obj_members);
2633
2634 for (int i = 0; i < cl->class_class_function_count; ++i)
2635 {
2636 ufunc_T *uf = cl->class_class_functions[i];
2637 func_clear_free(uf, FALSE);
2638 }
2639 vim_free(cl->class_class_functions);
2640
2641 for (int i = 0; i < cl->class_obj_method_count; ++i)
2642 {
2643 ufunc_T *uf = cl->class_obj_methods[i];
2644 func_clear_free(uf, FALSE);
2645 }
2646 vim_free(cl->class_obj_methods);
2647
2648 clear_type_list(&cl->class_type_list);
2649
2650 class_cleared(cl);
2651
2652 vim_free(cl);
2653}
2654
2655/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002656 * Unreference a class. Free it when the reference count goes down to zero.
2657 */
2658 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002659class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002660{
Bram Moolenaard505d172022-12-18 21:42:55 +00002661 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02002662 class_free(cl);
2663}
2664
2665/*
2666 * Go through the list of all classes and free items without "copyID".
2667 */
2668 int
2669class_free_nonref(int copyID)
2670{
2671 int did_free = FALSE;
2672
2673 for (class_T *cl = first_class; cl != NULL; cl = next_nonref_class)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002674 {
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02002675 next_nonref_class = cl->class_next_used;
2676 if ((cl->class_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00002677 {
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02002678 // Free the class and items it contains.
2679 class_free(cl);
2680 did_free = TRUE;
Bram Moolenaara94bd9d2023-01-12 15:01:32 +00002681 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002682 }
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02002683
2684 next_nonref_class = NULL;
2685 return did_free;
2686}
2687
2688 int
2689set_ref_in_classes(int copyID)
2690{
2691 for (class_T *cl = first_class; cl != NULL; cl = cl->class_next_used)
2692 set_ref_in_item_class(cl, copyID, NULL, NULL);
2693
2694 return FALSE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002695}
2696
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002697static object_T *first_object = NULL;
2698
2699/*
2700 * Call this function when an object has been created. It will be added to the
2701 * list headed by "first_object".
2702 */
2703 void
2704object_created(object_T *obj)
2705{
2706 if (first_object != NULL)
2707 {
2708 obj->obj_next_used = first_object;
2709 first_object->obj_prev_used = obj;
2710 }
2711 first_object = obj;
2712}
2713
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01002714static object_T *next_nonref_obj = NULL;
2715
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002716/*
2717 * Call this function when an object has been cleared and is about to be freed.
2718 * It is removed from the list headed by "first_object".
2719 */
2720 void
2721object_cleared(object_T *obj)
2722{
2723 if (obj->obj_next_used != NULL)
2724 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
2725 if (obj->obj_prev_used != NULL)
2726 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
2727 else if (first_object == obj)
2728 first_object = obj->obj_next_used;
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01002729
2730 // update the next object to check if needed
2731 if (obj == next_nonref_obj)
2732 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002733}
2734
2735/*
2736 * Go through the list of all objects and free items without "copyID".
2737 */
2738 int
2739object_free_nonref(int copyID)
2740{
2741 int did_free = FALSE;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002742
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01002743 for (object_T *obj = first_object; obj != NULL; obj = next_nonref_obj)
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002744 {
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01002745 next_nonref_obj = obj->obj_next_used;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002746 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
2747 {
2748 // Free the object and items it contains.
2749 object_clear(obj);
2750 did_free = TRUE;
2751 }
2752 }
2753
Bram Moolenaarf7ca56f2023-06-05 16:53:25 +01002754 next_nonref_obj = NULL;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00002755 return did_free;
2756}
2757
LemonBoyafe04662023-08-23 21:08:11 +02002758/*
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002759 * Echo a class or object method not found message.
2760 */
2761 void
2762method_not_found_msg(class_T *cl, vartype_T v_type, char_u *name, size_t len)
2763{
2764 char_u *method_name = vim_strnsave(name, len);
2765 if ((v_type == VAR_OBJECT)
2766 && (class_method_idx(cl, name, len) >= 0))
2767 {
2768 // If this is a class method, then give a different error
2769 if (*name == '_')
2770 semsg(_(e_cannot_access_private_method_str), method_name);
2771 else
2772 semsg(_(e_class_member_str_accessible_only_using_class_str),
2773 method_name, cl->class_name);
2774 }
2775 else if ((v_type == VAR_CLASS)
2776 && (object_method_idx(cl, name, len) >= 0))
2777 {
2778 // If this is an object method, then give a different error
2779 if (*name == '_')
2780 semsg(_(e_cannot_access_private_method_str), method_name);
2781 else
2782 semsg(_(e_object_member_str_accessible_only_using_object_str),
2783 method_name, cl->class_name);
2784 }
2785 else
2786 semsg(_(e_method_not_found_on_class_str_str), cl->class_name,
2787 method_name);
2788 vim_free(method_name);
2789}
2790
2791/*
2792 * Echo a class or object member not found message.
2793 */
2794 void
2795member_not_found_msg(class_T *cl, vartype_T v_type, char_u *name, size_t len)
2796{
2797 char_u *varname = len ? vim_strnsave(name, len) : vim_strsave(name);
2798
2799 if (v_type == VAR_OBJECT)
2800 {
2801 if (class_member_idx(cl, name, len) >= 0)
2802 semsg(_(e_class_member_str_accessible_only_using_class_str),
2803 varname, cl->class_name);
2804 else
2805 semsg(_(e_member_not_found_on_object_str_str), cl->class_name,
2806 varname);
2807 }
2808 else
2809 {
2810 if (object_member_idx(cl, name, len) >= 0)
2811 semsg(_(e_object_member_str_accessible_only_using_object_str),
2812 varname, cl->class_name);
2813 else
2814 semsg(_(e_class_member_str_not_found_in_class_str),
2815 varname, cl->class_name);
2816 }
2817 vim_free(varname);
2818}
2819
2820/*
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02002821 * Return TRUE when the class "cl", its base class or one of the implemented
2822 * interfaces matches the class "other_cl".
LemonBoyafe04662023-08-23 21:08:11 +02002823 */
2824 int
2825class_instance_of(class_T *cl, class_T *other_cl)
2826{
2827 if (cl == other_cl)
2828 return TRUE;
2829
2830 // Recursively check the base classes.
2831 for (; cl != NULL; cl = cl->class_extends)
2832 {
2833 if (cl == other_cl)
2834 return TRUE;
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02002835 // Check the implemented interfaces and the super interfaces
LemonBoyafe04662023-08-23 21:08:11 +02002836 for (int i = cl->class_interface_count - 1; i >= 0; --i)
Yegappan Lakshmanan92d9ee52023-09-17 17:03:19 +02002837 {
2838 class_T *intf = cl->class_interfaces_cl[i];
2839 while (intf != NULL)
2840 {
2841 if (intf == other_cl)
2842 return TRUE;
2843 // check the super interfaces
2844 intf = intf->class_extends;
2845 }
2846 }
LemonBoyafe04662023-08-23 21:08:11 +02002847 }
2848
2849 return FALSE;
2850}
2851
2852/*
2853 * "instanceof(object, classinfo)" function
2854 */
2855 void
2856f_instanceof(typval_T *argvars, typval_T *rettv)
2857{
2858 typval_T *object_tv = &argvars[0];
2859 typval_T *classinfo_tv = &argvars[1];
2860 listitem_T *li;
2861
2862 rettv->vval.v_number = VVAL_FALSE;
2863
2864 if (check_for_object_arg(argvars, 0) == FAIL
2865 || check_for_class_or_list_arg(argvars, 1) == FAIL)
2866 return;
2867
2868 if (classinfo_tv->v_type == VAR_LIST)
2869 {
2870 FOR_ALL_LIST_ITEMS(classinfo_tv->vval.v_list, li)
2871 {
2872 if (li->li_tv.v_type != VAR_CLASS)
2873 {
2874 emsg(_(e_class_required));
2875 return;
2876 }
2877
2878 if (class_instance_of(object_tv->vval.v_object->obj_class,
2879 li->li_tv.vval.v_class) == TRUE)
2880 {
2881 rettv->vval.v_number = VVAL_TRUE;
2882 return;
2883 }
2884 }
2885 }
2886 else if (classinfo_tv->v_type == VAR_CLASS)
2887 {
2888 rettv->vval.v_number = class_instance_of(object_tv->vval.v_object->obj_class,
2889 classinfo_tv->vval.v_class);
2890 }
2891}
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00002892
2893#endif // FEAT_EVAL