blob: 682211a59f1c7472f0b122ae6ff9debffc989c0b [file] [log] [blame]
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9class.c: Vim9 script class support
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
24/*
Bram Moolenaard505d172022-12-18 21:42:55 +000025 * Parse a member declaration, both object and class member.
26 * Returns OK or FAIL. When OK then "varname_end" is set to just after the
27 * variable name and "type_ret" is set to the decleared or detected type.
28 * "init_expr" is set to the initialisation expression (allocated), if there is
Bram Moolenaar554d0312023-01-05 19:59:18 +000029 * one. For an interface "init_expr" is NULL.
Bram Moolenaard505d172022-12-18 21:42:55 +000030 */
31 static int
32parse_member(
33 exarg_T *eap,
34 char_u *line,
35 char_u *varname,
36 int has_public, // TRUE if "public" seen before "varname"
37 char_u **varname_end,
38 garray_T *type_list,
39 type_T **type_ret,
40 char_u **init_expr)
41{
42 *varname_end = to_name_end(varname, FALSE);
43 if (*varname == '_' && has_public)
44 {
45 semsg(_(e_public_member_name_cannot_start_with_underscore_str), line);
46 return FAIL;
47 }
48
49 char_u *colon = skipwhite(*varname_end);
50 char_u *type_arg = colon;
51 type_T *type = NULL;
52 if (*colon == ':')
53 {
54 if (VIM_ISWHITE(**varname_end))
55 {
56 semsg(_(e_no_white_space_allowed_before_colon_str), varname);
57 return FAIL;
58 }
59 if (!VIM_ISWHITE(colon[1]))
60 {
61 semsg(_(e_white_space_required_after_str_str), ":", varname);
62 return FAIL;
63 }
64 type_arg = skipwhite(colon + 1);
65 type = parse_type(&type_arg, type_list, TRUE);
66 if (type == NULL)
67 return FAIL;
68 }
69
70 char_u *expr_start = skipwhite(type_arg);
71 char_u *expr_end = expr_start;
72 if (type == NULL && *expr_start != '=')
73 {
74 emsg(_(e_type_or_initialization_required));
75 return FAIL;
76 }
77
78 if (*expr_start == '=')
79 {
80 if (!VIM_ISWHITE(expr_start[-1]) || !VIM_ISWHITE(expr_start[1]))
81 {
82 semsg(_(e_white_space_required_before_and_after_str_at_str),
83 "=", type_arg);
84 return FAIL;
85 }
86 expr_start = skipwhite(expr_start + 1);
87
88 expr_end = expr_start;
89 evalarg_T evalarg;
90 fill_evalarg_from_eap(&evalarg, eap, FALSE);
91 skip_expr(&expr_end, NULL);
92
93 if (type == NULL)
94 {
95 // No type specified, use the type of the initializer.
96 typval_T tv;
97 tv.v_type = VAR_UNKNOWN;
98 char_u *expr = expr_start;
99 int res = eval0(expr, &tv, eap, &evalarg);
100
101 if (res == OK)
Bram Moolenaare83c1332023-01-02 21:04:04 +0000102 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000103 type = typval2type(&tv, get_copyID(), type_list,
104 TVTT_DO_MEMBER);
Bram Moolenaare83c1332023-01-02 21:04:04 +0000105 clear_tv(&tv);
106 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000107 if (type == NULL)
108 {
109 semsg(_(e_cannot_get_object_member_type_from_initializer_str),
110 expr_start);
111 clear_evalarg(&evalarg, NULL);
112 return FAIL;
113 }
114 }
115 clear_evalarg(&evalarg, NULL);
116 }
117 if (!valid_declaration_type(type))
118 return FAIL;
119
120 *type_ret = type;
121 if (expr_end > expr_start)
Bram Moolenaar554d0312023-01-05 19:59:18 +0000122 {
123 if (init_expr == NULL)
124 {
125 emsg(_(e_cannot_initialize_member_in_interface));
126 return FAIL;
127 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000128 *init_expr = vim_strnsave(expr_start, expr_end - expr_start);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000129 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000130 return OK;
131}
132
133/*
134 * Add a member to an object or a class.
135 * Returns OK when successful, "init_expr" will be consumed then.
136 * Returns FAIL otherwise, caller might need to free "init_expr".
137 */
138 static int
139add_member(
140 garray_T *gap,
141 char_u *varname,
142 char_u *varname_end,
143 int has_public,
144 type_T *type,
145 char_u *init_expr)
146{
147 if (ga_grow(gap, 1) == FAIL)
148 return FAIL;
149 ocmember_T *m = ((ocmember_T *)gap->ga_data) + gap->ga_len;
150 m->ocm_name = vim_strnsave(varname, varname_end - varname);
151 m->ocm_access = has_public ? ACCESS_ALL
152 : *varname == '_' ? ACCESS_PRIVATE : ACCESS_READ;
153 m->ocm_type = type;
154 if (init_expr != NULL)
155 m->ocm_init = init_expr;
156 ++gap->ga_len;
157 return OK;
158}
159
160/*
161 * Move the class or object members found while parsing a class into the class.
162 * "gap" contains the found members.
163 * "members" will be set to the newly allocated array of members and
164 * "member_count" set to the number of members.
165 * Returns OK or FAIL.
166 */
167 static int
168add_members_to_class(
169 garray_T *gap,
170 ocmember_T **members,
171 int *member_count)
172{
173 *member_count = gap->ga_len;
174 *members = gap->ga_len == 0 ? NULL : ALLOC_MULT(ocmember_T, gap->ga_len);
175 if (gap->ga_len > 0 && *members == NULL)
176 return FAIL;
Bram Moolenaar8efdcee2022-12-19 12:18:09 +0000177 if (gap->ga_len > 0)
178 mch_memmove(*members, gap->ga_data, sizeof(ocmember_T) * gap->ga_len);
Bram Moolenaard505d172022-12-18 21:42:55 +0000179 VIM_CLEAR(gap->ga_data);
180 return OK;
181}
182
183/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000184 * Handle ":class" and ":abstract class" up to ":endclass".
Bram Moolenaar554d0312023-01-05 19:59:18 +0000185 * Handle ":interface" up to ":endinterface".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000186 */
187 void
188ex_class(exarg_T *eap)
189{
Bram Moolenaar554d0312023-01-05 19:59:18 +0000190 int is_class = eap->cmdidx == CMD_class; // FALSE for :interface
191
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000192 if (!current_script_is_vim9()
193 || (cmdmod.cmod_flags & CMOD_LEGACY)
194 || !getline_equal(eap->getline, eap->cookie, getsourceline))
195 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000196 if (is_class)
197 emsg(_(e_class_can_only_be_defined_in_vim9_script));
198 else
199 emsg(_(e_interface_can_only_be_defined_in_vim9_script));
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000200 return;
201 }
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000202
203 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000204 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000205 if (is_abstract)
206 {
207 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
208 {
209 semsg(_(e_invalid_argument_str), arg);
210 return;
211 }
212 arg = skipwhite(arg + 5);
213 }
214
215 if (!ASCII_ISUPPER(*arg))
216 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000217 if (is_class)
218 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
219 else
220 semsg(_(e_interface_name_must_start_with_uppercase_letter_str),
221 arg);
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000222 return;
223 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000224 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
225 if (!IS_WHITE_OR_NUL(*name_end))
226 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000227 semsg(_(e_white_space_required_after_name_str), arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000228 return;
229 }
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000230
231 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000232 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000233 // extends SomeClass
234 // implements SomeInterface
235 // specifies SomeInterface
Bram Moolenaard505d172022-12-18 21:42:55 +0000236 // check that nothing follows
237 // handle "is_export" if it is set
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000238
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000239 garray_T type_list; // list of pointers to allocated types
240 ga_init2(&type_list, sizeof(type_T *), 10);
241
Bram Moolenaard505d172022-12-18 21:42:55 +0000242 // Growarray with class members declared in the class.
243 garray_T classmembers;
244 ga_init2(&classmembers, sizeof(ocmember_T), 10);
245
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000246 // Growarray with functions declared in the class.
247 garray_T classfunctions;
248 ga_init2(&classfunctions, sizeof(ufunc_T *), 10);
Bram Moolenaard505d172022-12-18 21:42:55 +0000249
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000250 // Growarray with object members declared in the class.
251 garray_T objmembers;
Bram Moolenaard505d172022-12-18 21:42:55 +0000252 ga_init2(&objmembers, sizeof(ocmember_T), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000253
254 // Growarray with object methods declared in the class.
255 garray_T objmethods;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000256 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000257
258 /*
Bram Moolenaar554d0312023-01-05 19:59:18 +0000259 * Go over the body of the class/interface until "endclass" or
260 * "endinterface" is found.
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000261 */
262 char_u *theline = NULL;
263 int success = FALSE;
264 for (;;)
265 {
266 vim_free(theline);
267 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
268 if (theline == NULL)
269 break;
270 char_u *line = skipwhite(theline);
271
Bram Moolenaar418b5472022-12-20 13:38:22 +0000272 // Skip empty and comment lines.
273 if (*line == NUL)
274 continue;
275 if (*line == '#')
276 {
277 if (vim9_bad_comment(line))
278 break;
279 continue;
280 }
281
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000282 char_u *p = line;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000283 char *end_name = is_class ? "endclass" : "endinterface";
284 if (checkforcmd(&p, end_name, is_class ? 4 : 5))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000285 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000286 if (STRNCMP(line, end_name, is_class ? 8 : 12) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000287 semsg(_(e_command_cannot_be_shortened_str), line);
288 else if (*p == '|' || !ends_excmd2(line, p))
289 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar98aeb212022-12-08 22:09:14 +0000290 else
291 success = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000292 break;
293 }
Bram Moolenaar554d0312023-01-05 19:59:18 +0000294 char *wrong_name = is_class ? "endinterface" : "endclass";
295 if (checkforcmd(&p, wrong_name, is_class ? 5 : 4))
296 {
297 semsg(_(e_invalid_command_str), line);
298 break;
299 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000300
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000301 int has_public = FALSE;
302 if (checkforcmd(&p, "public", 3))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000303 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000304 if (STRNCMP(line, "public", 6) != 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000305 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000306 semsg(_(e_command_cannot_be_shortened_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000307 break;
308 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000309 has_public = TRUE;
310 p = skipwhite(line + 6);
311
Bram Moolenaard505d172022-12-18 21:42:55 +0000312 if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000313 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000314 emsg(_(e_public_must_be_followed_by_this_or_static));
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000315 break;
316 }
317 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000318
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000319 int has_static = FALSE;
320 char_u *ps = p;
321 if (checkforcmd(&p, "static", 4))
322 {
323 if (STRNCMP(ps, "static", 6) != 0)
324 {
325 semsg(_(e_command_cannot_be_shortened_str), ps);
326 break;
327 }
328 has_static = TRUE;
329 p = skipwhite(ps + 6);
330 }
331
Bram Moolenaard505d172022-12-18 21:42:55 +0000332 // object members (public, read access, private):
333 // "this._varname"
334 // "this.varname"
335 // "public this.varname"
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000336 if (STRNCMP(p, "this", 4) == 0)
337 {
338 if (p[4] != '.' || !eval_isnamec1(p[5]))
339 {
340 semsg(_(e_invalid_object_member_declaration_str), p);
341 break;
342 }
343 char_u *varname = p + 5;
Bram Moolenaard505d172022-12-18 21:42:55 +0000344 char_u *varname_end = NULL;
Bram Moolenaar74e12742022-12-13 21:14:28 +0000345 type_T *type = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +0000346 char_u *init_expr = NULL;
347 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000348 &varname_end, &type_list, &type,
349 is_class ? &init_expr: NULL) == FAIL)
Bram Moolenaard505d172022-12-18 21:42:55 +0000350 break;
351 if (add_member(&objmembers, varname, varname_end,
352 has_public, type, init_expr) == FAIL)
Bram Moolenaar74e12742022-12-13 21:14:28 +0000353 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000354 vim_free(init_expr);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000355 break;
356 }
Bram Moolenaard505d172022-12-18 21:42:55 +0000357 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000358
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000359 // constructors:
360 // def new()
361 // enddef
362 // def newOther()
363 // enddef
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000364 // object methods and class functions:
365 // def SomeMethod()
366 // enddef
367 // static def ClassFunction()
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000368 // enddef
369 // TODO:
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000370 // def <Tval> someMethod()
371 // enddef
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000372 else if (checkforcmd(&p, "def", 3))
373 {
374 exarg_T ea;
375 garray_T lines_to_free;
376
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000377 // TODO: error for "public static def Func()"?
378
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000379 CLEAR_FIELD(ea);
380 ea.cmd = line;
381 ea.arg = p;
382 ea.cmdidx = CMD_def;
383 ea.getline = eap->getline;
384 ea.cookie = eap->cookie;
385
386 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +0000387 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free,
388 is_class ? CF_CLASS : CF_INTERFACE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000389 ga_clear_strings(&lines_to_free);
390
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000391 if (uf != NULL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000392 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000393 int is_new = STRNCMP(uf->uf_name, "new", 3) == 0;
394 garray_T *fgap = has_static || is_new
395 ? &classfunctions : &objmethods;
396 if (ga_grow(fgap, 1) == OK)
397 {
398 if (is_new)
399 uf->uf_flags |= FC_NEW;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000400
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000401 ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf;
402 ++fgap->ga_len;
403 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000404 }
405 }
406
407 // class members
408 else if (has_static)
409 {
410 // class members (public, read access, private):
411 // "static _varname"
412 // "static varname"
413 // "public static varname"
414 char_u *varname = p;
415 char_u *varname_end = NULL;
416 type_T *type = NULL;
417 char_u *init_expr = NULL;
418 if (parse_member(eap, line, varname, has_public,
Bram Moolenaar554d0312023-01-05 19:59:18 +0000419 &varname_end, &type_list, &type,
420 is_class ? &init_expr : NULL) == FAIL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000421 break;
422 if (add_member(&classmembers, varname, varname_end,
423 has_public, type, init_expr) == FAIL)
424 {
425 vim_free(init_expr);
426 break;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000427 }
428 }
429
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000430 else
431 {
Bram Moolenaar554d0312023-01-05 19:59:18 +0000432 if (is_class)
433 semsg(_(e_not_valid_command_in_class_str), line);
434 else
435 semsg(_(e_not_valid_command_in_interface_str), line);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000436 break;
437 }
438 }
439 vim_free(theline);
440
Bram Moolenaareb533502022-12-14 15:06:11 +0000441 class_T *cl = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000442 if (success)
443 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000444 // "endclass" encountered without failures: Create the class.
445
Bram Moolenaareb533502022-12-14 15:06:11 +0000446 cl = ALLOC_CLEAR_ONE(class_T);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000447 if (cl == NULL)
448 goto cleanup;
Bram Moolenaar554d0312023-01-05 19:59:18 +0000449 if (!is_class)
450 cl->class_flags = CLASS_INTERFACE;
451
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000452 cl->class_refcount = 1;
453 cl->class_name = vim_strnsave(arg, name_end - arg);
Bram Moolenaard505d172022-12-18 21:42:55 +0000454 if (cl->class_name == NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000455 goto cleanup;
Bram Moolenaard505d172022-12-18 21:42:55 +0000456
457 // Add class and object members to "cl".
458 if (add_members_to_class(&classmembers,
459 &cl->class_class_members,
460 &cl->class_class_member_count) == FAIL
461 || add_members_to_class(&objmembers,
462 &cl->class_obj_members,
463 &cl->class_obj_member_count) == FAIL)
464 goto cleanup;
465
Bram Moolenaar554d0312023-01-05 19:59:18 +0000466 if (is_class && cl->class_class_member_count > 0)
Bram Moolenaard505d172022-12-18 21:42:55 +0000467 {
468 // Allocate a typval for each class member and initialize it.
469 cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
470 cl->class_class_member_count);
471 if (cl->class_members_tv != NULL)
472 for (int i = 0; i < cl->class_class_member_count; ++i)
473 {
474 ocmember_T *m = &cl->class_class_members[i];
475 typval_T *tv = &cl->class_members_tv[i];
476 if (m->ocm_init != NULL)
477 {
478 typval_T *etv = eval_expr(m->ocm_init, eap);
479 if (etv != NULL)
480 {
481 *tv = *etv;
482 vim_free(etv);
483 }
484 }
485 else
486 {
487 // TODO: proper default value
488 tv->v_type = m->ocm_type->tt_type;
489 tv->vval.v_string = NULL;
490 }
491 }
492 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000493
494 int have_new = FALSE;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000495 for (int i = 0; i < classfunctions.ga_len; ++i)
496 if (STRCMP(((ufunc_T **)classfunctions.ga_data)[i]->uf_name,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000497 "new") == 0)
498 {
499 have_new = TRUE;
500 break;
501 }
502 if (!have_new)
503 {
504 // No new() method was defined, add the default constructor.
505 garray_T fga;
506 ga_init2(&fga, 1, 1000);
507 ga_concat(&fga, (char_u *)"new(");
508 for (int i = 0; i < cl->class_obj_member_count; ++i)
509 {
510 if (i > 0)
511 ga_concat(&fga, (char_u *)", ");
512 ga_concat(&fga, (char_u *)"this.");
Bram Moolenaard505d172022-12-18 21:42:55 +0000513 ocmember_T *m = cl->class_obj_members + i;
514 ga_concat(&fga, (char_u *)m->ocm_name);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000515 ga_concat(&fga, (char_u *)" = v:none");
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000516 }
517 ga_concat(&fga, (char_u *)")\nenddef\n");
518 ga_append(&fga, NUL);
519
520 exarg_T fea;
521 CLEAR_FIELD(fea);
522 fea.cmdidx = CMD_def;
523 fea.cmd = fea.arg = fga.ga_data;
524
525 garray_T lines_to_free;
526 ga_init2(&lines_to_free, sizeof(char_u *), 50);
527
Bram Moolenaar554d0312023-01-05 19:59:18 +0000528 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free,
529 is_class ? CF_CLASS : CF_INTERFACE);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000530
531 ga_clear_strings(&lines_to_free);
532 vim_free(fga.ga_data);
533
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000534 if (nf != NULL && ga_grow(&classfunctions, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000535 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000536 ((ufunc_T **)classfunctions.ga_data)[classfunctions.ga_len] = nf;
537 ++classfunctions.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000538
539 nf->uf_flags |= FC_NEW;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000540 nf->uf_ret_type = get_type_ptr(&type_list);
541 if (nf->uf_ret_type != NULL)
542 {
543 nf->uf_ret_type->tt_type = VAR_OBJECT;
544 nf->uf_ret_type->tt_member = (type_T *)cl;
545 nf->uf_ret_type->tt_argcount = 0;
546 nf->uf_ret_type->tt_args = NULL;
547 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000548 }
549 }
550
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000551 // loop 1: class functions, loop 2: object methods
552 for (int loop = 1; loop <= 2; ++loop)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000553 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000554 garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
555 int *fcount = loop == 1 ? &cl->class_class_function_count
556 : &cl->class_obj_method_count;
557 ufunc_T ***fup = loop == 1 ? &cl->class_class_functions
558 : &cl->class_obj_methods;
559
560 *fcount = gap->ga_len;
561 if (gap->ga_len == 0)
562 {
563 *fup = NULL;
564 continue;
565 }
566 *fup = ALLOC_MULT(ufunc_T *, gap->ga_len);
567 if (*fup == NULL)
568 goto cleanup;
569 mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len);
570 vim_free(gap->ga_data);
571
572 // Set the class pointer on all the object methods.
573 for (int i = 0; i < gap->ga_len; ++i)
574 {
575 ufunc_T *fp = (*fup)[i];
576 fp->uf_class = cl;
577 if (loop == 2)
578 fp->uf_flags |= FC_OBJECT;
579 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000580 }
581
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000582 cl->class_type.tt_type = VAR_CLASS;
583 cl->class_type.tt_member = (type_T *)cl;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000584 cl->class_object_type.tt_type = VAR_OBJECT;
585 cl->class_object_type.tt_member = (type_T *)cl;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000586 cl->class_type_list = type_list;
587
588 // TODO:
Bram Moolenaard505d172022-12-18 21:42:55 +0000589 // - Fill hashtab with object members and methods ?
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000590
591 // Add the class to the script-local variables.
592 typval_T tv;
593 tv.v_type = VAR_CLASS;
594 tv.vval.v_class = cl;
595 set_var_const(cl->class_name, current_sctx.sc_sid,
596 NULL, &tv, FALSE, ASSIGN_DECL, 0);
597 return;
598 }
599
600cleanup:
Bram Moolenaareb533502022-12-14 15:06:11 +0000601 if (cl != NULL)
602 {
603 vim_free(cl->class_name);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000604 vim_free(cl->class_class_functions);
Bram Moolenaareb533502022-12-14 15:06:11 +0000605 vim_free(cl->class_obj_members);
606 vim_free(cl->class_obj_methods);
607 vim_free(cl);
608 }
609
Bram Moolenaard505d172022-12-18 21:42:55 +0000610 for (int round = 1; round <= 2; ++round)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000611 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000612 garray_T *gap = round == 1 ? &classmembers : &objmembers;
613 if (gap->ga_len == 0 || gap->ga_data == NULL)
614 continue;
615
616 for (int i = 0; i < gap->ga_len; ++i)
617 {
618 ocmember_T *m = ((ocmember_T *)gap->ga_data) + i;
619 vim_free(m->ocm_name);
620 vim_free(m->ocm_init);
621 }
622 ga_clear(gap);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000623 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000624
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000625 for (int i = 0; i < objmethods.ga_len; ++i)
626 {
627 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
628 func_clear_free(uf, FALSE);
629 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000630 ga_clear(&objmethods);
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000631
632 for (int i = 0; i < classfunctions.ga_len; ++i)
633 {
634 ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i];
635 func_clear_free(uf, FALSE);
636 }
637 ga_clear(&classfunctions);
638
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000639 clear_type_list(&type_list);
640}
641
642/*
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000643 * Find member "name" in class "cl", set "member_idx" to the member index and
644 * return its type.
645 * When not found "member_idx" is set to -1 and t_any is returned.
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000646 */
647 type_T *
648class_member_type(
649 class_T *cl,
650 char_u *name,
651 char_u *name_end,
652 int *member_idx)
653{
654 *member_idx = -1; // not found (yet)
655 size_t len = name_end - name;
656
657 for (int i = 0; i < cl->class_obj_member_count; ++i)
658 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000659 ocmember_T *m = cl->class_obj_members + i;
660 if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000661 {
662 *member_idx = i;
Bram Moolenaard505d172022-12-18 21:42:55 +0000663 return m->ocm_type;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000664 }
665 }
Bram Moolenaarf54cedd2022-12-23 17:56:27 +0000666
667 semsg(_(e_unknown_variable_str), name);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000668 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000669}
670
671/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000672 * Handle ":enum" up to ":endenum".
673 */
674 void
675ex_enum(exarg_T *eap UNUSED)
676{
677 // TODO
678}
679
680/*
681 * Handle ":type".
682 */
683 void
684ex_type(exarg_T *eap UNUSED)
685{
686 // TODO
687}
688
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000689/*
690 * Evaluate what comes after a class:
691 * - class member: SomeClass.varname
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000692 * - class function: SomeClass.SomeMethod()
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000693 * - class constructor: SomeClass.new()
694 * - object member: someObject.varname
695 * - object method: someObject.SomeMethod()
696 *
697 * "*arg" points to the '.'.
698 * "*arg" is advanced to after the member name or method call.
699 *
700 * Returns FAIL or OK.
701 */
702 int
703class_object_index(
704 char_u **arg,
705 typval_T *rettv,
706 evalarg_T *evalarg,
707 int verbose UNUSED) // give error messages
708{
709 // int evaluate = evalarg != NULL
710 // && (evalarg->eval_flags & EVAL_EVALUATE);
711
712 if (VIM_ISWHITE((*arg)[1]))
713 {
714 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
715 return FAIL;
716 }
717
718 ++*arg;
719 char_u *name = *arg;
720 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
721 if (name_end == name)
722 return FAIL;
723 size_t len = name_end - name;
724
725 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
726 : rettv->vval.v_object->obj_class;
727 if (*name_end == '(')
728 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000729 int on_class = rettv->v_type == VAR_CLASS;
730 int count = on_class ? cl->class_class_function_count
731 : cl->class_obj_method_count;
732 for (int i = 0; i < count; ++i)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000733 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000734 ufunc_T *fp = on_class ? cl->class_class_functions[i]
735 : cl->class_obj_methods[i];
Bram Moolenaar4ae00572022-12-09 22:49:23 +0000736 // Use a separate pointer to avoid that ASAN complains about
737 // uf_name[] only being 4 characters.
738 char_u *ufname = (char_u *)fp->uf_name;
739 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000740 {
741 typval_T argvars[MAX_FUNC_ARGS + 1];
742 int argcount = 0;
743
744 char_u *argp = name_end;
745 int ret = get_func_arguments(&argp, evalarg, 0,
746 argvars, &argcount);
747 if (ret == FAIL)
748 return FAIL;
749
750 funcexe_T funcexe;
751 CLEAR_FIELD(funcexe);
752 funcexe.fe_evaluate = TRUE;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000753 if (rettv->v_type == VAR_OBJECT)
754 {
755 funcexe.fe_object = rettv->vval.v_object;
756 ++funcexe.fe_object->obj_refcount;
757 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000758
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000759 // Clear the class or object after calling the function, in
760 // case the refcount is one.
761 typval_T tv_tofree = *rettv;
762 rettv->v_type = VAR_UNKNOWN;
763
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000764 // Call the user function. Result goes into rettv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000765 int error = call_user_func_check(fp, argcount, argvars,
766 rettv, &funcexe, NULL);
767
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000768 // Clear the previous rettv and the arguments.
769 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000770 for (int idx = 0; idx < argcount; ++idx)
771 clear_tv(&argvars[idx]);
772
773 if (error != FCERR_NONE)
774 {
775 user_func_error(error, printable_func_name(fp),
776 funcexe.fe_found_var);
777 return FAIL;
778 }
779 *arg = argp;
780 return OK;
781 }
782 }
783
784 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
785 }
786
787 else if (rettv->v_type == VAR_OBJECT)
788 {
789 for (int i = 0; i < cl->class_obj_member_count; ++i)
790 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000791 ocmember_T *m = &cl->class_obj_members[i];
792 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000793 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000794 if (*name == '_')
795 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000796 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000797 return FAIL;
798 }
799
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000800 // The object only contains a pointer to the class, the member
801 // values array follows right after that.
802 object_T *obj = rettv->vval.v_object;
803 typval_T *tv = (typval_T *)(obj + 1) + i;
804 copy_tv(tv, rettv);
805 object_unref(obj);
806
807 *arg = name_end;
808 return OK;
809 }
810 }
811
812 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
813 }
814
Bram Moolenaard505d172022-12-18 21:42:55 +0000815 else if (rettv->v_type == VAR_CLASS)
816 {
817 // class member
818 for (int i = 0; i < cl->class_class_member_count; ++i)
819 {
820 ocmember_T *m = &cl->class_class_members[i];
821 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
822 {
823 if (*name == '_')
824 {
825 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
826 return FAIL;
827 }
828
829 typval_T *tv = &cl->class_members_tv[i];
830 copy_tv(tv, rettv);
831 class_unref(cl);
832
833 *arg = name_end;
834 return OK;
835 }
836 }
837
838 semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name);
839 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000840
841 return FAIL;
842}
843
844/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000845 * If "arg" points to a class or object method, return it.
846 * Otherwise return NULL.
847 */
848 ufunc_T *
849find_class_func(char_u **arg)
850{
851 char_u *name = *arg;
852 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
853 if (name_end == name || *name_end != '.')
854 return NULL;
855
856 size_t len = name_end - name;
857 typval_T tv;
858 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar993dbc32023-01-01 20:31:30 +0000859 if (eval_variable(name, (int)len,
860 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000861 return NULL;
862 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
Bram Moolenaareb533502022-12-14 15:06:11 +0000863 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000864
865 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
866 : tv.vval.v_object->obj_class;
867 if (cl == NULL)
Bram Moolenaareb533502022-12-14 15:06:11 +0000868 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000869 char_u *fname = name_end + 1;
870 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
871 if (fname_end == fname)
Bram Moolenaareb533502022-12-14 15:06:11 +0000872 goto fail_after_eval;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000873 len = fname_end - fname;
874
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000875 int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count
876 : cl->class_obj_method_count;
877 ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions
878 : cl->class_obj_methods;
879 for (int i = 0; i < count; ++i)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000880 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000881 ufunc_T *fp = funcs[i];
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000882 // Use a separate pointer to avoid that ASAN complains about
883 // uf_name[] only being 4 characters.
884 char_u *ufname = (char_u *)fp->uf_name;
885 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaareb533502022-12-14 15:06:11 +0000886 {
887 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000888 return fp;
Bram Moolenaareb533502022-12-14 15:06:11 +0000889 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000890 }
891
Bram Moolenaareb533502022-12-14 15:06:11 +0000892fail_after_eval:
893 clear_tv(&tv);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000894 return NULL;
895}
896
897/*
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000898 * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the
899 * index in class.class_class_members[].
900 * If "cl_ret" is not NULL set it to the class.
901 * Otherwise return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000902 */
903 int
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000904class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000905{
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000906 if (cctx == NULL || cctx->ctx_ufunc == NULL
907 || cctx->ctx_ufunc->uf_class == NULL)
908 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000909 class_T *cl = cctx->ctx_ufunc->uf_class;
910
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000911 for (int i = 0; i < cl->class_class_member_count; ++i)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000912 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000913 ocmember_T *m = &cl->class_class_members[i];
914 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000915 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000916 if (cl_ret != NULL)
917 *cl_ret = cl;
918 return i;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000919 }
920 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000921 return -1;
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000922}
923
924/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000925 * Make a copy of an object.
926 */
927 void
928copy_object(typval_T *from, typval_T *to)
929{
930 *to = *from;
931 if (to->vval.v_object != NULL)
932 ++to->vval.v_object->obj_refcount;
933}
934
935/*
936 * Free an object.
937 */
938 static void
939object_clear(object_T *obj)
940{
941 class_T *cl = obj->obj_class;
942
943 // the member values are just after the object structure
944 typval_T *tv = (typval_T *)(obj + 1);
945 for (int i = 0; i < cl->class_obj_member_count; ++i)
946 clear_tv(tv + i);
947
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000948 // Remove from the list headed by "first_object".
949 object_cleared(obj);
950
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000951 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000952 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000953}
954
955/*
956 * Unreference an object.
957 */
958 void
959object_unref(object_T *obj)
960{
961 if (obj != NULL && --obj->obj_refcount <= 0)
962 object_clear(obj);
963}
964
965/*
966 * Make a copy of a class.
967 */
968 void
969copy_class(typval_T *from, typval_T *to)
970{
971 *to = *from;
972 if (to->vval.v_class != NULL)
973 ++to->vval.v_class->class_refcount;
974}
975
976/*
977 * Unreference a class. Free it when the reference count goes down to zero.
978 */
979 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000980class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000981{
Bram Moolenaard505d172022-12-18 21:42:55 +0000982 if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000983 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000984 // Freeing what the class contains may recursively come back here.
985 // Clear "class_name" first, if it is NULL the class does not need to
986 // be freed.
987 VIM_CLEAR(cl->class_name);
988
989 for (int i = 0; i < cl->class_class_member_count; ++i)
990 {
991 ocmember_T *m = &cl->class_class_members[i];
992 vim_free(m->ocm_name);
993 vim_free(m->ocm_init);
994 if (cl->class_members_tv != NULL)
995 clear_tv(&cl->class_members_tv[i]);
996 }
997 vim_free(cl->class_class_members);
998 vim_free(cl->class_members_tv);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000999
1000 for (int i = 0; i < cl->class_obj_member_count; ++i)
1001 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001002 ocmember_T *m = &cl->class_obj_members[i];
1003 vim_free(m->ocm_name);
1004 vim_free(m->ocm_init);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001005 }
1006 vim_free(cl->class_obj_members);
1007
Bram Moolenaarec8b74f2023-01-01 14:11:27 +00001008 for (int i = 0; i < cl->class_class_function_count; ++i)
1009 {
1010 ufunc_T *uf = cl->class_class_functions[i];
1011 func_clear_free(uf, FALSE);
1012 }
1013 vim_free(cl->class_class_functions);
1014
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001015 for (int i = 0; i < cl->class_obj_method_count; ++i)
1016 {
1017 ufunc_T *uf = cl->class_obj_methods[i];
1018 func_clear_free(uf, FALSE);
1019 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001020 vim_free(cl->class_obj_methods);
1021
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001022 clear_type_list(&cl->class_type_list);
1023
1024 vim_free(cl);
1025 }
1026}
1027
Bram Moolenaard28d7b92022-12-08 20:42:00 +00001028static object_T *first_object = NULL;
1029
1030/*
1031 * Call this function when an object has been created. It will be added to the
1032 * list headed by "first_object".
1033 */
1034 void
1035object_created(object_T *obj)
1036{
1037 if (first_object != NULL)
1038 {
1039 obj->obj_next_used = first_object;
1040 first_object->obj_prev_used = obj;
1041 }
1042 first_object = obj;
1043}
1044
1045/*
1046 * Call this function when an object has been cleared and is about to be freed.
1047 * It is removed from the list headed by "first_object".
1048 */
1049 void
1050object_cleared(object_T *obj)
1051{
1052 if (obj->obj_next_used != NULL)
1053 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
1054 if (obj->obj_prev_used != NULL)
1055 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
1056 else if (first_object == obj)
1057 first_object = obj->obj_next_used;
1058}
1059
1060/*
1061 * Go through the list of all objects and free items without "copyID".
1062 */
1063 int
1064object_free_nonref(int copyID)
1065{
1066 int did_free = FALSE;
1067 object_T *next_obj;
1068
1069 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
1070 {
1071 next_obj = obj->obj_next_used;
1072 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
1073 {
1074 // Free the object and items it contains.
1075 object_clear(obj);
1076 did_free = TRUE;
1077 }
1078 }
1079
1080 return did_free;
1081}
1082
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001083
1084#endif // FEAT_EVAL