blob: 5804b129b797ee093b19aded760037d874f175fa [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
422 // Call the user function. Result goes into rettv;
423 // TODO: pass the object
424 rettv->v_type = VAR_UNKNOWN;
425 int error = call_user_func_check(fp, argcount, argvars,
426 rettv, &funcexe, NULL);
427
428 // Clear the arguments.
429 for (int idx = 0; idx < argcount; ++idx)
430 clear_tv(&argvars[idx]);
431
432 if (error != FCERR_NONE)
433 {
434 user_func_error(error, printable_func_name(fp),
435 funcexe.fe_found_var);
436 return FAIL;
437 }
438 *arg = argp;
439 return OK;
440 }
441 }
442
443 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
444 }
445
446 else if (rettv->v_type == VAR_OBJECT)
447 {
448 for (int i = 0; i < cl->class_obj_member_count; ++i)
449 {
450 objmember_T *m = &cl->class_obj_members[i];
451 if (STRNCMP(name, m->om_name, len) == 0 && m->om_name[len] == NUL)
452 {
453 // The object only contains a pointer to the class, the member
454 // values array follows right after that.
455 object_T *obj = rettv->vval.v_object;
456 typval_T *tv = (typval_T *)(obj + 1) + i;
457 copy_tv(tv, rettv);
458 object_unref(obj);
459
460 *arg = name_end;
461 return OK;
462 }
463 }
464
465 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
466 }
467
468 // TODO: class member
469
470 return FAIL;
471}
472
473/*
474 * Make a copy of an object.
475 */
476 void
477copy_object(typval_T *from, typval_T *to)
478{
479 *to = *from;
480 if (to->vval.v_object != NULL)
481 ++to->vval.v_object->obj_refcount;
482}
483
484/*
485 * Free an object.
486 */
487 static void
488object_clear(object_T *obj)
489{
490 class_T *cl = obj->obj_class;
491
492 // the member values are just after the object structure
493 typval_T *tv = (typval_T *)(obj + 1);
494 for (int i = 0; i < cl->class_obj_member_count; ++i)
495 clear_tv(tv + i);
496
497 vim_free(obj);
498}
499
500/*
501 * Unreference an object.
502 */
503 void
504object_unref(object_T *obj)
505{
506 if (obj != NULL && --obj->obj_refcount <= 0)
507 object_clear(obj);
508}
509
510/*
511 * Make a copy of a class.
512 */
513 void
514copy_class(typval_T *from, typval_T *to)
515{
516 *to = *from;
517 if (to->vval.v_class != NULL)
518 ++to->vval.v_class->class_refcount;
519}
520
521/*
522 * Unreference a class. Free it when the reference count goes down to zero.
523 */
524 void
525class_unref(typval_T *tv)
526{
527 class_T *cl = tv->vval.v_class;
528 if (cl != NULL && --cl->class_refcount <= 0)
529 {
530 vim_free(cl->class_name);
531
532 for (int i = 0; i < cl->class_obj_member_count; ++i)
533 {
534 objmember_T *m = &cl->class_obj_members[i];
535 vim_free(m->om_name);
536 }
537 vim_free(cl->class_obj_members);
538
539 vim_free(cl->class_obj_methods);
540
541 if (cl->class_new_func != NULL)
542 func_ptr_unref(cl->class_new_func);
543
544 clear_type_list(&cl->class_type_list);
545
546 vim_free(cl);
547 }
548}
549
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000550
551#endif // FEAT_EVAL