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