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