xref: /vim-8.2.3635/src/vim9compile.c (revision da58134e)
1 /* 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 // values for ctx_skip
27 typedef enum {
28     SKIP_NOT,		// condition is a constant, produce code
29     SKIP_YES,		// condition is a constant, do NOT produce code
30     SKIP_UNKNOWN	// condition is not a constant, produce code
31 } skip_T;
32 
33 /*
34  * Chain of jump instructions where the end label needs to be set.
35  */
36 typedef struct endlabel_S endlabel_T;
37 struct endlabel_S {
38     endlabel_T	*el_next;	    // chain end_label locations
39     int		el_end_label;	    // instruction idx where to set end
40 };
41 
42 /*
43  * info specific for the scope of :if / elseif / else
44  */
45 typedef struct {
46     int		is_seen_else;
47     int		is_had_return;	    // every block ends in :return
48     int		is_if_label;	    // instruction idx at IF or ELSEIF
49     endlabel_T	*is_end_label;	    // instructions to set end label
50 } ifscope_T;
51 
52 /*
53  * info specific for the scope of :while
54  */
55 typedef struct {
56     int		ws_top_label;	    // instruction idx at WHILE
57     endlabel_T	*ws_end_label;	    // instructions to set end
58 } whilescope_T;
59 
60 /*
61  * info specific for the scope of :for
62  */
63 typedef struct {
64     int		fs_top_label;	    // instruction idx at FOR
65     endlabel_T	*fs_end_label;	    // break instructions
66 } forscope_T;
67 
68 /*
69  * info specific for the scope of :try
70  */
71 typedef struct {
72     int		ts_try_label;	    // instruction idx at TRY
73     endlabel_T	*ts_end_label;	    // jump to :finally or :endtry
74     int		ts_catch_label;	    // instruction idx of last CATCH
75     int		ts_caught_all;	    // "catch" without argument encountered
76 } tryscope_T;
77 
78 typedef enum {
79     NO_SCOPE,
80     IF_SCOPE,
81     WHILE_SCOPE,
82     FOR_SCOPE,
83     TRY_SCOPE,
84     BLOCK_SCOPE
85 } scopetype_T;
86 
87 /*
88  * Info for one scope, pointed to by "ctx_scope".
89  */
90 typedef struct scope_S scope_T;
91 struct scope_S {
92     scope_T	*se_outer;	    // scope containing this one
93     scopetype_T se_type;
94     int		se_local_count;	    // ctx_locals.ga_len before scope
95     skip_T	se_skip_save;	    // ctx_skip before the block
96     union {
97 	ifscope_T	se_if;
98 	whilescope_T	se_while;
99 	forscope_T	se_for;
100 	tryscope_T	se_try;
101     } se_u;
102 };
103 
104 /*
105  * Entry for "ctx_locals".  Used for arguments and local variables.
106  */
107 typedef struct {
108     char_u	*lv_name;
109     type_T	*lv_type;
110     int		lv_idx;		// index of the variable on the stack
111     int		lv_from_outer;	// when TRUE using ctx_outer scope
112     int		lv_const;	// when TRUE cannot be assigned to
113     int		lv_arg;		// when TRUE this is an argument
114 } lvar_T;
115 
116 /*
117  * Context for compiling lines of Vim script.
118  * Stores info about the local variables and condition stack.
119  */
120 struct cctx_S {
121     ufunc_T	*ctx_ufunc;	    // current function
122     int		ctx_lnum;	    // line number in current function
123     char_u	*ctx_line_start;    // start of current line or NULL
124     garray_T	ctx_instr;	    // generated instructions
125 
126     garray_T	ctx_locals;	    // currently visible local variables
127     int		ctx_locals_count;   // total number of local variables
128 
129     int		ctx_closure_count;  // number of closures created in the
130 				    // function
131 
132     garray_T	ctx_imports;	    // imported items
133 
134     skip_T	ctx_skip;
135     scope_T	*ctx_scope;	    // current scope, NULL at toplevel
136     int		ctx_had_return;	    // last seen statement was "return"
137 
138     cctx_T	*ctx_outer;	    // outer scope for lambda or nested
139 				    // function
140     int		ctx_outer_used;	    // var in ctx_outer was used
141 
142     garray_T	ctx_type_stack;	    // type of each item on the stack
143     garray_T	*ctx_type_list;	    // list of pointers to allocated types
144 };
145 
146 static char e_var_notfound[] = N_("E1001: variable not found: %s");
147 static char e_syntax_at[] = N_("E1002: Syntax error at %s");
148 static char e_used_as_arg[] = N_("E1006: %s is used as an argument");
149 static char e_cannot_use_void[] = N_("E1031: Cannot use void value");
150 
151 static void delete_def_function_contents(dfunc_T *dfunc);
152 static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
153 
154 /*
155  * Lookup variable "name" in the local scope and return it.
156  * Return NULL if not found.
157  */
158     static lvar_T *
159 lookup_local(char_u *name, size_t len, cctx_T *cctx)
160 {
161     int	    idx;
162     lvar_T  *lvar;
163 
164     if (len == 0)
165 	return NULL;
166 
167     // Find local in current function scope.
168     for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
169     {
170 	lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
171 	if (STRNCMP(name, lvar->lv_name, len) == 0
172 					       && STRLEN(lvar->lv_name) == len)
173 	{
174 	    lvar->lv_from_outer = FALSE;
175 	    return lvar;
176 	}
177     }
178 
179     // Find local in outer function scope.
180     if (cctx->ctx_outer != NULL)
181     {
182 	lvar = lookup_local(name, len, cctx->ctx_outer);
183 	if (lvar != NULL)
184 	{
185 	    // TODO: are there situations we should not mark the outer scope as
186 	    // used?
187 	    cctx->ctx_outer_used = TRUE;
188 	    lvar->lv_from_outer = TRUE;
189 	    return lvar;
190 	}
191     }
192 
193     return NULL;
194 }
195 
196 /*
197  * Lookup an argument in the current function and an enclosing function.
198  * Returns the argument index in "idxp"
199  * Returns the argument type in "type"
200  * Sets "gen_load_outer" to TRUE if found in outer scope.
201  * Returns OK when found, FAIL otherwise.
202  */
203     static int
204 lookup_arg(
205 	char_u	*name,
206 	size_t	len,
207 	int	*idxp,
208 	type_T	**type,
209 	int	*gen_load_outer,
210 	cctx_T	*cctx)
211 {
212     int	    idx;
213     char_u  *va_name;
214 
215     if (len == 0)
216 	return FAIL;
217     for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
218     {
219 	char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
220 
221 	if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
222 	{
223 	    if (idxp != NULL)
224 	    {
225 		// Arguments are located above the frame pointer.  One further
226 		// if there is a vararg argument
227 		*idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
228 							    + STACK_FRAME_SIZE)
229 			      + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
230 
231 		if (cctx->ctx_ufunc->uf_arg_types != NULL)
232 		    *type = cctx->ctx_ufunc->uf_arg_types[idx];
233 		else
234 		    *type = &t_any;
235 	    }
236 	    return OK;
237 	}
238     }
239 
240     va_name = cctx->ctx_ufunc->uf_va_name;
241     if (va_name != NULL
242 		    && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
243     {
244 	if (idxp != NULL)
245 	{
246 	    // varargs is always the last argument
247 	    *idxp = -STACK_FRAME_SIZE - 1;
248 	    *type = cctx->ctx_ufunc->uf_va_type;
249 	}
250 	return OK;
251     }
252 
253     if (cctx->ctx_outer != NULL)
254     {
255 	// Lookup the name for an argument of the outer function.
256 	if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
257 									 == OK)
258 	{
259 	    *gen_load_outer = TRUE;
260 	    return OK;
261 	}
262     }
263 
264     return FAIL;
265 }
266 
267 /*
268  * Lookup a variable in the current script.
269  * Returns OK or FAIL.
270  */
271     static int
272 lookup_script(char_u *name, size_t len)
273 {
274     int		    cc;
275     hashtab_T	    *ht = &SCRIPT_VARS(current_sctx.sc_sid);
276     dictitem_T	    *di;
277 
278     cc = name[len];
279     name[len] = NUL;
280     di = find_var_in_ht(ht, 0, name, TRUE);
281     name[len] = cc;
282     return di == NULL ? FAIL: OK;
283 }
284 
285 /*
286  * Check if "p[len]" is already defined, either in script "import_sid" or in
287  * compilation context "cctx".
288  * Return FAIL and give an error if it defined.
289  */
290     int
291 check_defined(char_u *p, int len, cctx_T *cctx)
292 {
293     if (lookup_script(p, len) == OK
294 	    || (cctx != NULL
295 		&& (lookup_local(p, len, cctx) != NULL
296 		    || find_imported(p, len, cctx) != NULL)))
297     {
298 	semsg("E1073: imported name already defined: %s", p);
299 	return FAIL;
300     }
301     return OK;
302 }
303 
304 /*
305  * Allocate memory for a type_T and add the pointer to type_gap, so that it can
306  * be freed later.
307  */
308     static type_T *
309 alloc_type(garray_T *type_gap)
310 {
311     type_T *type;
312 
313     if (ga_grow(type_gap, 1) == FAIL)
314 	return NULL;
315     type = ALLOC_CLEAR_ONE(type_T);
316     if (type != NULL)
317     {
318 	((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
319 	++type_gap->ga_len;
320     }
321     return type;
322 }
323 
324     static type_T *
325 get_list_type(type_T *member_type, garray_T *type_gap)
326 {
327     type_T *type;
328 
329     // recognize commonly used types
330     if (member_type->tt_type == VAR_ANY)
331 	return &t_list_any;
332     if (member_type->tt_type == VAR_VOID
333 	    || member_type->tt_type == VAR_UNKNOWN)
334 	return &t_list_empty;
335     if (member_type->tt_type == VAR_BOOL)
336 	return &t_list_bool;
337     if (member_type->tt_type == VAR_NUMBER)
338 	return &t_list_number;
339     if (member_type->tt_type == VAR_STRING)
340 	return &t_list_string;
341 
342     // Not a common type, create a new entry.
343     type = alloc_type(type_gap);
344     if (type == NULL)
345 	return &t_any;
346     type->tt_type = VAR_LIST;
347     type->tt_member = member_type;
348     type->tt_argcount = 0;
349     type->tt_args = NULL;
350     return type;
351 }
352 
353     static type_T *
354 get_dict_type(type_T *member_type, garray_T *type_gap)
355 {
356     type_T *type;
357 
358     // recognize commonly used types
359     if (member_type->tt_type == VAR_ANY)
360 	return &t_dict_any;
361     if (member_type->tt_type == VAR_VOID
362 	    || member_type->tt_type == VAR_UNKNOWN)
363 	return &t_dict_empty;
364     if (member_type->tt_type == VAR_BOOL)
365 	return &t_dict_bool;
366     if (member_type->tt_type == VAR_NUMBER)
367 	return &t_dict_number;
368     if (member_type->tt_type == VAR_STRING)
369 	return &t_dict_string;
370 
371     // Not a common type, create a new entry.
372     type = alloc_type(type_gap);
373     if (type == NULL)
374 	return &t_any;
375     type->tt_type = VAR_DICT;
376     type->tt_member = member_type;
377     type->tt_argcount = 0;
378     type->tt_args = NULL;
379     return type;
380 }
381 
382 /*
383  * Allocate a new type for a function.
384  */
385     static type_T *
386 alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
387 {
388     type_T *type = alloc_type(type_gap);
389 
390     if (type == NULL)
391 	return &t_any;
392     type->tt_type = VAR_FUNC;
393     type->tt_member = ret_type;
394     type->tt_argcount = argcount;
395     type->tt_args = NULL;
396     return type;
397 }
398 
399 /*
400  * Get a function type, based on the return type "ret_type".
401  * If "argcount" is -1 or 0 a predefined type can be used.
402  * If "argcount" > 0 always create a new type, so that arguments can be added.
403  */
404     static type_T *
405 get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
406 {
407     // recognize commonly used types
408     if (argcount <= 0)
409     {
410 	if (ret_type == &t_unknown)
411 	{
412 	    // (argcount == 0) is not possible
413 	    return &t_func_unknown;
414 	}
415 	if (ret_type == &t_void)
416 	{
417 	    if (argcount == 0)
418 		return &t_func_0_void;
419 	    else
420 		return &t_func_void;
421 	}
422 	if (ret_type == &t_any)
423 	{
424 	    if (argcount == 0)
425 		return &t_func_0_any;
426 	    else
427 		return &t_func_any;
428 	}
429 	if (ret_type == &t_number)
430 	{
431 	    if (argcount == 0)
432 		return &t_func_0_number;
433 	    else
434 		return &t_func_number;
435 	}
436 	if (ret_type == &t_string)
437 	{
438 	    if (argcount == 0)
439 		return &t_func_0_string;
440 	    else
441 		return &t_func_string;
442 	}
443     }
444 
445     return alloc_func_type(ret_type, argcount, type_gap);
446 }
447 
448 /*
449  * For a function type, reserve space for "argcount" argument types (including
450  * vararg).
451  */
452     static int
453 func_type_add_arg_types(
454 	type_T	    *functype,
455 	int	    argcount,
456 	garray_T    *type_gap)
457 {
458     // To make it easy to free the space needed for the argument types, add the
459     // pointer to type_gap.
460     if (ga_grow(type_gap, 1) == FAIL)
461 	return FAIL;
462     functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
463     if (functype->tt_args == NULL)
464 	return FAIL;
465     ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
466 						     (void *)functype->tt_args;
467     ++type_gap->ga_len;
468     return OK;
469 }
470 
471 /*
472  * Return the type_T for a typval.  Only for primitive types.
473  */
474     type_T *
475 typval2type(typval_T *tv)
476 {
477     if (tv->v_type == VAR_NUMBER)
478 	return &t_number;
479     if (tv->v_type == VAR_BOOL)
480 	return &t_bool;  // not used
481     if (tv->v_type == VAR_STRING)
482 	return &t_string;
483     if (tv->v_type == VAR_LIST)  // e.g. for v:oldfiles
484 	return &t_list_string;
485     if (tv->v_type == VAR_DICT)  // e.g. for v:completed_item
486 	return &t_dict_any;
487     return &t_any;  // not used
488 }
489 
490     static void
491 type_mismatch(type_T *expected, type_T *actual)
492 {
493     char *tofree1, *tofree2;
494 
495     semsg(_("E1013: type mismatch, expected %s but got %s"),
496 		   type_name(expected, &tofree1), type_name(actual, &tofree2));
497     vim_free(tofree1);
498     vim_free(tofree2);
499 }
500 
501     static void
502 arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
503 {
504     char *tofree1, *tofree2;
505 
506     semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
507 	    argidx,
508 	    type_name(expected, &tofree1), type_name(actual, &tofree2));
509     vim_free(tofree1);
510     vim_free(tofree2);
511 }
512 
513 /*
514  * Check if the expected and actual types match.
515  * Does not allow for assigning "any" to a specific type.
516  */
517     int
518 check_type(type_T *expected, type_T *actual, int give_msg)
519 {
520     int ret = OK;
521 
522     // When expected is "unknown" we accept any actual type.
523     // When expected is "any" we accept any actual type except "void".
524     if (expected->tt_type != VAR_UNKNOWN
525 	    && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
526 
527     {
528 	if (expected->tt_type != actual->tt_type)
529 	{
530 	    if (give_msg)
531 		type_mismatch(expected, actual);
532 	    return FAIL;
533 	}
534 	if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
535 	{
536 	    // "unknown" is used for an empty list or dict
537 	    if (actual->tt_member != &t_unknown)
538 		ret = check_type(expected->tt_member, actual->tt_member, FALSE);
539 	}
540 	else if (expected->tt_type == VAR_FUNC)
541 	{
542 	    if (expected->tt_member != &t_unknown)
543 		ret = check_type(expected->tt_member, actual->tt_member, FALSE);
544 	    if (ret == OK && expected->tt_argcount != -1
545 		    && (actual->tt_argcount < expected->tt_min_argcount
546 			|| actual->tt_argcount > expected->tt_argcount))
547 		    ret = FAIL;
548 	}
549 	if (ret == FAIL && give_msg)
550 	    type_mismatch(expected, actual);
551     }
552     return ret;
553 }
554 
555 /////////////////////////////////////////////////////////////////////
556 // Following generate_ functions expect the caller to call ga_grow().
557 
558 #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
559 #define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
560 
561 /*
562  * Generate an instruction without arguments.
563  * Returns a pointer to the new instruction, NULL if failed.
564  */
565     static isn_T *
566 generate_instr(cctx_T *cctx, isntype_T isn_type)
567 {
568     garray_T	*instr = &cctx->ctx_instr;
569     isn_T	*isn;
570 
571     RETURN_NULL_IF_SKIP(cctx);
572     if (ga_grow(instr, 1) == FAIL)
573 	return NULL;
574     isn = ((isn_T *)instr->ga_data) + instr->ga_len;
575     isn->isn_type = isn_type;
576     isn->isn_lnum = cctx->ctx_lnum + 1;
577     ++instr->ga_len;
578 
579     return isn;
580 }
581 
582 /*
583  * Generate an instruction without arguments.
584  * "drop" will be removed from the stack.
585  * Returns a pointer to the new instruction, NULL if failed.
586  */
587     static isn_T *
588 generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
589 {
590     garray_T	*stack = &cctx->ctx_type_stack;
591 
592     RETURN_NULL_IF_SKIP(cctx);
593     stack->ga_len -= drop;
594     return generate_instr(cctx, isn_type);
595 }
596 
597 /*
598  * Generate instruction "isn_type" and put "type" on the type stack.
599  */
600     static isn_T *
601 generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
602 {
603     isn_T	*isn;
604     garray_T	*stack = &cctx->ctx_type_stack;
605 
606     if ((isn = generate_instr(cctx, isn_type)) == NULL)
607 	return NULL;
608 
609     if (ga_grow(stack, 1) == FAIL)
610 	return NULL;
611     ((type_T **)stack->ga_data)[stack->ga_len] = type;
612     ++stack->ga_len;
613 
614     return isn;
615 }
616 
617 /*
618  * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
619  */
620     static int
621 may_generate_2STRING(int offset, cctx_T *cctx)
622 {
623     isn_T	*isn;
624     garray_T	*stack = &cctx->ctx_type_stack;
625     type_T	**type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
626 
627     if ((*type)->tt_type == VAR_STRING)
628 	return OK;
629     *type = &t_string;
630 
631     if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
632 	return FAIL;
633     isn->isn_arg.number = offset;
634 
635     return OK;
636 }
637 
638     static int
639 check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
640 {
641     if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
642 	    && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
643 							 || type2 == VAR_ANY)))
644     {
645 	if (*op == '+')
646 	    emsg(_("E1035: wrong argument type for +"));
647 	else
648 	    semsg(_("E1036: %c requires number or float arguments"), *op);
649 	return FAIL;
650     }
651     return OK;
652 }
653 
654 /*
655  * Generate an instruction with two arguments.  The instruction depends on the
656  * type of the arguments.
657  */
658     static int
659 generate_two_op(cctx_T *cctx, char_u *op)
660 {
661     garray_T	*stack = &cctx->ctx_type_stack;
662     type_T	*type1;
663     type_T	*type2;
664     vartype_T	vartype;
665     isn_T	*isn;
666 
667     RETURN_OK_IF_SKIP(cctx);
668 
669     // Get the known type of the two items on the stack.  If they are matching
670     // use a type-specific instruction. Otherwise fall back to runtime type
671     // checking.
672     type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
673     type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
674     vartype = VAR_ANY;
675     if (type1->tt_type == type2->tt_type
676 	    && (type1->tt_type == VAR_NUMBER
677 		|| type1->tt_type == VAR_LIST
678 #ifdef FEAT_FLOAT
679 		|| type1->tt_type == VAR_FLOAT
680 #endif
681 		|| type1->tt_type == VAR_BLOB))
682 	vartype = type1->tt_type;
683 
684     switch (*op)
685     {
686 	case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
687 			  && type1->tt_type != VAR_ANY
688 			  && type2->tt_type != VAR_ANY
689 			  && check_number_or_float(
690 				   type1->tt_type, type2->tt_type, op) == FAIL)
691 		      return FAIL;
692 		  isn = generate_instr_drop(cctx,
693 			    vartype == VAR_NUMBER ? ISN_OPNR
694 			  : vartype == VAR_LIST ? ISN_ADDLIST
695 			  : vartype == VAR_BLOB ? ISN_ADDBLOB
696 #ifdef FEAT_FLOAT
697 			  : vartype == VAR_FLOAT ? ISN_OPFLOAT
698 #endif
699 			  : ISN_OPANY, 1);
700 		  if (isn != NULL)
701 		      isn->isn_arg.op.op_type = EXPR_ADD;
702 		  break;
703 
704 	case '-':
705 	case '*':
706 	case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
707 								   op) == FAIL)
708 		      return FAIL;
709 		  if (vartype == VAR_NUMBER)
710 		      isn = generate_instr_drop(cctx, ISN_OPNR, 1);
711 #ifdef FEAT_FLOAT
712 		  else if (vartype == VAR_FLOAT)
713 		      isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
714 #endif
715 		  else
716 		      isn = generate_instr_drop(cctx, ISN_OPANY, 1);
717 		  if (isn != NULL)
718 		      isn->isn_arg.op.op_type = *op == '*'
719 				 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
720 		  break;
721 
722 	case '%': if ((type1->tt_type != VAR_ANY
723 					       && type1->tt_type != VAR_NUMBER)
724 			  || (type2->tt_type != VAR_ANY
725 					      && type2->tt_type != VAR_NUMBER))
726 		  {
727 		      emsg(_("E1035: % requires number arguments"));
728 		      return FAIL;
729 		  }
730 		  isn = generate_instr_drop(cctx,
731 			      vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
732 		  if (isn != NULL)
733 		      isn->isn_arg.op.op_type = EXPR_REM;
734 		  break;
735     }
736 
737     // correct type of result
738     if (vartype == VAR_ANY)
739     {
740 	type_T *type = &t_any;
741 
742 #ifdef FEAT_FLOAT
743 	// float+number and number+float results in float
744 	if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
745 		&& (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
746 	    type = &t_float;
747 #endif
748 	((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
749     }
750 
751     return OK;
752 }
753 
754 /*
755  * Get the instruction to use for comparing "type1" with "type2"
756  * Return ISN_DROP when failed.
757  */
758     static isntype_T
759 get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
760 {
761     isntype_T	isntype = ISN_DROP;
762 
763     if (type1 == VAR_UNKNOWN)
764 	type1 = VAR_ANY;
765     if (type2 == VAR_UNKNOWN)
766 	type2 = VAR_ANY;
767 
768     if (type1 == type2)
769     {
770 	switch (type1)
771 	{
772 	    case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
773 	    case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
774 	    case VAR_NUMBER: isntype = ISN_COMPARENR; break;
775 	    case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
776 	    case VAR_STRING: isntype = ISN_COMPARESTRING; break;
777 	    case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
778 	    case VAR_LIST: isntype = ISN_COMPARELIST; break;
779 	    case VAR_DICT: isntype = ISN_COMPAREDICT; break;
780 	    case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
781 	    default: isntype = ISN_COMPAREANY; break;
782 	}
783     }
784     else if (type1 == VAR_ANY || type2 == VAR_ANY
785 	    || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
786 	      && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
787 	isntype = ISN_COMPAREANY;
788 
789     if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
790 	    && (isntype == ISN_COMPAREBOOL
791 	    || isntype == ISN_COMPARESPECIAL
792 	    || isntype == ISN_COMPARENR
793 	    || isntype == ISN_COMPAREFLOAT))
794     {
795 	semsg(_("E1037: Cannot use \"%s\" with %s"),
796 		exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
797 	return ISN_DROP;
798     }
799     if (isntype == ISN_DROP
800 	    || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
801 		    && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
802 		       || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
803 	    || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
804 				 && exptype != EXPR_IS && exptype != EXPR_ISNOT
805 		    && (type1 == VAR_BLOB || type2 == VAR_BLOB
806 			|| type1 == VAR_LIST || type2 == VAR_LIST))))
807     {
808 	semsg(_("E1072: Cannot compare %s with %s"),
809 		vartype_name(type1), vartype_name(type2));
810 	return ISN_DROP;
811     }
812     return isntype;
813 }
814 
815 /*
816  * Generate an ISN_COMPARE* instruction with a boolean result.
817  */
818     static int
819 generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
820 {
821     isntype_T	isntype;
822     isn_T	*isn;
823     garray_T	*stack = &cctx->ctx_type_stack;
824     vartype_T	type1;
825     vartype_T	type2;
826 
827     RETURN_OK_IF_SKIP(cctx);
828 
829     // Get the known type of the two items on the stack.  If they are matching
830     // use a type-specific instruction. Otherwise fall back to runtime type
831     // checking.
832     type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
833     type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
834     isntype = get_compare_isn(exptype, type1, type2);
835     if (isntype == ISN_DROP)
836 	return FAIL;
837 
838     if ((isn = generate_instr(cctx, isntype)) == NULL)
839 	return FAIL;
840     isn->isn_arg.op.op_type = exptype;
841     isn->isn_arg.op.op_ic = ic;
842 
843     // takes two arguments, puts one bool back
844     if (stack->ga_len >= 2)
845     {
846 	--stack->ga_len;
847 	((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
848     }
849 
850     return OK;
851 }
852 
853 /*
854  * Generate an ISN_2BOOL instruction.
855  */
856     static int
857 generate_2BOOL(cctx_T *cctx, int invert)
858 {
859     isn_T	*isn;
860     garray_T	*stack = &cctx->ctx_type_stack;
861 
862     RETURN_OK_IF_SKIP(cctx);
863     if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
864 	return FAIL;
865     isn->isn_arg.number = invert;
866 
867     // type becomes bool
868     ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
869 
870     return OK;
871 }
872 
873     static int
874 generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
875 {
876     isn_T	*isn;
877     garray_T	*stack = &cctx->ctx_type_stack;
878 
879     RETURN_OK_IF_SKIP(cctx);
880     if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
881 	return FAIL;
882     // TODO: whole type, e.g. for a function also arg and return types
883     isn->isn_arg.type.ct_type = vartype->tt_type;
884     isn->isn_arg.type.ct_off = offset;
885 
886     // type becomes vartype
887     ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
888 
889     return OK;
890 }
891 
892 /*
893  * Check that
894  * - "actual" is "expected" type or
895  * - "actual" is a type that can be "expected" type: add a runtime check; or
896  * - return FAIL.
897  */
898     static int
899 need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
900 {
901     if (check_type(expected, actual, FALSE) == OK)
902 	return OK;
903     if (actual->tt_type != VAR_ANY
904 	    && actual->tt_type != VAR_UNKNOWN
905 	    && !(actual->tt_type == VAR_FUNC
906 		&& (actual->tt_member == &t_any || actual->tt_argcount < 0)))
907     {
908 	type_mismatch(expected, actual);
909 	return FAIL;
910     }
911     generate_TYPECHECK(cctx, expected, offset);
912     return OK;
913 }
914 
915 /*
916  * Generate an ISN_PUSHNR instruction.
917  */
918     static int
919 generate_PUSHNR(cctx_T *cctx, varnumber_T number)
920 {
921     isn_T	*isn;
922 
923     RETURN_OK_IF_SKIP(cctx);
924     if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
925 	return FAIL;
926     isn->isn_arg.number = number;
927 
928     return OK;
929 }
930 
931 /*
932  * Generate an ISN_PUSHBOOL instruction.
933  */
934     static int
935 generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
936 {
937     isn_T	*isn;
938 
939     RETURN_OK_IF_SKIP(cctx);
940     if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
941 	return FAIL;
942     isn->isn_arg.number = number;
943 
944     return OK;
945 }
946 
947 /*
948  * Generate an ISN_PUSHSPEC instruction.
949  */
950     static int
951 generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
952 {
953     isn_T	*isn;
954 
955     RETURN_OK_IF_SKIP(cctx);
956     if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
957 	return FAIL;
958     isn->isn_arg.number = number;
959 
960     return OK;
961 }
962 
963 #ifdef FEAT_FLOAT
964 /*
965  * Generate an ISN_PUSHF instruction.
966  */
967     static int
968 generate_PUSHF(cctx_T *cctx, float_T fnumber)
969 {
970     isn_T	*isn;
971 
972     RETURN_OK_IF_SKIP(cctx);
973     if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
974 	return FAIL;
975     isn->isn_arg.fnumber = fnumber;
976 
977     return OK;
978 }
979 #endif
980 
981 /*
982  * Generate an ISN_PUSHS instruction.
983  * Consumes "str".
984  */
985     static int
986 generate_PUSHS(cctx_T *cctx, char_u *str)
987 {
988     isn_T	*isn;
989 
990     RETURN_OK_IF_SKIP(cctx);
991     if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
992 	return FAIL;
993     isn->isn_arg.string = str;
994 
995     return OK;
996 }
997 
998 /*
999  * Generate an ISN_PUSHCHANNEL instruction.
1000  * Consumes "channel".
1001  */
1002     static int
1003 generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1004 {
1005     isn_T	*isn;
1006 
1007     RETURN_OK_IF_SKIP(cctx);
1008     if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1009 	return FAIL;
1010     isn->isn_arg.channel = channel;
1011 
1012     return OK;
1013 }
1014 
1015 /*
1016  * Generate an ISN_PUSHJOB instruction.
1017  * Consumes "job".
1018  */
1019     static int
1020 generate_PUSHJOB(cctx_T *cctx, job_T *job)
1021 {
1022     isn_T	*isn;
1023 
1024     RETURN_OK_IF_SKIP(cctx);
1025     if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
1026 	return FAIL;
1027     isn->isn_arg.job = job;
1028 
1029     return OK;
1030 }
1031 
1032 /*
1033  * Generate an ISN_PUSHBLOB instruction.
1034  * Consumes "blob".
1035  */
1036     static int
1037 generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1038 {
1039     isn_T	*isn;
1040 
1041     RETURN_OK_IF_SKIP(cctx);
1042     if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1043 	return FAIL;
1044     isn->isn_arg.blob = blob;
1045 
1046     return OK;
1047 }
1048 
1049 /*
1050  * Generate an ISN_PUSHFUNC instruction with name "name".
1051  * Consumes "name".
1052  */
1053     static int
1054 generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
1055 {
1056     isn_T	*isn;
1057 
1058     RETURN_OK_IF_SKIP(cctx);
1059     if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
1060 	return FAIL;
1061     isn->isn_arg.string = name;
1062 
1063     return OK;
1064 }
1065 
1066 /*
1067  * Generate an ISN_GETITEM instruction with "index".
1068  */
1069     static int
1070 generate_GETITEM(cctx_T *cctx, int index)
1071 {
1072     isn_T	*isn;
1073     garray_T	*stack = &cctx->ctx_type_stack;
1074     type_T	*type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1075     type_T	*item_type = &t_any;
1076 
1077     RETURN_OK_IF_SKIP(cctx);
1078 
1079     if (type->tt_type == VAR_LIST)
1080 	item_type = type->tt_member;
1081     else if (type->tt_type != VAR_ANY)
1082     {
1083 	emsg(_(e_listreq));
1084 	return FAIL;
1085     }
1086     if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1087 	return FAIL;
1088     isn->isn_arg.number = index;
1089 
1090     // add the item type to the type stack
1091     if (ga_grow(stack, 1) == FAIL)
1092 	return FAIL;
1093     ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1094     ++stack->ga_len;
1095     return OK;
1096 }
1097 
1098 /*
1099  * Generate an ISN_SLICE instruction with "count".
1100  */
1101     static int
1102 generate_SLICE(cctx_T *cctx, int count)
1103 {
1104     isn_T	*isn;
1105 
1106     RETURN_OK_IF_SKIP(cctx);
1107     if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1108 	return FAIL;
1109     isn->isn_arg.number = count;
1110     return OK;
1111 }
1112 
1113 /*
1114  * Generate an ISN_CHECKLEN instruction with "min_len".
1115  */
1116     static int
1117 generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1118 {
1119     isn_T	*isn;
1120 
1121     RETURN_OK_IF_SKIP(cctx);
1122 
1123     if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1124 	return FAIL;
1125     isn->isn_arg.checklen.cl_min_len = min_len;
1126     isn->isn_arg.checklen.cl_more_OK = more_OK;
1127 
1128     return OK;
1129 }
1130 
1131 /*
1132  * Generate an ISN_STORE instruction.
1133  */
1134     static int
1135 generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1136 {
1137     isn_T	*isn;
1138 
1139     RETURN_OK_IF_SKIP(cctx);
1140     if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1141 	return FAIL;
1142     if (name != NULL)
1143 	isn->isn_arg.string = vim_strsave(name);
1144     else
1145 	isn->isn_arg.number = idx;
1146 
1147     return OK;
1148 }
1149 
1150 /*
1151  * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1152  */
1153     static int
1154 generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1155 {
1156     isn_T	*isn;
1157 
1158     RETURN_OK_IF_SKIP(cctx);
1159     if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1160 	return FAIL;
1161     isn->isn_arg.storenr.stnr_idx = idx;
1162     isn->isn_arg.storenr.stnr_val = value;
1163 
1164     return OK;
1165 }
1166 
1167 /*
1168  * Generate an ISN_STOREOPT instruction
1169  */
1170     static int
1171 generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1172 {
1173     isn_T	*isn;
1174 
1175     RETURN_OK_IF_SKIP(cctx);
1176     if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1177 	return FAIL;
1178     isn->isn_arg.storeopt.so_name = vim_strsave(name);
1179     isn->isn_arg.storeopt.so_flags = opt_flags;
1180 
1181     return OK;
1182 }
1183 
1184 /*
1185  * Generate an ISN_LOAD or similar instruction.
1186  */
1187     static int
1188 generate_LOAD(
1189 	cctx_T	    *cctx,
1190 	isntype_T   isn_type,
1191 	int	    idx,
1192 	char_u	    *name,
1193 	type_T	    *type)
1194 {
1195     isn_T	*isn;
1196 
1197     RETURN_OK_IF_SKIP(cctx);
1198     if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1199 	return FAIL;
1200     if (name != NULL)
1201 	isn->isn_arg.string = vim_strsave(name);
1202     else
1203 	isn->isn_arg.number = idx;
1204 
1205     return OK;
1206 }
1207 
1208 /*
1209  * Generate an ISN_LOADV instruction for v:var.
1210  */
1211     static int
1212 generate_LOADV(
1213 	cctx_T	    *cctx,
1214 	char_u	    *name,
1215 	int	    error)
1216 {
1217     int	    di_flags;
1218     int	    vidx = find_vim_var(name, &di_flags);
1219     type_T  *type;
1220 
1221     RETURN_OK_IF_SKIP(cctx);
1222     if (vidx < 0)
1223     {
1224 	if (error)
1225 	    semsg(_(e_var_notfound), name);
1226 	return FAIL;
1227     }
1228     type = typval2type(get_vim_var_tv(vidx));
1229 
1230     return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1231 }
1232 
1233 /*
1234  * Generate an ISN_UNLET instruction.
1235  */
1236     static int
1237 generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1238 {
1239     isn_T	*isn;
1240 
1241     RETURN_OK_IF_SKIP(cctx);
1242     if ((isn = generate_instr(cctx, isn_type)) == NULL)
1243 	return FAIL;
1244     isn->isn_arg.unlet.ul_name = vim_strsave(name);
1245     isn->isn_arg.unlet.ul_forceit = forceit;
1246 
1247     return OK;
1248 }
1249 
1250 /*
1251  * Generate an ISN_LOADS instruction.
1252  */
1253     static int
1254 generate_OLDSCRIPT(
1255 	cctx_T	    *cctx,
1256 	isntype_T   isn_type,
1257 	char_u	    *name,
1258 	int	    sid,
1259 	type_T	    *type)
1260 {
1261     isn_T	*isn;
1262 
1263     RETURN_OK_IF_SKIP(cctx);
1264     if (isn_type == ISN_LOADS)
1265 	isn = generate_instr_type(cctx, isn_type, type);
1266     else
1267 	isn = generate_instr_drop(cctx, isn_type, 1);
1268     if (isn == NULL)
1269 	return FAIL;
1270     isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1271     isn->isn_arg.loadstore.ls_sid = sid;
1272 
1273     return OK;
1274 }
1275 
1276 /*
1277  * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1278  */
1279     static int
1280 generate_VIM9SCRIPT(
1281 	cctx_T	    *cctx,
1282 	isntype_T   isn_type,
1283 	int	    sid,
1284 	int	    idx,
1285 	type_T	    *type)
1286 {
1287     isn_T	*isn;
1288 
1289     RETURN_OK_IF_SKIP(cctx);
1290     if (isn_type == ISN_LOADSCRIPT)
1291 	isn = generate_instr_type(cctx, isn_type, type);
1292     else
1293 	isn = generate_instr_drop(cctx, isn_type, 1);
1294     if (isn == NULL)
1295 	return FAIL;
1296     isn->isn_arg.script.script_sid = sid;
1297     isn->isn_arg.script.script_idx = idx;
1298     return OK;
1299 }
1300 
1301 /*
1302  * Generate an ISN_NEWLIST instruction.
1303  */
1304     static int
1305 generate_NEWLIST(cctx_T *cctx, int count)
1306 {
1307     isn_T	*isn;
1308     garray_T	*stack = &cctx->ctx_type_stack;
1309     type_T	*type;
1310     type_T	*member;
1311 
1312     RETURN_OK_IF_SKIP(cctx);
1313     if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1314 	return FAIL;
1315     isn->isn_arg.number = count;
1316 
1317     // drop the value types
1318     stack->ga_len -= count;
1319 
1320     // Use the first value type for the list member type.  Use "any" for an
1321     // empty list.
1322     if (count > 0)
1323 	member = ((type_T **)stack->ga_data)[stack->ga_len];
1324     else
1325 	member = &t_void;
1326     type = get_list_type(member, cctx->ctx_type_list);
1327 
1328     // add the list type to the type stack
1329     if (ga_grow(stack, 1) == FAIL)
1330 	return FAIL;
1331     ((type_T **)stack->ga_data)[stack->ga_len] = type;
1332     ++stack->ga_len;
1333 
1334     return OK;
1335 }
1336 
1337 /*
1338  * Generate an ISN_NEWDICT instruction.
1339  */
1340     static int
1341 generate_NEWDICT(cctx_T *cctx, int count)
1342 {
1343     isn_T	*isn;
1344     garray_T	*stack = &cctx->ctx_type_stack;
1345     type_T	*type;
1346     type_T	*member;
1347 
1348     RETURN_OK_IF_SKIP(cctx);
1349     if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1350 	return FAIL;
1351     isn->isn_arg.number = count;
1352 
1353     // drop the key and value types
1354     stack->ga_len -= 2 * count;
1355 
1356     // Use the first value type for the list member type.  Use "void" for an
1357     // empty dict.
1358     if (count > 0)
1359 	member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1360     else
1361 	member = &t_void;
1362     type = get_dict_type(member, cctx->ctx_type_list);
1363 
1364     // add the dict type to the type stack
1365     if (ga_grow(stack, 1) == FAIL)
1366 	return FAIL;
1367     ((type_T **)stack->ga_data)[stack->ga_len] = type;
1368     ++stack->ga_len;
1369 
1370     return OK;
1371 }
1372 
1373 /*
1374  * Generate an ISN_FUNCREF instruction.
1375  */
1376     static int
1377 generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1378 {
1379     isn_T	*isn;
1380     garray_T	*stack = &cctx->ctx_type_stack;
1381 
1382     RETURN_OK_IF_SKIP(cctx);
1383     if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1384 	return FAIL;
1385     isn->isn_arg.funcref.fr_func = dfunc_idx;
1386     isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
1387 
1388     if (ga_grow(stack, 1) == FAIL)
1389 	return FAIL;
1390     ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
1391     // TODO: argument and return types
1392     ++stack->ga_len;
1393 
1394     return OK;
1395 }
1396 
1397 /*
1398  * Generate an ISN_JUMP instruction.
1399  */
1400     static int
1401 generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1402 {
1403     isn_T	*isn;
1404     garray_T	*stack = &cctx->ctx_type_stack;
1405 
1406     RETURN_OK_IF_SKIP(cctx);
1407     if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1408 	return FAIL;
1409     isn->isn_arg.jump.jump_when = when;
1410     isn->isn_arg.jump.jump_where = where;
1411 
1412     if (when != JUMP_ALWAYS && stack->ga_len > 0)
1413 	--stack->ga_len;
1414 
1415     return OK;
1416 }
1417 
1418     static int
1419 generate_FOR(cctx_T *cctx, int loop_idx)
1420 {
1421     isn_T	*isn;
1422     garray_T	*stack = &cctx->ctx_type_stack;
1423 
1424     RETURN_OK_IF_SKIP(cctx);
1425     if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1426 	return FAIL;
1427     isn->isn_arg.forloop.for_idx = loop_idx;
1428 
1429     if (ga_grow(stack, 1) == FAIL)
1430 	return FAIL;
1431     // type doesn't matter, will be stored next
1432     ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1433     ++stack->ga_len;
1434 
1435     return OK;
1436 }
1437 
1438 /*
1439  * Generate an ISN_BCALL instruction.
1440  * Return FAIL if the number of arguments is wrong.
1441  */
1442     static int
1443 generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1444 {
1445     isn_T	*isn;
1446     garray_T	*stack = &cctx->ctx_type_stack;
1447     type_T	*argtypes[MAX_FUNC_ARGS];
1448     int		i;
1449 
1450     RETURN_OK_IF_SKIP(cctx);
1451     if (check_internal_func(func_idx, argcount) == FAIL)
1452 	return FAIL;
1453 
1454     if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1455 	return FAIL;
1456     isn->isn_arg.bfunc.cbf_idx = func_idx;
1457     isn->isn_arg.bfunc.cbf_argcount = argcount;
1458 
1459     for (i = 0; i < argcount; ++i)
1460 	argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1461 
1462     stack->ga_len -= argcount; // drop the arguments
1463     if (ga_grow(stack, 1) == FAIL)
1464 	return FAIL;
1465     ((type_T **)stack->ga_data)[stack->ga_len] =
1466 			  internal_func_ret_type(func_idx, argcount, argtypes);
1467     ++stack->ga_len;	    // add return value
1468 
1469     return OK;
1470 }
1471 
1472 /*
1473  * Generate an ISN_DCALL or ISN_UCALL instruction.
1474  * Return FAIL if the number of arguments is wrong.
1475  */
1476     static int
1477 generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1478 {
1479     isn_T	*isn;
1480     garray_T	*stack = &cctx->ctx_type_stack;
1481     int		regular_args = ufunc->uf_args.ga_len;
1482     int		argcount = pushed_argcount;
1483 
1484     RETURN_OK_IF_SKIP(cctx);
1485     if (argcount > regular_args && !has_varargs(ufunc))
1486     {
1487 	semsg(_(e_toomanyarg), ufunc->uf_name);
1488 	return FAIL;
1489     }
1490     if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1491     {
1492 	semsg(_(e_toofewarg), ufunc->uf_name);
1493 	return FAIL;
1494     }
1495 
1496     if (ufunc->uf_def_status != UF_NOT_COMPILED)
1497     {
1498 	int		i;
1499 
1500 	for (i = 0; i < argcount; ++i)
1501 	{
1502 	    type_T *expected;
1503 	    type_T *actual;
1504 
1505 	    if (i < regular_args)
1506 	    {
1507 		if (ufunc->uf_arg_types == NULL)
1508 		    continue;
1509 		expected = ufunc->uf_arg_types[i];
1510 	    }
1511 	    else
1512 		expected = ufunc->uf_va_type->tt_member;
1513 	    actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1514 	    if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
1515 	    {
1516 		arg_type_mismatch(expected, actual, i + 1);
1517 		return FAIL;
1518 	    }
1519 	}
1520 	if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
1521 	    if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
1522 		return FAIL;
1523     }
1524 
1525     if ((isn = generate_instr(cctx,
1526 		    ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1527 							 : ISN_UCALL)) == NULL)
1528 	return FAIL;
1529     if (ufunc->uf_def_status != UF_NOT_COMPILED)
1530     {
1531 	isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1532 	isn->isn_arg.dfunc.cdf_argcount = argcount;
1533     }
1534     else
1535     {
1536 	// A user function may be deleted and redefined later, can't use the
1537 	// ufunc pointer, need to look it up again at runtime.
1538 	isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1539 	isn->isn_arg.ufunc.cuf_argcount = argcount;
1540     }
1541 
1542     stack->ga_len -= argcount; // drop the arguments
1543     if (ga_grow(stack, 1) == FAIL)
1544 	return FAIL;
1545     // add return value
1546     ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1547     ++stack->ga_len;
1548 
1549     return OK;
1550 }
1551 
1552 /*
1553  * Generate an ISN_UCALL instruction when the function isn't defined yet.
1554  */
1555     static int
1556 generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1557 {
1558     isn_T	*isn;
1559     garray_T	*stack = &cctx->ctx_type_stack;
1560 
1561     RETURN_OK_IF_SKIP(cctx);
1562     if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1563 	return FAIL;
1564     isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1565     isn->isn_arg.ufunc.cuf_argcount = argcount;
1566 
1567     stack->ga_len -= argcount; // drop the arguments
1568     if (ga_grow(stack, 1) == FAIL)
1569 	return FAIL;
1570     // add return value
1571     ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1572     ++stack->ga_len;
1573 
1574     return OK;
1575 }
1576 
1577 /*
1578  * Generate an ISN_PCALL instruction.
1579  * "type" is the type of the FuncRef.
1580  */
1581     static int
1582 generate_PCALL(
1583 	cctx_T	*cctx,
1584 	int	argcount,
1585 	char_u	*name,
1586 	type_T	*type,
1587 	int	at_top)
1588 {
1589     isn_T	*isn;
1590     garray_T	*stack = &cctx->ctx_type_stack;
1591     type_T	*ret_type;
1592 
1593     RETURN_OK_IF_SKIP(cctx);
1594 
1595     if (type->tt_type == VAR_ANY)
1596 	ret_type = &t_any;
1597     else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1598     {
1599 	if (type->tt_argcount != -1)
1600 	{
1601 	    int	    varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1602 
1603 	    if (argcount < type->tt_min_argcount - varargs)
1604 	    {
1605 		semsg(_(e_toofewarg), "[reference]");
1606 		return FAIL;
1607 	    }
1608 	    if (!varargs && argcount > type->tt_argcount)
1609 	    {
1610 		semsg(_(e_toomanyarg), "[reference]");
1611 		return FAIL;
1612 	    }
1613 	}
1614 	ret_type = type->tt_member;
1615     }
1616     else
1617     {
1618 	semsg(_("E1085: Not a callable type: %s"), name);
1619 	return FAIL;
1620     }
1621 
1622     if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1623 	return FAIL;
1624     isn->isn_arg.pfunc.cpf_top = at_top;
1625     isn->isn_arg.pfunc.cpf_argcount = argcount;
1626 
1627     stack->ga_len -= argcount; // drop the arguments
1628 
1629     // drop the funcref/partial, get back the return value
1630     ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
1631 
1632     // If partial is above the arguments it must be cleared and replaced with
1633     // the return value.
1634     if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1635 	return FAIL;
1636 
1637     return OK;
1638 }
1639 
1640 /*
1641  * Generate an ISN_STRINGMEMBER instruction.
1642  */
1643     static int
1644 generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1645 {
1646     isn_T	*isn;
1647     garray_T	*stack = &cctx->ctx_type_stack;
1648     type_T	*type;
1649 
1650     RETURN_OK_IF_SKIP(cctx);
1651     if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1652 	return FAIL;
1653     isn->isn_arg.string = vim_strnsave(name, len);
1654 
1655     // check for dict type
1656     type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1657     if (type->tt_type != VAR_DICT && type != &t_any)
1658     {
1659 	emsg(_(e_dictreq));
1660 	return FAIL;
1661     }
1662     // change dict type to dict member type
1663     if (type->tt_type == VAR_DICT)
1664 	((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
1665 
1666     return OK;
1667 }
1668 
1669 /*
1670  * Generate an ISN_ECHO instruction.
1671  */
1672     static int
1673 generate_ECHO(cctx_T *cctx, int with_white, int count)
1674 {
1675     isn_T	*isn;
1676 
1677     RETURN_OK_IF_SKIP(cctx);
1678     if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1679 	return FAIL;
1680     isn->isn_arg.echo.echo_with_white = with_white;
1681     isn->isn_arg.echo.echo_count = count;
1682 
1683     return OK;
1684 }
1685 
1686 /*
1687  * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1688  */
1689     static int
1690 generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1691 {
1692     isn_T	*isn;
1693 
1694     if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1695 	return FAIL;
1696     isn->isn_arg.number = count;
1697 
1698     return OK;
1699 }
1700 
1701     static int
1702 generate_EXEC(cctx_T *cctx, char_u *line)
1703 {
1704     isn_T	*isn;
1705 
1706     RETURN_OK_IF_SKIP(cctx);
1707     if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1708 	return FAIL;
1709     isn->isn_arg.string = vim_strsave(line);
1710     return OK;
1711 }
1712 
1713     static int
1714 generate_EXECCONCAT(cctx_T *cctx, int count)
1715 {
1716     isn_T	*isn;
1717 
1718     if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1719 	return FAIL;
1720     isn->isn_arg.number = count;
1721     return OK;
1722 }
1723 
1724 /*
1725  * Reserve space for a local variable.
1726  * Return the variable or NULL if it failed.
1727  */
1728     static lvar_T *
1729 reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1730 {
1731     lvar_T  *lvar;
1732 
1733     if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
1734     {
1735 	emsg_namelen(_(e_used_as_arg), name, (int)len);
1736 	return NULL;
1737     }
1738 
1739     if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1740 	return NULL;
1741     lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
1742 
1743     // Every local variable uses the next entry on the stack.  We could re-use
1744     // the last ones when leaving a scope, but then variables used in a closure
1745     // might get overwritten.  To keep things simple do not re-use stack
1746     // entries.  This is less efficient, but memory is cheap these days.
1747     lvar->lv_idx = cctx->ctx_locals_count++;
1748 
1749     lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
1750     lvar->lv_const = isConst;
1751     lvar->lv_type = type;
1752 
1753     return lvar;
1754 }
1755 
1756 /*
1757  * Remove local variables above "new_top".
1758  */
1759     static void
1760 unwind_locals(cctx_T *cctx, int new_top)
1761 {
1762     if (cctx->ctx_locals.ga_len > new_top)
1763     {
1764 	int	idx;
1765 	lvar_T	*lvar;
1766 
1767 	for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1768 	{
1769 	    lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1770 	    vim_free(lvar->lv_name);
1771 	}
1772     }
1773     cctx->ctx_locals.ga_len = new_top;
1774 }
1775 
1776 /*
1777  * Free all local variables.
1778  */
1779     static void
1780 free_locals(cctx_T *cctx)
1781 {
1782     unwind_locals(cctx, 0);
1783     ga_clear(&cctx->ctx_locals);
1784 }
1785 
1786 /*
1787  * Skip over a type definition and return a pointer to just after it.
1788  */
1789     char_u *
1790 skip_type(char_u *start)
1791 {
1792     char_u *p = start;
1793 
1794     while (ASCII_ISALNUM(*p) || *p == '_')
1795 	++p;
1796 
1797     // Skip over "<type>"; this is permissive about white space.
1798     if (*skipwhite(p) == '<')
1799     {
1800 	p = skipwhite(p);
1801 	p = skip_type(skipwhite(p + 1));
1802 	p = skipwhite(p);
1803 	if (*p == '>')
1804 	    ++p;
1805     }
1806     else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1807     {
1808 	// handle func(args): type
1809 	++p;
1810 	while (*p != ')' && *p != NUL)
1811 	{
1812 	    char_u *sp = p;
1813 
1814 	    p = skip_type(p);
1815 	    if (p == sp)
1816 		return p;  // syntax error
1817 	    if (*p == ',')
1818 		p = skipwhite(p + 1);
1819 	}
1820 	if (*p == ')')
1821 	{
1822 	    if (p[1] == ':')
1823 		p = skip_type(skipwhite(p + 2));
1824 	    else
1825 		p = skipwhite(p + 1);
1826 	}
1827     }
1828 
1829     return p;
1830 }
1831 
1832 /*
1833  * Parse the member type: "<type>" and return "type" with the member set.
1834  * Use "type_gap" if a new type needs to be added.
1835  * Returns NULL in case of failure.
1836  */
1837     static type_T *
1838 parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
1839 {
1840     type_T  *member_type;
1841     int	    prev_called_emsg = called_emsg;
1842 
1843     if (**arg != '<')
1844     {
1845 	if (*skipwhite(*arg) == '<')
1846 	    semsg(_(e_no_white_before), "<");
1847 	else
1848 	    emsg(_("E1008: Missing <type>"));
1849 	return type;
1850     }
1851     *arg = skipwhite(*arg + 1);
1852 
1853     member_type = parse_type(arg, type_gap);
1854 
1855     *arg = skipwhite(*arg);
1856     if (**arg != '>' && called_emsg == prev_called_emsg)
1857     {
1858 	emsg(_("E1009: Missing > after type"));
1859 	return type;
1860     }
1861     ++*arg;
1862 
1863     if (type->tt_type == VAR_LIST)
1864 	return get_list_type(member_type, type_gap);
1865     return get_dict_type(member_type, type_gap);
1866 }
1867 
1868 /*
1869  * Parse a type at "arg" and advance over it.
1870  * Return &t_any for failure.
1871  */
1872     type_T *
1873 parse_type(char_u **arg, garray_T *type_gap)
1874 {
1875     char_u  *p = *arg;
1876     size_t  len;
1877 
1878     // skip over the first word
1879     while (ASCII_ISALNUM(*p) || *p == '_')
1880 	++p;
1881     len = p - *arg;
1882 
1883     switch (**arg)
1884     {
1885 	case 'a':
1886 	    if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1887 	    {
1888 		*arg += len;
1889 		return &t_any;
1890 	    }
1891 	    break;
1892 	case 'b':
1893 	    if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1894 	    {
1895 		*arg += len;
1896 		return &t_bool;
1897 	    }
1898 	    if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1899 	    {
1900 		*arg += len;
1901 		return &t_blob;
1902 	    }
1903 	    break;
1904 	case 'c':
1905 	    if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1906 	    {
1907 		*arg += len;
1908 		return &t_channel;
1909 	    }
1910 	    break;
1911 	case 'd':
1912 	    if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1913 	    {
1914 		*arg += len;
1915 		return parse_type_member(arg, &t_dict_any, type_gap);
1916 	    }
1917 	    break;
1918 	case 'f':
1919 	    if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1920 	    {
1921 #ifdef FEAT_FLOAT
1922 		*arg += len;
1923 		return &t_float;
1924 #else
1925 		emsg(_("E1076: This Vim is not compiled with float support"));
1926 		return &t_any;
1927 #endif
1928 	    }
1929 	    if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1930 	    {
1931 		type_T  *type;
1932 		type_T  *ret_type = &t_unknown;
1933 		int	argcount = -1;
1934 		int	flags = 0;
1935 		int	first_optional = -1;
1936 		type_T	*arg_type[MAX_FUNC_ARGS + 1];
1937 
1938 		// func({type}, ...{type}): {type}
1939 		*arg += len;
1940 		if (**arg == '(')
1941 		{
1942 		    // "func" may or may not return a value, "func()" does
1943 		    // not return a value.
1944 		    ret_type = &t_void;
1945 
1946 		    p = ++*arg;
1947 		    argcount = 0;
1948 		    while (*p != NUL && *p != ')')
1949 		    {
1950 			if (*p == '?')
1951 			{
1952 			    if (first_optional == -1)
1953 				first_optional = argcount;
1954 			    ++p;
1955 			}
1956 			else if (first_optional != -1)
1957 			{
1958 			    emsg(_("E1007: mandatory argument after optional argument"));
1959 			    return &t_any;
1960 			}
1961 			else if (STRNCMP(p, "...", 3) == 0)
1962 			{
1963 			    flags |= TTFLAG_VARARGS;
1964 			    p += 3;
1965 			}
1966 
1967 			arg_type[argcount++] = parse_type(&p, type_gap);
1968 
1969 			// Nothing comes after "...{type}".
1970 			if (flags & TTFLAG_VARARGS)
1971 			    break;
1972 
1973 			if (*p != ',' && *skipwhite(p) == ',')
1974 			{
1975 			    semsg(_(e_no_white_before), ",");
1976 			    return &t_any;
1977 			}
1978 			if (*p == ',')
1979 			{
1980 			    ++p;
1981 			    if (!VIM_ISWHITE(*p))
1982 			    {
1983 				semsg(_(e_white_after), ",");
1984 				return &t_any;
1985 			    }
1986 			}
1987 			p = skipwhite(p);
1988 			if (argcount == MAX_FUNC_ARGS)
1989 			{
1990 			    emsg(_("E740: Too many argument types"));
1991 			    return &t_any;
1992 			}
1993 		    }
1994 
1995 		    p = skipwhite(p);
1996 		    if (*p != ')')
1997 		    {
1998 			emsg(_(e_missing_close));
1999 			return &t_any;
2000 		    }
2001 		    *arg = p + 1;
2002 		}
2003 		if (**arg == ':')
2004 		{
2005 		    // parse return type
2006 		    ++*arg;
2007 		    if (!VIM_ISWHITE(**arg))
2008 			semsg(_(e_white_after), ":");
2009 		    *arg = skipwhite(*arg);
2010 		    ret_type = parse_type(arg, type_gap);
2011 		}
2012 		if (flags == 0 && first_optional == -1 && argcount <= 0)
2013 		    type = get_func_type(ret_type, argcount, type_gap);
2014 		else
2015 		{
2016 		    type = alloc_func_type(ret_type, argcount, type_gap);
2017 		    type->tt_flags = flags;
2018 		    if (argcount > 0)
2019 		    {
2020 			type->tt_argcount = argcount;
2021 			type->tt_min_argcount = first_optional == -1
2022 						   ? argcount : first_optional;
2023 			if (func_type_add_arg_types(type, argcount,
2024 							     type_gap) == FAIL)
2025 			    return &t_any;
2026 			mch_memmove(type->tt_args, arg_type,
2027 						  sizeof(type_T *) * argcount);
2028 		    }
2029 		}
2030 		return type;
2031 	    }
2032 	    break;
2033 	case 'j':
2034 	    if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2035 	    {
2036 		*arg += len;
2037 		return &t_job;
2038 	    }
2039 	    break;
2040 	case 'l':
2041 	    if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2042 	    {
2043 		*arg += len;
2044 		return parse_type_member(arg, &t_list_any, type_gap);
2045 	    }
2046 	    break;
2047 	case 'n':
2048 	    if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2049 	    {
2050 		*arg += len;
2051 		return &t_number;
2052 	    }
2053 	    break;
2054 	case 's':
2055 	    if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2056 	    {
2057 		*arg += len;
2058 		return &t_string;
2059 	    }
2060 	    break;
2061 	case 'v':
2062 	    if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2063 	    {
2064 		*arg += len;
2065 		return &t_void;
2066 	    }
2067 	    break;
2068     }
2069 
2070     semsg(_("E1010: Type not recognized: %s"), *arg);
2071     return &t_any;
2072 }
2073 
2074 /*
2075  * Check if "type1" and "type2" are exactly the same.
2076  */
2077     static int
2078 equal_type(type_T *type1, type_T *type2)
2079 {
2080     int i;
2081 
2082     if (type1->tt_type != type2->tt_type)
2083 	return FALSE;
2084     switch (type1->tt_type)
2085     {
2086 	case VAR_UNKNOWN:
2087 	case VAR_ANY:
2088 	case VAR_VOID:
2089 	case VAR_SPECIAL:
2090 	case VAR_BOOL:
2091 	case VAR_NUMBER:
2092 	case VAR_FLOAT:
2093 	case VAR_STRING:
2094 	case VAR_BLOB:
2095 	case VAR_JOB:
2096 	case VAR_CHANNEL:
2097 	    break;  // not composite is always OK
2098 	case VAR_LIST:
2099 	case VAR_DICT:
2100 	    return equal_type(type1->tt_member, type2->tt_member);
2101 	case VAR_FUNC:
2102 	case VAR_PARTIAL:
2103 	    if (!equal_type(type1->tt_member, type2->tt_member)
2104 		    || type1->tt_argcount != type2->tt_argcount)
2105 		return FALSE;
2106 	    if (type1->tt_argcount < 0
2107 			   || type1->tt_args == NULL || type2->tt_args == NULL)
2108 		return TRUE;
2109 	    for (i = 0; i < type1->tt_argcount; ++i)
2110 		if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2111 		    return FALSE;
2112 	    return TRUE;
2113     }
2114     return TRUE;
2115 }
2116 
2117 /*
2118  * Find the common type of "type1" and "type2" and put it in "dest".
2119  * "type2" and "dest" may be the same.
2120  */
2121     static void
2122 common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
2123 {
2124     if (equal_type(type1, type2))
2125     {
2126 	*dest = type1;
2127 	return;
2128     }
2129 
2130     if (type1->tt_type == type2->tt_type)
2131     {
2132 	if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2133 	{
2134 	    type_T *common;
2135 
2136 	    common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2137 	    if (type1->tt_type == VAR_LIST)
2138 		*dest = get_list_type(common, type_gap);
2139 	    else
2140 		*dest = get_dict_type(common, type_gap);
2141 	    return;
2142 	}
2143 	if (type1->tt_type == VAR_FUNC)
2144 	{
2145 	    type_T *common;
2146 
2147 	    common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2148 	    if (type1->tt_argcount == type2->tt_argcount
2149 						    && type1->tt_argcount >= 0)
2150 	    {
2151 		int argcount = type1->tt_argcount;
2152 		int i;
2153 
2154 		*dest = alloc_func_type(common, argcount, type_gap);
2155 		if (type1->tt_args != NULL && type2->tt_args != NULL)
2156 		{
2157 		    if (func_type_add_arg_types(*dest, argcount,
2158 							     type_gap) == OK)
2159 			for (i = 0; i < argcount; ++i)
2160 			    common_type(type1->tt_args[i], type2->tt_args[i],
2161 					       &(*dest)->tt_args[i], type_gap);
2162 		}
2163 	    }
2164 	    else
2165 		*dest = alloc_func_type(common, -1, type_gap);
2166 	    return;
2167 	}
2168     }
2169 
2170     *dest = &t_any;
2171 }
2172 
2173     char *
2174 vartype_name(vartype_T type)
2175 {
2176     switch (type)
2177     {
2178 	case VAR_UNKNOWN: break;
2179 	case VAR_ANY: return "any";
2180 	case VAR_VOID: return "void";
2181 	case VAR_SPECIAL: return "special";
2182 	case VAR_BOOL: return "bool";
2183 	case VAR_NUMBER: return "number";
2184 	case VAR_FLOAT: return "float";
2185 	case VAR_STRING: return "string";
2186 	case VAR_BLOB: return "blob";
2187 	case VAR_JOB: return "job";
2188 	case VAR_CHANNEL: return "channel";
2189 	case VAR_LIST: return "list";
2190 	case VAR_DICT: return "dict";
2191 
2192 	case VAR_FUNC:
2193 	case VAR_PARTIAL: return "func";
2194     }
2195     return "unknown";
2196 }
2197 
2198 /*
2199  * Return the name of a type.
2200  * The result may be in allocated memory, in which case "tofree" is set.
2201  */
2202     char *
2203 type_name(type_T *type, char **tofree)
2204 {
2205     char *name = vartype_name(type->tt_type);
2206 
2207     *tofree = NULL;
2208     if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2209     {
2210 	char *member_free;
2211 	char *member_name = type_name(type->tt_member, &member_free);
2212 	size_t len;
2213 
2214 	len = STRLEN(name) + STRLEN(member_name) + 3;
2215 	*tofree = alloc(len);
2216 	if (*tofree != NULL)
2217 	{
2218 	    vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2219 	    vim_free(member_free);
2220 	    return *tofree;
2221 	}
2222     }
2223     if (type->tt_type == VAR_FUNC)
2224     {
2225 	garray_T    ga;
2226 	int	    i;
2227 	int	    varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2228 
2229 	ga_init2(&ga, 1, 100);
2230 	if (ga_grow(&ga, 20) == FAIL)
2231 	    return "[unknown]";
2232 	*tofree = ga.ga_data;
2233 	STRCPY(ga.ga_data, "func(");
2234 	ga.ga_len += 5;
2235 
2236 	for (i = 0; i < type->tt_argcount; ++i)
2237 	{
2238 	    char *arg_free;
2239 	    char *arg_type;
2240 	    int  len;
2241 
2242 	    if (type->tt_args == NULL)
2243 		arg_type = "[unknown]";
2244 	    else
2245 		arg_type = type_name(type->tt_args[i], &arg_free);
2246 	    if (i > 0)
2247 	    {
2248 		STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
2249 		ga.ga_len += 2;
2250 	    }
2251 	    len = (int)STRLEN(arg_type);
2252 	    if (ga_grow(&ga, len + 8) == FAIL)
2253 	    {
2254 		vim_free(arg_free);
2255 		return "[unknown]";
2256 	    }
2257 	    *tofree = ga.ga_data;
2258 	    if (varargs && i == type->tt_argcount - 1)
2259 	    {
2260 		STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2261 		ga.ga_len += 3;
2262 	    }
2263 	    else if (i >= type->tt_min_argcount)
2264 		*((char *)ga.ga_data + ga.ga_len++) = '?';
2265 	    STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
2266 	    ga.ga_len += len;
2267 	    vim_free(arg_free);
2268 	}
2269 
2270 	if (type->tt_member == &t_void)
2271 	    STRCPY((char *)ga.ga_data + ga.ga_len, ")");
2272 	else
2273 	{
2274 	    char *ret_free;
2275 	    char *ret_name = type_name(type->tt_member, &ret_free);
2276 	    int  len;
2277 
2278 	    len = (int)STRLEN(ret_name) + 4;
2279 	    if (ga_grow(&ga, len) == FAIL)
2280 	    {
2281 		vim_free(ret_free);
2282 		return "[unknown]";
2283 	    }
2284 	    *tofree = ga.ga_data;
2285 	    STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2286 	    STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
2287 	    vim_free(ret_free);
2288 	}
2289 	return ga.ga_data;
2290     }
2291 
2292     return name;
2293 }
2294 
2295 /*
2296  * Find "name" in script-local items of script "sid".
2297  * Returns the index in "sn_var_vals" if found.
2298  * If found but not in "sn_var_vals" returns -1.
2299  * If not found returns -2.
2300  */
2301     int
2302 get_script_item_idx(int sid, char_u *name, int check_writable)
2303 {
2304     hashtab_T	    *ht;
2305     dictitem_T	    *di;
2306     scriptitem_T    *si = SCRIPT_ITEM(sid);
2307     int		    idx;
2308 
2309     // First look the name up in the hashtable.
2310     if (sid <= 0 || sid > script_items.ga_len)
2311 	return -1;
2312     ht = &SCRIPT_VARS(sid);
2313     di = find_var_in_ht(ht, 0, name, TRUE);
2314     if (di == NULL)
2315 	return -2;
2316 
2317     // Now find the svar_T index in sn_var_vals.
2318     for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2319     {
2320 	svar_T    *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2321 
2322 	if (sv->sv_tv == &di->di_tv)
2323 	{
2324 	    if (check_writable && sv->sv_const)
2325 		semsg(_(e_readonlyvar), name);
2326 	    return idx;
2327 	}
2328     }
2329     return -1;
2330 }
2331 
2332 /*
2333  * Find "name" in imported items of the current script or in "cctx" if not
2334  * NULL.
2335  */
2336     imported_T *
2337 find_imported(char_u *name, size_t len, cctx_T *cctx)
2338 {
2339     scriptitem_T    *si = SCRIPT_ITEM(current_sctx.sc_sid);
2340     int		    idx;
2341 
2342     if (cctx != NULL)
2343 	for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2344 	{
2345 	    imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2346 									 + idx;
2347 
2348 	    if (len == 0 ? STRCMP(name, import->imp_name) == 0
2349 			 : STRLEN(import->imp_name) == len
2350 				  && STRNCMP(name, import->imp_name, len) == 0)
2351 		return import;
2352 	}
2353 
2354     for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2355     {
2356 	imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2357 
2358 	if (len == 0 ? STRCMP(name, import->imp_name) == 0
2359 		     : STRLEN(import->imp_name) == len
2360 				  && STRNCMP(name, import->imp_name, len) == 0)
2361 	    return import;
2362     }
2363     return NULL;
2364 }
2365 
2366 /*
2367  * Free all imported variables.
2368  */
2369     static void
2370 free_imported(cctx_T *cctx)
2371 {
2372     int idx;
2373 
2374     for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2375     {
2376 	imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2377 
2378 	vim_free(import->imp_name);
2379     }
2380     ga_clear(&cctx->ctx_imports);
2381 }
2382 
2383 /*
2384  * Return TRUE if "p" points at a "#" but not at "#{".
2385  */
2386     static int
2387 comment_start(char_u *p)
2388 {
2389     return p[0] == '#' && p[1] != '{';
2390 }
2391 
2392 /*
2393  * Return a pointer to the next line that isn't empty or only contains a
2394  * comment. Skips over white space.
2395  * Returns NULL if there is none.
2396  */
2397     static char_u *
2398 peek_next_line(cctx_T *cctx)
2399 {
2400     int lnum = cctx->ctx_lnum;
2401 
2402     while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2403     {
2404 	char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
2405 	char_u *p = skipwhite(line);
2406 
2407 	if (*p != NUL && !comment_start(p))
2408 	    return p;
2409     }
2410     return NULL;
2411 }
2412 
2413 /*
2414  * Get the next line of the function from "cctx".
2415  * Skips over empty lines.  Skips over comment lines if "skip_comment" is TRUE.
2416  * Returns NULL when at the end.
2417  */
2418     static char_u *
2419 next_line_from_context(cctx_T *cctx, int skip_comment)
2420 {
2421     char_u	*line;
2422 
2423     do
2424     {
2425 	++cctx->ctx_lnum;
2426 	if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
2427 	{
2428 	    line = NULL;
2429 	    break;
2430 	}
2431 	line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
2432 	cctx->ctx_line_start = line;
2433 	SOURCING_LNUM = cctx->ctx_lnum + 1;
2434     } while (line == NULL || *skipwhite(line) == NUL
2435 			  || (skip_comment && comment_start(skipwhite(line))));
2436     return line;
2437 }
2438 
2439 /*
2440  * If "*arg" is at the end of the line, advance to the next line.
2441  * Also when "whitep" points to white space and "*arg" is on a "#".
2442  * Return FAIL if beyond the last line, "*arg" is unmodified then.
2443  */
2444     static int
2445 may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
2446 {
2447     if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
2448     {
2449 	char_u *next = next_line_from_context(cctx, TRUE);
2450 
2451 	if (next == NULL)
2452 	    return FAIL;
2453 	*arg = skipwhite(next);
2454     }
2455     return OK;
2456 }
2457 
2458 // Structure passed between the compile_expr* functions to keep track of
2459 // constants that have been parsed but for which no code was produced yet.  If
2460 // possible expressions on these constants are applied at compile time.  If
2461 // that is not possible, the code to push the constants needs to be generated
2462 // before other instructions.
2463 // Using 50 should be more than enough of 5 levels of ().
2464 #define PPSIZE 50
2465 typedef struct {
2466     typval_T	pp_tv[PPSIZE];	// stack of ppconst constants
2467     int		pp_used;	// active entries in pp_tv[]
2468 } ppconst_T;
2469 
2470 static int compile_expr0(char_u **arg,  cctx_T *cctx);
2471 static int compile_expr1(char_u **arg,  cctx_T *cctx, ppconst_T *ppconst);
2472 
2473 /*
2474  * Generate a PUSH instruction for "tv".
2475  * "tv" will be consumed or cleared.
2476  * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2477  */
2478     static int
2479 generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2480 {
2481     if (tv != NULL)
2482     {
2483 	switch (tv->v_type)
2484 	{
2485 	    case VAR_UNKNOWN:
2486 		break;
2487 	    case VAR_BOOL:
2488 		generate_PUSHBOOL(cctx, tv->vval.v_number);
2489 		break;
2490 	    case VAR_SPECIAL:
2491 		generate_PUSHSPEC(cctx, tv->vval.v_number);
2492 		break;
2493 	    case VAR_NUMBER:
2494 		generate_PUSHNR(cctx, tv->vval.v_number);
2495 		break;
2496 #ifdef FEAT_FLOAT
2497 	    case VAR_FLOAT:
2498 		generate_PUSHF(cctx, tv->vval.v_float);
2499 		break;
2500 #endif
2501 	    case VAR_BLOB:
2502 		generate_PUSHBLOB(cctx, tv->vval.v_blob);
2503 		tv->vval.v_blob = NULL;
2504 		break;
2505 	    case VAR_STRING:
2506 		generate_PUSHS(cctx, tv->vval.v_string);
2507 		tv->vval.v_string = NULL;
2508 		break;
2509 	    default:
2510 		iemsg("constant type not supported");
2511 		clear_tv(tv);
2512 		return FAIL;
2513 	}
2514 	tv->v_type = VAR_UNKNOWN;
2515     }
2516     return OK;
2517 }
2518 
2519 /*
2520  * Generate code for any ppconst entries.
2521  */
2522     static int
2523 generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2524 {
2525     int	    i;
2526     int	    ret = OK;
2527     int	    save_skip = cctx->ctx_skip;
2528 
2529     cctx->ctx_skip = SKIP_NOT;
2530     for (i = 0; i < ppconst->pp_used; ++i)
2531 	if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2532 	    ret = FAIL;
2533     ppconst->pp_used = 0;
2534     cctx->ctx_skip = save_skip;
2535     return ret;
2536 }
2537 
2538 /*
2539  * Clear ppconst constants.  Used when failing.
2540  */
2541     static void
2542 clear_ppconst(ppconst_T *ppconst)
2543 {
2544     int	    i;
2545 
2546     for (i = 0; i < ppconst->pp_used; ++i)
2547 	clear_tv(&ppconst->pp_tv[i]);
2548     ppconst->pp_used = 0;
2549 }
2550 
2551 /*
2552  * Generate an instruction to load script-local variable "name", without the
2553  * leading "s:".
2554  * Also finds imported variables.
2555  */
2556     static int
2557 compile_load_scriptvar(
2558 	cctx_T *cctx,
2559 	char_u *name,	    // variable NUL terminated
2560 	char_u *start,	    // start of variable
2561 	char_u **end,	    // end of variable
2562 	int    error)	    // when TRUE may give error
2563 {
2564     scriptitem_T    *si = SCRIPT_ITEM(current_sctx.sc_sid);
2565     int		    idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2566     imported_T	    *import;
2567 
2568     if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
2569     {
2570 	// variable is not in sn_var_vals: old style script.
2571 	return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2572 								       &t_any);
2573     }
2574     if (idx >= 0)
2575     {
2576 	svar_T		*sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2577 
2578 	generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2579 					current_sctx.sc_sid, idx, sv->sv_type);
2580 	return OK;
2581     }
2582 
2583     import = find_imported(name, 0, cctx);
2584     if (import != NULL)
2585     {
2586 	if (import->imp_all)
2587 	{
2588 	    char_u	*p = skipwhite(*end);
2589 	    int		name_len;
2590 	    ufunc_T	*ufunc;
2591 	    type_T	*type;
2592 
2593 	    // Used "import * as Name", need to lookup the member.
2594 	    if (*p != '.')
2595 	    {
2596 		semsg(_("E1060: expected dot after name: %s"), start);
2597 		return FAIL;
2598 	    }
2599 	    ++p;
2600 	    if (VIM_ISWHITE(*p))
2601 	    {
2602 		emsg(_("E1074: no white space allowed after dot"));
2603 		return FAIL;
2604 	    }
2605 
2606 	    idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2607 	    // TODO: what if it is a function?
2608 	    if (idx < 0)
2609 		return FAIL;
2610 	    *end = p;
2611 
2612 	    generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2613 		    import->imp_sid,
2614 		    idx,
2615 		    type);
2616 	}
2617 	else
2618 	{
2619 	    // TODO: check this is a variable, not a function?
2620 	    generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2621 		    import->imp_sid,
2622 		    import->imp_var_vals_idx,
2623 		    import->imp_type);
2624 	}
2625 	return OK;
2626     }
2627 
2628     if (error)
2629 	semsg(_("E1050: Item not found: %s"), name);
2630     return FAIL;
2631 }
2632 
2633     static int
2634 generate_funcref(cctx_T *cctx, char_u *name)
2635 {
2636     ufunc_T *ufunc = find_func(name, FALSE, cctx);
2637 
2638     if (ufunc == NULL)
2639 	return FAIL;
2640 
2641     return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2642 }
2643 
2644 /*
2645  * Compile a variable name into a load instruction.
2646  * "end" points to just after the name.
2647  * When "error" is FALSE do not give an error when not found.
2648  */
2649     static int
2650 compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
2651 {
2652     type_T	*type;
2653     char_u	*name;
2654     char_u	*end = end_arg;
2655     int		res = FAIL;
2656     int		prev_called_emsg = called_emsg;
2657 
2658     if (*(*arg + 1) == ':')
2659     {
2660 	// load namespaced variable
2661 	if (end <= *arg + 2)
2662 	    name = vim_strsave((char_u *)"[empty]");
2663 	else
2664 	    name = vim_strnsave(*arg + 2, end - (*arg + 2));
2665 	if (name == NULL)
2666 	    return FAIL;
2667 
2668 	if (**arg == 'v')
2669 	{
2670 	    res = generate_LOADV(cctx, name, error);
2671 	}
2672 	else if (**arg == 'g')
2673 	{
2674 	    // Global variables can be defined later, thus we don't check if it
2675 	    // exists, give error at runtime.
2676 	    res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2677 	}
2678 	else if (**arg == 's')
2679 	{
2680 	    res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
2681 	}
2682 	else if (**arg == 'b')
2683 	{
2684 	    // Buffer-local variables can be defined later, thus we don't check
2685 	    // if it exists, give error at runtime.
2686 	    res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
2687 	}
2688 	else if (**arg == 'w')
2689 	{
2690 	    // Window-local variables can be defined later, thus we don't check
2691 	    // if it exists, give error at runtime.
2692 	    res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
2693 	}
2694 	else if (**arg == 't')
2695 	{
2696 	    // Tabpage-local variables can be defined later, thus we don't
2697 	    // check if it exists, give error at runtime.
2698 	    res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
2699 	}
2700 	else
2701 	{
2702 	    semsg("E1075: Namespace not supported: %s", *arg);
2703 	    goto theend;
2704 	}
2705     }
2706     else
2707     {
2708 	size_t	    len = end - *arg;
2709 	int	    idx;
2710 	int	    gen_load = FALSE;
2711 	int	    gen_load_outer = FALSE;
2712 
2713 	name = vim_strnsave(*arg, end - *arg);
2714 	if (name == NULL)
2715 	    return FAIL;
2716 
2717 	if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
2718 	{
2719 	    if (!gen_load_outer)
2720 		gen_load = TRUE;
2721 	}
2722 	else
2723 	{
2724 	    lvar_T *lvar = lookup_local(*arg, len, cctx);
2725 
2726 	    if (lvar != NULL)
2727 	    {
2728 		type = lvar->lv_type;
2729 		idx = lvar->lv_idx;
2730 		if (lvar->lv_from_outer)
2731 		    gen_load_outer = TRUE;
2732 		else
2733 		    gen_load = TRUE;
2734 	    }
2735 	    else
2736 	    {
2737 		// "var" can be script-local even without using "s:" if it
2738 		// already exists.
2739 		if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2740 						    == SCRIPT_VERSION_VIM9
2741 			    || lookup_script(*arg, len) == OK)
2742 		   res = compile_load_scriptvar(cctx, name, *arg, &end,
2743 								    FALSE);
2744 
2745 		// When the name starts with an uppercase letter or "x:" it
2746 		// can be a user defined function.
2747 		if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2748 		    res = generate_funcref(cctx, name);
2749 	    }
2750 	}
2751 	if (gen_load)
2752 	    res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
2753 	if (gen_load_outer)
2754 	    res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
2755     }
2756 
2757     *arg = end;
2758 
2759 theend:
2760     if (res == FAIL && error && called_emsg == prev_called_emsg)
2761 	semsg(_(e_var_notfound), name);
2762     vim_free(name);
2763     return res;
2764 }
2765 
2766 /*
2767  * Compile the argument expressions.
2768  * "arg" points to just after the "(" and is advanced to after the ")"
2769  */
2770     static int
2771 compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2772 {
2773     char_u  *p = *arg;
2774     char_u  *whitep = *arg;
2775 
2776     for (;;)
2777     {
2778 	if (may_get_next_line(whitep, &p, cctx) == FAIL)
2779 	    goto failret;
2780 	if (*p == ')')
2781 	{
2782 	    *arg = p + 1;
2783 	    return OK;
2784 	}
2785 
2786 	if (compile_expr0(&p, cctx) == FAIL)
2787 	    return FAIL;
2788 	++*argcount;
2789 
2790 	if (*p != ',' && *skipwhite(p) == ',')
2791 	{
2792 	    semsg(_(e_no_white_before), ",");
2793 	    p = skipwhite(p);
2794 	}
2795 	if (*p == ',')
2796 	{
2797 	    ++p;
2798 	    if (*p != NUL && !VIM_ISWHITE(*p))
2799 		semsg(_(e_white_after), ",");
2800 	}
2801 	whitep = p;
2802 	p = skipwhite(p);
2803     }
2804 failret:
2805     emsg(_(e_missing_close));
2806     return FAIL;
2807 }
2808 
2809 /*
2810  * Compile a function call:  name(arg1, arg2)
2811  * "arg" points to "name", "arg + varlen" to the "(".
2812  * "argcount_init" is 1 for "value->method()"
2813  * Instructions:
2814  *	EVAL arg1
2815  *	EVAL arg2
2816  *	BCALL / DCALL / UCALL
2817  */
2818     static int
2819 compile_call(
2820 	char_u	    **arg,
2821 	size_t	    varlen,
2822 	cctx_T	    *cctx,
2823 	ppconst_T   *ppconst,
2824 	int	    argcount_init)
2825 {
2826     char_u	*name = *arg;
2827     char_u	*p;
2828     int		argcount = argcount_init;
2829     char_u	namebuf[100];
2830     char_u	fname_buf[FLEN_FIXED + 1];
2831     char_u	*tofree = NULL;
2832     int		error = FCERR_NONE;
2833     ufunc_T	*ufunc;
2834     int		res = FAIL;
2835 
2836     // we can evaluate "has('name')" at compile time
2837     if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2838     {
2839 	char_u	    *s = skipwhite(*arg + varlen + 1);
2840 	typval_T    argvars[2];
2841 
2842 	argvars[0].v_type = VAR_UNKNOWN;
2843 	if (*s == '"')
2844 	    (void)get_string_tv(&s, &argvars[0], TRUE);
2845 	else if (*s == '\'')
2846 	    (void)get_lit_string_tv(&s, &argvars[0], TRUE);
2847 	s = skipwhite(s);
2848 	if (*s == ')' && argvars[0].v_type == VAR_STRING)
2849 	{
2850 	    typval_T	*tv = &ppconst->pp_tv[ppconst->pp_used];
2851 
2852 	    *arg = s + 1;
2853 	    argvars[1].v_type = VAR_UNKNOWN;
2854 	    tv->v_type = VAR_NUMBER;
2855 	    tv->vval.v_number = 0;
2856 	    f_has(argvars, tv);
2857 	    clear_tv(&argvars[0]);
2858 	    ++ppconst->pp_used;
2859 	    return OK;
2860 	}
2861 	clear_tv(&argvars[0]);
2862     }
2863 
2864     if (generate_ppconst(cctx, ppconst) == FAIL)
2865 	return FAIL;
2866 
2867     if (varlen >= sizeof(namebuf))
2868     {
2869 	semsg(_("E1011: name too long: %s"), name);
2870 	return FAIL;
2871     }
2872     vim_strncpy(namebuf, *arg, varlen);
2873     name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
2874 
2875     *arg = skipwhite(*arg + varlen + 1);
2876     if (compile_arguments(arg, cctx, &argcount) == FAIL)
2877 	goto theend;
2878 
2879     if (ASCII_ISLOWER(*name) && name[1] != ':')
2880     {
2881 	int	    idx;
2882 
2883 	// builtin function
2884 	idx = find_internal_func(name);
2885 	if (idx >= 0)
2886 	    res = generate_BCALL(cctx, idx, argcount);
2887 	else
2888 	    semsg(_(e_unknownfunc), namebuf);
2889 	goto theend;
2890     }
2891 
2892     // If we can find the function by name generate the right call.
2893     ufunc = find_func(name, FALSE, cctx);
2894     if (ufunc != NULL)
2895     {
2896 	res = generate_CALL(cctx, ufunc, argcount);
2897 	goto theend;
2898     }
2899 
2900     // If the name is a variable, load it and use PCALL.
2901     // Not for g:Func(), we don't know if it is a variable or not.
2902     p = namebuf;
2903     if (STRNCMP(namebuf, "g:", 2) != 0
2904 	    && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
2905     {
2906 	garray_T    *stack = &cctx->ctx_type_stack;
2907 	type_T	    *type;
2908 
2909 	type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2910 	res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
2911 	goto theend;
2912     }
2913 
2914     // A global function may be defined only later.  Need to figure out at
2915     // runtime.  Also handles a FuncRef at runtime.
2916     if (STRNCMP(namebuf, "g:", 2) == 0)
2917 	res = generate_UCALL(cctx, name, argcount);
2918     else
2919 	semsg(_(e_unknownfunc), namebuf);
2920 
2921 theend:
2922     vim_free(tofree);
2923     return res;
2924 }
2925 
2926 // like NAMESPACE_CHAR but with 'a' and 'l'.
2927 #define VIM9_NAMESPACE_CHAR	(char_u *)"bgstvw"
2928 
2929 /*
2930  * Find the end of a variable or function name.  Unlike find_name_end() this
2931  * does not recognize magic braces.
2932  * When "namespace" is TRUE recognize "b:", "s:", etc.
2933  * Return a pointer to just after the name.  Equal to "arg" if there is no
2934  * valid name.
2935  */
2936     static char_u *
2937 to_name_end(char_u *arg, int namespace)
2938 {
2939     char_u	*p;
2940 
2941     // Quick check for valid starting character.
2942     if (!eval_isnamec1(*arg))
2943 	return arg;
2944 
2945     for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2946 	// Include a namespace such as "s:var" and "v:var".  But "n:" is not
2947 	// and can be used in slice "[n:]".
2948 	if (*p == ':' && (p != arg + 1
2949 			     || !namespace
2950 			     || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2951 	    break;
2952     return p;
2953 }
2954 
2955 /*
2956  * Like to_name_end() but also skip over a list or dict constant.
2957  */
2958     char_u *
2959 to_name_const_end(char_u *arg)
2960 {
2961     char_u	*p = to_name_end(arg, TRUE);
2962     typval_T	rettv;
2963 
2964     if (p == arg && *arg == '[')
2965     {
2966 
2967 	// Can be "[1, 2, 3]->Func()".
2968 	if (get_list_tv(&p, &rettv, 0, FALSE) == FAIL)
2969 	    p = arg;
2970     }
2971     else if (p == arg && *arg == '#' && arg[1] == '{')
2972     {
2973 	// Can be "#{a: 1}->Func()".
2974 	++p;
2975 	if (eval_dict(&p, &rettv, 0, TRUE) == FAIL)
2976 	    p = arg;
2977     }
2978     else if (p == arg && *arg == '{')
2979     {
2980 	int	    ret = get_lambda_tv(&p, &rettv, FALSE);
2981 
2982 	// Can be "{x -> ret}()".
2983 	// Can be "{'a': 1}->Func()".
2984 	if (ret == NOTDONE)
2985 	    ret = eval_dict(&p, &rettv, 0, FALSE);
2986 	if (ret != OK)
2987 	    p = arg;
2988     }
2989 
2990     return p;
2991 }
2992 
2993 /*
2994  * parse a list: [expr, expr]
2995  * "*arg" points to the '['.
2996  */
2997     static int
2998 compile_list(char_u **arg, cctx_T *cctx)
2999 {
3000     char_u	*p = skipwhite(*arg + 1);
3001     char_u	*whitep = *arg + 1;
3002     int		count = 0;
3003 
3004     for (;;)
3005     {
3006 	if (may_get_next_line(whitep, &p, cctx) == FAIL)
3007 	{
3008 	    semsg(_(e_list_end), *arg);
3009 	    return FAIL;
3010 	}
3011 	if (*p == ']')
3012 	{
3013 	    ++p;
3014 	    // Allow for following comment, after at least one space.
3015 	    if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
3016 		p += STRLEN(p);
3017 	    break;
3018 	}
3019 	if (compile_expr0(&p, cctx) == FAIL)
3020 	    break;
3021 	++count;
3022 	if (*p == ',')
3023 	    ++p;
3024 	whitep = p;
3025 	p = skipwhite(p);
3026     }
3027     *arg = p;
3028 
3029     generate_NEWLIST(cctx, count);
3030     return OK;
3031 }
3032 
3033 /*
3034  * parse a lambda: {arg, arg -> expr}
3035  * "*arg" points to the '{'.
3036  */
3037     static int
3038 compile_lambda(char_u **arg, cctx_T *cctx)
3039 {
3040     typval_T	rettv;
3041     ufunc_T	*ufunc;
3042 
3043     // Get the funcref in "rettv".
3044     if (get_lambda_tv(arg, &rettv, TRUE) != OK)
3045 	return FAIL;
3046 
3047     ufunc = rettv.vval.v_partial->pt_func;
3048     ++ufunc->uf_refcount;
3049     clear_tv(&rettv);
3050     ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
3051 
3052     // The function will have one line: "return {expr}".
3053     // Compile it into instructions.
3054     compile_def_function(ufunc, TRUE, cctx);
3055 
3056     if (ufunc->uf_def_status == UF_COMPILED)
3057 	return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
3058     return FAIL;
3059 }
3060 
3061 /*
3062  * Compile a lamda call: expr->{lambda}(args)
3063  * "arg" points to the "{".
3064  */
3065     static int
3066 compile_lambda_call(char_u **arg, cctx_T *cctx)
3067 {
3068     ufunc_T	*ufunc;
3069     typval_T	rettv;
3070     int		argcount = 1;
3071     int		ret = FAIL;
3072 
3073     // Get the funcref in "rettv".
3074     if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
3075 	return FAIL;
3076 
3077     if (**arg != '(')
3078     {
3079 	if (*skipwhite(*arg) == '(')
3080 	    emsg(_(e_nowhitespace));
3081 	else
3082 	    semsg(_(e_missing_paren), "lambda");
3083 	clear_tv(&rettv);
3084 	return FAIL;
3085     }
3086 
3087     ufunc = rettv.vval.v_partial->pt_func;
3088     ++ufunc->uf_refcount;
3089     clear_tv(&rettv);
3090     ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
3091 
3092     // The function will have one line: "return {expr}".
3093     // Compile it into instructions.
3094     compile_def_function(ufunc, TRUE, cctx);
3095 
3096     // compile the arguments
3097     *arg = skipwhite(*arg + 1);
3098     if (compile_arguments(arg, cctx, &argcount) == OK)
3099 	// call the compiled function
3100 	ret = generate_CALL(cctx, ufunc, argcount);
3101 
3102     return ret;
3103 }
3104 
3105 /*
3106  * parse a dict: {'key': val} or #{key: val}
3107  * "*arg" points to the '{'.
3108  */
3109     static int
3110 compile_dict(char_u **arg, cctx_T *cctx, int literal)
3111 {
3112     garray_T	*instr = &cctx->ctx_instr;
3113     int		count = 0;
3114     dict_T	*d = dict_alloc();
3115     dictitem_T	*item;
3116     char_u	*whitep = *arg;
3117     char_u	*p;
3118 
3119     if (d == NULL)
3120 	return FAIL;
3121     *arg = skipwhite(*arg + 1);
3122     for (;;)
3123     {
3124 	char_u *key = NULL;
3125 
3126 	if (may_get_next_line(whitep, arg, cctx) == FAIL)
3127 	{
3128 	    *arg = NULL;
3129 	    goto failret;
3130 	}
3131 
3132 	if (**arg == '}')
3133 	    break;
3134 
3135 	if (literal)
3136 	{
3137 	    char_u *end = to_name_end(*arg, !literal);
3138 
3139 	    if (end == *arg)
3140 	    {
3141 		semsg(_("E1014: Invalid key: %s"), *arg);
3142 		return FAIL;
3143 	    }
3144 	    key = vim_strnsave(*arg, end - *arg);
3145 	    if (generate_PUSHS(cctx, key) == FAIL)
3146 		return FAIL;
3147 	    *arg = end;
3148 	}
3149 	else
3150 	{
3151 	    isn_T		*isn;
3152 
3153 	    if (compile_expr0(arg, cctx) == FAIL)
3154 		return FAIL;
3155 	    // TODO: check type is string
3156 	    isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3157 	    if (isn->isn_type == ISN_PUSHS)
3158 		key = isn->isn_arg.string;
3159 	}
3160 
3161 	// Check for duplicate keys, if using string keys.
3162 	if (key != NULL)
3163 	{
3164 	    item = dict_find(d, key, -1);
3165 	    if (item != NULL)
3166 	    {
3167 		semsg(_(e_duplicate_key), key);
3168 		goto failret;
3169 	    }
3170 	    item = dictitem_alloc(key);
3171 	    if (item != NULL)
3172 	    {
3173 		item->di_tv.v_type = VAR_UNKNOWN;
3174 		item->di_tv.v_lock = 0;
3175 		if (dict_add(d, item) == FAIL)
3176 		    dictitem_free(item);
3177 	    }
3178 	}
3179 
3180 	*arg = skipwhite(*arg);
3181 	if (**arg != ':')
3182 	{
3183 	    semsg(_(e_missing_dict_colon), *arg);
3184 	    return FAIL;
3185 	}
3186 
3187 	whitep = *arg + 1;
3188 	*arg = skipwhite(*arg + 1);
3189 	if (may_get_next_line(whitep, arg, cctx) == FAIL)
3190 	{
3191 	    *arg = NULL;
3192 	    goto failret;
3193 	}
3194 
3195 	if (compile_expr0(arg, cctx) == FAIL)
3196 	    return FAIL;
3197 	++count;
3198 
3199 	whitep = *arg;
3200 	*arg = skipwhite(*arg);
3201 	if (may_get_next_line(whitep, arg, cctx) == FAIL)
3202 	{
3203 	    *arg = NULL;
3204 	    goto failret;
3205 	}
3206 	if (**arg == '}')
3207 	    break;
3208 	if (**arg != ',')
3209 	{
3210 	    semsg(_(e_missing_dict_comma), *arg);
3211 	    goto failret;
3212 	}
3213 	whitep = *arg + 1;
3214 	*arg = skipwhite(*arg + 1);
3215     }
3216 
3217     *arg = *arg + 1;
3218 
3219     // Allow for following comment, after at least one space.
3220     p = skipwhite(*arg);
3221     if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
3222 	*arg += STRLEN(*arg);
3223 
3224     dict_unref(d);
3225     return generate_NEWDICT(cctx, count);
3226 
3227 failret:
3228     if (*arg == NULL)
3229 	semsg(_(e_missing_dict_end), _("[end of lines]"));
3230     dict_unref(d);
3231     return FAIL;
3232 }
3233 
3234 /*
3235  * Compile "&option".
3236  */
3237     static int
3238 compile_get_option(char_u **arg, cctx_T *cctx)
3239 {
3240     typval_T	rettv;
3241     char_u	*start = *arg;
3242     int		ret;
3243 
3244     // parse the option and get the current value to get the type.
3245     rettv.v_type = VAR_UNKNOWN;
3246     ret = get_option_tv(arg, &rettv, TRUE);
3247     if (ret == OK)
3248     {
3249 	// include the '&' in the name, get_option_tv() expects it.
3250 	char_u *name = vim_strnsave(start, *arg - start);
3251 	type_T	*type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3252 
3253 	ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3254 	vim_free(name);
3255     }
3256     clear_tv(&rettv);
3257 
3258     return ret;
3259 }
3260 
3261 /*
3262  * Compile "$VAR".
3263  */
3264     static int
3265 compile_get_env(char_u **arg, cctx_T *cctx)
3266 {
3267     char_u	*start = *arg;
3268     int		len;
3269     int		ret;
3270     char_u	*name;
3271 
3272     ++*arg;
3273     len = get_env_len(arg);
3274     if (len == 0)
3275     {
3276 	semsg(_(e_syntax_at), start - 1);
3277 	return FAIL;
3278     }
3279 
3280     // include the '$' in the name, get_env_tv() expects it.
3281     name = vim_strnsave(start, len + 1);
3282     ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3283     vim_free(name);
3284     return ret;
3285 }
3286 
3287 /*
3288  * Compile "@r".
3289  */
3290     static int
3291 compile_get_register(char_u **arg, cctx_T *cctx)
3292 {
3293     int		ret;
3294 
3295     ++*arg;
3296     if (**arg == NUL)
3297     {
3298 	semsg(_(e_syntax_at), *arg - 1);
3299 	return FAIL;
3300     }
3301     if (!valid_yank_reg(**arg, TRUE))
3302     {
3303 	emsg_invreg(**arg);
3304 	return FAIL;
3305     }
3306     ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3307     ++*arg;
3308     return ret;
3309 }
3310 
3311 /*
3312  * Apply leading '!', '-' and '+' to constant "rettv".
3313  */
3314     static int
3315 apply_leader(typval_T *rettv, char_u *start, char_u *end)
3316 {
3317     char_u *p = end;
3318 
3319     // this works from end to start
3320     while (p > start)
3321     {
3322 	--p;
3323 	if (*p == '-' || *p == '+')
3324 	{
3325 	    // only '-' has an effect, for '+' we only check the type
3326 #ifdef FEAT_FLOAT
3327 	    if (rettv->v_type == VAR_FLOAT)
3328 	    {
3329 		if (*p == '-')
3330 		    rettv->vval.v_float = -rettv->vval.v_float;
3331 	    }
3332 	    else
3333 #endif
3334 	    {
3335 		varnumber_T	val;
3336 		int		error = FALSE;
3337 
3338 		// tv_get_number_chk() accepts a string, but we don't want that
3339 		// here
3340 		if (check_not_string(rettv) == FAIL)
3341 		    return FAIL;
3342 		val = tv_get_number_chk(rettv, &error);
3343 		clear_tv(rettv);
3344 		if (error)
3345 		    return FAIL;
3346 		if (*p == '-')
3347 		    val = -val;
3348 		rettv->v_type = VAR_NUMBER;
3349 		rettv->vval.v_number = val;
3350 	    }
3351 	}
3352 	else
3353 	{
3354 	    int v = tv2bool(rettv);
3355 
3356 	    // '!' is permissive in the type.
3357 	    clear_tv(rettv);
3358 	    rettv->v_type = VAR_BOOL;
3359 	    rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3360 	}
3361     }
3362     return OK;
3363 }
3364 
3365 /*
3366  * Recognize v: variables that are constants and set "rettv".
3367  */
3368     static void
3369 get_vim_constant(char_u **arg, typval_T *rettv)
3370 {
3371     if (STRNCMP(*arg, "v:true", 6) == 0)
3372     {
3373 	rettv->v_type = VAR_BOOL;
3374 	rettv->vval.v_number = VVAL_TRUE;
3375 	*arg += 6;
3376     }
3377     else if (STRNCMP(*arg, "v:false", 7) == 0)
3378     {
3379 	rettv->v_type = VAR_BOOL;
3380 	rettv->vval.v_number = VVAL_FALSE;
3381 	*arg += 7;
3382     }
3383     else if (STRNCMP(*arg, "v:null", 6) == 0)
3384     {
3385 	rettv->v_type = VAR_SPECIAL;
3386 	rettv->vval.v_number = VVAL_NULL;
3387 	*arg += 6;
3388     }
3389     else if (STRNCMP(*arg, "v:none", 6) == 0)
3390     {
3391 	rettv->v_type = VAR_SPECIAL;
3392 	rettv->vval.v_number = VVAL_NONE;
3393 	*arg += 6;
3394     }
3395 }
3396 
3397     static exptype_T
3398 get_compare_type(char_u *p, int *len, int *type_is)
3399 {
3400     exptype_T	type = EXPR_UNKNOWN;
3401     int		i;
3402 
3403     switch (p[0])
3404     {
3405 	case '=':   if (p[1] == '=')
3406 			type = EXPR_EQUAL;
3407 		    else if (p[1] == '~')
3408 			type = EXPR_MATCH;
3409 		    break;
3410 	case '!':   if (p[1] == '=')
3411 			type = EXPR_NEQUAL;
3412 		    else if (p[1] == '~')
3413 			type = EXPR_NOMATCH;
3414 		    break;
3415 	case '>':   if (p[1] != '=')
3416 		    {
3417 			type = EXPR_GREATER;
3418 			*len = 1;
3419 		    }
3420 		    else
3421 			type = EXPR_GEQUAL;
3422 		    break;
3423 	case '<':   if (p[1] != '=')
3424 		    {
3425 			type = EXPR_SMALLER;
3426 			*len = 1;
3427 		    }
3428 		    else
3429 			type = EXPR_SEQUAL;
3430 		    break;
3431 	case 'i':   if (p[1] == 's')
3432 		    {
3433 			// "is" and "isnot"; but not a prefix of a name
3434 			if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3435 			    *len = 5;
3436 			i = p[*len];
3437 			if (!isalnum(i) && i != '_')
3438 			{
3439 			    type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3440 			    *type_is = TRUE;
3441 			}
3442 		    }
3443 		    break;
3444     }
3445     return type;
3446 }
3447 
3448 /*
3449  * Compile code to apply '-', '+' and '!'.
3450  */
3451     static int
3452 compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3453 {
3454     char_u	*p = end;
3455 
3456     // this works from end to start
3457     while (p > start)
3458     {
3459 	--p;
3460 	if (*p == '-' || *p == '+')
3461 	{
3462 	    int	    negate = *p == '-';
3463 	    isn_T   *isn;
3464 
3465 	    // TODO: check type
3466 	    while (p > start && (p[-1] == '-' || p[-1] == '+'))
3467 	    {
3468 		--p;
3469 		if (*p == '-')
3470 		    negate = !negate;
3471 	    }
3472 	    // only '-' has an effect, for '+' we only check the type
3473 	    if (negate)
3474 		isn = generate_instr(cctx, ISN_NEGATENR);
3475 	    else
3476 		isn = generate_instr(cctx, ISN_CHECKNR);
3477 	    if (isn == NULL)
3478 		return FAIL;
3479 	}
3480 	else
3481 	{
3482 	    int  invert = TRUE;
3483 
3484 	    while (p > start && p[-1] == '!')
3485 	    {
3486 		--p;
3487 		invert = !invert;
3488 	    }
3489 	    if (generate_2BOOL(cctx, invert) == FAIL)
3490 		return FAIL;
3491 	}
3492     }
3493     return OK;
3494 }
3495 
3496 /*
3497  * Compile whatever comes after "name" or "name()".
3498  */
3499     static int
3500 compile_subscript(
3501 	char_u **arg,
3502 	cctx_T *cctx,
3503 	char_u **start_leader,
3504 	char_u *end_leader,
3505 	ppconst_T *ppconst)
3506 {
3507     for (;;)
3508     {
3509 	char_u *p = skipwhite(*arg);
3510 
3511 	if (*p == NUL || (VIM_ISWHITE(**arg) && comment_start(p)))
3512 	{
3513 	    char_u *next = peek_next_line(cctx);
3514 
3515 	    // If a following line starts with "->{" or "->X" advance to that
3516 	    // line, so that a line break before "->" is allowed.
3517 	    if (next != NULL && next[0] == '-' && next[1] == '>'
3518 		    && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3519 	    {
3520 		next = next_line_from_context(cctx, TRUE);
3521 		if (next == NULL)
3522 		    return FAIL;
3523 		*arg = skipwhite(next);
3524 	    }
3525 	}
3526 
3527 	if (**arg == '(')
3528 	{
3529 	    garray_T    *stack = &cctx->ctx_type_stack;
3530 	    type_T	*type;
3531 	    int		argcount = 0;
3532 
3533 	    if (generate_ppconst(cctx, ppconst) == FAIL)
3534 		return FAIL;
3535 
3536 	    // funcref(arg)
3537 	    type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3538 
3539 	    *arg = skipwhite(*arg + 1);
3540 	    if (compile_arguments(arg, cctx, &argcount) == FAIL)
3541 		return FAIL;
3542 	    if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
3543 		return FAIL;
3544 	}
3545 	else if (**arg == '-' && (*arg)[1] == '>')
3546 	{
3547 	    if (generate_ppconst(cctx, ppconst) == FAIL)
3548 		return FAIL;
3549 
3550 	    // something->method()
3551 	    // Apply the '!', '-' and '+' first:
3552 	    //   -1.0->func() works like (-1.0)->func()
3553 	    if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3554 		return FAIL;
3555 	    *start_leader = end_leader;   // don't apply again later
3556 
3557 	    p = *arg + 2;
3558 	    *arg = skipwhite(p);
3559 	    if (may_get_next_line(p, arg, cctx) == FAIL)
3560 		return FAIL;
3561 	    if (**arg == '{')
3562 	    {
3563 		// lambda call:  list->{lambda}
3564 		if (compile_lambda_call(arg, cctx) == FAIL)
3565 		    return FAIL;
3566 	    }
3567 	    else
3568 	    {
3569 		// method call:  list->method()
3570 		p = *arg;
3571 		if (ASCII_ISALPHA(*p) && p[1] == ':')
3572 		    p += 2;
3573 		for ( ; eval_isnamec1(*p); ++p)
3574 		    ;
3575 		if (*p != '(')
3576 		{
3577 		    semsg(_(e_missing_paren), *arg);
3578 		    return FAIL;
3579 		}
3580 		// TODO: base value may not be the first argument
3581 		if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
3582 		    return FAIL;
3583 	    }
3584 	}
3585 	else if (**arg == '[')
3586 	{
3587 	    garray_T	*stack = &cctx->ctx_type_stack;
3588 	    type_T	**typep;
3589 
3590 	    // list index: list[123]
3591 	    // dict member: dict[key]
3592 	    // TODO: blob index
3593 	    // TODO: more arguments
3594 	    // TODO: recognize list or dict at runtime
3595 	    if (generate_ppconst(cctx, ppconst) == FAIL)
3596 		return FAIL;
3597 
3598 	    p = *arg + 1;
3599 	    *arg = skipwhite(p);
3600 	    if (may_get_next_line(p, arg, cctx) == FAIL)
3601 		return FAIL;
3602 	    if (compile_expr0(arg, cctx) == FAIL)
3603 		return FAIL;
3604 
3605 	    if (**arg != ']')
3606 	    {
3607 		emsg(_(e_missbrac));
3608 		return FAIL;
3609 	    }
3610 	    *arg = *arg + 1;
3611 
3612 	    typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3613 	    if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
3614 	    {
3615 		if ((*typep)->tt_type == VAR_LIST)
3616 		    *typep = (*typep)->tt_member;
3617 		if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3618 		    return FAIL;
3619 	    }
3620 	    else if ((*typep)->tt_type == VAR_DICT)
3621 	    {
3622 		*typep = (*typep)->tt_member;
3623 		if (may_generate_2STRING(-1, cctx) == FAIL)
3624 		    return FAIL;
3625 		if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3626 		    return FAIL;
3627 	    }
3628 	    else
3629 	    {
3630 		emsg(_(e_listdictblobreq));
3631 		return FAIL;
3632 	    }
3633 	}
3634 	else if (**arg == '.' && (*arg)[1] != '.')
3635 	{
3636 	    if (generate_ppconst(cctx, ppconst) == FAIL)
3637 		return FAIL;
3638 
3639 	    ++*arg;
3640 	    if (may_get_next_line(*arg, arg, cctx) == FAIL)
3641 		return FAIL;
3642 	    // dictionary member: dict.name
3643 	    p = *arg;
3644 	    if (eval_isnamec1(*p))
3645 		while (eval_isnamec(*p))
3646 		    MB_PTR_ADV(p);
3647 	    if (p == *arg)
3648 	    {
3649 		semsg(_(e_syntax_at), *arg);
3650 		return FAIL;
3651 	    }
3652 	    if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
3653 		return FAIL;
3654 	    *arg = p;
3655 	}
3656 	else
3657 	    break;
3658     }
3659 
3660     // TODO - see handle_subscript():
3661     // Turn "dict.Func" into a partial for "Func" bound to "dict".
3662     // Don't do this when "Func" is already a partial that was bound
3663     // explicitly (pt_auto is FALSE).
3664 
3665     return OK;
3666 }
3667 
3668 /*
3669  * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3670  * "arg" is advanced until after the expression, skipping white space.
3671  *
3672  * If the value is a constant "ppconst->pp_ret" will be set.
3673  * Before instructions are generated, any values in "ppconst" will generated.
3674  *
3675  * This is the compiling equivalent of eval1(), eval2(), etc.
3676  */
3677 
3678 /*
3679  *  number		number constant
3680  *  0zFFFFFFFF		Blob constant
3681  *  "string"		string constant
3682  *  'string'		literal string constant
3683  *  &option-name	option value
3684  *  @r			register contents
3685  *  identifier		variable value
3686  *  function()		function call
3687  *  $VAR		environment variable
3688  *  (expression)	nested expression
3689  *  [expr, expr]	List
3690  *  {key: val, key: val}   Dictionary
3691  *  #{key: val, key: val}  Dictionary with literal keys
3692  *
3693  *  Also handle:
3694  *  ! in front		logical NOT
3695  *  - in front		unary minus
3696  *  + in front		unary plus (ignored)
3697  *  trailing (arg)	funcref/partial call
3698  *  trailing []		subscript in String or List
3699  *  trailing .name	entry in Dictionary
3700  *  trailing ->name()	method call
3701  */
3702     static int
3703 compile_expr7(
3704 	char_u **arg,
3705 	cctx_T *cctx,
3706 	ppconst_T *ppconst)
3707 {
3708     char_u	*start_leader, *end_leader;
3709     int		ret = OK;
3710     typval_T	*rettv = &ppconst->pp_tv[ppconst->pp_used];
3711     int		used_before = ppconst->pp_used;
3712 
3713     /*
3714      * Skip '!', '-' and '+' characters.  They are handled later.
3715      */
3716     start_leader = *arg;
3717     while (**arg == '!' || **arg == '-' || **arg == '+')
3718 	*arg = skipwhite(*arg + 1);
3719     end_leader = *arg;
3720 
3721     rettv->v_type = VAR_UNKNOWN;
3722     switch (**arg)
3723     {
3724 	/*
3725 	 * Number constant.
3726 	 */
3727 	case '0':	// also for blob starting with 0z
3728 	case '1':
3729 	case '2':
3730 	case '3':
3731 	case '4':
3732 	case '5':
3733 	case '6':
3734 	case '7':
3735 	case '8':
3736 	case '9':
3737 	case '.':   if (get_number_tv(arg, rettv, TRUE, FALSE) == FAIL)
3738 			return FAIL;
3739 		    break;
3740 
3741 	/*
3742 	 * String constant: "string".
3743 	 */
3744 	case '"':   if (get_string_tv(arg, rettv, TRUE) == FAIL)
3745 			return FAIL;
3746 		    break;
3747 
3748 	/*
3749 	 * Literal string constant: 'str''ing'.
3750 	 */
3751 	case '\'':  if (get_lit_string_tv(arg, rettv, TRUE) == FAIL)
3752 			return FAIL;
3753 		    break;
3754 
3755 	/*
3756 	 * Constant Vim variable.
3757 	 */
3758 	case 'v':   get_vim_constant(arg, rettv);
3759 		    ret = NOTDONE;
3760 		    break;
3761 
3762 	/*
3763 	 * "true" constant
3764 	 */
3765 	case 't':   if (STRNCMP(*arg, "true", 4) == 0
3766 						   && !eval_isnamec((*arg)[4]))
3767 		    {
3768 			*arg += 4;
3769 			rettv->v_type = VAR_BOOL;
3770 			rettv->vval.v_number = VVAL_TRUE;
3771 		    }
3772 		    else
3773 			ret = NOTDONE;
3774 		    break;
3775 
3776 	/*
3777 	 * "false" constant
3778 	 */
3779 	case 'f':   if (STRNCMP(*arg, "false", 5) == 0
3780 						   && !eval_isnamec((*arg)[5]))
3781 		    {
3782 			*arg += 5;
3783 			rettv->v_type = VAR_BOOL;
3784 			rettv->vval.v_number = VVAL_FALSE;
3785 		    }
3786 		    else
3787 			ret = NOTDONE;
3788 		    break;
3789 
3790 	/*
3791 	 * List: [expr, expr]
3792 	 */
3793 	case '[':   ret = compile_list(arg, cctx);
3794 		    break;
3795 
3796 	/*
3797 	 * Dictionary: #{key: val, key: val}
3798 	 */
3799 	case '#':   if ((*arg)[1] == '{')
3800 		    {
3801 			++*arg;
3802 			ret = compile_dict(arg, cctx, TRUE);
3803 		    }
3804 		    else
3805 			ret = NOTDONE;
3806 		    break;
3807 
3808 	/*
3809 	 * Lambda: {arg, arg -> expr}
3810 	 * Dictionary: {'key': val, 'key': val}
3811 	 */
3812 	case '{':   {
3813 			char_u *start = skipwhite(*arg + 1);
3814 
3815 			// Find out what comes after the arguments.
3816 			// TODO: pass getline function
3817 			ret = get_function_args(&start, '-', NULL,
3818 					   NULL, NULL, NULL, TRUE, NULL, NULL);
3819 			if (ret != FAIL && *start == '>')
3820 			    ret = compile_lambda(arg, cctx);
3821 			else
3822 			    ret = compile_dict(arg, cctx, FALSE);
3823 		    }
3824 		    break;
3825 
3826 	/*
3827 	 * Option value: &name
3828 	 */
3829 	case '&':	ret = compile_get_option(arg, cctx);
3830 			break;
3831 
3832 	/*
3833 	 * Environment variable: $VAR.
3834 	 */
3835 	case '$':	ret = compile_get_env(arg, cctx);
3836 			break;
3837 
3838 	/*
3839 	 * Register contents: @r.
3840 	 */
3841 	case '@':	ret = compile_get_register(arg, cctx);
3842 			break;
3843 	/*
3844 	 * nested expression: (expression).
3845 	 */
3846 	case '(':   *arg = skipwhite(*arg + 1);
3847 
3848 		    // recursive!
3849 		    if (ppconst->pp_used <= PPSIZE - 10)
3850 		    {
3851 			ret = compile_expr1(arg, cctx, ppconst);
3852 		    }
3853 		    else
3854 		    {
3855 			// Not enough space in ppconst, flush constants.
3856 			if (generate_ppconst(cctx, ppconst) == FAIL)
3857 			    return FAIL;
3858 			ret = compile_expr0(arg, cctx);
3859 		    }
3860 		    *arg = skipwhite(*arg);
3861 		    if (**arg == ')')
3862 			++*arg;
3863 		    else if (ret == OK)
3864 		    {
3865 			emsg(_(e_missing_close));
3866 			ret = FAIL;
3867 		    }
3868 		    break;
3869 
3870 	default:    ret = NOTDONE;
3871 		    break;
3872     }
3873     if (ret == FAIL)
3874 	return FAIL;
3875 
3876     if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
3877     {
3878 	// apply the '!', '-' and '+' before the constant
3879 	if (apply_leader(rettv, start_leader, end_leader) == FAIL)
3880 	{
3881 	    clear_tv(rettv);
3882 	    return FAIL;
3883 	}
3884 	start_leader = end_leader;   // don't apply again below
3885 
3886 	if (cctx->ctx_skip == SKIP_YES)
3887 	    clear_tv(rettv);
3888 	else
3889 	    // A constant expression can possibly be handled compile time,
3890 	    // return the value instead of generating code.
3891 	    ++ppconst->pp_used;
3892     }
3893     else if (ret == NOTDONE)
3894     {
3895 	char_u	    *p;
3896 	int	    r;
3897 
3898 	if (!eval_isnamec1(**arg))
3899 	{
3900 	    semsg(_("E1015: Name expected: %s"), *arg);
3901 	    return FAIL;
3902 	}
3903 
3904 	// "name" or "name()"
3905 	p = to_name_end(*arg, TRUE);
3906 	if (*p == '(')
3907 	{
3908 	    r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3909 	}
3910 	else
3911 	{
3912 	    if (generate_ppconst(cctx, ppconst) == FAIL)
3913 		return FAIL;
3914 	    r = compile_load(arg, p, cctx, TRUE);
3915 	}
3916 	if (r == FAIL)
3917 	    return FAIL;
3918     }
3919 
3920     // Handle following "[]", ".member", etc.
3921     // Then deal with prefixed '-', '+' and '!', if not done already.
3922     if (compile_subscript(arg, cctx, &start_leader, end_leader,
3923 							     ppconst) == FAIL)
3924 	return FAIL;
3925     if (ppconst->pp_used > 0)
3926     {
3927 	// apply the '!', '-' and '+' before the constant
3928 	rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3929 	if (apply_leader(rettv, start_leader, end_leader) == FAIL)
3930 	    return FAIL;
3931 	return OK;
3932     }
3933     if (compile_leader(cctx, start_leader, end_leader) == FAIL)
3934 	return FAIL;
3935     return OK;
3936 }
3937 
3938 /*
3939  *	*	number multiplication
3940  *	/	number division
3941  *	%	number modulo
3942  */
3943     static int
3944 compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3945 {
3946     char_u	*op;
3947     int		ppconst_used = ppconst->pp_used;
3948 
3949     // get the first expression
3950     if (compile_expr7(arg, cctx, ppconst) == FAIL)
3951 	return FAIL;
3952 
3953     /*
3954      * Repeat computing, until no "*", "/" or "%" is following.
3955      */
3956     for (;;)
3957     {
3958 	op = skipwhite(*arg);
3959 	if (*op != '*' && *op != '/' && *op != '%')
3960 	    break;
3961 
3962 	if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
3963 	{
3964 	    char_u buf[3];
3965 
3966 	    vim_strncpy(buf, op, 1);
3967 	    semsg(_(e_white_both), buf);
3968 	    return FAIL;
3969 	}
3970 	*arg = skipwhite(op + 1);
3971 	if (may_get_next_line(op + 1, arg, cctx) == FAIL)
3972 	    return FAIL;
3973 
3974 	// get the second expression
3975 	if (compile_expr7(arg, cctx, ppconst) == FAIL)
3976 	    return FAIL;
3977 
3978 	if (ppconst->pp_used == ppconst_used + 2
3979 		&& ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3980 		&& ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
3981 	{
3982 	    typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3983 	    typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
3984 	    varnumber_T res = 0;
3985 
3986 	    // both are numbers: compute the result
3987 	    switch (*op)
3988 	    {
3989 		case '*': res = tv1->vval.v_number * tv2->vval.v_number;
3990 			  break;
3991 		case '/': res = tv1->vval.v_number / tv2->vval.v_number;
3992 			  break;
3993 		case '%': res = tv1->vval.v_number % tv2->vval.v_number;
3994 			  break;
3995 	    }
3996 	    tv1->vval.v_number = res;
3997 	    --ppconst->pp_used;
3998 	}
3999 	else
4000 	{
4001 	    generate_ppconst(cctx, ppconst);
4002 	    generate_two_op(cctx, op);
4003 	}
4004     }
4005 
4006     return OK;
4007 }
4008 
4009 /*
4010  *      +	number addition
4011  *      -	number subtraction
4012  *      ..	string concatenation
4013  */
4014     static int
4015 compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4016 {
4017     char_u	*op;
4018     int		oplen;
4019     int		ppconst_used = ppconst->pp_used;
4020 
4021     // get the first variable
4022     if (compile_expr6(arg, cctx, ppconst) == FAIL)
4023 	return FAIL;
4024 
4025     /*
4026      * Repeat computing, until no "+", "-" or ".." is following.
4027      */
4028     for (;;)
4029     {
4030 	op = skipwhite(*arg);
4031 	if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
4032 	    break;
4033 	oplen = (*op == '.' ? 2 : 1);
4034 
4035 	if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
4036 	{
4037 	    char_u buf[3];
4038 
4039 	    vim_strncpy(buf, op, oplen);
4040 	    semsg(_(e_white_both), buf);
4041 	    return FAIL;
4042 	}
4043 
4044 	*arg = skipwhite(op + oplen);
4045 	if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
4046 	    return FAIL;
4047 
4048 	// get the second expression
4049 	if (compile_expr6(arg, cctx, ppconst) == FAIL)
4050 	    return FAIL;
4051 
4052 	if (ppconst->pp_used == ppconst_used + 2
4053 		&& (*op == '.'
4054 		    ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4055 		    && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4056 		    : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4057 		    && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
4058 	{
4059 	    typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4060 	    typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4061 
4062 	    // concat/subtract/add constant numbers
4063 	    if (*op == '+')
4064 		tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4065 	    else if (*op == '-')
4066 		tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4067 	    else
4068 	    {
4069 		// concatenate constant strings
4070 		char_u *s1 = tv1->vval.v_string;
4071 		char_u *s2 = tv2->vval.v_string;
4072 		size_t len1 = STRLEN(s1);
4073 
4074 		tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4075 		if (tv1->vval.v_string == NULL)
4076 		{
4077 		    clear_ppconst(ppconst);
4078 		    return FAIL;
4079 		}
4080 		mch_memmove(tv1->vval.v_string, s1, len1);
4081 		STRCPY(tv1->vval.v_string + len1, s2);
4082 		vim_free(s1);
4083 		vim_free(s2);
4084 	    }
4085 	    --ppconst->pp_used;
4086 	}
4087 	else
4088 	{
4089 	    generate_ppconst(cctx, ppconst);
4090 	    if (*op == '.')
4091 	    {
4092 		if (may_generate_2STRING(-2, cctx) == FAIL
4093 			|| may_generate_2STRING(-1, cctx) == FAIL)
4094 		    return FAIL;
4095 		generate_instr_drop(cctx, ISN_CONCAT, 1);
4096 	    }
4097 	    else
4098 		generate_two_op(cctx, op);
4099 	}
4100     }
4101 
4102     return OK;
4103 }
4104 
4105 /*
4106  * expr5a == expr5b
4107  * expr5a =~ expr5b
4108  * expr5a != expr5b
4109  * expr5a !~ expr5b
4110  * expr5a > expr5b
4111  * expr5a >= expr5b
4112  * expr5a < expr5b
4113  * expr5a <= expr5b
4114  * expr5a is expr5b
4115  * expr5a isnot expr5b
4116  *
4117  * Produces instructions:
4118  *	EVAL expr5a		Push result of "expr5a"
4119  *	EVAL expr5b		Push result of "expr5b"
4120  *	COMPARE			one of the compare instructions
4121  */
4122     static int
4123 compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4124 {
4125     exptype_T	type = EXPR_UNKNOWN;
4126     char_u	*p;
4127     int		len = 2;
4128     int		type_is = FALSE;
4129     int		ppconst_used = ppconst->pp_used;
4130 
4131     // get the first variable
4132     if (compile_expr5(arg, cctx, ppconst) == FAIL)
4133 	return FAIL;
4134 
4135     p = skipwhite(*arg);
4136     type = get_compare_type(p, &len, &type_is);
4137 
4138     /*
4139      * If there is a comparative operator, use it.
4140      */
4141     if (type != EXPR_UNKNOWN)
4142     {
4143 	int ic = FALSE;  // Default: do not ignore case
4144 
4145 	if (type_is && (p[len] == '?' || p[len] == '#'))
4146 	{
4147 	    semsg(_(e_invexpr2), *arg);
4148 	    return FAIL;
4149 	}
4150 	// extra question mark appended: ignore case
4151 	if (p[len] == '?')
4152 	{
4153 	    ic = TRUE;
4154 	    ++len;
4155 	}
4156 	// extra '#' appended: match case (ignored)
4157 	else if (p[len] == '#')
4158 	    ++len;
4159 	// nothing appended: match case
4160 
4161 	if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
4162 	{
4163 	    char_u buf[7];
4164 
4165 	    vim_strncpy(buf, p, len);
4166 	    semsg(_(e_white_both), buf);
4167 	    return FAIL;
4168 	}
4169 
4170 	// get the second variable
4171 	*arg = skipwhite(p + len);
4172 	if (may_get_next_line(p + len, arg, cctx) == FAIL)
4173 	    return FAIL;
4174 
4175 	if (compile_expr5(arg, cctx, ppconst) == FAIL)
4176 	    return FAIL;
4177 
4178 	if (ppconst->pp_used == ppconst_used + 2)
4179 	{
4180 	    typval_T *	tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4181 	    typval_T	*tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4182 	    int		ret;
4183 
4184 	    // Both sides are a constant, compute the result now.
4185 	    // First check for a valid combination of types, this is more
4186 	    // strict than typval_compare().
4187 	    if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
4188 		ret = FAIL;
4189 	    else
4190 	    {
4191 		ret = typval_compare(tv1, tv2, type, ic);
4192 		tv1->v_type = VAR_BOOL;
4193 		tv1->vval.v_number = tv1->vval.v_number
4194 						      ? VVAL_TRUE : VVAL_FALSE;
4195 		clear_tv(tv2);
4196 		--ppconst->pp_used;
4197 	    }
4198 	    return ret;
4199 	}
4200 
4201 	generate_ppconst(cctx, ppconst);
4202 	return generate_COMPARE(cctx, type, ic);
4203     }
4204 
4205     return OK;
4206 }
4207 
4208 static int compile_expr3(char_u **arg,  cctx_T *cctx, ppconst_T *ppconst);
4209 
4210 /*
4211  * Compile || or &&.
4212  */
4213     static int
4214 compile_and_or(
4215 	char_u **arg,
4216 	cctx_T	*cctx,
4217 	char	*op,
4218 	ppconst_T *ppconst,
4219 	int	ppconst_used UNUSED)
4220 {
4221     char_u	*p = skipwhite(*arg);
4222     int		opchar = *op;
4223 
4224     if (p[0] == opchar && p[1] == opchar)
4225     {
4226 	garray_T	*instr = &cctx->ctx_instr;
4227 	garray_T	end_ga;
4228 
4229 	/*
4230 	 * Repeat until there is no following "||" or "&&"
4231 	 */
4232 	ga_init2(&end_ga, sizeof(int), 10);
4233 	while (p[0] == opchar && p[1] == opchar)
4234 	{
4235 	    if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4236 	    {
4237 		semsg(_(e_white_both), op);
4238 		return FAIL;
4239 	    }
4240 
4241 	    // TODO: use ppconst if the value is a constant
4242 	    generate_ppconst(cctx, ppconst);
4243 
4244 	    if (ga_grow(&end_ga, 1) == FAIL)
4245 	    {
4246 		ga_clear(&end_ga);
4247 		return FAIL;
4248 	    }
4249 	    *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4250 	    ++end_ga.ga_len;
4251 	    generate_JUMP(cctx, opchar == '|'
4252 			 ?  JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4253 
4254 	    // eval the next expression
4255 	    *arg = skipwhite(p + 2);
4256 	    if (may_get_next_line(p + 2, arg, cctx) == FAIL)
4257 		return FAIL;
4258 
4259 	    if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4260 				  : compile_expr4(arg, cctx, ppconst)) == FAIL)
4261 	    {
4262 		ga_clear(&end_ga);
4263 		return FAIL;
4264 	    }
4265 	    p = skipwhite(*arg);
4266 	}
4267 	generate_ppconst(cctx, ppconst);
4268 
4269 	// Fill in the end label in all jumps.
4270 	while (end_ga.ga_len > 0)
4271 	{
4272 	    isn_T	*isn;
4273 
4274 	    --end_ga.ga_len;
4275 	    isn = ((isn_T *)instr->ga_data)
4276 				  + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4277 	    isn->isn_arg.jump.jump_where = instr->ga_len;
4278 	}
4279 	ga_clear(&end_ga);
4280     }
4281 
4282     return OK;
4283 }
4284 
4285 /*
4286  * expr4a && expr4a && expr4a	    logical AND
4287  *
4288  * Produces instructions:
4289  *	EVAL expr4a		Push result of "expr4a"
4290  *	JUMP_AND_KEEP_IF_FALSE end
4291  *	EVAL expr4b		Push result of "expr4b"
4292  *	JUMP_AND_KEEP_IF_FALSE end
4293  *	EVAL expr4c		Push result of "expr4c"
4294  * end:
4295  */
4296     static int
4297 compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4298 {
4299     int		ppconst_used = ppconst->pp_used;
4300 
4301     // get the first variable
4302     if (compile_expr4(arg, cctx, ppconst) == FAIL)
4303 	return FAIL;
4304 
4305     // || and && work almost the same
4306     return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
4307 }
4308 
4309 /*
4310  * expr3a || expr3b || expr3c	    logical OR
4311  *
4312  * Produces instructions:
4313  *	EVAL expr3a		Push result of "expr3a"
4314  *	JUMP_AND_KEEP_IF_TRUE end
4315  *	EVAL expr3b		Push result of "expr3b"
4316  *	JUMP_AND_KEEP_IF_TRUE end
4317  *	EVAL expr3c		Push result of "expr3c"
4318  * end:
4319  */
4320     static int
4321 compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4322 {
4323     int		ppconst_used = ppconst->pp_used;
4324 
4325     // eval the first expression
4326     if (compile_expr3(arg, cctx, ppconst) == FAIL)
4327 	return FAIL;
4328 
4329     // || and && work almost the same
4330     return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
4331 }
4332 
4333 /*
4334  * Toplevel expression: expr2 ? expr1a : expr1b
4335  *
4336  * Produces instructions:
4337  *	EVAL expr2		Push result of "expr"
4338  *      JUMP_IF_FALSE alt	jump if false
4339  *      EVAL expr1a
4340  *      JUMP_ALWAYS end
4341  * alt:	EVAL expr1b
4342  * end:
4343  */
4344     static int
4345 compile_expr1(char_u **arg,  cctx_T *cctx, ppconst_T *ppconst)
4346 {
4347     char_u	*p;
4348     int		ppconst_used = ppconst->pp_used;
4349 
4350     // Evaluate the first expression.
4351     if (compile_expr2(arg, cctx, ppconst) == FAIL)
4352 	return FAIL;
4353 
4354     p = skipwhite(*arg);
4355     if (*p == '?')
4356     {
4357 	garray_T	*instr = &cctx->ctx_instr;
4358 	garray_T	*stack = &cctx->ctx_type_stack;
4359 	int		alt_idx = instr->ga_len;
4360 	int		end_idx;
4361 	isn_T		*isn;
4362 	type_T		*type1;
4363 	type_T		*type2;
4364 	int		has_const_expr = FALSE;
4365 	int		const_value = FALSE;
4366 	int		save_skip = cctx->ctx_skip;
4367 
4368 	if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4369 	{
4370 	    semsg(_(e_white_both), "?");
4371 	    return FAIL;
4372 	}
4373 
4374 	if (ppconst->pp_used == ppconst_used + 1)
4375 	{
4376 	    // the condition is a constant, we know whether the ? or the :
4377 	    // expression is to be evaluated.
4378 	    has_const_expr = TRUE;
4379 	    const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4380 	    clear_tv(&ppconst->pp_tv[ppconst_used]);
4381 	    --ppconst->pp_used;
4382 	    cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4383 							 ? SKIP_YES : SKIP_NOT;
4384 	}
4385 	else
4386 	{
4387 	    generate_ppconst(cctx, ppconst);
4388 	    generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4389 	}
4390 
4391 	// evaluate the second expression; any type is accepted
4392 	*arg = skipwhite(p + 1);
4393 	if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4394 	    return FAIL;
4395 	if (compile_expr1(arg, cctx, ppconst) == FAIL)
4396 	    return FAIL;
4397 
4398 	if (!has_const_expr)
4399 	{
4400 	    generate_ppconst(cctx, ppconst);
4401 
4402 	    // remember the type and drop it
4403 	    --stack->ga_len;
4404 	    type1 = ((type_T **)stack->ga_data)[stack->ga_len];
4405 
4406 	    end_idx = instr->ga_len;
4407 	    generate_JUMP(cctx, JUMP_ALWAYS, 0);
4408 
4409 	    // jump here from JUMP_IF_FALSE
4410 	    isn = ((isn_T *)instr->ga_data) + alt_idx;
4411 	    isn->isn_arg.jump.jump_where = instr->ga_len;
4412 	}
4413 
4414 	// Check for the ":".
4415 	p = skipwhite(*arg);
4416 	if (*p != ':')
4417 	{
4418 	    emsg(_(e_missing_colon));
4419 	    return FAIL;
4420 	}
4421 	if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4422 	{
4423 	    semsg(_(e_white_both), ":");
4424 	    return FAIL;
4425 	}
4426 
4427 	// evaluate the third expression
4428 	if (has_const_expr)
4429 	    cctx->ctx_skip = save_skip == SKIP_YES || const_value
4430 							 ? SKIP_YES : SKIP_NOT;
4431 	*arg = skipwhite(p + 1);
4432 	if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4433 	    return FAIL;
4434 	if (compile_expr1(arg, cctx, ppconst) == FAIL)
4435 	    return FAIL;
4436 
4437 	if (!has_const_expr)
4438 	{
4439 	    generate_ppconst(cctx, ppconst);
4440 
4441 	    // If the types differ, the result has a more generic type.
4442 	    type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4443 	    common_type(type1, type2, &type2, cctx->ctx_type_list);
4444 
4445 	    // jump here from JUMP_ALWAYS
4446 	    isn = ((isn_T *)instr->ga_data) + end_idx;
4447 	    isn->isn_arg.jump.jump_where = instr->ga_len;
4448 	}
4449 
4450 	cctx->ctx_skip = save_skip;
4451     }
4452     return OK;
4453 }
4454 
4455 /*
4456  * Toplevel expression.
4457  */
4458     static int
4459 compile_expr0(char_u **arg,  cctx_T *cctx)
4460 {
4461     ppconst_T	ppconst;
4462 
4463     CLEAR_FIELD(ppconst);
4464     if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4465     {
4466 	clear_ppconst(&ppconst);
4467 	return FAIL;
4468     }
4469     if (generate_ppconst(cctx, &ppconst) == FAIL)
4470 	return FAIL;
4471     return OK;
4472 }
4473 
4474 /*
4475  * compile "return [expr]"
4476  */
4477     static char_u *
4478 compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4479 {
4480     char_u	*p = arg;
4481     garray_T	*stack = &cctx->ctx_type_stack;
4482     type_T	*stack_type;
4483 
4484     if (*p != NUL && *p != '|' && *p != '\n')
4485     {
4486 	// compile return argument into instructions
4487 	if (compile_expr0(&p, cctx) == FAIL)
4488 	    return NULL;
4489 
4490 	stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4491 	if (set_return_type)
4492 	    cctx->ctx_ufunc->uf_ret_type = stack_type;
4493 	else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4494 								       == FAIL)
4495 	    return NULL;
4496     }
4497     else
4498     {
4499 	// "set_return_type" cannot be TRUE, only used for a lambda which
4500 	// always has an argument.
4501 	if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4502 		&& cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
4503 	{
4504 	    emsg(_("E1003: Missing return value"));
4505 	    return NULL;
4506 	}
4507 
4508 	// No argument, return zero.
4509 	generate_PUSHNR(cctx, 0);
4510     }
4511 
4512     if (generate_instr(cctx, ISN_RETURN) == NULL)
4513 	return NULL;
4514 
4515     // "return val | endif" is possible
4516     return skipwhite(p);
4517 }
4518 
4519 /*
4520  * Get a line from the compilation context, compatible with exarg_T getline().
4521  * Return a pointer to the line in allocated memory.
4522  * Return NULL for end-of-file or some error.
4523  */
4524     static char_u *
4525 exarg_getline(
4526 	int c UNUSED,
4527 	void *cookie,
4528 	int indent UNUSED,
4529 	int do_concat UNUSED)
4530 {
4531     cctx_T  *cctx = (cctx_T *)cookie;
4532 
4533     if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4534     {
4535 	iemsg("Heredoc got to end");
4536 	return NULL;
4537     }
4538     ++cctx->ctx_lnum;
4539     return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4540 							     [cctx->ctx_lnum]);
4541 }
4542 
4543 /*
4544  * Compile a nested :def command.
4545  */
4546     static char_u *
4547 compile_nested_function(exarg_T *eap, cctx_T *cctx)
4548 {
4549     char_u	*name_start = eap->arg;
4550     char_u	*name_end = to_name_end(eap->arg, FALSE);
4551     char_u	*name = get_lambda_name();
4552     lvar_T	*lvar;
4553     ufunc_T	*ufunc;
4554 
4555     eap->arg = name_end;
4556     eap->getline = exarg_getline;
4557     eap->cookie = cctx;
4558     eap->skip = cctx->ctx_skip == SKIP_YES;
4559     eap->forceit = FALSE;
4560     ufunc = def_function(eap, name);
4561 
4562     if (ufunc == NULL)
4563 	return NULL;
4564     if (ufunc->uf_def_status == UF_TO_BE_COMPILED
4565 	    && compile_def_function(ufunc, TRUE, cctx) == FAIL)
4566 	return NULL;
4567 
4568     // Define a local variable for the function reference.
4569     lvar = reserve_local(cctx, name_start, name_end - name_start,
4570 						    TRUE, ufunc->uf_func_type);
4571 
4572     if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4573 	    || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4574 	return NULL;
4575 
4576     // TODO: warning for trailing text?
4577     return (char_u *)"";
4578 }
4579 
4580 /*
4581  * Return the length of an assignment operator, or zero if there isn't one.
4582  */
4583     int
4584 assignment_len(char_u *p, int *heredoc)
4585 {
4586     if (*p == '=')
4587     {
4588 	if (p[1] == '<' && p[2] == '<')
4589 	{
4590 	    *heredoc = TRUE;
4591 	    return 3;
4592 	}
4593 	return 1;
4594     }
4595     if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4596 	return 2;
4597     if (STRNCMP(p, "..=", 3) == 0)
4598 	return 3;
4599     return 0;
4600 }
4601 
4602 // words that cannot be used as a variable
4603 static char *reserved[] = {
4604     "true",
4605     "false",
4606     NULL
4607 };
4608 
4609 typedef enum {
4610     dest_local,
4611     dest_option,
4612     dest_env,
4613     dest_global,
4614     dest_buffer,
4615     dest_window,
4616     dest_tab,
4617     dest_vimvar,
4618     dest_script,
4619     dest_reg,
4620 } assign_dest_T;
4621 
4622 /*
4623  * Generate the load instruction for "name".
4624  */
4625     static void
4626 generate_loadvar(
4627 	cctx_T		*cctx,
4628 	assign_dest_T	dest,
4629 	char_u		*name,
4630 	lvar_T		*lvar,
4631 	type_T		*type)
4632 {
4633     switch (dest)
4634     {
4635 	case dest_option:
4636 	    // TODO: check the option exists
4637 	    generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4638 	    break;
4639 	case dest_global:
4640 	    generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4641 	    break;
4642 	case dest_buffer:
4643 	    generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4644 	    break;
4645 	case dest_window:
4646 	    generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4647 	    break;
4648 	case dest_tab:
4649 	    generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4650 	    break;
4651 	case dest_script:
4652 	    compile_load_scriptvar(cctx,
4653 		    name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4654 	    break;
4655 	case dest_env:
4656 	    // Include $ in the name here
4657 	    generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4658 	    break;
4659 	case dest_reg:
4660 	    generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4661 	    break;
4662 	case dest_vimvar:
4663 	    generate_LOADV(cctx, name + 2, TRUE);
4664 	    break;
4665 	case dest_local:
4666 	    if (lvar->lv_from_outer)
4667 		generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4668 							   NULL, type);
4669 	    else
4670 		generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4671 	    break;
4672     }
4673 }
4674 
4675     void
4676 vim9_declare_error(char_u *name)
4677 {
4678     char *scope = "";
4679 
4680     switch (*name)
4681     {
4682 	case 'g': scope = " global"; break;
4683 	case 'b': scope = " buffer"; break;
4684 	case 'w': scope = " window"; break;
4685 	case 't': scope = " tab"; break;
4686 	case 'v': scope = " v:"; break;
4687 	case '$': scope = "n environment"; break;
4688     }
4689     semsg(_(e_declare_var), scope, name);
4690 }
4691 
4692 /*
4693  * Compile declaration and assignment:
4694  * "let var", "let var = expr", "const var = expr" and "var = expr"
4695  * "arg" points to "var".
4696  * Return NULL for an error.
4697  * Return "arg" if it does not look like a variable list.
4698  */
4699     static char_u *
4700 compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4701 {
4702     char_u	*var_start;
4703     char_u	*p;
4704     char_u	*end = arg;
4705     char_u	*ret = NULL;
4706     int		var_count = 0;
4707     int		var_idx;
4708     int		semicolon = 0;
4709     garray_T	*instr = &cctx->ctx_instr;
4710     garray_T    *stack = &cctx->ctx_type_stack;
4711     char_u	*op;
4712     int		oplen = 0;
4713     int		heredoc = FALSE;
4714     type_T	*type = &t_any;
4715     type_T	*member_type = &t_any;
4716     char_u	*name = NULL;
4717     char_u	*sp;
4718     int		is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
4719 
4720     // Skip over the "var" or "[var, var]" to get to any "=".
4721     p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4722     if (p == NULL)
4723 	return *arg == '[' ? arg : NULL;
4724 
4725     if (var_count > 0 && is_decl)
4726     {
4727 	emsg(_("E1092: Cannot use a list for a declaration"));
4728 	return NULL;
4729     }
4730 
4731     sp = p;
4732     p = skipwhite(p);
4733     op = p;
4734     oplen = assignment_len(p, &heredoc);
4735 
4736     if (var_count > 0 && oplen == 0)
4737 	// can be something like "[1, 2]->func()"
4738 	return arg;
4739 
4740     if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4741     {
4742 	char_u  buf[4];
4743 
4744 	vim_strncpy(buf, op, oplen);
4745 	semsg(_(e_white_both), buf);
4746 	return NULL;
4747     }
4748 
4749     if (heredoc)
4750     {
4751 	list_T	   *l;
4752 	listitem_T *li;
4753 
4754 	// [let] varname =<< [trim] {end}
4755 	eap->getline = exarg_getline;
4756 	eap->cookie = cctx;
4757 	l = heredoc_get(eap, op + 3, FALSE);
4758 
4759 	// Push each line and the create the list.
4760 	FOR_ALL_LIST_ITEMS(l, li)
4761 	{
4762 	    generate_PUSHS(cctx, li->li_tv.vval.v_string);
4763 	    li->li_tv.vval.v_string = NULL;
4764 	}
4765 	generate_NEWLIST(cctx, l->lv_len);
4766 	type = &t_list_string;
4767 	member_type = &t_list_string;
4768 	list_free(l);
4769 	p += STRLEN(p);
4770 	end = p;
4771     }
4772     else if (var_count > 0)
4773     {
4774 	// for "[var, var] = expr" evaluate the expression here, loop over the
4775 	// list of variables below.
4776 
4777 	p = skipwhite(op + oplen);
4778 	if (compile_expr0(&p, cctx) == FAIL)
4779 	    return NULL;
4780 	end = p;
4781 
4782 	if (cctx->ctx_skip != SKIP_YES)
4783 	{
4784 	    type_T	*stacktype;
4785 
4786 	    stacktype = stack->ga_len == 0 ? &t_void
4787 			      : ((type_T **)stack->ga_data)[stack->ga_len - 1];
4788 	    if (stacktype->tt_type == VAR_VOID)
4789 	    {
4790 		emsg(_(e_cannot_use_void));
4791 		goto theend;
4792 	    }
4793 	    if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL)
4794 		goto theend;
4795 	    generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4796 								    semicolon);
4797 	}
4798     }
4799 
4800     /*
4801      * Loop over variables in "[var, var] = expr".
4802      * For "var = expr" and "let var: type" this is done only once.
4803      */
4804     if (var_count > 0)
4805 	var_start = skipwhite(arg + 1);  // skip over the "["
4806     else
4807 	var_start = arg;
4808     for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4809     {
4810 	char_u		*var_end = skip_var_one(var_start, FALSE);
4811 	size_t		varlen;
4812 	int		new_local = FALSE;
4813 	int		opt_type;
4814 	int		opt_flags = 0;
4815 	assign_dest_T	dest = dest_local;
4816 	int		vimvaridx = -1;
4817 	lvar_T		*lvar = NULL;
4818 	lvar_T		arg_lvar;
4819 	int		has_type = FALSE;
4820 	int		has_index = FALSE;
4821 	int		instr_count = -1;
4822 
4823 	p = (*var_start == '&' || *var_start == '$'
4824 			     || *var_start == '@') ? var_start + 1 : var_start;
4825 	p = to_name_end(p, TRUE);
4826 
4827 	// "a: type" is declaring variable "a" with a type, not "a:".
4828 	if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4829 	    --var_end;
4830 	if (is_decl && p == var_start + 2 && p[-1] == ':')
4831 	    --p;
4832 
4833 	varlen = p - var_start;
4834 	vim_free(name);
4835 	name = vim_strnsave(var_start, varlen);
4836 	if (name == NULL)
4837 	    return NULL;
4838 	if (!heredoc)
4839 	    type = &t_any;
4840 
4841 	if (cctx->ctx_skip != SKIP_YES)
4842 	{
4843 	    if (*var_start == '&')
4844 	    {
4845 		int	    cc;
4846 		long	    numval;
4847 
4848 		dest = dest_option;
4849 		if (cmdidx == CMD_const)
4850 		{
4851 		    emsg(_(e_const_option));
4852 		    goto theend;
4853 		}
4854 		if (is_decl)
4855 		{
4856 		    semsg(_("E1052: Cannot declare an option: %s"), var_start);
4857 		    goto theend;
4858 		}
4859 		p = var_start;
4860 		p = find_option_end(&p, &opt_flags);
4861 		if (p == NULL)
4862 		{
4863 		    // cannot happen?
4864 		    emsg(_(e_letunexp));
4865 		    goto theend;
4866 		}
4867 		cc = *p;
4868 		*p = NUL;
4869 		opt_type = get_option_value(var_start + 1, &numval,
4870 							      NULL, opt_flags);
4871 		*p = cc;
4872 		if (opt_type == -3)
4873 		{
4874 		    semsg(_(e_unknown_option), var_start);
4875 		    goto theend;
4876 		}
4877 		if (opt_type == -2 || opt_type == 0)
4878 		    type = &t_string;
4879 		else
4880 		    type = &t_number;	// both number and boolean option
4881 	    }
4882 	    else if (*var_start == '$')
4883 	    {
4884 		dest = dest_env;
4885 		type = &t_string;
4886 		if (is_decl)
4887 		{
4888 		    vim9_declare_error(name);
4889 		    goto theend;
4890 		}
4891 	    }
4892 	    else if (*var_start == '@')
4893 	    {
4894 		if (!valid_yank_reg(var_start[1], TRUE))
4895 		{
4896 		    emsg_invreg(var_start[1]);
4897 		    goto theend;
4898 		}
4899 		dest = dest_reg;
4900 		type = &t_string;
4901 		if (is_decl)
4902 		{
4903 		    semsg(_("E1066: Cannot declare a register: %s"), name);
4904 		    goto theend;
4905 		}
4906 	    }
4907 	    else if (STRNCMP(var_start, "g:", 2) == 0)
4908 	    {
4909 		dest = dest_global;
4910 		if (is_decl)
4911 		{
4912 		    vim9_declare_error(name);
4913 		    goto theend;
4914 		}
4915 	    }
4916 	    else if (STRNCMP(var_start, "b:", 2) == 0)
4917 	    {
4918 		dest = dest_buffer;
4919 		if (is_decl)
4920 		{
4921 		    vim9_declare_error(name);
4922 		    goto theend;
4923 		}
4924 	    }
4925 	    else if (STRNCMP(var_start, "w:", 2) == 0)
4926 	    {
4927 		dest = dest_window;
4928 		if (is_decl)
4929 		{
4930 		    vim9_declare_error(name);
4931 		    goto theend;
4932 		}
4933 	    }
4934 	    else if (STRNCMP(var_start, "t:", 2) == 0)
4935 	    {
4936 		dest = dest_tab;
4937 		if (is_decl)
4938 		{
4939 		    vim9_declare_error(name);
4940 		    goto theend;
4941 		}
4942 	    }
4943 	    else if (STRNCMP(var_start, "v:", 2) == 0)
4944 	    {
4945 		typval_T	*vtv;
4946 		int		di_flags;
4947 
4948 		vimvaridx = find_vim_var(name + 2, &di_flags);
4949 		if (vimvaridx < 0)
4950 		{
4951 		    semsg(_(e_var_notfound), var_start);
4952 		    goto theend;
4953 		}
4954 		// We use the current value of "sandbox" here, is that OK?
4955 		if (var_check_ro(di_flags, name, FALSE))
4956 		    goto theend;
4957 		dest = dest_vimvar;
4958 		vtv = get_vim_var_tv(vimvaridx);
4959 		type = typval2type(vtv);
4960 		if (is_decl)
4961 		{
4962 		    vim9_declare_error(name);
4963 		    goto theend;
4964 		}
4965 	    }
4966 	    else
4967 	    {
4968 		int idx;
4969 
4970 		for (idx = 0; reserved[idx] != NULL; ++idx)
4971 		    if (STRCMP(reserved[idx], name) == 0)
4972 		    {
4973 			semsg(_("E1034: Cannot use reserved name %s"), name);
4974 			goto theend;
4975 		    }
4976 
4977 		lvar = lookup_local(var_start, varlen, cctx);
4978 		if (lvar == NULL)
4979 		{
4980 		    CLEAR_FIELD(arg_lvar);
4981 		    if (lookup_arg(var_start, varlen,
4982 				&arg_lvar.lv_idx, &arg_lvar.lv_type,
4983 				&arg_lvar.lv_from_outer, cctx) == OK)
4984 		    {
4985 			if (is_decl)
4986 			{
4987 			    semsg(_(e_used_as_arg), name);
4988 			    goto theend;
4989 			}
4990 			lvar = &arg_lvar;
4991 		    }
4992 		}
4993 		if (lvar != NULL)
4994 		{
4995 		    if (is_decl)
4996 		    {
4997 			semsg(_("E1017: Variable already declared: %s"), name);
4998 			goto theend;
4999 		    }
5000 		    else if (lvar->lv_const)
5001 		    {
5002 			semsg(_("E1018: Cannot assign to a constant: %s"),
5003 									 name);
5004 			goto theend;
5005 		    }
5006 		}
5007 		else if (STRNCMP(var_start, "s:", 2) == 0
5008 			|| lookup_script(var_start, varlen) == OK
5009 			|| find_imported(var_start, varlen, cctx) != NULL)
5010 		{
5011 		    dest = dest_script;
5012 		    if (is_decl)
5013 		    {
5014 			semsg(_("E1054: Variable already declared in the script: %s"),
5015 									 name);
5016 			goto theend;
5017 		    }
5018 		}
5019 		else if (name[1] == ':' && name[2] != NUL)
5020 		{
5021 		    semsg(_("E1082: Cannot use a namespaced variable: %s"),
5022 									 name);
5023 		    goto theend;
5024 		}
5025 		else if (!is_decl)
5026 		{
5027 		    semsg(_("E1089: unknown variable: %s"), name);
5028 		    goto theend;
5029 		}
5030 	    }
5031 	}
5032 
5033 	// handle "a:name" as a name, not index "name" on "a"
5034 	if (varlen > 1 || var_start[varlen] != ':')
5035 	    p = var_end;
5036 
5037 	if (dest != dest_option)
5038 	{
5039 	    if (is_decl && *p == ':')
5040 	    {
5041 		// parse optional type: "let var: type = expr"
5042 		if (!VIM_ISWHITE(p[1]))
5043 		{
5044 		    semsg(_(e_white_after), ":");
5045 		    goto theend;
5046 		}
5047 		p = skipwhite(p + 1);
5048 		type = parse_type(&p, cctx->ctx_type_list);
5049 		has_type = TRUE;
5050 	    }
5051 	    else if (lvar != NULL)
5052 		type = lvar->lv_type;
5053 	}
5054 
5055 	if (oplen == 3 && !heredoc && dest != dest_global
5056 			&& type->tt_type != VAR_STRING
5057 			&& type->tt_type != VAR_ANY)
5058 	{
5059 	    emsg(_("E1019: Can only concatenate to string"));
5060 	    goto theend;
5061 	}
5062 
5063 	if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
5064 	{
5065 	    if (oplen > 1 && !heredoc)
5066 	    {
5067 		// +=, /=, etc. require an existing variable
5068 		semsg(_("E1020: cannot use an operator on a new variable: %s"),
5069 									 name);
5070 		goto theend;
5071 	    }
5072 
5073 	    // new local variable
5074 	    if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5075 		goto theend;
5076 	    lvar = reserve_local(cctx, var_start, varlen,
5077 						    cmdidx == CMD_const, type);
5078 	    if (lvar == NULL)
5079 		goto theend;
5080 	    new_local = TRUE;
5081 	}
5082 
5083 	member_type = type;
5084 	if (var_end > var_start + varlen)
5085 	{
5086 	    // Something follows after the variable: "var[idx]".
5087 	    if (is_decl)
5088 	    {
5089 		emsg(_("E1087: cannot use an index when declaring a variable"));
5090 		goto theend;
5091 	    }
5092 
5093 	    if (var_start[varlen] == '[')
5094 	    {
5095 		has_index = TRUE;
5096 		if (type->tt_member == NULL)
5097 		{
5098 		    semsg(_("E1088: cannot use an index on %s"), name);
5099 		    goto theend;
5100 		}
5101 		member_type = type->tt_member;
5102 	    }
5103 	    else
5104 	    {
5105 		semsg("Not supported yet: %s", var_start);
5106 		goto theend;
5107 	    }
5108 	}
5109 	else if (lvar == &arg_lvar)
5110 	{
5111 	    semsg(_("E1090: Cannot assign to argument %s"), name);
5112 	    goto theend;
5113 	}
5114 
5115 	if (!heredoc)
5116 	{
5117 	    if (cctx->ctx_skip == SKIP_YES)
5118 	    {
5119 		if (oplen > 0 && var_count == 0)
5120 		{
5121 		    // skip over the "=" and the expression
5122 		    p = skipwhite(op + oplen);
5123 		    compile_expr0(&p, cctx);
5124 		}
5125 	    }
5126 	    else if (oplen > 0)
5127 	    {
5128 		type_T	*stacktype;
5129 
5130 		// For "var = expr" evaluate the expression.
5131 		if (var_count == 0)
5132 		{
5133 		    int	r;
5134 
5135 		    // for "+=", "*=", "..=" etc. first load the current value
5136 		    if (*op != '=')
5137 		    {
5138 			generate_loadvar(cctx, dest, name, lvar, type);
5139 
5140 			if (has_index)
5141 			{
5142 			    // TODO: get member from list or dict
5143 			    emsg("Index with operation not supported yet");
5144 			    goto theend;
5145 			}
5146 		    }
5147 
5148 		    // Compile the expression.  Temporarily hide the new local
5149 		    // variable here, it is not available to this expression.
5150 		    if (new_local)
5151 			--cctx->ctx_locals.ga_len;
5152 		    instr_count = instr->ga_len;
5153 		    p = skipwhite(op + oplen);
5154 		    r = compile_expr0(&p, cctx);
5155 		    if (new_local)
5156 			++cctx->ctx_locals.ga_len;
5157 		    if (r == FAIL)
5158 			goto theend;
5159 		}
5160 		else if (semicolon && var_idx == var_count - 1)
5161 		{
5162 		    // For "[var; var] = expr" get the rest of the list
5163 		    if (generate_SLICE(cctx, var_count - 1) == FAIL)
5164 			goto theend;
5165 		}
5166 		else
5167 		{
5168 		    // For "[var, var] = expr" get the "var_idx" item from the
5169 		    // list.
5170 		    if (generate_GETITEM(cctx, var_idx) == FAIL)
5171 			return FAIL;
5172 		}
5173 
5174 		stacktype = stack->ga_len == 0 ? &t_void
5175 			  : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5176 		if (lvar != NULL && (is_decl || !has_type))
5177 		{
5178 		    if (new_local && !has_type)
5179 		    {
5180 			if (stacktype->tt_type == VAR_VOID)
5181 			{
5182 			    emsg(_(e_cannot_use_void));
5183 			    goto theend;
5184 			}
5185 			else
5186 			{
5187 			    // An empty list or dict has a &t_void member,
5188 			    // for a variable that implies &t_any.
5189 			    if (stacktype == &t_list_empty)
5190 				lvar->lv_type = &t_list_any;
5191 			    else if (stacktype == &t_dict_empty)
5192 				lvar->lv_type = &t_dict_any;
5193 			    else
5194 				lvar->lv_type = stacktype;
5195 			}
5196 		    }
5197 		    else
5198 		    {
5199 			type_T *use_type = lvar->lv_type;
5200 
5201 			if (has_index)
5202 			{
5203 			    use_type = use_type->tt_member;
5204 			    if (use_type == NULL)
5205 				use_type = &t_void;
5206 			}
5207 			if (need_type(stacktype, use_type, -1, cctx)
5208 								   == FAIL)
5209 			    goto theend;
5210 		    }
5211 		}
5212 		else if (*p != '=' && need_type(stacktype, member_type, -1,
5213 							     cctx) == FAIL)
5214 		    goto theend;
5215 	    }
5216 	    else if (cmdidx == CMD_const)
5217 	    {
5218 		emsg(_(e_const_req_value));
5219 		goto theend;
5220 	    }
5221 	    else if (!has_type || dest == dest_option)
5222 	    {
5223 		emsg(_(e_type_req));
5224 		goto theend;
5225 	    }
5226 	    else
5227 	    {
5228 		// variables are always initialized
5229 		if (ga_grow(instr, 1) == FAIL)
5230 		    goto theend;
5231 		switch (member_type->tt_type)
5232 		{
5233 		    case VAR_BOOL:
5234 			generate_PUSHBOOL(cctx, VVAL_FALSE);
5235 			break;
5236 		    case VAR_FLOAT:
5237 #ifdef FEAT_FLOAT
5238 			generate_PUSHF(cctx, 0.0);
5239 #endif
5240 			break;
5241 		    case VAR_STRING:
5242 			generate_PUSHS(cctx, NULL);
5243 			break;
5244 		    case VAR_BLOB:
5245 			generate_PUSHBLOB(cctx, NULL);
5246 			break;
5247 		    case VAR_FUNC:
5248 			generate_PUSHFUNC(cctx, NULL, &t_func_void);
5249 			break;
5250 		    case VAR_LIST:
5251 			generate_NEWLIST(cctx, 0);
5252 			break;
5253 		    case VAR_DICT:
5254 			generate_NEWDICT(cctx, 0);
5255 			break;
5256 		    case VAR_JOB:
5257 			generate_PUSHJOB(cctx, NULL);
5258 			break;
5259 		    case VAR_CHANNEL:
5260 			generate_PUSHCHANNEL(cctx, NULL);
5261 			break;
5262 		    case VAR_NUMBER:
5263 		    case VAR_UNKNOWN:
5264 		    case VAR_ANY:
5265 		    case VAR_PARTIAL:
5266 		    case VAR_VOID:
5267 		    case VAR_SPECIAL:  // cannot happen
5268 			generate_PUSHNR(cctx, 0);
5269 			break;
5270 		}
5271 	    }
5272 	    if (var_count == 0)
5273 		end = p;
5274 	}
5275 
5276 	// no need to parse more when skipping
5277 	if (cctx->ctx_skip == SKIP_YES)
5278 	    break;
5279 
5280 	if (oplen > 0 && *op != '=')
5281 	{
5282 	    type_T	    *expected = &t_number;
5283 	    type_T	    *stacktype;
5284 
5285 	    // TODO: if type is known use float or any operation
5286 
5287 	    if (*op == '.')
5288 		expected = &t_string;
5289 	    stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5290 	    if (need_type(stacktype, expected, -1, cctx) == FAIL)
5291 		goto theend;
5292 
5293 	    if (*op == '.')
5294 		generate_instr_drop(cctx, ISN_CONCAT, 1);
5295 	    else
5296 	    {
5297 		isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5298 
5299 		if (isn == NULL)
5300 		    goto theend;
5301 		switch (*op)
5302 		{
5303 		    case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5304 		    case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5305 		    case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5306 		    case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5307 		    case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5308 		}
5309 	    }
5310 	}
5311 
5312 	if (has_index)
5313 	{
5314 	    int r;
5315 
5316 	    // Compile the "idx" in "var[idx]".
5317 	    if (new_local)
5318 		--cctx->ctx_locals.ga_len;
5319 	    p = skipwhite(var_start + varlen + 1);
5320 	    r = compile_expr0(&p, cctx);
5321 	    if (new_local)
5322 		++cctx->ctx_locals.ga_len;
5323 	    if (r == FAIL)
5324 		goto theend;
5325 	    if (*skipwhite(p) != ']')
5326 	    {
5327 		emsg(_(e_missbrac));
5328 		goto theend;
5329 	    }
5330 	    if (type->tt_type == VAR_DICT
5331 		    && may_generate_2STRING(-1, cctx) == FAIL)
5332 		goto theend;
5333 	    if (type->tt_type == VAR_LIST
5334 		    && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
5335 								 != VAR_NUMBER)
5336 	    {
5337 		emsg(_(e_number_exp));
5338 		goto theend;
5339 	    }
5340 
5341 	    // Load the dict or list.  On the stack we then have:
5342 	    // - value
5343 	    // - index
5344 	    // - variable
5345 	    generate_loadvar(cctx, dest, name, lvar, type);
5346 
5347 	    if (type->tt_type == VAR_LIST)
5348 	    {
5349 		if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5350 		    return FAIL;
5351 	    }
5352 	    else if (type->tt_type == VAR_DICT)
5353 	    {
5354 		if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5355 		    return FAIL;
5356 	    }
5357 	    else
5358 	    {
5359 		emsg(_(e_listreq));
5360 		goto theend;
5361 	    }
5362 	}
5363 	else
5364 	{
5365 	    switch (dest)
5366 	    {
5367 		case dest_option:
5368 		    generate_STOREOPT(cctx, name + 1, opt_flags);
5369 		    break;
5370 		case dest_global:
5371 		    // include g: with the name, easier to execute that way
5372 		    generate_STORE(cctx, ISN_STOREG, 0, name);
5373 		    break;
5374 		case dest_buffer:
5375 		    // include b: with the name, easier to execute that way
5376 		    generate_STORE(cctx, ISN_STOREB, 0, name);
5377 		    break;
5378 		case dest_window:
5379 		    // include w: with the name, easier to execute that way
5380 		    generate_STORE(cctx, ISN_STOREW, 0, name);
5381 		    break;
5382 		case dest_tab:
5383 		    // include t: with the name, easier to execute that way
5384 		    generate_STORE(cctx, ISN_STORET, 0, name);
5385 		    break;
5386 		case dest_env:
5387 		    generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5388 		    break;
5389 		case dest_reg:
5390 		    generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5391 		    break;
5392 		case dest_vimvar:
5393 		    generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5394 		    break;
5395 		case dest_script:
5396 		    {
5397 			char_u	    *rawname = name + (name[1] == ':' ? 2 : 0);
5398 			imported_T  *import = NULL;
5399 			int	    sid = current_sctx.sc_sid;
5400 			int	    idx;
5401 
5402 			if (name[1] != ':')
5403 			{
5404 			    import = find_imported(name, 0, cctx);
5405 			    if (import != NULL)
5406 				sid = import->imp_sid;
5407 			}
5408 
5409 			idx = get_script_item_idx(sid, rawname, TRUE);
5410 			// TODO: specific type
5411 			if (idx < 0)
5412 			{
5413 			    char_u *name_s = name;
5414 
5415 			    // Include s: in the name for store_var()
5416 			    if (name[1] != ':')
5417 			    {
5418 				int len = (int)STRLEN(name) + 3;
5419 
5420 				name_s = alloc(len);
5421 				if (name_s == NULL)
5422 				    name_s = name;
5423 				else
5424 				    vim_snprintf((char *)name_s, len,
5425 								 "s:%s", name);
5426 			    }
5427 			    generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
5428 								       &t_any);
5429 			    if (name_s != name)
5430 				vim_free(name_s);
5431 			}
5432 			else
5433 			    generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5434 							     sid, idx, &t_any);
5435 		    }
5436 		    break;
5437 		case dest_local:
5438 		    if (lvar != NULL)
5439 		    {
5440 			isn_T *isn = ((isn_T *)instr->ga_data)
5441 							   + instr->ga_len - 1;
5442 
5443 			// optimization: turn "var = 123" from ISN_PUSHNR +
5444 			// ISN_STORE into ISN_STORENR
5445 			if (!lvar->lv_from_outer
5446 					&& instr->ga_len == instr_count + 1
5447 					&& isn->isn_type == ISN_PUSHNR)
5448 			{
5449 			    varnumber_T val = isn->isn_arg.number;
5450 
5451 			    isn->isn_type = ISN_STORENR;
5452 			    isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5453 			    isn->isn_arg.storenr.stnr_val = val;
5454 			    if (stack->ga_len > 0)
5455 				--stack->ga_len;
5456 			}
5457 			else if (lvar->lv_from_outer)
5458 			    generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
5459 									 NULL);
5460 			else
5461 			    generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5462 		    }
5463 		    break;
5464 	    }
5465 	}
5466 
5467 	if (var_idx + 1 < var_count)
5468 	    var_start = skipwhite(var_end + 1);
5469     }
5470 
5471     // for "[var, var] = expr" drop the "expr" value
5472     if (var_count > 0 && !semicolon)
5473     {
5474 	    if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5475 	    goto theend;
5476     }
5477 
5478     ret = end;
5479 
5480 theend:
5481     vim_free(name);
5482     return ret;
5483 }
5484 
5485 /*
5486  * Check if "name" can be "unlet".
5487  */
5488     int
5489 check_vim9_unlet(char_u *name)
5490 {
5491     if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5492     {
5493 	semsg(_("E1081: Cannot unlet %s"), name);
5494 	return FAIL;
5495     }
5496     return OK;
5497 }
5498 
5499 /*
5500  * Callback passed to ex_unletlock().
5501  */
5502     static int
5503 compile_unlet(
5504     lval_T  *lvp,
5505     char_u  *name_end,
5506     exarg_T *eap,
5507     int	    deep UNUSED,
5508     void    *coookie)
5509 {
5510     cctx_T *cctx = coookie;
5511 
5512     if (lvp->ll_tv == NULL)
5513     {
5514 	char_u	*p = lvp->ll_name;
5515 	int	cc = *name_end;
5516 	int	ret = OK;
5517 
5518 	// Normal name.  Only supports g:, w:, t: and b: namespaces.
5519 	*name_end = NUL;
5520 	if (*p == '$')
5521 	    ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5522 	else if (check_vim9_unlet(p) == FAIL)
5523 	    ret = FAIL;
5524 	else
5525 	    ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
5526 
5527 	*name_end = cc;
5528 	return ret;
5529     }
5530 
5531     // TODO: unlet {list}[idx]
5532     // TODO: unlet {dict}[key]
5533     emsg("Sorry, :unlet not fully implemented yet");
5534     return FAIL;
5535 }
5536 
5537 /*
5538  * compile "unlet var", "lock var" and "unlock var"
5539  * "arg" points to "var".
5540  */
5541     static char_u *
5542 compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5543 {
5544     char_u *p = arg;
5545 
5546     if (eap->cmdidx != CMD_unlet)
5547     {
5548 	emsg("Sorry, :lock and unlock not implemented yet");
5549 	return NULL;
5550     }
5551 
5552     if (*p == '!')
5553     {
5554 	p = skipwhite(p + 1);
5555 	eap->forceit = TRUE;
5556     }
5557 
5558     ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5559     return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5560 }
5561 
5562 /*
5563  * Compile an :import command.
5564  */
5565     static char_u *
5566 compile_import(char_u *arg, cctx_T *cctx)
5567 {
5568     return handle_import(arg, &cctx->ctx_imports, 0, cctx);
5569 }
5570 
5571 /*
5572  * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5573  */
5574     static int
5575 compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5576 {
5577     garray_T	*instr = &cctx->ctx_instr;
5578     endlabel_T  *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5579 
5580     if (endlabel == NULL)
5581 	return FAIL;
5582     endlabel->el_next = *el;
5583     *el = endlabel;
5584     endlabel->el_end_label = instr->ga_len;
5585 
5586     generate_JUMP(cctx, when, 0);
5587     return OK;
5588 }
5589 
5590     static void
5591 compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5592 {
5593     garray_T	*instr = &cctx->ctx_instr;
5594 
5595     while (*el != NULL)
5596     {
5597 	endlabel_T  *cur = (*el);
5598 	isn_T	    *isn;
5599 
5600 	isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5601 	isn->isn_arg.jump.jump_where = instr->ga_len;
5602 	*el = cur->el_next;
5603 	vim_free(cur);
5604     }
5605 }
5606 
5607     static void
5608 compile_free_jump_to_end(endlabel_T **el)
5609 {
5610     while (*el != NULL)
5611     {
5612 	endlabel_T  *cur = (*el);
5613 
5614 	*el = cur->el_next;
5615 	vim_free(cur);
5616     }
5617 }
5618 
5619 /*
5620  * Create a new scope and set up the generic items.
5621  */
5622     static scope_T *
5623 new_scope(cctx_T *cctx, scopetype_T type)
5624 {
5625     scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5626 
5627     if (scope == NULL)
5628 	return NULL;
5629     scope->se_outer = cctx->ctx_scope;
5630     cctx->ctx_scope = scope;
5631     scope->se_type = type;
5632     scope->se_local_count = cctx->ctx_locals.ga_len;
5633     return scope;
5634 }
5635 
5636 /*
5637  * Free the current scope and go back to the outer scope.
5638  */
5639     static void
5640 drop_scope(cctx_T *cctx)
5641 {
5642     scope_T *scope = cctx->ctx_scope;
5643 
5644     if (scope == NULL)
5645     {
5646 	iemsg("calling drop_scope() without a scope");
5647 	return;
5648     }
5649     cctx->ctx_scope = scope->se_outer;
5650     switch (scope->se_type)
5651     {
5652 	case IF_SCOPE:
5653 	    compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5654 	case FOR_SCOPE:
5655 	    compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5656 	case WHILE_SCOPE:
5657 	    compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5658 	case TRY_SCOPE:
5659 	    compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5660 	case NO_SCOPE:
5661 	case BLOCK_SCOPE:
5662 	    break;
5663     }
5664     vim_free(scope);
5665 }
5666 
5667 /*
5668  * compile "if expr"
5669  *
5670  * "if expr" Produces instructions:
5671  *	EVAL expr		Push result of "expr"
5672  *	JUMP_IF_FALSE end
5673  *	... body ...
5674  * end:
5675  *
5676  * "if expr | else" Produces instructions:
5677  *	EVAL expr		Push result of "expr"
5678  *	JUMP_IF_FALSE else
5679  *	... body ...
5680  *	JUMP_ALWAYS end
5681  * else:
5682  *	... body ...
5683  * end:
5684  *
5685  * "if expr1 | elseif expr2 | else" Produces instructions:
5686  *	EVAL expr		Push result of "expr"
5687  *	JUMP_IF_FALSE elseif
5688  *	... body ...
5689  *	JUMP_ALWAYS end
5690  * elseif:
5691  *	EVAL expr		Push result of "expr"
5692  *	JUMP_IF_FALSE else
5693  *	... body ...
5694  *	JUMP_ALWAYS end
5695  * else:
5696  *	... body ...
5697  * end:
5698  */
5699     static char_u *
5700 compile_if(char_u *arg, cctx_T *cctx)
5701 {
5702     char_u	*p = arg;
5703     garray_T	*instr = &cctx->ctx_instr;
5704     int		instr_count = instr->ga_len;
5705     scope_T	*scope;
5706     skip_T	skip_save = cctx->ctx_skip;
5707     ppconst_T	ppconst;
5708 
5709     CLEAR_FIELD(ppconst);
5710     if (compile_expr1(&p, cctx, &ppconst) == FAIL)
5711     {
5712 	clear_ppconst(&ppconst);
5713 	return NULL;
5714     }
5715     if (cctx->ctx_skip == SKIP_YES)
5716 	clear_ppconst(&ppconst);
5717     else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
5718     {
5719 	// The expression results in a constant.
5720 	cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
5721 	clear_ppconst(&ppconst);
5722     }
5723     else
5724     {
5725 	// Not a constant, generate instructions for the expression.
5726 	cctx->ctx_skip = SKIP_UNKNOWN;
5727 	if (generate_ppconst(cctx, &ppconst) == FAIL)
5728 	    return NULL;
5729     }
5730 
5731     scope = new_scope(cctx, IF_SCOPE);
5732     if (scope == NULL)
5733 	return NULL;
5734     scope->se_skip_save = skip_save;
5735     // "is_had_return" will be reset if any block does not end in :return
5736     scope->se_u.se_if.is_had_return = TRUE;
5737 
5738     if (cctx->ctx_skip == SKIP_UNKNOWN)
5739     {
5740 	// "where" is set when ":elseif", "else" or ":endif" is found
5741 	scope->se_u.se_if.is_if_label = instr->ga_len;
5742 	generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5743     }
5744     else
5745 	scope->se_u.se_if.is_if_label = -1;
5746 
5747     return p;
5748 }
5749 
5750     static char_u *
5751 compile_elseif(char_u *arg, cctx_T *cctx)
5752 {
5753     char_u	*p = arg;
5754     garray_T	*instr = &cctx->ctx_instr;
5755     int		instr_count = instr->ga_len;
5756     isn_T	*isn;
5757     scope_T	*scope = cctx->ctx_scope;
5758     ppconst_T	ppconst;
5759 
5760     if (scope == NULL || scope->se_type != IF_SCOPE)
5761     {
5762 	emsg(_(e_elseif_without_if));
5763 	return NULL;
5764     }
5765     unwind_locals(cctx, scope->se_local_count);
5766     if (!cctx->ctx_had_return)
5767 	scope->se_u.se_if.is_had_return = FALSE;
5768 
5769     if (cctx->ctx_skip == SKIP_UNKNOWN)
5770     {
5771 	if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5772 						    JUMP_ALWAYS, cctx) == FAIL)
5773 	    return NULL;
5774 	// previous "if" or "elseif" jumps here
5775 	isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5776 	isn->isn_arg.jump.jump_where = instr->ga_len;
5777     }
5778 
5779     // compile "expr"; if we know it evaluates to FALSE skip the block
5780     CLEAR_FIELD(ppconst);
5781     if (compile_expr1(&p, cctx, &ppconst) == FAIL)
5782     {
5783 	clear_ppconst(&ppconst);
5784 	return NULL;
5785     }
5786     if (scope->se_skip_save == SKIP_YES)
5787 	clear_ppconst(&ppconst);
5788     else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
5789     {
5790 	// The expression results in a constant.
5791 	// TODO: how about nesting?
5792 	cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
5793 	clear_ppconst(&ppconst);
5794 	scope->se_u.se_if.is_if_label = -1;
5795     }
5796     else
5797     {
5798 	// Not a constant, generate instructions for the expression.
5799 	cctx->ctx_skip = SKIP_UNKNOWN;
5800 	if (generate_ppconst(cctx, &ppconst) == FAIL)
5801 	    return NULL;
5802 
5803 	// "where" is set when ":elseif", "else" or ":endif" is found
5804 	scope->se_u.se_if.is_if_label = instr->ga_len;
5805 	generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5806     }
5807 
5808     return p;
5809 }
5810 
5811     static char_u *
5812 compile_else(char_u *arg, cctx_T *cctx)
5813 {
5814     char_u	*p = arg;
5815     garray_T	*instr = &cctx->ctx_instr;
5816     isn_T	*isn;
5817     scope_T	*scope = cctx->ctx_scope;
5818 
5819     if (scope == NULL || scope->se_type != IF_SCOPE)
5820     {
5821 	emsg(_(e_else_without_if));
5822 	return NULL;
5823     }
5824     unwind_locals(cctx, scope->se_local_count);
5825     if (!cctx->ctx_had_return)
5826 	scope->se_u.se_if.is_had_return = FALSE;
5827     scope->se_u.se_if.is_seen_else = TRUE;
5828 
5829     if (scope->se_skip_save != SKIP_YES)
5830     {
5831 	// jump from previous block to the end, unless the else block is empty
5832 	if (cctx->ctx_skip == SKIP_UNKNOWN)
5833 	{
5834 	    if (!cctx->ctx_had_return
5835 		    && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5836 						    JUMP_ALWAYS, cctx) == FAIL)
5837 		return NULL;
5838 	}
5839 
5840 	if (cctx->ctx_skip == SKIP_UNKNOWN)
5841 	{
5842 	    if (scope->se_u.se_if.is_if_label >= 0)
5843 	    {
5844 		// previous "if" or "elseif" jumps here
5845 		isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5846 		isn->isn_arg.jump.jump_where = instr->ga_len;
5847 		scope->se_u.se_if.is_if_label = -1;
5848 	    }
5849 	}
5850 
5851 	if (cctx->ctx_skip != SKIP_UNKNOWN)
5852 	    cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5853     }
5854 
5855     return p;
5856 }
5857 
5858     static char_u *
5859 compile_endif(char_u *arg, cctx_T *cctx)
5860 {
5861     scope_T	*scope = cctx->ctx_scope;
5862     ifscope_T	*ifscope;
5863     garray_T	*instr = &cctx->ctx_instr;
5864     isn_T	*isn;
5865 
5866     if (scope == NULL || scope->se_type != IF_SCOPE)
5867     {
5868 	emsg(_(e_endif_without_if));
5869 	return NULL;
5870     }
5871     ifscope = &scope->se_u.se_if;
5872     unwind_locals(cctx, scope->se_local_count);
5873     if (!cctx->ctx_had_return)
5874 	ifscope->is_had_return = FALSE;
5875 
5876     if (scope->se_u.se_if.is_if_label >= 0)
5877     {
5878 	// previous "if" or "elseif" jumps here
5879 	isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5880 	isn->isn_arg.jump.jump_where = instr->ga_len;
5881     }
5882     // Fill in the "end" label in jumps at the end of the blocks.
5883     compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
5884     cctx->ctx_skip = scope->se_skip_save;
5885 
5886     // If all the blocks end in :return and there is an :else then the
5887     // had_return flag is set.
5888     cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
5889 
5890     drop_scope(cctx);
5891     return arg;
5892 }
5893 
5894 /*
5895  * compile "for var in expr"
5896  *
5897  * Produces instructions:
5898  *       PUSHNR -1
5899  *       STORE loop-idx		Set index to -1
5900  *       EVAL expr		Push result of "expr"
5901  * top:  FOR loop-idx, end	Increment index, use list on bottom of stack
5902  *				- if beyond end, jump to "end"
5903  *				- otherwise get item from list and push it
5904  *       STORE var		Store item in "var"
5905  *       ... body ...
5906  *       JUMP top		Jump back to repeat
5907  * end:	 DROP			Drop the result of "expr"
5908  *
5909  */
5910     static char_u *
5911 compile_for(char_u *arg, cctx_T *cctx)
5912 {
5913     char_u	*p;
5914     size_t	varlen;
5915     garray_T	*instr = &cctx->ctx_instr;
5916     garray_T	*stack = &cctx->ctx_type_stack;
5917     scope_T	*scope;
5918     lvar_T	*loop_lvar;	// loop iteration variable
5919     lvar_T	*var_lvar;	// variable for "var"
5920     type_T	*vartype;
5921 
5922     // TODO: list of variables: "for [key, value] in dict"
5923     // parse "var"
5924     for (p = arg; eval_isnamec1(*p); ++p)
5925 	;
5926     varlen = p - arg;
5927     var_lvar = lookup_local(arg, varlen, cctx);
5928     if (var_lvar != NULL)
5929     {
5930 	semsg(_("E1023: variable already defined: %s"), arg);
5931 	return NULL;
5932     }
5933 
5934     // consume "in"
5935     p = skipwhite(p);
5936     if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5937     {
5938 	emsg(_(e_missing_in));
5939 	return NULL;
5940     }
5941     p = skipwhite(p + 2);
5942 
5943 
5944     scope = new_scope(cctx, FOR_SCOPE);
5945     if (scope == NULL)
5946 	return NULL;
5947 
5948     // Reserve a variable to store the loop iteration counter.
5949     loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5950     if (loop_lvar == NULL)
5951     {
5952 	// out of memory
5953 	drop_scope(cctx);
5954 	return NULL;
5955     }
5956 
5957     // Reserve a variable to store "var"
5958     var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5959     if (var_lvar == NULL)
5960     {
5961 	// out of memory or used as an argument
5962 	drop_scope(cctx);
5963 	return NULL;
5964     }
5965 
5966     generate_STORENR(cctx, loop_lvar->lv_idx, -1);
5967 
5968     // compile "expr", it remains on the stack until "endfor"
5969     arg = p;
5970     if (compile_expr0(&arg, cctx) == FAIL)
5971     {
5972 	drop_scope(cctx);
5973 	return NULL;
5974     }
5975 
5976     // now we know the type of "var"
5977     vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5978     if (vartype->tt_type != VAR_LIST)
5979     {
5980 	emsg(_("E1024: need a List to iterate over"));
5981 	drop_scope(cctx);
5982 	return NULL;
5983     }
5984     if (vartype->tt_member->tt_type != VAR_ANY)
5985 	var_lvar->lv_type = vartype->tt_member;
5986 
5987     // "for_end" is set when ":endfor" is found
5988     scope->se_u.se_for.fs_top_label = instr->ga_len;
5989 
5990     generate_FOR(cctx, loop_lvar->lv_idx);
5991     generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
5992 
5993     return arg;
5994 }
5995 
5996 /*
5997  * compile "endfor"
5998  */
5999     static char_u *
6000 compile_endfor(char_u *arg, cctx_T *cctx)
6001 {
6002     garray_T	*instr = &cctx->ctx_instr;
6003     scope_T	*scope = cctx->ctx_scope;
6004     forscope_T	*forscope;
6005     isn_T	*isn;
6006 
6007     if (scope == NULL || scope->se_type != FOR_SCOPE)
6008     {
6009 	emsg(_(e_for));
6010 	return NULL;
6011     }
6012     forscope = &scope->se_u.se_for;
6013     cctx->ctx_scope = scope->se_outer;
6014     unwind_locals(cctx, scope->se_local_count);
6015 
6016     // At end of ":for" scope jump back to the FOR instruction.
6017     generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6018 
6019     // Fill in the "end" label in the FOR statement so it can jump here
6020     isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6021     isn->isn_arg.forloop.for_end = instr->ga_len;
6022 
6023     // Fill in the "end" label any BREAK statements
6024     compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6025 
6026     // Below the ":for" scope drop the "expr" list from the stack.
6027     if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6028 	return NULL;
6029 
6030     vim_free(scope);
6031 
6032     return arg;
6033 }
6034 
6035 /*
6036  * compile "while expr"
6037  *
6038  * Produces instructions:
6039  * top:  EVAL expr		Push result of "expr"
6040  *       JUMP_IF_FALSE end	jump if false
6041  *       ... body ...
6042  *       JUMP top		Jump back to repeat
6043  * end:
6044  *
6045  */
6046     static char_u *
6047 compile_while(char_u *arg, cctx_T *cctx)
6048 {
6049     char_u	*p = arg;
6050     garray_T	*instr = &cctx->ctx_instr;
6051     scope_T	*scope;
6052 
6053     scope = new_scope(cctx, WHILE_SCOPE);
6054     if (scope == NULL)
6055 	return NULL;
6056 
6057     scope->se_u.se_while.ws_top_label = instr->ga_len;
6058 
6059     // compile "expr"
6060     if (compile_expr0(&p, cctx) == FAIL)
6061 	return NULL;
6062 
6063     // "while_end" is set when ":endwhile" is found
6064     if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
6065 						  JUMP_IF_FALSE, cctx) == FAIL)
6066 	return FAIL;
6067 
6068     return p;
6069 }
6070 
6071 /*
6072  * compile "endwhile"
6073  */
6074     static char_u *
6075 compile_endwhile(char_u *arg, cctx_T *cctx)
6076 {
6077     scope_T	*scope = cctx->ctx_scope;
6078 
6079     if (scope == NULL || scope->se_type != WHILE_SCOPE)
6080     {
6081 	emsg(_(e_while));
6082 	return NULL;
6083     }
6084     cctx->ctx_scope = scope->se_outer;
6085     unwind_locals(cctx, scope->se_local_count);
6086 
6087     // At end of ":for" scope jump back to the FOR instruction.
6088     generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
6089 
6090     // Fill in the "end" label in the WHILE statement so it can jump here.
6091     // And in any jumps for ":break"
6092     compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
6093 
6094     vim_free(scope);
6095 
6096     return arg;
6097 }
6098 
6099 /*
6100  * compile "continue"
6101  */
6102     static char_u *
6103 compile_continue(char_u *arg, cctx_T *cctx)
6104 {
6105     scope_T	*scope = cctx->ctx_scope;
6106 
6107     for (;;)
6108     {
6109 	if (scope == NULL)
6110 	{
6111 	    emsg(_(e_continue));
6112 	    return NULL;
6113 	}
6114 	if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6115 	    break;
6116 	scope = scope->se_outer;
6117     }
6118 
6119     // Jump back to the FOR or WHILE instruction.
6120     generate_JUMP(cctx, JUMP_ALWAYS,
6121 	    scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6122 					  : scope->se_u.se_while.ws_top_label);
6123     return arg;
6124 }
6125 
6126 /*
6127  * compile "break"
6128  */
6129     static char_u *
6130 compile_break(char_u *arg, cctx_T *cctx)
6131 {
6132     scope_T	*scope = cctx->ctx_scope;
6133     endlabel_T	**el;
6134 
6135     for (;;)
6136     {
6137 	if (scope == NULL)
6138 	{
6139 	    emsg(_(e_break));
6140 	    return NULL;
6141 	}
6142 	if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6143 	    break;
6144 	scope = scope->se_outer;
6145     }
6146 
6147     // Jump to the end of the FOR or WHILE loop.
6148     if (scope->se_type == FOR_SCOPE)
6149 	el = &scope->se_u.se_for.fs_end_label;
6150     else
6151 	el = &scope->se_u.se_while.ws_end_label;
6152     if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6153 	return FAIL;
6154 
6155     return arg;
6156 }
6157 
6158 /*
6159  * compile "{" start of block
6160  */
6161     static char_u *
6162 compile_block(char_u *arg, cctx_T *cctx)
6163 {
6164     if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6165 	return NULL;
6166     return skipwhite(arg + 1);
6167 }
6168 
6169 /*
6170  * compile end of block: drop one scope
6171  */
6172     static void
6173 compile_endblock(cctx_T *cctx)
6174 {
6175     scope_T	*scope = cctx->ctx_scope;
6176 
6177     cctx->ctx_scope = scope->se_outer;
6178     unwind_locals(cctx, scope->se_local_count);
6179     vim_free(scope);
6180 }
6181 
6182 /*
6183  * compile "try"
6184  * Creates a new scope for the try-endtry, pointing to the first catch and
6185  * finally.
6186  * Creates another scope for the "try" block itself.
6187  * TRY instruction sets up exception handling at runtime.
6188  *
6189  *	"try"
6190  *	    TRY -> catch1, -> finally  push trystack entry
6191  *	    ... try block
6192  *	"throw {exception}"
6193  *	    EVAL {exception}
6194  *	    THROW		create exception
6195  *	    ... try block
6196  *	" catch {expr}"
6197  *	    JUMP -> finally
6198  * catch1:  PUSH exeception
6199  *	    EVAL {expr}
6200  *	    MATCH
6201  *	    JUMP nomatch -> catch2
6202  *	    CATCH   remove exception
6203  *	    ... catch block
6204  *	" catch"
6205  *	    JUMP -> finally
6206  * catch2:  CATCH   remove exception
6207  *	    ... catch block
6208  *	" finally"
6209  * finally:
6210  *	    ... finally block
6211  *	" endtry"
6212  *	    ENDTRY  pop trystack entry, may rethrow
6213  */
6214     static char_u *
6215 compile_try(char_u *arg, cctx_T *cctx)
6216 {
6217     garray_T	*instr = &cctx->ctx_instr;
6218     scope_T	*try_scope;
6219     scope_T	*scope;
6220 
6221     // scope that holds the jumps that go to catch/finally/endtry
6222     try_scope = new_scope(cctx, TRY_SCOPE);
6223     if (try_scope == NULL)
6224 	return NULL;
6225 
6226     // "catch" is set when the first ":catch" is found.
6227     // "finally" is set when ":finally" or ":endtry" is found
6228     try_scope->se_u.se_try.ts_try_label = instr->ga_len;
6229     if (generate_instr(cctx, ISN_TRY) == NULL)
6230 	return NULL;
6231 
6232     // scope for the try block itself
6233     scope = new_scope(cctx, BLOCK_SCOPE);
6234     if (scope == NULL)
6235 	return NULL;
6236 
6237     return arg;
6238 }
6239 
6240 /*
6241  * compile "catch {expr}"
6242  */
6243     static char_u *
6244 compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6245 {
6246     scope_T	*scope = cctx->ctx_scope;
6247     garray_T	*instr = &cctx->ctx_instr;
6248     char_u	*p;
6249     isn_T	*isn;
6250 
6251     // end block scope from :try or :catch
6252     if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6253 	compile_endblock(cctx);
6254     scope = cctx->ctx_scope;
6255 
6256     // Error if not in a :try scope
6257     if (scope == NULL || scope->se_type != TRY_SCOPE)
6258     {
6259 	emsg(_(e_catch));
6260 	return NULL;
6261     }
6262 
6263     if (scope->se_u.se_try.ts_caught_all)
6264     {
6265 	emsg(_("E1033: catch unreachable after catch-all"));
6266 	return NULL;
6267     }
6268 
6269     // Jump from end of previous block to :finally or :endtry
6270     if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
6271 						    JUMP_ALWAYS, cctx) == FAIL)
6272 	return NULL;
6273 
6274     // End :try or :catch scope: set value in ISN_TRY instruction
6275     isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
6276     if (isn->isn_arg.try.try_catch == 0)
6277 	isn->isn_arg.try.try_catch = instr->ga_len;
6278     if (scope->se_u.se_try.ts_catch_label != 0)
6279     {
6280 	// Previous catch without match jumps here
6281 	isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6282 	isn->isn_arg.jump.jump_where = instr->ga_len;
6283     }
6284 
6285     p = skipwhite(arg);
6286     if (ends_excmd2(arg, p))
6287     {
6288 	scope->se_u.se_try.ts_caught_all = TRUE;
6289 	scope->se_u.se_try.ts_catch_label = 0;
6290     }
6291     else
6292     {
6293 	char_u *end;
6294 	char_u *pat;
6295 	char_u *tofree = NULL;
6296 	int	dropped = 0;
6297 	int	len;
6298 
6299 	// Push v:exception, push {expr} and MATCH
6300 	generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6301 
6302 	end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
6303 	if (*end != *p)
6304 	{
6305 	    semsg(_("E1067: Separator mismatch: %s"), p);
6306 	    vim_free(tofree);
6307 	    return FAIL;
6308 	}
6309 	if (tofree == NULL)
6310 	    len = (int)(end - (p + 1));
6311 	else
6312 	    len = (int)(end - tofree);
6313 	pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
6314 	vim_free(tofree);
6315 	p += len + 2 + dropped;
6316 	if (pat == NULL)
6317 	    return FAIL;
6318 	if (generate_PUSHS(cctx, pat) == FAIL)
6319 	    return FAIL;
6320 
6321 	if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6322 	    return NULL;
6323 
6324 	scope->se_u.se_try.ts_catch_label = instr->ga_len;
6325 	if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6326 	    return NULL;
6327     }
6328 
6329     if (generate_instr(cctx, ISN_CATCH) == NULL)
6330 	return NULL;
6331 
6332     if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6333 	return NULL;
6334     return p;
6335 }
6336 
6337     static char_u *
6338 compile_finally(char_u *arg, cctx_T *cctx)
6339 {
6340     scope_T	*scope = cctx->ctx_scope;
6341     garray_T	*instr = &cctx->ctx_instr;
6342     isn_T	*isn;
6343 
6344     // end block scope from :try or :catch
6345     if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6346 	compile_endblock(cctx);
6347     scope = cctx->ctx_scope;
6348 
6349     // Error if not in a :try scope
6350     if (scope == NULL || scope->se_type != TRY_SCOPE)
6351     {
6352 	emsg(_(e_finally));
6353 	return NULL;
6354     }
6355 
6356     // End :catch or :finally scope: set value in ISN_TRY instruction
6357     isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
6358     if (isn->isn_arg.try.try_finally != 0)
6359     {
6360 	emsg(_(e_finally_dup));
6361 	return NULL;
6362     }
6363 
6364     // Fill in the "end" label in jumps at the end of the blocks.
6365     compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
6366 
6367     isn->isn_arg.try.try_finally = instr->ga_len;
6368     if (scope->se_u.se_try.ts_catch_label != 0)
6369     {
6370 	// Previous catch without match jumps here
6371 	isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6372 	isn->isn_arg.jump.jump_where = instr->ga_len;
6373     }
6374 
6375     // TODO: set index in ts_finally_label jumps
6376 
6377     return arg;
6378 }
6379 
6380     static char_u *
6381 compile_endtry(char_u *arg, cctx_T *cctx)
6382 {
6383     scope_T	*scope = cctx->ctx_scope;
6384     garray_T	*instr = &cctx->ctx_instr;
6385     isn_T	*isn;
6386 
6387     // end block scope from :catch or :finally
6388     if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6389 	compile_endblock(cctx);
6390     scope = cctx->ctx_scope;
6391 
6392     // Error if not in a :try scope
6393     if (scope == NULL || scope->se_type != TRY_SCOPE)
6394     {
6395 	if (scope == NULL)
6396 	    emsg(_(e_no_endtry));
6397 	else if (scope->se_type == WHILE_SCOPE)
6398 	    emsg(_(e_endwhile));
6399 	else if (scope->se_type == FOR_SCOPE)
6400 	    emsg(_(e_endfor));
6401 	else
6402 	    emsg(_(e_endif));
6403 	return NULL;
6404     }
6405 
6406     isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
6407     if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6408     {
6409 	emsg(_("E1032: missing :catch or :finally"));
6410 	return NULL;
6411     }
6412 
6413     // Fill in the "end" label in jumps at the end of the blocks, if not done
6414     // by ":finally".
6415     compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
6416 
6417     // End :catch or :finally scope: set value in ISN_TRY instruction
6418     if (isn->isn_arg.try.try_finally == 0)
6419 	isn->isn_arg.try.try_finally = instr->ga_len;
6420     compile_endblock(cctx);
6421 
6422     if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6423 	return NULL;
6424     return arg;
6425 }
6426 
6427 /*
6428  * compile "throw {expr}"
6429  */
6430     static char_u *
6431 compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6432 {
6433     char_u *p = skipwhite(arg);
6434 
6435     if (compile_expr0(&p, cctx) == FAIL)
6436 	return NULL;
6437     if (may_generate_2STRING(-1, cctx) == FAIL)
6438 	return NULL;
6439     if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6440 	return NULL;
6441 
6442     return p;
6443 }
6444 
6445 /*
6446  * compile "echo expr"
6447  * compile "echomsg expr"
6448  * compile "echoerr expr"
6449  * compile "execute expr"
6450  */
6451     static char_u *
6452 compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
6453 {
6454     char_u	*p = arg;
6455     int		count = 0;
6456 
6457     for (;;)
6458     {
6459 	if (compile_expr0(&p, cctx) == FAIL)
6460 	    return NULL;
6461 	++count;
6462 	p = skipwhite(p);
6463 	if (ends_excmd(*p))
6464 	    break;
6465     }
6466 
6467     if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6468 	generate_ECHO(cctx, cmdidx == CMD_echo, count);
6469     else if (cmdidx == CMD_execute)
6470 	generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6471     else if (cmdidx == CMD_echomsg)
6472 	generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6473     else
6474 	generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
6475     return p;
6476 }
6477 
6478 /*
6479  * A command that is not compiled, execute with legacy code.
6480  */
6481     static char_u *
6482 compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6483 {
6484     char_u  *p;
6485     int	    has_expr = FALSE;
6486 
6487     if (cctx->ctx_skip == SKIP_YES)
6488 	goto theend;
6489 
6490     if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
6491 	has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
6492     if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6493     {
6494 	// expand filename in "syntax include [@group] filename"
6495 	has_expr = TRUE;
6496 	eap->arg = skipwhite(eap->arg + 7);
6497 	if (*eap->arg == '@')
6498 	    eap->arg = skiptowhite(eap->arg);
6499     }
6500 
6501     if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
6502     {
6503 	int	count = 0;
6504 	char_u	*start = skipwhite(line);
6505 
6506 	// :cmd xxx`=expr1`yyy`=expr2`zzz
6507 	// PUSHS ":cmd xxx"
6508 	// eval expr1
6509 	// PUSHS "yyy"
6510 	// eval expr2
6511 	// PUSHS "zzz"
6512 	// EXECCONCAT 5
6513 	for (;;)
6514 	{
6515 	    if (p > start)
6516 	    {
6517 		generate_PUSHS(cctx, vim_strnsave(start, p - start));
6518 		++count;
6519 	    }
6520 	    p += 2;
6521 	    if (compile_expr0(&p, cctx) == FAIL)
6522 		return NULL;
6523 	    may_generate_2STRING(-1, cctx);
6524 	    ++count;
6525 	    p = skipwhite(p);
6526 	    if (*p != '`')
6527 	    {
6528 		emsg(_("E1083: missing backtick"));
6529 		return NULL;
6530 	    }
6531 	    start = p + 1;
6532 
6533 	    p = (char_u *)strstr((char *)start, "`=");
6534 	    if (p == NULL)
6535 	    {
6536 		if (*skipwhite(start) != NUL)
6537 		{
6538 		    generate_PUSHS(cctx, vim_strsave(start));
6539 		    ++count;
6540 		}
6541 		break;
6542 	    }
6543 	}
6544 	generate_EXECCONCAT(cctx, count);
6545     }
6546     else
6547 	generate_EXEC(cctx, line);
6548 
6549 theend:
6550     return (char_u *)"";
6551 }
6552 
6553 /*
6554  * Add a function to the list of :def functions.
6555  * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
6556  */
6557     static int
6558 add_def_function(ufunc_T *ufunc)
6559 {
6560     dfunc_T *dfunc;
6561 
6562     if (def_functions.ga_len == 0)
6563     {
6564 	// The first position is not used, so that a zero uf_dfunc_idx means it
6565 	// wasn't set.
6566 	if (ga_grow(&def_functions, 1) == FAIL)
6567 	    return FAIL;
6568 	++def_functions.ga_len;
6569     }
6570 
6571     // Add the function to "def_functions".
6572     if (ga_grow(&def_functions, 1) == FAIL)
6573 	return FAIL;
6574     dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6575     CLEAR_POINTER(dfunc);
6576     dfunc->df_idx = def_functions.ga_len;
6577     ufunc->uf_dfunc_idx = dfunc->df_idx;
6578     dfunc->df_ufunc = ufunc;
6579     ++def_functions.ga_len;
6580     return OK;
6581 }
6582 
6583 /*
6584  * After ex_function() has collected all the function lines: parse and compile
6585  * the lines into instructions.
6586  * Adds the function to "def_functions".
6587  * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6588  * return statement (used for lambda).
6589  * "outer_cctx" is set for a nested function.
6590  * This can be used recursively through compile_lambda(), which may reallocate
6591  * "def_functions".
6592  * Returns OK or FAIL.
6593  */
6594     int
6595 compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
6596 {
6597     char_u	*line = NULL;
6598     char_u	*p;
6599     char	*errormsg = NULL;	// error message
6600     cctx_T	cctx;
6601     garray_T	*instr;
6602     int		called_emsg_before = called_emsg;
6603     int		ret = FAIL;
6604     sctx_T	save_current_sctx = current_sctx;
6605     int		do_estack_push;
6606     int		emsg_before = called_emsg;
6607 
6608     // When using a function that was compiled before: Free old instructions.
6609     // Otherwise add a new entry in "def_functions".
6610     if (ufunc->uf_dfunc_idx > 0)
6611     {
6612 	dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6613 							 + ufunc->uf_dfunc_idx;
6614 	delete_def_function_contents(dfunc);
6615     }
6616     else if (add_def_function(ufunc) == FAIL)
6617 	return FAIL;
6618 
6619     CLEAR_FIELD(cctx);
6620     cctx.ctx_ufunc = ufunc;
6621     cctx.ctx_lnum = -1;
6622     cctx.ctx_outer = outer_cctx;
6623     ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6624     ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6625     ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6626     cctx.ctx_type_list = &ufunc->uf_type_list;
6627     ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6628     instr = &cctx.ctx_instr;
6629 
6630     // Set the context to the function, it may be compiled when called from
6631     // another script.  Set the script version to the most modern one.
6632     // The line number will be set in next_line_from_context().
6633     current_sctx = ufunc->uf_script_ctx;
6634     current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6635 
6636     // Make sure error messages are OK.
6637     do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6638     if (do_estack_push)
6639 	estack_push_ufunc(ufunc, 1);
6640 
6641     if (ufunc->uf_def_args.ga_len > 0)
6642     {
6643 	int	count = ufunc->uf_def_args.ga_len;
6644 	int	first_def_arg = ufunc->uf_args.ga_len - count;
6645 	int	i;
6646 	char_u	*arg;
6647 	int	off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6648 
6649 	// Produce instructions for the default values of optional arguments.
6650 	// Store the instruction index in uf_def_arg_idx[] so that we know
6651 	// where to start when the function is called, depending on the number
6652 	// of arguments.
6653 	ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6654 	if (ufunc->uf_def_arg_idx == NULL)
6655 	    goto erret;
6656 	for (i = 0; i < count; ++i)
6657 	{
6658 	    garray_T	*stack = &cctx.ctx_type_stack;
6659 	    type_T	*val_type;
6660 	    int		arg_idx = first_def_arg + i;
6661 
6662 	    ufunc->uf_def_arg_idx[i] = instr->ga_len;
6663 	    arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
6664 	    if (compile_expr0(&arg, &cctx) == FAIL)
6665 		goto erret;
6666 
6667 	    // If no type specified use the type of the default value.
6668 	    // Otherwise check that the default value type matches the
6669 	    // specified type.
6670 	    val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6671 	    if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6672 		ufunc->uf_arg_types[arg_idx] = val_type;
6673 	    else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6674 								       == FAIL)
6675 	    {
6676 		arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6677 								  arg_idx + 1);
6678 		goto erret;
6679 	    }
6680 
6681 	    if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
6682 		goto erret;
6683 	}
6684 	ufunc->uf_def_arg_idx[count] = instr->ga_len;
6685     }
6686 
6687     /*
6688      * Loop over all the lines of the function and generate instructions.
6689      */
6690     for (;;)
6691     {
6692 	exarg_T	ea;
6693 	int	starts_with_colon = FALSE;
6694 
6695 	// Bail out on the first error to avoid a flood of errors and report
6696 	// the right line number when inside try/catch.
6697 	if (emsg_before != called_emsg)
6698 	    goto erret;
6699 
6700 	if (line != NULL && *line == '|')
6701 	    // the line continues after a '|'
6702 	    ++line;
6703 	else if (line != NULL && *skipwhite(line) != NUL
6704 		&& !(*line == '#' && (line == cctx.ctx_line_start
6705 						    || VIM_ISWHITE(line[-1]))))
6706 	{
6707 	    semsg(_("E488: Trailing characters: %s"), line);
6708 	    goto erret;
6709 	}
6710 	else
6711 	{
6712 	    line = next_line_from_context(&cctx, FALSE);
6713 	    if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
6714 		// beyond the last line
6715 		break;
6716 	}
6717 	emsg_before = called_emsg;
6718 
6719 	CLEAR_FIELD(ea);
6720 	ea.cmdlinep = &line;
6721 	ea.cmd = skipwhite(line);
6722 
6723 	// Some things can be recognized by the first character.
6724 	switch (*ea.cmd)
6725 	{
6726 	    case '#':
6727 		// "#" starts a comment, but "#{" does not.
6728 		if (ea.cmd[1] != '{')
6729 		{
6730 		    line = (char_u *)"";
6731 		    continue;
6732 		}
6733 		break;
6734 
6735 	    case '}':
6736 		{
6737 		    // "}" ends a block scope
6738 		    scopetype_T stype = cctx.ctx_scope == NULL
6739 					  ? NO_SCOPE : cctx.ctx_scope->se_type;
6740 
6741 		    if (stype == BLOCK_SCOPE)
6742 		    {
6743 			compile_endblock(&cctx);
6744 			line = ea.cmd;
6745 		    }
6746 		    else
6747 		    {
6748 			emsg(_("E1025: using } outside of a block scope"));
6749 			goto erret;
6750 		    }
6751 		    if (line != NULL)
6752 			line = skipwhite(ea.cmd + 1);
6753 		    continue;
6754 		}
6755 
6756 	    case '{':
6757 		// "{" starts a block scope
6758 		// "{'a': 1}->func() is something else
6759 		if (ends_excmd(*skipwhite(ea.cmd + 1)))
6760 		{
6761 		    line = compile_block(ea.cmd, &cctx);
6762 		    continue;
6763 		}
6764 		break;
6765 
6766 	    case ':':
6767 		starts_with_colon = TRUE;
6768 		break;
6769 	}
6770 
6771 	/*
6772 	 * COMMAND MODIFIERS
6773 	 */
6774 	if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6775 	{
6776 	    if (errormsg != NULL)
6777 		goto erret;
6778 	    // empty line or comment
6779 	    line = (char_u *)"";
6780 	    continue;
6781 	}
6782 
6783 	// Skip ":call" to get to the function name.
6784 	if (checkforcmd(&ea.cmd, "call", 3))
6785 	    ea.cmd = skipwhite(ea.cmd);
6786 
6787 	if (!starts_with_colon)
6788 	{
6789 	    char_u *pskip;
6790 
6791 	    // Assuming the command starts with a variable or function name,
6792 	    // find what follows.
6793 	    // Skip over "var.member", "var[idx]" and the like.
6794 	    // Also "&opt = val", "$ENV = val" and "@r = val".
6795 	    pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
6796 							 ? ea.cmd + 1 : ea.cmd;
6797 	    p = to_name_end(pskip, TRUE);
6798 	    if (p > ea.cmd && *p != NUL)
6799 	    {
6800 		char_u *var_end;
6801 		int	oplen;
6802 		int	heredoc;
6803 
6804 		var_end = find_name_end(pskip, NULL, NULL,
6805 						FNE_CHECK_START | FNE_INCL_BR);
6806 		oplen = assignment_len(skipwhite(var_end), &heredoc);
6807 		if (oplen > 0)
6808 		{
6809 		    size_t len = p - ea.cmd;
6810 
6811 		    // Recognize an assignment if we recognize the variable
6812 		    // name:
6813 		    // "g:var = expr"
6814 		    // "local = expr"  where "local" is a local var.
6815 		    // "script = expr"  where "script" is a script-local var.
6816 		    // "import = expr"  where "import" is an imported var
6817 		    // "&opt = expr"
6818 		    // "$ENV = expr"
6819 		    // "@r = expr"
6820 		    if (*ea.cmd == '&'
6821 			    || *ea.cmd == '$'
6822 			    || *ea.cmd == '@'
6823 			    || ((len) > 2 && ea.cmd[1] == ':')
6824 			    || lookup_local(ea.cmd, len, &cctx) != NULL
6825 			    || lookup_arg(ea.cmd, len, NULL, NULL,
6826 							     NULL, &cctx) == OK
6827 			    || lookup_script(ea.cmd, len) == OK
6828 			    || find_imported(ea.cmd, len, &cctx) != NULL)
6829 		    {
6830 			line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6831 			if (line == NULL || line == ea.cmd)
6832 			    goto erret;
6833 			continue;
6834 		    }
6835 		}
6836 	    }
6837 
6838 	    if (*ea.cmd == '[')
6839 	    {
6840 		// [var, var] = expr
6841 		line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6842 		if (line == NULL)
6843 		    goto erret;
6844 		if (line != ea.cmd)
6845 		    continue;
6846 	    }
6847 	}
6848 
6849 	/*
6850 	 * COMMAND after range
6851 	 */
6852 	ea.cmd = skip_range(ea.cmd, NULL);
6853 	p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
6854 		   : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
6855 									&cctx);
6856 
6857 	if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6858 	{
6859 	    if (cctx.ctx_skip == SKIP_YES)
6860 	    {
6861 		line += STRLEN(line);
6862 		continue;
6863 	    }
6864 
6865 	    // Expression or function call.
6866 	    if (ea.cmdidx == CMD_eval)
6867 	    {
6868 		p = ea.cmd;
6869 		if (compile_expr0(&p, &cctx) == FAIL)
6870 		    goto erret;
6871 
6872 		// drop the return value
6873 		generate_instr_drop(&cctx, ISN_DROP, 1);
6874 		line = p;
6875 		continue;
6876 	    }
6877 	    // CMD_let cannot happen, compile_assignment() above is used
6878 	    iemsg("Command from find_ex_command() not handled");
6879 	    goto erret;
6880 	}
6881 
6882 	p = skipwhite(p);
6883 
6884 	if (cctx.ctx_skip == SKIP_YES
6885 		&& ea.cmdidx != CMD_if
6886 		&& ea.cmdidx != CMD_elseif
6887 		&& ea.cmdidx != CMD_else
6888 		&& ea.cmdidx != CMD_endif)
6889 	{
6890 	    line = (char_u *)"";
6891 	    continue;
6892 	}
6893 
6894 	if (ea.cmdidx != CMD_elseif
6895 		&& ea.cmdidx != CMD_else
6896 		&& ea.cmdidx != CMD_endif
6897 		&& ea.cmdidx != CMD_endfor
6898 		&& ea.cmdidx != CMD_endwhile
6899 		&& ea.cmdidx != CMD_catch
6900 		&& ea.cmdidx != CMD_finally
6901 		&& ea.cmdidx != CMD_endtry)
6902 	{
6903 	    if (cctx.ctx_had_return)
6904 	    {
6905 		emsg(_("E1095: Unreachable code after :return"));
6906 		goto erret;
6907 	    }
6908 	}
6909 
6910 	switch (ea.cmdidx)
6911 	{
6912 	    case CMD_def:
6913 		    ea.arg = p;
6914 		    line = compile_nested_function(&ea, &cctx);
6915 		    break;
6916 
6917 	    case CMD_function:
6918 		    emsg(_("E1086: Cannot use :function inside :def"));
6919 		    goto erret;
6920 
6921 	    case CMD_return:
6922 		    line = compile_return(p, set_return_type, &cctx);
6923 		    cctx.ctx_had_return = TRUE;
6924 		    break;
6925 
6926 	    case CMD_let:
6927 	    case CMD_const:
6928 		    line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
6929 		    if (line == p)
6930 			line = NULL;
6931 		    break;
6932 
6933 	    case CMD_unlet:
6934 	    case CMD_unlockvar:
6935 	    case CMD_lockvar:
6936 		    line = compile_unletlock(p, &ea, &cctx);
6937 		    break;
6938 
6939 	    case CMD_import:
6940 		    line = compile_import(p, &cctx);
6941 		    break;
6942 
6943 	    case CMD_if:
6944 		    line = compile_if(p, &cctx);
6945 		    break;
6946 	    case CMD_elseif:
6947 		    line = compile_elseif(p, &cctx);
6948 		    cctx.ctx_had_return = FALSE;
6949 		    break;
6950 	    case CMD_else:
6951 		    line = compile_else(p, &cctx);
6952 		    cctx.ctx_had_return = FALSE;
6953 		    break;
6954 	    case CMD_endif:
6955 		    line = compile_endif(p, &cctx);
6956 		    break;
6957 
6958 	    case CMD_while:
6959 		    line = compile_while(p, &cctx);
6960 		    break;
6961 	    case CMD_endwhile:
6962 		    line = compile_endwhile(p, &cctx);
6963 		    cctx.ctx_had_return = FALSE;
6964 		    break;
6965 
6966 	    case CMD_for:
6967 		    line = compile_for(p, &cctx);
6968 		    break;
6969 	    case CMD_endfor:
6970 		    line = compile_endfor(p, &cctx);
6971 		    cctx.ctx_had_return = FALSE;
6972 		    break;
6973 	    case CMD_continue:
6974 		    line = compile_continue(p, &cctx);
6975 		    break;
6976 	    case CMD_break:
6977 		    line = compile_break(p, &cctx);
6978 		    break;
6979 
6980 	    case CMD_try:
6981 		    line = compile_try(p, &cctx);
6982 		    break;
6983 	    case CMD_catch:
6984 		    line = compile_catch(p, &cctx);
6985 		    cctx.ctx_had_return = FALSE;
6986 		    break;
6987 	    case CMD_finally:
6988 		    line = compile_finally(p, &cctx);
6989 		    cctx.ctx_had_return = FALSE;
6990 		    break;
6991 	    case CMD_endtry:
6992 		    line = compile_endtry(p, &cctx);
6993 		    cctx.ctx_had_return = FALSE;
6994 		    break;
6995 	    case CMD_throw:
6996 		    line = compile_throw(p, &cctx);
6997 		    break;
6998 
6999 	    case CMD_echo:
7000 	    case CMD_echon:
7001 	    case CMD_execute:
7002 	    case CMD_echomsg:
7003 	    case CMD_echoerr:
7004 		    line = compile_mult_expr(p, ea.cmdidx, &cctx);
7005 		    break;
7006 
7007 	    default:
7008 		    // TODO: other commands with an expression argument
7009 		    // Not recognized, execute with do_cmdline_cmd().
7010 		    ea.arg = p;
7011 		    line = compile_exec(line, &ea, &cctx);
7012 		    break;
7013 	}
7014 	if (line == NULL)
7015 	    goto erret;
7016 	line = skipwhite(line);
7017 
7018 	if (cctx.ctx_type_stack.ga_len < 0)
7019 	{
7020 	    iemsg("Type stack underflow");
7021 	    goto erret;
7022 	}
7023     }
7024 
7025     if (cctx.ctx_scope != NULL)
7026     {
7027 	if (cctx.ctx_scope->se_type == IF_SCOPE)
7028 	    emsg(_(e_endif));
7029 	else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7030 	    emsg(_(e_endwhile));
7031 	else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7032 	    emsg(_(e_endfor));
7033 	else
7034 	    emsg(_("E1026: Missing }"));
7035 	goto erret;
7036     }
7037 
7038     if (!cctx.ctx_had_return)
7039     {
7040 	if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7041 	{
7042 	    emsg(_("E1027: Missing return statement"));
7043 	    goto erret;
7044 	}
7045 
7046 	// Return zero if there is no return at the end.
7047 	generate_PUSHNR(&cctx, 0);
7048 	generate_instr(&cctx, ISN_RETURN);
7049     }
7050 
7051     {
7052 	dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
7053 							 + ufunc->uf_dfunc_idx;
7054 	dfunc->df_deleted = FALSE;
7055 	dfunc->df_instr = instr->ga_data;
7056 	dfunc->df_instr_count = instr->ga_len;
7057 	dfunc->df_varcount = cctx.ctx_locals_count;
7058 	dfunc->df_closure_count = cctx.ctx_closure_count;
7059 	if (cctx.ctx_outer_used)
7060 	    ufunc->uf_flags |= FC_CLOSURE;
7061 	ufunc->uf_def_status = UF_COMPILED;
7062     }
7063 
7064     ret = OK;
7065 
7066 erret:
7067     if (ret == FAIL)
7068     {
7069 	int idx;
7070 	dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
7071 							 + ufunc->uf_dfunc_idx;
7072 
7073 	for (idx = 0; idx < instr->ga_len; ++idx)
7074 	    delete_instr(((isn_T *)instr->ga_data) + idx);
7075 	ga_clear(instr);
7076 
7077 	// if using the last entry in the table we might as well remove it
7078 	if (!dfunc->df_deleted
7079 			    && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
7080 	    --def_functions.ga_len;
7081 	ufunc->uf_def_status = UF_NOT_COMPILED;
7082 
7083 	while (cctx.ctx_scope != NULL)
7084 	    drop_scope(&cctx);
7085 
7086 	// Don't execute this function body.
7087 	ga_clear_strings(&ufunc->uf_lines);
7088 
7089 	if (errormsg != NULL)
7090 	    emsg(errormsg);
7091 	else if (called_emsg == called_emsg_before)
7092 	    emsg(_("E1028: compile_def_function failed"));
7093     }
7094 
7095     current_sctx = save_current_sctx;
7096     if (do_estack_push)
7097 	estack_pop();
7098 
7099     free_imported(&cctx);
7100     free_locals(&cctx);
7101     ga_clear(&cctx.ctx_type_stack);
7102     return ret;
7103 }
7104 
7105     void
7106 set_function_type(ufunc_T *ufunc)
7107 {
7108     int varargs = ufunc->uf_va_name != NULL;
7109     int argcount = ufunc->uf_args.ga_len;
7110 
7111     // Create a type for the function, with the return type and any
7112     // argument types.
7113     // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7114     // The type is included in "tt_args".
7115     if (argcount > 0 || varargs)
7116     {
7117 	ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7118 					   argcount, &ufunc->uf_type_list);
7119 	// Add argument types to the function type.
7120 	if (func_type_add_arg_types(ufunc->uf_func_type,
7121 				    argcount + varargs,
7122 				    &ufunc->uf_type_list) == FAIL)
7123 	    return;
7124 	ufunc->uf_func_type->tt_argcount = argcount + varargs;
7125 	ufunc->uf_func_type->tt_min_argcount =
7126 				      argcount - ufunc->uf_def_args.ga_len;
7127 	if (ufunc->uf_arg_types == NULL)
7128 	{
7129 	    int i;
7130 
7131 	    // lambda does not have argument types.
7132 	    for (i = 0; i < argcount; ++i)
7133 		ufunc->uf_func_type->tt_args[i] = &t_any;
7134 	}
7135 	else
7136 	    mch_memmove(ufunc->uf_func_type->tt_args,
7137 			 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7138 	if (varargs)
7139 	{
7140 	    ufunc->uf_func_type->tt_args[argcount] =
7141 		    ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7142 	    ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7143 	}
7144     }
7145     else
7146 	// No arguments, can use a predefined type.
7147 	ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7148 					   argcount, &ufunc->uf_type_list);
7149 }
7150 
7151 
7152 /*
7153  * Delete an instruction, free what it contains.
7154  */
7155     void
7156 delete_instr(isn_T *isn)
7157 {
7158     switch (isn->isn_type)
7159     {
7160 	case ISN_EXEC:
7161 	case ISN_LOADENV:
7162 	case ISN_LOADG:
7163 	case ISN_LOADB:
7164 	case ISN_LOADW:
7165 	case ISN_LOADT:
7166 	case ISN_LOADOPT:
7167 	case ISN_STRINGMEMBER:
7168 	case ISN_PUSHEXC:
7169 	case ISN_PUSHS:
7170 	case ISN_STOREENV:
7171 	case ISN_STOREG:
7172 	case ISN_STOREB:
7173 	case ISN_STOREW:
7174 	case ISN_STORET:
7175 	case ISN_PUSHFUNC:
7176 	    vim_free(isn->isn_arg.string);
7177 	    break;
7178 
7179 	case ISN_LOADS:
7180 	case ISN_STORES:
7181 	    vim_free(isn->isn_arg.loadstore.ls_name);
7182 	    break;
7183 
7184 	case ISN_UNLET:
7185 	case ISN_UNLETENV:
7186 	    vim_free(isn->isn_arg.unlet.ul_name);
7187 	    break;
7188 
7189 	case ISN_STOREOPT:
7190 	    vim_free(isn->isn_arg.storeopt.so_name);
7191 	    break;
7192 
7193 	case ISN_PUSHBLOB:   // push blob isn_arg.blob
7194 	    blob_unref(isn->isn_arg.blob);
7195 	    break;
7196 
7197 	case ISN_PUSHJOB:
7198 #ifdef FEAT_JOB_CHANNEL
7199 	    job_unref(isn->isn_arg.job);
7200 #endif
7201 	    break;
7202 
7203 	case ISN_PUSHCHANNEL:
7204 #ifdef FEAT_JOB_CHANNEL
7205 	    channel_unref(isn->isn_arg.channel);
7206 #endif
7207 	    break;
7208 
7209 	case ISN_UCALL:
7210 	    vim_free(isn->isn_arg.ufunc.cuf_name);
7211 	    break;
7212 
7213 	case ISN_FUNCREF:
7214 	    {
7215 		dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7216 					       + isn->isn_arg.funcref.fr_func;
7217 		func_ptr_unref(dfunc->df_ufunc);
7218 	    }
7219 	    break;
7220 
7221 	case ISN_2BOOL:
7222 	case ISN_2STRING:
7223 	case ISN_ADDBLOB:
7224 	case ISN_ADDLIST:
7225 	case ISN_BCALL:
7226 	case ISN_CATCH:
7227 	case ISN_CHECKNR:
7228 	case ISN_CHECKTYPE:
7229 	case ISN_CHECKLEN:
7230 	case ISN_COMPAREANY:
7231 	case ISN_COMPAREBLOB:
7232 	case ISN_COMPAREBOOL:
7233 	case ISN_COMPAREDICT:
7234 	case ISN_COMPAREFLOAT:
7235 	case ISN_COMPAREFUNC:
7236 	case ISN_COMPARELIST:
7237 	case ISN_COMPARENR:
7238 	case ISN_COMPARESPECIAL:
7239 	case ISN_COMPARESTRING:
7240 	case ISN_CONCAT:
7241 	case ISN_DCALL:
7242 	case ISN_DROP:
7243 	case ISN_ECHO:
7244 	case ISN_ECHOERR:
7245 	case ISN_ECHOMSG:
7246 	case ISN_ENDTRY:
7247 	case ISN_EXECCONCAT:
7248 	case ISN_EXECUTE:
7249 	case ISN_FOR:
7250 	case ISN_INDEX:
7251 	case ISN_GETITEM:
7252 	case ISN_SLICE:
7253 	case ISN_MEMBER:
7254 	case ISN_JUMP:
7255 	case ISN_LOAD:
7256 	case ISN_LOADOUTER:
7257 	case ISN_LOADSCRIPT:
7258 	case ISN_LOADREG:
7259 	case ISN_LOADV:
7260 	case ISN_NEGATENR:
7261 	case ISN_NEWDICT:
7262 	case ISN_NEWLIST:
7263 	case ISN_OPNR:
7264 	case ISN_OPFLOAT:
7265 	case ISN_OPANY:
7266 	case ISN_PCALL:
7267 	case ISN_PCALL_END:
7268 	case ISN_PUSHF:
7269 	case ISN_PUSHNR:
7270 	case ISN_PUSHBOOL:
7271 	case ISN_PUSHSPEC:
7272 	case ISN_RETURN:
7273 	case ISN_STORE:
7274 	case ISN_STOREOUTER:
7275 	case ISN_STOREV:
7276 	case ISN_STORENR:
7277 	case ISN_STOREREG:
7278 	case ISN_STORESCRIPT:
7279 	case ISN_STOREDICT:
7280 	case ISN_STORELIST:
7281 	case ISN_THROW:
7282 	case ISN_TRY:
7283 	    // nothing allocated
7284 	    break;
7285     }
7286 }
7287 
7288 /*
7289  * Free all instructions for "dfunc".
7290  */
7291     static void
7292 delete_def_function_contents(dfunc_T *dfunc)
7293 {
7294     int idx;
7295 
7296     ga_clear(&dfunc->df_def_args_isn);
7297 
7298     if (dfunc->df_instr != NULL)
7299     {
7300 	for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7301 	    delete_instr(dfunc->df_instr + idx);
7302 	VIM_CLEAR(dfunc->df_instr);
7303     }
7304 
7305     dfunc->df_deleted = TRUE;
7306 }
7307 
7308 /*
7309  * When a user function is deleted, clear the contents of any associated def
7310  * function.  The position in def_functions can be re-used.
7311  */
7312     void
7313 clear_def_function(ufunc_T *ufunc)
7314 {
7315     if (ufunc->uf_dfunc_idx > 0)
7316     {
7317 	dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7318 							 + ufunc->uf_dfunc_idx;
7319 
7320 	delete_def_function_contents(dfunc);
7321 	ufunc->uf_def_status = UF_NOT_COMPILED;
7322     }
7323 }
7324 
7325 #if defined(EXITFREE) || defined(PROTO)
7326 /*
7327  * Free all functions defined with ":def".
7328  */
7329     void
7330 free_def_functions(void)
7331 {
7332     int idx;
7333 
7334     for (idx = 0; idx < def_functions.ga_len; ++idx)
7335     {
7336 	dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7337 
7338 	delete_def_function_contents(dfunc);
7339     }
7340 
7341     ga_clear(&def_functions);
7342 }
7343 #endif
7344 
7345 
7346 #endif // FEAT_EVAL
7347