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