blob: 69a2d1b1427b6f560eee5d9c8419b98d7cd84b6f [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 == '+')
338 semsg(_("E1035: wrong argument type for +"));
339 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/*
724 * Generate an ISN_LOADS instruction.
725 */
726 static int
727generate_LOADS(
728 cctx_T *cctx,
729 char_u *name,
730 int sid)
731{
732 isn_T *isn;
733
734 if ((isn = generate_instr_type(cctx, ISN_LOADS, &t_any)) == NULL)
735 return FAIL;
736 isn->isn_arg.loads.ls_name = vim_strsave(name);
737 isn->isn_arg.loads.ls_sid = sid;
738
739 return OK;
740}
741
742/*
743 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
744 */
745 static int
746generate_SCRIPT(
747 cctx_T *cctx,
748 isntype_T isn_type,
749 int sid,
750 int idx,
751 type_T *type)
752{
753 isn_T *isn;
754
755 if (isn_type == ISN_LOADSCRIPT)
756 isn = generate_instr_type(cctx, isn_type, type);
757 else
758 isn = generate_instr_drop(cctx, isn_type, 1);
759 if (isn == NULL)
760 return FAIL;
761 isn->isn_arg.script.script_sid = sid;
762 isn->isn_arg.script.script_idx = idx;
763 return OK;
764}
765
766/*
767 * Generate an ISN_NEWLIST instruction.
768 */
769 static int
770generate_NEWLIST(cctx_T *cctx, int count)
771{
772 isn_T *isn;
773 garray_T *stack = &cctx->ctx_type_stack;
774 garray_T *type_list = cctx->ctx_type_list;
775 type_T *type;
776 type_T *member;
777
778 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
779 return FAIL;
780 isn->isn_arg.number = count;
781
782 // drop the value types
783 stack->ga_len -= count;
784
785 // use the first value type for the list member type
786 if (count > 0)
787 member = ((type_T **)stack->ga_data)[stack->ga_len];
788 else
789 member = &t_any;
790 type = get_list_type(member, type_list);
791
792 // add the list type to the type stack
793 if (ga_grow(stack, 1) == FAIL)
794 return FAIL;
795 ((type_T **)stack->ga_data)[stack->ga_len] = type;
796 ++stack->ga_len;
797
798 return OK;
799}
800
801/*
802 * Generate an ISN_NEWDICT instruction.
803 */
804 static int
805generate_NEWDICT(cctx_T *cctx, int count)
806{
807 isn_T *isn;
808 garray_T *stack = &cctx->ctx_type_stack;
809 garray_T *type_list = cctx->ctx_type_list;
810 type_T *type;
811 type_T *member;
812
813 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
814 return FAIL;
815 isn->isn_arg.number = count;
816
817 // drop the key and value types
818 stack->ga_len -= 2 * count;
819
820 // use the first value type for the list member type
821 if (count > 0)
822 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
823 else
824 member = &t_any;
825 type = get_dict_type(member, type_list);
826
827 // add the dict type to the type stack
828 if (ga_grow(stack, 1) == FAIL)
829 return FAIL;
830 ((type_T **)stack->ga_data)[stack->ga_len] = type;
831 ++stack->ga_len;
832
833 return OK;
834}
835
836/*
837 * Generate an ISN_FUNCREF instruction.
838 */
839 static int
840generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
841{
842 isn_T *isn;
843 garray_T *stack = &cctx->ctx_type_stack;
844
845 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
846 return FAIL;
847 isn->isn_arg.number = dfunc_idx;
848
849 if (ga_grow(stack, 1) == FAIL)
850 return FAIL;
851 ((type_T **)stack->ga_data)[stack->ga_len] = &t_partial_any;
852 // TODO: argument and return types
853 ++stack->ga_len;
854
855 return OK;
856}
857
858/*
859 * Generate an ISN_JUMP instruction.
860 */
861 static int
862generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
863{
864 isn_T *isn;
865 garray_T *stack = &cctx->ctx_type_stack;
866
867 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
868 return FAIL;
869 isn->isn_arg.jump.jump_when = when;
870 isn->isn_arg.jump.jump_where = where;
871
872 if (when != JUMP_ALWAYS && stack->ga_len > 0)
873 --stack->ga_len;
874
875 return OK;
876}
877
878 static int
879generate_FOR(cctx_T *cctx, int loop_idx)
880{
881 isn_T *isn;
882 garray_T *stack = &cctx->ctx_type_stack;
883
884 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
885 return FAIL;
886 isn->isn_arg.forloop.for_idx = loop_idx;
887
888 if (ga_grow(stack, 1) == FAIL)
889 return FAIL;
890 // type doesn't matter, will be stored next
891 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
892 ++stack->ga_len;
893
894 return OK;
895}
896
897/*
898 * Generate an ISN_BCALL instruction.
899 * Return FAIL if the number of arguments is wrong.
900 */
901 static int
902generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
903{
904 isn_T *isn;
905 garray_T *stack = &cctx->ctx_type_stack;
906
907 if (check_internal_func(func_idx, argcount) == FAIL)
908 return FAIL;
909
910 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
911 return FAIL;
912 isn->isn_arg.bfunc.cbf_idx = func_idx;
913 isn->isn_arg.bfunc.cbf_argcount = argcount;
914
915 stack->ga_len -= argcount; // drop the arguments
916 if (ga_grow(stack, 1) == FAIL)
917 return FAIL;
918 ((type_T **)stack->ga_data)[stack->ga_len] =
919 internal_func_ret_type(func_idx, argcount);
920 ++stack->ga_len; // add return value
921
922 return OK;
923}
924
925/*
926 * Generate an ISN_DCALL or ISN_UCALL instruction.
927 * Return FAIL if the number of arguments is wrong.
928 */
929 static int
930generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int argcount)
931{
932 isn_T *isn;
933 garray_T *stack = &cctx->ctx_type_stack;
934 int regular_args = ufunc->uf_args.ga_len;
935
936 if (argcount > regular_args && !has_varargs(ufunc))
937 {
938 semsg(_(e_toomanyarg), ufunc->uf_name);
939 return FAIL;
940 }
941 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
942 {
943 semsg(_(e_toofewarg), ufunc->uf_name);
944 return FAIL;
945 }
946
947 // Turn varargs into a list.
948 if (ufunc->uf_va_name != NULL)
949 {
950 int count = argcount - regular_args;
951
952 // TODO: add default values for optional arguments?
953 generate_NEWLIST(cctx, count < 0 ? 0 : count);
954 argcount = regular_args + 1;
955 }
956
957 if ((isn = generate_instr(cctx,
958 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
959 return FAIL;
960 if (ufunc->uf_dfunc_idx >= 0)
961 {
962 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
963 isn->isn_arg.dfunc.cdf_argcount = argcount;
964 }
965 else
966 {
967 // A user function may be deleted and redefined later, can't use the
968 // ufunc pointer, need to look it up again at runtime.
969 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
970 isn->isn_arg.ufunc.cuf_argcount = argcount;
971 }
972
973 stack->ga_len -= argcount; // drop the arguments
974 if (ga_grow(stack, 1) == FAIL)
975 return FAIL;
976 // add return value
977 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
978 ++stack->ga_len;
979
980 return OK;
981}
982
983/*
984 * Generate an ISN_UCALL instruction when the function isn't defined yet.
985 */
986 static int
987generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
988{
989 isn_T *isn;
990 garray_T *stack = &cctx->ctx_type_stack;
991
992 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
993 return FAIL;
994 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
995 isn->isn_arg.ufunc.cuf_argcount = argcount;
996
997 stack->ga_len -= argcount; // drop the arguments
998
999 // drop the funcref/partial, get back the return value
1000 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1001
1002 return OK;
1003}
1004
1005/*
1006 * Generate an ISN_PCALL instruction.
1007 */
1008 static int
1009generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1010{
1011 isn_T *isn;
1012 garray_T *stack = &cctx->ctx_type_stack;
1013
1014 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1015 return FAIL;
1016 isn->isn_arg.pfunc.cpf_top = at_top;
1017 isn->isn_arg.pfunc.cpf_argcount = argcount;
1018
1019 stack->ga_len -= argcount; // drop the arguments
1020
1021 // drop the funcref/partial, get back the return value
1022 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1023
1024 return OK;
1025}
1026
1027/*
1028 * Generate an ISN_MEMBER instruction.
1029 */
1030 static int
1031generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1032{
1033 isn_T *isn;
1034 garray_T *stack = &cctx->ctx_type_stack;
1035 type_T *type;
1036
1037 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1038 return FAIL;
1039 isn->isn_arg.string = vim_strnsave(name, (int)len);
1040
1041 // change dict type to dict member type
1042 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1043 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
1044
1045 return OK;
1046}
1047
1048/*
1049 * Generate an ISN_ECHO instruction.
1050 */
1051 static int
1052generate_ECHO(cctx_T *cctx, int with_white, int count)
1053{
1054 isn_T *isn;
1055
1056 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1057 return FAIL;
1058 isn->isn_arg.echo.echo_with_white = with_white;
1059 isn->isn_arg.echo.echo_count = count;
1060
1061 return OK;
1062}
1063
1064 static int
1065generate_EXEC(cctx_T *cctx, char_u *line)
1066{
1067 isn_T *isn;
1068
1069 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1070 return FAIL;
1071 isn->isn_arg.string = vim_strsave(line);
1072 return OK;
1073}
1074
1075static char e_white_both[] =
1076 N_("E1004: white space required before and after '%s'");
1077
1078/*
1079 * Reserve space for a local variable.
1080 * Return the index or -1 if it failed.
1081 */
1082 static int
1083reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1084{
1085 int idx;
1086 lvar_T *lvar;
1087
1088 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1089 {
1090 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1091 return -1;
1092 }
1093
1094 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1095 return -1;
1096 idx = cctx->ctx_locals.ga_len;
1097 if (cctx->ctx_max_local < idx + 1)
1098 cctx->ctx_max_local = idx + 1;
1099 ++cctx->ctx_locals.ga_len;
1100
1101 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1102 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1103 lvar->lv_const = isConst;
1104 lvar->lv_type = type;
1105
1106 return idx;
1107}
1108
1109/*
1110 * Skip over a type definition and return a pointer to just after it.
1111 */
1112 char_u *
1113skip_type(char_u *start)
1114{
1115 char_u *p = start;
1116
1117 while (ASCII_ISALNUM(*p) || *p == '_')
1118 ++p;
1119
1120 // Skip over "<type>"; this is permissive about white space.
1121 if (*skipwhite(p) == '<')
1122 {
1123 p = skipwhite(p);
1124 p = skip_type(skipwhite(p + 1));
1125 p = skipwhite(p);
1126 if (*p == '>')
1127 ++p;
1128 }
1129 return p;
1130}
1131
1132/*
1133 * Parse the member type: "<type>" and return "type" with the member set.
1134 * Use "type_list" if a new type needs to be added.
1135 * Returns NULL in case of failure.
1136 */
1137 static type_T *
1138parse_type_member(char_u **arg, type_T *type, garray_T *type_list)
1139{
1140 type_T *member_type;
1141
1142 if (**arg != '<')
1143 {
1144 if (*skipwhite(*arg) == '<')
1145 emsg(_("E1007: No white space allowed before <"));
1146 else
1147 emsg(_("E1008: Missing <type>"));
1148 return NULL;
1149 }
1150 *arg = skipwhite(*arg + 1);
1151
1152 member_type = parse_type(arg, type_list);
1153 if (member_type == NULL)
1154 return NULL;
1155
1156 *arg = skipwhite(*arg);
1157 if (**arg != '>')
1158 {
1159 emsg(_("E1009: Missing > after type"));
1160 return NULL;
1161 }
1162 ++*arg;
1163
1164 if (type->tt_type == VAR_LIST)
1165 return get_list_type(member_type, type_list);
1166 return get_dict_type(member_type, type_list);
1167}
1168
1169/*
1170 * Parse a type at "arg" and advance over it.
1171 * Return NULL for failure.
1172 */
1173 type_T *
1174parse_type(char_u **arg, garray_T *type_list)
1175{
1176 char_u *p = *arg;
1177 size_t len;
1178
1179 // skip over the first word
1180 while (ASCII_ISALNUM(*p) || *p == '_')
1181 ++p;
1182 len = p - *arg;
1183
1184 switch (**arg)
1185 {
1186 case 'a':
1187 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1188 {
1189 *arg += len;
1190 return &t_any;
1191 }
1192 break;
1193 case 'b':
1194 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1195 {
1196 *arg += len;
1197 return &t_bool;
1198 }
1199 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1200 {
1201 *arg += len;
1202 return &t_blob;
1203 }
1204 break;
1205 case 'c':
1206 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1207 {
1208 *arg += len;
1209 return &t_channel;
1210 }
1211 break;
1212 case 'd':
1213 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1214 {
1215 *arg += len;
1216 return parse_type_member(arg, &t_dict_any, type_list);
1217 }
1218 break;
1219 case 'f':
1220 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1221 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001222#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223 *arg += len;
1224 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001225#else
1226 emsg(_("E1055: This Vim is not compiled with float support"));
1227 return &t_any;
1228#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 }
1230 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1231 {
1232 *arg += len;
1233 // TODO: arguments and return type
1234 return &t_func_any;
1235 }
1236 break;
1237 case 'j':
1238 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1239 {
1240 *arg += len;
1241 return &t_job;
1242 }
1243 break;
1244 case 'l':
1245 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1246 {
1247 *arg += len;
1248 return parse_type_member(arg, &t_list_any, type_list);
1249 }
1250 break;
1251 case 'n':
1252 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1253 {
1254 *arg += len;
1255 return &t_number;
1256 }
1257 break;
1258 case 'p':
1259 if (len == 4 && STRNCMP(*arg, "partial", len) == 0)
1260 {
1261 *arg += len;
1262 // TODO: arguments and return type
1263 return &t_partial_any;
1264 }
1265 break;
1266 case 's':
1267 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1268 {
1269 *arg += len;
1270 return &t_string;
1271 }
1272 break;
1273 case 'v':
1274 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1275 {
1276 *arg += len;
1277 return &t_void;
1278 }
1279 break;
1280 }
1281
1282 semsg(_("E1010: Type not recognized: %s"), *arg);
1283 return &t_any;
1284}
1285
1286/*
1287 * Check if "type1" and "type2" are exactly the same.
1288 */
1289 static int
1290equal_type(type_T *type1, type_T *type2)
1291{
1292 if (type1->tt_type != type2->tt_type)
1293 return FALSE;
1294 switch (type1->tt_type)
1295 {
1296 case VAR_VOID:
1297 case VAR_UNKNOWN:
1298 case VAR_SPECIAL:
1299 case VAR_BOOL:
1300 case VAR_NUMBER:
1301 case VAR_FLOAT:
1302 case VAR_STRING:
1303 case VAR_BLOB:
1304 case VAR_JOB:
1305 case VAR_CHANNEL:
1306 return TRUE; // not composite is always OK
1307 case VAR_LIST:
1308 case VAR_DICT:
1309 return equal_type(type1->tt_member, type2->tt_member);
1310 case VAR_FUNC:
1311 case VAR_PARTIAL:
1312 // TODO; check argument types.
1313 return equal_type(type1->tt_member, type2->tt_member)
1314 && type1->tt_argcount == type2->tt_argcount;
1315 }
1316 return TRUE;
1317}
1318
1319/*
1320 * Find the common type of "type1" and "type2" and put it in "dest".
1321 * "type2" and "dest" may be the same.
1322 */
1323 static void
1324common_type(type_T *type1, type_T *type2, type_T *dest)
1325{
1326 if (equal_type(type1, type2))
1327 {
1328 if (dest != type2)
1329 *dest = *type2;
1330 return;
1331 }
1332
1333 if (type1->tt_type == type2->tt_type)
1334 {
1335 dest->tt_type = type1->tt_type;
1336 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1337 {
1338 common_type(type1->tt_member, type2->tt_member, dest->tt_member);
1339 return;
1340 }
1341 // TODO: VAR_FUNC and VAR_PARTIAL
1342 }
1343
1344 dest->tt_type = VAR_UNKNOWN; // "any"
1345}
1346
1347 char *
1348vartype_name(vartype_T type)
1349{
1350 switch (type)
1351 {
1352 case VAR_VOID: return "void";
1353 case VAR_UNKNOWN: return "any";
1354 case VAR_SPECIAL: return "special";
1355 case VAR_BOOL: return "bool";
1356 case VAR_NUMBER: return "number";
1357 case VAR_FLOAT: return "float";
1358 case VAR_STRING: return "string";
1359 case VAR_BLOB: return "blob";
1360 case VAR_JOB: return "job";
1361 case VAR_CHANNEL: return "channel";
1362 case VAR_LIST: return "list";
1363 case VAR_DICT: return "dict";
1364 case VAR_FUNC: return "function";
1365 case VAR_PARTIAL: return "partial";
1366 }
1367 return "???";
1368}
1369
1370/*
1371 * Return the name of a type.
1372 * The result may be in allocated memory, in which case "tofree" is set.
1373 */
1374 char *
1375type_name(type_T *type, char **tofree)
1376{
1377 char *name = vartype_name(type->tt_type);
1378
1379 *tofree = NULL;
1380 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1381 {
1382 char *member_free;
1383 char *member_name = type_name(type->tt_member, &member_free);
1384 size_t len;
1385
1386 len = STRLEN(name) + STRLEN(member_name) + 3;
1387 *tofree = alloc(len);
1388 if (*tofree != NULL)
1389 {
1390 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1391 vim_free(member_free);
1392 return *tofree;
1393 }
1394 }
1395 // TODO: function and partial argument types
1396
1397 return name;
1398}
1399
1400/*
1401 * Find "name" in script-local items of script "sid".
1402 * Returns the index in "sn_var_vals" if found.
1403 * If found but not in "sn_var_vals" returns -1.
1404 * If not found returns -2.
1405 */
1406 int
1407get_script_item_idx(int sid, char_u *name, int check_writable)
1408{
1409 hashtab_T *ht;
1410 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001411 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 int idx;
1413
1414 // First look the name up in the hashtable.
1415 if (sid <= 0 || sid > script_items.ga_len)
1416 return -1;
1417 ht = &SCRIPT_VARS(sid);
1418 di = find_var_in_ht(ht, 0, name, TRUE);
1419 if (di == NULL)
1420 return -2;
1421
1422 // Now find the svar_T index in sn_var_vals.
1423 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1424 {
1425 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1426
1427 if (sv->sv_tv == &di->di_tv)
1428 {
1429 if (check_writable && sv->sv_const)
1430 semsg(_(e_readonlyvar), name);
1431 return idx;
1432 }
1433 }
1434 return -1;
1435}
1436
1437/*
1438 * Find "name" in imported items of the current script/
1439 */
1440 imported_T *
1441find_imported(char_u *name, cctx_T *cctx)
1442{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001443 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 int idx;
1445
1446 if (cctx != NULL)
1447 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1448 {
1449 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1450 + idx;
1451
1452 if (STRCMP(name, import->imp_name) == 0)
1453 return import;
1454 }
1455
1456 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1457 {
1458 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1459
1460 if (STRCMP(name, import->imp_name) == 0)
1461 return import;
1462 }
1463 return NULL;
1464}
1465
1466/*
1467 * Generate an instruction to load script-local variable "name".
1468 */
1469 static int
1470compile_load_scriptvar(cctx_T *cctx, char_u *name)
1471{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001472 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1474 imported_T *import;
1475
1476 if (idx == -1)
1477 {
1478 // variable exists but is not in sn_var_vals: old style script.
1479 return generate_LOADS(cctx, name, current_sctx.sc_sid);
1480 }
1481 if (idx >= 0)
1482 {
1483 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1484
1485 generate_SCRIPT(cctx, ISN_LOADSCRIPT,
1486 current_sctx.sc_sid, idx, sv->sv_type);
1487 return OK;
1488 }
1489
1490 import = find_imported(name, cctx);
1491 if (import != NULL)
1492 {
1493 // TODO: check this is a variable, not a function
1494 generate_SCRIPT(cctx, ISN_LOADSCRIPT,
1495 import->imp_sid,
1496 import->imp_var_vals_idx,
1497 import->imp_type);
1498 return OK;
1499 }
1500
1501 semsg(_("E1050: Item not found: %s"), name);
1502 return FAIL;
1503}
1504
1505/*
1506 * Compile a variable name into a load instruction.
1507 * "end" points to just after the name.
1508 * When "error" is FALSE do not give an error when not found.
1509 */
1510 static int
1511compile_load(char_u **arg, char_u *end, cctx_T *cctx, int error)
1512{
1513 type_T *type;
1514 char_u *name;
1515 int res = FAIL;
1516
1517 if (*(*arg + 1) == ':')
1518 {
1519 // load namespaced variable
1520 name = vim_strnsave(*arg + 2, end - (*arg + 2));
1521 if (name == NULL)
1522 return FAIL;
1523
1524 if (**arg == 'v')
1525 {
1526 // load v:var
1527 int vidx = find_vim_var(name);
1528
1529 if (vidx < 0)
1530 {
1531 if (error)
1532 semsg(_(e_var_notfound), name);
1533 goto theend;
1534 }
1535
1536 // TODO: get actual type
1537 res = generate_LOAD(cctx, ISN_LOADV, vidx, NULL, &t_any);
1538 }
1539 else if (**arg == 'g')
1540 {
1541 // Global variables can be defined later, thus we don't check if it
1542 // exists, give error at runtime.
1543 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
1544 }
1545 else if (**arg == 's')
1546 {
1547 res = compile_load_scriptvar(cctx, name);
1548 }
1549 else
1550 {
1551 semsg("Namespace not supported yet: %s", **arg);
1552 goto theend;
1553 }
1554 }
1555 else
1556 {
1557 size_t len = end - *arg;
1558 int idx;
1559 int gen_load = FALSE;
1560
1561 name = vim_strnsave(*arg, end - *arg);
1562 if (name == NULL)
1563 return FAIL;
1564
1565 idx = lookup_arg(*arg, len, cctx);
1566 if (idx >= 0)
1567 {
1568 if (cctx->ctx_ufunc->uf_arg_types != NULL)
1569 type = cctx->ctx_ufunc->uf_arg_types[idx];
1570 else
1571 type = &t_any;
1572
1573 // Arguments are located above the frame pointer.
1574 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
1575 if (cctx->ctx_ufunc->uf_va_name != NULL)
1576 --idx;
1577 gen_load = TRUE;
1578 }
1579 else if (lookup_vararg(*arg, len, cctx))
1580 {
1581 // varargs is always the last argument
1582 idx = -STACK_FRAME_SIZE - 1;
1583 type = cctx->ctx_ufunc->uf_va_type;
1584 gen_load = TRUE;
1585 }
1586 else
1587 {
1588 idx = lookup_local(*arg, len, cctx);
1589 if (idx >= 0)
1590 {
1591 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
1592 gen_load = TRUE;
1593 }
1594 else
1595 {
1596 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
1597 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
1598 res = generate_PUSHBOOL(cctx, **arg == 't'
1599 ? VVAL_TRUE : VVAL_FALSE);
1600 else
1601 res = compile_load_scriptvar(cctx, name);
1602 }
1603 }
1604 if (gen_load)
1605 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
1606 }
1607
1608 *arg = end;
1609
1610theend:
1611 if (res == FAIL && error)
1612 semsg(_(e_var_notfound), name);
1613 vim_free(name);
1614 return res;
1615}
1616
1617/*
1618 * Compile the argument expressions.
1619 * "arg" points to just after the "(" and is advanced to after the ")"
1620 */
1621 static int
1622compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
1623{
1624 char_u *p = *arg;
1625
1626 while (*p != NUL && *p != ')')
1627 {
1628 if (compile_expr1(&p, cctx) == FAIL)
1629 return FAIL;
1630 ++*argcount;
1631 if (*p == ',')
1632 p = skipwhite(p + 1);
1633 }
1634 if (*p != ')')
1635 {
1636 emsg(_(e_missing_close));
1637 return FAIL;
1638 }
1639 *arg = p + 1;
1640 return OK;
1641}
1642
1643/*
1644 * Compile a function call: name(arg1, arg2)
1645 * "arg" points to "name", "arg + varlen" to the "(".
1646 * "argcount_init" is 1 for "value->method()"
1647 * Instructions:
1648 * EVAL arg1
1649 * EVAL arg2
1650 * BCALL / DCALL / UCALL
1651 */
1652 static int
1653compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
1654{
1655 char_u *name = *arg;
1656 char_u *p = *arg + varlen + 1;
1657 int argcount = argcount_init;
1658 char_u namebuf[100];
1659 ufunc_T *ufunc;
1660
1661 if (varlen >= sizeof(namebuf))
1662 {
1663 semsg(_("E1011: name too long: %s"), name);
1664 return FAIL;
1665 }
1666 vim_strncpy(namebuf, name, varlen);
1667
1668 *arg = skipwhite(*arg + varlen + 1);
1669 if (compile_arguments(arg, cctx, &argcount) == FAIL)
1670 return FAIL;
1671
1672 if (ASCII_ISLOWER(*name))
1673 {
1674 int idx;
1675
1676 // builtin function
1677 idx = find_internal_func(namebuf);
1678 if (idx >= 0)
1679 return generate_BCALL(cctx, idx, argcount);
1680 semsg(_(e_unknownfunc), namebuf);
1681 }
1682
1683 // User defined function or variable must start with upper case.
1684 if (!ASCII_ISUPPER(*name))
1685 {
1686 semsg(_("E1012: Invalid function name: %s"), namebuf);
1687 return FAIL;
1688 }
1689
1690 // If we can find the function by name generate the right call.
1691 ufunc = find_func(namebuf, cctx);
1692 if (ufunc != NULL)
1693 return generate_CALL(cctx, ufunc, argcount);
1694
1695 // If the name is a variable, load it and use PCALL.
1696 p = namebuf;
1697 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
1698 return generate_PCALL(cctx, argcount, FALSE);
1699
1700 // The function may be defined only later. Need to figure out at runtime.
1701 return generate_UCALL(cctx, namebuf, argcount);
1702}
1703
1704// like NAMESPACE_CHAR but with 'a' and 'l'.
1705#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1706
1707/*
1708 * Find the end of a variable or function name. Unlike find_name_end() this
1709 * does not recognize magic braces.
1710 * Return a pointer to just after the name. Equal to "arg" if there is no
1711 * valid name.
1712 */
1713 char_u *
1714to_name_end(char_u *arg)
1715{
1716 char_u *p;
1717
1718 // Quick check for valid starting character.
1719 if (!eval_isnamec1(*arg))
1720 return arg;
1721
1722 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1723 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1724 // and can be used in slice "[n:]".
1725 if (*p == ':' && (p != arg + 1
1726 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1727 break;
1728 return p;
1729}
1730
1731/*
1732 * Like to_name_end() but also skip over a list or dict constant.
1733 */
1734 char_u *
1735to_name_const_end(char_u *arg)
1736{
1737 char_u *p = to_name_end(arg);
1738 typval_T rettv;
1739
1740 if (p == arg && *arg == '[')
1741 {
1742
1743 // Can be "[1, 2, 3]->Func()".
1744 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
1745 p = arg;
1746 }
1747 else if (p == arg && *arg == '#' && arg[1] == '{')
1748 {
1749 ++p;
1750 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
1751 p = arg;
1752 }
1753 else if (p == arg && *arg == '{')
1754 {
1755 int ret = get_lambda_tv(&p, &rettv, FALSE);
1756
1757 if (ret == NOTDONE)
1758 ret = eval_dict(&p, &rettv, FALSE, FALSE);
1759 if (ret != OK)
1760 p = arg;
1761 }
1762
1763 return p;
1764}
1765
1766 static void
1767type_mismatch(type_T *expected, type_T *actual)
1768{
1769 char *tofree1, *tofree2;
1770
1771 semsg(_("E1013: type mismatch, expected %s but got %s"),
1772 type_name(expected, &tofree1), type_name(actual, &tofree2));
1773 vim_free(tofree1);
1774 vim_free(tofree2);
1775}
1776
1777/*
1778 * Check if the expected and actual types match.
1779 */
1780 static int
1781check_type(type_T *expected, type_T *actual, int give_msg)
1782{
1783 if (expected->tt_type != VAR_UNKNOWN)
1784 {
1785 if (expected->tt_type != actual->tt_type)
1786 {
1787 if (give_msg)
1788 type_mismatch(expected, actual);
1789 return FAIL;
1790 }
1791 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
1792 {
1793 int ret = check_type(expected->tt_member, actual->tt_member,
1794 FALSE);
1795 if (ret == FAIL && give_msg)
1796 type_mismatch(expected, actual);
1797 return ret;
1798 }
1799 }
1800 return OK;
1801}
1802
1803/*
1804 * Check that
1805 * - "actual" is "expected" type or
1806 * - "actual" is a type that can be "expected" type: add a runtime check; or
1807 * - return FAIL.
1808 */
1809 static int
1810need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
1811{
1812 if (equal_type(actual, expected) || expected->tt_type == VAR_UNKNOWN)
1813 return OK;
1814 if (actual->tt_type != VAR_UNKNOWN)
1815 {
1816 type_mismatch(expected, actual);
1817 return FAIL;
1818 }
1819 generate_TYPECHECK(cctx, expected, offset);
1820 return OK;
1821}
1822
1823/*
1824 * parse a list: [expr, expr]
1825 * "*arg" points to the '['.
1826 */
1827 static int
1828compile_list(char_u **arg, cctx_T *cctx)
1829{
1830 char_u *p = skipwhite(*arg + 1);
1831 int count = 0;
1832
1833 while (*p != ']')
1834 {
1835 if (*p == NUL)
1836 return FAIL;
1837 if (compile_expr1(&p, cctx) == FAIL)
1838 break;
1839 ++count;
1840 if (*p == ',')
1841 ++p;
1842 p = skipwhite(p);
1843 }
1844 *arg = p + 1;
1845
1846 generate_NEWLIST(cctx, count);
1847 return OK;
1848}
1849
1850/*
1851 * parse a lambda: {arg, arg -> expr}
1852 * "*arg" points to the '{'.
1853 */
1854 static int
1855compile_lambda(char_u **arg, cctx_T *cctx)
1856{
1857 garray_T *instr = &cctx->ctx_instr;
1858 typval_T rettv;
1859 ufunc_T *ufunc;
1860
1861 // Get the funcref in "rettv".
1862 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
1863 return FAIL;
1864 ufunc = rettv.vval.v_partial->pt_func;
1865
1866 // The function will have one line: "return {expr}".
1867 // Compile it into instructions.
1868 compile_def_function(ufunc, TRUE);
1869
1870 if (ufunc->uf_dfunc_idx >= 0)
1871 {
1872 if (ga_grow(instr, 1) == FAIL)
1873 return FAIL;
1874 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
1875 return OK;
1876 }
1877 return FAIL;
1878}
1879
1880/*
1881 * Compile a lamda call: expr->{lambda}(args)
1882 * "arg" points to the "{".
1883 */
1884 static int
1885compile_lambda_call(char_u **arg, cctx_T *cctx)
1886{
1887 ufunc_T *ufunc;
1888 typval_T rettv;
1889 int argcount = 1;
1890 int ret = FAIL;
1891
1892 // Get the funcref in "rettv".
1893 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
1894 return FAIL;
1895
1896 if (**arg != '(')
1897 {
1898 if (*skipwhite(*arg) == '(')
1899 semsg(_(e_nowhitespace));
1900 else
1901 semsg(_(e_missing_paren), "lambda");
1902 clear_tv(&rettv);
1903 return FAIL;
1904 }
1905
1906 // The function will have one line: "return {expr}".
1907 // Compile it into instructions.
1908 ufunc = rettv.vval.v_partial->pt_func;
1909 ++ufunc->uf_refcount;
1910 compile_def_function(ufunc, TRUE);
1911
1912 // compile the arguments
1913 *arg = skipwhite(*arg + 1);
1914 if (compile_arguments(arg, cctx, &argcount) == OK)
1915 // call the compiled function
1916 ret = generate_CALL(cctx, ufunc, argcount);
1917
1918 clear_tv(&rettv);
1919 return ret;
1920}
1921
1922/*
1923 * parse a dict: {'key': val} or #{key: val}
1924 * "*arg" points to the '{'.
1925 */
1926 static int
1927compile_dict(char_u **arg, cctx_T *cctx, int literal)
1928{
1929 garray_T *instr = &cctx->ctx_instr;
1930 int count = 0;
1931 dict_T *d = dict_alloc();
1932 dictitem_T *item;
1933
1934 if (d == NULL)
1935 return FAIL;
1936 *arg = skipwhite(*arg + 1);
1937 while (**arg != '}' && **arg != NUL)
1938 {
1939 char_u *key = NULL;
1940
1941 if (literal)
1942 {
1943 char_u *p = to_name_end(*arg);
1944
1945 if (p == *arg)
1946 {
1947 semsg(_("E1014: Invalid key: %s"), *arg);
1948 return FAIL;
1949 }
1950 key = vim_strnsave(*arg, p - *arg);
1951 if (generate_PUSHS(cctx, key) == FAIL)
1952 return FAIL;
1953 *arg = p;
1954 }
1955 else
1956 {
1957 isn_T *isn;
1958
1959 if (compile_expr1(arg, cctx) == FAIL)
1960 return FAIL;
1961 // TODO: check type is string
1962 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1963 if (isn->isn_type == ISN_PUSHS)
1964 key = isn->isn_arg.string;
1965 }
1966
1967 // Check for duplicate keys, if using string keys.
1968 if (key != NULL)
1969 {
1970 item = dict_find(d, key, -1);
1971 if (item != NULL)
1972 {
1973 semsg(_(e_duplicate_key), key);
1974 goto failret;
1975 }
1976 item = dictitem_alloc(key);
1977 if (item != NULL)
1978 {
1979 item->di_tv.v_type = VAR_UNKNOWN;
1980 item->di_tv.v_lock = 0;
1981 if (dict_add(d, item) == FAIL)
1982 dictitem_free(item);
1983 }
1984 }
1985
1986 *arg = skipwhite(*arg);
1987 if (**arg != ':')
1988 {
1989 semsg(_(e_missing_dict_colon), *arg);
1990 return FAIL;
1991 }
1992
1993 *arg = skipwhite(*arg + 1);
1994 if (compile_expr1(arg, cctx) == FAIL)
1995 return FAIL;
1996 ++count;
1997
1998 if (**arg == '}')
1999 break;
2000 if (**arg != ',')
2001 {
2002 semsg(_(e_missing_dict_comma), *arg);
2003 goto failret;
2004 }
2005 *arg = skipwhite(*arg + 1);
2006 }
2007
2008 if (**arg != '}')
2009 {
2010 semsg(_(e_missing_dict_end), *arg);
2011 goto failret;
2012 }
2013 *arg = *arg + 1;
2014
2015 dict_unref(d);
2016 return generate_NEWDICT(cctx, count);
2017
2018failret:
2019 dict_unref(d);
2020 return FAIL;
2021}
2022
2023/*
2024 * Compile "&option".
2025 */
2026 static int
2027compile_get_option(char_u **arg, cctx_T *cctx)
2028{
2029 typval_T rettv;
2030 char_u *start = *arg;
2031 int ret;
2032
2033 // parse the option and get the current value to get the type.
2034 rettv.v_type = VAR_UNKNOWN;
2035 ret = get_option_tv(arg, &rettv, TRUE);
2036 if (ret == OK)
2037 {
2038 // include the '&' in the name, get_option_tv() expects it.
2039 char_u *name = vim_strnsave(start, *arg - start);
2040 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2041
2042 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2043 vim_free(name);
2044 }
2045 clear_tv(&rettv);
2046
2047 return ret;
2048}
2049
2050/*
2051 * Compile "$VAR".
2052 */
2053 static int
2054compile_get_env(char_u **arg, cctx_T *cctx)
2055{
2056 char_u *start = *arg;
2057 int len;
2058 int ret;
2059 char_u *name;
2060
2061 start = *arg;
2062 ++*arg;
2063 len = get_env_len(arg);
2064 if (len == 0)
2065 {
2066 semsg(_(e_syntax_at), start - 1);
2067 return FAIL;
2068 }
2069
2070 // include the '$' in the name, get_env_tv() expects it.
2071 name = vim_strnsave(start, len + 1);
2072 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2073 vim_free(name);
2074 return ret;
2075}
2076
2077/*
2078 * Compile "@r".
2079 */
2080 static int
2081compile_get_register(char_u **arg, cctx_T *cctx)
2082{
2083 int ret;
2084
2085 ++*arg;
2086 if (**arg == NUL)
2087 {
2088 semsg(_(e_syntax_at), *arg - 1);
2089 return FAIL;
2090 }
2091 if (!valid_yank_reg(**arg, TRUE))
2092 {
2093 emsg_invreg(**arg);
2094 return FAIL;
2095 }
2096 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2097 ++*arg;
2098 return ret;
2099}
2100
2101/*
2102 * Apply leading '!', '-' and '+' to constant "rettv".
2103 */
2104 static int
2105apply_leader(typval_T *rettv, char_u *start, char_u *end)
2106{
2107 char_u *p = end;
2108
2109 // this works from end to start
2110 while (p > start)
2111 {
2112 --p;
2113 if (*p == '-' || *p == '+')
2114 {
2115 // only '-' has an effect, for '+' we only check the type
2116#ifdef FEAT_FLOAT
2117 if (rettv->v_type == VAR_FLOAT)
2118 {
2119 if (*p == '-')
2120 rettv->vval.v_float = -rettv->vval.v_float;
2121 }
2122 else
2123#endif
2124 {
2125 varnumber_T val;
2126 int error = FALSE;
2127
2128 // tv_get_number_chk() accepts a string, but we don't want that
2129 // here
2130 if (check_not_string(rettv) == FAIL)
2131 return FAIL;
2132 val = tv_get_number_chk(rettv, &error);
2133 clear_tv(rettv);
2134 if (error)
2135 return FAIL;
2136 if (*p == '-')
2137 val = -val;
2138 rettv->v_type = VAR_NUMBER;
2139 rettv->vval.v_number = val;
2140 }
2141 }
2142 else
2143 {
2144 int v = tv2bool(rettv);
2145
2146 // '!' is permissive in the type.
2147 clear_tv(rettv);
2148 rettv->v_type = VAR_BOOL;
2149 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2150 }
2151 }
2152 return OK;
2153}
2154
2155/*
2156 * Recognize v: variables that are constants and set "rettv".
2157 */
2158 static void
2159get_vim_constant(char_u **arg, typval_T *rettv)
2160{
2161 if (STRNCMP(*arg, "v:true", 6) == 0)
2162 {
2163 rettv->v_type = VAR_BOOL;
2164 rettv->vval.v_number = VVAL_TRUE;
2165 *arg += 6;
2166 }
2167 else if (STRNCMP(*arg, "v:false", 7) == 0)
2168 {
2169 rettv->v_type = VAR_BOOL;
2170 rettv->vval.v_number = VVAL_FALSE;
2171 *arg += 7;
2172 }
2173 else if (STRNCMP(*arg, "v:null", 6) == 0)
2174 {
2175 rettv->v_type = VAR_SPECIAL;
2176 rettv->vval.v_number = VVAL_NULL;
2177 *arg += 6;
2178 }
2179 else if (STRNCMP(*arg, "v:none", 6) == 0)
2180 {
2181 rettv->v_type = VAR_SPECIAL;
2182 rettv->vval.v_number = VVAL_NONE;
2183 *arg += 6;
2184 }
2185}
2186
2187/*
2188 * Compile code to apply '-', '+' and '!'.
2189 */
2190 static int
2191compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2192{
2193 char_u *p = end;
2194
2195 // this works from end to start
2196 while (p > start)
2197 {
2198 --p;
2199 if (*p == '-' || *p == '+')
2200 {
2201 int negate = *p == '-';
2202 isn_T *isn;
2203
2204 // TODO: check type
2205 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2206 {
2207 --p;
2208 if (*p == '-')
2209 negate = !negate;
2210 }
2211 // only '-' has an effect, for '+' we only check the type
2212 if (negate)
2213 isn = generate_instr(cctx, ISN_NEGATENR);
2214 else
2215 isn = generate_instr(cctx, ISN_CHECKNR);
2216 if (isn == NULL)
2217 return FAIL;
2218 }
2219 else
2220 {
2221 int invert = TRUE;
2222
2223 while (p > start && p[-1] == '!')
2224 {
2225 --p;
2226 invert = !invert;
2227 }
2228 if (generate_2BOOL(cctx, invert) == FAIL)
2229 return FAIL;
2230 }
2231 }
2232 return OK;
2233}
2234
2235/*
2236 * Compile whatever comes after "name" or "name()".
2237 */
2238 static int
2239compile_subscript(
2240 char_u **arg,
2241 cctx_T *cctx,
2242 char_u **start_leader,
2243 char_u *end_leader)
2244{
2245 for (;;)
2246 {
2247 if (**arg == '(')
2248 {
2249 int argcount = 0;
2250
2251 // funcref(arg)
2252 *arg = skipwhite(*arg + 1);
2253 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2254 return FAIL;
2255 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2256 return FAIL;
2257 }
2258 else if (**arg == '-' && (*arg)[1] == '>')
2259 {
2260 char_u *p;
2261
2262 // something->method()
2263 // Apply the '!', '-' and '+' first:
2264 // -1.0->func() works like (-1.0)->func()
2265 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2266 return FAIL;
2267 *start_leader = end_leader; // don't apply again later
2268
2269 *arg = skipwhite(*arg + 2);
2270 if (**arg == '{')
2271 {
2272 // lambda call: list->{lambda}
2273 if (compile_lambda_call(arg, cctx) == FAIL)
2274 return FAIL;
2275 }
2276 else
2277 {
2278 // method call: list->method()
2279 for (p = *arg; eval_isnamec1(*p); ++p)
2280 ;
2281 if (*p != '(')
2282 {
2283 semsg(_(e_missing_paren), arg);
2284 return FAIL;
2285 }
2286 // TODO: base value may not be the first argument
2287 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2288 return FAIL;
2289 }
2290 }
2291 else if (**arg == '[')
2292 {
2293 // list index: list[123]
2294 // TODO: more arguments
2295 // TODO: dict member dict['name']
2296 *arg = skipwhite(*arg + 1);
2297 if (compile_expr1(arg, cctx) == FAIL)
2298 return FAIL;
2299
2300 if (**arg != ']')
2301 {
2302 emsg(_(e_missbrac));
2303 return FAIL;
2304 }
2305 *arg = skipwhite(*arg + 1);
2306
2307 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
2308 return FAIL;
2309 }
2310 else if (**arg == '.' && (*arg)[1] != '.')
2311 {
2312 char_u *p;
2313
2314 ++*arg;
2315 p = *arg;
2316 // dictionary member: dict.name
2317 if (eval_isnamec1(*p))
2318 while (eval_isnamec(*p))
2319 MB_PTR_ADV(p);
2320 if (p == *arg)
2321 {
2322 semsg(_(e_syntax_at), *arg);
2323 return FAIL;
2324 }
2325 // TODO: check type is dict
2326 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
2327 return FAIL;
2328 *arg = p;
2329 }
2330 else
2331 break;
2332 }
2333
2334 // TODO - see handle_subscript():
2335 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2336 // Don't do this when "Func" is already a partial that was bound
2337 // explicitly (pt_auto is FALSE).
2338
2339 return OK;
2340}
2341
2342/*
2343 * Compile an expression at "*p" and add instructions to "instr".
2344 * "p" is advanced until after the expression, skipping white space.
2345 *
2346 * This is the equivalent of eval1(), eval2(), etc.
2347 */
2348
2349/*
2350 * number number constant
2351 * 0zFFFFFFFF Blob constant
2352 * "string" string constant
2353 * 'string' literal string constant
2354 * &option-name option value
2355 * @r register contents
2356 * identifier variable value
2357 * function() function call
2358 * $VAR environment variable
2359 * (expression) nested expression
2360 * [expr, expr] List
2361 * {key: val, key: val} Dictionary
2362 * #{key: val, key: val} Dictionary with literal keys
2363 *
2364 * Also handle:
2365 * ! in front logical NOT
2366 * - in front unary minus
2367 * + in front unary plus (ignored)
2368 * trailing (arg) funcref/partial call
2369 * trailing [] subscript in String or List
2370 * trailing .name entry in Dictionary
2371 * trailing ->name() method call
2372 */
2373 static int
2374compile_expr7(char_u **arg, cctx_T *cctx)
2375{
2376 typval_T rettv;
2377 char_u *start_leader, *end_leader;
2378 int ret = OK;
2379
2380 /*
2381 * Skip '!', '-' and '+' characters. They are handled later.
2382 */
2383 start_leader = *arg;
2384 while (**arg == '!' || **arg == '-' || **arg == '+')
2385 *arg = skipwhite(*arg + 1);
2386 end_leader = *arg;
2387
2388 rettv.v_type = VAR_UNKNOWN;
2389 switch (**arg)
2390 {
2391 /*
2392 * Number constant.
2393 */
2394 case '0': // also for blob starting with 0z
2395 case '1':
2396 case '2':
2397 case '3':
2398 case '4':
2399 case '5':
2400 case '6':
2401 case '7':
2402 case '8':
2403 case '9':
2404 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
2405 return FAIL;
2406 break;
2407
2408 /*
2409 * String constant: "string".
2410 */
2411 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
2412 return FAIL;
2413 break;
2414
2415 /*
2416 * Literal string constant: 'str''ing'.
2417 */
2418 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
2419 return FAIL;
2420 break;
2421
2422 /*
2423 * Constant Vim variable.
2424 */
2425 case 'v': get_vim_constant(arg, &rettv);
2426 ret = NOTDONE;
2427 break;
2428
2429 /*
2430 * List: [expr, expr]
2431 */
2432 case '[': ret = compile_list(arg, cctx);
2433 break;
2434
2435 /*
2436 * Dictionary: #{key: val, key: val}
2437 */
2438 case '#': if ((*arg)[1] == '{')
2439 {
2440 ++*arg;
2441 ret = compile_dict(arg, cctx, TRUE);
2442 }
2443 else
2444 ret = NOTDONE;
2445 break;
2446
2447 /*
2448 * Lambda: {arg, arg -> expr}
2449 * Dictionary: {'key': val, 'key': val}
2450 */
2451 case '{': {
2452 char_u *start = skipwhite(*arg + 1);
2453
2454 // Find out what comes after the arguments.
2455 ret = get_function_args(&start, '-', NULL,
2456 NULL, NULL, NULL, TRUE);
2457 if (ret != FAIL && *start == '>')
2458 ret = compile_lambda(arg, cctx);
2459 else
2460 ret = compile_dict(arg, cctx, FALSE);
2461 }
2462 break;
2463
2464 /*
2465 * Option value: &name
2466 */
2467 case '&': ret = compile_get_option(arg, cctx);
2468 break;
2469
2470 /*
2471 * Environment variable: $VAR.
2472 */
2473 case '$': ret = compile_get_env(arg, cctx);
2474 break;
2475
2476 /*
2477 * Register contents: @r.
2478 */
2479 case '@': ret = compile_get_register(arg, cctx);
2480 break;
2481 /*
2482 * nested expression: (expression).
2483 */
2484 case '(': *arg = skipwhite(*arg + 1);
2485 ret = compile_expr1(arg, cctx); // recursive!
2486 *arg = skipwhite(*arg);
2487 if (**arg == ')')
2488 ++*arg;
2489 else if (ret == OK)
2490 {
2491 emsg(_(e_missing_close));
2492 ret = FAIL;
2493 }
2494 break;
2495
2496 default: ret = NOTDONE;
2497 break;
2498 }
2499 if (ret == FAIL)
2500 return FAIL;
2501
2502 if (rettv.v_type != VAR_UNKNOWN)
2503 {
2504 // apply the '!', '-' and '+' before the constant
2505 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
2506 {
2507 clear_tv(&rettv);
2508 return FAIL;
2509 }
2510 start_leader = end_leader; // don't apply again below
2511
2512 // push constant
2513 switch (rettv.v_type)
2514 {
2515 case VAR_BOOL:
2516 generate_PUSHBOOL(cctx, rettv.vval.v_number);
2517 break;
2518 case VAR_SPECIAL:
2519 generate_PUSHSPEC(cctx, rettv.vval.v_number);
2520 break;
2521 case VAR_NUMBER:
2522 generate_PUSHNR(cctx, rettv.vval.v_number);
2523 break;
2524#ifdef FEAT_FLOAT
2525 case VAR_FLOAT:
2526 generate_PUSHF(cctx, rettv.vval.v_float);
2527 break;
2528#endif
2529 case VAR_BLOB:
2530 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
2531 rettv.vval.v_blob = NULL;
2532 break;
2533 case VAR_STRING:
2534 generate_PUSHS(cctx, rettv.vval.v_string);
2535 rettv.vval.v_string = NULL;
2536 break;
2537 default:
2538 iemsg("constant type missing");
2539 return FAIL;
2540 }
2541 }
2542 else if (ret == NOTDONE)
2543 {
2544 char_u *p;
2545 int r;
2546
2547 if (!eval_isnamec1(**arg))
2548 {
2549 semsg(_("E1015: Name expected: %s"), *arg);
2550 return FAIL;
2551 }
2552
2553 // "name" or "name()"
2554 p = to_name_end(*arg);
2555 if (*p == '(')
2556 r = compile_call(arg, p - *arg, cctx, 0);
2557 else
2558 r = compile_load(arg, p, cctx, TRUE);
2559 if (r == FAIL)
2560 return FAIL;
2561 }
2562
2563 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
2564 return FAIL;
2565
2566 // Now deal with prefixed '-', '+' and '!', if not done already.
2567 return compile_leader(cctx, start_leader, end_leader);
2568}
2569
2570/*
2571 * * number multiplication
2572 * / number division
2573 * % number modulo
2574 */
2575 static int
2576compile_expr6(char_u **arg, cctx_T *cctx)
2577{
2578 char_u *op;
2579
2580 // get the first variable
2581 if (compile_expr7(arg, cctx) == FAIL)
2582 return FAIL;
2583
2584 /*
2585 * Repeat computing, until no "*", "/" or "%" is following.
2586 */
2587 for (;;)
2588 {
2589 op = skipwhite(*arg);
2590 if (*op != '*' && *op != '/' && *op != '%')
2591 break;
2592 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
2593 {
2594 char_u buf[3];
2595
2596 vim_strncpy(buf, op, 1);
2597 semsg(_(e_white_both), buf);
2598 }
2599 *arg = skipwhite(op + 1);
2600
2601 // get the second variable
2602 if (compile_expr7(arg, cctx) == FAIL)
2603 return FAIL;
2604
2605 generate_two_op(cctx, op);
2606 }
2607
2608 return OK;
2609}
2610
2611/*
2612 * + number addition
2613 * - number subtraction
2614 * .. string concatenation
2615 */
2616 static int
2617compile_expr5(char_u **arg, cctx_T *cctx)
2618{
2619 char_u *op;
2620 int oplen;
2621
2622 // get the first variable
2623 if (compile_expr6(arg, cctx) == FAIL)
2624 return FAIL;
2625
2626 /*
2627 * Repeat computing, until no "+", "-" or ".." is following.
2628 */
2629 for (;;)
2630 {
2631 op = skipwhite(*arg);
2632 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
2633 break;
2634 oplen = (*op == '.' ? 2 : 1);
2635
2636 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
2637 {
2638 char_u buf[3];
2639
2640 vim_strncpy(buf, op, oplen);
2641 semsg(_(e_white_both), buf);
2642 }
2643
2644 *arg = skipwhite(op + oplen);
2645
2646 // get the second variable
2647 if (compile_expr6(arg, cctx) == FAIL)
2648 return FAIL;
2649
2650 if (*op == '.')
2651 {
2652 if (may_generate_2STRING(-2, cctx) == FAIL
2653 || may_generate_2STRING(-1, cctx) == FAIL)
2654 return FAIL;
2655 generate_instr_drop(cctx, ISN_CONCAT, 1);
2656 }
2657 else
2658 generate_two_op(cctx, op);
2659 }
2660
2661 return OK;
2662}
2663
2664/*
2665 * expr5a == expr5b
2666 * expr5a =~ expr5b
2667 * expr5a != expr5b
2668 * expr5a !~ expr5b
2669 * expr5a > expr5b
2670 * expr5a >= expr5b
2671 * expr5a < expr5b
2672 * expr5a <= expr5b
2673 * expr5a is expr5b
2674 * expr5a isnot expr5b
2675 *
2676 * Produces instructions:
2677 * EVAL expr5a Push result of "expr5a"
2678 * EVAL expr5b Push result of "expr5b"
2679 * COMPARE one of the compare instructions
2680 */
2681 static int
2682compile_expr4(char_u **arg, cctx_T *cctx)
2683{
2684 exptype_T type = EXPR_UNKNOWN;
2685 char_u *p;
2686 int len = 2;
2687 int i;
2688 int type_is = FALSE;
2689
2690 // get the first variable
2691 if (compile_expr5(arg, cctx) == FAIL)
2692 return FAIL;
2693
2694 p = skipwhite(*arg);
2695 switch (p[0])
2696 {
2697 case '=': if (p[1] == '=')
2698 type = EXPR_EQUAL;
2699 else if (p[1] == '~')
2700 type = EXPR_MATCH;
2701 break;
2702 case '!': if (p[1] == '=')
2703 type = EXPR_NEQUAL;
2704 else if (p[1] == '~')
2705 type = EXPR_NOMATCH;
2706 break;
2707 case '>': if (p[1] != '=')
2708 {
2709 type = EXPR_GREATER;
2710 len = 1;
2711 }
2712 else
2713 type = EXPR_GEQUAL;
2714 break;
2715 case '<': if (p[1] != '=')
2716 {
2717 type = EXPR_SMALLER;
2718 len = 1;
2719 }
2720 else
2721 type = EXPR_SEQUAL;
2722 break;
2723 case 'i': if (p[1] == 's')
2724 {
2725 // "is" and "isnot"; but not a prefix of a name
2726 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2727 len = 5;
2728 i = p[len];
2729 if (!isalnum(i) && i != '_')
2730 {
2731 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
2732 type_is = TRUE;
2733 }
2734 }
2735 break;
2736 }
2737
2738 /*
2739 * If there is a comparative operator, use it.
2740 */
2741 if (type != EXPR_UNKNOWN)
2742 {
2743 int ic = FALSE; // Default: do not ignore case
2744
2745 if (type_is && (p[len] == '?' || p[len] == '#'))
2746 {
2747 semsg(_(e_invexpr2), *arg);
2748 return FAIL;
2749 }
2750 // extra question mark appended: ignore case
2751 if (p[len] == '?')
2752 {
2753 ic = TRUE;
2754 ++len;
2755 }
2756 // extra '#' appended: match case (ignored)
2757 else if (p[len] == '#')
2758 ++len;
2759 // nothing appended: match case
2760
2761 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
2762 {
2763 char_u buf[7];
2764
2765 vim_strncpy(buf, p, len);
2766 semsg(_(e_white_both), buf);
2767 }
2768
2769 // get the second variable
2770 *arg = skipwhite(p + len);
2771 if (compile_expr5(arg, cctx) == FAIL)
2772 return FAIL;
2773
2774 generate_COMPARE(cctx, type, ic);
2775 }
2776
2777 return OK;
2778}
2779
2780/*
2781 * Compile || or &&.
2782 */
2783 static int
2784compile_and_or(char_u **arg, cctx_T *cctx, char *op)
2785{
2786 char_u *p = skipwhite(*arg);
2787 int opchar = *op;
2788
2789 if (p[0] == opchar && p[1] == opchar)
2790 {
2791 garray_T *instr = &cctx->ctx_instr;
2792 garray_T end_ga;
2793
2794 /*
2795 * Repeat until there is no following "||" or "&&"
2796 */
2797 ga_init2(&end_ga, sizeof(int), 10);
2798 while (p[0] == opchar && p[1] == opchar)
2799 {
2800 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
2801 semsg(_(e_white_both), op);
2802
2803 if (ga_grow(&end_ga, 1) == FAIL)
2804 {
2805 ga_clear(&end_ga);
2806 return FAIL;
2807 }
2808 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2809 ++end_ga.ga_len;
2810 generate_JUMP(cctx, opchar == '|'
2811 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
2812
2813 // eval the next expression
2814 *arg = skipwhite(p + 2);
2815 if ((opchar == '|' ? compile_expr3(arg, cctx)
2816 : compile_expr4(arg, cctx)) == FAIL)
2817 {
2818 ga_clear(&end_ga);
2819 return FAIL;
2820 }
2821 p = skipwhite(*arg);
2822 }
2823
2824 // Fill in the end label in all jumps.
2825 while (end_ga.ga_len > 0)
2826 {
2827 isn_T *isn;
2828
2829 --end_ga.ga_len;
2830 isn = ((isn_T *)instr->ga_data)
2831 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
2832 isn->isn_arg.jump.jump_where = instr->ga_len;
2833 }
2834 ga_clear(&end_ga);
2835 }
2836
2837 return OK;
2838}
2839
2840/*
2841 * expr4a && expr4a && expr4a logical AND
2842 *
2843 * Produces instructions:
2844 * EVAL expr4a Push result of "expr4a"
2845 * JUMP_AND_KEEP_IF_FALSE end
2846 * EVAL expr4b Push result of "expr4b"
2847 * JUMP_AND_KEEP_IF_FALSE end
2848 * EVAL expr4c Push result of "expr4c"
2849 * end:
2850 */
2851 static int
2852compile_expr3(char_u **arg, cctx_T *cctx)
2853{
2854 // get the first variable
2855 if (compile_expr4(arg, cctx) == FAIL)
2856 return FAIL;
2857
2858 // || and && work almost the same
2859 return compile_and_or(arg, cctx, "&&");
2860}
2861
2862/*
2863 * expr3a || expr3b || expr3c logical OR
2864 *
2865 * Produces instructions:
2866 * EVAL expr3a Push result of "expr3a"
2867 * JUMP_AND_KEEP_IF_TRUE end
2868 * EVAL expr3b Push result of "expr3b"
2869 * JUMP_AND_KEEP_IF_TRUE end
2870 * EVAL expr3c Push result of "expr3c"
2871 * end:
2872 */
2873 static int
2874compile_expr2(char_u **arg, cctx_T *cctx)
2875{
2876 // eval the first expression
2877 if (compile_expr3(arg, cctx) == FAIL)
2878 return FAIL;
2879
2880 // || and && work almost the same
2881 return compile_and_or(arg, cctx, "||");
2882}
2883
2884/*
2885 * Toplevel expression: expr2 ? expr1a : expr1b
2886 *
2887 * Produces instructions:
2888 * EVAL expr2 Push result of "expr"
2889 * JUMP_IF_FALSE alt jump if false
2890 * EVAL expr1a
2891 * JUMP_ALWAYS end
2892 * alt: EVAL expr1b
2893 * end:
2894 */
2895 static int
2896compile_expr1(char_u **arg, cctx_T *cctx)
2897{
2898 char_u *p;
2899
2900 // evaluate the first expression
2901 if (compile_expr2(arg, cctx) == FAIL)
2902 return FAIL;
2903
2904 p = skipwhite(*arg);
2905 if (*p == '?')
2906 {
2907 garray_T *instr = &cctx->ctx_instr;
2908 garray_T *stack = &cctx->ctx_type_stack;
2909 int alt_idx = instr->ga_len;
2910 int end_idx;
2911 isn_T *isn;
2912 type_T *type1;
2913 type_T *type2;
2914
2915 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
2916 semsg(_(e_white_both), "?");
2917
2918 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
2919
2920 // evaluate the second expression; any type is accepted
2921 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01002922 if (compile_expr1(arg, cctx) == FAIL)
2923 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924
2925 // remember the type and drop it
2926 --stack->ga_len;
2927 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
2928
2929 end_idx = instr->ga_len;
2930 generate_JUMP(cctx, JUMP_ALWAYS, 0);
2931
2932 // jump here from JUMP_IF_FALSE
2933 isn = ((isn_T *)instr->ga_data) + alt_idx;
2934 isn->isn_arg.jump.jump_where = instr->ga_len;
2935
2936 // Check for the ":".
2937 p = skipwhite(*arg);
2938 if (*p != ':')
2939 {
2940 emsg(_(e_missing_colon));
2941 return FAIL;
2942 }
2943 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
2944 semsg(_(e_white_both), ":");
2945
2946 // evaluate the third expression
2947 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01002948 if (compile_expr1(arg, cctx) == FAIL)
2949 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950
2951 // If the types differ, the result has a more generic type.
2952 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2953 common_type(type1, type2, type2);
2954
2955 // jump here from JUMP_ALWAYS
2956 isn = ((isn_T *)instr->ga_data) + end_idx;
2957 isn->isn_arg.jump.jump_where = instr->ga_len;
2958 }
2959 return OK;
2960}
2961
2962/*
2963 * compile "return [expr]"
2964 */
2965 static char_u *
2966compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
2967{
2968 char_u *p = arg;
2969 garray_T *stack = &cctx->ctx_type_stack;
2970 type_T *stack_type;
2971
2972 if (*p != NUL && *p != '|' && *p != '\n')
2973 {
2974 // compile return argument into instructions
2975 if (compile_expr1(&p, cctx) == FAIL)
2976 return NULL;
2977
2978 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2979 if (set_return_type)
2980 cctx->ctx_ufunc->uf_ret_type = stack_type;
2981 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
2982 == FAIL)
2983 return NULL;
2984 }
2985 else
2986 {
2987 if (set_return_type)
2988 cctx->ctx_ufunc->uf_ret_type = &t_void;
2989 else if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID)
2990 {
2991 emsg(_("E1003: Missing return value"));
2992 return NULL;
2993 }
2994
2995 // No argument, return zero.
2996 generate_PUSHNR(cctx, 0);
2997 }
2998
2999 if (generate_instr(cctx, ISN_RETURN) == NULL)
3000 return NULL;
3001
3002 // "return val | endif" is possible
3003 return skipwhite(p);
3004}
3005
3006/*
3007 * Return the length of an assignment operator, or zero if there isn't one.
3008 */
3009 int
3010assignment_len(char_u *p, int *heredoc)
3011{
3012 if (*p == '=')
3013 {
3014 if (p[1] == '<' && p[2] == '<')
3015 {
3016 *heredoc = TRUE;
3017 return 3;
3018 }
3019 return 1;
3020 }
3021 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3022 return 2;
3023 if (STRNCMP(p, "..=", 3) == 0)
3024 return 3;
3025 return 0;
3026}
3027
3028// words that cannot be used as a variable
3029static char *reserved[] = {
3030 "true",
3031 "false",
3032 NULL
3033};
3034
3035/*
3036 * Get a line for "=<<".
3037 * Return a pointer to the line in allocated memory.
3038 * Return NULL for end-of-file or some error.
3039 */
3040 static char_u *
3041heredoc_getline(
3042 int c UNUSED,
3043 void *cookie,
3044 int indent UNUSED,
3045 int do_concat UNUSED)
3046{
3047 cctx_T *cctx = (cctx_T *)cookie;
3048
3049 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
3050 NULL;
3051 ++cctx->ctx_lnum;
3052 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3053 [cctx->ctx_lnum]);
3054}
3055
3056/*
3057 * compile "let var [= expr]", "const var = expr" and "var = expr"
3058 * "arg" points to "var".
3059 */
3060 static char_u *
3061compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3062{
3063 char_u *p;
3064 char_u *ret = NULL;
3065 int var_count = 0;
3066 int semicolon = 0;
3067 size_t varlen;
3068 garray_T *instr = &cctx->ctx_instr;
3069 int idx = -1;
3070 char_u *op;
3071 int option = FALSE;
3072 int opt_type;
3073 int opt_flags = 0;
3074 int global = FALSE;
3075 int script = FALSE;
3076 int oplen = 0;
3077 int heredoc = FALSE;
3078 type_T *type;
3079 lvar_T *lvar;
3080 char_u *name;
3081 char_u *sp;
3082 int has_type = FALSE;
3083 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3084 int instr_count = -1;
3085
3086 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3087 if (p == NULL)
3088 return NULL;
3089 if (var_count > 0)
3090 {
3091 // TODO: let [var, var] = list
3092 emsg("Cannot handle a list yet");
3093 return NULL;
3094 }
3095
3096 varlen = p - arg;
3097 name = vim_strnsave(arg, (int)varlen);
3098 if (name == NULL)
3099 return NULL;
3100
3101 if (*arg == '&')
3102 {
3103 int cc;
3104 long numval;
3105 char_u *stringval = NULL;
3106
3107 option = TRUE;
3108 if (cmdidx == CMD_const)
3109 {
3110 emsg(_(e_const_option));
3111 return NULL;
3112 }
3113 if (is_decl)
3114 {
3115 semsg(_("E1052: Cannot declare an option: %s"), arg);
3116 goto theend;
3117 }
3118 p = arg;
3119 p = find_option_end(&p, &opt_flags);
3120 if (p == NULL)
3121 {
3122 emsg(_(e_letunexp));
3123 return NULL;
3124 }
3125 cc = *p;
3126 *p = NUL;
3127 opt_type = get_option_value(arg + 1, &numval, &stringval, opt_flags);
3128 *p = cc;
3129 if (opt_type == -3)
3130 {
3131 semsg(_(e_unknown_option), *arg);
3132 return NULL;
3133 }
3134 if (opt_type == -2 || opt_type == 0)
3135 type = &t_string;
3136 else
3137 type = &t_number; // both number and boolean option
3138 }
3139 else if (STRNCMP(arg, "g:", 2) == 0)
3140 {
3141 global = TRUE;
3142 if (is_decl)
3143 {
3144 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3145 goto theend;
3146 }
3147 }
3148 else
3149 {
3150 for (idx = 0; reserved[idx] != NULL; ++idx)
3151 if (STRCMP(reserved[idx], name) == 0)
3152 {
3153 semsg(_("E1034: Cannot use reserved name %s"), name);
3154 goto theend;
3155 }
3156
3157 idx = lookup_local(arg, varlen, cctx);
3158 if (idx >= 0)
3159 {
3160 if (is_decl)
3161 {
3162 semsg(_("E1017: Variable already declared: %s"), name);
3163 goto theend;
3164 }
3165 else
3166 {
3167 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3168 if (lvar->lv_const)
3169 {
3170 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3171 goto theend;
3172 }
3173 }
3174 }
3175 else if (lookup_script(arg, varlen) == OK)
3176 {
3177 script = TRUE;
3178 if (is_decl)
3179 {
3180 semsg(_("E1054: Variable already declared in the script: %s"),
3181 name);
3182 goto theend;
3183 }
3184 }
3185 }
3186
3187 if (!option)
3188 {
3189 if (is_decl && *p == ':')
3190 {
3191 // parse optional type: "let var: type = expr"
3192 p = skipwhite(p + 1);
3193 type = parse_type(&p, cctx->ctx_type_list);
3194 if (type == NULL)
3195 goto theend;
3196 has_type = TRUE;
3197 }
3198 else if (idx < 0)
3199 {
3200 // global and new local default to "any" type
3201 type = &t_any;
3202 }
3203 else
3204 {
3205 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3206 type = lvar->lv_type;
3207 }
3208 }
3209
3210 sp = p;
3211 p = skipwhite(p);
3212 op = p;
3213 oplen = assignment_len(p, &heredoc);
3214 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3215 {
3216 char_u buf[4];
3217
3218 vim_strncpy(buf, op, oplen);
3219 semsg(_(e_white_both), buf);
3220 }
3221
3222 if (oplen == 3 && !heredoc && !global && type->tt_type != VAR_STRING
3223 && type->tt_type != VAR_UNKNOWN)
3224 {
3225 emsg("E1019: Can only concatenate to string");
3226 goto theend;
3227 }
3228
3229 // +=, /=, etc. require an existing variable
3230 if (idx < 0 && !global && !option)
3231 {
3232 if (oplen > 1 && !heredoc)
3233 {
3234 semsg(_("E1020: cannot use an operator on a new variable: %s"),
3235 name);
3236 goto theend;
3237 }
3238
3239 // new local variable
3240 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
3241 if (idx < 0)
3242 goto theend;
3243 }
3244
3245 if (heredoc)
3246 {
3247 list_T *l;
3248 listitem_T *li;
3249
3250 // [let] varname =<< [trim] {end}
3251 eap->getline = heredoc_getline;
3252 eap->cookie = cctx;
3253 l = heredoc_get(eap, op + 3);
3254
3255 // Push each line and the create the list.
3256 for (li = l->lv_first; li != NULL; li = li->li_next)
3257 {
3258 generate_PUSHS(cctx, li->li_tv.vval.v_string);
3259 li->li_tv.vval.v_string = NULL;
3260 }
3261 generate_NEWLIST(cctx, l->lv_len);
3262 type = &t_list_string;
3263 list_free(l);
3264 p += STRLEN(p);
3265 }
3266 else if (oplen > 0)
3267 {
3268 // for "+=", "*=", "..=" etc. first load the current value
3269 if (*op != '=')
3270 {
3271 if (option)
Bram Moolenaara6d53682020-01-28 23:04:06 +01003272 // TODO: check the option exists
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 generate_LOAD(cctx, ISN_LOADOPT, 0, name + 1, type);
3274 else if (global)
3275 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
3276 else
3277 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
3278 }
3279
3280 // compile the expression
3281 instr_count = instr->ga_len;
3282 p = skipwhite(p + oplen);
3283 if (compile_expr1(&p, cctx) == FAIL)
3284 goto theend;
3285
3286 if (idx >= 0 && (is_decl || !has_type))
3287 {
3288 garray_T *stack = &cctx->ctx_type_stack;
3289 type_T *stacktype =
3290 ((type_T **)stack->ga_data)[stack->ga_len - 1];
3291
3292 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3293 if (!has_type)
3294 {
3295 if (stacktype->tt_type == VAR_VOID)
3296 {
3297 emsg(_("E1031: Cannot use void value"));
3298 goto theend;
3299 }
3300 else
3301 lvar->lv_type = stacktype;
3302 }
3303 else
3304 if (check_type(lvar->lv_type, stacktype, TRUE) == FAIL)
3305 goto theend;
3306 }
3307 }
3308 else if (cmdidx == CMD_const)
3309 {
3310 emsg(_("E1021: const requires a value"));
3311 goto theend;
3312 }
3313 else if (!has_type || option)
3314 {
3315 emsg(_("E1022: type or initialization required"));
3316 goto theend;
3317 }
3318 else
3319 {
3320 // variables are always initialized
3321 // TODO: support more types
3322 if (ga_grow(instr, 1) == FAIL)
3323 goto theend;
3324 if (type->tt_type == VAR_STRING)
3325 generate_PUSHS(cctx, vim_strsave((char_u *)""));
3326 else
3327 generate_PUSHNR(cctx, 0);
3328 }
3329
3330 if (oplen > 0 && *op != '=')
3331 {
3332 type_T *expected = &t_number;
3333 garray_T *stack = &cctx->ctx_type_stack;
3334 type_T *stacktype;
3335
3336 // TODO: if type is known use float or any operation
3337
3338 if (*op == '.')
3339 expected = &t_string;
3340 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3341 if (need_type(stacktype, expected, -1, cctx) == FAIL)
3342 goto theend;
3343
3344 if (*op == '.')
3345 generate_instr_drop(cctx, ISN_CONCAT, 1);
3346 else
3347 {
3348 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3349
3350 if (isn == NULL)
3351 goto theend;
3352 switch (*op)
3353 {
3354 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
3355 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
3356 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
3357 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
3358 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
3359 }
3360 }
3361 }
3362
3363 if (option)
3364 generate_STOREOPT(cctx, name + 1, opt_flags);
3365 else if (global)
3366 generate_STORE(cctx, ISN_STOREG, 0, name + 2);
3367 else if (script)
3368 {
3369 idx = get_script_item_idx(current_sctx.sc_sid, name, TRUE);
3370 // TODO: specific type
3371 generate_SCRIPT(cctx, ISN_STORESCRIPT,
3372 current_sctx.sc_sid, idx, &t_any);
3373 }
3374 else
3375 {
3376 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3377
3378 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
3379 // ISN_STORENR
3380 if (instr->ga_len == instr_count + 1 && isn->isn_type == ISN_PUSHNR)
3381 {
3382 varnumber_T val = isn->isn_arg.number;
3383 garray_T *stack = &cctx->ctx_type_stack;
3384
3385 isn->isn_type = ISN_STORENR;
3386 isn->isn_arg.storenr.str_idx = idx;
3387 isn->isn_arg.storenr.str_val = val;
3388 if (stack->ga_len > 0)
3389 --stack->ga_len;
3390 }
3391 else
3392 generate_STORE(cctx, ISN_STORE, idx, NULL);
3393 }
3394 ret = p;
3395
3396theend:
3397 vim_free(name);
3398 return ret;
3399}
3400
3401/*
3402 * Compile an :import command.
3403 */
3404 static char_u *
3405compile_import(char_u *arg, cctx_T *cctx)
3406{
3407 return handle_import(arg, &cctx->ctx_imports, 0);
3408}
3409
3410/*
3411 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
3412 */
3413 static int
3414compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
3415{
3416 garray_T *instr = &cctx->ctx_instr;
3417 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
3418
3419 if (endlabel == NULL)
3420 return FAIL;
3421 endlabel->el_next = *el;
3422 *el = endlabel;
3423 endlabel->el_end_label = instr->ga_len;
3424
3425 generate_JUMP(cctx, when, 0);
3426 return OK;
3427}
3428
3429 static void
3430compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
3431{
3432 garray_T *instr = &cctx->ctx_instr;
3433
3434 while (*el != NULL)
3435 {
3436 endlabel_T *cur = (*el);
3437 isn_T *isn;
3438
3439 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
3440 isn->isn_arg.jump.jump_where = instr->ga_len;
3441 *el = cur->el_next;
3442 vim_free(cur);
3443 }
3444}
3445
3446/*
3447 * Create a new scope and set up the generic items.
3448 */
3449 static scope_T *
3450new_scope(cctx_T *cctx, scopetype_T type)
3451{
3452 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
3453
3454 if (scope == NULL)
3455 return NULL;
3456 scope->se_outer = cctx->ctx_scope;
3457 cctx->ctx_scope = scope;
3458 scope->se_type = type;
3459 scope->se_local_count = cctx->ctx_locals.ga_len;
3460 return scope;
3461}
3462
3463/*
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003464 * Evaluate an expression that is a constant: has(arg)
3465 * Return FAIL if the expression is not a constant.
3466 */
3467 static int
3468evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
3469{
3470 typval_T argvars[2];
3471
3472 if (STRNCMP("has(", *arg, 4) != 0)
3473 return FAIL;
3474 *arg = skipwhite(*arg + 4);
3475
3476 if (**arg == '"')
3477 {
3478 if (get_string_tv(arg, tv, TRUE) == FAIL)
3479 return FAIL;
3480 }
3481 else if (**arg == '\'')
3482 {
3483 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
3484 return FAIL;
3485 }
3486 else
3487 return FAIL;
3488
3489 *arg = skipwhite(*arg);
3490 if (**arg != ')')
3491 return FAIL;
3492 *arg = skipwhite(*arg + 1);
3493
3494 argvars[0] = *tv;
3495 argvars[1].v_type = VAR_UNKNOWN;
3496 tv->v_type = VAR_NUMBER;
3497 tv->vval.v_number = 0;
3498 f_has(argvars, tv);
3499 clear_tv(&argvars[0]);
3500
3501 return OK;
3502}
3503
3504static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
3505
3506/*
3507 * Compile constant || or &&.
3508 */
3509 static int
3510evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
3511{
3512 char_u *p = skipwhite(*arg);
3513 int opchar = *op;
3514
3515 if (p[0] == opchar && p[1] == opchar)
3516 {
3517 int val = tv2bool(tv);
3518
3519 /*
3520 * Repeat until there is no following "||" or "&&"
3521 */
3522 while (p[0] == opchar && p[1] == opchar)
3523 {
3524 typval_T tv2;
3525
3526 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3527 return FAIL;
3528
3529 // eval the next expression
3530 *arg = skipwhite(p + 2);
3531 tv2.v_type = VAR_UNKNOWN;
3532 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
3533 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
3534 {
3535 clear_tv(&tv2);
3536 return FAIL;
3537 }
3538 if ((opchar == '&') == val)
3539 {
3540 // false || tv2 or true && tv2: use tv2
3541 clear_tv(tv);
3542 *tv = tv2;
3543 val = tv2bool(tv);
3544 }
3545 else
3546 clear_tv(&tv2);
3547 p = skipwhite(*arg);
3548 }
3549 }
3550
3551 return OK;
3552}
3553
3554/*
3555 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
3556 * Return FAIL if the expression is not a constant.
3557 */
3558 static int
3559evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
3560{
3561 // evaluate the first expression
3562 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
3563 return FAIL;
3564
3565 // || and && work almost the same
3566 return evaluate_const_and_or(arg, cctx, "&&", tv);
3567}
3568
3569/*
3570 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
3571 * Return FAIL if the expression is not a constant.
3572 */
3573 static int
3574evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
3575{
3576 // evaluate the first expression
3577 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
3578 return FAIL;
3579
3580 // || and && work almost the same
3581 return evaluate_const_and_or(arg, cctx, "||", tv);
3582}
3583
3584/*
3585 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
3586 * E.g. for "has('feature')".
3587 * This does not produce error messages. "tv" should be cleared afterwards.
3588 * Return FAIL if the expression is not a constant.
3589 */
3590 static int
3591evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
3592{
3593 char_u *p;
3594
3595 // evaluate the first expression
3596 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
3597 return FAIL;
3598
3599 p = skipwhite(*arg);
3600 if (*p == '?')
3601 {
3602 int val = tv2bool(tv);
3603 typval_T tv2;
3604
3605 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3606 return FAIL;
3607
3608 // evaluate the second expression; any type is accepted
3609 clear_tv(tv);
3610 *arg = skipwhite(p + 1);
3611 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
3612 return FAIL;
3613
3614 // Check for the ":".
3615 p = skipwhite(*arg);
3616 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3617 return FAIL;
3618
3619 // evaluate the third expression
3620 *arg = skipwhite(p + 1);
3621 tv2.v_type = VAR_UNKNOWN;
3622 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
3623 {
3624 clear_tv(&tv2);
3625 return FAIL;
3626 }
3627 if (val)
3628 {
3629 // use the expr after "?"
3630 clear_tv(&tv2);
3631 }
3632 else
3633 {
3634 // use the expr after ":"
3635 clear_tv(tv);
3636 *tv = tv2;
3637 }
3638 }
3639 return OK;
3640}
3641
3642/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 * compile "if expr"
3644 *
3645 * "if expr" Produces instructions:
3646 * EVAL expr Push result of "expr"
3647 * JUMP_IF_FALSE end
3648 * ... body ...
3649 * end:
3650 *
3651 * "if expr | else" Produces instructions:
3652 * EVAL expr Push result of "expr"
3653 * JUMP_IF_FALSE else
3654 * ... body ...
3655 * JUMP_ALWAYS end
3656 * else:
3657 * ... body ...
3658 * end:
3659 *
3660 * "if expr1 | elseif expr2 | else" Produces instructions:
3661 * EVAL expr Push result of "expr"
3662 * JUMP_IF_FALSE elseif
3663 * ... body ...
3664 * JUMP_ALWAYS end
3665 * elseif:
3666 * EVAL expr Push result of "expr"
3667 * JUMP_IF_FALSE else
3668 * ... body ...
3669 * JUMP_ALWAYS end
3670 * else:
3671 * ... body ...
3672 * end:
3673 */
3674 static char_u *
3675compile_if(char_u *arg, cctx_T *cctx)
3676{
3677 char_u *p = arg;
3678 garray_T *instr = &cctx->ctx_instr;
3679 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003680 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003682 // compile "expr"; if we know it evaluates to FALSE skip the block
3683 tv.v_type = VAR_UNKNOWN;
3684 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
3685 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
3686 else
3687 cctx->ctx_skip = MAYBE;
3688 clear_tv(&tv);
3689 if (cctx->ctx_skip == MAYBE)
3690 {
3691 p = arg;
3692 if (compile_expr1(&p, cctx) == FAIL)
3693 return NULL;
3694 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695
3696 scope = new_scope(cctx, IF_SCOPE);
3697 if (scope == NULL)
3698 return NULL;
3699
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003700 if (cctx->ctx_skip == MAYBE)
3701 {
3702 // "where" is set when ":elseif", "else" or ":endif" is found
3703 scope->se_u.se_if.is_if_label = instr->ga_len;
3704 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3705 }
3706 else
3707 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708
3709 return p;
3710}
3711
3712 static char_u *
3713compile_elseif(char_u *arg, cctx_T *cctx)
3714{
3715 char_u *p = arg;
3716 garray_T *instr = &cctx->ctx_instr;
3717 isn_T *isn;
3718 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003719 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720
3721 if (scope == NULL || scope->se_type != IF_SCOPE)
3722 {
3723 emsg(_(e_elseif_without_if));
3724 return NULL;
3725 }
3726 cctx->ctx_locals.ga_len = scope->se_local_count;
3727
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003728 if (cctx->ctx_skip != TRUE)
3729 {
3730 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003732 return NULL;
3733 // previous "if" or "elseif" jumps here
3734 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
3735 isn->isn_arg.jump.jump_where = instr->ga_len;
3736 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003738 // compile "expr"; if we know it evaluates to FALSE skip the block
3739 tv.v_type = VAR_UNKNOWN;
3740 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
3741 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
3742 else
3743 cctx->ctx_skip = MAYBE;
3744 clear_tv(&tv);
3745 if (cctx->ctx_skip == MAYBE)
3746 {
3747 p = arg;
3748 if (compile_expr1(&p, cctx) == FAIL)
3749 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003751 // "where" is set when ":elseif", "else" or ":endif" is found
3752 scope->se_u.se_if.is_if_label = instr->ga_len;
3753 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3754 }
3755 else
3756 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757
3758 return p;
3759}
3760
3761 static char_u *
3762compile_else(char_u *arg, cctx_T *cctx)
3763{
3764 char_u *p = arg;
3765 garray_T *instr = &cctx->ctx_instr;
3766 isn_T *isn;
3767 scope_T *scope = cctx->ctx_scope;
3768
3769 if (scope == NULL || scope->se_type != IF_SCOPE)
3770 {
3771 emsg(_(e_else_without_if));
3772 return NULL;
3773 }
3774 cctx->ctx_locals.ga_len = scope->se_local_count;
3775
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003776 // jump from previous block to the end, unless the else block is empty
3777 if (cctx->ctx_skip == MAYBE)
3778 {
3779 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003781 return NULL;
3782 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003784 if (cctx->ctx_skip != TRUE)
3785 {
3786 if (scope->se_u.se_if.is_if_label >= 0)
3787 {
3788 // previous "if" or "elseif" jumps here
3789 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
3790 isn->isn_arg.jump.jump_where = instr->ga_len;
3791 }
3792 }
3793
3794 if (cctx->ctx_skip != MAYBE)
3795 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796
3797 return p;
3798}
3799
3800 static char_u *
3801compile_endif(char_u *arg, cctx_T *cctx)
3802{
3803 scope_T *scope = cctx->ctx_scope;
3804 ifscope_T *ifscope;
3805 garray_T *instr = &cctx->ctx_instr;
3806 isn_T *isn;
3807
3808 if (scope == NULL || scope->se_type != IF_SCOPE)
3809 {
3810 emsg(_(e_endif_without_if));
3811 return NULL;
3812 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01003813 ifscope = &scope->se_u.se_if;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 cctx->ctx_scope = scope->se_outer;
3815 cctx->ctx_locals.ga_len = scope->se_local_count;
3816
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003817 if (scope->se_u.se_if.is_if_label >= 0)
3818 {
3819 // previous "if" or "elseif" jumps here
3820 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
3821 isn->isn_arg.jump.jump_where = instr->ga_len;
3822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 // Fill in the "end" label in jumps at the end of the blocks.
3824 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003825 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826
3827 vim_free(scope);
3828 return arg;
3829}
3830
3831/*
3832 * compile "for var in expr"
3833 *
3834 * Produces instructions:
3835 * PUSHNR -1
3836 * STORE loop-idx Set index to -1
3837 * EVAL expr Push result of "expr"
3838 * top: FOR loop-idx, end Increment index, use list on bottom of stack
3839 * - if beyond end, jump to "end"
3840 * - otherwise get item from list and push it
3841 * STORE var Store item in "var"
3842 * ... body ...
3843 * JUMP top Jump back to repeat
3844 * end: DROP Drop the result of "expr"
3845 *
3846 */
3847 static char_u *
3848compile_for(char_u *arg, cctx_T *cctx)
3849{
3850 char_u *p;
3851 size_t varlen;
3852 garray_T *instr = &cctx->ctx_instr;
3853 garray_T *stack = &cctx->ctx_type_stack;
3854 scope_T *scope;
3855 int loop_idx; // index of loop iteration variable
3856 int var_idx; // index of "var"
3857 type_T *vartype;
3858
3859 // TODO: list of variables: "for [key, value] in dict"
3860 // parse "var"
3861 for (p = arg; eval_isnamec1(*p); ++p)
3862 ;
3863 varlen = p - arg;
3864 var_idx = lookup_local(arg, varlen, cctx);
3865 if (var_idx >= 0)
3866 {
3867 semsg(_("E1023: variable already defined: %s"), arg);
3868 return NULL;
3869 }
3870
3871 // consume "in"
3872 p = skipwhite(p);
3873 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
3874 {
3875 emsg(_(e_missing_in));
3876 return NULL;
3877 }
3878 p = skipwhite(p + 2);
3879
3880
3881 scope = new_scope(cctx, FOR_SCOPE);
3882 if (scope == NULL)
3883 return NULL;
3884
3885 // Reserve a variable to store the loop iteration counter.
3886 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
3887 if (loop_idx < 0)
3888 return NULL;
3889
3890 // Reserve a variable to store "var"
3891 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
3892 if (var_idx < 0)
3893 return NULL;
3894
3895 generate_STORENR(cctx, loop_idx, -1);
3896
3897 // compile "expr", it remains on the stack until "endfor"
3898 arg = p;
3899 if (compile_expr1(&arg, cctx) == FAIL)
3900 return NULL;
3901
3902 // now we know the type of "var"
3903 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3904 if (vartype->tt_type != VAR_LIST)
3905 {
3906 emsg(_("E1024: need a List to iterate over"));
3907 return NULL;
3908 }
3909 if (vartype->tt_member->tt_type != VAR_UNKNOWN)
3910 {
3911 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
3912
3913 lvar->lv_type = vartype->tt_member;
3914 }
3915
3916 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01003917 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918
3919 generate_FOR(cctx, loop_idx);
3920 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
3921
3922 return arg;
3923}
3924
3925/*
3926 * compile "endfor"
3927 */
3928 static char_u *
3929compile_endfor(char_u *arg, cctx_T *cctx)
3930{
3931 garray_T *instr = &cctx->ctx_instr;
3932 scope_T *scope = cctx->ctx_scope;
3933 forscope_T *forscope;
3934 isn_T *isn;
3935
3936 if (scope == NULL || scope->se_type != FOR_SCOPE)
3937 {
3938 emsg(_(e_for));
3939 return NULL;
3940 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01003941 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 cctx->ctx_scope = scope->se_outer;
3943 cctx->ctx_locals.ga_len = scope->se_local_count;
3944
3945 // At end of ":for" scope jump back to the FOR instruction.
3946 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
3947
3948 // Fill in the "end" label in the FOR statement so it can jump here
3949 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
3950 isn->isn_arg.forloop.for_end = instr->ga_len;
3951
3952 // Fill in the "end" label any BREAK statements
3953 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
3954
3955 // Below the ":for" scope drop the "expr" list from the stack.
3956 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
3957 return NULL;
3958
3959 vim_free(scope);
3960
3961 return arg;
3962}
3963
3964/*
3965 * compile "while expr"
3966 *
3967 * Produces instructions:
3968 * top: EVAL expr Push result of "expr"
3969 * JUMP_IF_FALSE end jump if false
3970 * ... body ...
3971 * JUMP top Jump back to repeat
3972 * end:
3973 *
3974 */
3975 static char_u *
3976compile_while(char_u *arg, cctx_T *cctx)
3977{
3978 char_u *p = arg;
3979 garray_T *instr = &cctx->ctx_instr;
3980 scope_T *scope;
3981
3982 scope = new_scope(cctx, WHILE_SCOPE);
3983 if (scope == NULL)
3984 return NULL;
3985
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01003986 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987
3988 // compile "expr"
3989 if (compile_expr1(&p, cctx) == FAIL)
3990 return NULL;
3991
3992 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01003993 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 JUMP_IF_FALSE, cctx) == FAIL)
3995 return FAIL;
3996
3997 return p;
3998}
3999
4000/*
4001 * compile "endwhile"
4002 */
4003 static char_u *
4004compile_endwhile(char_u *arg, cctx_T *cctx)
4005{
4006 scope_T *scope = cctx->ctx_scope;
4007
4008 if (scope == NULL || scope->se_type != WHILE_SCOPE)
4009 {
4010 emsg(_(e_while));
4011 return NULL;
4012 }
4013 cctx->ctx_scope = scope->se_outer;
4014 cctx->ctx_locals.ga_len = scope->se_local_count;
4015
4016 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004017 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018
4019 // Fill in the "end" label in the WHILE statement so it can jump here.
4020 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004021 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022
4023 vim_free(scope);
4024
4025 return arg;
4026}
4027
4028/*
4029 * compile "continue"
4030 */
4031 static char_u *
4032compile_continue(char_u *arg, cctx_T *cctx)
4033{
4034 scope_T *scope = cctx->ctx_scope;
4035
4036 for (;;)
4037 {
4038 if (scope == NULL)
4039 {
4040 emsg(_(e_continue));
4041 return NULL;
4042 }
4043 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4044 break;
4045 scope = scope->se_outer;
4046 }
4047
4048 // Jump back to the FOR or WHILE instruction.
4049 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004050 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
4051 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 return arg;
4053}
4054
4055/*
4056 * compile "break"
4057 */
4058 static char_u *
4059compile_break(char_u *arg, cctx_T *cctx)
4060{
4061 scope_T *scope = cctx->ctx_scope;
4062 endlabel_T **el;
4063
4064 for (;;)
4065 {
4066 if (scope == NULL)
4067 {
4068 emsg(_(e_break));
4069 return NULL;
4070 }
4071 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
4072 break;
4073 scope = scope->se_outer;
4074 }
4075
4076 // Jump to the end of the FOR or WHILE loop.
4077 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004078 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004080 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
4082 return FAIL;
4083
4084 return arg;
4085}
4086
4087/*
4088 * compile "{" start of block
4089 */
4090 static char_u *
4091compile_block(char_u *arg, cctx_T *cctx)
4092{
4093 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4094 return NULL;
4095 return skipwhite(arg + 1);
4096}
4097
4098/*
4099 * compile end of block: drop one scope
4100 */
4101 static void
4102compile_endblock(cctx_T *cctx)
4103{
4104 scope_T *scope = cctx->ctx_scope;
4105
4106 cctx->ctx_scope = scope->se_outer;
4107 cctx->ctx_locals.ga_len = scope->se_local_count;
4108 vim_free(scope);
4109}
4110
4111/*
4112 * compile "try"
4113 * Creates a new scope for the try-endtry, pointing to the first catch and
4114 * finally.
4115 * Creates another scope for the "try" block itself.
4116 * TRY instruction sets up exception handling at runtime.
4117 *
4118 * "try"
4119 * TRY -> catch1, -> finally push trystack entry
4120 * ... try block
4121 * "throw {exception}"
4122 * EVAL {exception}
4123 * THROW create exception
4124 * ... try block
4125 * " catch {expr}"
4126 * JUMP -> finally
4127 * catch1: PUSH exeception
4128 * EVAL {expr}
4129 * MATCH
4130 * JUMP nomatch -> catch2
4131 * CATCH remove exception
4132 * ... catch block
4133 * " catch"
4134 * JUMP -> finally
4135 * catch2: CATCH remove exception
4136 * ... catch block
4137 * " finally"
4138 * finally:
4139 * ... finally block
4140 * " endtry"
4141 * ENDTRY pop trystack entry, may rethrow
4142 */
4143 static char_u *
4144compile_try(char_u *arg, cctx_T *cctx)
4145{
4146 garray_T *instr = &cctx->ctx_instr;
4147 scope_T *try_scope;
4148 scope_T *scope;
4149
4150 // scope that holds the jumps that go to catch/finally/endtry
4151 try_scope = new_scope(cctx, TRY_SCOPE);
4152 if (try_scope == NULL)
4153 return NULL;
4154
4155 // "catch" is set when the first ":catch" is found.
4156 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004157 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 if (generate_instr(cctx, ISN_TRY) == NULL)
4159 return NULL;
4160
4161 // scope for the try block itself
4162 scope = new_scope(cctx, BLOCK_SCOPE);
4163 if (scope == NULL)
4164 return NULL;
4165
4166 return arg;
4167}
4168
4169/*
4170 * compile "catch {expr}"
4171 */
4172 static char_u *
4173compile_catch(char_u *arg, cctx_T *cctx UNUSED)
4174{
4175 scope_T *scope = cctx->ctx_scope;
4176 garray_T *instr = &cctx->ctx_instr;
4177 char_u *p;
4178 isn_T *isn;
4179
4180 // end block scope from :try or :catch
4181 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4182 compile_endblock(cctx);
4183 scope = cctx->ctx_scope;
4184
4185 // Error if not in a :try scope
4186 if (scope == NULL || scope->se_type != TRY_SCOPE)
4187 {
4188 emsg(_(e_catch));
4189 return NULL;
4190 }
4191
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004192 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 {
4194 emsg(_("E1033: catch unreachable after catch-all"));
4195 return NULL;
4196 }
4197
4198 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004199 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200 JUMP_ALWAYS, cctx) == FAIL)
4201 return NULL;
4202
4203 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004204 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 if (isn->isn_arg.try.try_catch == 0)
4206 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004207 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208 {
4209 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004210 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 isn->isn_arg.jump.jump_where = instr->ga_len;
4212 }
4213
4214 p = skipwhite(arg);
4215 if (ends_excmd(*p))
4216 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004217 scope->se_u.se_try.ts_caught_all = TRUE;
4218 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 }
4220 else
4221 {
4222 // Push v:exception, push {expr} and MATCH
4223 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
4224
4225 if (compile_expr1(&p, cctx) == FAIL)
4226 return NULL;
4227
4228 // TODO: check for strings?
4229 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
4230 return NULL;
4231
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004232 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
4234 return NULL;
4235 }
4236
4237 if (generate_instr(cctx, ISN_CATCH) == NULL)
4238 return NULL;
4239
4240 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
4241 return NULL;
4242 return p;
4243}
4244
4245 static char_u *
4246compile_finally(char_u *arg, cctx_T *cctx)
4247{
4248 scope_T *scope = cctx->ctx_scope;
4249 garray_T *instr = &cctx->ctx_instr;
4250 isn_T *isn;
4251
4252 // end block scope from :try or :catch
4253 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4254 compile_endblock(cctx);
4255 scope = cctx->ctx_scope;
4256
4257 // Error if not in a :try scope
4258 if (scope == NULL || scope->se_type != TRY_SCOPE)
4259 {
4260 emsg(_(e_finally));
4261 return NULL;
4262 }
4263
4264 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004265 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 if (isn->isn_arg.try.try_finally != 0)
4267 {
4268 emsg(_(e_finally_dup));
4269 return NULL;
4270 }
4271
4272 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004273 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004275 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 {
4277 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004278 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 isn->isn_arg.jump.jump_where = instr->ga_len;
4280 }
4281
4282 isn->isn_arg.try.try_finally = instr->ga_len;
4283 // TODO: set index in ts_finally_label jumps
4284
4285 return arg;
4286}
4287
4288 static char_u *
4289compile_endtry(char_u *arg, cctx_T *cctx)
4290{
4291 scope_T *scope = cctx->ctx_scope;
4292 garray_T *instr = &cctx->ctx_instr;
4293 isn_T *isn;
4294
4295 // end block scope from :catch or :finally
4296 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
4297 compile_endblock(cctx);
4298 scope = cctx->ctx_scope;
4299
4300 // Error if not in a :try scope
4301 if (scope == NULL || scope->se_type != TRY_SCOPE)
4302 {
4303 if (scope == NULL)
4304 emsg(_(e_no_endtry));
4305 else if (scope->se_type == WHILE_SCOPE)
4306 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01004307 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 emsg(_(e_endfor));
4309 else
4310 emsg(_(e_endif));
4311 return NULL;
4312 }
4313
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004314 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
4316 {
4317 emsg(_("E1032: missing :catch or :finally"));
4318 return NULL;
4319 }
4320
4321 // Fill in the "end" label in jumps at the end of the blocks, if not done
4322 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004323 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324
4325 // End :catch or :finally scope: set value in ISN_TRY instruction
4326 if (isn->isn_arg.try.try_finally == 0)
4327 isn->isn_arg.try.try_finally = instr->ga_len;
4328 compile_endblock(cctx);
4329
4330 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
4331 return NULL;
4332 return arg;
4333}
4334
4335/*
4336 * compile "throw {expr}"
4337 */
4338 static char_u *
4339compile_throw(char_u *arg, cctx_T *cctx UNUSED)
4340{
4341 char_u *p = skipwhite(arg);
4342
4343 if (ends_excmd(*p))
4344 {
4345 emsg(_(e_argreq));
4346 return NULL;
4347 }
4348 if (compile_expr1(&p, cctx) == FAIL)
4349 return NULL;
4350 if (may_generate_2STRING(-1, cctx) == FAIL)
4351 return NULL;
4352 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
4353 return NULL;
4354
4355 return p;
4356}
4357
4358/*
4359 * compile "echo expr"
4360 */
4361 static char_u *
4362compile_echo(char_u *arg, int with_white, cctx_T *cctx)
4363{
4364 char_u *p = arg;
4365 int count = 0;
4366
4367 // for ()
4368 {
4369 if (compile_expr1(&p, cctx) == FAIL)
4370 return NULL;
4371 ++count;
4372 }
4373
4374 generate_ECHO(cctx, with_white, count);
4375
4376 return p;
4377}
4378
4379/*
4380 * After ex_function() has collected all the function lines: parse and compile
4381 * the lines into instructions.
4382 * Adds the function to "def_functions".
4383 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
4384 * return statement (used for lambda).
4385 */
4386 void
4387compile_def_function(ufunc_T *ufunc, int set_return_type)
4388{
4389 dfunc_T *dfunc;
4390 char_u *line = NULL;
4391 char_u *p;
4392 exarg_T ea;
4393 char *errormsg = NULL; // error message
4394 int had_return = FALSE;
4395 cctx_T cctx;
4396 garray_T *instr;
4397 int called_emsg_before = called_emsg;
4398 int ret = FAIL;
4399 sctx_T save_current_sctx = current_sctx;
4400
4401 if (ufunc->uf_dfunc_idx >= 0)
4402 {
4403 // redefining a function that was compiled before
4404 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4405 dfunc->df_deleted = FALSE;
4406 }
4407 else
4408 {
4409 // Add the function to "def_functions".
4410 if (ga_grow(&def_functions, 1) == FAIL)
4411 return;
4412 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
4413 vim_memset(dfunc, 0, sizeof(dfunc_T));
4414 dfunc->df_idx = def_functions.ga_len;
4415 ufunc->uf_dfunc_idx = dfunc->df_idx;
4416 dfunc->df_ufunc = ufunc;
4417 ++def_functions.ga_len;
4418 }
4419
4420 vim_memset(&cctx, 0, sizeof(cctx));
4421 cctx.ctx_ufunc = ufunc;
4422 cctx.ctx_lnum = -1;
4423 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
4424 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
4425 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
4426 cctx.ctx_type_list = &ufunc->uf_type_list;
4427 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
4428 instr = &cctx.ctx_instr;
4429
4430 // Most modern script version.
4431 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4432
4433 for (;;)
4434 {
4435 if (line != NULL && *line == '|')
4436 // the line continues after a '|'
4437 ++line;
4438 else if (line != NULL && *line != NUL)
4439 {
4440 semsg(_("E488: Trailing characters: %s"), line);
4441 goto erret;
4442 }
4443 else
4444 {
4445 do
4446 {
4447 ++cctx.ctx_lnum;
4448 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
4449 break;
4450 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
4451 } while (line == NULL);
4452 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
4453 break;
4454 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
4455 }
4456
4457 had_return = FALSE;
4458 vim_memset(&ea, 0, sizeof(ea));
4459 ea.cmdlinep = &line;
4460 ea.cmd = skipwhite(line);
4461
4462 // "}" ends a block scope
4463 if (*ea.cmd == '}')
4464 {
4465 scopetype_T stype = cctx.ctx_scope == NULL
4466 ? NO_SCOPE : cctx.ctx_scope->se_type;
4467
4468 if (stype == BLOCK_SCOPE)
4469 {
4470 compile_endblock(&cctx);
4471 line = ea.cmd;
4472 }
4473 else
4474 {
4475 emsg("E1025: using } outside of a block scope");
4476 goto erret;
4477 }
4478 if (line != NULL)
4479 line = skipwhite(ea.cmd + 1);
4480 continue;
4481 }
4482
4483 // "{" starts a block scope
4484 if (*ea.cmd == '{')
4485 {
4486 line = compile_block(ea.cmd, &cctx);
4487 continue;
4488 }
4489
4490 /*
4491 * COMMAND MODIFIERS
4492 */
4493 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
4494 {
4495 if (errormsg != NULL)
4496 goto erret;
4497 // empty line or comment
4498 line = (char_u *)"";
4499 continue;
4500 }
4501
4502 // Skip ":call" to get to the function name.
4503 if (checkforcmd(&ea.cmd, "call", 3))
4504 ea.cmd = skipwhite(ea.cmd);
4505
4506 // Assuming the command starts with a variable or function name, find
4507 // what follows. Also "&opt = value".
4508 p = (*ea.cmd == '&') ? ea.cmd + 1 : ea.cmd;
4509 p = to_name_end(p);
4510 if (p > ea.cmd && *p != NUL)
4511 {
4512 int oplen;
4513 int heredoc;
4514
4515 // "funcname(" is always a function call.
4516 // "varname[]" is an expression.
4517 // "g:varname" is an expression.
4518 // "varname->expr" is an expression.
4519 if (*p == '('
4520 || *p == '['
4521 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
4522 || (*p == '-' && p[1] == '>'))
4523 {
4524 // TODO
4525 }
4526
4527 oplen = assignment_len(skipwhite(p), &heredoc);
4528 if (oplen > 0)
4529 {
4530 // Recognize an assignment if we recognize the variable name:
4531 // "g:var = expr"
4532 // "var = expr" where "var" is a local var name.
4533 // "&opt = expr"
4534 if (*ea.cmd == '&'
4535 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
4536 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
4537 || lookup_script(ea.cmd, p - ea.cmd) == OK)
4538 {
4539 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
4540 if (line == NULL)
4541 goto erret;
4542 continue;
4543 }
4544 }
4545 }
4546
4547 /*
4548 * COMMAND after range
4549 */
4550 ea.cmd = skip_range(ea.cmd, NULL);
4551 p = find_ex_command(&ea, NULL, lookup_local, &cctx);
4552
4553 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
4554 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004555 if (cctx.ctx_skip == TRUE)
4556 {
4557 line += STRLEN(line);
4558 continue;
4559 }
4560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561 // Expression or function call.
4562 if (ea.cmdidx == CMD_eval)
4563 {
4564 p = ea.cmd;
4565 if (compile_expr1(&p, &cctx) == FAIL)
4566 goto erret;
4567
4568 // drop the return value
4569 generate_instr_drop(&cctx, ISN_DROP, 1);
4570 line = p;
4571 continue;
4572 }
4573 if (ea.cmdidx == CMD_let)
4574 {
4575 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
4576 if (line == NULL)
4577 goto erret;
4578 continue;
4579 }
4580 iemsg("Command from find_ex_command() not handled");
4581 goto erret;
4582 }
4583
4584 p = skipwhite(p);
4585
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004586 if (cctx.ctx_skip == TRUE
4587 && ea.cmdidx != CMD_elseif
4588 && ea.cmdidx != CMD_else
4589 && ea.cmdidx != CMD_endif)
4590 {
4591 line += STRLEN(line);
4592 continue;
4593 }
4594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595 switch (ea.cmdidx)
4596 {
4597 case CMD_def:
4598 case CMD_function:
4599 // TODO: Nested function
4600 emsg("Nested function not implemented yet");
4601 goto erret;
4602
4603 case CMD_return:
4604 line = compile_return(p, set_return_type, &cctx);
4605 had_return = TRUE;
4606 break;
4607
4608 case CMD_let:
4609 case CMD_const:
4610 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
4611 break;
4612
4613 case CMD_import:
4614 line = compile_import(p, &cctx);
4615 break;
4616
4617 case CMD_if:
4618 line = compile_if(p, &cctx);
4619 break;
4620 case CMD_elseif:
4621 line = compile_elseif(p, &cctx);
4622 break;
4623 case CMD_else:
4624 line = compile_else(p, &cctx);
4625 break;
4626 case CMD_endif:
4627 line = compile_endif(p, &cctx);
4628 break;
4629
4630 case CMD_while:
4631 line = compile_while(p, &cctx);
4632 break;
4633 case CMD_endwhile:
4634 line = compile_endwhile(p, &cctx);
4635 break;
4636
4637 case CMD_for:
4638 line = compile_for(p, &cctx);
4639 break;
4640 case CMD_endfor:
4641 line = compile_endfor(p, &cctx);
4642 break;
4643 case CMD_continue:
4644 line = compile_continue(p, &cctx);
4645 break;
4646 case CMD_break:
4647 line = compile_break(p, &cctx);
4648 break;
4649
4650 case CMD_try:
4651 line = compile_try(p, &cctx);
4652 break;
4653 case CMD_catch:
4654 line = compile_catch(p, &cctx);
4655 break;
4656 case CMD_finally:
4657 line = compile_finally(p, &cctx);
4658 break;
4659 case CMD_endtry:
4660 line = compile_endtry(p, &cctx);
4661 break;
4662 case CMD_throw:
4663 line = compile_throw(p, &cctx);
4664 break;
4665
4666 case CMD_echo:
4667 line = compile_echo(p, TRUE, &cctx);
4668 break;
4669 case CMD_echon:
4670 line = compile_echo(p, FALSE, &cctx);
4671 break;
4672
4673 default:
4674 // Not recognized, execute with do_cmdline_cmd().
4675 generate_EXEC(&cctx, line);
4676 line = (char_u *)"";
4677 break;
4678 }
4679 if (line == NULL)
4680 goto erret;
4681
4682 if (cctx.ctx_type_stack.ga_len < 0)
4683 {
4684 iemsg("Type stack underflow");
4685 goto erret;
4686 }
4687 }
4688
4689 if (cctx.ctx_scope != NULL)
4690 {
4691 if (cctx.ctx_scope->se_type == IF_SCOPE)
4692 emsg(_(e_endif));
4693 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
4694 emsg(_(e_endwhile));
4695 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
4696 emsg(_(e_endfor));
4697 else
4698 emsg(_("E1026: Missing }"));
4699 goto erret;
4700 }
4701
4702 if (!had_return)
4703 {
4704 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
4705 {
4706 emsg(_("E1027: Missing return statement"));
4707 goto erret;
4708 }
4709
4710 // Return zero if there is no return at the end.
4711 generate_PUSHNR(&cctx, 0);
4712 generate_instr(&cctx, ISN_RETURN);
4713 }
4714
4715 dfunc->df_instr = instr->ga_data;
4716 dfunc->df_instr_count = instr->ga_len;
4717 dfunc->df_varcount = cctx.ctx_max_local;
4718
4719 ret = OK;
4720
4721erret:
4722 if (ret == FAIL)
4723 {
4724 ga_clear(instr);
4725 ufunc->uf_dfunc_idx = -1;
4726 --def_functions.ga_len;
4727 if (errormsg != NULL)
4728 emsg(errormsg);
4729 else if (called_emsg == called_emsg_before)
4730 emsg("E1028: compile_def_function failed");
4731
4732 // don't execute this function body
4733 ufunc->uf_lines.ga_len = 0;
4734 }
4735
4736 current_sctx = save_current_sctx;
4737 ga_clear(&cctx.ctx_type_stack);
4738 ga_clear(&cctx.ctx_locals);
4739}
4740
4741/*
4742 * Delete an instruction, free what it contains.
4743 */
4744 static void
4745delete_instr(isn_T *isn)
4746{
4747 switch (isn->isn_type)
4748 {
4749 case ISN_EXEC:
4750 case ISN_LOADENV:
4751 case ISN_LOADG:
4752 case ISN_LOADOPT:
4753 case ISN_MEMBER:
4754 case ISN_PUSHEXC:
4755 case ISN_PUSHS:
4756 case ISN_STOREG:
4757 vim_free(isn->isn_arg.string);
4758 break;
4759
4760 case ISN_LOADS:
4761 vim_free(isn->isn_arg.loads.ls_name);
4762 break;
4763
4764 case ISN_STOREOPT:
4765 vim_free(isn->isn_arg.storeopt.so_name);
4766 break;
4767
4768 case ISN_PUSHBLOB: // push blob isn_arg.blob
4769 blob_unref(isn->isn_arg.blob);
4770 break;
4771
4772 case ISN_UCALL:
4773 vim_free(isn->isn_arg.ufunc.cuf_name);
4774 break;
4775
4776 case ISN_2BOOL:
4777 case ISN_2STRING:
4778 case ISN_ADDBLOB:
4779 case ISN_ADDLIST:
4780 case ISN_BCALL:
4781 case ISN_CATCH:
4782 case ISN_CHECKNR:
4783 case ISN_CHECKTYPE:
4784 case ISN_COMPAREANY:
4785 case ISN_COMPAREBLOB:
4786 case ISN_COMPAREBOOL:
4787 case ISN_COMPAREDICT:
4788 case ISN_COMPAREFLOAT:
4789 case ISN_COMPAREFUNC:
4790 case ISN_COMPARELIST:
4791 case ISN_COMPARENR:
4792 case ISN_COMPAREPARTIAL:
4793 case ISN_COMPARESPECIAL:
4794 case ISN_COMPARESTRING:
4795 case ISN_CONCAT:
4796 case ISN_DCALL:
4797 case ISN_DROP:
4798 case ISN_ECHO:
4799 case ISN_ENDTRY:
4800 case ISN_FOR:
4801 case ISN_FUNCREF:
4802 case ISN_INDEX:
4803 case ISN_JUMP:
4804 case ISN_LOAD:
4805 case ISN_LOADSCRIPT:
4806 case ISN_LOADREG:
4807 case ISN_LOADV:
4808 case ISN_NEGATENR:
4809 case ISN_NEWDICT:
4810 case ISN_NEWLIST:
4811 case ISN_OPNR:
4812 case ISN_OPFLOAT:
4813 case ISN_OPANY:
4814 case ISN_PCALL:
4815 case ISN_PUSHF:
4816 case ISN_PUSHNR:
4817 case ISN_PUSHBOOL:
4818 case ISN_PUSHSPEC:
4819 case ISN_RETURN:
4820 case ISN_STORE:
4821 case ISN_STORENR:
4822 case ISN_STORESCRIPT:
4823 case ISN_THROW:
4824 case ISN_TRY:
4825 // nothing allocated
4826 break;
4827 }
4828}
4829
4830/*
4831 * When a user function is deleted, delete any associated def function.
4832 */
4833 void
4834delete_def_function(ufunc_T *ufunc)
4835{
4836 int idx;
4837
4838 if (ufunc->uf_dfunc_idx >= 0)
4839 {
4840 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4841 + ufunc->uf_dfunc_idx;
4842 ga_clear(&dfunc->df_def_args_isn);
4843
4844 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
4845 delete_instr(dfunc->df_instr + idx);
4846 VIM_CLEAR(dfunc->df_instr);
4847
4848 dfunc->df_deleted = TRUE;
4849 }
4850}
4851
4852#if defined(EXITFREE) || defined(PROTO)
4853 void
4854free_def_functions(void)
4855{
4856 vim_free(def_functions.ga_data);
4857}
4858#endif
4859
4860
4861#endif // FEAT_EVAL