blob: 29595cca4ebc1b4e1ad984df886ec12af135e611 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* 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 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
26/*
27 * Chain of jump instructions where the end label needs to be set.
28 */
29typedef struct endlabel_S endlabel_T;
30struct endlabel_S {
31 endlabel_T *el_next; // chain end_label locations
32 int el_end_label; // instruction idx where to set end
33};
34
35/*
36 * info specific for the scope of :if / elseif / else
37 */
38typedef struct {
39 int is_if_label; // instruction idx at IF or ELSEIF
40 endlabel_T *is_end_label; // instructions to set end label
41} ifscope_T;
42
43/*
44 * info specific for the scope of :while
45 */
46typedef struct {
47 int ws_top_label; // instruction idx at WHILE
48 endlabel_T *ws_end_label; // instructions to set end
49} whilescope_T;
50
51/*
52 * info specific for the scope of :for
53 */
54typedef struct {
55 int fs_top_label; // instruction idx at FOR
56 endlabel_T *fs_end_label; // break instructions
57} forscope_T;
58
59/*
60 * info specific for the scope of :try
61 */
62typedef struct {
63 int ts_try_label; // instruction idx at TRY
64 endlabel_T *ts_end_label; // jump to :finally or :endtry
65 int ts_catch_label; // instruction idx of last CATCH
66 int ts_caught_all; // "catch" without argument encountered
67} tryscope_T;
68
69typedef enum {
70 NO_SCOPE,
71 IF_SCOPE,
72 WHILE_SCOPE,
73 FOR_SCOPE,
74 TRY_SCOPE,
75 BLOCK_SCOPE
76} scopetype_T;
77
78/*
79 * Info for one scope, pointed to by "ctx_scope".
80 */
81typedef struct scope_S scope_T;
82struct scope_S {
83 scope_T *se_outer; // scope containing this one
84 scopetype_T se_type;
85 int se_local_count; // ctx_locals.ga_len before scope
86 union {
87 ifscope_T se_if;
88 whilescope_T se_while;
89 forscope_T se_for;
90 tryscope_T se_try;
91 };
92};
93
94/*
95 * Entry for "ctx_locals". Used for arguments and local variables.
96 */
97typedef struct {
98 char_u *lv_name;
99 type_T *lv_type;
100 int lv_const; // when TRUE cannot be assigned to
101 int lv_arg; // when TRUE this is an argument
102} lvar_T;
103
104/*
105 * Context for compiling lines of Vim script.
106 * Stores info about the local variables and condition stack.
107 */
108struct cctx_S {
109 ufunc_T *ctx_ufunc; // current function
110 int ctx_lnum; // line number in current function
111 garray_T ctx_instr; // generated instructions
112
113 garray_T ctx_locals; // currently visible local variables
114 int ctx_max_local; // maximum number of locals at one time
115
116 garray_T ctx_imports; // imported items
117
118 scope_T *ctx_scope; // current scope, NULL at toplevel
119
120 garray_T ctx_type_stack; // type of each item on the stack
121 garray_T *ctx_type_list; // space for adding types
122};
123
124static char e_var_notfound[] = N_("E1001: variable not found: %s");
125static char e_syntax_at[] = N_("E1002: Syntax error at %s");
126
127static int compile_expr1(char_u **arg, cctx_T *cctx);
128static int compile_expr2(char_u **arg, cctx_T *cctx);
129static int compile_expr3(char_u **arg, cctx_T *cctx);
130
131/*
132 * Lookup variable "name" in the local scope and return the index.
133 */
134 static int
135lookup_local(char_u *name, size_t len, cctx_T *cctx)
136{
137 int idx;
138
139 if (len <= 0)
140 return -1;
141 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
142 {
143 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
144
145 if (STRNCMP(name, lvar->lv_name, len) == 0
146 && STRLEN(lvar->lv_name) == len)
147 return idx;
148 }
149 return -1;
150}
151
152/*
153 * Lookup an argument in the current function.
154 * Returns the argument index or -1 if not found.
155 */
156 static int
157lookup_arg(char_u *name, size_t len, cctx_T *cctx)
158{
159 int idx;
160
161 if (len <= 0)
162 return -1;
163 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
164 {
165 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
166
167 if (STRNCMP(name, arg, len) == 0 && STRLEN(arg) == len)
168 return idx;
169 }
170 return -1;
171}
172
173/*
174 * Lookup a vararg argument in the current function.
175 * Returns TRUE if there is a match.
176 */
177 static int
178lookup_vararg(char_u *name, size_t len, cctx_T *cctx)
179{
180 char_u *va_name = cctx->ctx_ufunc->uf_va_name;
181
182 return len > 0 && va_name != NULL
183 && STRNCMP(name, va_name, len) == 0 && STRLEN(va_name) == len;
184}
185
186/*
187 * Lookup a variable in the current script.
188 * Returns OK or FAIL.
189 */
190 static int
191lookup_script(char_u *name, size_t len)
192{
193 int cc;
194 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
195 dictitem_T *di;
196
197 cc = name[len];
198 name[len] = NUL;
199 di = find_var_in_ht(ht, 0, name, TRUE);
200 name[len] = cc;
201 return di == NULL ? FAIL: OK;
202}
203
204 static type_T *
205get_list_type(type_T *member_type, garray_T *type_list)
206{
207 type_T *type;
208
209 // recognize commonly used types
210 if (member_type->tt_type == VAR_UNKNOWN)
211 return &t_list_any;
212 if (member_type->tt_type == VAR_NUMBER)
213 return &t_list_number;
214 if (member_type->tt_type == VAR_STRING)
215 return &t_list_string;
216
217 // Not a common type, create a new entry.
218 if (ga_grow(type_list, 1) == FAIL)
219 return FAIL;
220 type = ((type_T *)type_list->ga_data) + type_list->ga_len;
221 ++type_list->ga_len;
222 type->tt_type = VAR_LIST;
223 type->tt_member = member_type;
224 return type;
225}
226
227 static type_T *
228get_dict_type(type_T *member_type, garray_T *type_list)
229{
230 type_T *type;
231
232 // recognize commonly used types
233 if (member_type->tt_type == VAR_UNKNOWN)
234 return &t_dict_any;
235 if (member_type->tt_type == VAR_NUMBER)
236 return &t_dict_number;
237 if (member_type->tt_type == VAR_STRING)
238 return &t_dict_string;
239
240 // Not a common type, create a new entry.
241 if (ga_grow(type_list, 1) == FAIL)
242 return FAIL;
243 type = ((type_T *)type_list->ga_data) + type_list->ga_len;
244 ++type_list->ga_len;
245 type->tt_type = VAR_DICT;
246 type->tt_member = member_type;
247 return type;
248}
249
250/////////////////////////////////////////////////////////////////////
251// Following generate_ functions expect the caller to call ga_grow().
252
253/*
254 * Generate an instruction without arguments.
255 * Returns a pointer to the new instruction, NULL if failed.
256 */
257 static isn_T *
258generate_instr(cctx_T *cctx, isntype_T isn_type)
259{
260 garray_T *instr = &cctx->ctx_instr;
261 isn_T *isn;
262
263 if (ga_grow(instr, 1) == FAIL)
264 return NULL;
265 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
266 isn->isn_type = isn_type;
267 isn->isn_lnum = cctx->ctx_lnum + 1;
268 ++instr->ga_len;
269
270 return isn;
271}
272
273/*
274 * Generate an instruction without arguments.
275 * "drop" will be removed from the stack.
276 * Returns a pointer to the new instruction, NULL if failed.
277 */
278 static isn_T *
279generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
280{
281 garray_T *stack = &cctx->ctx_type_stack;
282
283 stack->ga_len -= drop;
284 return generate_instr(cctx, isn_type);
285}
286
287/*
288 * Generate instruction "isn_type" and put "type" on the type stack.
289 */
290 static isn_T *
291generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
292{
293 isn_T *isn;
294 garray_T *stack = &cctx->ctx_type_stack;
295
296 if ((isn = generate_instr(cctx, isn_type)) == NULL)
297 return NULL;
298
299 if (ga_grow(stack, 1) == FAIL)
300 return NULL;
301 ((type_T **)stack->ga_data)[stack->ga_len] = type;
302 ++stack->ga_len;
303
304 return isn;
305}
306
307/*
308 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
309 */
310 static int
311may_generate_2STRING(int offset, cctx_T *cctx)
312{
313 isn_T *isn;
314 garray_T *stack = &cctx->ctx_type_stack;
315 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
316
317 if ((*type)->tt_type == VAR_STRING)
318 return OK;
319 *type = &t_string;
320
321 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
322 return FAIL;
323 isn->isn_arg.number = offset;
324
325 return OK;
326}
327
328 static int
329check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
330{
331 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_UNKNOWN)
332 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
333 || type2 == VAR_UNKNOWN)))
334 {
335 if (*op == '+')
336 semsg(_("E1035: wrong argument type for +"));
337 else
338 semsg(_("E1036: %c requires number or float arguments"), *op);
339 return FAIL;
340 }
341 return OK;
342}
343
344/*
345 * Generate an instruction with two arguments. The instruction depends on the
346 * type of the arguments.
347 */
348 static int
349generate_two_op(cctx_T *cctx, char_u *op)
350{
351 garray_T *stack = &cctx->ctx_type_stack;
352 type_T *type1;
353 type_T *type2;
354 vartype_T vartype;
355 isn_T *isn;
356
357 // Get the known type of the two items on the stack. If they are matching
358 // use a type-specific instruction. Otherwise fall back to runtime type
359 // checking.
360 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
361 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
362 vartype = VAR_UNKNOWN;
363 if (type1->tt_type == type2->tt_type
364 && (type1->tt_type == VAR_NUMBER
365 || type1->tt_type == VAR_LIST
366#ifdef FEAT_FLOAT
367 || type1->tt_type == VAR_FLOAT
368#endif
369 || type1->tt_type == VAR_BLOB))
370 vartype = type1->tt_type;
371
372 switch (*op)
373 {
374 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
375 && check_number_or_float(
376 type1->tt_type, type2->tt_type, op) == FAIL)
377 return FAIL;
378 isn = generate_instr_drop(cctx,
379 vartype == VAR_NUMBER ? ISN_OPNR
380 : vartype == VAR_LIST ? ISN_ADDLIST
381 : vartype == VAR_BLOB ? ISN_ADDBLOB
382#ifdef FEAT_FLOAT
383 : vartype == VAR_FLOAT ? ISN_OPFLOAT
384#endif
385 : ISN_OPANY, 1);
386 if (isn != NULL)
387 isn->isn_arg.op.op_type = EXPR_ADD;
388 break;
389
390 case '-':
391 case '*':
392 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
393 op) == FAIL)
394 return FAIL;
395 if (vartype == VAR_NUMBER)
396 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
397#ifdef FEAT_FLOAT
398 else if (vartype == VAR_FLOAT)
399 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
400#endif
401 else
402 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
403 if (isn != NULL)
404 isn->isn_arg.op.op_type = *op == '*'
405 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
406 break;
407
408 case '%': if ((type1->tt_type != VAR_UNKNOWN
409 && type1->tt_type != VAR_NUMBER)
410 || (type2->tt_type != VAR_UNKNOWN
411 && type2->tt_type != VAR_NUMBER))
412 {
413 emsg(_("E1035: % requires number arguments"));
414 return FAIL;
415 }
416 isn = generate_instr_drop(cctx,
417 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
418 if (isn != NULL)
419 isn->isn_arg.op.op_type = EXPR_REM;
420 break;
421 }
422
423 // correct type of result
424 if (vartype == VAR_UNKNOWN)
425 {
426 type_T *type = &t_any;
427
428#ifdef FEAT_FLOAT
429 // float+number and number+float results in float
430 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
431 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
432 type = &t_float;
433#endif
434 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
435 }
436
437 return OK;
438}
439
440/*
441 * Generate an ISN_COMPARE* instruction with a boolean result.
442 */
443 static int
444generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
445{
446 isntype_T isntype = ISN_DROP;
447 isn_T *isn;
448 garray_T *stack = &cctx->ctx_type_stack;
449 vartype_T type1;
450 vartype_T type2;
451
452 // Get the known type of the two items on the stack. If they are matching
453 // use a type-specific instruction. Otherwise fall back to runtime type
454 // checking.
455 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
456 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
457 if (type1 == type2)
458 {
459 switch (type1)
460 {
461 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
462 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
463 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
464 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
465 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
466 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
467 case VAR_LIST: isntype = ISN_COMPARELIST; break;
468 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
469 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
470 case VAR_PARTIAL: isntype = ISN_COMPAREPARTIAL; break;
471 default: isntype = ISN_COMPAREANY; break;
472 }
473 }
474 else if (type1 == VAR_UNKNOWN || type2 == VAR_UNKNOWN
475 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
476 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
477 isntype = ISN_COMPAREANY;
478
479 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
480 && (isntype == ISN_COMPAREBOOL
481 || isntype == ISN_COMPARESPECIAL
482 || isntype == ISN_COMPARENR
483 || isntype == ISN_COMPAREFLOAT))
484 {
485 semsg(_("E1037: Cannot use \"%s\" with %s"),
486 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
487 return FAIL;
488 }
489 if (isntype == ISN_DROP
490 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
491 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
492 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
493 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
494 && exptype != EXPR_IS && exptype != EXPR_ISNOT
495 && (type1 == VAR_BLOB || type2 == VAR_BLOB
496 || type1 == VAR_LIST || type2 == VAR_LIST))))
497 {
498 semsg(_("E1037: Cannot compare %s with %s"),
499 vartype_name(type1), vartype_name(type2));
500 return FAIL;
501 }
502
503 if ((isn = generate_instr(cctx, isntype)) == NULL)
504 return FAIL;
505 isn->isn_arg.op.op_type = exptype;
506 isn->isn_arg.op.op_ic = ic;
507
508 // takes two arguments, puts one bool back
509 if (stack->ga_len >= 2)
510 {
511 --stack->ga_len;
512 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
513 }
514
515 return OK;
516}
517
518/*
519 * Generate an ISN_2BOOL instruction.
520 */
521 static int
522generate_2BOOL(cctx_T *cctx, int invert)
523{
524 isn_T *isn;
525 garray_T *stack = &cctx->ctx_type_stack;
526
527 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
528 return FAIL;
529 isn->isn_arg.number = invert;
530
531 // type becomes bool
532 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
533
534 return OK;
535}
536
537 static int
538generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
539{
540 isn_T *isn;
541 garray_T *stack = &cctx->ctx_type_stack;
542
543 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
544 return FAIL;
545 isn->isn_arg.type.ct_type = vartype->tt_type; // TODO: whole type
546 isn->isn_arg.type.ct_off = offset;
547
548 // type becomes vartype
549 ((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype;
550
551 return OK;
552}
553
554/*
555 * Generate an ISN_PUSHNR instruction.
556 */
557 static int
558generate_PUSHNR(cctx_T *cctx, varnumber_T number)
559{
560 isn_T *isn;
561
562 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
563 return FAIL;
564 isn->isn_arg.number = number;
565
566 return OK;
567}
568
569/*
570 * Generate an ISN_PUSHBOOL instruction.
571 */
572 static int
573generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
574{
575 isn_T *isn;
576
577 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
578 return FAIL;
579 isn->isn_arg.number = number;
580
581 return OK;
582}
583
584/*
585 * Generate an ISN_PUSHSPEC instruction.
586 */
587 static int
588generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
589{
590 isn_T *isn;
591
592 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
593 return FAIL;
594 isn->isn_arg.number = number;
595
596 return OK;
597}
598
599#ifdef FEAT_FLOAT
600/*
601 * Generate an ISN_PUSHF instruction.
602 */
603 static int
604generate_PUSHF(cctx_T *cctx, float_T fnumber)
605{
606 isn_T *isn;
607
608 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
609 return FAIL;
610 isn->isn_arg.fnumber = fnumber;
611
612 return OK;
613}
614#endif
615
616/*
617 * Generate an ISN_PUSHS instruction.
618 * Consumes "str".
619 */
620 static int
621generate_PUSHS(cctx_T *cctx, char_u *str)
622{
623 isn_T *isn;
624
625 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
626 return FAIL;
627 isn->isn_arg.string = str;
628
629 return OK;
630}
631
632/*
633 * Generate an ISN_PUSHBLOB instruction.
634 * Consumes "blob".
635 */
636 static int
637generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
638{
639 isn_T *isn;
640
641 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
642 return FAIL;
643 isn->isn_arg.blob = blob;
644
645 return OK;
646}
647
648/*
649 * Generate an ISN_STORE instruction.
650 */
651 static int
652generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
653{
654 isn_T *isn;
655
656 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
657 return FAIL;
658 if (name != NULL)
659 isn->isn_arg.string = vim_strsave(name);
660 else
661 isn->isn_arg.number = idx;
662
663 return OK;
664}
665
666/*
667 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
668 */
669 static int
670generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
671{
672 isn_T *isn;
673
674 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
675 return FAIL;
676 isn->isn_arg.storenr.str_idx = idx;
677 isn->isn_arg.storenr.str_val = value;
678
679 return OK;
680}
681
682/*
683 * Generate an ISN_STOREOPT instruction
684 */
685 static int
686generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
687{
688 isn_T *isn;
689
690 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
691 return FAIL;
692 isn->isn_arg.storeopt.so_name = vim_strsave(name);
693 isn->isn_arg.storeopt.so_flags = opt_flags;
694
695 return OK;
696}
697
698/*
699 * Generate an ISN_LOAD or similar instruction.
700 */
701 static int
702generate_LOAD(
703 cctx_T *cctx,
704 isntype_T isn_type,
705 int idx,
706 char_u *name,
707 type_T *type)
708{
709 isn_T *isn;
710
711 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
712 return FAIL;
713 if (name != NULL)
714 isn->isn_arg.string = vim_strsave(name);
715 else
716 isn->isn_arg.number = idx;
717
718 return OK;
719}
720
721/*
722 * Generate an ISN_LOADS instruction.
723 */
724 static int
725generate_LOADS(
726 cctx_T *cctx,
727 char_u *name,
728 int sid)
729{
730 isn_T *isn;
731
732 if ((isn = generate_instr_type(cctx, ISN_LOADS, &t_any)) == NULL)
733 return FAIL;
734 isn->isn_arg.loads.ls_name = vim_strsave(name);
735 isn->isn_arg.loads.ls_sid = sid;
736
737 return OK;
738}
739
740/*
741 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
742 */
743 static int
744generate_SCRIPT(
745 cctx_T *cctx,
746 isntype_T isn_type,
747 int sid,
748 int idx,
749 type_T *type)
750{
751 isn_T *isn;
752
753 if (isn_type == ISN_LOADSCRIPT)
754 isn = generate_instr_type(cctx, isn_type, type);
755 else
756 isn = generate_instr_drop(cctx, isn_type, 1);
757 if (isn == NULL)
758 return FAIL;
759 isn->isn_arg.script.script_sid = sid;
760 isn->isn_arg.script.script_idx = idx;
761 return OK;
762}
763
764/*
765 * Generate an ISN_NEWLIST instruction.
766 */
767 static int
768generate_NEWLIST(cctx_T *cctx, int count)
769{
770 isn_T *isn;
771 garray_T *stack = &cctx->ctx_type_stack;
772 garray_T *type_list = cctx->ctx_type_list;
773 type_T *type;
774 type_T *member;
775
776 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
777 return FAIL;
778 isn->isn_arg.number = count;
779
780 // drop the value types
781 stack->ga_len -= count;
782
783 // use the first value type for the list member type
784 if (count > 0)
785 member = ((type_T **)stack->ga_data)[stack->ga_len];
786 else
787 member = &t_any;
788 type = get_list_type(member, type_list);
789
790 // add the list type to the type stack
791 if (ga_grow(stack, 1) == FAIL)
792 return FAIL;
793 ((type_T **)stack->ga_data)[stack->ga_len] = type;
794 ++stack->ga_len;
795
796 return OK;
797}
798
799/*
800 * Generate an ISN_NEWDICT instruction.
801 */
802 static int
803generate_NEWDICT(cctx_T *cctx, int count)
804{
805 isn_T *isn;
806 garray_T *stack = &cctx->ctx_type_stack;
807 garray_T *type_list = cctx->ctx_type_list;
808 type_T *type;
809 type_T *member;
810
811 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
812 return FAIL;
813 isn->isn_arg.number = count;
814
815 // drop the key and value types
816 stack->ga_len -= 2 * count;
817
818 // use the first value type for the list member type
819 if (count > 0)
820 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
821 else
822 member = &t_any;
823 type = get_dict_type(member, type_list);
824
825 // add the dict type to the type stack
826 if (ga_grow(stack, 1) == FAIL)
827 return FAIL;
828 ((type_T **)stack->ga_data)[stack->ga_len] = type;
829 ++stack->ga_len;
830
831 return OK;
832}
833
834/*
835 * Generate an ISN_FUNCREF instruction.
836 */
837 static int
838generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
839{
840 isn_T *isn;
841 garray_T *stack = &cctx->ctx_type_stack;
842
843 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
844 return FAIL;
845 isn->isn_arg.number = dfunc_idx;
846
847 if (ga_grow(stack, 1) == FAIL)
848 return FAIL;
849 ((type_T **)stack->ga_data)[stack->ga_len] = &t_partial_any;
850 // TODO: argument and return types
851 ++stack->ga_len;
852
853 return OK;
854}
855
856/*
857 * Generate an ISN_JUMP instruction.
858 */
859 static int
860generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
861{
862 isn_T *isn;
863 garray_T *stack = &cctx->ctx_type_stack;
864
865 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
866 return FAIL;
867 isn->isn_arg.jump.jump_when = when;
868 isn->isn_arg.jump.jump_where = where;
869
870 if (when != JUMP_ALWAYS && stack->ga_len > 0)
871 --stack->ga_len;
872
873 return OK;
874}
875
876 static int
877generate_FOR(cctx_T *cctx, int loop_idx)
878{
879 isn_T *isn;
880 garray_T *stack = &cctx->ctx_type_stack;
881
882 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
883 return FAIL;
884 isn->isn_arg.forloop.for_idx = loop_idx;
885
886 if (ga_grow(stack, 1) == FAIL)
887 return FAIL;
888 // type doesn't matter, will be stored next
889 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
890 ++stack->ga_len;
891
892 return OK;
893}
894
895/*
896 * Generate an ISN_BCALL instruction.
897 * Return FAIL if the number of arguments is wrong.
898 */
899 static int
900generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
901{
902 isn_T *isn;
903 garray_T *stack = &cctx->ctx_type_stack;
904
905 if (check_internal_func(func_idx, argcount) == FAIL)
906 return FAIL;
907
908 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
909 return FAIL;
910 isn->isn_arg.bfunc.cbf_idx = func_idx;
911 isn->isn_arg.bfunc.cbf_argcount = argcount;
912
913 stack->ga_len -= argcount; // drop the arguments
914 if (ga_grow(stack, 1) == FAIL)
915 return FAIL;
916 ((type_T **)stack->ga_data)[stack->ga_len] =
917 internal_func_ret_type(func_idx, argcount);
918 ++stack->ga_len; // add return value
919
920 return OK;
921}
922
923/*
924 * Generate an ISN_DCALL or ISN_UCALL instruction.
925 * Return FAIL if the number of arguments is wrong.
926 */
927 static int
928generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int argcount)
929{
930 isn_T *isn;
931 garray_T *stack = &cctx->ctx_type_stack;
932 int regular_args = ufunc->uf_args.ga_len;
933
934 if (argcount > regular_args && !has_varargs(ufunc))
935 {
936 semsg(_(e_toomanyarg), ufunc->uf_name);
937 return FAIL;
938 }
939 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
940 {
941 semsg(_(e_toofewarg), ufunc->uf_name);
942 return FAIL;
943 }
944
945 // Turn varargs into a list.
946 if (ufunc->uf_va_name != NULL)
947 {
948 int count = argcount - regular_args;
949
950 // TODO: add default values for optional arguments?
951 generate_NEWLIST(cctx, count < 0 ? 0 : count);
952 argcount = regular_args + 1;
953 }
954
955 if ((isn = generate_instr(cctx,
956 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
957 return FAIL;
958 if (ufunc->uf_dfunc_idx >= 0)
959 {
960 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
961 isn->isn_arg.dfunc.cdf_argcount = argcount;
962 }
963 else
964 {
965 // A user function may be deleted and redefined later, can't use the
966 // ufunc pointer, need to look it up again at runtime.
967 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
968 isn->isn_arg.ufunc.cuf_argcount = argcount;
969 }
970
971 stack->ga_len -= argcount; // drop the arguments
972 if (ga_grow(stack, 1) == FAIL)
973 return FAIL;
974 // add return value
975 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
976 ++stack->ga_len;
977
978 return OK;
979}
980
981/*
982 * Generate an ISN_UCALL instruction when the function isn't defined yet.
983 */
984 static int
985generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
986{
987 isn_T *isn;
988 garray_T *stack = &cctx->ctx_type_stack;
989
990 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
991 return FAIL;
992 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
993 isn->isn_arg.ufunc.cuf_argcount = argcount;
994
995 stack->ga_len -= argcount; // drop the arguments
996
997 // drop the funcref/partial, get back the return value
998 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
999
1000 return OK;
1001}
1002
1003/*
1004 * Generate an ISN_PCALL instruction.
1005 */
1006 static int
1007generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1008{
1009 isn_T *isn;
1010 garray_T *stack = &cctx->ctx_type_stack;
1011
1012 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1013 return FAIL;
1014 isn->isn_arg.pfunc.cpf_top = at_top;
1015 isn->isn_arg.pfunc.cpf_argcount = argcount;
1016
1017 stack->ga_len -= argcount; // drop the arguments
1018
1019 // drop the funcref/partial, get back the return value
1020 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1021
1022 return OK;
1023}
1024
1025/*
1026 * Generate an ISN_MEMBER instruction.
1027 */
1028 static int
1029generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1030{
1031 isn_T *isn;
1032 garray_T *stack = &cctx->ctx_type_stack;
1033 type_T *type;
1034
1035 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1036 return FAIL;
1037 isn->isn_arg.string = vim_strnsave(name, (int)len);
1038
1039 // change dict type to dict member type
1040 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1041 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
1042
1043 return OK;
1044}
1045
1046/*
1047 * Generate an ISN_ECHO instruction.
1048 */
1049 static int
1050generate_ECHO(cctx_T *cctx, int with_white, int count)
1051{
1052 isn_T *isn;
1053
1054 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1055 return FAIL;
1056 isn->isn_arg.echo.echo_with_white = with_white;
1057 isn->isn_arg.echo.echo_count = count;
1058
1059 return OK;
1060}
1061
1062 static int
1063generate_EXEC(cctx_T *cctx, char_u *line)
1064{
1065 isn_T *isn;
1066
1067 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1068 return FAIL;
1069 isn->isn_arg.string = vim_strsave(line);
1070 return OK;
1071}
1072
1073static char e_white_both[] =
1074 N_("E1004: white space required before and after '%s'");
1075
1076/*
1077 * Reserve space for a local variable.
1078 * Return the index or -1 if it failed.
1079 */
1080 static int
1081reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1082{
1083 int idx;
1084 lvar_T *lvar;
1085
1086 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1087 {
1088 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1089 return -1;
1090 }
1091
1092 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1093 return -1;
1094 idx = cctx->ctx_locals.ga_len;
1095 if (cctx->ctx_max_local < idx + 1)
1096 cctx->ctx_max_local = idx + 1;
1097 ++cctx->ctx_locals.ga_len;
1098
1099 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1100 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1101 lvar->lv_const = isConst;
1102 lvar->lv_type = type;
1103
1104 return idx;
1105}
1106
1107/*
1108 * Skip over a type definition and return a pointer to just after it.
1109 */
1110 char_u *
1111skip_type(char_u *start)
1112{
1113 char_u *p = start;
1114
1115 while (ASCII_ISALNUM(*p) || *p == '_')
1116 ++p;
1117
1118 // Skip over "<type>"; this is permissive about white space.
1119 if (*skipwhite(p) == '<')
1120 {
1121 p = skipwhite(p);
1122 p = skip_type(skipwhite(p + 1));
1123 p = skipwhite(p);
1124 if (*p == '>')
1125 ++p;
1126 }
1127 return p;
1128}
1129
1130/*
1131 * Parse the member type: "<type>" and return "type" with the member set.
1132 * Use "type_list" if a new type needs to be added.
1133 * Returns NULL in case of failure.
1134 */
1135 static type_T *
1136parse_type_member(char_u **arg, type_T *type, garray_T *type_list)
1137{
1138 type_T *member_type;
1139
1140 if (**arg != '<')
1141 {
1142 if (*skipwhite(*arg) == '<')
1143 emsg(_("E1007: No white space allowed before <"));
1144 else
1145 emsg(_("E1008: Missing <type>"));
1146 return NULL;
1147 }
1148 *arg = skipwhite(*arg + 1);
1149
1150 member_type = parse_type(arg, type_list);
1151 if (member_type == NULL)
1152 return NULL;
1153
1154 *arg = skipwhite(*arg);
1155 if (**arg != '>')
1156 {
1157 emsg(_("E1009: Missing > after type"));
1158 return NULL;
1159 }
1160 ++*arg;
1161
1162 if (type->tt_type == VAR_LIST)
1163 return get_list_type(member_type, type_list);
1164 return get_dict_type(member_type, type_list);
1165}
1166
1167/*
1168 * Parse a type at "arg" and advance over it.
1169 * Return NULL for failure.
1170 */
1171 type_T *
1172parse_type(char_u **arg, garray_T *type_list)
1173{
1174 char_u *p = *arg;
1175 size_t len;
1176
1177 // skip over the first word
1178 while (ASCII_ISALNUM(*p) || *p == '_')
1179 ++p;
1180 len = p - *arg;
1181
1182 switch (**arg)
1183 {
1184 case 'a':
1185 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1186 {
1187 *arg += len;
1188 return &t_any;
1189 }
1190 break;
1191 case 'b':
1192 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1193 {
1194 *arg += len;
1195 return &t_bool;
1196 }
1197 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1198 {
1199 *arg += len;
1200 return &t_blob;
1201 }
1202 break;
1203 case 'c':
1204 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1205 {
1206 *arg += len;
1207 return &t_channel;
1208 }
1209 break;
1210 case 'd':
1211 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1212 {
1213 *arg += len;
1214 return parse_type_member(arg, &t_dict_any, type_list);
1215 }
1216 break;
1217 case 'f':
1218 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1219 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001220#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221 *arg += len;
1222 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001223#else
1224 emsg(_("E1055: This Vim is not compiled with float support"));
1225 return &t_any;
1226#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001227 }
1228 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1229 {
1230 *arg += len;
1231 // TODO: arguments and return type
1232 return &t_func_any;
1233 }
1234 break;
1235 case 'j':
1236 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1237 {
1238 *arg += len;
1239 return &t_job;
1240 }
1241 break;
1242 case 'l':
1243 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1244 {
1245 *arg += len;
1246 return parse_type_member(arg, &t_list_any, type_list);
1247 }
1248 break;
1249 case 'n':
1250 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1251 {
1252 *arg += len;
1253 return &t_number;
1254 }
1255 break;
1256 case 'p':
1257 if (len == 4 && STRNCMP(*arg, "partial", len) == 0)
1258 {
1259 *arg += len;
1260 // TODO: arguments and return type
1261 return &t_partial_any;
1262 }
1263 break;
1264 case 's':
1265 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1266 {
1267 *arg += len;
1268 return &t_string;
1269 }
1270 break;
1271 case 'v':
1272 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1273 {
1274 *arg += len;
1275 return &t_void;
1276 }
1277 break;
1278 }
1279
1280 semsg(_("E1010: Type not recognized: %s"), *arg);
1281 return &t_any;
1282}
1283
1284/*
1285 * Check if "type1" and "type2" are exactly the same.
1286 */
1287 static int
1288equal_type(type_T *type1, type_T *type2)
1289{
1290 if (type1->tt_type != type2->tt_type)
1291 return FALSE;
1292 switch (type1->tt_type)
1293 {
1294 case VAR_VOID:
1295 case VAR_UNKNOWN:
1296 case VAR_SPECIAL:
1297 case VAR_BOOL:
1298 case VAR_NUMBER:
1299 case VAR_FLOAT:
1300 case VAR_STRING:
1301 case VAR_BLOB:
1302 case VAR_JOB:
1303 case VAR_CHANNEL:
1304 return TRUE; // not composite is always OK
1305 case VAR_LIST:
1306 case VAR_DICT:
1307 return equal_type(type1->tt_member, type2->tt_member);
1308 case VAR_FUNC:
1309 case VAR_PARTIAL:
1310 // TODO; check argument types.
1311 return equal_type(type1->tt_member, type2->tt_member)
1312 && type1->tt_argcount == type2->tt_argcount;
1313 }
1314 return TRUE;
1315}
1316
1317/*
1318 * Find the common type of "type1" and "type2" and put it in "dest".
1319 * "type2" and "dest" may be the same.
1320 */
1321 static void
1322common_type(type_T *type1, type_T *type2, type_T *dest)
1323{
1324 if (equal_type(type1, type2))
1325 {
1326 if (dest != type2)
1327 *dest = *type2;
1328 return;
1329 }
1330
1331 if (type1->tt_type == type2->tt_type)
1332 {
1333 dest->tt_type = type1->tt_type;
1334 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1335 {
1336 common_type(type1->tt_member, type2->tt_member, dest->tt_member);
1337 return;
1338 }
1339 // TODO: VAR_FUNC and VAR_PARTIAL
1340 }
1341
1342 dest->tt_type = VAR_UNKNOWN; // "any"
1343}
1344
1345 char *
1346vartype_name(vartype_T type)
1347{
1348 switch (type)
1349 {
1350 case VAR_VOID: return "void";
1351 case VAR_UNKNOWN: return "any";
1352 case VAR_SPECIAL: return "special";
1353 case VAR_BOOL: return "bool";
1354 case VAR_NUMBER: return "number";
1355 case VAR_FLOAT: return "float";
1356 case VAR_STRING: return "string";
1357 case VAR_BLOB: return "blob";
1358 case VAR_JOB: return "job";
1359 case VAR_CHANNEL: return "channel";
1360 case VAR_LIST: return "list";
1361 case VAR_DICT: return "dict";
1362 case VAR_FUNC: return "function";
1363 case VAR_PARTIAL: return "partial";
1364 }
1365 return "???";
1366}
1367
1368/*
1369 * Return the name of a type.
1370 * The result may be in allocated memory, in which case "tofree" is set.
1371 */
1372 char *
1373type_name(type_T *type, char **tofree)
1374{
1375 char *name = vartype_name(type->tt_type);
1376
1377 *tofree = NULL;
1378 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1379 {
1380 char *member_free;
1381 char *member_name = type_name(type->tt_member, &member_free);
1382 size_t len;
1383
1384 len = STRLEN(name) + STRLEN(member_name) + 3;
1385 *tofree = alloc(len);
1386 if (*tofree != NULL)
1387 {
1388 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1389 vim_free(member_free);
1390 return *tofree;
1391 }
1392 }
1393 // TODO: function and partial argument types
1394
1395 return name;
1396}
1397
1398/*
1399 * Find "name" in script-local items of script "sid".
1400 * Returns the index in "sn_var_vals" if found.
1401 * If found but not in "sn_var_vals" returns -1.
1402 * If not found returns -2.
1403 */
1404 int
1405get_script_item_idx(int sid, char_u *name, int check_writable)
1406{
1407 hashtab_T *ht;
1408 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001409 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 int idx;
1411
1412 // First look the name up in the hashtable.
1413 if (sid <= 0 || sid > script_items.ga_len)
1414 return -1;
1415 ht = &SCRIPT_VARS(sid);
1416 di = find_var_in_ht(ht, 0, name, TRUE);
1417 if (di == NULL)
1418 return -2;
1419
1420 // Now find the svar_T index in sn_var_vals.
1421 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1422 {
1423 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1424
1425 if (sv->sv_tv == &di->di_tv)
1426 {
1427 if (check_writable && sv->sv_const)
1428 semsg(_(e_readonlyvar), name);
1429 return idx;
1430 }
1431 }
1432 return -1;
1433}
1434
1435/*
1436 * Find "name" in imported items of the current script/
1437 */
1438 imported_T *
1439find_imported(char_u *name, cctx_T *cctx)
1440{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001441 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 int idx;
1443
1444 if (cctx != NULL)
1445 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1446 {
1447 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1448 + idx;
1449
1450 if (STRCMP(name, import->imp_name) == 0)
1451 return import;
1452 }
1453
1454 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1455 {
1456 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1457
1458 if (STRCMP(name, import->imp_name) == 0)
1459 return import;
1460 }
1461 return NULL;
1462}
1463
1464/*
1465 * Generate an instruction to load script-local variable "name".
1466 */
1467 static int
1468compile_load_scriptvar(cctx_T *cctx, char_u *name)
1469{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001470 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1472 imported_T *import;
1473
1474 if (idx == -1)
1475 {
1476 // variable exists but is not in sn_var_vals: old style script.
1477 return generate_LOADS(cctx, name, current_sctx.sc_sid);
1478 }
1479 if (idx >= 0)
1480 {
1481 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1482
1483 generate_SCRIPT(cctx, ISN_LOADSCRIPT,
1484 current_sctx.sc_sid, idx, sv->sv_type);
1485 return OK;
1486 }
1487
1488 import = find_imported(name, cctx);
1489 if (import != NULL)
1490 {
1491 // TODO: check this is a variable, not a function
1492 generate_SCRIPT(cctx, ISN_LOADSCRIPT,
1493 import->imp_sid,
1494 import->imp_var_vals_idx,
1495 import->imp_type);
1496 return OK;
1497 }
1498
1499 semsg(_("E1050: Item not found: %s"), name);
1500 return FAIL;
1501}
1502
1503/*
1504 * Compile a variable name into a load instruction.
1505 * "end" points to just after the name.
1506 * When "error" is FALSE do not give an error when not found.
1507 */
1508 static int
1509compile_load(char_u **arg, char_u *end, cctx_T *cctx, int error)
1510{
1511 type_T *type;
1512 char_u *name;
1513 int res = FAIL;
1514
1515 if (*(*arg + 1) == ':')
1516 {
1517 // load namespaced variable
1518 name = vim_strnsave(*arg + 2, end - (*arg + 2));
1519 if (name == NULL)
1520 return FAIL;
1521
1522 if (**arg == 'v')
1523 {
1524 // load v:var
1525 int vidx = find_vim_var(name);
1526
1527 if (vidx < 0)
1528 {
1529 if (error)
1530 semsg(_(e_var_notfound), name);
1531 goto theend;
1532 }
1533
1534 // TODO: get actual type
1535 res = generate_LOAD(cctx, ISN_LOADV, vidx, NULL, &t_any);
1536 }
1537 else if (**arg == 'g')
1538 {
1539 // Global variables can be defined later, thus we don't check if it
1540 // exists, give error at runtime.
1541 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
1542 }
1543 else if (**arg == 's')
1544 {
1545 res = compile_load_scriptvar(cctx, name);
1546 }
1547 else
1548 {
1549 semsg("Namespace not supported yet: %s", **arg);
1550 goto theend;
1551 }
1552 }
1553 else
1554 {
1555 size_t len = end - *arg;
1556 int idx;
1557 int gen_load = FALSE;
1558
1559 name = vim_strnsave(*arg, end - *arg);
1560 if (name == NULL)
1561 return FAIL;
1562
1563 idx = lookup_arg(*arg, len, cctx);
1564 if (idx >= 0)
1565 {
1566 if (cctx->ctx_ufunc->uf_arg_types != NULL)
1567 type = cctx->ctx_ufunc->uf_arg_types[idx];
1568 else
1569 type = &t_any;
1570
1571 // Arguments are located above the frame pointer.
1572 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
1573 if (cctx->ctx_ufunc->uf_va_name != NULL)
1574 --idx;
1575 gen_load = TRUE;
1576 }
1577 else if (lookup_vararg(*arg, len, cctx))
1578 {
1579 // varargs is always the last argument
1580 idx = -STACK_FRAME_SIZE - 1;
1581 type = cctx->ctx_ufunc->uf_va_type;
1582 gen_load = TRUE;
1583 }
1584 else
1585 {
1586 idx = lookup_local(*arg, len, cctx);
1587 if (idx >= 0)
1588 {
1589 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
1590 gen_load = TRUE;
1591 }
1592 else
1593 {
1594 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
1595 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
1596 res = generate_PUSHBOOL(cctx, **arg == 't'
1597 ? VVAL_TRUE : VVAL_FALSE);
1598 else
1599 res = compile_load_scriptvar(cctx, name);
1600 }
1601 }
1602 if (gen_load)
1603 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
1604 }
1605
1606 *arg = end;
1607
1608theend:
1609 if (res == FAIL && error)
1610 semsg(_(e_var_notfound), name);
1611 vim_free(name);
1612 return res;
1613}
1614
1615/*
1616 * Compile the argument expressions.
1617 * "arg" points to just after the "(" and is advanced to after the ")"
1618 */
1619 static int
1620compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
1621{
1622 char_u *p = *arg;
1623
1624 while (*p != NUL && *p != ')')
1625 {
1626 if (compile_expr1(&p, cctx) == FAIL)
1627 return FAIL;
1628 ++*argcount;
1629 if (*p == ',')
1630 p = skipwhite(p + 1);
1631 }
1632 if (*p != ')')
1633 {
1634 emsg(_(e_missing_close));
1635 return FAIL;
1636 }
1637 *arg = p + 1;
1638 return OK;
1639}
1640
1641/*
1642 * Compile a function call: name(arg1, arg2)
1643 * "arg" points to "name", "arg + varlen" to the "(".
1644 * "argcount_init" is 1 for "value->method()"
1645 * Instructions:
1646 * EVAL arg1
1647 * EVAL arg2
1648 * BCALL / DCALL / UCALL
1649 */
1650 static int
1651compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
1652{
1653 char_u *name = *arg;
1654 char_u *p = *arg + varlen + 1;
1655 int argcount = argcount_init;
1656 char_u namebuf[100];
1657 ufunc_T *ufunc;
1658
1659 if (varlen >= sizeof(namebuf))
1660 {
1661 semsg(_("E1011: name too long: %s"), name);
1662 return FAIL;
1663 }
1664 vim_strncpy(namebuf, name, varlen);
1665
1666 *arg = skipwhite(*arg + varlen + 1);
1667 if (compile_arguments(arg, cctx, &argcount) == FAIL)
1668 return FAIL;
1669
1670 if (ASCII_ISLOWER(*name))
1671 {
1672 int idx;
1673
1674 // builtin function
1675 idx = find_internal_func(namebuf);
1676 if (idx >= 0)
1677 return generate_BCALL(cctx, idx, argcount);
1678 semsg(_(e_unknownfunc), namebuf);
1679 }
1680
1681 // User defined function or variable must start with upper case.
1682 if (!ASCII_ISUPPER(*name))
1683 {
1684 semsg(_("E1012: Invalid function name: %s"), namebuf);
1685 return FAIL;
1686 }
1687
1688 // If we can find the function by name generate the right call.
1689 ufunc = find_func(namebuf, cctx);
1690 if (ufunc != NULL)
1691 return generate_CALL(cctx, ufunc, argcount);
1692
1693 // If the name is a variable, load it and use PCALL.
1694 p = namebuf;
1695 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
1696 return generate_PCALL(cctx, argcount, FALSE);
1697
1698 // The function may be defined only later. Need to figure out at runtime.
1699 return generate_UCALL(cctx, namebuf, argcount);
1700}
1701
1702// like NAMESPACE_CHAR but with 'a' and 'l'.
1703#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1704
1705/*
1706 * Find the end of a variable or function name. Unlike find_name_end() this
1707 * does not recognize magic braces.
1708 * Return a pointer to just after the name. Equal to "arg" if there is no
1709 * valid name.
1710 */
1711 char_u *
1712to_name_end(char_u *arg)
1713{
1714 char_u *p;
1715
1716 // Quick check for valid starting character.
1717 if (!eval_isnamec1(*arg))
1718 return arg;
1719
1720 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1721 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1722 // and can be used in slice "[n:]".
1723 if (*p == ':' && (p != arg + 1
1724 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1725 break;
1726 return p;
1727}
1728
1729/*
1730 * Like to_name_end() but also skip over a list or dict constant.
1731 */
1732 char_u *
1733to_name_const_end(char_u *arg)
1734{
1735 char_u *p = to_name_end(arg);
1736 typval_T rettv;
1737
1738 if (p == arg && *arg == '[')
1739 {
1740
1741 // Can be "[1, 2, 3]->Func()".
1742 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
1743 p = arg;
1744 }
1745 else if (p == arg && *arg == '#' && arg[1] == '{')
1746 {
1747 ++p;
1748 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
1749 p = arg;
1750 }
1751 else if (p == arg && *arg == '{')
1752 {
1753 int ret = get_lambda_tv(&p, &rettv, FALSE);
1754
1755 if (ret == NOTDONE)
1756 ret = eval_dict(&p, &rettv, FALSE, FALSE);
1757 if (ret != OK)
1758 p = arg;
1759 }
1760
1761 return p;
1762}
1763
1764 static void
1765type_mismatch(type_T *expected, type_T *actual)
1766{
1767 char *tofree1, *tofree2;
1768
1769 semsg(_("E1013: type mismatch, expected %s but got %s"),
1770 type_name(expected, &tofree1), type_name(actual, &tofree2));
1771 vim_free(tofree1);
1772 vim_free(tofree2);
1773}
1774
1775/*
1776 * Check if the expected and actual types match.
1777 */
1778 static int
1779check_type(type_T *expected, type_T *actual, int give_msg)
1780{
1781 if (expected->tt_type != VAR_UNKNOWN)
1782 {
1783 if (expected->tt_type != actual->tt_type)
1784 {
1785 if (give_msg)
1786 type_mismatch(expected, actual);
1787 return FAIL;
1788 }
1789 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
1790 {
1791 int ret = check_type(expected->tt_member, actual->tt_member,
1792 FALSE);
1793 if (ret == FAIL && give_msg)
1794 type_mismatch(expected, actual);
1795 return ret;
1796 }
1797 }
1798 return OK;
1799}
1800
1801/*
1802 * Check that
1803 * - "actual" is "expected" type or
1804 * - "actual" is a type that can be "expected" type: add a runtime check; or
1805 * - return FAIL.
1806 */
1807 static int
1808need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
1809{
1810 if (equal_type(actual, expected) || expected->tt_type == VAR_UNKNOWN)
1811 return OK;
1812 if (actual->tt_type != VAR_UNKNOWN)
1813 {
1814 type_mismatch(expected, actual);
1815 return FAIL;
1816 }
1817 generate_TYPECHECK(cctx, expected, offset);
1818 return OK;
1819}
1820
1821/*
1822 * parse a list: [expr, expr]
1823 * "*arg" points to the '['.
1824 */
1825 static int
1826compile_list(char_u **arg, cctx_T *cctx)
1827{
1828 char_u *p = skipwhite(*arg + 1);
1829 int count = 0;
1830
1831 while (*p != ']')
1832 {
1833 if (*p == NUL)
1834 return FAIL;
1835 if (compile_expr1(&p, cctx) == FAIL)
1836 break;
1837 ++count;
1838 if (*p == ',')
1839 ++p;
1840 p = skipwhite(p);
1841 }
1842 *arg = p + 1;
1843
1844 generate_NEWLIST(cctx, count);
1845 return OK;
1846}
1847
1848/*
1849 * parse a lambda: {arg, arg -> expr}
1850 * "*arg" points to the '{'.
1851 */
1852 static int
1853compile_lambda(char_u **arg, cctx_T *cctx)
1854{
1855 garray_T *instr = &cctx->ctx_instr;
1856 typval_T rettv;
1857 ufunc_T *ufunc;
1858
1859 // Get the funcref in "rettv".
1860 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
1861 return FAIL;
1862 ufunc = rettv.vval.v_partial->pt_func;
1863
1864 // The function will have one line: "return {expr}".
1865 // Compile it into instructions.
1866 compile_def_function(ufunc, TRUE);
1867
1868 if (ufunc->uf_dfunc_idx >= 0)
1869 {
1870 if (ga_grow(instr, 1) == FAIL)
1871 return FAIL;
1872 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
1873 return OK;
1874 }
1875 return FAIL;
1876}
1877
1878/*
1879 * Compile a lamda call: expr->{lambda}(args)
1880 * "arg" points to the "{".
1881 */
1882 static int
1883compile_lambda_call(char_u **arg, cctx_T *cctx)
1884{
1885 ufunc_T *ufunc;
1886 typval_T rettv;
1887 int argcount = 1;
1888 int ret = FAIL;
1889
1890 // Get the funcref in "rettv".
1891 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
1892 return FAIL;
1893
1894 if (**arg != '(')
1895 {
1896 if (*skipwhite(*arg) == '(')
1897 semsg(_(e_nowhitespace));
1898 else
1899 semsg(_(e_missing_paren), "lambda");
1900 clear_tv(&rettv);
1901 return FAIL;
1902 }
1903
1904 // The function will have one line: "return {expr}".
1905 // Compile it into instructions.
1906 ufunc = rettv.vval.v_partial->pt_func;
1907 ++ufunc->uf_refcount;
1908 compile_def_function(ufunc, TRUE);
1909
1910 // compile the arguments
1911 *arg = skipwhite(*arg + 1);
1912 if (compile_arguments(arg, cctx, &argcount) == OK)
1913 // call the compiled function
1914 ret = generate_CALL(cctx, ufunc, argcount);
1915
1916 clear_tv(&rettv);
1917 return ret;
1918}
1919
1920/*
1921 * parse a dict: {'key': val} or #{key: val}
1922 * "*arg" points to the '{'.
1923 */
1924 static int
1925compile_dict(char_u **arg, cctx_T *cctx, int literal)
1926{
1927 garray_T *instr = &cctx->ctx_instr;
1928 int count = 0;
1929 dict_T *d = dict_alloc();
1930 dictitem_T *item;
1931
1932 if (d == NULL)
1933 return FAIL;
1934 *arg = skipwhite(*arg + 1);
1935 while (**arg != '}' && **arg != NUL)
1936 {
1937 char_u *key = NULL;
1938
1939 if (literal)
1940 {
1941 char_u *p = to_name_end(*arg);
1942
1943 if (p == *arg)
1944 {
1945 semsg(_("E1014: Invalid key: %s"), *arg);
1946 return FAIL;
1947 }
1948 key = vim_strnsave(*arg, p - *arg);
1949 if (generate_PUSHS(cctx, key) == FAIL)
1950 return FAIL;
1951 *arg = p;
1952 }
1953 else
1954 {
1955 isn_T *isn;
1956
1957 if (compile_expr1(arg, cctx) == FAIL)
1958 return FAIL;
1959 // TODO: check type is string
1960 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1961 if (isn->isn_type == ISN_PUSHS)
1962 key = isn->isn_arg.string;
1963 }
1964
1965 // Check for duplicate keys, if using string keys.
1966 if (key != NULL)
1967 {
1968 item = dict_find(d, key, -1);
1969 if (item != NULL)
1970 {
1971 semsg(_(e_duplicate_key), key);
1972 goto failret;
1973 }
1974 item = dictitem_alloc(key);
1975 if (item != NULL)
1976 {
1977 item->di_tv.v_type = VAR_UNKNOWN;
1978 item->di_tv.v_lock = 0;
1979 if (dict_add(d, item) == FAIL)
1980 dictitem_free(item);
1981 }
1982 }
1983
1984 *arg = skipwhite(*arg);
1985 if (**arg != ':')
1986 {
1987 semsg(_(e_missing_dict_colon), *arg);
1988 return FAIL;
1989 }
1990
1991 *arg = skipwhite(*arg + 1);
1992 if (compile_expr1(arg, cctx) == FAIL)
1993 return FAIL;
1994 ++count;
1995
1996 if (**arg == '}')
1997 break;
1998 if (**arg != ',')
1999 {
2000 semsg(_(e_missing_dict_comma), *arg);
2001 goto failret;
2002 }
2003 *arg = skipwhite(*arg + 1);
2004 }
2005
2006 if (**arg != '}')
2007 {
2008 semsg(_(e_missing_dict_end), *arg);
2009 goto failret;
2010 }
2011 *arg = *arg + 1;
2012
2013 dict_unref(d);
2014 return generate_NEWDICT(cctx, count);
2015
2016failret:
2017 dict_unref(d);
2018 return FAIL;
2019}
2020
2021/*
2022 * Compile "&option".
2023 */
2024 static int
2025compile_get_option(char_u **arg, cctx_T *cctx)
2026{
2027 typval_T rettv;
2028 char_u *start = *arg;
2029 int ret;
2030
2031 // parse the option and get the current value to get the type.
2032 rettv.v_type = VAR_UNKNOWN;
2033 ret = get_option_tv(arg, &rettv, TRUE);
2034 if (ret == OK)
2035 {
2036 // include the '&' in the name, get_option_tv() expects it.
2037 char_u *name = vim_strnsave(start, *arg - start);
2038 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2039
2040 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2041 vim_free(name);
2042 }
2043 clear_tv(&rettv);
2044
2045 return ret;
2046}
2047
2048/*
2049 * Compile "$VAR".
2050 */
2051 static int
2052compile_get_env(char_u **arg, cctx_T *cctx)
2053{
2054 char_u *start = *arg;
2055 int len;
2056 int ret;
2057 char_u *name;
2058
2059 start = *arg;
2060 ++*arg;
2061 len = get_env_len(arg);
2062 if (len == 0)
2063 {
2064 semsg(_(e_syntax_at), start - 1);
2065 return FAIL;
2066 }
2067
2068 // include the '$' in the name, get_env_tv() expects it.
2069 name = vim_strnsave(start, len + 1);
2070 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2071 vim_free(name);
2072 return ret;
2073}
2074
2075/*
2076 * Compile "@r".
2077 */
2078 static int
2079compile_get_register(char_u **arg, cctx_T *cctx)
2080{
2081 int ret;
2082
2083 ++*arg;
2084 if (**arg == NUL)
2085 {
2086 semsg(_(e_syntax_at), *arg - 1);
2087 return FAIL;
2088 }
2089 if (!valid_yank_reg(**arg, TRUE))
2090 {
2091 emsg_invreg(**arg);
2092 return FAIL;
2093 }
2094 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2095 ++*arg;
2096 return ret;
2097}
2098
2099/*
2100 * Apply leading '!', '-' and '+' to constant "rettv".
2101 */
2102 static int
2103apply_leader(typval_T *rettv, char_u *start, char_u *end)
2104{
2105 char_u *p = end;
2106
2107 // this works from end to start
2108 while (p > start)
2109 {
2110 --p;
2111 if (*p == '-' || *p == '+')
2112 {
2113 // only '-' has an effect, for '+' we only check the type
2114#ifdef FEAT_FLOAT
2115 if (rettv->v_type == VAR_FLOAT)
2116 {
2117 if (*p == '-')
2118 rettv->vval.v_float = -rettv->vval.v_float;
2119 }
2120 else
2121#endif
2122 {
2123 varnumber_T val;
2124 int error = FALSE;
2125
2126 // tv_get_number_chk() accepts a string, but we don't want that
2127 // here
2128 if (check_not_string(rettv) == FAIL)
2129 return FAIL;
2130 val = tv_get_number_chk(rettv, &error);
2131 clear_tv(rettv);
2132 if (error)
2133 return FAIL;
2134 if (*p == '-')
2135 val = -val;
2136 rettv->v_type = VAR_NUMBER;
2137 rettv->vval.v_number = val;
2138 }
2139 }
2140 else
2141 {
2142 int v = tv2bool(rettv);
2143
2144 // '!' is permissive in the type.
2145 clear_tv(rettv);
2146 rettv->v_type = VAR_BOOL;
2147 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2148 }
2149 }
2150 return OK;
2151}
2152
2153/*
2154 * Recognize v: variables that are constants and set "rettv".
2155 */
2156 static void
2157get_vim_constant(char_u **arg, typval_T *rettv)
2158{
2159 if (STRNCMP(*arg, "v:true", 6) == 0)
2160 {
2161 rettv->v_type = VAR_BOOL;
2162 rettv->vval.v_number = VVAL_TRUE;
2163 *arg += 6;
2164 }
2165 else if (STRNCMP(*arg, "v:false", 7) == 0)
2166 {
2167 rettv->v_type = VAR_BOOL;
2168 rettv->vval.v_number = VVAL_FALSE;
2169 *arg += 7;
2170 }
2171 else if (STRNCMP(*arg, "v:null", 6) == 0)
2172 {
2173 rettv->v_type = VAR_SPECIAL;
2174 rettv->vval.v_number = VVAL_NULL;
2175 *arg += 6;
2176 }
2177 else if (STRNCMP(*arg, "v:none", 6) == 0)
2178 {
2179 rettv->v_type = VAR_SPECIAL;
2180 rettv->vval.v_number = VVAL_NONE;
2181 *arg += 6;
2182 }
2183}
2184
2185/*
2186 * Compile code to apply '-', '+' and '!'.
2187 */
2188 static int
2189compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2190{
2191 char_u *p = end;
2192
2193 // this works from end to start
2194 while (p > start)
2195 {
2196 --p;
2197 if (*p == '-' || *p == '+')
2198 {
2199 int negate = *p == '-';
2200 isn_T *isn;
2201
2202 // TODO: check type
2203 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2204 {
2205 --p;
2206 if (*p == '-')
2207 negate = !negate;
2208 }
2209 // only '-' has an effect, for '+' we only check the type
2210 if (negate)
2211 isn = generate_instr(cctx, ISN_NEGATENR);
2212 else
2213 isn = generate_instr(cctx, ISN_CHECKNR);
2214 if (isn == NULL)
2215 return FAIL;
2216 }
2217 else
2218 {
2219 int invert = TRUE;
2220
2221 while (p > start && p[-1] == '!')
2222 {
2223 --p;
2224 invert = !invert;
2225 }
2226 if (generate_2BOOL(cctx, invert) == FAIL)
2227 return FAIL;
2228 }
2229 }
2230 return OK;
2231}
2232
2233/*
2234 * Compile whatever comes after "name" or "name()".
2235 */
2236 static int
2237compile_subscript(
2238 char_u **arg,
2239 cctx_T *cctx,
2240 char_u **start_leader,
2241 char_u *end_leader)
2242{
2243 for (;;)
2244 {
2245 if (**arg == '(')
2246 {
2247 int argcount = 0;
2248
2249 // funcref(arg)
2250 *arg = skipwhite(*arg + 1);
2251 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2252 return FAIL;
2253 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2254 return FAIL;
2255 }
2256 else if (**arg == '-' && (*arg)[1] == '>')
2257 {
2258 char_u *p;
2259
2260 // something->method()
2261 // Apply the '!', '-' and '+' first:
2262 // -1.0->func() works like (-1.0)->func()
2263 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2264 return FAIL;
2265 *start_leader = end_leader; // don't apply again later
2266
2267 *arg = skipwhite(*arg + 2);
2268 if (**arg == '{')
2269 {
2270 // lambda call: list->{lambda}
2271 if (compile_lambda_call(arg, cctx) == FAIL)
2272 return FAIL;
2273 }
2274 else
2275 {
2276 // method call: list->method()
2277 for (p = *arg; eval_isnamec1(*p); ++p)
2278 ;
2279 if (*p != '(')
2280 {
2281 semsg(_(e_missing_paren), arg);
2282 return FAIL;
2283 }
2284 // TODO: base value may not be the first argument
2285 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2286 return FAIL;
2287 }
2288 }
2289 else if (**arg == '[')
2290 {
2291 // list index: list[123]
2292 // TODO: more arguments
2293 // TODO: dict member dict['name']
2294 *arg = skipwhite(*arg + 1);
2295 if (compile_expr1(arg, cctx) == FAIL)
2296 return FAIL;
2297
2298 if (**arg != ']')
2299 {
2300 emsg(_(e_missbrac));
2301 return FAIL;
2302 }
2303 *arg = skipwhite(*arg + 1);
2304
2305 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
2306 return FAIL;
2307 }
2308 else if (**arg == '.' && (*arg)[1] != '.')
2309 {
2310 char_u *p;
2311
2312 ++*arg;
2313 p = *arg;
2314 // dictionary member: dict.name
2315 if (eval_isnamec1(*p))
2316 while (eval_isnamec(*p))
2317 MB_PTR_ADV(p);
2318 if (p == *arg)
2319 {
2320 semsg(_(e_syntax_at), *arg);
2321 return FAIL;
2322 }
2323 // TODO: check type is dict
2324 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
2325 return FAIL;
2326 *arg = p;
2327 }
2328 else
2329 break;
2330 }
2331
2332 // TODO - see handle_subscript():
2333 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2334 // Don't do this when "Func" is already a partial that was bound
2335 // explicitly (pt_auto is FALSE).
2336
2337 return OK;
2338}
2339
2340/*
2341 * Compile an expression at "*p" and add instructions to "instr".
2342 * "p" is advanced until after the expression, skipping white space.
2343 *
2344 * This is the equivalent of eval1(), eval2(), etc.
2345 */
2346
2347/*
2348 * number number constant
2349 * 0zFFFFFFFF Blob constant
2350 * "string" string constant
2351 * 'string' literal string constant
2352 * &option-name option value
2353 * @r register contents
2354 * identifier variable value
2355 * function() function call
2356 * $VAR environment variable
2357 * (expression) nested expression
2358 * [expr, expr] List
2359 * {key: val, key: val} Dictionary
2360 * #{key: val, key: val} Dictionary with literal keys
2361 *
2362 * Also handle:
2363 * ! in front logical NOT
2364 * - in front unary minus
2365 * + in front unary plus (ignored)
2366 * trailing (arg) funcref/partial call
2367 * trailing [] subscript in String or List
2368 * trailing .name entry in Dictionary
2369 * trailing ->name() method call
2370 */
2371 static int
2372compile_expr7(char_u **arg, cctx_T *cctx)
2373{
2374 typval_T rettv;
2375 char_u *start_leader, *end_leader;
2376 int ret = OK;
2377
2378 /*
2379 * Skip '!', '-' and '+' characters. They are handled later.
2380 */
2381 start_leader = *arg;
2382 while (**arg == '!' || **arg == '-' || **arg == '+')
2383 *arg = skipwhite(*arg + 1);
2384 end_leader = *arg;
2385
2386 rettv.v_type = VAR_UNKNOWN;
2387 switch (**arg)
2388 {
2389 /*
2390 * Number constant.
2391 */
2392 case '0': // also for blob starting with 0z
2393 case '1':
2394 case '2':
2395 case '3':
2396 case '4':
2397 case '5':
2398 case '6':
2399 case '7':
2400 case '8':
2401 case '9':
2402 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
2403 return FAIL;
2404 break;
2405
2406 /*
2407 * String constant: "string".
2408 */
2409 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
2410 return FAIL;
2411 break;
2412
2413 /*
2414 * Literal string constant: 'str''ing'.
2415 */
2416 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
2417 return FAIL;
2418 break;
2419
2420 /*
2421 * Constant Vim variable.
2422 */
2423 case 'v': get_vim_constant(arg, &rettv);
2424 ret = NOTDONE;
2425 break;
2426
2427 /*
2428 * List: [expr, expr]
2429 */
2430 case '[': ret = compile_list(arg, cctx);
2431 break;
2432
2433 /*
2434 * Dictionary: #{key: val, key: val}
2435 */
2436 case '#': if ((*arg)[1] == '{')
2437 {
2438 ++*arg;
2439 ret = compile_dict(arg, cctx, TRUE);
2440 }
2441 else
2442 ret = NOTDONE;
2443 break;
2444
2445 /*
2446 * Lambda: {arg, arg -> expr}
2447 * Dictionary: {'key': val, 'key': val}
2448 */
2449 case '{': {
2450 char_u *start = skipwhite(*arg + 1);
2451
2452 // Find out what comes after the arguments.
2453 ret = get_function_args(&start, '-', NULL,
2454 NULL, NULL, NULL, TRUE);
2455 if (ret != FAIL && *start == '>')
2456 ret = compile_lambda(arg, cctx);
2457 else
2458 ret = compile_dict(arg, cctx, FALSE);
2459 }
2460 break;
2461
2462 /*
2463 * Option value: &name
2464 */
2465 case '&': ret = compile_get_option(arg, cctx);
2466 break;
2467
2468 /*
2469 * Environment variable: $VAR.
2470 */
2471 case '$': ret = compile_get_env(arg, cctx);
2472 break;
2473
2474 /*
2475 * Register contents: @r.
2476 */
2477 case '@': ret = compile_get_register(arg, cctx);
2478 break;
2479 /*
2480 * nested expression: (expression).
2481 */
2482 case '(': *arg = skipwhite(*arg + 1);
2483 ret = compile_expr1(arg, cctx); // recursive!
2484 *arg = skipwhite(*arg);
2485 if (**arg == ')')
2486 ++*arg;
2487 else if (ret == OK)
2488 {
2489 emsg(_(e_missing_close));
2490 ret = FAIL;
2491 }
2492 break;
2493
2494 default: ret = NOTDONE;
2495 break;
2496 }
2497 if (ret == FAIL)
2498 return FAIL;
2499
2500 if (rettv.v_type != VAR_UNKNOWN)
2501 {
2502 // apply the '!', '-' and '+' before the constant
2503 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
2504 {
2505 clear_tv(&rettv);
2506 return FAIL;
2507 }
2508 start_leader = end_leader; // don't apply again below
2509
2510 // push constant
2511 switch (rettv.v_type)
2512 {
2513 case VAR_BOOL:
2514 generate_PUSHBOOL(cctx, rettv.vval.v_number);
2515 break;
2516 case VAR_SPECIAL:
2517 generate_PUSHSPEC(cctx, rettv.vval.v_number);
2518 break;
2519 case VAR_NUMBER:
2520 generate_PUSHNR(cctx, rettv.vval.v_number);
2521 break;
2522#ifdef FEAT_FLOAT
2523 case VAR_FLOAT:
2524 generate_PUSHF(cctx, rettv.vval.v_float);
2525 break;
2526#endif
2527 case VAR_BLOB:
2528 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
2529 rettv.vval.v_blob = NULL;
2530 break;
2531 case VAR_STRING:
2532 generate_PUSHS(cctx, rettv.vval.v_string);
2533 rettv.vval.v_string = NULL;
2534 break;
2535 default:
2536 iemsg("constant type missing");
2537 return FAIL;
2538 }
2539 }
2540 else if (ret == NOTDONE)
2541 {
2542 char_u *p;
2543 int r;
2544
2545 if (!eval_isnamec1(**arg))
2546 {
2547 semsg(_("E1015: Name expected: %s"), *arg);
2548 return FAIL;
2549 }
2550
2551 // "name" or "name()"
2552 p = to_name_end(*arg);
2553 if (*p == '(')
2554 r = compile_call(arg, p - *arg, cctx, 0);
2555 else
2556 r = compile_load(arg, p, cctx, TRUE);
2557 if (r == FAIL)
2558 return FAIL;
2559 }
2560
2561 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
2562 return FAIL;
2563
2564 // Now deal with prefixed '-', '+' and '!', if not done already.
2565 return compile_leader(cctx, start_leader, end_leader);
2566}
2567
2568/*
2569 * * number multiplication
2570 * / number division
2571 * % number modulo
2572 */
2573 static int
2574compile_expr6(char_u **arg, cctx_T *cctx)
2575{
2576 char_u *op;
2577
2578 // get the first variable
2579 if (compile_expr7(arg, cctx) == FAIL)
2580 return FAIL;
2581
2582 /*
2583 * Repeat computing, until no "*", "/" or "%" is following.
2584 */
2585 for (;;)
2586 {
2587 op = skipwhite(*arg);
2588 if (*op != '*' && *op != '/' && *op != '%')
2589 break;
2590 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
2591 {
2592 char_u buf[3];
2593
2594 vim_strncpy(buf, op, 1);
2595 semsg(_(e_white_both), buf);
2596 }
2597 *arg = skipwhite(op + 1);
2598
2599 // get the second variable
2600 if (compile_expr7(arg, cctx) == FAIL)
2601 return FAIL;
2602
2603 generate_two_op(cctx, op);
2604 }
2605
2606 return OK;
2607}
2608
2609/*
2610 * + number addition
2611 * - number subtraction
2612 * .. string concatenation
2613 */
2614 static int
2615compile_expr5(char_u **arg, cctx_T *cctx)
2616{
2617 char_u *op;
2618 int oplen;
2619
2620 // get the first variable
2621 if (compile_expr6(arg, cctx) == FAIL)
2622 return FAIL;
2623
2624 /*
2625 * Repeat computing, until no "+", "-" or ".." is following.
2626 */
2627 for (;;)
2628 {
2629 op = skipwhite(*arg);
2630 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
2631 break;
2632 oplen = (*op == '.' ? 2 : 1);
2633
2634 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
2635 {
2636 char_u buf[3];
2637
2638 vim_strncpy(buf, op, oplen);
2639 semsg(_(e_white_both), buf);
2640 }
2641
2642 *arg = skipwhite(op + oplen);
2643
2644 // get the second variable
2645 if (compile_expr6(arg, cctx) == FAIL)
2646 return FAIL;
2647
2648 if (*op == '.')
2649 {
2650 if (may_generate_2STRING(-2, cctx) == FAIL
2651 || may_generate_2STRING(-1, cctx) == FAIL)
2652 return FAIL;
2653 generate_instr_drop(cctx, ISN_CONCAT, 1);
2654 }
2655 else
2656 generate_two_op(cctx, op);
2657 }
2658
2659 return OK;
2660}
2661
2662/*
2663 * expr5a == expr5b
2664 * expr5a =~ expr5b
2665 * expr5a != expr5b
2666 * expr5a !~ expr5b
2667 * expr5a > expr5b
2668 * expr5a >= expr5b
2669 * expr5a < expr5b
2670 * expr5a <= expr5b
2671 * expr5a is expr5b
2672 * expr5a isnot expr5b
2673 *
2674 * Produces instructions:
2675 * EVAL expr5a Push result of "expr5a"
2676 * EVAL expr5b Push result of "expr5b"
2677 * COMPARE one of the compare instructions
2678 */
2679 static int
2680compile_expr4(char_u **arg, cctx_T *cctx)
2681{
2682 exptype_T type = EXPR_UNKNOWN;
2683 char_u *p;
2684 int len = 2;
2685 int i;
2686 int type_is = FALSE;
2687
2688 // get the first variable
2689 if (compile_expr5(arg, cctx) == FAIL)
2690 return FAIL;
2691
2692 p = skipwhite(*arg);
2693 switch (p[0])
2694 {
2695 case '=': if (p[1] == '=')
2696 type = EXPR_EQUAL;
2697 else if (p[1] == '~')
2698 type = EXPR_MATCH;
2699 break;
2700 case '!': if (p[1] == '=')
2701 type = EXPR_NEQUAL;
2702 else if (p[1] == '~')
2703 type = EXPR_NOMATCH;
2704 break;
2705 case '>': if (p[1] != '=')
2706 {
2707 type = EXPR_GREATER;
2708 len = 1;
2709 }
2710 else
2711 type = EXPR_GEQUAL;
2712 break;
2713 case '<': if (p[1] != '=')
2714 {
2715 type = EXPR_SMALLER;
2716 len = 1;
2717 }
2718 else
2719 type = EXPR_SEQUAL;
2720 break;
2721 case 'i': if (p[1] == 's')
2722 {
2723 // "is" and "isnot"; but not a prefix of a name
2724 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2725 len = 5;
2726 i = p[len];
2727 if (!isalnum(i) && i != '_')
2728 {
2729 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
2730 type_is = TRUE;
2731 }
2732 }
2733 break;
2734 }
2735
2736 /*
2737 * If there is a comparative operator, use it.
2738 */
2739 if (type != EXPR_UNKNOWN)
2740 {
2741 int ic = FALSE; // Default: do not ignore case
2742
2743 if (type_is && (p[len] == '?' || p[len] == '#'))
2744 {
2745 semsg(_(e_invexpr2), *arg);
2746 return FAIL;
2747 }
2748 // extra question mark appended: ignore case
2749 if (p[len] == '?')
2750 {
2751 ic = TRUE;
2752 ++len;
2753 }
2754 // extra '#' appended: match case (ignored)
2755 else if (p[len] == '#')
2756 ++len;
2757 // nothing appended: match case
2758
2759 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
2760 {
2761 char_u buf[7];
2762
2763 vim_strncpy(buf, p, len);
2764 semsg(_(e_white_both), buf);
2765 }
2766
2767 // get the second variable
2768 *arg = skipwhite(p + len);
2769 if (compile_expr5(arg, cctx) == FAIL)
2770 return FAIL;
2771
2772 generate_COMPARE(cctx, type, ic);
2773 }
2774
2775 return OK;
2776}
2777
2778/*
2779 * Compile || or &&.
2780 */
2781 static int
2782compile_and_or(char_u **arg, cctx_T *cctx, char *op)
2783{
2784 char_u *p = skipwhite(*arg);
2785 int opchar = *op;
2786
2787 if (p[0] == opchar && p[1] == opchar)
2788 {
2789 garray_T *instr = &cctx->ctx_instr;
2790 garray_T end_ga;
2791
2792 /*
2793 * Repeat until there is no following "||" or "&&"
2794 */
2795 ga_init2(&end_ga, sizeof(int), 10);
2796 while (p[0] == opchar && p[1] == opchar)
2797 {
2798 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
2799 semsg(_(e_white_both), op);
2800
2801 if (ga_grow(&end_ga, 1) == FAIL)
2802 {
2803 ga_clear(&end_ga);
2804 return FAIL;
2805 }
2806 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2807 ++end_ga.ga_len;
2808 generate_JUMP(cctx, opchar == '|'
2809 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
2810
2811 // eval the next expression
2812 *arg = skipwhite(p + 2);
2813 if ((opchar == '|' ? compile_expr3(arg, cctx)
2814 : compile_expr4(arg, cctx)) == FAIL)
2815 {
2816 ga_clear(&end_ga);
2817 return FAIL;
2818 }
2819 p = skipwhite(*arg);
2820 }
2821
2822 // Fill in the end label in all jumps.
2823 while (end_ga.ga_len > 0)
2824 {
2825 isn_T *isn;
2826
2827 --end_ga.ga_len;
2828 isn = ((isn_T *)instr->ga_data)
2829 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
2830 isn->isn_arg.jump.jump_where = instr->ga_len;
2831 }
2832 ga_clear(&end_ga);
2833 }
2834
2835 return OK;
2836}
2837
2838/*
2839 * expr4a && expr4a && expr4a logical AND
2840 *
2841 * Produces instructions:
2842 * EVAL expr4a Push result of "expr4a"
2843 * JUMP_AND_KEEP_IF_FALSE end
2844 * EVAL expr4b Push result of "expr4b"
2845 * JUMP_AND_KEEP_IF_FALSE end
2846 * EVAL expr4c Push result of "expr4c"
2847 * end:
2848 */
2849 static int
2850compile_expr3(char_u **arg, cctx_T *cctx)
2851{
2852 // get the first variable
2853 if (compile_expr4(arg, cctx) == FAIL)
2854 return FAIL;
2855
2856 // || and && work almost the same
2857 return compile_and_or(arg, cctx, "&&");
2858}
2859
2860/*
2861 * expr3a || expr3b || expr3c logical OR
2862 *
2863 * Produces instructions:
2864 * EVAL expr3a Push result of "expr3a"
2865 * JUMP_AND_KEEP_IF_TRUE end
2866 * EVAL expr3b Push result of "expr3b"
2867 * JUMP_AND_KEEP_IF_TRUE end
2868 * EVAL expr3c Push result of "expr3c"
2869 * end:
2870 */
2871 static int
2872compile_expr2(char_u **arg, cctx_T *cctx)
2873{
2874 // eval the first expression
2875 if (compile_expr3(arg, cctx) == FAIL)
2876 return FAIL;
2877
2878 // || and && work almost the same
2879 return compile_and_or(arg, cctx, "||");
2880}
2881
2882/*
2883 * Toplevel expression: expr2 ? expr1a : expr1b
2884 *
2885 * Produces instructions:
2886 * EVAL expr2 Push result of "expr"
2887 * JUMP_IF_FALSE alt jump if false
2888 * EVAL expr1a
2889 * JUMP_ALWAYS end
2890 * alt: EVAL expr1b
2891 * end:
2892 */
2893 static int
2894compile_expr1(char_u **arg, cctx_T *cctx)
2895{
2896 char_u *p;
2897
2898 // evaluate the first expression
2899 if (compile_expr2(arg, cctx) == FAIL)
2900 return FAIL;
2901
2902 p = skipwhite(*arg);
2903 if (*p == '?')
2904 {
2905 garray_T *instr = &cctx->ctx_instr;
2906 garray_T *stack = &cctx->ctx_type_stack;
2907 int alt_idx = instr->ga_len;
2908 int end_idx;
2909 isn_T *isn;
2910 type_T *type1;
2911 type_T *type2;
2912
2913 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
2914 semsg(_(e_white_both), "?");
2915
2916 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
2917
2918 // evaluate the second expression; any type is accepted
2919 *arg = skipwhite(p + 1);
2920 compile_expr1(arg, cctx);
2921
2922 // remember the type and drop it
2923 --stack->ga_len;
2924 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
2925
2926 end_idx = instr->ga_len;
2927 generate_JUMP(cctx, JUMP_ALWAYS, 0);
2928
2929 // jump here from JUMP_IF_FALSE
2930 isn = ((isn_T *)instr->ga_data) + alt_idx;
2931 isn->isn_arg.jump.jump_where = instr->ga_len;
2932
2933 // Check for the ":".
2934 p = skipwhite(*arg);
2935 if (*p != ':')
2936 {
2937 emsg(_(e_missing_colon));
2938 return FAIL;
2939 }
2940 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
2941 semsg(_(e_white_both), ":");
2942
2943 // evaluate the third expression
2944 *arg = skipwhite(p + 1);
2945 compile_expr1(arg, cctx);
2946
2947 // If the types differ, the result has a more generic type.
2948 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2949 common_type(type1, type2, type2);
2950
2951 // jump here from JUMP_ALWAYS
2952 isn = ((isn_T *)instr->ga_data) + end_idx;
2953 isn->isn_arg.jump.jump_where = instr->ga_len;
2954 }
2955 return OK;
2956}
2957
2958/*
2959 * compile "return [expr]"
2960 */
2961 static char_u *
2962compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
2963{
2964 char_u *p = arg;
2965 garray_T *stack = &cctx->ctx_type_stack;
2966 type_T *stack_type;
2967
2968 if (*p != NUL && *p != '|' && *p != '\n')
2969 {
2970 // compile return argument into instructions
2971 if (compile_expr1(&p, cctx) == FAIL)
2972 return NULL;
2973
2974 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2975 if (set_return_type)
2976 cctx->ctx_ufunc->uf_ret_type = stack_type;
2977 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
2978 == FAIL)
2979 return NULL;
2980 }
2981 else
2982 {
2983 if (set_return_type)
2984 cctx->ctx_ufunc->uf_ret_type = &t_void;
2985 else if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID)
2986 {
2987 emsg(_("E1003: Missing return value"));
2988 return NULL;
2989 }
2990
2991 // No argument, return zero.
2992 generate_PUSHNR(cctx, 0);
2993 }
2994
2995 if (generate_instr(cctx, ISN_RETURN) == NULL)
2996 return NULL;
2997
2998 // "return val | endif" is possible
2999 return skipwhite(p);
3000}
3001
3002/*
3003 * Return the length of an assignment operator, or zero if there isn't one.
3004 */
3005 int
3006assignment_len(char_u *p, int *heredoc)
3007{
3008 if (*p == '=')
3009 {
3010 if (p[1] == '<' && p[2] == '<')
3011 {
3012 *heredoc = TRUE;
3013 return 3;
3014 }
3015 return 1;
3016 }
3017 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3018 return 2;
3019 if (STRNCMP(p, "..=", 3) == 0)
3020 return 3;
3021 return 0;
3022}
3023
3024// words that cannot be used as a variable
3025static char *reserved[] = {
3026 "true",
3027 "false",
3028 NULL
3029};
3030
3031/*
3032 * Get a line for "=<<".
3033 * Return a pointer to the line in allocated memory.
3034 * Return NULL for end-of-file or some error.
3035 */
3036 static char_u *
3037heredoc_getline(
3038 int c UNUSED,
3039 void *cookie,
3040 int indent UNUSED,
3041 int do_concat UNUSED)
3042{
3043 cctx_T *cctx = (cctx_T *)cookie;
3044
3045 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
3046 NULL;
3047 ++cctx->ctx_lnum;
3048 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3049 [cctx->ctx_lnum]);
3050}
3051
3052/*
3053 * compile "let var [= expr]", "const var = expr" and "var = expr"
3054 * "arg" points to "var".
3055 */
3056 static char_u *
3057compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3058{
3059 char_u *p;
3060 char_u *ret = NULL;
3061 int var_count = 0;
3062 int semicolon = 0;
3063 size_t varlen;
3064 garray_T *instr = &cctx->ctx_instr;
3065 int idx = -1;
3066 char_u *op;
3067 int option = FALSE;
3068 int opt_type;
3069 int opt_flags = 0;
3070 int global = FALSE;
3071 int script = FALSE;
3072 int oplen = 0;
3073 int heredoc = FALSE;
3074 type_T *type;
3075 lvar_T *lvar;
3076 char_u *name;
3077 char_u *sp;
3078 int has_type = FALSE;
3079 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3080 int instr_count = -1;
3081
3082 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3083 if (p == NULL)
3084 return NULL;
3085 if (var_count > 0)
3086 {
3087 // TODO: let [var, var] = list
3088 emsg("Cannot handle a list yet");
3089 return NULL;
3090 }
3091
3092 varlen = p - arg;
3093 name = vim_strnsave(arg, (int)varlen);
3094 if (name == NULL)
3095 return NULL;
3096
3097 if (*arg == '&')
3098 {
3099 int cc;
3100 long numval;
3101 char_u *stringval = NULL;
3102
3103 option = TRUE;
3104 if (cmdidx == CMD_const)
3105 {
3106 emsg(_(e_const_option));
3107 return NULL;
3108 }
3109 if (is_decl)
3110 {
3111 semsg(_("E1052: Cannot declare an option: %s"), arg);
3112 goto theend;
3113 }
3114 p = arg;
3115 p = find_option_end(&p, &opt_flags);
3116 if (p == NULL)
3117 {
3118 emsg(_(e_letunexp));
3119 return NULL;
3120 }
3121 cc = *p;
3122 *p = NUL;
3123 opt_type = get_option_value(arg + 1, &numval, &stringval, opt_flags);
3124 *p = cc;
3125 if (opt_type == -3)
3126 {
3127 semsg(_(e_unknown_option), *arg);
3128 return NULL;
3129 }
3130 if (opt_type == -2 || opt_type == 0)
3131 type = &t_string;
3132 else
3133 type = &t_number; // both number and boolean option
3134 }
3135 else if (STRNCMP(arg, "g:", 2) == 0)
3136 {
3137 global = TRUE;
3138 if (is_decl)
3139 {
3140 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3141 goto theend;
3142 }
3143 }
3144 else
3145 {
3146 for (idx = 0; reserved[idx] != NULL; ++idx)
3147 if (STRCMP(reserved[idx], name) == 0)
3148 {
3149 semsg(_("E1034: Cannot use reserved name %s"), name);
3150 goto theend;
3151 }
3152
3153 idx = lookup_local(arg, varlen, cctx);
3154 if (idx >= 0)
3155 {
3156 if (is_decl)
3157 {
3158 semsg(_("E1017: Variable already declared: %s"), name);
3159 goto theend;
3160 }
3161 else
3162 {
3163 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3164 if (lvar->lv_const)
3165 {
3166 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3167 goto theend;
3168 }
3169 }
3170 }
3171 else if (lookup_script(arg, varlen) == OK)
3172 {
3173 script = TRUE;
3174 if (is_decl)
3175 {
3176 semsg(_("E1054: Variable already declared in the script: %s"),
3177 name);
3178 goto theend;
3179 }
3180 }
3181 }
3182
3183 if (!option)
3184 {
3185 if (is_decl && *p == ':')
3186 {
3187 // parse optional type: "let var: type = expr"
3188 p = skipwhite(p + 1);
3189 type = parse_type(&p, cctx->ctx_type_list);
3190 if (type == NULL)
3191 goto theend;
3192 has_type = TRUE;
3193 }
3194 else if (idx < 0)
3195 {
3196 // global and new local default to "any" type
3197 type = &t_any;
3198 }
3199 else
3200 {
3201 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3202 type = lvar->lv_type;
3203 }
3204 }
3205
3206 sp = p;
3207 p = skipwhite(p);
3208 op = p;
3209 oplen = assignment_len(p, &heredoc);
3210 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3211 {
3212 char_u buf[4];
3213
3214 vim_strncpy(buf, op, oplen);
3215 semsg(_(e_white_both), buf);
3216 }
3217
3218 if (oplen == 3 && !heredoc && !global && type->tt_type != VAR_STRING
3219 && type->tt_type != VAR_UNKNOWN)
3220 {
3221 emsg("E1019: Can only concatenate to string");
3222 goto theend;
3223 }
3224
3225 // +=, /=, etc. require an existing variable
3226 if (idx < 0 && !global && !option)
3227 {
3228 if (oplen > 1 && !heredoc)
3229 {
3230 semsg(_("E1020: cannot use an operator on a new variable: %s"),
3231 name);
3232 goto theend;
3233 }
3234
3235 // new local variable
3236 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
3237 if (idx < 0)
3238 goto theend;
3239 }
3240
3241 if (heredoc)
3242 {
3243 list_T *l;
3244 listitem_T *li;
3245
3246 // [let] varname =<< [trim] {end}
3247 eap->getline = heredoc_getline;
3248 eap->cookie = cctx;
3249 l = heredoc_get(eap, op + 3);
3250
3251 // Push each line and the create the list.
3252 for (li = l->lv_first; li != NULL; li = li->li_next)
3253 {
3254 generate_PUSHS(cctx, li->li_tv.vval.v_string);
3255 li->li_tv.vval.v_string = NULL;
3256 }
3257 generate_NEWLIST(cctx, l->lv_len);
3258 type = &t_list_string;
3259 list_free(l);
3260 p += STRLEN(p);
3261 }
3262 else if (oplen > 0)
3263 {
3264 // for "+=", "*=", "..=" etc. first load the current value
3265 if (*op != '=')
3266 {
3267 if (option)
3268 generate_LOAD(cctx, ISN_LOADOPT, 0, name + 1, type);
3269 else if (global)
3270 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
3271 else
3272 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
3273 }
3274
3275 // compile the expression
3276 instr_count = instr->ga_len;
3277 p = skipwhite(p + oplen);
3278 if (compile_expr1(&p, cctx) == FAIL)
3279 goto theend;
3280
3281 if (idx >= 0 && (is_decl || !has_type))
3282 {
3283 garray_T *stack = &cctx->ctx_type_stack;
3284 type_T *stacktype =
3285 ((type_T **)stack->ga_data)[stack->ga_len - 1];
3286
3287 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3288 if (!has_type)
3289 {
3290 if (stacktype->tt_type == VAR_VOID)
3291 {
3292 emsg(_("E1031: Cannot use void value"));
3293 goto theend;
3294 }
3295 else
3296 lvar->lv_type = stacktype;
3297 }
3298 else
3299 if (check_type(lvar->lv_type, stacktype, TRUE) == FAIL)
3300 goto theend;
3301 }
3302 }
3303 else if (cmdidx == CMD_const)
3304 {
3305 emsg(_("E1021: const requires a value"));
3306 goto theend;
3307 }
3308 else if (!has_type || option)
3309 {
3310 emsg(_("E1022: type or initialization required"));
3311 goto theend;
3312 }
3313 else
3314 {
3315 // variables are always initialized
3316 // TODO: support more types
3317 if (ga_grow(instr, 1) == FAIL)
3318 goto theend;
3319 if (type->tt_type == VAR_STRING)
3320 generate_PUSHS(cctx, vim_strsave((char_u *)""));
3321 else
3322 generate_PUSHNR(cctx, 0);
3323 }
3324
3325 if (oplen > 0 && *op != '=')
3326 {
3327 type_T *expected = &t_number;
3328 garray_T *stack = &cctx->ctx_type_stack;
3329 type_T *stacktype;
3330
3331 // TODO: if type is known use float or any operation
3332
3333 if (*op == '.')
3334 expected = &t_string;
3335 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3336 if (need_type(stacktype, expected, -1, cctx) == FAIL)
3337 goto theend;
3338
3339 if (*op == '.')
3340 generate_instr_drop(cctx, ISN_CONCAT, 1);
3341 else
3342 {
3343 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3344
3345 if (isn == NULL)
3346 goto theend;
3347 switch (*op)
3348 {
3349 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
3350 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
3351 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
3352 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
3353 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
3354 }
3355 }
3356 }
3357
3358 if (option)
3359 generate_STOREOPT(cctx, name + 1, opt_flags);
3360 else if (global)
3361 generate_STORE(cctx, ISN_STOREG, 0, name + 2);
3362 else if (script)
3363 {
3364 idx = get_script_item_idx(current_sctx.sc_sid, name, TRUE);
3365 // TODO: specific type
3366 generate_SCRIPT(cctx, ISN_STORESCRIPT,
3367 current_sctx.sc_sid, idx, &t_any);
3368 }
3369 else
3370 {
3371 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3372
3373 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
3374 // ISN_STORENR
3375 if (instr->ga_len == instr_count + 1 && isn->isn_type == ISN_PUSHNR)
3376 {
3377 varnumber_T val = isn->isn_arg.number;
3378 garray_T *stack = &cctx->ctx_type_stack;
3379
3380 isn->isn_type = ISN_STORENR;
3381 isn->isn_arg.storenr.str_idx = idx;
3382 isn->isn_arg.storenr.str_val = val;
3383 if (stack->ga_len > 0)
3384 --stack->ga_len;
3385 }
3386 else
3387 generate_STORE(cctx, ISN_STORE, idx, NULL);
3388 }
3389 ret = p;
3390
3391theend:
3392 vim_free(name);
3393 return ret;
3394}
3395
3396/*
3397 * Compile an :import command.
3398 */
3399 static char_u *
3400compile_import(char_u *arg, cctx_T *cctx)
3401{
3402 return handle_import(arg, &cctx->ctx_imports, 0);
3403}
3404
3405/*
3406 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
3407 */
3408 static int
3409compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
3410{
3411 garray_T *instr = &cctx->ctx_instr;
3412 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
3413
3414 if (endlabel == NULL)
3415 return FAIL;
3416 endlabel->el_next = *el;
3417 *el = endlabel;
3418 endlabel->el_end_label = instr->ga_len;
3419
3420 generate_JUMP(cctx, when, 0);
3421 return OK;
3422}
3423
3424 static void
3425compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
3426{
3427 garray_T *instr = &cctx->ctx_instr;
3428
3429 while (*el != NULL)
3430 {
3431 endlabel_T *cur = (*el);
3432 isn_T *isn;
3433
3434 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
3435 isn->isn_arg.jump.jump_where = instr->ga_len;
3436 *el = cur->el_next;
3437 vim_free(cur);
3438 }
3439}
3440
3441/*
3442 * Create a new scope and set up the generic items.
3443 */
3444 static scope_T *
3445new_scope(cctx_T *cctx, scopetype_T type)
3446{
3447 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
3448
3449 if (scope == NULL)
3450 return NULL;
3451 scope->se_outer = cctx->ctx_scope;
3452 cctx->ctx_scope = scope;
3453 scope->se_type = type;
3454 scope->se_local_count = cctx->ctx_locals.ga_len;
3455 return scope;
3456}
3457
3458/*
3459 * compile "if expr"
3460 *
3461 * "if expr" Produces instructions:
3462 * EVAL expr Push result of "expr"
3463 * JUMP_IF_FALSE end
3464 * ... body ...
3465 * end:
3466 *
3467 * "if expr | else" Produces instructions:
3468 * EVAL expr Push result of "expr"
3469 * JUMP_IF_FALSE else
3470 * ... body ...
3471 * JUMP_ALWAYS end
3472 * else:
3473 * ... body ...
3474 * end:
3475 *
3476 * "if expr1 | elseif expr2 | else" Produces instructions:
3477 * EVAL expr Push result of "expr"
3478 * JUMP_IF_FALSE elseif
3479 * ... body ...
3480 * JUMP_ALWAYS end
3481 * elseif:
3482 * EVAL expr Push result of "expr"
3483 * JUMP_IF_FALSE else
3484 * ... body ...
3485 * JUMP_ALWAYS end
3486 * else:
3487 * ... body ...
3488 * end:
3489 */
3490 static char_u *
3491compile_if(char_u *arg, cctx_T *cctx)
3492{
3493 char_u *p = arg;
3494 garray_T *instr = &cctx->ctx_instr;
3495 scope_T *scope;
3496
3497 // compile "expr"
3498 if (compile_expr1(&p, cctx) == FAIL)
3499 return NULL;
3500
3501 scope = new_scope(cctx, IF_SCOPE);
3502 if (scope == NULL)
3503 return NULL;
3504
3505 // "where" is set when ":elseif", "else" or ":endif" is found
3506 scope->se_if.is_if_label = instr->ga_len;
3507 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3508
3509 return p;
3510}
3511
3512 static char_u *
3513compile_elseif(char_u *arg, cctx_T *cctx)
3514{
3515 char_u *p = arg;
3516 garray_T *instr = &cctx->ctx_instr;
3517 isn_T *isn;
3518 scope_T *scope = cctx->ctx_scope;
3519
3520 if (scope == NULL || scope->se_type != IF_SCOPE)
3521 {
3522 emsg(_(e_elseif_without_if));
3523 return NULL;
3524 }
3525 cctx->ctx_locals.ga_len = scope->se_local_count;
3526
3527 // jump from previous block to the end
3528 if (compile_jump_to_end(&scope->se_if.is_end_label,
3529 JUMP_ALWAYS, cctx) == FAIL)
3530 return NULL;
3531
3532 // previous "if" or "elseif" jumps here
3533 isn = ((isn_T *)instr->ga_data) + scope->se_if.is_if_label;
3534 isn->isn_arg.jump.jump_where = instr->ga_len;
3535
3536 // compile "expr"
3537 if (compile_expr1(&p, cctx) == FAIL)
3538 return NULL;
3539
3540 // "where" is set when ":elseif", "else" or ":endif" is found
3541 scope->se_if.is_if_label = instr->ga_len;
3542 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3543
3544 return p;
3545}
3546
3547 static char_u *
3548compile_else(char_u *arg, cctx_T *cctx)
3549{
3550 char_u *p = arg;
3551 garray_T *instr = &cctx->ctx_instr;
3552 isn_T *isn;
3553 scope_T *scope = cctx->ctx_scope;
3554
3555 if (scope == NULL || scope->se_type != IF_SCOPE)
3556 {
3557 emsg(_(e_else_without_if));
3558 return NULL;
3559 }
3560 cctx->ctx_locals.ga_len = scope->se_local_count;
3561
3562 // jump from previous block to the end
3563 if (compile_jump_to_end(&scope->se_if.is_end_label,
3564 JUMP_ALWAYS, cctx) == FAIL)
3565 return NULL;
3566
3567 // previous "if" or "elseif" jumps here
3568 isn = ((isn_T *)instr->ga_data) + scope->se_if.is_if_label;
3569 isn->isn_arg.jump.jump_where = instr->ga_len;
3570
3571 return p;
3572}
3573
3574 static char_u *
3575compile_endif(char_u *arg, cctx_T *cctx)
3576{
3577 scope_T *scope = cctx->ctx_scope;
3578 ifscope_T *ifscope;
3579 garray_T *instr = &cctx->ctx_instr;
3580 isn_T *isn;
3581
3582 if (scope == NULL || scope->se_type != IF_SCOPE)
3583 {
3584 emsg(_(e_endif_without_if));
3585 return NULL;
3586 }
3587 ifscope = &scope->se_if;
3588 cctx->ctx_scope = scope->se_outer;
3589 cctx->ctx_locals.ga_len = scope->se_local_count;
3590
3591 // previous "if" or "elseif" jumps here
3592 isn = ((isn_T *)instr->ga_data) + scope->se_if.is_if_label;
3593 isn->isn_arg.jump.jump_where = instr->ga_len;
3594
3595 // Fill in the "end" label in jumps at the end of the blocks.
3596 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
3597
3598 vim_free(scope);
3599 return arg;
3600}
3601
3602/*
3603 * compile "for var in expr"
3604 *
3605 * Produces instructions:
3606 * PUSHNR -1
3607 * STORE loop-idx Set index to -1
3608 * EVAL expr Push result of "expr"
3609 * top: FOR loop-idx, end Increment index, use list on bottom of stack
3610 * - if beyond end, jump to "end"
3611 * - otherwise get item from list and push it
3612 * STORE var Store item in "var"
3613 * ... body ...
3614 * JUMP top Jump back to repeat
3615 * end: DROP Drop the result of "expr"
3616 *
3617 */
3618 static char_u *
3619compile_for(char_u *arg, cctx_T *cctx)
3620{
3621 char_u *p;
3622 size_t varlen;
3623 garray_T *instr = &cctx->ctx_instr;
3624 garray_T *stack = &cctx->ctx_type_stack;
3625 scope_T *scope;
3626 int loop_idx; // index of loop iteration variable
3627 int var_idx; // index of "var"
3628 type_T *vartype;
3629
3630 // TODO: list of variables: "for [key, value] in dict"
3631 // parse "var"
3632 for (p = arg; eval_isnamec1(*p); ++p)
3633 ;
3634 varlen = p - arg;
3635 var_idx = lookup_local(arg, varlen, cctx);
3636 if (var_idx >= 0)
3637 {
3638 semsg(_("E1023: variable already defined: %s"), arg);
3639 return NULL;
3640 }
3641
3642 // consume "in"
3643 p = skipwhite(p);
3644 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
3645 {
3646 emsg(_(e_missing_in));
3647 return NULL;
3648 }
3649 p = skipwhite(p + 2);
3650
3651
3652 scope = new_scope(cctx, FOR_SCOPE);
3653 if (scope == NULL)
3654 return NULL;
3655
3656 // Reserve a variable to store the loop iteration counter.
3657 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
3658 if (loop_idx < 0)
3659 return NULL;
3660
3661 // Reserve a variable to store "var"
3662 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
3663 if (var_idx < 0)
3664 return NULL;
3665
3666 generate_STORENR(cctx, loop_idx, -1);
3667
3668 // compile "expr", it remains on the stack until "endfor"
3669 arg = p;
3670 if (compile_expr1(&arg, cctx) == FAIL)
3671 return NULL;
3672
3673 // now we know the type of "var"
3674 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3675 if (vartype->tt_type != VAR_LIST)
3676 {
3677 emsg(_("E1024: need a List to iterate over"));
3678 return NULL;
3679 }
3680 if (vartype->tt_member->tt_type != VAR_UNKNOWN)
3681 {
3682 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
3683
3684 lvar->lv_type = vartype->tt_member;
3685 }
3686
3687 // "for_end" is set when ":endfor" is found
3688 scope->se_for.fs_top_label = instr->ga_len;
3689
3690 generate_FOR(cctx, loop_idx);
3691 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
3692
3693 return arg;
3694}
3695
3696/*
3697 * compile "endfor"
3698 */
3699 static char_u *
3700compile_endfor(char_u *arg, cctx_T *cctx)
3701{
3702 garray_T *instr = &cctx->ctx_instr;
3703 scope_T *scope = cctx->ctx_scope;
3704 forscope_T *forscope;
3705 isn_T *isn;
3706
3707 if (scope == NULL || scope->se_type != FOR_SCOPE)
3708 {
3709 emsg(_(e_for));
3710 return NULL;
3711 }
3712 forscope = &scope->se_for;
3713 cctx->ctx_scope = scope->se_outer;
3714 cctx->ctx_locals.ga_len = scope->se_local_count;
3715
3716 // At end of ":for" scope jump back to the FOR instruction.
3717 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
3718
3719 // Fill in the "end" label in the FOR statement so it can jump here
3720 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
3721 isn->isn_arg.forloop.for_end = instr->ga_len;
3722
3723 // Fill in the "end" label any BREAK statements
3724 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
3725
3726 // Below the ":for" scope drop the "expr" list from the stack.
3727 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
3728 return NULL;
3729
3730 vim_free(scope);
3731
3732 return arg;
3733}
3734
3735/*
3736 * compile "while expr"
3737 *
3738 * Produces instructions:
3739 * top: EVAL expr Push result of "expr"
3740 * JUMP_IF_FALSE end jump if false
3741 * ... body ...
3742 * JUMP top Jump back to repeat
3743 * end:
3744 *
3745 */
3746 static char_u *
3747compile_while(char_u *arg, cctx_T *cctx)
3748{
3749 char_u *p = arg;
3750 garray_T *instr = &cctx->ctx_instr;
3751 scope_T *scope;
3752
3753 scope = new_scope(cctx, WHILE_SCOPE);
3754 if (scope == NULL)
3755 return NULL;
3756
3757 scope->se_while.ws_top_label = instr->ga_len;
3758
3759 // compile "expr"
3760 if (compile_expr1(&p, cctx) == FAIL)
3761 return NULL;
3762
3763 // "while_end" is set when ":endwhile" is found
3764 if (compile_jump_to_end(&scope->se_while.ws_end_label,
3765 JUMP_IF_FALSE, cctx) == FAIL)
3766 return FAIL;
3767
3768 return p;
3769}
3770
3771/*
3772 * compile "endwhile"
3773 */
3774 static char_u *
3775compile_endwhile(char_u *arg, cctx_T *cctx)
3776{
3777 scope_T *scope = cctx->ctx_scope;
3778
3779 if (scope == NULL || scope->se_type != WHILE_SCOPE)
3780 {
3781 emsg(_(e_while));
3782 return NULL;
3783 }
3784 cctx->ctx_scope = scope->se_outer;
3785 cctx->ctx_locals.ga_len = scope->se_local_count;
3786
3787 // At end of ":for" scope jump back to the FOR instruction.
3788 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_while.ws_top_label);
3789
3790 // Fill in the "end" label in the WHILE statement so it can jump here.
3791 // And in any jumps for ":break"
3792 compile_fill_jump_to_end(&scope->se_while.ws_end_label, cctx);
3793
3794 vim_free(scope);
3795
3796 return arg;
3797}
3798
3799/*
3800 * compile "continue"
3801 */
3802 static char_u *
3803compile_continue(char_u *arg, cctx_T *cctx)
3804{
3805 scope_T *scope = cctx->ctx_scope;
3806
3807 for (;;)
3808 {
3809 if (scope == NULL)
3810 {
3811 emsg(_(e_continue));
3812 return NULL;
3813 }
3814 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
3815 break;
3816 scope = scope->se_outer;
3817 }
3818
3819 // Jump back to the FOR or WHILE instruction.
3820 generate_JUMP(cctx, JUMP_ALWAYS,
3821 scope->se_type == FOR_SCOPE ? scope->se_for.fs_top_label
3822 : scope->se_while.ws_top_label);
3823 return arg;
3824}
3825
3826/*
3827 * compile "break"
3828 */
3829 static char_u *
3830compile_break(char_u *arg, cctx_T *cctx)
3831{
3832 scope_T *scope = cctx->ctx_scope;
3833 endlabel_T **el;
3834
3835 for (;;)
3836 {
3837 if (scope == NULL)
3838 {
3839 emsg(_(e_break));
3840 return NULL;
3841 }
3842 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
3843 break;
3844 scope = scope->se_outer;
3845 }
3846
3847 // Jump to the end of the FOR or WHILE loop.
3848 if (scope->se_type == FOR_SCOPE)
3849 el = &scope->se_for.fs_end_label;
3850 else
3851 el = &scope->se_while.ws_end_label;
3852 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
3853 return FAIL;
3854
3855 return arg;
3856}
3857
3858/*
3859 * compile "{" start of block
3860 */
3861 static char_u *
3862compile_block(char_u *arg, cctx_T *cctx)
3863{
3864 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
3865 return NULL;
3866 return skipwhite(arg + 1);
3867}
3868
3869/*
3870 * compile end of block: drop one scope
3871 */
3872 static void
3873compile_endblock(cctx_T *cctx)
3874{
3875 scope_T *scope = cctx->ctx_scope;
3876
3877 cctx->ctx_scope = scope->se_outer;
3878 cctx->ctx_locals.ga_len = scope->se_local_count;
3879 vim_free(scope);
3880}
3881
3882/*
3883 * compile "try"
3884 * Creates a new scope for the try-endtry, pointing to the first catch and
3885 * finally.
3886 * Creates another scope for the "try" block itself.
3887 * TRY instruction sets up exception handling at runtime.
3888 *
3889 * "try"
3890 * TRY -> catch1, -> finally push trystack entry
3891 * ... try block
3892 * "throw {exception}"
3893 * EVAL {exception}
3894 * THROW create exception
3895 * ... try block
3896 * " catch {expr}"
3897 * JUMP -> finally
3898 * catch1: PUSH exeception
3899 * EVAL {expr}
3900 * MATCH
3901 * JUMP nomatch -> catch2
3902 * CATCH remove exception
3903 * ... catch block
3904 * " catch"
3905 * JUMP -> finally
3906 * catch2: CATCH remove exception
3907 * ... catch block
3908 * " finally"
3909 * finally:
3910 * ... finally block
3911 * " endtry"
3912 * ENDTRY pop trystack entry, may rethrow
3913 */
3914 static char_u *
3915compile_try(char_u *arg, cctx_T *cctx)
3916{
3917 garray_T *instr = &cctx->ctx_instr;
3918 scope_T *try_scope;
3919 scope_T *scope;
3920
3921 // scope that holds the jumps that go to catch/finally/endtry
3922 try_scope = new_scope(cctx, TRY_SCOPE);
3923 if (try_scope == NULL)
3924 return NULL;
3925
3926 // "catch" is set when the first ":catch" is found.
3927 // "finally" is set when ":finally" or ":endtry" is found
3928 try_scope->se_try.ts_try_label = instr->ga_len;
3929 if (generate_instr(cctx, ISN_TRY) == NULL)
3930 return NULL;
3931
3932 // scope for the try block itself
3933 scope = new_scope(cctx, BLOCK_SCOPE);
3934 if (scope == NULL)
3935 return NULL;
3936
3937 return arg;
3938}
3939
3940/*
3941 * compile "catch {expr}"
3942 */
3943 static char_u *
3944compile_catch(char_u *arg, cctx_T *cctx UNUSED)
3945{
3946 scope_T *scope = cctx->ctx_scope;
3947 garray_T *instr = &cctx->ctx_instr;
3948 char_u *p;
3949 isn_T *isn;
3950
3951 // end block scope from :try or :catch
3952 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
3953 compile_endblock(cctx);
3954 scope = cctx->ctx_scope;
3955
3956 // Error if not in a :try scope
3957 if (scope == NULL || scope->se_type != TRY_SCOPE)
3958 {
3959 emsg(_(e_catch));
3960 return NULL;
3961 }
3962
3963 if (scope->se_try.ts_caught_all)
3964 {
3965 emsg(_("E1033: catch unreachable after catch-all"));
3966 return NULL;
3967 }
3968
3969 // Jump from end of previous block to :finally or :endtry
3970 if (compile_jump_to_end(&scope->se_try.ts_end_label,
3971 JUMP_ALWAYS, cctx) == FAIL)
3972 return NULL;
3973
3974 // End :try or :catch scope: set value in ISN_TRY instruction
3975 isn = ((isn_T *)instr->ga_data) + scope->se_try.ts_try_label;
3976 if (isn->isn_arg.try.try_catch == 0)
3977 isn->isn_arg.try.try_catch = instr->ga_len;
3978 if (scope->se_try.ts_catch_label != 0)
3979 {
3980 // Previous catch without match jumps here
3981 isn = ((isn_T *)instr->ga_data) + scope->se_try.ts_catch_label;
3982 isn->isn_arg.jump.jump_where = instr->ga_len;
3983 }
3984
3985 p = skipwhite(arg);
3986 if (ends_excmd(*p))
3987 {
3988 scope->se_try.ts_caught_all = TRUE;
3989 scope->se_try.ts_catch_label = 0;
3990 }
3991 else
3992 {
3993 // Push v:exception, push {expr} and MATCH
3994 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
3995
3996 if (compile_expr1(&p, cctx) == FAIL)
3997 return NULL;
3998
3999 // TODO: check for strings?
4000 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
4001 return NULL;
4002
4003 scope->se_try.ts_catch_label = instr->ga_len;
4004 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
4005 return NULL;
4006 }
4007
4008 if (generate_instr(cctx, ISN_CATCH) == NULL)
4009 return NULL;
4010
4011 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4012 return NULL;
4013 return p;
4014}
4015
4016 static char_u *
4017compile_finally(char_u *arg, cctx_T *cctx)
4018{
4019 scope_T *scope = cctx->ctx_scope;
4020 garray_T *instr = &cctx->ctx_instr;
4021 isn_T *isn;
4022
4023 // end block scope from :try or :catch
4024 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4025 compile_endblock(cctx);
4026 scope = cctx->ctx_scope;
4027
4028 // Error if not in a :try scope
4029 if (scope == NULL || scope->se_type != TRY_SCOPE)
4030 {
4031 emsg(_(e_finally));
4032 return NULL;
4033 }
4034
4035 // End :catch or :finally scope: set value in ISN_TRY instruction
4036 isn = ((isn_T *)instr->ga_data) + scope->se_try.ts_try_label;
4037 if (isn->isn_arg.try.try_finally != 0)
4038 {
4039 emsg(_(e_finally_dup));
4040 return NULL;
4041 }
4042
4043 // Fill in the "end" label in jumps at the end of the blocks.
4044 compile_fill_jump_to_end(&scope->se_try.ts_end_label, cctx);
4045
4046 if (scope->se_try.ts_catch_label != 0)
4047 {
4048 // Previous catch without match jumps here
4049 isn = ((isn_T *)instr->ga_data) + scope->se_try.ts_catch_label;
4050 isn->isn_arg.jump.jump_where = instr->ga_len;
4051 }
4052
4053 isn->isn_arg.try.try_finally = instr->ga_len;
4054 // TODO: set index in ts_finally_label jumps
4055
4056 return arg;
4057}
4058
4059 static char_u *
4060compile_endtry(char_u *arg, cctx_T *cctx)
4061{
4062 scope_T *scope = cctx->ctx_scope;
4063 garray_T *instr = &cctx->ctx_instr;
4064 isn_T *isn;
4065
4066 // end block scope from :catch or :finally
4067 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4068 compile_endblock(cctx);
4069 scope = cctx->ctx_scope;
4070
4071 // Error if not in a :try scope
4072 if (scope == NULL || scope->se_type != TRY_SCOPE)
4073 {
4074 if (scope == NULL)
4075 emsg(_(e_no_endtry));
4076 else if (scope->se_type == WHILE_SCOPE)
4077 emsg(_(e_endwhile));
4078 if (scope->se_type == FOR_SCOPE)
4079 emsg(_(e_endfor));
4080 else
4081 emsg(_(e_endif));
4082 return NULL;
4083 }
4084
4085 isn = ((isn_T *)instr->ga_data) + scope->se_try.ts_try_label;
4086 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
4087 {
4088 emsg(_("E1032: missing :catch or :finally"));
4089 return NULL;
4090 }
4091
4092 // Fill in the "end" label in jumps at the end of the blocks, if not done
4093 // by ":finally".
4094 compile_fill_jump_to_end(&scope->se_try.ts_end_label, cctx);
4095
4096 // End :catch or :finally scope: set value in ISN_TRY instruction
4097 if (isn->isn_arg.try.try_finally == 0)
4098 isn->isn_arg.try.try_finally = instr->ga_len;
4099 compile_endblock(cctx);
4100
4101 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
4102 return NULL;
4103 return arg;
4104}
4105
4106/*
4107 * compile "throw {expr}"
4108 */
4109 static char_u *
4110compile_throw(char_u *arg, cctx_T *cctx UNUSED)
4111{
4112 char_u *p = skipwhite(arg);
4113
4114 if (ends_excmd(*p))
4115 {
4116 emsg(_(e_argreq));
4117 return NULL;
4118 }
4119 if (compile_expr1(&p, cctx) == FAIL)
4120 return NULL;
4121 if (may_generate_2STRING(-1, cctx) == FAIL)
4122 return NULL;
4123 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
4124 return NULL;
4125
4126 return p;
4127}
4128
4129/*
4130 * compile "echo expr"
4131 */
4132 static char_u *
4133compile_echo(char_u *arg, int with_white, cctx_T *cctx)
4134{
4135 char_u *p = arg;
4136 int count = 0;
4137
4138 // for ()
4139 {
4140 if (compile_expr1(&p, cctx) == FAIL)
4141 return NULL;
4142 ++count;
4143 }
4144
4145 generate_ECHO(cctx, with_white, count);
4146
4147 return p;
4148}
4149
4150/*
4151 * After ex_function() has collected all the function lines: parse and compile
4152 * the lines into instructions.
4153 * Adds the function to "def_functions".
4154 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
4155 * return statement (used for lambda).
4156 */
4157 void
4158compile_def_function(ufunc_T *ufunc, int set_return_type)
4159{
4160 dfunc_T *dfunc;
4161 char_u *line = NULL;
4162 char_u *p;
4163 exarg_T ea;
4164 char *errormsg = NULL; // error message
4165 int had_return = FALSE;
4166 cctx_T cctx;
4167 garray_T *instr;
4168 int called_emsg_before = called_emsg;
4169 int ret = FAIL;
4170 sctx_T save_current_sctx = current_sctx;
4171
4172 if (ufunc->uf_dfunc_idx >= 0)
4173 {
4174 // redefining a function that was compiled before
4175 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4176 dfunc->df_deleted = FALSE;
4177 }
4178 else
4179 {
4180 // Add the function to "def_functions".
4181 if (ga_grow(&def_functions, 1) == FAIL)
4182 return;
4183 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
4184 vim_memset(dfunc, 0, sizeof(dfunc_T));
4185 dfunc->df_idx = def_functions.ga_len;
4186 ufunc->uf_dfunc_idx = dfunc->df_idx;
4187 dfunc->df_ufunc = ufunc;
4188 ++def_functions.ga_len;
4189 }
4190
4191 vim_memset(&cctx, 0, sizeof(cctx));
4192 cctx.ctx_ufunc = ufunc;
4193 cctx.ctx_lnum = -1;
4194 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
4195 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
4196 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
4197 cctx.ctx_type_list = &ufunc->uf_type_list;
4198 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
4199 instr = &cctx.ctx_instr;
4200
4201 // Most modern script version.
4202 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4203
4204 for (;;)
4205 {
4206 if (line != NULL && *line == '|')
4207 // the line continues after a '|'
4208 ++line;
4209 else if (line != NULL && *line != NUL)
4210 {
4211 semsg(_("E488: Trailing characters: %s"), line);
4212 goto erret;
4213 }
4214 else
4215 {
4216 do
4217 {
4218 ++cctx.ctx_lnum;
4219 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
4220 break;
4221 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
4222 } while (line == NULL);
4223 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
4224 break;
4225 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
4226 }
4227
4228 had_return = FALSE;
4229 vim_memset(&ea, 0, sizeof(ea));
4230 ea.cmdlinep = &line;
4231 ea.cmd = skipwhite(line);
4232
4233 // "}" ends a block scope
4234 if (*ea.cmd == '}')
4235 {
4236 scopetype_T stype = cctx.ctx_scope == NULL
4237 ? NO_SCOPE : cctx.ctx_scope->se_type;
4238
4239 if (stype == BLOCK_SCOPE)
4240 {
4241 compile_endblock(&cctx);
4242 line = ea.cmd;
4243 }
4244 else
4245 {
4246 emsg("E1025: using } outside of a block scope");
4247 goto erret;
4248 }
4249 if (line != NULL)
4250 line = skipwhite(ea.cmd + 1);
4251 continue;
4252 }
4253
4254 // "{" starts a block scope
4255 if (*ea.cmd == '{')
4256 {
4257 line = compile_block(ea.cmd, &cctx);
4258 continue;
4259 }
4260
4261 /*
4262 * COMMAND MODIFIERS
4263 */
4264 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
4265 {
4266 if (errormsg != NULL)
4267 goto erret;
4268 // empty line or comment
4269 line = (char_u *)"";
4270 continue;
4271 }
4272
4273 // Skip ":call" to get to the function name.
4274 if (checkforcmd(&ea.cmd, "call", 3))
4275 ea.cmd = skipwhite(ea.cmd);
4276
4277 // Assuming the command starts with a variable or function name, find
4278 // what follows. Also "&opt = value".
4279 p = (*ea.cmd == '&') ? ea.cmd + 1 : ea.cmd;
4280 p = to_name_end(p);
4281 if (p > ea.cmd && *p != NUL)
4282 {
4283 int oplen;
4284 int heredoc;
4285
4286 // "funcname(" is always a function call.
4287 // "varname[]" is an expression.
4288 // "g:varname" is an expression.
4289 // "varname->expr" is an expression.
4290 if (*p == '('
4291 || *p == '['
4292 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
4293 || (*p == '-' && p[1] == '>'))
4294 {
4295 // TODO
4296 }
4297
4298 oplen = assignment_len(skipwhite(p), &heredoc);
4299 if (oplen > 0)
4300 {
4301 // Recognize an assignment if we recognize the variable name:
4302 // "g:var = expr"
4303 // "var = expr" where "var" is a local var name.
4304 // "&opt = expr"
4305 if (*ea.cmd == '&'
4306 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
4307 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
4308 || lookup_script(ea.cmd, p - ea.cmd) == OK)
4309 {
4310 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
4311 if (line == NULL)
4312 goto erret;
4313 continue;
4314 }
4315 }
4316 }
4317
4318 /*
4319 * COMMAND after range
4320 */
4321 ea.cmd = skip_range(ea.cmd, NULL);
4322 p = find_ex_command(&ea, NULL, lookup_local, &cctx);
4323
4324 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
4325 {
4326 // Expression or function call.
4327 if (ea.cmdidx == CMD_eval)
4328 {
4329 p = ea.cmd;
4330 if (compile_expr1(&p, &cctx) == FAIL)
4331 goto erret;
4332
4333 // drop the return value
4334 generate_instr_drop(&cctx, ISN_DROP, 1);
4335 line = p;
4336 continue;
4337 }
4338 if (ea.cmdidx == CMD_let)
4339 {
4340 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
4341 if (line == NULL)
4342 goto erret;
4343 continue;
4344 }
4345 iemsg("Command from find_ex_command() not handled");
4346 goto erret;
4347 }
4348
4349 p = skipwhite(p);
4350
4351 switch (ea.cmdidx)
4352 {
4353 case CMD_def:
4354 case CMD_function:
4355 // TODO: Nested function
4356 emsg("Nested function not implemented yet");
4357 goto erret;
4358
4359 case CMD_return:
4360 line = compile_return(p, set_return_type, &cctx);
4361 had_return = TRUE;
4362 break;
4363
4364 case CMD_let:
4365 case CMD_const:
4366 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
4367 break;
4368
4369 case CMD_import:
4370 line = compile_import(p, &cctx);
4371 break;
4372
4373 case CMD_if:
4374 line = compile_if(p, &cctx);
4375 break;
4376 case CMD_elseif:
4377 line = compile_elseif(p, &cctx);
4378 break;
4379 case CMD_else:
4380 line = compile_else(p, &cctx);
4381 break;
4382 case CMD_endif:
4383 line = compile_endif(p, &cctx);
4384 break;
4385
4386 case CMD_while:
4387 line = compile_while(p, &cctx);
4388 break;
4389 case CMD_endwhile:
4390 line = compile_endwhile(p, &cctx);
4391 break;
4392
4393 case CMD_for:
4394 line = compile_for(p, &cctx);
4395 break;
4396 case CMD_endfor:
4397 line = compile_endfor(p, &cctx);
4398 break;
4399 case CMD_continue:
4400 line = compile_continue(p, &cctx);
4401 break;
4402 case CMD_break:
4403 line = compile_break(p, &cctx);
4404 break;
4405
4406 case CMD_try:
4407 line = compile_try(p, &cctx);
4408 break;
4409 case CMD_catch:
4410 line = compile_catch(p, &cctx);
4411 break;
4412 case CMD_finally:
4413 line = compile_finally(p, &cctx);
4414 break;
4415 case CMD_endtry:
4416 line = compile_endtry(p, &cctx);
4417 break;
4418 case CMD_throw:
4419 line = compile_throw(p, &cctx);
4420 break;
4421
4422 case CMD_echo:
4423 line = compile_echo(p, TRUE, &cctx);
4424 break;
4425 case CMD_echon:
4426 line = compile_echo(p, FALSE, &cctx);
4427 break;
4428
4429 default:
4430 // Not recognized, execute with do_cmdline_cmd().
4431 generate_EXEC(&cctx, line);
4432 line = (char_u *)"";
4433 break;
4434 }
4435 if (line == NULL)
4436 goto erret;
4437
4438 if (cctx.ctx_type_stack.ga_len < 0)
4439 {
4440 iemsg("Type stack underflow");
4441 goto erret;
4442 }
4443 }
4444
4445 if (cctx.ctx_scope != NULL)
4446 {
4447 if (cctx.ctx_scope->se_type == IF_SCOPE)
4448 emsg(_(e_endif));
4449 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
4450 emsg(_(e_endwhile));
4451 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
4452 emsg(_(e_endfor));
4453 else
4454 emsg(_("E1026: Missing }"));
4455 goto erret;
4456 }
4457
4458 if (!had_return)
4459 {
4460 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
4461 {
4462 emsg(_("E1027: Missing return statement"));
4463 goto erret;
4464 }
4465
4466 // Return zero if there is no return at the end.
4467 generate_PUSHNR(&cctx, 0);
4468 generate_instr(&cctx, ISN_RETURN);
4469 }
4470
4471 dfunc->df_instr = instr->ga_data;
4472 dfunc->df_instr_count = instr->ga_len;
4473 dfunc->df_varcount = cctx.ctx_max_local;
4474
4475 ret = OK;
4476
4477erret:
4478 if (ret == FAIL)
4479 {
4480 ga_clear(instr);
4481 ufunc->uf_dfunc_idx = -1;
4482 --def_functions.ga_len;
4483 if (errormsg != NULL)
4484 emsg(errormsg);
4485 else if (called_emsg == called_emsg_before)
4486 emsg("E1028: compile_def_function failed");
4487
4488 // don't execute this function body
4489 ufunc->uf_lines.ga_len = 0;
4490 }
4491
4492 current_sctx = save_current_sctx;
4493 ga_clear(&cctx.ctx_type_stack);
4494 ga_clear(&cctx.ctx_locals);
4495}
4496
4497/*
4498 * Delete an instruction, free what it contains.
4499 */
4500 static void
4501delete_instr(isn_T *isn)
4502{
4503 switch (isn->isn_type)
4504 {
4505 case ISN_EXEC:
4506 case ISN_LOADENV:
4507 case ISN_LOADG:
4508 case ISN_LOADOPT:
4509 case ISN_MEMBER:
4510 case ISN_PUSHEXC:
4511 case ISN_PUSHS:
4512 case ISN_STOREG:
4513 vim_free(isn->isn_arg.string);
4514 break;
4515
4516 case ISN_LOADS:
4517 vim_free(isn->isn_arg.loads.ls_name);
4518 break;
4519
4520 case ISN_STOREOPT:
4521 vim_free(isn->isn_arg.storeopt.so_name);
4522 break;
4523
4524 case ISN_PUSHBLOB: // push blob isn_arg.blob
4525 blob_unref(isn->isn_arg.blob);
4526 break;
4527
4528 case ISN_UCALL:
4529 vim_free(isn->isn_arg.ufunc.cuf_name);
4530 break;
4531
4532 case ISN_2BOOL:
4533 case ISN_2STRING:
4534 case ISN_ADDBLOB:
4535 case ISN_ADDLIST:
4536 case ISN_BCALL:
4537 case ISN_CATCH:
4538 case ISN_CHECKNR:
4539 case ISN_CHECKTYPE:
4540 case ISN_COMPAREANY:
4541 case ISN_COMPAREBLOB:
4542 case ISN_COMPAREBOOL:
4543 case ISN_COMPAREDICT:
4544 case ISN_COMPAREFLOAT:
4545 case ISN_COMPAREFUNC:
4546 case ISN_COMPARELIST:
4547 case ISN_COMPARENR:
4548 case ISN_COMPAREPARTIAL:
4549 case ISN_COMPARESPECIAL:
4550 case ISN_COMPARESTRING:
4551 case ISN_CONCAT:
4552 case ISN_DCALL:
4553 case ISN_DROP:
4554 case ISN_ECHO:
4555 case ISN_ENDTRY:
4556 case ISN_FOR:
4557 case ISN_FUNCREF:
4558 case ISN_INDEX:
4559 case ISN_JUMP:
4560 case ISN_LOAD:
4561 case ISN_LOADSCRIPT:
4562 case ISN_LOADREG:
4563 case ISN_LOADV:
4564 case ISN_NEGATENR:
4565 case ISN_NEWDICT:
4566 case ISN_NEWLIST:
4567 case ISN_OPNR:
4568 case ISN_OPFLOAT:
4569 case ISN_OPANY:
4570 case ISN_PCALL:
4571 case ISN_PUSHF:
4572 case ISN_PUSHNR:
4573 case ISN_PUSHBOOL:
4574 case ISN_PUSHSPEC:
4575 case ISN_RETURN:
4576 case ISN_STORE:
4577 case ISN_STORENR:
4578 case ISN_STORESCRIPT:
4579 case ISN_THROW:
4580 case ISN_TRY:
4581 // nothing allocated
4582 break;
4583 }
4584}
4585
4586/*
4587 * When a user function is deleted, delete any associated def function.
4588 */
4589 void
4590delete_def_function(ufunc_T *ufunc)
4591{
4592 int idx;
4593
4594 if (ufunc->uf_dfunc_idx >= 0)
4595 {
4596 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4597 + ufunc->uf_dfunc_idx;
4598 ga_clear(&dfunc->df_def_args_isn);
4599
4600 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
4601 delete_instr(dfunc->df_instr + idx);
4602 VIM_CLEAR(dfunc->df_instr);
4603
4604 dfunc->df_deleted = TRUE;
4605 }
4606}
4607
4608#if defined(EXITFREE) || defined(PROTO)
4609 void
4610free_def_functions(void)
4611{
4612 vim_free(def_functions.ga_data);
4613}
4614#endif
4615
4616
4617#endif // FEAT_EVAL