blob: 864a2e00b937f7f7612f67cfd9484c641c0a36c3 [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/*
25 * Handle ":class" and ":abstract class" up to ":endclass".
26 */
27 void
28ex_class(exarg_T *eap)
29{
Bram Moolenaar00b28d62022-12-08 15:32:33 +000030 if (!current_script_is_vim9()
31 || (cmdmod.cmod_flags & CMOD_LEGACY)
32 || !getline_equal(eap->getline, eap->cookie, getsourceline))
33 {
34 emsg(_(e_class_can_only_be_defined_in_vim9_script));
35 return;
36 }
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000037
38 char_u *arg = eap->arg;
Bram Moolenaar00b28d62022-12-08 15:32:33 +000039 int is_abstract = eap->cmdidx == CMD_abstract;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000040 if (is_abstract)
41 {
42 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
43 {
44 semsg(_(e_invalid_argument_str), arg);
45 return;
46 }
47 arg = skipwhite(arg + 5);
48 }
49
50 if (!ASCII_ISUPPER(*arg))
51 {
52 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
53 return;
54 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +000055 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
56 if (!IS_WHITE_OR_NUL(*name_end))
57 {
58 semsg(_(e_white_space_required_after_class_name_str), arg);
59 return;
60 }
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000061
62 // TODO:
Bram Moolenaar00b28d62022-12-08 15:32:33 +000063 // generics: <Tkey, Tentry>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000064 // extends SomeClass
65 // implements SomeInterface
66 // specifies SomeInterface
Bram Moolenaar00b28d62022-12-08 15:32:33 +000067 // check nothing follows
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000068
Bram Moolenaar00b28d62022-12-08 15:32:33 +000069 // TODO: handle "is_export" if it is set
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000070
Bram Moolenaar00b28d62022-12-08 15:32:33 +000071 garray_T type_list; // list of pointers to allocated types
72 ga_init2(&type_list, sizeof(type_T *), 10);
73
74 // Growarray with object members declared in the class.
75 garray_T objmembers;
76 ga_init2(&objmembers, sizeof(objmember_T), 10);
77
78 // Growarray with object methods declared in the class.
79 garray_T objmethods;
80 ga_init2(&objmethods, sizeof(ufunc_T), 10);
81
82 /*
83 * Go over the body of the class until "endclass" is found.
84 */
85 char_u *theline = NULL;
86 int success = FALSE;
87 for (;;)
88 {
89 vim_free(theline);
90 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
91 if (theline == NULL)
92 break;
93 char_u *line = skipwhite(theline);
94
95 // TODO:
96 // class members (public, read access, private):
97 // static varname
98 // public static varname
99 // static _varname
100 //
101 // constructors:
102 // def new()
103 // enddef
104 // def newOther()
105 // enddef
106 //
107 // methods (object, class, generics):
108 // def someMethod()
109 // enddef
110 // static def someMethod()
111 // enddef
112 // def <Tval> someMethod()
113 // enddef
114 // static def <Tval> someMethod()
115 // enddef
116
117 char_u *p = line;
118 if (checkforcmd(&p, "endclass", 4))
119 {
120 if (STRNCMP(line, "endclass", 8) != 0)
121 semsg(_(e_command_cannot_be_shortened_str), line);
122 else if (*p == '|' || !ends_excmd2(line, p))
123 semsg(_(e_trailing_characters_str), p);
124
125 success = TRUE;
126 break;
127 }
128
129 // "this.varname"
130 // "this._varname"
131 // TODO:
132 // "public this.varname"
133 if (STRNCMP(line, "this", 4) == 0)
134 {
135 if (line[4] != '.' || !eval_isnamec1(line[5]))
136 {
137 semsg(_(e_invalid_object_member_declaration_str), line);
138 break;
139 }
140 char_u *varname = line + 5;
141 char_u *varname_end = to_name_end(varname, FALSE);
142
143 char_u *colon = skipwhite(varname_end);
144 // TODO: accept initialization and figure out type from it
145 if (*colon != ':')
146 {
147 emsg(_(e_type_or_initialization_required));
148 break;
149 }
150 if (VIM_ISWHITE(*varname_end))
151 {
152 semsg(_(e_no_white_space_allowed_before_colon_str), varname);
153 break;
154 }
155 if (!VIM_ISWHITE(colon[1]))
156 {
157 semsg(_(e_white_space_required_after_str_str), ":", varname);
158 break;
159 }
160
161 char_u *type_arg = skipwhite(colon + 1);
162 type_T *type = parse_type(&type_arg, &type_list, TRUE);
163 if (type == NULL)
164 break;
165
166 if (ga_grow(&objmembers, 1) == FAIL)
167 break;
168 objmember_T *m = ((objmember_T *)objmembers.ga_data)
169 + objmembers.ga_len;
170 m->om_name = vim_strnsave(varname, varname_end - varname);
171 m->om_type = type;
172 ++objmembers.ga_len;
173 }
174
175 else
176 {
177 semsg(_(e_not_valid_command_in_class_str), line);
178 break;
179 }
180 }
181 vim_free(theline);
182
183 if (success)
184 {
185 class_T *cl = ALLOC_CLEAR_ONE(class_T);
186 if (cl == NULL)
187 goto cleanup;
188 cl->class_refcount = 1;
189 cl->class_name = vim_strnsave(arg, name_end - arg);
190
191 // Members are used by the new() function, add them here.
192 cl->class_obj_member_count = objmembers.ga_len;
193 cl->class_obj_members = ALLOC_MULT(objmember_T, objmembers.ga_len);
194 if (cl->class_name == NULL
195 || cl->class_obj_members == NULL)
196 {
197 vim_free(cl->class_name);
198 vim_free(cl->class_obj_members);
199 vim_free(cl);
200 goto cleanup;
201 }
202 mch_memmove(cl->class_obj_members, objmembers.ga_data,
203 sizeof(objmember_T) * objmembers.ga_len);
204 vim_free(objmembers.ga_data);
205
206 int have_new = FALSE;
207 for (int i = 0; i < objmethods.ga_len; ++i)
208 if (STRCMP((((ufunc_T *)objmethods.ga_data) + i)->uf_name,
209 "new") == 0)
210 {
211 have_new = TRUE;
212 break;
213 }
214 if (!have_new)
215 {
216 // No new() method was defined, add the default constructor.
217 garray_T fga;
218 ga_init2(&fga, 1, 1000);
219 ga_concat(&fga, (char_u *)"new(");
220 for (int i = 0; i < cl->class_obj_member_count; ++i)
221 {
222 if (i > 0)
223 ga_concat(&fga, (char_u *)", ");
224 ga_concat(&fga, (char_u *)"this.");
225 objmember_T *m = cl->class_obj_members + i;
226 ga_concat(&fga, (char_u *)m->om_name);
227 }
228 ga_concat(&fga, (char_u *)")\nenddef\n");
229 ga_append(&fga, NUL);
230
231 exarg_T fea;
232 CLEAR_FIELD(fea);
233 fea.cmdidx = CMD_def;
234 fea.cmd = fea.arg = fga.ga_data;
235
236 garray_T lines_to_free;
237 ga_init2(&lines_to_free, sizeof(char_u *), 50);
238
239 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, cl);
240
241 ga_clear_strings(&lines_to_free);
242 vim_free(fga.ga_data);
243
244 if (nf != NULL && ga_grow(&objmethods, 1) == OK)
245 {
246 ((ufunc_T **)objmethods.ga_data)[objmethods.ga_len] = nf;
247 ++objmethods.ga_len;
248
249 nf->uf_flags |= FC_NEW;
250 nf->uf_class = cl;
251 nf->uf_ret_type = get_type_ptr(&type_list);
252 if (nf->uf_ret_type != NULL)
253 {
254 nf->uf_ret_type->tt_type = VAR_OBJECT;
255 nf->uf_ret_type->tt_member = (type_T *)cl;
256 nf->uf_ret_type->tt_argcount = 0;
257 nf->uf_ret_type->tt_args = NULL;
258 }
259 cl->class_new_func = nf;
260 }
261 }
262
263 cl->class_obj_method_count = objmethods.ga_len;
264 cl->class_obj_methods = ALLOC_MULT(ufunc_T *, objmethods.ga_len);
265 if (cl->class_obj_methods == NULL)
266 {
267 vim_free(cl->class_name);
268 vim_free(cl->class_obj_members);
269 vim_free(cl->class_obj_methods);
270 vim_free(cl);
271 goto cleanup;
272 }
273 mch_memmove(cl->class_obj_methods, objmethods.ga_data,
274 sizeof(ufunc_T *) * objmethods.ga_len);
275 vim_free(objmethods.ga_data);
276
277 cl->class_type.tt_type = VAR_CLASS;
278 cl->class_type.tt_member = (type_T *)cl;
279 cl->class_type_list = type_list;
280
281 // TODO:
282 // - Add the methods to the class
283 // - array with ufunc_T pointers
284 // - Fill hashtab with object members and methods
285 // - Generate the default new() method, if needed.
286 // Later:
287 // - class members
288 // - class methods
289
290 // Add the class to the script-local variables.
291 typval_T tv;
292 tv.v_type = VAR_CLASS;
293 tv.vval.v_class = cl;
294 set_var_const(cl->class_name, current_sctx.sc_sid,
295 NULL, &tv, FALSE, ASSIGN_DECL, 0);
296 return;
297 }
298
299cleanup:
300 for (int i = 0; i < objmembers.ga_len; ++i)
301 {
302 objmember_T *m = ((objmember_T *)objmembers.ga_data) + i;
303 vim_free(m->om_name);
304 }
305 ga_clear(&objmembers);
306
307 ga_clear(&objmethods);
308 clear_type_list(&type_list);
309}
310
311/*
312 * Find member "name" in class "cl" and return its type.
313 * When not found t_any is returned.
314 */
315 type_T *
316class_member_type(
317 class_T *cl,
318 char_u *name,
319 char_u *name_end,
320 int *member_idx)
321{
322 *member_idx = -1; // not found (yet)
323 size_t len = name_end - name;
324
325 for (int i = 0; i < cl->class_obj_member_count; ++i)
326 {
327 objmember_T *m = cl->class_obj_members + i;
328 if (STRNCMP(m->om_name, name, len) == 0 && m->om_name[len] == NUL)
329 {
330 *member_idx = i;
331 return m->om_type;
332 }
333 }
334 return &t_any;
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000335}
336
337/*
338 * Handle ":interface" up to ":endinterface".
339 */
340 void
341ex_interface(exarg_T *eap UNUSED)
342{
343 // TODO
344}
345
346/*
347 * Handle ":enum" up to ":endenum".
348 */
349 void
350ex_enum(exarg_T *eap UNUSED)
351{
352 // TODO
353}
354
355/*
356 * Handle ":type".
357 */
358 void
359ex_type(exarg_T *eap UNUSED)
360{
361 // TODO
362}
363
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000364/*
365 * Evaluate what comes after a class:
366 * - class member: SomeClass.varname
367 * - class method: SomeClass.SomeMethod()
368 * - class constructor: SomeClass.new()
369 * - object member: someObject.varname
370 * - object method: someObject.SomeMethod()
371 *
372 * "*arg" points to the '.'.
373 * "*arg" is advanced to after the member name or method call.
374 *
375 * Returns FAIL or OK.
376 */
377 int
378class_object_index(
379 char_u **arg,
380 typval_T *rettv,
381 evalarg_T *evalarg,
382 int verbose UNUSED) // give error messages
383{
384 // int evaluate = evalarg != NULL
385 // && (evalarg->eval_flags & EVAL_EVALUATE);
386
387 if (VIM_ISWHITE((*arg)[1]))
388 {
389 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
390 return FAIL;
391 }
392
393 ++*arg;
394 char_u *name = *arg;
395 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
396 if (name_end == name)
397 return FAIL;
398 size_t len = name_end - name;
399
400 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
401 : rettv->vval.v_object->obj_class;
402 if (*name_end == '(')
403 {
404 for (int i = 0; i < cl->class_obj_method_count; ++i)
405 {
406 ufunc_T *fp = cl->class_obj_methods[i];
407 if (STRNCMP(name, fp->uf_name, len) == 0 && fp->uf_name[len] == NUL)
408 {
409 typval_T argvars[MAX_FUNC_ARGS + 1];
410 int argcount = 0;
411
412 char_u *argp = name_end;
413 int ret = get_func_arguments(&argp, evalarg, 0,
414 argvars, &argcount);
415 if (ret == FAIL)
416 return FAIL;
417
418 funcexe_T funcexe;
419 CLEAR_FIELD(funcexe);
420 funcexe.fe_evaluate = TRUE;
421
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000422 // Clear the class or object after calling the function, in
423 // case the refcount is one.
424 typval_T tv_tofree = *rettv;
425 rettv->v_type = VAR_UNKNOWN;
426
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000427 // Call the user function. Result goes into rettv;
428 // TODO: pass the object
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000429 int error = call_user_func_check(fp, argcount, argvars,
430 rettv, &funcexe, NULL);
431
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000432 // Clear the previous rettv and the arguments.
433 clear_tv(&tv_tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000434 for (int idx = 0; idx < argcount; ++idx)
435 clear_tv(&argvars[idx]);
436
437 if (error != FCERR_NONE)
438 {
439 user_func_error(error, printable_func_name(fp),
440 funcexe.fe_found_var);
441 return FAIL;
442 }
443 *arg = argp;
444 return OK;
445 }
446 }
447
448 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
449 }
450
451 else if (rettv->v_type == VAR_OBJECT)
452 {
453 for (int i = 0; i < cl->class_obj_member_count; ++i)
454 {
455 objmember_T *m = &cl->class_obj_members[i];
456 if (STRNCMP(name, m->om_name, len) == 0 && m->om_name[len] == NUL)
457 {
458 // The object only contains a pointer to the class, the member
459 // values array follows right after that.
460 object_T *obj = rettv->vval.v_object;
461 typval_T *tv = (typval_T *)(obj + 1) + i;
462 copy_tv(tv, rettv);
463 object_unref(obj);
464
465 *arg = name_end;
466 return OK;
467 }
468 }
469
470 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
471 }
472
473 // TODO: class member
474
475 return FAIL;
476}
477
478/*
479 * Make a copy of an object.
480 */
481 void
482copy_object(typval_T *from, typval_T *to)
483{
484 *to = *from;
485 if (to->vval.v_object != NULL)
486 ++to->vval.v_object->obj_refcount;
487}
488
489/*
490 * Free an object.
491 */
492 static void
493object_clear(object_T *obj)
494{
495 class_T *cl = obj->obj_class;
496
497 // the member values are just after the object structure
498 typval_T *tv = (typval_T *)(obj + 1);
499 for (int i = 0; i < cl->class_obj_member_count; ++i)
500 clear_tv(tv + i);
501
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000502 // Remove from the list headed by "first_object".
503 object_cleared(obj);
504
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000505 vim_free(obj);
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000506 class_unref(cl);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000507}
508
509/*
510 * Unreference an object.
511 */
512 void
513object_unref(object_T *obj)
514{
515 if (obj != NULL && --obj->obj_refcount <= 0)
516 object_clear(obj);
517}
518
519/*
520 * Make a copy of a class.
521 */
522 void
523copy_class(typval_T *from, typval_T *to)
524{
525 *to = *from;
526 if (to->vval.v_class != NULL)
527 ++to->vval.v_class->class_refcount;
528}
529
530/*
531 * Unreference a class. Free it when the reference count goes down to zero.
532 */
533 void
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000534class_unref(class_T *cl)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000535{
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000536 if (cl != NULL && --cl->class_refcount <= 0)
537 {
538 vim_free(cl->class_name);
539
540 for (int i = 0; i < cl->class_obj_member_count; ++i)
541 {
542 objmember_T *m = &cl->class_obj_members[i];
543 vim_free(m->om_name);
544 }
545 vim_free(cl->class_obj_members);
546
547 vim_free(cl->class_obj_methods);
548
549 if (cl->class_new_func != NULL)
550 func_ptr_unref(cl->class_new_func);
551
552 clear_type_list(&cl->class_type_list);
553
554 vim_free(cl);
555 }
556}
557
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000558static object_T *first_object = NULL;
559
560/*
561 * Call this function when an object has been created. It will be added to the
562 * list headed by "first_object".
563 */
564 void
565object_created(object_T *obj)
566{
567 if (first_object != NULL)
568 {
569 obj->obj_next_used = first_object;
570 first_object->obj_prev_used = obj;
571 }
572 first_object = obj;
573}
574
575/*
576 * Call this function when an object has been cleared and is about to be freed.
577 * It is removed from the list headed by "first_object".
578 */
579 void
580object_cleared(object_T *obj)
581{
582 if (obj->obj_next_used != NULL)
583 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
584 if (obj->obj_prev_used != NULL)
585 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
586 else if (first_object == obj)
587 first_object = obj->obj_next_used;
588}
589
590/*
591 * Go through the list of all objects and free items without "copyID".
592 */
593 int
594object_free_nonref(int copyID)
595{
596 int did_free = FALSE;
597 object_T *next_obj;
598
599 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
600 {
601 next_obj = obj->obj_next_used;
602 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
603 {
604 // Free the object and items it contains.
605 object_clear(obj);
606 did_free = TRUE;
607 }
608 }
609
610 return did_free;
611}
612
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000613
614#endif // FEAT_EVAL