xref: /vim-8.2.3635/src/vim9compile.c (revision 80b2ba3e)
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_seen_skip_not;   // a block was unconditionally executed
48     int		is_had_return;	    // every block ends in :return
49     int		is_if_label;	    // instruction idx at IF or ELSEIF
50     endlabel_T	*is_end_label;	    // instructions to set end label
51 } ifscope_T;
52 
53 /*
54  * info specific for the scope of :while
55  */
56 typedef struct {
57     int		ws_top_label;	    // instruction idx at WHILE
58     endlabel_T	*ws_end_label;	    // instructions to set end
59 } whilescope_T;
60 
61 /*
62  * info specific for the scope of :for
63  */
64 typedef struct {
65     int		fs_top_label;	    // instruction idx at FOR
66     endlabel_T	*fs_end_label;	    // break instructions
67 } forscope_T;
68 
69 /*
70  * info specific for the scope of :try
71  */
72 typedef struct {
73     int		ts_try_label;	    // instruction idx at TRY
74     endlabel_T	*ts_end_label;	    // jump to :finally or :endtry
75     int		ts_catch_label;	    // instruction idx of last CATCH
76     int		ts_caught_all;	    // "catch" without argument encountered
77 } tryscope_T;
78 
79 typedef enum {
80     NO_SCOPE,
81     IF_SCOPE,
82     WHILE_SCOPE,
83     FOR_SCOPE,
84     TRY_SCOPE,
85     BLOCK_SCOPE
86 } scopetype_T;
87 
88 /*
89  * Info for one scope, pointed to by "ctx_scope".
90  */
91 typedef struct scope_S scope_T;
92 struct scope_S {
93     scope_T	*se_outer;	    // scope containing this one
94     scopetype_T se_type;
95     int		se_local_count;	    // ctx_locals.ga_len before scope
96     skip_T	se_skip_save;	    // ctx_skip before the block
97     union {
98 	ifscope_T	se_if;
99 	whilescope_T	se_while;
100 	forscope_T	se_for;
101 	tryscope_T	se_try;
102     } se_u;
103 };
104 
105 /*
106  * Entry for "ctx_locals".  Used for arguments and local variables.
107  */
108 typedef struct {
109     char_u	*lv_name;
110     type_T	*lv_type;
111     int		lv_idx;		// index of the variable on the stack
112     int		lv_from_outer;	// nesting level, using ctx_outer scope
113     int		lv_const;	// when TRUE cannot be assigned to
114     int		lv_arg;		// when TRUE this is an argument
115 } lvar_T;
116 
117 // Destination for an assignment or ":unlet" with an index.
118 typedef enum {
119     dest_local,
120     dest_option,
121     dest_env,
122     dest_global,
123     dest_buffer,
124     dest_window,
125     dest_tab,
126     dest_vimvar,
127     dest_script,
128     dest_reg,
129     dest_expr,
130 } assign_dest_T;
131 
132 // Used by compile_lhs() to store information about the LHS of an assignment
133 // and one argument of ":unlet" with an index.
134 typedef struct {
135     assign_dest_T   lhs_dest;	    // type of destination
136 
137     char_u	    *lhs_name;	    // allocated name excluding the last
138 				    // "[expr]" or ".name".
139     size_t	    lhs_varlen;	    // length of the variable without
140 				    // "[expr]" or ".name"
141     char_u	    *lhs_whole;	    // allocated name including the last
142 				    // "[expr]" or ".name" for :redir
143     size_t	    lhs_varlen_total; // length of the variable including
144 				      // any "[expr]" or ".name"
145     char_u	    *lhs_dest_end;  // end of the destination, including
146 				    // "[expr]" or ".name".
147 
148     int		    lhs_has_index;  // has "[expr]" or ".name"
149 
150     int		    lhs_new_local;  // create new local variable
151     int		    lhs_opt_flags;  // for when destination is an option
152     int		    lhs_vimvaridx;  // for when destination is a v:var
153 
154     lvar_T	    lhs_local_lvar; // used for existing local destination
155     lvar_T	    lhs_arg_lvar;   // used for argument destination
156     lvar_T	    *lhs_lvar;	    // points to destination lvar
157     int		    lhs_scriptvar_sid;
158     int		    lhs_scriptvar_idx;
159 
160     int		    lhs_has_type;   // type was specified
161     type_T	    *lhs_type;
162     type_T	    *lhs_member_type;
163 
164     int		    lhs_append;	    // used by ISN_REDIREND
165 } lhs_T;
166 
167 /*
168  * Context for compiling lines of Vim script.
169  * Stores info about the local variables and condition stack.
170  */
171 struct cctx_S {
172     ufunc_T	*ctx_ufunc;	    // current function
173     int		ctx_lnum;	    // line number in current function
174     char_u	*ctx_line_start;    // start of current line or NULL
175     garray_T	ctx_instr;	    // generated instructions
176 
177     int		ctx_prev_lnum;	    // line number below previous command, for
178 				    // debugging
179 
180     compiletype_T ctx_compile_type;
181 
182     garray_T	ctx_locals;	    // currently visible local variables
183 
184     int		ctx_has_closure;    // set to one if a closures was created in
185 				    // the function
186 
187     garray_T	ctx_imports;	    // imported items
188 
189     skip_T	ctx_skip;
190     scope_T	*ctx_scope;	    // current scope, NULL at toplevel
191     int		ctx_had_return;	    // last seen statement was "return"
192 
193     cctx_T	*ctx_outer;	    // outer scope for lambda or nested
194 				    // function
195     int		ctx_outer_used;	    // var in ctx_outer was used
196 
197     garray_T	ctx_type_stack;	    // type of each item on the stack
198     garray_T	*ctx_type_list;	    // list of pointers to allocated types
199 
200     int		ctx_has_cmdmod;	    // ISN_CMDMOD was generated
201 
202     lhs_T	ctx_redir_lhs;	    // LHS for ":redir => var", valid when
203 				    // lhs_name is not NULL
204 };
205 
206 static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
207 
208 /*
209  * Lookup variable "name" in the local scope and return it in "lvar".
210  * "lvar->lv_from_outer" is incremented accordingly.
211  * If "lvar" is NULL only check if the variable can be found.
212  * Return FAIL if not found.
213  */
214     static int
215 lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
216 {
217     int	    idx;
218     lvar_T  *lvp;
219 
220     if (len == 0)
221 	return FAIL;
222 
223     // Find local in current function scope.
224     for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
225     {
226 	lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
227 	if (STRNCMP(name, lvp->lv_name, len) == 0
228 					       && STRLEN(lvp->lv_name) == len)
229 	{
230 	    if (lvar != NULL)
231 	    {
232 		*lvar = *lvp;
233 		lvar->lv_from_outer = 0;
234 	    }
235 	    return OK;
236 	}
237     }
238 
239     // Find local in outer function scope.
240     if (cctx->ctx_outer != NULL)
241     {
242 	if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
243 	{
244 	    if (lvar != NULL)
245 	    {
246 		cctx->ctx_outer_used = TRUE;
247 		++lvar->lv_from_outer;
248 	    }
249 	    return OK;
250 	}
251     }
252 
253     return FAIL;
254 }
255 
256 /*
257  * Lookup an argument in the current function and an enclosing function.
258  * Returns the argument index in "idxp"
259  * Returns the argument type in "type"
260  * Sets "gen_load_outer" to TRUE if found in outer scope.
261  * Returns OK when found, FAIL otherwise.
262  */
263     static int
264 arg_exists(
265 	char_u	*name,
266 	size_t	len,
267 	int	*idxp,
268 	type_T	**type,
269 	int	*gen_load_outer,
270 	cctx_T	*cctx)
271 {
272     int	    idx;
273     char_u  *va_name;
274 
275     if (len == 0)
276 	return FAIL;
277     for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
278     {
279 	char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
280 
281 	if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
282 	{
283 	    if (idxp != NULL)
284 	    {
285 		// Arguments are located above the frame pointer.  One further
286 		// if there is a vararg argument
287 		*idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
288 							    + STACK_FRAME_SIZE)
289 			      + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
290 
291 		if (cctx->ctx_ufunc->uf_arg_types != NULL)
292 		    *type = cctx->ctx_ufunc->uf_arg_types[idx];
293 		else
294 		    *type = &t_any;
295 	    }
296 	    return OK;
297 	}
298     }
299 
300     va_name = cctx->ctx_ufunc->uf_va_name;
301     if (va_name != NULL
302 		    && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
303     {
304 	if (idxp != NULL)
305 	{
306 	    // varargs is always the last argument
307 	    *idxp = -STACK_FRAME_SIZE - 1;
308 	    *type = cctx->ctx_ufunc->uf_va_type;
309 	}
310 	return OK;
311     }
312 
313     if (cctx->ctx_outer != NULL)
314     {
315 	// Lookup the name for an argument of the outer function.
316 	if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
317 									 == OK)
318 	{
319 	    if (gen_load_outer != NULL)
320 		++*gen_load_outer;
321 	    return OK;
322 	}
323     }
324 
325     return FAIL;
326 }
327 
328 /*
329  * Lookup a script-local variable in the current script, possibly defined in a
330  * block that contains the function "cctx->ctx_ufunc".
331  * "cctx" is NULL at the script level.
332  * If "len" is <= 0 "name" must be NUL terminated.
333  * Return NULL when not found.
334  */
335     static sallvar_T *
336 find_script_var(char_u *name, size_t len, cctx_T *cctx)
337 {
338     scriptitem_T    *si = SCRIPT_ITEM(current_sctx.sc_sid);
339     hashitem_T	    *hi;
340     int		    cc;
341     sallvar_T	    *sav;
342     ufunc_T	    *ufunc;
343 
344     // Find the list of all script variables with the right name.
345     if (len > 0)
346     {
347 	cc = name[len];
348 	name[len] = NUL;
349     }
350     hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
351     if (len > 0)
352 	name[len] = cc;
353     if (HASHITEM_EMPTY(hi))
354 	return NULL;
355 
356     sav = HI2SAV(hi);
357     if (sav->sav_block_id == 0 || cctx == NULL)
358 	// variable defined in the script scope or not in a function.
359 	return sav;
360 
361     // Go over the variables with this name and find one that was visible
362     // from the function.
363     ufunc = cctx->ctx_ufunc;
364     while (sav != NULL)
365     {
366 	int idx;
367 
368 	// Go over the blocks that this function was defined in.  If the
369 	// variable block ID matches it was visible to the function.
370 	for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
371 	    if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
372 		return sav;
373 	sav = sav->sav_next;
374     }
375 
376     return NULL;
377 }
378 
379 /*
380  * Return TRUE if the script context is Vim9 script.
381  */
382     static int
383 script_is_vim9()
384 {
385     return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
386 }
387 
388 /*
389  * Lookup a variable (without s: prefix) in the current script.
390  * "cctx" is NULL at the script level.
391  * Returns OK or FAIL.
392  */
393     static int
394 script_var_exists(char_u *name, size_t len, cctx_T *cctx)
395 {
396     if (current_sctx.sc_sid <= 0)
397 	return FAIL;
398     if (script_is_vim9())
399     {
400 	// Check script variables that were visible where the function was
401 	// defined.
402 	if (find_script_var(name, len, cctx) != NULL)
403 	    return OK;
404     }
405     else
406     {
407 	hashtab_T	*ht = &SCRIPT_VARS(current_sctx.sc_sid);
408 	dictitem_T	*di;
409 	int		cc;
410 
411 	// Check script variables that are currently visible
412 	cc = name[len];
413 	name[len] = NUL;
414 	di = find_var_in_ht(ht, 0, name, TRUE);
415 	name[len] = cc;
416 	if (di != NULL)
417 	    return OK;
418     }
419 
420     return FAIL;
421 }
422 
423 /*
424  * Return TRUE if "name" is a local variable, argument, script variable or
425  * imported.
426  */
427     static int
428 variable_exists(char_u *name, size_t len, cctx_T *cctx)
429 {
430     return (cctx != NULL
431 		&& (lookup_local(name, len, NULL, cctx) == OK
432 		    || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
433 	    || script_var_exists(name, len, cctx) == OK
434 	    || find_imported(name, len, cctx) != NULL;
435 }
436 
437 /*
438  * Return TRUE if "name" is a local variable, argument, script variable,
439  * imported or function.
440  */
441     static int
442 item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
443 {
444     int	    is_global;
445     char_u  *p;
446 
447     if (variable_exists(name, len, cctx))
448 	return TRUE;
449 
450     // This is similar to what is in lookup_scriptitem():
451     // Find a function, so that a following "->" works.
452     // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
453     // a function call.
454     p = skipwhite(name + len);
455 
456     if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
457     {
458 	// Do not check for an internal function, since it might also be a
459 	// valid command, such as ":split" versus "split()".
460 	// Skip "g:" before a function name.
461 	is_global = (name[0] == 'g' && name[1] == ':');
462 	return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
463     }
464     return FALSE;
465 }
466 
467 /*
468  * Check if "p[len]" is already defined, either in script "import_sid" or in
469  * compilation context "cctx".  "cctx" is NULL at the script level.
470  * Does not check the global namespace.
471  * If "is_arg" is TRUE the error message is for an argument name.
472  * Return FAIL and give an error if it defined.
473  */
474     int
475 check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
476 {
477     int		c = p[len];
478     ufunc_T	*ufunc = NULL;
479 
480     // underscore argument is OK
481     if (len == 1 && *p == '_')
482 	return OK;
483 
484     if (script_var_exists(p, len, cctx) == OK)
485     {
486 	if (is_arg)
487 	    semsg(_(e_argument_already_declared_in_script_str), p);
488 	else
489 	    semsg(_(e_variable_already_declared_in_script_str), p);
490 	return FAIL;
491     }
492 
493     p[len] = NUL;
494     if ((cctx != NULL
495 		&& (lookup_local(p, len, NULL, cctx) == OK
496 		    || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
497 	    || find_imported(p, len, cctx) != NULL
498 	    || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
499     {
500 	// A local or script-local function can shadow a global function.
501 	if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
502 		    && (!func_is_global(ufunc)
503 					     || (p[0] == 'g' && p[1] == ':'))))
504 	{
505 	    if (is_arg)
506 		semsg(_(e_argument_name_shadows_existing_variable_str), p);
507 	    else
508 		semsg(_(e_name_already_defined_str), p);
509 	    p[len] = c;
510 	    return FAIL;
511 	}
512     }
513     p[len] = c;
514     return OK;
515 }
516 
517 
518 /////////////////////////////////////////////////////////////////////
519 // Following generate_ functions expect the caller to call ga_grow().
520 
521 #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
522 #define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
523 
524 /*
525  * Generate an instruction without arguments.
526  * Returns a pointer to the new instruction, NULL if failed.
527  */
528     static isn_T *
529 generate_instr(cctx_T *cctx, isntype_T isn_type)
530 {
531     garray_T	*instr = &cctx->ctx_instr;
532     isn_T	*isn;
533 
534     RETURN_NULL_IF_SKIP(cctx);
535     if (ga_grow(instr, 1) == FAIL)
536 	return NULL;
537     isn = ((isn_T *)instr->ga_data) + instr->ga_len;
538     isn->isn_type = isn_type;
539     isn->isn_lnum = cctx->ctx_lnum + 1;
540     ++instr->ga_len;
541 
542     return isn;
543 }
544 
545 /*
546  * Generate an instruction without arguments.
547  * "drop" will be removed from the stack.
548  * Returns a pointer to the new instruction, NULL if failed.
549  */
550     static isn_T *
551 generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
552 {
553     garray_T	*stack = &cctx->ctx_type_stack;
554 
555     RETURN_NULL_IF_SKIP(cctx);
556     stack->ga_len -= drop;
557     return generate_instr(cctx, isn_type);
558 }
559 
560 /*
561  * Generate instruction "isn_type" and put "type" on the type stack.
562  */
563     static isn_T *
564 generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
565 {
566     isn_T	*isn;
567     garray_T	*stack = &cctx->ctx_type_stack;
568 
569     if ((isn = generate_instr(cctx, isn_type)) == NULL)
570 	return NULL;
571 
572     if (ga_grow(stack, 1) == FAIL)
573 	return NULL;
574     ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
575     ++stack->ga_len;
576 
577     return isn;
578 }
579 
580 /*
581  * Generate an ISN_DEBUG instruction.
582  */
583     static isn_T *
584 generate_instr_debug(cctx_T *cctx)
585 {
586     isn_T	*isn;
587     dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
588 					       + cctx->ctx_ufunc->uf_dfunc_idx;
589 
590     if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
591 	return NULL;
592     isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
593     isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
594     return isn;
595 }
596 
597 /*
598  * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
599  * But only for simple types.
600  * When "tolerant" is TRUE convert most types to string, e.g. a List.
601  */
602     static int
603 may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
604 {
605     isn_T	*isn;
606     isntype_T	isntype = ISN_2STRING;
607     garray_T	*stack = &cctx->ctx_type_stack;
608     type_T	**type;
609 
610     RETURN_OK_IF_SKIP(cctx);
611     type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
612     switch ((*type)->tt_type)
613     {
614 	// nothing to be done
615 	case VAR_STRING: return OK;
616 
617 	// conversion possible
618 	case VAR_SPECIAL:
619 	case VAR_BOOL:
620 	case VAR_NUMBER:
621 	case VAR_FLOAT:
622 			 break;
623 
624 	// conversion possible (with runtime check)
625 	case VAR_ANY:
626 	case VAR_UNKNOWN:
627 			 isntype = ISN_2STRING_ANY;
628 			 break;
629 
630 	// conversion possible when tolerant
631 	case VAR_LIST:
632 			 if (tolerant)
633 			 {
634 			     isntype = ISN_2STRING_ANY;
635 			     break;
636 			 }
637 			 // FALLTHROUGH
638 
639 	// conversion not possible
640 	case VAR_VOID:
641 	case VAR_BLOB:
642 	case VAR_FUNC:
643 	case VAR_PARTIAL:
644 	case VAR_DICT:
645 	case VAR_JOB:
646 	case VAR_CHANNEL:
647 	case VAR_INSTR:
648 			 to_string_error((*type)->tt_type);
649 			 return FAIL;
650     }
651 
652     *type = &t_string;
653     if ((isn = generate_instr(cctx, isntype)) == NULL)
654 	return FAIL;
655     isn->isn_arg.tostring.offset = offset;
656     isn->isn_arg.tostring.tolerant = tolerant;
657 
658     return OK;
659 }
660 
661     static int
662 check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
663 {
664     if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
665 	    && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
666 							 || type2 == VAR_ANY)))
667     {
668 	if (*op == '+')
669 	    emsg(_(e_wrong_argument_type_for_plus));
670 	else
671 	    semsg(_(e_char_requires_number_or_float_arguments), *op);
672 	return FAIL;
673     }
674     return OK;
675 }
676 
677     static int
678 generate_add_instr(
679 	cctx_T *cctx,
680 	vartype_T vartype,
681 	type_T *type1,
682 	type_T *type2)
683 {
684     garray_T	*stack = &cctx->ctx_type_stack;
685     isn_T	*isn = generate_instr_drop(cctx,
686 		      vartype == VAR_NUMBER ? ISN_OPNR
687 		    : vartype == VAR_LIST ? ISN_ADDLIST
688 		    : vartype == VAR_BLOB ? ISN_ADDBLOB
689 #ifdef FEAT_FLOAT
690 		    : vartype == VAR_FLOAT ? ISN_OPFLOAT
691 #endif
692 		    : ISN_OPANY, 1);
693 
694     if (vartype != VAR_LIST && vartype != VAR_BLOB
695 	    && type1->tt_type != VAR_ANY
696 	    && type2->tt_type != VAR_ANY
697 	    && check_number_or_float(
698 			type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
699 	return FAIL;
700 
701     if (isn != NULL)
702 	isn->isn_arg.op.op_type = EXPR_ADD;
703 
704     // When concatenating two lists with different member types the member type
705     // becomes "any".
706     if (vartype == VAR_LIST
707 	    && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
708 	    && type1->tt_member != type2->tt_member)
709 	(((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
710 
711     return isn == NULL ? FAIL : OK;
712 }
713 
714 /*
715  * Get the type to use for an instruction for an operation on "type1" and
716  * "type2".  If they are matching use a type-specific instruction. Otherwise
717  * fall back to runtime type checking.
718  */
719     static vartype_T
720 operator_type(type_T *type1, type_T *type2)
721 {
722     if (type1->tt_type == type2->tt_type
723 	    && (type1->tt_type == VAR_NUMBER
724 		|| type1->tt_type == VAR_LIST
725 #ifdef FEAT_FLOAT
726 		|| type1->tt_type == VAR_FLOAT
727 #endif
728 		|| type1->tt_type == VAR_BLOB))
729 	return type1->tt_type;
730     return VAR_ANY;
731 }
732 
733 /*
734  * Generate an instruction with two arguments.  The instruction depends on the
735  * type of the arguments.
736  */
737     static int
738 generate_two_op(cctx_T *cctx, char_u *op)
739 {
740     garray_T	*stack = &cctx->ctx_type_stack;
741     type_T	*type1;
742     type_T	*type2;
743     vartype_T	vartype;
744     isn_T	*isn;
745 
746     RETURN_OK_IF_SKIP(cctx);
747 
748     // Get the known type of the two items on the stack.
749     type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
750     type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
751     vartype = operator_type(type1, type2);
752 
753     switch (*op)
754     {
755 	case '+':
756 		  if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
757 		      return FAIL;
758 		  break;
759 
760 	case '-':
761 	case '*':
762 	case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
763 								   op) == FAIL)
764 		      return FAIL;
765 		  if (vartype == VAR_NUMBER)
766 		      isn = generate_instr_drop(cctx, ISN_OPNR, 1);
767 #ifdef FEAT_FLOAT
768 		  else if (vartype == VAR_FLOAT)
769 		      isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
770 #endif
771 		  else
772 		      isn = generate_instr_drop(cctx, ISN_OPANY, 1);
773 		  if (isn != NULL)
774 		      isn->isn_arg.op.op_type = *op == '*'
775 				 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
776 		  break;
777 
778 	case '%': if ((type1->tt_type != VAR_ANY
779 					       && type1->tt_type != VAR_NUMBER)
780 			  || (type2->tt_type != VAR_ANY
781 					      && type2->tt_type != VAR_NUMBER))
782 		  {
783 		      emsg(_(e_percent_requires_number_arguments));
784 		      return FAIL;
785 		  }
786 		  isn = generate_instr_drop(cctx,
787 			      vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
788 		  if (isn != NULL)
789 		      isn->isn_arg.op.op_type = EXPR_REM;
790 		  break;
791     }
792 
793     // correct type of result
794     if (vartype == VAR_ANY)
795     {
796 	type_T *type = &t_any;
797 
798 #ifdef FEAT_FLOAT
799 	// float+number and number+float results in float
800 	if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
801 		&& (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
802 	    type = &t_float;
803 #endif
804 	((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
805     }
806 
807     return OK;
808 }
809 
810 /*
811  * Get the instruction to use for comparing "type1" with "type2"
812  * Return ISN_DROP when failed.
813  */
814     static isntype_T
815 get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
816 {
817     isntype_T	isntype = ISN_DROP;
818 
819     if (type1 == VAR_UNKNOWN)
820 	type1 = VAR_ANY;
821     if (type2 == VAR_UNKNOWN)
822 	type2 = VAR_ANY;
823 
824     if (type1 == type2)
825     {
826 	switch (type1)
827 	{
828 	    case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
829 	    case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
830 	    case VAR_NUMBER: isntype = ISN_COMPARENR; break;
831 	    case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
832 	    case VAR_STRING: isntype = ISN_COMPARESTRING; break;
833 	    case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
834 	    case VAR_LIST: isntype = ISN_COMPARELIST; break;
835 	    case VAR_DICT: isntype = ISN_COMPAREDICT; break;
836 	    case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
837 	    default: isntype = ISN_COMPAREANY; break;
838 	}
839     }
840     else if (type1 == VAR_ANY || type2 == VAR_ANY
841 	    || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
842 	      && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
843 	isntype = ISN_COMPAREANY;
844 
845     if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
846 	    && (isntype == ISN_COMPAREBOOL
847 	    || isntype == ISN_COMPARESPECIAL
848 	    || isntype == ISN_COMPARENR
849 	    || isntype == ISN_COMPAREFLOAT))
850     {
851 	semsg(_(e_cannot_use_str_with_str),
852 		exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
853 	return ISN_DROP;
854     }
855     if (isntype == ISN_DROP
856 	    || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
857 		    && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
858 		       || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
859 	    || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
860 				 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
861 		    && (type1 == VAR_BLOB || type2 == VAR_BLOB
862 			|| type1 == VAR_LIST || type2 == VAR_LIST))))
863     {
864 	semsg(_(e_cannot_compare_str_with_str),
865 		vartype_name(type1), vartype_name(type2));
866 	return ISN_DROP;
867     }
868     return isntype;
869 }
870 
871     int
872 check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
873 {
874     if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
875 	return FAIL;
876     return OK;
877 }
878 
879 /*
880  * Generate an ISN_COMPARE* instruction with a boolean result.
881  */
882     static int
883 generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
884 {
885     isntype_T	isntype;
886     isn_T	*isn;
887     garray_T	*stack = &cctx->ctx_type_stack;
888     vartype_T	type1;
889     vartype_T	type2;
890 
891     RETURN_OK_IF_SKIP(cctx);
892 
893     // Get the known type of the two items on the stack.  If they are matching
894     // use a type-specific instruction. Otherwise fall back to runtime type
895     // checking.
896     type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
897     type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
898     isntype = get_compare_isn(exprtype, type1, type2);
899     if (isntype == ISN_DROP)
900 	return FAIL;
901 
902     if ((isn = generate_instr(cctx, isntype)) == NULL)
903 	return FAIL;
904     isn->isn_arg.op.op_type = exprtype;
905     isn->isn_arg.op.op_ic = ic;
906 
907     // takes two arguments, puts one bool back
908     if (stack->ga_len >= 2)
909     {
910 	--stack->ga_len;
911 	((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
912     }
913 
914     return OK;
915 }
916 
917 /*
918  * Generate an ISN_2BOOL instruction.
919  * "offset" is the offset in the type stack.
920  */
921     static int
922 generate_2BOOL(cctx_T *cctx, int invert, int offset)
923 {
924     isn_T	*isn;
925     garray_T	*stack = &cctx->ctx_type_stack;
926 
927     RETURN_OK_IF_SKIP(cctx);
928     if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
929 	return FAIL;
930     isn->isn_arg.tobool.invert = invert;
931     isn->isn_arg.tobool.offset = offset;
932 
933     // type becomes bool
934     ((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
935 
936     return OK;
937 }
938 
939 /*
940  * Generate an ISN_COND2BOOL instruction.
941  */
942     static int
943 generate_COND2BOOL(cctx_T *cctx)
944 {
945     isn_T	*isn;
946     garray_T	*stack = &cctx->ctx_type_stack;
947 
948     RETURN_OK_IF_SKIP(cctx);
949     if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
950 	return FAIL;
951 
952     // type becomes bool
953     ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
954 
955     return OK;
956 }
957 
958     static int
959 generate_TYPECHECK(
960 	cctx_T	    *cctx,
961 	type_T	    *expected,
962 	int	    offset,
963 	int	    argidx)
964 {
965     isn_T	*isn;
966     garray_T	*stack = &cctx->ctx_type_stack;
967 
968     RETURN_OK_IF_SKIP(cctx);
969     if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
970 	return FAIL;
971     isn->isn_arg.type.ct_type = alloc_type(expected);
972     isn->isn_arg.type.ct_off = (int8_T)offset;
973     isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
974 
975     // type becomes expected
976     ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
977 
978     return OK;
979 }
980 
981     static int
982 generate_SETTYPE(
983 	cctx_T	    *cctx,
984 	type_T	    *expected)
985 {
986     isn_T	*isn;
987 
988     RETURN_OK_IF_SKIP(cctx);
989     if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
990 	return FAIL;
991     isn->isn_arg.type.ct_type = alloc_type(expected);
992     return OK;
993 }
994 
995 /*
996  * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
997  * used.  Return FALSE if the types will never match.
998  */
999     int
1000 use_typecheck(type_T *actual, type_T *expected)
1001 {
1002     if (actual->tt_type == VAR_ANY
1003 	    || actual->tt_type == VAR_UNKNOWN
1004 	    || (actual->tt_type == VAR_FUNC
1005 		&& (expected->tt_type == VAR_FUNC
1006 					   || expected->tt_type == VAR_PARTIAL)
1007 		&& (actual->tt_member == &t_any || actual->tt_argcount < 0)
1008 		&& ((actual->tt_member == &t_void)
1009 					 == (expected->tt_member == &t_void))))
1010 	return TRUE;
1011     if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
1012 				       && actual->tt_type == expected->tt_type)
1013 	// This takes care of a nested list or dict.
1014 	return use_typecheck(actual->tt_member, expected->tt_member);
1015     return FALSE;
1016 }
1017 
1018 /*
1019  * Check that
1020  * - "actual" matches "expected" type or
1021  * - "actual" is a type that can be "expected" type: add a runtime check; or
1022  * - return FAIL.
1023  * If "actual_is_const" is TRUE then the type won't change at runtime, do not
1024  * generate a TYPECHECK.
1025  */
1026     int
1027 need_type(
1028 	type_T	*actual,
1029 	type_T	*expected,
1030 	int	offset,
1031 	int	arg_idx,
1032 	cctx_T	*cctx,
1033 	int	silent,
1034 	int	actual_is_const)
1035 {
1036     where_T where;
1037 
1038     if (expected == &t_bool && actual != &t_bool
1039 					&& (actual->tt_flags & TTFLAG_BOOL_OK))
1040     {
1041 	// Using "0", "1" or the result of an expression with "&&" or "||" as a
1042 	// boolean is OK but requires a conversion.
1043 	generate_2BOOL(cctx, FALSE, offset);
1044 	return OK;
1045     }
1046 
1047     where.wt_index = arg_idx;
1048     where.wt_variable = FALSE;
1049     if (check_type(expected, actual, FALSE, where) == OK)
1050 	return OK;
1051 
1052     // If the actual type can be the expected type add a runtime check.
1053     // If it's a constant a runtime check makes no sense.
1054     if (!actual_is_const && use_typecheck(actual, expected))
1055     {
1056 	generate_TYPECHECK(cctx, expected, offset, arg_idx);
1057 	return OK;
1058     }
1059 
1060     if (!silent)
1061 	arg_type_mismatch(expected, actual, arg_idx);
1062     return FAIL;
1063 }
1064 
1065 /*
1066  * Check that the top of the type stack has a type that can be used as a
1067  * condition.  Give an error and return FAIL if not.
1068  */
1069     static int
1070 bool_on_stack(cctx_T *cctx)
1071 {
1072     garray_T	*stack = &cctx->ctx_type_stack;
1073     type_T	*type;
1074 
1075     type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1076     if (type == &t_bool)
1077 	return OK;
1078 
1079     if (type == &t_any || type == &t_number || type == &t_number_bool)
1080 	// Number 0 and 1 are OK to use as a bool.  "any" could also be a bool.
1081 	// This requires a runtime type check.
1082 	return generate_COND2BOOL(cctx);
1083 
1084     return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
1085 }
1086 
1087 /*
1088  * Generate an ISN_PUSHNR instruction.
1089  */
1090     static int
1091 generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1092 {
1093     isn_T	*isn;
1094     garray_T	*stack = &cctx->ctx_type_stack;
1095 
1096     RETURN_OK_IF_SKIP(cctx);
1097     if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1098 	return FAIL;
1099     isn->isn_arg.number = number;
1100 
1101     if (number == 0 || number == 1)
1102 	// A 0 or 1 number can also be used as a bool.
1103 	((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
1104     return OK;
1105 }
1106 
1107 /*
1108  * Generate an ISN_PUSHBOOL instruction.
1109  */
1110     static int
1111 generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1112 {
1113     isn_T	*isn;
1114 
1115     RETURN_OK_IF_SKIP(cctx);
1116     if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1117 	return FAIL;
1118     isn->isn_arg.number = number;
1119 
1120     return OK;
1121 }
1122 
1123 /*
1124  * Generate an ISN_PUSHSPEC instruction.
1125  */
1126     static int
1127 generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1128 {
1129     isn_T	*isn;
1130 
1131     RETURN_OK_IF_SKIP(cctx);
1132     if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1133 	return FAIL;
1134     isn->isn_arg.number = number;
1135 
1136     return OK;
1137 }
1138 
1139 #ifdef FEAT_FLOAT
1140 /*
1141  * Generate an ISN_PUSHF instruction.
1142  */
1143     static int
1144 generate_PUSHF(cctx_T *cctx, float_T fnumber)
1145 {
1146     isn_T	*isn;
1147 
1148     RETURN_OK_IF_SKIP(cctx);
1149     if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1150 	return FAIL;
1151     isn->isn_arg.fnumber = fnumber;
1152 
1153     return OK;
1154 }
1155 #endif
1156 
1157 /*
1158  * Generate an ISN_PUSHS instruction.
1159  * Consumes "str".
1160  */
1161     static int
1162 generate_PUSHS(cctx_T *cctx, char_u *str)
1163 {
1164     isn_T	*isn;
1165 
1166     if (cctx->ctx_skip == SKIP_YES)
1167     {
1168 	vim_free(str);
1169 	return OK;
1170     }
1171     if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1172 	return FAIL;
1173     isn->isn_arg.string = str;
1174 
1175     return OK;
1176 }
1177 
1178 /*
1179  * Generate an ISN_PUSHCHANNEL instruction.
1180  * Consumes "channel".
1181  */
1182     static int
1183 generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1184 {
1185     isn_T	*isn;
1186 
1187     RETURN_OK_IF_SKIP(cctx);
1188     if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1189 	return FAIL;
1190     isn->isn_arg.channel = channel;
1191 
1192     return OK;
1193 }
1194 
1195 /*
1196  * Generate an ISN_PUSHJOB instruction.
1197  * Consumes "job".
1198  */
1199     static int
1200 generate_PUSHJOB(cctx_T *cctx, job_T *job)
1201 {
1202     isn_T	*isn;
1203 
1204     RETURN_OK_IF_SKIP(cctx);
1205     if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
1206 	return FAIL;
1207     isn->isn_arg.job = job;
1208 
1209     return OK;
1210 }
1211 
1212 /*
1213  * Generate an ISN_PUSHBLOB instruction.
1214  * Consumes "blob".
1215  */
1216     static int
1217 generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1218 {
1219     isn_T	*isn;
1220 
1221     RETURN_OK_IF_SKIP(cctx);
1222     if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1223 	return FAIL;
1224     isn->isn_arg.blob = blob;
1225 
1226     return OK;
1227 }
1228 
1229 /*
1230  * Generate an ISN_PUSHFUNC instruction with name "name".
1231  * Consumes "name".
1232  */
1233     static int
1234 generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
1235 {
1236     isn_T	*isn;
1237 
1238     RETURN_OK_IF_SKIP(cctx);
1239     if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
1240 	return FAIL;
1241     isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
1242 
1243     return OK;
1244 }
1245 
1246 /*
1247  * Generate an ISN_GETITEM instruction with "index".
1248  * "with_op" is TRUE for "+=" and other operators, the stack has the current
1249  * value below the list with values.
1250  */
1251     static int
1252 generate_GETITEM(cctx_T *cctx, int index, int with_op)
1253 {
1254     isn_T	*isn;
1255     garray_T	*stack = &cctx->ctx_type_stack;
1256     type_T	*type = ((type_T **)stack->ga_data)[stack->ga_len
1257 							  - (with_op ? 2 : 1)];
1258     type_T	*item_type = &t_any;
1259 
1260     RETURN_OK_IF_SKIP(cctx);
1261 
1262     if (type->tt_type != VAR_LIST)
1263     {
1264 	// cannot happen, caller has checked the type
1265 	emsg(_(e_listreq));
1266 	return FAIL;
1267     }
1268     item_type = type->tt_member;
1269     if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1270 	return FAIL;
1271     isn->isn_arg.getitem.gi_index = index;
1272     isn->isn_arg.getitem.gi_with_op = with_op;
1273 
1274     // add the item type to the type stack
1275     if (ga_grow(stack, 1) == FAIL)
1276 	return FAIL;
1277     ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1278     ++stack->ga_len;
1279     return OK;
1280 }
1281 
1282 /*
1283  * Generate an ISN_SLICE instruction with "count".
1284  */
1285     static int
1286 generate_SLICE(cctx_T *cctx, int count)
1287 {
1288     isn_T	*isn;
1289 
1290     RETURN_OK_IF_SKIP(cctx);
1291     if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1292 	return FAIL;
1293     isn->isn_arg.number = count;
1294     return OK;
1295 }
1296 
1297 /*
1298  * Generate an ISN_CHECKLEN instruction with "min_len".
1299  */
1300     static int
1301 generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1302 {
1303     isn_T	*isn;
1304 
1305     RETURN_OK_IF_SKIP(cctx);
1306 
1307     if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1308 	return FAIL;
1309     isn->isn_arg.checklen.cl_min_len = min_len;
1310     isn->isn_arg.checklen.cl_more_OK = more_OK;
1311 
1312     return OK;
1313 }
1314 
1315 /*
1316  * Generate an ISN_STORE instruction.
1317  */
1318     static int
1319 generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1320 {
1321     isn_T	*isn;
1322 
1323     RETURN_OK_IF_SKIP(cctx);
1324     if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1325 	return FAIL;
1326     if (name != NULL)
1327 	isn->isn_arg.string = vim_strsave(name);
1328     else
1329 	isn->isn_arg.number = idx;
1330 
1331     return OK;
1332 }
1333 
1334 /*
1335  * Generate an ISN_STOREOUTER instruction.
1336  */
1337     static int
1338 generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1339 {
1340     isn_T	*isn;
1341 
1342     RETURN_OK_IF_SKIP(cctx);
1343     if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1344 	return FAIL;
1345     isn->isn_arg.outer.outer_idx = idx;
1346     isn->isn_arg.outer.outer_depth = level;
1347 
1348     return OK;
1349 }
1350 
1351 /*
1352  * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1353  */
1354     static int
1355 generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1356 {
1357     isn_T	*isn;
1358 
1359     RETURN_OK_IF_SKIP(cctx);
1360     if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1361 	return FAIL;
1362     isn->isn_arg.storenr.stnr_idx = idx;
1363     isn->isn_arg.storenr.stnr_val = value;
1364 
1365     return OK;
1366 }
1367 
1368 /*
1369  * Generate an ISN_STOREOPT instruction
1370  */
1371     static int
1372 generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1373 {
1374     isn_T	*isn;
1375 
1376     RETURN_OK_IF_SKIP(cctx);
1377     if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
1378 	return FAIL;
1379     isn->isn_arg.storeopt.so_name = vim_strsave(name);
1380     isn->isn_arg.storeopt.so_flags = opt_flags;
1381 
1382     return OK;
1383 }
1384 
1385 /*
1386  * Generate an ISN_LOAD or similar instruction.
1387  */
1388     static int
1389 generate_LOAD(
1390 	cctx_T	    *cctx,
1391 	isntype_T   isn_type,
1392 	int	    idx,
1393 	char_u	    *name,
1394 	type_T	    *type)
1395 {
1396     isn_T	*isn;
1397 
1398     RETURN_OK_IF_SKIP(cctx);
1399     if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1400 	return FAIL;
1401     if (name != NULL)
1402 	isn->isn_arg.string = vim_strsave(name);
1403     else
1404 	isn->isn_arg.number = idx;
1405 
1406     return OK;
1407 }
1408 
1409 /*
1410  * Generate an ISN_LOADOUTER instruction
1411  */
1412     static int
1413 generate_LOADOUTER(
1414 	cctx_T	    *cctx,
1415 	int	    idx,
1416 	int	    nesting,
1417 	type_T	    *type)
1418 {
1419     isn_T	*isn;
1420 
1421     RETURN_OK_IF_SKIP(cctx);
1422     if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1423 	return FAIL;
1424     isn->isn_arg.outer.outer_idx = idx;
1425     isn->isn_arg.outer.outer_depth = nesting;
1426 
1427     return OK;
1428 }
1429 
1430 /*
1431  * Generate an ISN_LOADV instruction for v:var.
1432  */
1433     static int
1434 generate_LOADV(
1435 	cctx_T	    *cctx,
1436 	char_u	    *name,
1437 	int	    error)
1438 {
1439     int	    di_flags;
1440     int	    vidx = find_vim_var(name, &di_flags);
1441     type_T  *type;
1442 
1443     RETURN_OK_IF_SKIP(cctx);
1444     if (vidx < 0)
1445     {
1446 	if (error)
1447 	    semsg(_(e_variable_not_found_str), name);
1448 	return FAIL;
1449     }
1450     type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
1451 
1452     return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1453 }
1454 
1455 /*
1456  * Generate an ISN_UNLET instruction.
1457  */
1458     static int
1459 generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1460 {
1461     isn_T	*isn;
1462 
1463     RETURN_OK_IF_SKIP(cctx);
1464     if ((isn = generate_instr(cctx, isn_type)) == NULL)
1465 	return FAIL;
1466     isn->isn_arg.unlet.ul_name = vim_strsave(name);
1467     isn->isn_arg.unlet.ul_forceit = forceit;
1468 
1469     return OK;
1470 }
1471 
1472 /*
1473  * Generate an ISN_LOCKCONST instruction.
1474  */
1475     static int
1476 generate_LOCKCONST(cctx_T *cctx)
1477 {
1478     isn_T	*isn;
1479 
1480     RETURN_OK_IF_SKIP(cctx);
1481     if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1482 	return FAIL;
1483     return OK;
1484 }
1485 
1486 /*
1487  * Generate an ISN_LOADS instruction.
1488  */
1489     static int
1490 generate_OLDSCRIPT(
1491 	cctx_T	    *cctx,
1492 	isntype_T   isn_type,
1493 	char_u	    *name,
1494 	int	    sid,
1495 	type_T	    *type)
1496 {
1497     isn_T	*isn;
1498 
1499     RETURN_OK_IF_SKIP(cctx);
1500     if (isn_type == ISN_LOADS)
1501 	isn = generate_instr_type(cctx, isn_type, type);
1502     else
1503 	isn = generate_instr_drop(cctx, isn_type, 1);
1504     if (isn == NULL)
1505 	return FAIL;
1506     isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1507     isn->isn_arg.loadstore.ls_sid = sid;
1508 
1509     return OK;
1510 }
1511 
1512 /*
1513  * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1514  */
1515     static int
1516 generate_VIM9SCRIPT(
1517 	cctx_T	    *cctx,
1518 	isntype_T   isn_type,
1519 	int	    sid,
1520 	int	    idx,
1521 	type_T	    *type)
1522 {
1523     isn_T	*isn;
1524     scriptref_T	*sref;
1525     scriptitem_T *si = SCRIPT_ITEM(sid);
1526 
1527     RETURN_OK_IF_SKIP(cctx);
1528     if (isn_type == ISN_LOADSCRIPT)
1529 	isn = generate_instr_type(cctx, isn_type, type);
1530     else
1531 	isn = generate_instr_drop(cctx, isn_type, 1);
1532     if (isn == NULL)
1533 	return FAIL;
1534 
1535     // This requires three arguments, which doesn't fit in an instruction, thus
1536     // we need to allocate a struct for this.
1537     sref = ALLOC_ONE(scriptref_T);
1538     if (sref == NULL)
1539 	return FAIL;
1540     isn->isn_arg.script.scriptref = sref;
1541     sref->sref_sid = sid;
1542     sref->sref_idx = idx;
1543     sref->sref_seq = si->sn_script_seq;
1544     sref->sref_type = type;
1545     return OK;
1546 }
1547 
1548 /*
1549  * Generate an ISN_NEWLIST instruction.
1550  */
1551     static int
1552 generate_NEWLIST(cctx_T *cctx, int count)
1553 {
1554     isn_T	*isn;
1555     garray_T	*stack = &cctx->ctx_type_stack;
1556     type_T	*type;
1557     type_T	*member;
1558 
1559     RETURN_OK_IF_SKIP(cctx);
1560     if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1561 	return FAIL;
1562     isn->isn_arg.number = count;
1563 
1564     // get the member type from all the items on the stack.
1565     if (count == 0)
1566 	member = &t_void;
1567     else
1568 	member = get_member_type_from_stack(
1569 	    ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1570 							  cctx->ctx_type_list);
1571     type = get_list_type(member, cctx->ctx_type_list);
1572 
1573     // drop the value types
1574     stack->ga_len -= count;
1575 
1576     // add the list type to the type stack
1577     if (ga_grow(stack, 1) == FAIL)
1578 	return FAIL;
1579     ((type_T **)stack->ga_data)[stack->ga_len] = type;
1580     ++stack->ga_len;
1581 
1582     return OK;
1583 }
1584 
1585 /*
1586  * Generate an ISN_NEWDICT instruction.
1587  */
1588     static int
1589 generate_NEWDICT(cctx_T *cctx, int count)
1590 {
1591     isn_T	*isn;
1592     garray_T	*stack = &cctx->ctx_type_stack;
1593     type_T	*type;
1594     type_T	*member;
1595 
1596     RETURN_OK_IF_SKIP(cctx);
1597     if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1598 	return FAIL;
1599     isn->isn_arg.number = count;
1600 
1601     if (count == 0)
1602 	member = &t_void;
1603     else
1604 	member = get_member_type_from_stack(
1605 	    ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1606 							  cctx->ctx_type_list);
1607     type = get_dict_type(member, cctx->ctx_type_list);
1608 
1609     // drop the key and value types
1610     stack->ga_len -= 2 * count;
1611 
1612     // add the dict type to the type stack
1613     if (ga_grow(stack, 1) == FAIL)
1614 	return FAIL;
1615     ((type_T **)stack->ga_data)[stack->ga_len] = type;
1616     ++stack->ga_len;
1617 
1618     return OK;
1619 }
1620 
1621 /*
1622  * Generate an ISN_FUNCREF instruction.
1623  */
1624     static int
1625 generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
1626 {
1627     isn_T	*isn;
1628     garray_T	*stack = &cctx->ctx_type_stack;
1629 
1630     RETURN_OK_IF_SKIP(cctx);
1631     if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1632 	return FAIL;
1633     isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
1634     cctx->ctx_has_closure = 1;
1635 
1636     // If the referenced function is a closure, it may use items further up in
1637     // the nested context, including this one.
1638     if (ufunc->uf_flags & FC_CLOSURE)
1639 	cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1640 
1641     if (ga_grow(stack, 1) == FAIL)
1642 	return FAIL;
1643     ((type_T **)stack->ga_data)[stack->ga_len] =
1644 	       ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1645     ++stack->ga_len;
1646 
1647     return OK;
1648 }
1649 
1650 /*
1651  * Generate an ISN_NEWFUNC instruction.
1652  * "lambda_name" and "func_name" must be in allocated memory and will be
1653  * consumed.
1654  */
1655     static int
1656 generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1657 {
1658     isn_T	*isn;
1659 
1660     if (cctx->ctx_skip == SKIP_YES)
1661     {
1662 	vim_free(lambda_name);
1663 	vim_free(func_name);
1664 	return OK;
1665     }
1666     if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1667     {
1668 	vim_free(lambda_name);
1669 	vim_free(func_name);
1670 	return FAIL;
1671     }
1672     isn->isn_arg.newfunc.nf_lambda = lambda_name;
1673     isn->isn_arg.newfunc.nf_global = func_name;
1674 
1675     return OK;
1676 }
1677 
1678 /*
1679  * Generate an ISN_DEF instruction: list functions
1680  */
1681     static int
1682 generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1683 {
1684     isn_T	*isn;
1685 
1686     RETURN_OK_IF_SKIP(cctx);
1687     if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1688 	return FAIL;
1689     if (len > 0)
1690     {
1691 	isn->isn_arg.string = vim_strnsave(name, len);
1692 	if (isn->isn_arg.string == NULL)
1693 	    return FAIL;
1694     }
1695     return OK;
1696 }
1697 
1698 /*
1699  * Generate an ISN_JUMP instruction.
1700  */
1701     static int
1702 generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1703 {
1704     isn_T	*isn;
1705     garray_T	*stack = &cctx->ctx_type_stack;
1706 
1707     RETURN_OK_IF_SKIP(cctx);
1708     if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1709 	return FAIL;
1710     isn->isn_arg.jump.jump_when = when;
1711     isn->isn_arg.jump.jump_where = where;
1712 
1713     if (when != JUMP_ALWAYS && stack->ga_len > 0)
1714 	--stack->ga_len;
1715 
1716     return OK;
1717 }
1718 
1719 /*
1720  * Generate an ISN_JUMP_IF_ARG_SET instruction.
1721  */
1722     static int
1723 generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1724 {
1725     isn_T	*isn;
1726 
1727     RETURN_OK_IF_SKIP(cctx);
1728     if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1729 	return FAIL;
1730     isn->isn_arg.jumparg.jump_arg_off = arg_off;
1731     // jump_where is set later
1732     return OK;
1733 }
1734 
1735     static int
1736 generate_FOR(cctx_T *cctx, int loop_idx)
1737 {
1738     isn_T	*isn;
1739     garray_T	*stack = &cctx->ctx_type_stack;
1740 
1741     RETURN_OK_IF_SKIP(cctx);
1742     if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1743 	return FAIL;
1744     isn->isn_arg.forloop.for_idx = loop_idx;
1745 
1746     if (ga_grow(stack, 1) == FAIL)
1747 	return FAIL;
1748     // type doesn't matter, will be stored next
1749     ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1750     ++stack->ga_len;
1751 
1752     return OK;
1753 }
1754 /*
1755  * Generate an ISN_TRYCONT instruction.
1756  */
1757     static int
1758 generate_TRYCONT(cctx_T *cctx, int levels, int where)
1759 {
1760     isn_T	*isn;
1761 
1762     RETURN_OK_IF_SKIP(cctx);
1763     if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1764 	return FAIL;
1765     isn->isn_arg.trycont.tct_levels = levels;
1766     isn->isn_arg.trycont.tct_where = where;
1767 
1768     return OK;
1769 }
1770 
1771 
1772 /*
1773  * Generate an ISN_BCALL instruction.
1774  * "method_call" is TRUE for "value->method()"
1775  * Return FAIL if the number of arguments is wrong.
1776  */
1777     static int
1778 generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1779 {
1780     isn_T	*isn;
1781     garray_T	*stack = &cctx->ctx_type_stack;
1782     int		argoff;
1783     type_T	**argtypes = NULL;
1784     type_T	*maptype = NULL;
1785 
1786     RETURN_OK_IF_SKIP(cctx);
1787     argoff = check_internal_func(func_idx, argcount);
1788     if (argoff < 0)
1789 	return FAIL;
1790 
1791     if (method_call && argoff > 1)
1792     {
1793 	if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1794 	    return FAIL;
1795 	isn->isn_arg.shuffle.shfl_item = argcount;
1796 	isn->isn_arg.shuffle.shfl_up = argoff - 1;
1797     }
1798 
1799     if (argcount > 0)
1800     {
1801 	// Check the types of the arguments.
1802 	argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1803 	if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1804 								 cctx) == FAIL)
1805 	    return FAIL;
1806 	if (internal_func_is_map(func_idx))
1807 	    maptype = *argtypes;
1808     }
1809 
1810     if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1811 	return FAIL;
1812     isn->isn_arg.bfunc.cbf_idx = func_idx;
1813     isn->isn_arg.bfunc.cbf_argcount = argcount;
1814 
1815     // Drop the argument types and push the return type.
1816     stack->ga_len -= argcount;
1817     if (ga_grow(stack, 1) == FAIL)
1818 	return FAIL;
1819     ((type_T **)stack->ga_data)[stack->ga_len] =
1820 			  internal_func_ret_type(func_idx, argcount, argtypes);
1821     ++stack->ga_len;
1822 
1823     if (maptype != NULL && maptype->tt_member != NULL
1824 					       && maptype->tt_member != &t_any)
1825 	// Check that map() didn't change the item types.
1826 	generate_TYPECHECK(cctx, maptype, -1, 1);
1827 
1828     return OK;
1829 }
1830 
1831 /*
1832  * Generate an ISN_LISTAPPEND instruction.  Works like add().
1833  * Argument count is already checked.
1834  */
1835     static int
1836 generate_LISTAPPEND(cctx_T *cctx)
1837 {
1838     garray_T	*stack = &cctx->ctx_type_stack;
1839     type_T	*list_type;
1840     type_T	*item_type;
1841     type_T	*expected;
1842 
1843     // Caller already checked that list_type is a list.
1844     list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1845     item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1846     expected = list_type->tt_member;
1847     if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1848 	return FAIL;
1849 
1850     if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1851 	return FAIL;
1852 
1853     --stack->ga_len;	    // drop the argument
1854     return OK;
1855 }
1856 
1857 /*
1858  * Generate an ISN_BLOBAPPEND instruction.  Works like add().
1859  * Argument count is already checked.
1860  */
1861     static int
1862 generate_BLOBAPPEND(cctx_T *cctx)
1863 {
1864     garray_T	*stack = &cctx->ctx_type_stack;
1865     type_T	*item_type;
1866 
1867     // Caller already checked that blob_type is a blob.
1868     item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1869     if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1870 	return FAIL;
1871 
1872     if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1873 	return FAIL;
1874 
1875     --stack->ga_len;	    // drop the argument
1876     return OK;
1877 }
1878 
1879 /*
1880  * Return TRUE if "ufunc" should be compiled, taking into account whether
1881  * "profile" indicates profiling is to be done.
1882  */
1883     int
1884 func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
1885 {
1886     switch (ufunc->uf_def_status)
1887     {
1888 	case UF_TO_BE_COMPILED:
1889 	    return TRUE;
1890 
1891 	case UF_COMPILED:
1892 	{
1893 	    dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1894 							 + ufunc->uf_dfunc_idx;
1895 
1896 	    switch (compile_type)
1897 	    {
1898 		case CT_PROFILE:
1899 #ifdef FEAT_PROFILE
1900 		    return dfunc->df_instr_prof == NULL;
1901 #endif
1902 		case CT_NONE:
1903 		    return dfunc->df_instr == NULL;
1904 		case CT_DEBUG:
1905 		    return dfunc->df_instr_debug == NULL;
1906 	    }
1907 	}
1908 
1909 	case UF_NOT_COMPILED:
1910 	case UF_COMPILE_ERROR:
1911 	case UF_COMPILING:
1912 	    break;
1913     }
1914     return FALSE;
1915 }
1916 
1917 /*
1918  * Generate an ISN_DCALL or ISN_UCALL instruction.
1919  * Return FAIL if the number of arguments is wrong.
1920  */
1921     static int
1922 generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1923 {
1924     isn_T	*isn;
1925     garray_T	*stack = &cctx->ctx_type_stack;
1926     int		regular_args = ufunc->uf_args.ga_len;
1927     int		argcount = pushed_argcount;
1928 
1929     RETURN_OK_IF_SKIP(cctx);
1930     if (argcount > regular_args && !has_varargs(ufunc))
1931     {
1932 	semsg(_(e_toomanyarg), printable_func_name(ufunc));
1933 	return FAIL;
1934     }
1935     if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1936     {
1937 	semsg(_(e_toofewarg), printable_func_name(ufunc));
1938 	return FAIL;
1939     }
1940 
1941     if (ufunc->uf_def_status != UF_NOT_COMPILED
1942 	    && ufunc->uf_def_status != UF_COMPILE_ERROR)
1943     {
1944 	int		i;
1945 
1946 	for (i = 0; i < argcount; ++i)
1947 	{
1948 	    type_T *expected;
1949 	    type_T *actual;
1950 
1951 	    actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1952 	    if (actual == &t_special
1953 			      && i >= regular_args - ufunc->uf_def_args.ga_len)
1954 	    {
1955 		// assume v:none used for default argument value
1956 		continue;
1957 	    }
1958 	    if (i < regular_args)
1959 	    {
1960 		if (ufunc->uf_arg_types == NULL)
1961 		    continue;
1962 		expected = ufunc->uf_arg_types[i];
1963 	    }
1964 	    else if (ufunc->uf_va_type == NULL
1965 					   || ufunc->uf_va_type == &t_list_any)
1966 		// possibly a lambda or "...: any"
1967 		expected = &t_any;
1968 	    else
1969 		expected = ufunc->uf_va_type->tt_member;
1970 	    if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1971 							  TRUE, FALSE) == FAIL)
1972 	    {
1973 		arg_type_mismatch(expected, actual, i + 1);
1974 		return FAIL;
1975 	    }
1976 	}
1977 	if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
1978 		&& compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
1979 					    COMPILE_TYPE(ufunc), NULL) == FAIL)
1980 	    return FAIL;
1981     }
1982     if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1983     {
1984 	emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1985 							       ufunc->uf_name);
1986 	return FAIL;
1987     }
1988 
1989     if ((isn = generate_instr(cctx,
1990 		    ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1991 							 : ISN_UCALL)) == NULL)
1992 	return FAIL;
1993     if (isn->isn_type == ISN_DCALL)
1994     {
1995 	isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1996 	isn->isn_arg.dfunc.cdf_argcount = argcount;
1997     }
1998     else
1999     {
2000 	// A user function may be deleted and redefined later, can't use the
2001 	// ufunc pointer, need to look it up again at runtime.
2002 	isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
2003 	isn->isn_arg.ufunc.cuf_argcount = argcount;
2004     }
2005 
2006     stack->ga_len -= argcount; // drop the arguments
2007     if (ga_grow(stack, 1) == FAIL)
2008 	return FAIL;
2009     // add return value
2010     ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
2011     ++stack->ga_len;
2012 
2013     return OK;
2014 }
2015 
2016 /*
2017  * Generate an ISN_UCALL instruction when the function isn't defined yet.
2018  */
2019     static int
2020 generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
2021 {
2022     isn_T	*isn;
2023     garray_T	*stack = &cctx->ctx_type_stack;
2024 
2025     RETURN_OK_IF_SKIP(cctx);
2026     if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
2027 	return FAIL;
2028     isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
2029     isn->isn_arg.ufunc.cuf_argcount = argcount;
2030 
2031     stack->ga_len -= argcount; // drop the arguments
2032     if (ga_grow(stack, 1) == FAIL)
2033 	return FAIL;
2034     // add return value
2035     ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2036     ++stack->ga_len;
2037 
2038     return OK;
2039 }
2040 
2041 /*
2042  * Generate an ISN_PCALL instruction.
2043  * "type" is the type of the FuncRef.
2044  */
2045     static int
2046 generate_PCALL(
2047 	cctx_T	*cctx,
2048 	int	argcount,
2049 	char_u	*name,
2050 	type_T	*type,
2051 	int	at_top)
2052 {
2053     isn_T	*isn;
2054     garray_T	*stack = &cctx->ctx_type_stack;
2055     type_T	*ret_type;
2056 
2057     RETURN_OK_IF_SKIP(cctx);
2058 
2059     if (type->tt_type == VAR_ANY)
2060 	ret_type = &t_any;
2061     else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
2062     {
2063 	if (type->tt_argcount != -1)
2064 	{
2065 	    int	    varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
2066 
2067 	    if (argcount < type->tt_min_argcount - varargs)
2068 	    {
2069 		semsg(_(e_toofewarg), name);
2070 		return FAIL;
2071 	    }
2072 	    if (!varargs && argcount > type->tt_argcount)
2073 	    {
2074 		semsg(_(e_toomanyarg), name);
2075 		return FAIL;
2076 	    }
2077 	    if (type->tt_args != NULL)
2078 	    {
2079 		int i;
2080 
2081 		for (i = 0; i < argcount; ++i)
2082 		{
2083 		    int	    offset = -argcount + i - (at_top ? 0 : 1);
2084 		    type_T *actual = ((type_T **)stack->ga_data)[
2085 						       stack->ga_len + offset];
2086 		    type_T *expected;
2087 
2088 		    if (varargs && i >= type->tt_argcount - 1)
2089 			expected = type->tt_args[
2090 					     type->tt_argcount - 1]->tt_member;
2091 		    else if (i >= type->tt_min_argcount
2092 						       && actual == &t_special)
2093 			expected = &t_any;
2094 		    else
2095 			expected = type->tt_args[i];
2096 		    if (need_type(actual, expected, offset, i + 1,
2097 						    cctx, TRUE, FALSE) == FAIL)
2098 		    {
2099 			arg_type_mismatch(expected, actual, i + 1);
2100 			return FAIL;
2101 		    }
2102 		}
2103 	    }
2104 	}
2105 	ret_type = type->tt_member;
2106     }
2107     else
2108     {
2109 	semsg(_(e_not_callable_type_str), name);
2110 	return FAIL;
2111     }
2112 
2113     if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2114 	return FAIL;
2115     isn->isn_arg.pfunc.cpf_top = at_top;
2116     isn->isn_arg.pfunc.cpf_argcount = argcount;
2117 
2118     stack->ga_len -= argcount; // drop the arguments
2119 
2120     // drop the funcref/partial, get back the return value
2121     ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
2122 
2123     // If partial is above the arguments it must be cleared and replaced with
2124     // the return value.
2125     if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2126 	return FAIL;
2127 
2128     return OK;
2129 }
2130 
2131 /*
2132  * Generate an ISN_STRINGMEMBER instruction.
2133  */
2134     static int
2135 generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
2136 {
2137     isn_T	*isn;
2138     garray_T	*stack = &cctx->ctx_type_stack;
2139     type_T	*type;
2140 
2141     RETURN_OK_IF_SKIP(cctx);
2142     if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
2143 	return FAIL;
2144     isn->isn_arg.string = vim_strnsave(name, len);
2145 
2146     // check for dict type
2147     type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2148     if (type->tt_type != VAR_DICT && type != &t_any)
2149     {
2150 	emsg(_(e_dictreq));
2151 	return FAIL;
2152     }
2153     // change dict type to dict member type
2154     if (type->tt_type == VAR_DICT)
2155     {
2156 	((type_T **)stack->ga_data)[stack->ga_len - 1] =
2157 		      type->tt_member == &t_unknown ? &t_any : type->tt_member;
2158     }
2159 
2160     return OK;
2161 }
2162 
2163 /*
2164  * Generate an ISN_ECHO instruction.
2165  */
2166     static int
2167 generate_ECHO(cctx_T *cctx, int with_white, int count)
2168 {
2169     isn_T	*isn;
2170 
2171     RETURN_OK_IF_SKIP(cctx);
2172     if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2173 	return FAIL;
2174     isn->isn_arg.echo.echo_with_white = with_white;
2175     isn->isn_arg.echo.echo_count = count;
2176 
2177     return OK;
2178 }
2179 
2180 /*
2181  * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
2182  */
2183     static int
2184 generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
2185 {
2186     isn_T	*isn;
2187 
2188     if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
2189 	return FAIL;
2190     isn->isn_arg.number = count;
2191 
2192     return OK;
2193 }
2194 
2195 /*
2196  * Generate an ISN_PUT instruction.
2197  */
2198     static int
2199 generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2200 {
2201     isn_T	*isn;
2202 
2203     RETURN_OK_IF_SKIP(cctx);
2204     if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2205 	return FAIL;
2206     isn->isn_arg.put.put_regname = regname;
2207     isn->isn_arg.put.put_lnum = lnum;
2208     return OK;
2209 }
2210 
2211     static int
2212 generate_EXEC(cctx_T *cctx, char_u *line)
2213 {
2214     isn_T	*isn;
2215 
2216     RETURN_OK_IF_SKIP(cctx);
2217     if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2218 	return FAIL;
2219     isn->isn_arg.string = vim_strsave(line);
2220     return OK;
2221 }
2222 
2223     static int
2224 generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2225 {
2226     isn_T	*isn;
2227     garray_T	*stack = &cctx->ctx_type_stack;
2228 
2229     RETURN_OK_IF_SKIP(cctx);
2230     if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2231 	return FAIL;
2232     isn->isn_arg.string = vim_strsave(line);
2233 
2234     if (ga_grow(stack, 1) == FAIL)
2235 	return FAIL;
2236     ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
2237     ++stack->ga_len;
2238 
2239     return OK;
2240 }
2241 
2242     static int
2243 generate_EXECCONCAT(cctx_T *cctx, int count)
2244 {
2245     isn_T	*isn;
2246 
2247     if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2248 	return FAIL;
2249     isn->isn_arg.number = count;
2250     return OK;
2251 }
2252 
2253 /*
2254  * Generate ISN_RANGE.  Consumes "range".  Return OK/FAIL.
2255  */
2256     static int
2257 generate_RANGE(cctx_T *cctx, char_u *range)
2258 {
2259     isn_T	*isn;
2260     garray_T	*stack = &cctx->ctx_type_stack;
2261 
2262     if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2263 	return FAIL;
2264     isn->isn_arg.string = range;
2265 
2266     if (ga_grow(stack, 1) == FAIL)
2267 	return FAIL;
2268     ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2269     ++stack->ga_len;
2270     return OK;
2271 }
2272 
2273     static int
2274 generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2275 {
2276     isn_T	*isn;
2277 
2278     RETURN_OK_IF_SKIP(cctx);
2279     if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2280 	return FAIL;
2281     isn->isn_arg.unpack.unp_count = var_count;
2282     isn->isn_arg.unpack.unp_semicolon = semicolon;
2283     return OK;
2284 }
2285 
2286 /*
2287  * Generate an instruction for any command modifiers.
2288  */
2289     static int
2290 generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2291 {
2292     isn_T	*isn;
2293 
2294     if (has_cmdmod(cmod))
2295     {
2296 	cctx->ctx_has_cmdmod = TRUE;
2297 
2298 	if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2299 	    return FAIL;
2300 	isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2301 	if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2302 	    return FAIL;
2303 	mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2304 	// filter program now belongs to the instruction
2305 	cmod->cmod_filter_regmatch.regprog = NULL;
2306     }
2307 
2308     return OK;
2309 }
2310 
2311     static int
2312 generate_undo_cmdmods(cctx_T *cctx)
2313 {
2314     if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2315 	return FAIL;
2316     cctx->ctx_has_cmdmod = FALSE;
2317     return OK;
2318 }
2319 
2320     static int
2321 misplaced_cmdmod(cctx_T *cctx)
2322 {
2323     garray_T	*instr = &cctx->ctx_instr;
2324 
2325     if (cctx->ctx_has_cmdmod
2326 	    && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2327 								 == ISN_CMDMOD)
2328     {
2329 	emsg(_(e_misplaced_command_modifier));
2330 	return TRUE;
2331     }
2332     return FALSE;
2333 }
2334 
2335 /*
2336  * Get the index of the current instruction.
2337  * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2338  */
2339     static int
2340 current_instr_idx(cctx_T *cctx)
2341 {
2342     garray_T	*instr = &cctx->ctx_instr;
2343     int		idx = instr->ga_len;
2344 
2345     while (idx > 0)
2346     {
2347 	if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2348 						       .isn_type == ISN_CMDMOD)
2349 	{
2350 	    --idx;
2351 	    continue;
2352 	}
2353 #ifdef FEAT_PROFILE
2354 	if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
2355 	{
2356 	    --idx;
2357 	    continue;
2358 	}
2359 #endif
2360 	if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
2361 	{
2362 	    --idx;
2363 	    continue;
2364 	}
2365 	break;
2366     }
2367     return idx;
2368 }
2369 
2370 #ifdef FEAT_PROFILE
2371     static void
2372 may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2373 {
2374     if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2375 	generate_instr(cctx, ISN_PROF_END);
2376 }
2377 #endif
2378 
2379 /*
2380  * Reserve space for a local variable.
2381  * Return the variable or NULL if it failed.
2382  */
2383     static lvar_T *
2384 reserve_local(
2385 	cctx_T	*cctx,
2386 	char_u	*name,
2387 	size_t	len,
2388 	int	isConst,
2389 	type_T	*type)
2390 {
2391     lvar_T  *lvar;
2392     dfunc_T *dfunc;
2393 
2394     if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
2395     {
2396 	emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
2397 	return NULL;
2398     }
2399 
2400     if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
2401 	return NULL;
2402     lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
2403     CLEAR_POINTER(lvar);
2404 
2405     // Every local variable uses the next entry on the stack.  We could re-use
2406     // the last ones when leaving a scope, but then variables used in a closure
2407     // might get overwritten.  To keep things simple do not re-use stack
2408     // entries.  This is less efficient, but memory is cheap these days.
2409     dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
2410     lvar->lv_idx = dfunc->df_var_names.ga_len;
2411 
2412     lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
2413     lvar->lv_const = isConst;
2414     lvar->lv_type = type;
2415 
2416     // Remember the name for debugging.
2417     if (ga_grow(&dfunc->df_var_names, 1) == FAIL)
2418 	return NULL;
2419     ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
2420 						    vim_strsave(lvar->lv_name);
2421     ++dfunc->df_var_names.ga_len;
2422 
2423     return lvar;
2424 }
2425 
2426 /*
2427  * Remove local variables above "new_top".
2428  */
2429     static void
2430 unwind_locals(cctx_T *cctx, int new_top)
2431 {
2432     if (cctx->ctx_locals.ga_len > new_top)
2433     {
2434 	int	idx;
2435 	lvar_T	*lvar;
2436 
2437 	for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2438 	{
2439 	    lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2440 	    vim_free(lvar->lv_name);
2441 	}
2442     }
2443     cctx->ctx_locals.ga_len = new_top;
2444 }
2445 
2446 /*
2447  * Free all local variables.
2448  */
2449     static void
2450 free_locals(cctx_T *cctx)
2451 {
2452     unwind_locals(cctx, 0);
2453     ga_clear(&cctx->ctx_locals);
2454 }
2455 
2456 /*
2457  * If "check_writable" is ASSIGN_CONST give an error if the variable was
2458  * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2459  * error if the variable was defined with :const.
2460  */
2461     static int
2462 check_item_writable(svar_T *sv, int check_writable, char_u *name)
2463 {
2464     if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2465 	    || (check_writable == ASSIGN_FINAL
2466 					      && sv->sv_const == ASSIGN_CONST))
2467     {
2468 	semsg(_(e_readonlyvar), name);
2469 	return FAIL;
2470     }
2471     return OK;
2472 }
2473 
2474 /*
2475  * Find "name" in script-local items of script "sid".
2476  * Pass "check_writable" to check_item_writable().
2477  * Returns the index in "sn_var_vals" if found.
2478  * If found but not in "sn_var_vals" returns -1.
2479  * If not found or the variable is not writable returns -2.
2480  */
2481     int
2482 get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
2483 {
2484     hashtab_T	    *ht;
2485     dictitem_T	    *di;
2486     scriptitem_T    *si = SCRIPT_ITEM(sid);
2487     svar_T	    *sv;
2488     int		    idx;
2489 
2490     if (!SCRIPT_ID_VALID(sid))
2491 	return -1;
2492     if (sid == current_sctx.sc_sid)
2493     {
2494 	sallvar_T *sav = find_script_var(name, 0, cctx);
2495 
2496 	if (sav == NULL)
2497 	    return -2;
2498 	idx = sav->sav_var_vals_idx;
2499 	sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2500 	if (check_item_writable(sv, check_writable, name) == FAIL)
2501 	    return -2;
2502 	return idx;
2503     }
2504 
2505     // First look the name up in the hashtable.
2506     ht = &SCRIPT_VARS(sid);
2507     di = find_var_in_ht(ht, 0, name, TRUE);
2508     if (di == NULL)
2509 	return -2;
2510 
2511     // Now find the svar_T index in sn_var_vals.
2512     for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2513     {
2514 	sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2515 	if (sv->sv_tv == &di->di_tv)
2516 	{
2517 	    if (check_item_writable(sv, check_writable, name) == FAIL)
2518 		return -2;
2519 	    return idx;
2520 	}
2521     }
2522     return -1;
2523 }
2524 
2525 /*
2526  * Find "name" in imported items of the current script or in "cctx" if not
2527  * NULL.
2528  */
2529     imported_T *
2530 find_imported(char_u *name, size_t len, cctx_T *cctx)
2531 {
2532     int		    idx;
2533 
2534     if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2535 	return NULL;
2536     if (cctx != NULL)
2537 	for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2538 	{
2539 	    imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2540 									 + idx;
2541 
2542 	    if (len == 0 ? STRCMP(name, import->imp_name) == 0
2543 			 : STRLEN(import->imp_name) == len
2544 				  && STRNCMP(name, import->imp_name, len) == 0)
2545 		return import;
2546 	}
2547 
2548     return find_imported_in_script(name, len, current_sctx.sc_sid);
2549 }
2550 
2551     imported_T *
2552 find_imported_in_script(char_u *name, size_t len, int sid)
2553 {
2554     scriptitem_T    *si;
2555     int		    idx;
2556 
2557     if (!SCRIPT_ID_VALID(sid))
2558 	return NULL;
2559     si = SCRIPT_ITEM(sid);
2560     for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2561     {
2562 	imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2563 
2564 	if (len == 0 ? STRCMP(name, import->imp_name) == 0
2565 		     : STRLEN(import->imp_name) == len
2566 				  && STRNCMP(name, import->imp_name, len) == 0)
2567 	    return import;
2568     }
2569     return NULL;
2570 }
2571 
2572 /*
2573  * Free all imported variables.
2574  */
2575     static void
2576 free_imported(cctx_T *cctx)
2577 {
2578     int idx;
2579 
2580     for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2581     {
2582 	imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2583 
2584 	vim_free(import->imp_name);
2585     }
2586     ga_clear(&cctx->ctx_imports);
2587 }
2588 
2589 /*
2590  * Return a pointer to the next line that isn't empty or only contains a
2591  * comment. Skips over white space.
2592  * Returns NULL if there is none.
2593  */
2594     char_u *
2595 peek_next_line_from_context(cctx_T *cctx)
2596 {
2597     int lnum = cctx->ctx_lnum;
2598 
2599     while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2600     {
2601 	char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
2602 	char_u *p;
2603 
2604 	// ignore NULLs inserted for continuation lines
2605 	if (line != NULL)
2606 	{
2607 	    p = skipwhite(line);
2608 	    if (vim9_bad_comment(p))
2609 		return NULL;
2610 	    if (*p != NUL && !vim9_comment_start(p))
2611 		return p;
2612 	}
2613     }
2614     return NULL;
2615 }
2616 
2617 /*
2618  * Called when checking for a following operator at "arg".  When the rest of
2619  * the line is empty or only a comment, peek the next line.  If there is a next
2620  * line return a pointer to it and set "nextp".
2621  * Otherwise skip over white space.
2622  */
2623     static char_u *
2624 may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2625 {
2626     char_u *p = skipwhite(arg);
2627 
2628     *nextp = NULL;
2629     if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
2630     {
2631 	*nextp = peek_next_line_from_context(cctx);
2632 	if (*nextp != NULL)
2633 	    return *nextp;
2634     }
2635     return p;
2636 }
2637 
2638 /*
2639  * Get the next line of the function from "cctx".
2640  * Skips over empty lines.  Skips over comment lines if "skip_comment" is TRUE.
2641  * Returns NULL when at the end.
2642  */
2643     char_u *
2644 next_line_from_context(cctx_T *cctx, int skip_comment)
2645 {
2646     char_u	*line;
2647 
2648     do
2649     {
2650 	++cctx->ctx_lnum;
2651 	if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
2652 	{
2653 	    line = NULL;
2654 	    break;
2655 	}
2656 	line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
2657 	cctx->ctx_line_start = line;
2658 	SOURCING_LNUM = cctx->ctx_lnum + 1;
2659     } while (line == NULL || *skipwhite(line) == NUL
2660 		     || (skip_comment && vim9_comment_start(skipwhite(line))));
2661     return line;
2662 }
2663 
2664 /*
2665  * Skip over white space at "whitep" and assign to "*arg".
2666  * If "*arg" is at the end of the line, advance to the next line.
2667  * Also when "whitep" points to white space and "*arg" is on a "#".
2668  * Return FAIL if beyond the last line, "*arg" is unmodified then.
2669  */
2670     static int
2671 may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
2672 {
2673     *arg = skipwhite(whitep);
2674     if (vim9_bad_comment(*arg))
2675 	return FAIL;
2676     if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
2677     {
2678 	char_u *next = next_line_from_context(cctx, TRUE);
2679 
2680 	if (next == NULL)
2681 	    return FAIL;
2682 	*arg = skipwhite(next);
2683     }
2684     return OK;
2685 }
2686 
2687 /*
2688  * Idem, and give an error when failed.
2689  */
2690     static int
2691 may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2692 {
2693     if (may_get_next_line(whitep, arg, cctx) == FAIL)
2694     {
2695 	SOURCING_LNUM = cctx->ctx_lnum + 1;
2696 	emsg(_(e_line_incomplete));
2697 	return FAIL;
2698     }
2699     return OK;
2700 }
2701 
2702 
2703 // Structure passed between the compile_expr* functions to keep track of
2704 // constants that have been parsed but for which no code was produced yet.  If
2705 // possible expressions on these constants are applied at compile time.  If
2706 // that is not possible, the code to push the constants needs to be generated
2707 // before other instructions.
2708 // Using 50 should be more than enough of 5 levels of ().
2709 #define PPSIZE 50
2710 typedef struct {
2711     typval_T	pp_tv[PPSIZE];	// stack of ppconst constants
2712     int		pp_used;	// active entries in pp_tv[]
2713     int		pp_is_const;	// all generated code was constants, used for a
2714 				// list or dict with constant members
2715 } ppconst_T;
2716 
2717 static int compile_expr0_ext(char_u **arg,  cctx_T *cctx, int *is_const);
2718 static int compile_expr0(char_u **arg,  cctx_T *cctx);
2719 static int compile_expr1(char_u **arg,  cctx_T *cctx, ppconst_T *ppconst);
2720 
2721 /*
2722  * Generate a PUSH instruction for "tv".
2723  * "tv" will be consumed or cleared.
2724  * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2725  */
2726     static int
2727 generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2728 {
2729     if (tv != NULL)
2730     {
2731 	switch (tv->v_type)
2732 	{
2733 	    case VAR_UNKNOWN:
2734 		break;
2735 	    case VAR_BOOL:
2736 		generate_PUSHBOOL(cctx, tv->vval.v_number);
2737 		break;
2738 	    case VAR_SPECIAL:
2739 		generate_PUSHSPEC(cctx, tv->vval.v_number);
2740 		break;
2741 	    case VAR_NUMBER:
2742 		generate_PUSHNR(cctx, tv->vval.v_number);
2743 		break;
2744 #ifdef FEAT_FLOAT
2745 	    case VAR_FLOAT:
2746 		generate_PUSHF(cctx, tv->vval.v_float);
2747 		break;
2748 #endif
2749 	    case VAR_BLOB:
2750 		generate_PUSHBLOB(cctx, tv->vval.v_blob);
2751 		tv->vval.v_blob = NULL;
2752 		break;
2753 	    case VAR_STRING:
2754 		generate_PUSHS(cctx, tv->vval.v_string);
2755 		tv->vval.v_string = NULL;
2756 		break;
2757 	    default:
2758 		iemsg("constant type not supported");
2759 		clear_tv(tv);
2760 		return FAIL;
2761 	}
2762 	tv->v_type = VAR_UNKNOWN;
2763     }
2764     return OK;
2765 }
2766 
2767 /*
2768  * Generate code for any ppconst entries.
2769  */
2770     static int
2771 generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2772 {
2773     int	    i;
2774     int	    ret = OK;
2775     int	    save_skip = cctx->ctx_skip;
2776 
2777     cctx->ctx_skip = SKIP_NOT;
2778     for (i = 0; i < ppconst->pp_used; ++i)
2779 	if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2780 	    ret = FAIL;
2781     ppconst->pp_used = 0;
2782     cctx->ctx_skip = save_skip;
2783     return ret;
2784 }
2785 
2786 /*
2787  * Clear ppconst constants.  Used when failing.
2788  */
2789     static void
2790 clear_ppconst(ppconst_T *ppconst)
2791 {
2792     int	    i;
2793 
2794     for (i = 0; i < ppconst->pp_used; ++i)
2795 	clear_tv(&ppconst->pp_tv[i]);
2796     ppconst->pp_used = 0;
2797 }
2798 
2799 /*
2800  * Compile getting a member from a list/dict/string/blob.  Stack has the
2801  * indexable value and the index or the two indexes of a slice.
2802  */
2803     static int
2804 compile_member(int is_slice, cctx_T *cctx)
2805 {
2806     type_T	**typep;
2807     garray_T	*stack = &cctx->ctx_type_stack;
2808     vartype_T	vartype;
2809     type_T	*idxtype;
2810 
2811     // We can index a list, dict and blob.  If we don't know the type
2812     // we can use the index value type.  If we still don't know use an "ANY"
2813     // instruction.
2814     typep = ((type_T **)stack->ga_data) + stack->ga_len
2815 						  - (is_slice ? 3 : 2);
2816     vartype = (*typep)->tt_type;
2817     idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2818     // If the index is a string, the variable must be a Dict.
2819     if (*typep == &t_any && idxtype == &t_string)
2820 	vartype = VAR_DICT;
2821     if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
2822     {
2823 	if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
2824 	    return FAIL;
2825 	if (is_slice)
2826 	{
2827 	    idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2828 	    if (need_type(idxtype, &t_number, -2, 0, cctx,
2829 							 FALSE, FALSE) == FAIL)
2830 		return FAIL;
2831 	}
2832     }
2833 
2834     if (vartype == VAR_DICT)
2835     {
2836 	if (is_slice)
2837 	{
2838 	    emsg(_(e_cannot_slice_dictionary));
2839 	    return FAIL;
2840 	}
2841 	if ((*typep)->tt_type == VAR_DICT)
2842 	{
2843 	    *typep = (*typep)->tt_member;
2844 	    if (*typep == &t_unknown)
2845 		// empty dict was used
2846 		*typep = &t_any;
2847 	}
2848 	else
2849 	{
2850 	    if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2851 							 FALSE, FALSE) == FAIL)
2852 		return FAIL;
2853 	    *typep = &t_any;
2854 	}
2855 	if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2856 	    return FAIL;
2857 	if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2858 	    return FAIL;
2859     }
2860     else if (vartype == VAR_STRING)
2861     {
2862 	*typep = &t_string;
2863 	if ((is_slice
2864 		? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2865 		: generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2866 	    return FAIL;
2867     }
2868     else if (vartype == VAR_BLOB)
2869     {
2870 	if (is_slice)
2871 	{
2872 	    *typep = &t_blob;
2873 	    if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2874 		return FAIL;
2875 	}
2876 	else
2877 	{
2878 	    *typep = &t_number;
2879 	    if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2880 		return FAIL;
2881 	}
2882     }
2883     else if (vartype == VAR_LIST || *typep == &t_any)
2884     {
2885 	if (is_slice)
2886 	{
2887 	    if (generate_instr_drop(cctx,
2888 		     vartype == VAR_LIST ?  ISN_LISTSLICE : ISN_ANYSLICE,
2889 							    2) == FAIL)
2890 		return FAIL;
2891 	}
2892 	else
2893 	{
2894 	    if ((*typep)->tt_type == VAR_LIST)
2895 	    {
2896 		*typep = (*typep)->tt_member;
2897 		if (*typep == &t_unknown)
2898 		    // empty list was used
2899 		    *typep = &t_any;
2900 	    }
2901 	    if (generate_instr_drop(cctx,
2902 			vartype == VAR_LIST ?  ISN_LISTINDEX : ISN_ANYINDEX, 1)
2903 								       == FAIL)
2904 		return FAIL;
2905 	}
2906     }
2907     else
2908     {
2909 	emsg(_(e_string_list_dict_or_blob_required));
2910 	return FAIL;
2911     }
2912     return OK;
2913 }
2914 
2915 /*
2916  * Generate an instruction to load script-local variable "name", without the
2917  * leading "s:".
2918  * Also finds imported variables.
2919  */
2920     static int
2921 compile_load_scriptvar(
2922 	cctx_T *cctx,
2923 	char_u *name,	    // variable NUL terminated
2924 	char_u *start,	    // start of variable
2925 	char_u **end,	    // end of variable
2926 	int    error)	    // when TRUE may give error
2927 {
2928     scriptitem_T    *si;
2929     int		    idx;
2930     imported_T	    *import;
2931 
2932     if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2933 	return FAIL;
2934     si = SCRIPT_ITEM(current_sctx.sc_sid);
2935     idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
2936     if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
2937     {
2938 	// variable is not in sn_var_vals: old style script.
2939 	return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2940 								       &t_any);
2941     }
2942     if (idx >= 0)
2943     {
2944 	svar_T		*sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2945 
2946 	generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2947 					current_sctx.sc_sid, idx, sv->sv_type);
2948 	return OK;
2949     }
2950 
2951     import = find_imported(name, 0, cctx);
2952     if (import != NULL)
2953     {
2954 	if (import->imp_flags & IMP_FLAGS_STAR)
2955 	{
2956 	    char_u	*p = skipwhite(*end);
2957 	    char_u	*exp_name;
2958 	    int		cc;
2959 	    ufunc_T	*ufunc;
2960 	    type_T	*type;
2961 
2962 	    // Used "import * as Name", need to lookup the member.
2963 	    if (*p != '.')
2964 	    {
2965 		semsg(_(e_expected_dot_after_name_str), start);
2966 		return FAIL;
2967 	    }
2968 	    ++p;
2969 	    if (VIM_ISWHITE(*p))
2970 	    {
2971 		emsg(_(e_no_white_space_allowed_after_dot));
2972 		return FAIL;
2973 	    }
2974 
2975 	    // isolate one name
2976 	    exp_name = p;
2977 	    while (eval_isnamec(*p))
2978 		++p;
2979 	    cc = *p;
2980 	    *p = NUL;
2981 
2982 	    idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2983 								   cctx, TRUE);
2984 	    *p = cc;
2985 	    p = skipwhite(p);
2986 	    *end = p;
2987 
2988 	    if (idx < 0)
2989 	    {
2990 		if (*p == '(' && ufunc != NULL)
2991 		{
2992 		    generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2993 		    return OK;
2994 		}
2995 		return FAIL;
2996 	    }
2997 
2998 	    generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2999 		    import->imp_sid,
3000 		    idx,
3001 		    type);
3002 	}
3003 	else if (import->imp_funcname != NULL)
3004 	    generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
3005 	else
3006 	    generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
3007 		    import->imp_sid,
3008 		    import->imp_var_vals_idx,
3009 		    import->imp_type);
3010 	return OK;
3011     }
3012 
3013     if (error)
3014 	semsg(_(e_item_not_found_str), name);
3015     return FAIL;
3016 }
3017 
3018     static int
3019 generate_funcref(cctx_T *cctx, char_u *name)
3020 {
3021     ufunc_T *ufunc = find_func(name, FALSE, cctx);
3022 
3023     if (ufunc == NULL)
3024 	return FAIL;
3025 
3026     // Need to compile any default values to get the argument types.
3027     if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
3028 	    && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
3029 								       == FAIL)
3030 	return FAIL;
3031     return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
3032 }
3033 
3034 /*
3035  * Compile a variable name into a load instruction.
3036  * "end" points to just after the name.
3037  * "is_expr" is TRUE when evaluating an expression, might be a funcref.
3038  * When "error" is FALSE do not give an error when not found.
3039  */
3040     static int
3041 compile_load(
3042 	char_u **arg,
3043 	char_u *end_arg,
3044 	cctx_T	*cctx,
3045 	int	is_expr,
3046 	int	error)
3047 {
3048     type_T	*type;
3049     char_u	*name = NULL;
3050     char_u	*end = end_arg;
3051     int		res = FAIL;
3052     int		prev_called_emsg = called_emsg;
3053 
3054     if (*(*arg + 1) == ':')
3055     {
3056 	if (end <= *arg + 2)
3057 	{
3058 	    isntype_T  isn_type;
3059 
3060 	    // load dictionary of namespace
3061 	    switch (**arg)
3062 	    {
3063 		case 'g': isn_type = ISN_LOADGDICT; break;
3064 		case 'w': isn_type = ISN_LOADWDICT; break;
3065 		case 't': isn_type = ISN_LOADTDICT; break;
3066 		case 'b': isn_type = ISN_LOADBDICT; break;
3067 		default:
3068 		    semsg(_(e_namespace_not_supported_str), *arg);
3069 		    goto theend;
3070 	    }
3071 	    if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
3072 		goto theend;
3073 	    res = OK;
3074 	}
3075 	else
3076 	{
3077 	    isntype_T  isn_type = ISN_DROP;
3078 
3079 	    // load namespaced variable
3080 	    name = vim_strnsave(*arg + 2, end - (*arg + 2));
3081 	    if (name == NULL)
3082 		return FAIL;
3083 
3084 	    switch (**arg)
3085 	    {
3086 		case 'v': res = generate_LOADV(cctx, name, error);
3087 			  break;
3088 		case 's': if (is_expr && ASCII_ISUPPER(*name)
3089 				       && find_func(name, FALSE, cctx) != NULL)
3090 			      res = generate_funcref(cctx, name);
3091 			  else
3092 			      res = compile_load_scriptvar(cctx, name,
3093 							    NULL, &end, error);
3094 			  break;
3095 		case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
3096 			  {
3097 			      if (is_expr && ASCII_ISUPPER(*name)
3098 				       && find_func(name, FALSE, cctx) != NULL)
3099 				  res = generate_funcref(cctx, name);
3100 			      else
3101 				  isn_type = ISN_LOADG;
3102 			  }
3103 			  else
3104 			  {
3105 			      isn_type = ISN_LOADAUTO;
3106 			      vim_free(name);
3107 			      name = vim_strnsave(*arg, end - *arg);
3108 			      if (name == NULL)
3109 				  return FAIL;
3110 			  }
3111 			  break;
3112 		case 'w': isn_type = ISN_LOADW; break;
3113 		case 't': isn_type = ISN_LOADT; break;
3114 		case 'b': isn_type = ISN_LOADB; break;
3115 		default:  // cannot happen, just in case
3116 			  semsg(_(e_namespace_not_supported_str), *arg);
3117 			  goto theend;
3118 	    }
3119 	    if (isn_type != ISN_DROP)
3120 	    {
3121 		// Global, Buffer-local, Window-local and Tabpage-local
3122 		// variables can be defined later, thus we don't check if it
3123 		// exists, give an error at runtime.
3124 		res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3125 	    }
3126 	}
3127     }
3128     else
3129     {
3130 	size_t	    len = end - *arg;
3131 	int	    idx;
3132 	int	    gen_load = FALSE;
3133 	int	    gen_load_outer = 0;
3134 
3135 	name = vim_strnsave(*arg, end - *arg);
3136 	if (name == NULL)
3137 	    return FAIL;
3138 
3139 	if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3140 	{
3141 	    script_autoload(name, FALSE);
3142 	    res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
3143 	}
3144 	else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
3145 									 == OK)
3146 	{
3147 	    if (gen_load_outer == 0)
3148 		gen_load = TRUE;
3149 	}
3150 	else
3151 	{
3152 	    lvar_T lvar;
3153 
3154 	    if (lookup_local(*arg, len, &lvar, cctx) == OK)
3155 	    {
3156 		type = lvar.lv_type;
3157 		idx = lvar.lv_idx;
3158 		if (lvar.lv_from_outer != 0)
3159 		    gen_load_outer = lvar.lv_from_outer;
3160 		else
3161 		    gen_load = TRUE;
3162 	    }
3163 	    else
3164 	    {
3165 		// "var" can be script-local even without using "s:" if it
3166 		// already exists in a Vim9 script or when it's imported.
3167 		if (script_var_exists(*arg, len, cctx) == OK
3168 			|| find_imported(name, 0, cctx) != NULL)
3169 		   res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
3170 
3171 		// When evaluating an expression and the name starts with an
3172 		// uppercase letter it can be a user defined function.
3173 		// generate_funcref() will fail if the function can't be found.
3174 		if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
3175 		    res = generate_funcref(cctx, name);
3176 	    }
3177 	}
3178 	if (gen_load)
3179 	    res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
3180 	if (gen_load_outer > 0)
3181 	{
3182 	    res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
3183 	    cctx->ctx_outer_used = TRUE;
3184 	}
3185     }
3186 
3187     *arg = end;
3188 
3189 theend:
3190     if (res == FAIL && error && called_emsg == prev_called_emsg)
3191 	semsg(_(e_variable_not_found_str), name);
3192     vim_free(name);
3193     return res;
3194 }
3195 
3196     static void
3197 clear_instr_ga(garray_T *gap)
3198 {
3199     int idx;
3200 
3201     for (idx = 0; idx < gap->ga_len; ++idx)
3202 	delete_instr(((isn_T *)gap->ga_data) + idx);
3203     ga_clear(gap);
3204 }
3205 
3206 /*
3207  * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
3208  * Returns FAIL if compilation fails.
3209  */
3210     static int
3211 compile_string(isn_T *isn, cctx_T *cctx)
3212 {
3213     char_u	*s = isn->isn_arg.string;
3214     garray_T	save_ga = cctx->ctx_instr;
3215     int		expr_res;
3216     int		trailing_error;
3217     int		instr_count;
3218     isn_T	*instr = NULL;
3219 
3220     // Temporarily reset the list of instructions so that the jump labels are
3221     // correct.
3222     cctx->ctx_instr.ga_len = 0;
3223     cctx->ctx_instr.ga_maxlen = 0;
3224     cctx->ctx_instr.ga_data = NULL;
3225     expr_res = compile_expr0(&s, cctx);
3226     s = skipwhite(s);
3227     trailing_error = *s != NUL;
3228 
3229     if (expr_res == FAIL || trailing_error
3230 				       || ga_grow(&cctx->ctx_instr, 1) == FAIL)
3231     {
3232 	if (trailing_error)
3233 	    semsg(_(e_trailing_arg), s);
3234 	clear_instr_ga(&cctx->ctx_instr);
3235 	cctx->ctx_instr = save_ga;
3236 	return FAIL;
3237     }
3238 
3239     // Move the generated instructions into the ISN_INSTR instruction, then
3240     // restore the list of instructions.
3241     instr_count = cctx->ctx_instr.ga_len;
3242     instr = cctx->ctx_instr.ga_data;
3243     instr[instr_count].isn_type = ISN_FINISH;
3244 
3245     cctx->ctx_instr = save_ga;
3246     vim_free(isn->isn_arg.string);
3247     isn->isn_type = ISN_INSTR;
3248     isn->isn_arg.instr = instr;
3249     return OK;
3250 }
3251 
3252 /*
3253  * Compile the argument expressions.
3254  * "arg" points to just after the "(" and is advanced to after the ")"
3255  */
3256     static int
3257 compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
3258 {
3259     char_u  *p = *arg;
3260     char_u  *whitep = *arg;
3261     int	    must_end = FALSE;
3262     int	    instr_count;
3263 
3264     for (;;)
3265     {
3266 	if (may_get_next_line(whitep, &p, cctx) == FAIL)
3267 	    goto failret;
3268 	if (*p == ')')
3269 	{
3270 	    *arg = p + 1;
3271 	    return OK;
3272 	}
3273 	if (must_end)
3274 	{
3275 	    semsg(_(e_missing_comma_before_argument_str), p);
3276 	    return FAIL;
3277 	}
3278 
3279 	instr_count = cctx->ctx_instr.ga_len;
3280 	if (compile_expr0(&p, cctx) == FAIL)
3281 	    return FAIL;
3282 	++*argcount;
3283 
3284 	if (is_searchpair && *argcount >= 5
3285 		&& cctx->ctx_instr.ga_len == instr_count + 1)
3286 	{
3287 	    isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
3288 
3289 	    // {skip} argument of searchpair() can be compiled if not empty
3290 	    if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
3291 		compile_string(isn, cctx);
3292 	}
3293 
3294 	if (*p != ',' && *skipwhite(p) == ',')
3295 	{
3296 	    semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
3297 	    p = skipwhite(p);
3298 	}
3299 	if (*p == ',')
3300 	{
3301 	    ++p;
3302 	    if (*p != NUL && !VIM_ISWHITE(*p))
3303 		semsg(_(e_white_space_required_after_str_str), ",", p - 1);
3304 	}
3305 	else
3306 	    must_end = TRUE;
3307 	whitep = p;
3308 	p = skipwhite(p);
3309     }
3310 failret:
3311     emsg(_(e_missing_close));
3312     return FAIL;
3313 }
3314 
3315 /*
3316  * Compile a function call:  name(arg1, arg2)
3317  * "arg" points to "name", "arg + varlen" to the "(".
3318  * "argcount_init" is 1 for "value->method()"
3319  * Instructions:
3320  *	EVAL arg1
3321  *	EVAL arg2
3322  *	BCALL / DCALL / UCALL
3323  */
3324     static int
3325 compile_call(
3326 	char_u	    **arg,
3327 	size_t	    varlen,
3328 	cctx_T	    *cctx,
3329 	ppconst_T   *ppconst,
3330 	int	    argcount_init)
3331 {
3332     char_u	*name = *arg;
3333     char_u	*p;
3334     int		argcount = argcount_init;
3335     char_u	namebuf[100];
3336     char_u	fname_buf[FLEN_FIXED + 1];
3337     char_u	*tofree = NULL;
3338     int		error = FCERR_NONE;
3339     ufunc_T	*ufunc = NULL;
3340     int		res = FAIL;
3341     int		is_autoload;
3342     int		is_searchpair;
3343 
3344     // we can evaluate "has('name')" at compile time
3345     if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3346     {
3347 	char_u	    *s = skipwhite(*arg + varlen + 1);
3348 	typval_T    argvars[2];
3349 
3350 	argvars[0].v_type = VAR_UNKNOWN;
3351 	if (*s == '"')
3352 	    (void)eval_string(&s, &argvars[0], TRUE);
3353 	else if (*s == '\'')
3354 	    (void)eval_lit_string(&s, &argvars[0], TRUE);
3355 	s = skipwhite(s);
3356 	if (*s == ')' && argvars[0].v_type == VAR_STRING
3357 		&& !dynamic_feature(argvars[0].vval.v_string))
3358 	{
3359 	    typval_T	*tv = &ppconst->pp_tv[ppconst->pp_used];
3360 
3361 	    *arg = s + 1;
3362 	    argvars[1].v_type = VAR_UNKNOWN;
3363 	    tv->v_type = VAR_NUMBER;
3364 	    tv->vval.v_number = 0;
3365 	    f_has(argvars, tv);
3366 	    clear_tv(&argvars[0]);
3367 	    ++ppconst->pp_used;
3368 	    return OK;
3369 	}
3370 	clear_tv(&argvars[0]);
3371     }
3372 
3373     if (generate_ppconst(cctx, ppconst) == FAIL)
3374 	return FAIL;
3375 
3376     if (varlen >= sizeof(namebuf))
3377     {
3378 	semsg(_(e_name_too_long_str), name);
3379 	return FAIL;
3380     }
3381     vim_strncpy(namebuf, *arg, varlen);
3382     name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
3383 
3384     // We handle the "skip" argument of searchpair() and searchpairpos()
3385     // differently.
3386     is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
3387 	         || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
3388 	        || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
3389 	        || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
3390 
3391     *arg = skipwhite(*arg + varlen + 1);
3392     if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
3393 	goto theend;
3394 
3395     is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
3396     if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
3397     {
3398 	int	    idx;
3399 
3400 	// builtin function
3401 	idx = find_internal_func(name);
3402 	if (idx >= 0)
3403 	{
3404 	    if (STRCMP(name, "flatten") == 0)
3405 	    {
3406 		emsg(_(e_cannot_use_flatten_in_vim9_script));
3407 		goto theend;
3408 	    }
3409 
3410 	    if (STRCMP(name, "add") == 0 && argcount == 2)
3411 	    {
3412 		garray_T    *stack = &cctx->ctx_type_stack;
3413 		type_T	    *type = ((type_T **)stack->ga_data)[
3414 							    stack->ga_len - 2];
3415 
3416 		// add() can be compiled to instructions if we know the type
3417 		if (type->tt_type == VAR_LIST)
3418 		{
3419 		    // inline "add(list, item)" so that the type can be checked
3420 		    res = generate_LISTAPPEND(cctx);
3421 		    idx = -1;
3422 		}
3423 		else if (type->tt_type == VAR_BLOB)
3424 		{
3425 		    // inline "add(blob, nr)" so that the type can be checked
3426 		    res = generate_BLOBAPPEND(cctx);
3427 		    idx = -1;
3428 		}
3429 	    }
3430 
3431 	    if (idx >= 0)
3432 		res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3433 	}
3434 	else
3435 	    semsg(_(e_unknownfunc), namebuf);
3436 	goto theend;
3437     }
3438 
3439     // An argument or local variable can be a function reference, this
3440     // overrules a function name.
3441     if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
3442 	    && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
3443     {
3444 	// If we can find the function by name generate the right call.
3445 	// Skip global functions here, a local funcref takes precedence.
3446 	ufunc = find_func(name, FALSE, cctx);
3447 	if (ufunc != NULL && !func_is_global(ufunc))
3448 	{
3449 	    res = generate_CALL(cctx, ufunc, argcount);
3450 	    goto theend;
3451 	}
3452     }
3453 
3454     // If the name is a variable, load it and use PCALL.
3455     // Not for g:Func(), we don't know if it is a variable or not.
3456     // Not for eome#Func(), it will be loaded later.
3457     p = namebuf;
3458     if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
3459 	    && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
3460     {
3461 	garray_T    *stack = &cctx->ctx_type_stack;
3462 	type_T	    *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3463 
3464 	res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
3465 	goto theend;
3466     }
3467 
3468     // If we can find a global function by name generate the right call.
3469     if (ufunc != NULL)
3470     {
3471 	res = generate_CALL(cctx, ufunc, argcount);
3472 	goto theend;
3473     }
3474 
3475     // A global function may be defined only later.  Need to figure out at
3476     // runtime.  Also handles a FuncRef at runtime.
3477     if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
3478 	res = generate_UCALL(cctx, name, argcount);
3479     else
3480 	semsg(_(e_unknownfunc), namebuf);
3481 
3482 theend:
3483     vim_free(tofree);
3484     return res;
3485 }
3486 
3487 // like NAMESPACE_CHAR but with 'a' and 'l'.
3488 #define VIM9_NAMESPACE_CHAR	(char_u *)"bgstvw"
3489 
3490 /*
3491  * Find the end of a variable or function name.  Unlike find_name_end() this
3492  * does not recognize magic braces.
3493  * When "use_namespace" is TRUE recognize "b:", "s:", etc.
3494  * Return a pointer to just after the name.  Equal to "arg" if there is no
3495  * valid name.
3496  */
3497     char_u *
3498 to_name_end(char_u *arg, int use_namespace)
3499 {
3500     char_u	*p;
3501 
3502     // Quick check for valid starting character.
3503     if (!eval_isnamec1(*arg))
3504 	return arg;
3505 
3506     for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3507 	// Include a namespace such as "s:var" and "v:var".  But "n:" is not
3508 	// and can be used in slice "[n:]".
3509 	if (*p == ':' && (p != arg + 1
3510 			     || !use_namespace
3511 			     || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3512 	    break;
3513     return p;
3514 }
3515 
3516 /*
3517  * Like to_name_end() but also skip over a list or dict constant.
3518  * This intentionally does not handle line continuation.
3519  */
3520     char_u *
3521 to_name_const_end(char_u *arg)
3522 {
3523     char_u	*p = to_name_end(arg, TRUE);
3524     typval_T	rettv;
3525 
3526     if (p == arg && *arg == '[')
3527     {
3528 
3529 	// Can be "[1, 2, 3]->Func()".
3530 	if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
3531 	    p = arg;
3532     }
3533     return p;
3534 }
3535 
3536 /*
3537  * parse a list: [expr, expr]
3538  * "*arg" points to the '['.
3539  * ppconst->pp_is_const is set if all items are a constant.
3540  */
3541     static int
3542 compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3543 {
3544     char_u	*p = skipwhite(*arg + 1);
3545     char_u	*whitep = *arg + 1;
3546     int		count = 0;
3547     int		is_const;
3548     int		is_all_const = TRUE;	// reset when non-const encountered
3549 
3550     for (;;)
3551     {
3552 	if (may_get_next_line(whitep, &p, cctx) == FAIL)
3553 	{
3554 	    semsg(_(e_list_end), *arg);
3555 	    return FAIL;
3556 	}
3557 	if (*p == ',')
3558 	{
3559 	    semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
3560 	    return FAIL;
3561 	}
3562 	if (*p == ']')
3563 	{
3564 	    ++p;
3565 	    break;
3566 	}
3567 	if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
3568 	    return FAIL;
3569 	if (!is_const)
3570 	    is_all_const = FALSE;
3571 	++count;
3572 	if (*p == ',')
3573 	{
3574 	    ++p;
3575 	    if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3576 	    {
3577 		semsg(_(e_white_space_required_after_str_str), ",", p - 1);
3578 		return FAIL;
3579 	    }
3580 	}
3581 	whitep = p;
3582 	p = skipwhite(p);
3583     }
3584     *arg = p;
3585 
3586     ppconst->pp_is_const = is_all_const;
3587     return generate_NEWLIST(cctx, count);
3588 }
3589 
3590 /*
3591  * Parse a lambda: "(arg, arg) => expr"
3592  * "*arg" points to the '('.
3593  * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
3594  */
3595     static int
3596 compile_lambda(char_u **arg, cctx_T *cctx)
3597 {
3598     int		r;
3599     typval_T	rettv;
3600     ufunc_T	*ufunc;
3601     evalarg_T	evalarg;
3602 
3603     CLEAR_FIELD(evalarg);
3604     evalarg.eval_flags = EVAL_EVALUATE;
3605     evalarg.eval_cctx = cctx;
3606 
3607     // Get the funcref in "rettv".
3608     r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3609     if (r != OK)
3610     {
3611 	clear_evalarg(&evalarg, NULL);
3612 	return r;
3613     }
3614 
3615     // "rettv" will now be a partial referencing the function.
3616     ufunc = rettv.vval.v_partial->pt_func;
3617     ++ufunc->uf_refcount;
3618     clear_tv(&rettv);
3619 
3620     // Compile it here to get the return type.  The return type is optional,
3621     // when it's missing use t_unknown.  This is recognized in
3622     // compile_return().
3623     if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3624 	ufunc->uf_ret_type = &t_unknown;
3625     compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
3626 
3627     // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3628     // points into it.  Point to the original line to avoid a dangling pointer.
3629     if (evalarg.eval_tofree_cmdline != NULL)
3630     {
3631 	size_t	off = *arg - evalarg.eval_tofree_cmdline;
3632 
3633 	*arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3634 									 + off;
3635     }
3636 
3637     clear_evalarg(&evalarg, NULL);
3638 
3639     if (ufunc->uf_def_status == UF_COMPILED)
3640     {
3641 	// The return type will now be known.
3642 	set_function_type(ufunc);
3643 
3644 	// The function reference count will be 1.  When the ISN_FUNCREF
3645 	// instruction is deleted the reference count is decremented and the
3646 	// function is freed.
3647 	return generate_FUNCREF(cctx, ufunc);
3648     }
3649 
3650     func_ptr_unref(ufunc);
3651     return FAIL;
3652 }
3653 
3654 /*
3655  * parse a dict: {key: val, [key]: val}
3656  * "*arg" points to the '{'.
3657  * ppconst->pp_is_const is set if all item values are a constant.
3658  */
3659     static int
3660 compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3661 {
3662     garray_T	*instr = &cctx->ctx_instr;
3663     int		count = 0;
3664     dict_T	*d = dict_alloc();
3665     dictitem_T	*item;
3666     char_u	*whitep = *arg + 1;
3667     char_u	*p;
3668     int		is_const;
3669     int		is_all_const = TRUE;	// reset when non-const encountered
3670 
3671     if (d == NULL)
3672 	return FAIL;
3673     if (generate_ppconst(cctx, ppconst) == FAIL)
3674 	return FAIL;
3675     for (;;)
3676     {
3677 	char_u	    *key = NULL;
3678 
3679 	if (may_get_next_line(whitep, arg, cctx) == FAIL)
3680 	{
3681 	    *arg = NULL;
3682 	    goto failret;
3683 	}
3684 
3685 	if (**arg == '}')
3686 	    break;
3687 
3688 	if (**arg == '[')
3689 	{
3690 	    isn_T	*isn;
3691 
3692 	    // {[expr]: value} uses an evaluated key.
3693 	    *arg = skipwhite(*arg + 1);
3694 	    if (compile_expr0(arg, cctx) == FAIL)
3695 		return FAIL;
3696 	    isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3697 	    if (isn->isn_type == ISN_PUSHNR)
3698 	    {
3699 		char buf[NUMBUFLEN];
3700 
3701 		// Convert to string at compile time.
3702 		vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3703 		isn->isn_type = ISN_PUSHS;
3704 		isn->isn_arg.string = vim_strsave((char_u *)buf);
3705 	    }
3706 	    if (isn->isn_type == ISN_PUSHS)
3707 		key = isn->isn_arg.string;
3708 	    else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
3709 		return FAIL;
3710 	    *arg = skipwhite(*arg);
3711 	    if (**arg != ']')
3712 	    {
3713 		emsg(_(e_missing_matching_bracket_after_dict_key));
3714 		return FAIL;
3715 	    }
3716 	    ++*arg;
3717 	}
3718 	else
3719 	{
3720 	    // {"name": value},
3721 	    // {'name': value},
3722 	    // {name: value} use "name" as a literal key
3723 	    key = get_literal_key(arg);
3724 	    if (key == NULL)
3725 		return FAIL;
3726 	    if (generate_PUSHS(cctx, key) == FAIL)
3727 		return FAIL;
3728 	}
3729 
3730 	// Check for duplicate keys, if using string keys.
3731 	if (key != NULL)
3732 	{
3733 	    item = dict_find(d, key, -1);
3734 	    if (item != NULL)
3735 	    {
3736 		semsg(_(e_duplicate_key), key);
3737 		goto failret;
3738 	    }
3739 	    item = dictitem_alloc(key);
3740 	    if (item != NULL)
3741 	    {
3742 		item->di_tv.v_type = VAR_UNKNOWN;
3743 		item->di_tv.v_lock = 0;
3744 		if (dict_add(d, item) == FAIL)
3745 		    dictitem_free(item);
3746 	    }
3747 	}
3748 
3749 	if (**arg != ':')
3750 	{
3751 	    if (*skipwhite(*arg) == ':')
3752 		semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
3753 	    else
3754 		semsg(_(e_missing_dict_colon), *arg);
3755 	    return FAIL;
3756 	}
3757 	whitep = *arg + 1;
3758 	if (!IS_WHITE_OR_NUL(*whitep))
3759 	{
3760 	    semsg(_(e_white_space_required_after_str_str), ":", *arg);
3761 	    return FAIL;
3762 	}
3763 
3764 	if (may_get_next_line(whitep, arg, cctx) == FAIL)
3765 	{
3766 	    *arg = NULL;
3767 	    goto failret;
3768 	}
3769 
3770 	if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
3771 	    return FAIL;
3772 	if (!is_const)
3773 	    is_all_const = FALSE;
3774 	++count;
3775 
3776 	whitep = *arg;
3777 	if (may_get_next_line(whitep, arg, cctx) == FAIL)
3778 	{
3779 	    *arg = NULL;
3780 	    goto failret;
3781 	}
3782 	if (**arg == '}')
3783 	    break;
3784 	if (**arg != ',')
3785 	{
3786 	    semsg(_(e_missing_dict_comma), *arg);
3787 	    goto failret;
3788 	}
3789 	if (IS_WHITE_OR_NUL(*whitep))
3790 	{
3791 	    semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
3792 	    return FAIL;
3793 	}
3794 	whitep = *arg + 1;
3795 	if (!IS_WHITE_OR_NUL(*whitep))
3796 	{
3797 	    semsg(_(e_white_space_required_after_str_str), ",", *arg);
3798 	    return FAIL;
3799 	}
3800 	*arg = skipwhite(whitep);
3801     }
3802 
3803     *arg = *arg + 1;
3804 
3805     // Allow for following comment, after at least one space.
3806     p = skipwhite(*arg);
3807     if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
3808 	*arg += STRLEN(*arg);
3809 
3810     dict_unref(d);
3811     ppconst->pp_is_const = is_all_const;
3812     return generate_NEWDICT(cctx, count);
3813 
3814 failret:
3815     if (*arg == NULL)
3816     {
3817 	semsg(_(e_missing_dict_end), _("[end of lines]"));
3818 	*arg = (char_u *)"";
3819     }
3820     dict_unref(d);
3821     return FAIL;
3822 }
3823 
3824 /*
3825  * Compile "&option".
3826  */
3827     static int
3828 compile_get_option(char_u **arg, cctx_T *cctx)
3829 {
3830     typval_T	rettv;
3831     char_u	*start = *arg;
3832     int		ret;
3833 
3834     // parse the option and get the current value to get the type.
3835     rettv.v_type = VAR_UNKNOWN;
3836     ret = eval_option(arg, &rettv, TRUE);
3837     if (ret == OK)
3838     {
3839 	// include the '&' in the name, eval_option() expects it.
3840 	char_u	*name = vim_strnsave(start, *arg - start);
3841 	type_T	*type = rettv.v_type == VAR_BOOL ? &t_bool
3842 			  : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3843 
3844 	ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3845 	vim_free(name);
3846     }
3847     clear_tv(&rettv);
3848 
3849     return ret;
3850 }
3851 
3852 /*
3853  * Compile "$VAR".
3854  */
3855     static int
3856 compile_get_env(char_u **arg, cctx_T *cctx)
3857 {
3858     char_u	*start = *arg;
3859     int		len;
3860     int		ret;
3861     char_u	*name;
3862 
3863     ++*arg;
3864     len = get_env_len(arg);
3865     if (len == 0)
3866     {
3867 	semsg(_(e_syntax_error_at_str), start - 1);
3868 	return FAIL;
3869     }
3870 
3871     // include the '$' in the name, eval_env_var() expects it.
3872     name = vim_strnsave(start, len + 1);
3873     ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3874     vim_free(name);
3875     return ret;
3876 }
3877 
3878 /*
3879  * Compile "@r".
3880  */
3881     static int
3882 compile_get_register(char_u **arg, cctx_T *cctx)
3883 {
3884     int		ret;
3885 
3886     ++*arg;
3887     if (**arg == NUL)
3888     {
3889 	semsg(_(e_syntax_error_at_str), *arg - 1);
3890 	return FAIL;
3891     }
3892     if (!valid_yank_reg(**arg, FALSE))
3893     {
3894 	emsg_invreg(**arg);
3895 	return FAIL;
3896     }
3897     ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3898     ++*arg;
3899     return ret;
3900 }
3901 
3902 /*
3903  * Apply leading '!', '-' and '+' to constant "rettv".
3904  * When "numeric_only" is TRUE do not apply '!'.
3905  */
3906     static int
3907 apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
3908 {
3909     char_u *p = *end;
3910 
3911     // this works from end to start
3912     while (p > start)
3913     {
3914 	--p;
3915 	if (*p == '-' || *p == '+')
3916 	{
3917 	    // only '-' has an effect, for '+' we only check the type
3918 #ifdef FEAT_FLOAT
3919 	    if (rettv->v_type == VAR_FLOAT)
3920 	    {
3921 		if (*p == '-')
3922 		    rettv->vval.v_float = -rettv->vval.v_float;
3923 	    }
3924 	    else
3925 #endif
3926 	    {
3927 		varnumber_T	val;
3928 		int		error = FALSE;
3929 
3930 		// tv_get_number_chk() accepts a string, but we don't want that
3931 		// here
3932 		if (check_not_string(rettv) == FAIL)
3933 		    return FAIL;
3934 		val = tv_get_number_chk(rettv, &error);
3935 		clear_tv(rettv);
3936 		if (error)
3937 		    return FAIL;
3938 		if (*p == '-')
3939 		    val = -val;
3940 		rettv->v_type = VAR_NUMBER;
3941 		rettv->vval.v_number = val;
3942 	    }
3943 	}
3944 	else if (numeric_only)
3945 	{
3946 	    ++p;
3947 	    break;
3948 	}
3949 	else if (*p == '!')
3950 	{
3951 	    int v = tv2bool(rettv);
3952 
3953 	    // '!' is permissive in the type.
3954 	    clear_tv(rettv);
3955 	    rettv->v_type = VAR_BOOL;
3956 	    rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3957 	}
3958     }
3959     *end = p;
3960     return OK;
3961 }
3962 
3963 /*
3964  * Recognize v: variables that are constants and set "rettv".
3965  */
3966     static void
3967 get_vim_constant(char_u **arg, typval_T *rettv)
3968 {
3969     if (STRNCMP(*arg, "v:true", 6) == 0)
3970     {
3971 	rettv->v_type = VAR_BOOL;
3972 	rettv->vval.v_number = VVAL_TRUE;
3973 	*arg += 6;
3974     }
3975     else if (STRNCMP(*arg, "v:false", 7) == 0)
3976     {
3977 	rettv->v_type = VAR_BOOL;
3978 	rettv->vval.v_number = VVAL_FALSE;
3979 	*arg += 7;
3980     }
3981     else if (STRNCMP(*arg, "v:null", 6) == 0)
3982     {
3983 	rettv->v_type = VAR_SPECIAL;
3984 	rettv->vval.v_number = VVAL_NULL;
3985 	*arg += 6;
3986     }
3987     else if (STRNCMP(*arg, "v:none", 6) == 0)
3988     {
3989 	rettv->v_type = VAR_SPECIAL;
3990 	rettv->vval.v_number = VVAL_NONE;
3991 	*arg += 6;
3992     }
3993 }
3994 
3995     exprtype_T
3996 get_compare_type(char_u *p, int *len, int *type_is)
3997 {
3998     exprtype_T	type = EXPR_UNKNOWN;
3999     int		i;
4000 
4001     switch (p[0])
4002     {
4003 	case '=':   if (p[1] == '=')
4004 			type = EXPR_EQUAL;
4005 		    else if (p[1] == '~')
4006 			type = EXPR_MATCH;
4007 		    break;
4008 	case '!':   if (p[1] == '=')
4009 			type = EXPR_NEQUAL;
4010 		    else if (p[1] == '~')
4011 			type = EXPR_NOMATCH;
4012 		    break;
4013 	case '>':   if (p[1] != '=')
4014 		    {
4015 			type = EXPR_GREATER;
4016 			*len = 1;
4017 		    }
4018 		    else
4019 			type = EXPR_GEQUAL;
4020 		    break;
4021 	case '<':   if (p[1] != '=')
4022 		    {
4023 			type = EXPR_SMALLER;
4024 			*len = 1;
4025 		    }
4026 		    else
4027 			type = EXPR_SEQUAL;
4028 		    break;
4029 	case 'i':   if (p[1] == 's')
4030 		    {
4031 			// "is" and "isnot"; but not a prefix of a name
4032 			if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4033 			    *len = 5;
4034 			i = p[*len];
4035 			if (!isalnum(i) && i != '_')
4036 			{
4037 			    type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
4038 			    *type_is = TRUE;
4039 			}
4040 		    }
4041 		    break;
4042     }
4043     return type;
4044 }
4045 
4046 /*
4047  * Skip over an expression, ignoring most errors.
4048  */
4049     static void
4050 skip_expr_cctx(char_u **arg, cctx_T *cctx)
4051 {
4052     evalarg_T	evalarg;
4053 
4054     CLEAR_FIELD(evalarg);
4055     evalarg.eval_cctx = cctx;
4056     skip_expr(arg, &evalarg);
4057 }
4058 
4059 /*
4060  * Compile code to apply '-', '+' and '!'.
4061  * When "numeric_only" is TRUE do not apply '!'.
4062  */
4063     static int
4064 compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
4065 {
4066     char_u	*p = *end;
4067 
4068     // this works from end to start
4069     while (p > start)
4070     {
4071 	--p;
4072 	while (VIM_ISWHITE(*p))
4073 	    --p;
4074 	if (*p == '-' || *p == '+')
4075 	{
4076 	    int	    negate = *p == '-';
4077 	    isn_T   *isn;
4078 
4079 	    // TODO: check type
4080 	    while (p > start && (p[-1] == '-' || p[-1] == '+'))
4081 	    {
4082 		--p;
4083 		if (*p == '-')
4084 		    negate = !negate;
4085 	    }
4086 	    // only '-' has an effect, for '+' we only check the type
4087 	    if (negate)
4088 		isn = generate_instr(cctx, ISN_NEGATENR);
4089 	    else
4090 		isn = generate_instr(cctx, ISN_CHECKNR);
4091 	    if (isn == NULL)
4092 		return FAIL;
4093 	}
4094 	else if (numeric_only)
4095 	{
4096 	    ++p;
4097 	    break;
4098 	}
4099 	else
4100 	{
4101 	    int  invert = *p == '!';
4102 
4103 	    while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
4104 	    {
4105 		if (p[-1] == '!')
4106 		    invert = !invert;
4107 		--p;
4108 	    }
4109 	    if (generate_2BOOL(cctx, invert, -1) == FAIL)
4110 		return FAIL;
4111 	}
4112     }
4113     *end = p;
4114     return OK;
4115 }
4116 
4117 /*
4118  * Compile "(expression)": recursive!
4119  * Return FAIL/OK.
4120  */
4121     static int
4122 compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4123 {
4124     int	    ret;
4125     char_u  *p = *arg + 1;
4126 
4127     if (may_get_next_line_error(p, arg, cctx) == FAIL)
4128 	return FAIL;
4129     if (ppconst->pp_used <= PPSIZE - 10)
4130     {
4131 	ret = compile_expr1(arg, cctx, ppconst);
4132     }
4133     else
4134     {
4135 	// Not enough space in ppconst, flush constants.
4136 	if (generate_ppconst(cctx, ppconst) == FAIL)
4137 	    return FAIL;
4138 	ret = compile_expr0(arg, cctx);
4139     }
4140     if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4141 	return FAIL;
4142     if (**arg == ')')
4143 	++*arg;
4144     else if (ret == OK)
4145     {
4146 	emsg(_(e_missing_close));
4147 	ret = FAIL;
4148     }
4149     return ret;
4150 }
4151 
4152 /*
4153  * Compile whatever comes after "name" or "name()".
4154  * Advances "*arg" only when something was recognized.
4155  */
4156     static int
4157 compile_subscript(
4158 	char_u **arg,
4159 	cctx_T *cctx,
4160 	char_u *start_leader,
4161 	char_u **end_leader,
4162 	ppconst_T *ppconst)
4163 {
4164     char_u	*name_start = *end_leader;
4165 
4166     for (;;)
4167     {
4168 	char_u *p = skipwhite(*arg);
4169 
4170 	if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
4171 	{
4172 	    char_u *next = peek_next_line_from_context(cctx);
4173 
4174 	    // If a following line starts with "->{" or "->X" advance to that
4175 	    // line, so that a line break before "->" is allowed.
4176 	    // Also if a following line starts with ".x".
4177 	    if (next != NULL &&
4178 		    ((next[0] == '-' && next[1] == '>'
4179 				 && (next[2] == '{'
4180 				       || ASCII_ISALPHA(*skipwhite(next + 2))))
4181 		    || (next[0] == '.' && eval_isdictc(next[1]))))
4182 	    {
4183 		next = next_line_from_context(cctx, TRUE);
4184 		if (next == NULL)
4185 		    return FAIL;
4186 		*arg = next;
4187 		p = skipwhite(*arg);
4188 	    }
4189 	}
4190 
4191 	// Do not skip over white space to find the "(", "execute 'x' ()" is
4192 	// not a function call.
4193 	if (**arg == '(')
4194 	{
4195 	    garray_T    *stack = &cctx->ctx_type_stack;
4196 	    type_T	*type;
4197 	    int		argcount = 0;
4198 
4199 	    if (generate_ppconst(cctx, ppconst) == FAIL)
4200 		return FAIL;
4201 	    ppconst->pp_is_const = FALSE;
4202 
4203 	    // funcref(arg)
4204 	    type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4205 
4206 	    *arg = skipwhite(p + 1);
4207 	    if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
4208 		return FAIL;
4209 	    if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
4210 		return FAIL;
4211 	}
4212 	else if (*p == '-' && p[1] == '>')
4213 	{
4214 	    char_u *pstart = p;
4215 
4216 	    if (generate_ppconst(cctx, ppconst) == FAIL)
4217 		return FAIL;
4218 	    ppconst->pp_is_const = FALSE;
4219 
4220 	    // something->method()
4221 	    // Apply the '!', '-' and '+' first:
4222 	    //   -1.0->func() works like (-1.0)->func()
4223 	    if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
4224 		return FAIL;
4225 
4226 	    p += 2;
4227 	    *arg = skipwhite(p);
4228 	    // No line break supported right after "->".
4229 	    if (**arg == '(')
4230 	    {
4231 		int	    argcount = 1;
4232 		garray_T    *stack = &cctx->ctx_type_stack;
4233 		int	    type_idx_start = stack->ga_len;
4234 		type_T	    *type;
4235 		int	    expr_isn_start = cctx->ctx_instr.ga_len;
4236 		int	    expr_isn_end;
4237 		int	    arg_isn_count;
4238 
4239 		// Funcref call:  list->(Refs[2])(arg)
4240 		// or lambda:	  list->((arg) => expr)(arg)
4241 		//
4242 		// Fist compile the function expression.
4243 		if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
4244 		    return FAIL;
4245 
4246 		// Remember the next instruction index, where the instructions
4247 		// for arguments are being written.
4248 		expr_isn_end = cctx->ctx_instr.ga_len;
4249 
4250 		// Compile the arguments.
4251 		if (**arg != '(')
4252 		{
4253 		    if (*skipwhite(*arg) == '(')
4254 			emsg(_(e_nowhitespace));
4255 		    else
4256 			semsg(_(e_missing_paren), *arg);
4257 		    return FAIL;
4258 		}
4259 		*arg = skipwhite(*arg + 1);
4260 		if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
4261 		    return FAIL;
4262 
4263 		// Move the instructions for the arguments to before the
4264 		// instructions of the expression and move the type of the
4265 		// expression after the argument types.  This is what ISN_PCALL
4266 		// expects.
4267 		stack = &cctx->ctx_type_stack;
4268 		arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4269 		if (arg_isn_count > 0)
4270 		{
4271 		    int	    expr_isn_count = expr_isn_end - expr_isn_start;
4272 		    isn_T   *isn = ALLOC_MULT(isn_T, expr_isn_count);
4273 
4274 		    if (isn == NULL)
4275 			return FAIL;
4276 		    mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4277 							      + expr_isn_start,
4278 					       sizeof(isn_T) * expr_isn_count);
4279 		    mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4280 							      + expr_isn_start,
4281 			     ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4282 						sizeof(isn_T) * arg_isn_count);
4283 		    mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4284 					      + expr_isn_start + arg_isn_count,
4285 					  isn, sizeof(isn_T) * expr_isn_count);
4286 		    vim_free(isn);
4287 
4288 		    type = ((type_T **)stack->ga_data)[type_idx_start];
4289 		    mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4290 			      ((type_T **)stack->ga_data) + type_idx_start + 1,
4291 			      sizeof(type_T *)
4292 				       * (stack->ga_len - type_idx_start - 1));
4293 		    ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4294 		}
4295 
4296 		type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4297 		if (generate_PCALL(cctx, argcount,
4298 				(char_u *)"[expression]", type, FALSE) == FAIL)
4299 		    return FAIL;
4300 	    }
4301 	    else
4302 	    {
4303 		// method call:  list->method()
4304 		p = *arg;
4305 		if (!eval_isnamec1(*p))
4306 		{
4307 		    semsg(_(e_trailing_arg), pstart);
4308 		    return FAIL;
4309 		}
4310 		if (ASCII_ISALPHA(*p) && p[1] == ':')
4311 		    p += 2;
4312 		for ( ; eval_isnamec(*p); ++p)
4313 		    ;
4314 		if (*p != '(')
4315 		{
4316 		    semsg(_(e_missing_paren), *arg);
4317 		    return FAIL;
4318 		}
4319 		// TODO: base value may not be the first argument
4320 		if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
4321 		    return FAIL;
4322 	    }
4323 	}
4324 	else if (**arg == '[')
4325 	{
4326 	    int		is_slice = FALSE;
4327 
4328 	    // list index: list[123]
4329 	    // dict member: dict[key]
4330 	    // string index: text[123]
4331 	    // blob index: blob[123]
4332 	    // TODO: more arguments
4333 	    // TODO: recognize list or dict at runtime
4334 	    if (generate_ppconst(cctx, ppconst) == FAIL)
4335 		return FAIL;
4336 	    ppconst->pp_is_const = FALSE;
4337 
4338 	    ++p;
4339 	    if (may_get_next_line_error(p, arg, cctx) == FAIL)
4340 		return FAIL;
4341 	    if (**arg == ':')
4342 	    {
4343 		// missing first index is equal to zero
4344 		generate_PUSHNR(cctx, 0);
4345 	    }
4346 	    else
4347 	    {
4348 		if (compile_expr0(arg, cctx) == FAIL)
4349 		    return FAIL;
4350 		if (**arg == ':')
4351 		{
4352 		    semsg(_(e_white_space_required_before_and_after_str_at_str),
4353 								    ":", *arg);
4354 		    return FAIL;
4355 		}
4356 		if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4357 		    return FAIL;
4358 		*arg = skipwhite(*arg);
4359 	    }
4360 	    if (**arg == ':')
4361 	    {
4362 		is_slice = TRUE;
4363 		++*arg;
4364 		if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4365 		{
4366 		    semsg(_(e_white_space_required_before_and_after_str_at_str),
4367 								    ":", *arg);
4368 		    return FAIL;
4369 		}
4370 		if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4371 		    return FAIL;
4372 		if (**arg == ']')
4373 		    // missing second index is equal to end of string
4374 		    generate_PUSHNR(cctx, -1);
4375 		else
4376 		{
4377 		    if (compile_expr0(arg, cctx) == FAIL)
4378 			return FAIL;
4379 		    if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4380 			return FAIL;
4381 		    *arg = skipwhite(*arg);
4382 		}
4383 	    }
4384 
4385 	    if (**arg != ']')
4386 	    {
4387 		emsg(_(e_missbrac));
4388 		return FAIL;
4389 	    }
4390 	    *arg = *arg + 1;
4391 
4392 	    if (compile_member(is_slice, cctx) == FAIL)
4393 		return FAIL;
4394 	}
4395 	else if (*p == '.' && p[1] != '.')
4396 	{
4397 	    // dictionary member: dict.name
4398 	    if (generate_ppconst(cctx, ppconst) == FAIL)
4399 		return FAIL;
4400 	    ppconst->pp_is_const = FALSE;
4401 
4402 	    *arg = p + 1;
4403 	    if (IS_WHITE_OR_NUL(**arg))
4404 	    {
4405 		emsg(_(e_missing_name_after_dot));
4406 		return FAIL;
4407 	    }
4408 	    p = *arg;
4409 	    if (eval_isdictc(*p))
4410 		while (eval_isnamec(*p))
4411 		    MB_PTR_ADV(p);
4412 	    if (p == *arg)
4413 	    {
4414 		semsg(_(e_syntax_error_at_str), *arg);
4415 		return FAIL;
4416 	    }
4417 	    if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
4418 		return FAIL;
4419 	    *arg = p;
4420 	}
4421 	else
4422 	    break;
4423     }
4424 
4425     // TODO - see handle_subscript():
4426     // Turn "dict.Func" into a partial for "Func" bound to "dict".
4427     // Don't do this when "Func" is already a partial that was bound
4428     // explicitly (pt_auto is FALSE).
4429 
4430     return OK;
4431 }
4432 
4433 /*
4434  * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4435  * "arg" is advanced until after the expression, skipping white space.
4436  *
4437  * If the value is a constant "ppconst->pp_used" will be non-zero.
4438  * Before instructions are generated, any values in "ppconst" will generated.
4439  *
4440  * This is the compiling equivalent of eval1(), eval2(), etc.
4441  */
4442 
4443 /*
4444  *  number		number constant
4445  *  0zFFFFFFFF		Blob constant
4446  *  "string"		string constant
4447  *  'string'		literal string constant
4448  *  &option-name	option value
4449  *  @r			register contents
4450  *  identifier		variable value
4451  *  function()		function call
4452  *  $VAR		environment variable
4453  *  (expression)	nested expression
4454  *  [expr, expr]	List
4455  *  {key: val, [key]: val}   Dictionary
4456  *
4457  *  Also handle:
4458  *  ! in front		logical NOT
4459  *  - in front		unary minus
4460  *  + in front		unary plus (ignored)
4461  *  trailing (arg)	funcref/partial call
4462  *  trailing []		subscript in String or List
4463  *  trailing .name	entry in Dictionary
4464  *  trailing ->name()	method call
4465  */
4466     static int
4467 compile_expr7(
4468 	char_u **arg,
4469 	cctx_T *cctx,
4470 	ppconst_T *ppconst)
4471 {
4472     char_u	*start_leader, *end_leader;
4473     int		ret = OK;
4474     typval_T	*rettv = &ppconst->pp_tv[ppconst->pp_used];
4475     int		used_before = ppconst->pp_used;
4476 
4477     ppconst->pp_is_const = FALSE;
4478 
4479     /*
4480      * Skip '!', '-' and '+' characters.  They are handled later.
4481      */
4482     start_leader = *arg;
4483     if (eval_leader(arg, TRUE) == FAIL)
4484 	return FAIL;
4485     end_leader = *arg;
4486 
4487     rettv->v_type = VAR_UNKNOWN;
4488     switch (**arg)
4489     {
4490 	/*
4491 	 * Number constant.
4492 	 */
4493 	case '0':	// also for blob starting with 0z
4494 	case '1':
4495 	case '2':
4496 	case '3':
4497 	case '4':
4498 	case '5':
4499 	case '6':
4500 	case '7':
4501 	case '8':
4502 	case '9':
4503 	case '.':   if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
4504 			return FAIL;
4505 		    // Apply "-" and "+" just before the number now, right to
4506 		    // left.  Matters especially when "->" follows.  Stops at
4507 		    // '!'.
4508 		    if (apply_leader(rettv, TRUE,
4509 					    start_leader, &end_leader) == FAIL)
4510 		    {
4511 			clear_tv(rettv);
4512 			return FAIL;
4513 		    }
4514 		    break;
4515 
4516 	/*
4517 	 * String constant: "string".
4518 	 */
4519 	case '"':   if (eval_string(arg, rettv, TRUE) == FAIL)
4520 			return FAIL;
4521 		    break;
4522 
4523 	/*
4524 	 * Literal string constant: 'str''ing'.
4525 	 */
4526 	case '\'':  if (eval_lit_string(arg, rettv, TRUE) == FAIL)
4527 			return FAIL;
4528 		    break;
4529 
4530 	/*
4531 	 * Constant Vim variable.
4532 	 */
4533 	case 'v':   get_vim_constant(arg, rettv);
4534 		    ret = NOTDONE;
4535 		    break;
4536 
4537 	/*
4538 	 * "true" constant
4539 	 */
4540 	case 't':   if (STRNCMP(*arg, "true", 4) == 0
4541 						   && !eval_isnamec((*arg)[4]))
4542 		    {
4543 			*arg += 4;
4544 			rettv->v_type = VAR_BOOL;
4545 			rettv->vval.v_number = VVAL_TRUE;
4546 		    }
4547 		    else
4548 			ret = NOTDONE;
4549 		    break;
4550 
4551 	/*
4552 	 * "false" constant
4553 	 */
4554 	case 'f':   if (STRNCMP(*arg, "false", 5) == 0
4555 						   && !eval_isnamec((*arg)[5]))
4556 		    {
4557 			*arg += 5;
4558 			rettv->v_type = VAR_BOOL;
4559 			rettv->vval.v_number = VVAL_FALSE;
4560 		    }
4561 		    else
4562 			ret = NOTDONE;
4563 		    break;
4564 
4565 	/*
4566 	 * "null" constant
4567 	 */
4568 	case 'n':   if (STRNCMP(*arg, "null", 4) == 0
4569 						   && !eval_isnamec((*arg)[4]))
4570 		    {
4571 			*arg += 4;
4572 			rettv->v_type = VAR_SPECIAL;
4573 			rettv->vval.v_number = VVAL_NULL;
4574 		    }
4575 		    else
4576 			ret = NOTDONE;
4577 		    break;
4578 
4579 	/*
4580 	 * List: [expr, expr]
4581 	 */
4582 	case '[':   if (generate_ppconst(cctx, ppconst) == FAIL)
4583 			return FAIL;
4584 		    ret = compile_list(arg, cctx, ppconst);
4585 		    break;
4586 
4587 	/*
4588 	 * Dictionary: {'key': val, 'key': val}
4589 	 */
4590 	case '{':   if (generate_ppconst(cctx, ppconst) == FAIL)
4591 			return FAIL;
4592 		    ret = compile_dict(arg, cctx, ppconst);
4593 		    break;
4594 
4595 	/*
4596 	 * Option value: &name
4597 	 */
4598 	case '&':	if (generate_ppconst(cctx, ppconst) == FAIL)
4599 			    return FAIL;
4600 			ret = compile_get_option(arg, cctx);
4601 			break;
4602 
4603 	/*
4604 	 * Environment variable: $VAR.
4605 	 */
4606 	case '$':	if (generate_ppconst(cctx, ppconst) == FAIL)
4607 			    return FAIL;
4608 			ret = compile_get_env(arg, cctx);
4609 			break;
4610 
4611 	/*
4612 	 * Register contents: @r.
4613 	 */
4614 	case '@':	if (generate_ppconst(cctx, ppconst) == FAIL)
4615 			    return FAIL;
4616 			ret = compile_get_register(arg, cctx);
4617 			break;
4618 	/*
4619 	 * nested expression: (expression).
4620 	 * lambda: (arg, arg) => expr
4621 	 * funcref: (arg, arg) => { statement }
4622 	 */
4623 	case '(':   // if compile_lambda returns NOTDONE then it must be (expr)
4624 		    ret = compile_lambda(arg, cctx);
4625 		    if (ret == NOTDONE)
4626 			ret = compile_parenthesis(arg, cctx, ppconst);
4627 		    break;
4628 
4629 	default:    ret = NOTDONE;
4630 		    break;
4631     }
4632     if (ret == FAIL)
4633 	return FAIL;
4634 
4635     if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
4636     {
4637 	if (cctx->ctx_skip == SKIP_YES)
4638 	    clear_tv(rettv);
4639 	else
4640 	    // A constant expression can possibly be handled compile time,
4641 	    // return the value instead of generating code.
4642 	    ++ppconst->pp_used;
4643     }
4644     else if (ret == NOTDONE)
4645     {
4646 	char_u	    *p;
4647 	int	    r;
4648 
4649 	if (!eval_isnamec1(**arg))
4650 	{
4651 	    if (!vim9_bad_comment(*arg))
4652 	    {
4653 		if (ends_excmd(*skipwhite(*arg)))
4654 		    semsg(_(e_empty_expression_str), *arg);
4655 		else
4656 		    semsg(_(e_name_expected_str), *arg);
4657 	    }
4658 	    return FAIL;
4659 	}
4660 
4661 	// "name" or "name()"
4662 	p = to_name_end(*arg, TRUE);
4663 	if (p - *arg == (size_t)1 && **arg == '_')
4664 	{
4665 	    emsg(_(e_cannot_use_underscore_here));
4666 	    return FAIL;
4667 	}
4668 
4669 	if (*p == '(')
4670 	{
4671 	    r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4672 	}
4673 	else
4674 	{
4675 	    if (generate_ppconst(cctx, ppconst) == FAIL)
4676 		return FAIL;
4677 	    r = compile_load(arg, p, cctx, TRUE, TRUE);
4678 	}
4679 	if (r == FAIL)
4680 	    return FAIL;
4681     }
4682 
4683     // Handle following "[]", ".member", etc.
4684     // Then deal with prefixed '-', '+' and '!', if not done already.
4685     if (compile_subscript(arg, cctx, start_leader, &end_leader,
4686 							     ppconst) == FAIL)
4687 	return FAIL;
4688     if (ppconst->pp_used > 0)
4689     {
4690 	// apply the '!', '-' and '+' before the constant
4691 	rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4692 	if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
4693 	    return FAIL;
4694 	return OK;
4695     }
4696     if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
4697 	return FAIL;
4698     return OK;
4699 }
4700 
4701 /*
4702  * Give the "white on both sides" error, taking the operator from "p[len]".
4703  */
4704     void
4705 error_white_both(char_u *op, int len)
4706 {
4707     char_u	buf[10];
4708 
4709     vim_strncpy(buf, op, len);
4710     semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
4711 }
4712 
4713 /*
4714  * <type>expr7: runtime type check / conversion
4715  */
4716     static int
4717 compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4718 {
4719     type_T *want_type = NULL;
4720 
4721     // Recognize <type>
4722     if (**arg == '<' && eval_isnamec1((*arg)[1]))
4723     {
4724 	++*arg;
4725 	want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4726 	if (want_type == NULL)
4727 	    return FAIL;
4728 
4729 	if (**arg != '>')
4730 	{
4731 	    if (*skipwhite(*arg) == '>')
4732 		semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4733 	    else
4734 		emsg(_(e_missing_gt));
4735 	    return FAIL;
4736 	}
4737 	++*arg;
4738 	if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
4739 	    return FAIL;
4740     }
4741 
4742     if (compile_expr7(arg, cctx, ppconst) == FAIL)
4743 	return FAIL;
4744 
4745     if (want_type != NULL)
4746     {
4747 	garray_T    *stack = &cctx->ctx_type_stack;
4748 	type_T	    *actual;
4749 	where_T	    where;
4750 
4751 	generate_ppconst(cctx, ppconst);
4752 	actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4753 	where.wt_index = 0;
4754 	where.wt_variable = FALSE;
4755 	if (check_type(want_type, actual, FALSE, where) == FAIL)
4756 	{
4757 	    if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
4758 		return FAIL;
4759 	}
4760     }
4761 
4762     return OK;
4763 }
4764 
4765 /*
4766  *	*	number multiplication
4767  *	/	number division
4768  *	%	number modulo
4769  */
4770     static int
4771 compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4772 {
4773     char_u	*op;
4774     char_u	*next;
4775     int		ppconst_used = ppconst->pp_used;
4776 
4777     // get the first expression
4778     if (compile_expr7t(arg, cctx, ppconst) == FAIL)
4779 	return FAIL;
4780 
4781     /*
4782      * Repeat computing, until no "*", "/" or "%" is following.
4783      */
4784     for (;;)
4785     {
4786 	op = may_peek_next_line(cctx, *arg, &next);
4787 	if (*op != '*' && *op != '/' && *op != '%')
4788 	    break;
4789 	if (next != NULL)
4790 	{
4791 	    *arg = next_line_from_context(cctx, TRUE);
4792 	    op = skipwhite(*arg);
4793 	}
4794 
4795 	if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
4796 	{
4797 	    error_white_both(op, 1);
4798 	    return FAIL;
4799 	}
4800 	if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
4801 	    return FAIL;
4802 
4803 	// get the second expression
4804 	if (compile_expr7t(arg, cctx, ppconst) == FAIL)
4805 	    return FAIL;
4806 
4807 	if (ppconst->pp_used == ppconst_used + 2
4808 		&& ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4809 		&& ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
4810 	{
4811 	    typval_T	    *tv1 = &ppconst->pp_tv[ppconst_used];
4812 	    typval_T	    *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4813 	    varnumber_T	    res = 0;
4814 	    int		    failed = FALSE;
4815 
4816 	    // both are numbers: compute the result
4817 	    switch (*op)
4818 	    {
4819 		case '*': res = tv1->vval.v_number * tv2->vval.v_number;
4820 			  break;
4821 		case '/': res = num_divide(tv1->vval.v_number,
4822 						  tv2->vval.v_number, &failed);
4823 			  break;
4824 		case '%': res = num_modulus(tv1->vval.v_number,
4825 						  tv2->vval.v_number, &failed);
4826 			  break;
4827 	    }
4828 	    if (failed)
4829 		return FAIL;
4830 	    tv1->vval.v_number = res;
4831 	    --ppconst->pp_used;
4832 	}
4833 	else
4834 	{
4835 	    generate_ppconst(cctx, ppconst);
4836 	    generate_two_op(cctx, op);
4837 	}
4838     }
4839 
4840     return OK;
4841 }
4842 
4843 /*
4844  *      +	number addition or list/blobl concatenation
4845  *      -	number subtraction
4846  *      ..	string concatenation
4847  */
4848     static int
4849 compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4850 {
4851     char_u	*op;
4852     char_u	*next;
4853     int		oplen;
4854     int		ppconst_used = ppconst->pp_used;
4855 
4856     // get the first variable
4857     if (compile_expr6(arg, cctx, ppconst) == FAIL)
4858 	return FAIL;
4859 
4860     /*
4861      * Repeat computing, until no "+", "-" or ".." is following.
4862      */
4863     for (;;)
4864     {
4865 	op = may_peek_next_line(cctx, *arg, &next);
4866 	if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
4867 	    break;
4868 	if (op[0] == op[1] && *op != '.' && next)
4869 	    // Finding "++" or "--" on the next line is a separate command.
4870 	    // But ".." is concatenation.
4871 	    break;
4872 	oplen = (*op == '.' ? 2 : 1);
4873 	if (next != NULL)
4874 	{
4875 	    *arg = next_line_from_context(cctx, TRUE);
4876 	    op = skipwhite(*arg);
4877 	}
4878 
4879 	if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
4880 	{
4881 	    error_white_both(op, oplen);
4882 	    return FAIL;
4883 	}
4884 
4885 	if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
4886 	    return FAIL;
4887 
4888 	// get the second expression
4889 	if (compile_expr6(arg, cctx, ppconst) == FAIL)
4890 	    return FAIL;
4891 
4892 	if (ppconst->pp_used == ppconst_used + 2
4893 		&& (*op == '.'
4894 		    ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4895 		    && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4896 		    : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4897 		    && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
4898 	{
4899 	    typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4900 	    typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4901 
4902 	    // concat/subtract/add constant numbers
4903 	    if (*op == '+')
4904 		tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4905 	    else if (*op == '-')
4906 		tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4907 	    else
4908 	    {
4909 		// concatenate constant strings
4910 		char_u *s1 = tv1->vval.v_string;
4911 		char_u *s2 = tv2->vval.v_string;
4912 		size_t len1 = STRLEN(s1);
4913 
4914 		tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4915 		if (tv1->vval.v_string == NULL)
4916 		{
4917 		    clear_ppconst(ppconst);
4918 		    return FAIL;
4919 		}
4920 		mch_memmove(tv1->vval.v_string, s1, len1);
4921 		STRCPY(tv1->vval.v_string + len1, s2);
4922 		vim_free(s1);
4923 		vim_free(s2);
4924 	    }
4925 	    --ppconst->pp_used;
4926 	}
4927 	else
4928 	{
4929 	    generate_ppconst(cctx, ppconst);
4930 	    ppconst->pp_is_const = FALSE;
4931 	    if (*op == '.')
4932 	    {
4933 		if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
4934 			|| may_generate_2STRING(-1, FALSE, cctx) == FAIL)
4935 		    return FAIL;
4936 		generate_instr_drop(cctx, ISN_CONCAT, 1);
4937 	    }
4938 	    else
4939 		generate_two_op(cctx, op);
4940 	}
4941     }
4942 
4943     return OK;
4944 }
4945 
4946 /*
4947  * expr5a == expr5b
4948  * expr5a =~ expr5b
4949  * expr5a != expr5b
4950  * expr5a !~ expr5b
4951  * expr5a > expr5b
4952  * expr5a >= expr5b
4953  * expr5a < expr5b
4954  * expr5a <= expr5b
4955  * expr5a is expr5b
4956  * expr5a isnot expr5b
4957  *
4958  * Produces instructions:
4959  *	EVAL expr5a		Push result of "expr5a"
4960  *	EVAL expr5b		Push result of "expr5b"
4961  *	COMPARE			one of the compare instructions
4962  */
4963     static int
4964 compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4965 {
4966     exprtype_T	type = EXPR_UNKNOWN;
4967     char_u	*p;
4968     char_u	*next;
4969     int		len = 2;
4970     int		type_is = FALSE;
4971     int		ppconst_used = ppconst->pp_used;
4972 
4973     // get the first variable
4974     if (compile_expr5(arg, cctx, ppconst) == FAIL)
4975 	return FAIL;
4976 
4977     p = may_peek_next_line(cctx, *arg, &next);
4978     type = get_compare_type(p, &len, &type_is);
4979 
4980     /*
4981      * If there is a comparative operator, use it.
4982      */
4983     if (type != EXPR_UNKNOWN)
4984     {
4985 	int ic = FALSE;  // Default: do not ignore case
4986 
4987 	if (next != NULL)
4988 	{
4989 	    *arg = next_line_from_context(cctx, TRUE);
4990 	    p = skipwhite(*arg);
4991 	}
4992 	if (type_is && (p[len] == '?' || p[len] == '#'))
4993 	{
4994 	    semsg(_(e_invalid_expression_str), *arg);
4995 	    return FAIL;
4996 	}
4997 	// extra question mark appended: ignore case
4998 	if (p[len] == '?')
4999 	{
5000 	    ic = TRUE;
5001 	    ++len;
5002 	}
5003 	// extra '#' appended: match case (ignored)
5004 	else if (p[len] == '#')
5005 	    ++len;
5006 	// nothing appended: match case
5007 
5008 	if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
5009 	{
5010 	    error_white_both(p, len);
5011 	    return FAIL;
5012 	}
5013 
5014 	// get the second variable
5015 	if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
5016 	    return FAIL;
5017 
5018 	if (compile_expr5(arg, cctx, ppconst) == FAIL)
5019 	    return FAIL;
5020 
5021 	if (ppconst->pp_used == ppconst_used + 2)
5022 	{
5023 	    typval_T *	tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
5024 	    typval_T	*tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
5025 	    int		ret;
5026 
5027 	    // Both sides are a constant, compute the result now.
5028 	    // First check for a valid combination of types, this is more
5029 	    // strict than typval_compare().
5030 	    if (check_compare_types(type, tv1, tv2) == FAIL)
5031 		ret = FAIL;
5032 	    else
5033 	    {
5034 		ret = typval_compare(tv1, tv2, type, ic);
5035 		tv1->v_type = VAR_BOOL;
5036 		tv1->vval.v_number = tv1->vval.v_number
5037 						      ? VVAL_TRUE : VVAL_FALSE;
5038 		clear_tv(tv2);
5039 		--ppconst->pp_used;
5040 	    }
5041 	    return ret;
5042 	}
5043 
5044 	generate_ppconst(cctx, ppconst);
5045 	return generate_COMPARE(cctx, type, ic);
5046     }
5047 
5048     return OK;
5049 }
5050 
5051 static int compile_expr3(char_u **arg,  cctx_T *cctx, ppconst_T *ppconst);
5052 
5053 /*
5054  * Compile || or &&.
5055  */
5056     static int
5057 compile_and_or(
5058 	char_u **arg,
5059 	cctx_T	*cctx,
5060 	char	*op,
5061 	ppconst_T *ppconst,
5062 	int	ppconst_used UNUSED)
5063 {
5064     char_u	*next;
5065     char_u	*p = may_peek_next_line(cctx, *arg, &next);
5066     int		opchar = *op;
5067 
5068     if (p[0] == opchar && p[1] == opchar)
5069     {
5070 	garray_T	*instr = &cctx->ctx_instr;
5071 	garray_T	end_ga;
5072 
5073 	/*
5074 	 * Repeat until there is no following "||" or "&&"
5075 	 */
5076 	ga_init2(&end_ga, sizeof(int), 10);
5077 	while (p[0] == opchar && p[1] == opchar)
5078 	{
5079 	    long	start_lnum = SOURCING_LNUM;
5080 	    int		start_ctx_lnum = cctx->ctx_lnum;
5081 	    int		save_lnum;
5082 
5083 	    if (next != NULL)
5084 	    {
5085 		*arg = next_line_from_context(cctx, TRUE);
5086 		p = skipwhite(*arg);
5087 	    }
5088 
5089 	    if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
5090 	    {
5091 		semsg(_(e_white_space_required_before_and_after_str_at_str),
5092 									op, p);
5093 		return FAIL;
5094 	    }
5095 
5096 	    // TODO: use ppconst if the value is a constant and check
5097 	    // evaluating to bool
5098 	    generate_ppconst(cctx, ppconst);
5099 
5100 	    // Every part must evaluate to a bool.
5101 	    SOURCING_LNUM = start_lnum;
5102 	    save_lnum = cctx->ctx_lnum;
5103 	    cctx->ctx_lnum = start_ctx_lnum;
5104 	    if (bool_on_stack(cctx) == FAIL)
5105 	    {
5106 		cctx->ctx_lnum = save_lnum;
5107 		ga_clear(&end_ga);
5108 		return FAIL;
5109 	    }
5110 	    cctx->ctx_lnum = save_lnum;
5111 
5112 	    if (ga_grow(&end_ga, 1) == FAIL)
5113 	    {
5114 		ga_clear(&end_ga);
5115 		return FAIL;
5116 	    }
5117 	    *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
5118 	    ++end_ga.ga_len;
5119 	    generate_JUMP(cctx, opchar == '|'
5120 				 ?  JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
5121 
5122 	    // eval the next expression
5123 	    if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
5124 	    {
5125 		ga_clear(&end_ga);
5126 		return FAIL;
5127 	    }
5128 
5129 	    if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
5130 				  : compile_expr4(arg, cctx, ppconst)) == FAIL)
5131 	    {
5132 		ga_clear(&end_ga);
5133 		return FAIL;
5134 	    }
5135 
5136 	    p = may_peek_next_line(cctx, *arg, &next);
5137 	}
5138 	generate_ppconst(cctx, ppconst);
5139 
5140 	// Every part must evaluate to a bool.
5141 	if (bool_on_stack(cctx) == FAIL)
5142 	{
5143 	    ga_clear(&end_ga);
5144 	    return FAIL;
5145 	}
5146 
5147 	// Fill in the end label in all jumps.
5148 	while (end_ga.ga_len > 0)
5149 	{
5150 	    isn_T	*isn;
5151 
5152 	    --end_ga.ga_len;
5153 	    isn = ((isn_T *)instr->ga_data)
5154 				  + *(((int *)end_ga.ga_data) + end_ga.ga_len);
5155 	    isn->isn_arg.jump.jump_where = instr->ga_len;
5156 	}
5157 	ga_clear(&end_ga);
5158     }
5159 
5160     return OK;
5161 }
5162 
5163 /*
5164  * expr4a && expr4a && expr4a	    logical AND
5165  *
5166  * Produces instructions:
5167  *	EVAL expr4a		Push result of "expr4a"
5168  *	COND2BOOL		convert to bool if needed
5169  *	JUMP_IF_COND_FALSE end
5170  *	EVAL expr4b		Push result of "expr4b"
5171  *	JUMP_IF_COND_FALSE end
5172  *	EVAL expr4c		Push result of "expr4c"
5173  * end:
5174  */
5175     static int
5176 compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
5177 {
5178     int		ppconst_used = ppconst->pp_used;
5179 
5180     // get the first variable
5181     if (compile_expr4(arg, cctx, ppconst) == FAIL)
5182 	return FAIL;
5183 
5184     // || and && work almost the same
5185     return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
5186 }
5187 
5188 /*
5189  * expr3a || expr3b || expr3c	    logical OR
5190  *
5191  * Produces instructions:
5192  *	EVAL expr3a		Push result of "expr3a"
5193  *	COND2BOOL		convert to bool if needed
5194  *	JUMP_IF_COND_TRUE end
5195  *	EVAL expr3b		Push result of "expr3b"
5196  *	JUMP_IF_COND_TRUE end
5197  *	EVAL expr3c		Push result of "expr3c"
5198  * end:
5199  */
5200     static int
5201 compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
5202 {
5203     int		ppconst_used = ppconst->pp_used;
5204 
5205     // eval the first expression
5206     if (compile_expr3(arg, cctx, ppconst) == FAIL)
5207 	return FAIL;
5208 
5209     // || and && work almost the same
5210     return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
5211 }
5212 
5213 /*
5214  * Toplevel expression: expr2 ? expr1a : expr1b
5215  * Produces instructions:
5216  *	EVAL expr2		Push result of "expr2"
5217  *      JUMP_IF_FALSE alt	jump if false
5218  *      EVAL expr1a
5219  *      JUMP_ALWAYS end
5220  * alt:	EVAL expr1b
5221  * end:
5222  *
5223  * Toplevel expression: expr2 ?? expr1
5224  * Produces instructions:
5225  *	EVAL expr2		    Push result of "expr2"
5226  *      JUMP_AND_KEEP_IF_TRUE end   jump if true
5227  *      EVAL expr1
5228  * end:
5229  */
5230     static int
5231 compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
5232 {
5233     char_u	*p;
5234     int		ppconst_used = ppconst->pp_used;
5235     char_u	*next;
5236 
5237     // Ignore all kinds of errors when not producing code.
5238     if (cctx->ctx_skip == SKIP_YES)
5239     {
5240 	skip_expr_cctx(arg, cctx);
5241 	return OK;
5242     }
5243 
5244     // Evaluate the first expression.
5245     if (compile_expr2(arg, cctx, ppconst) == FAIL)
5246 	return FAIL;
5247 
5248     p = may_peek_next_line(cctx, *arg, &next);
5249     if (*p == '?')
5250     {
5251 	int		op_falsy = p[1] == '?';
5252 	garray_T	*instr = &cctx->ctx_instr;
5253 	garray_T	*stack = &cctx->ctx_type_stack;
5254 	int		alt_idx = instr->ga_len;
5255 	int		end_idx = 0;
5256 	isn_T		*isn;
5257 	type_T		*type1 = NULL;
5258 	int		has_const_expr = FALSE;
5259 	int		const_value = FALSE;
5260 	int		save_skip = cctx->ctx_skip;
5261 
5262 	if (next != NULL)
5263 	{
5264 	    *arg = next_line_from_context(cctx, TRUE);
5265 	    p = skipwhite(*arg);
5266 	}
5267 
5268 	if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
5269 	{
5270 	    semsg(_(e_white_space_required_before_and_after_str_at_str),
5271 						     op_falsy ? "??" : "?", p);
5272 	    return FAIL;
5273 	}
5274 
5275 	if (ppconst->pp_used == ppconst_used + 1)
5276 	{
5277 	    // the condition is a constant, we know whether the ? or the :
5278 	    // expression is to be evaluated.
5279 	    has_const_expr = TRUE;
5280 	    if (op_falsy)
5281 		const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5282 	    else
5283 	    {
5284 		int error = FALSE;
5285 
5286 		const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5287 								       &error);
5288 		if (error)
5289 		    return FAIL;
5290 	    }
5291 	    cctx->ctx_skip = save_skip == SKIP_YES ||
5292 		 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5293 
5294 	    if (op_falsy && cctx->ctx_skip == SKIP_YES)
5295 		// "left ?? right" and "left" is truthy: produce "left"
5296 		generate_ppconst(cctx, ppconst);
5297 	    else
5298 	    {
5299 		clear_tv(&ppconst->pp_tv[ppconst_used]);
5300 		--ppconst->pp_used;
5301 	    }
5302 	}
5303 	else
5304 	{
5305 	    generate_ppconst(cctx, ppconst);
5306 	    if (op_falsy)
5307 		end_idx = instr->ga_len;
5308 	    generate_JUMP(cctx, op_falsy
5309 				   ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5310 	    if (op_falsy)
5311 		type1 = ((type_T **)stack->ga_data)[stack->ga_len];
5312 	}
5313 
5314 	// evaluate the second expression; any type is accepted
5315 	if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
5316 	    return FAIL;
5317 	if (compile_expr1(arg, cctx, ppconst) == FAIL)
5318 	    return FAIL;
5319 
5320 	if (!has_const_expr)
5321 	{
5322 	    generate_ppconst(cctx, ppconst);
5323 
5324 	    if (!op_falsy)
5325 	    {
5326 		// remember the type and drop it
5327 		--stack->ga_len;
5328 		type1 = ((type_T **)stack->ga_data)[stack->ga_len];
5329 
5330 		end_idx = instr->ga_len;
5331 		generate_JUMP(cctx, JUMP_ALWAYS, 0);
5332 
5333 		// jump here from JUMP_IF_FALSE
5334 		isn = ((isn_T *)instr->ga_data) + alt_idx;
5335 		isn->isn_arg.jump.jump_where = instr->ga_len;
5336 	    }
5337 	}
5338 
5339 	if (!op_falsy)
5340 	{
5341 	    // Check for the ":".
5342 	    p = may_peek_next_line(cctx, *arg, &next);
5343 	    if (*p != ':')
5344 	    {
5345 		emsg(_(e_missing_colon));
5346 		return FAIL;
5347 	    }
5348 	    if (next != NULL)
5349 	    {
5350 		*arg = next_line_from_context(cctx, TRUE);
5351 		p = skipwhite(*arg);
5352 	    }
5353 
5354 	    if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5355 	    {
5356 		semsg(_(e_white_space_required_before_and_after_str_at_str),
5357 								       ":", p);
5358 		return FAIL;
5359 	    }
5360 
5361 	    // evaluate the third expression
5362 	    if (has_const_expr)
5363 		cctx->ctx_skip = save_skip == SKIP_YES || const_value
5364 							 ? SKIP_YES : SKIP_NOT;
5365 	    if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
5366 		return FAIL;
5367 	    if (compile_expr1(arg, cctx, ppconst) == FAIL)
5368 		return FAIL;
5369 	}
5370 
5371 	if (!has_const_expr)
5372 	{
5373 	    type_T	**typep;
5374 
5375 	    generate_ppconst(cctx, ppconst);
5376 
5377 	    // If the types differ, the result has a more generic type.
5378 	    typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5379 	    common_type(type1, *typep, typep, cctx->ctx_type_list);
5380 
5381 	    // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
5382 	    isn = ((isn_T *)instr->ga_data) + end_idx;
5383 	    isn->isn_arg.jump.jump_where = instr->ga_len;
5384 	}
5385 
5386 	cctx->ctx_skip = save_skip;
5387     }
5388     return OK;
5389 }
5390 
5391 /*
5392  * Toplevel expression.
5393  * Sets "is_const" (if not NULL) to indicate the value is a constant.
5394  * Returns OK or FAIL.
5395  */
5396     static int
5397 compile_expr0_ext(char_u **arg,  cctx_T *cctx, int *is_const)
5398 {
5399     ppconst_T	ppconst;
5400 
5401     CLEAR_FIELD(ppconst);
5402     if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5403     {
5404 	clear_ppconst(&ppconst);
5405 	return FAIL;
5406     }
5407     if (is_const != NULL)
5408 	*is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
5409     if (generate_ppconst(cctx, &ppconst) == FAIL)
5410 	return FAIL;
5411     return OK;
5412 }
5413 
5414 /*
5415  * Toplevel expression.
5416  */
5417     static int
5418 compile_expr0(char_u **arg,  cctx_T *cctx)
5419 {
5420     return compile_expr0_ext(arg, cctx, NULL);
5421 }
5422 
5423 /*
5424  * Compile "return [expr]".
5425  * When "legacy" is TRUE evaluate [expr] with legacy syntax
5426  */
5427     static char_u *
5428 compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
5429 {
5430     char_u	*p = arg;
5431     garray_T	*stack = &cctx->ctx_type_stack;
5432     type_T	*stack_type;
5433 
5434     if (*p != NUL && *p != '|' && *p != '\n')
5435     {
5436 	if (legacy)
5437 	{
5438 	    int save_flags = cmdmod.cmod_flags;
5439 
5440 	    generate_LEGACY_EVAL(cctx, p);
5441 	    if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
5442 						0, cctx, FALSE, FALSE) == FAIL)
5443 		return NULL;
5444 	    cmdmod.cmod_flags |= CMOD_LEGACY;
5445 	    (void)skip_expr(&p, NULL);
5446 	    cmdmod.cmod_flags = save_flags;
5447 	}
5448 	else
5449 	{
5450 	    // compile return argument into instructions
5451 	    if (compile_expr0(&p, cctx) == FAIL)
5452 		return NULL;
5453 	}
5454 
5455 	if (cctx->ctx_skip != SKIP_YES)
5456 	{
5457 	    // "check_return_type" with uf_ret_type set to &t_unknown is used
5458 	    // for an inline function without a specified return type.  Set the
5459 	    // return type here.
5460 	    stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5461 	    if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
5462 				|| cctx->ctx_ufunc->uf_ret_type == &t_unknown
5463 				|| cctx->ctx_ufunc->uf_ret_type == &t_any))
5464 		    || (!check_return_type
5465 				&& cctx->ctx_ufunc->uf_ret_type == &t_unknown))
5466 	    {
5467 		cctx->ctx_ufunc->uf_ret_type = stack_type;
5468 	    }
5469 	    else
5470 	    {
5471 		if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5472 			&& stack_type->tt_type != VAR_VOID
5473 			&& stack_type->tt_type != VAR_UNKNOWN)
5474 		{
5475 		    emsg(_(e_returning_value_in_function_without_return_type));
5476 		    return NULL;
5477 		}
5478 		if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
5479 						0, cctx, FALSE, FALSE) == FAIL)
5480 		    return NULL;
5481 	    }
5482 	}
5483     }
5484     else
5485     {
5486 	// "check_return_type" cannot be TRUE, only used for a lambda which
5487 	// always has an argument.
5488 	if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5489 		&& cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
5490 	{
5491 	    emsg(_(e_missing_return_value));
5492 	    return NULL;
5493 	}
5494 
5495 	// No argument, return zero.
5496 	generate_PUSHNR(cctx, 0);
5497     }
5498 
5499     // Undo any command modifiers.
5500     generate_undo_cmdmods(cctx);
5501 
5502     if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
5503 	return NULL;
5504 
5505     // "return val | endif" is possible
5506     return skipwhite(p);
5507 }
5508 
5509 /*
5510  * Get a line from the compilation context, compatible with exarg_T getline().
5511  * Return a pointer to the line in allocated memory.
5512  * Return NULL for end-of-file or some error.
5513  */
5514     static char_u *
5515 exarg_getline(
5516 	int c UNUSED,
5517 	void *cookie,
5518 	int indent UNUSED,
5519 	getline_opt_T options UNUSED)
5520 {
5521     cctx_T  *cctx = (cctx_T *)cookie;
5522     char_u  *p;
5523 
5524     for (;;)
5525     {
5526 	if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
5527 	    return NULL;
5528 	++cctx->ctx_lnum;
5529 	p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5530 	// Comment lines result in NULL pointers, skip them.
5531 	if (p != NULL)
5532 	    return vim_strsave(p);
5533     }
5534 }
5535 
5536     void
5537 fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5538 {
5539     eap->getline = exarg_getline;
5540     eap->cookie = cctx;
5541 }
5542 
5543 /*
5544  * Compile a nested :def command.
5545  */
5546     static char_u *
5547 compile_nested_function(exarg_T *eap, cctx_T *cctx)
5548 {
5549     int		is_global = *eap->arg == 'g' && eap->arg[1] == ':';
5550     char_u	*name_start = eap->arg;
5551     char_u	*name_end = to_name_end(eap->arg, TRUE);
5552     char_u	*lambda_name;
5553     ufunc_T	*ufunc;
5554     int		r = FAIL;
5555 
5556     if (eap->forceit)
5557     {
5558 	emsg(_(e_cannot_use_bang_with_nested_def));
5559 	return NULL;
5560     }
5561 
5562     if (*name_start == '/')
5563     {
5564 	name_end = skip_regexp(name_start + 1, '/', TRUE);
5565 	if (*name_end == '/')
5566 	    ++name_end;
5567 	eap->nextcmd = check_nextcmd(name_end);
5568     }
5569     if (name_end == name_start || *skipwhite(name_end) != '(')
5570     {
5571 	if (!ends_excmd2(name_start, name_end))
5572 	{
5573 	    semsg(_(e_invalid_command_str), eap->cmd);
5574 	    return NULL;
5575 	}
5576 
5577 	// "def" or "def Name": list functions
5578 	if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5579 	    return NULL;
5580 	return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5581     }
5582 
5583     // Only g:Func() can use a namespace.
5584     if (name_start[1] == ':' && !is_global)
5585     {
5586 	semsg(_(e_namespace_not_supported_str), name_start);
5587 	return NULL;
5588     }
5589     if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
5590 	return NULL;
5591 
5592     eap->arg = name_end;
5593     fill_exarg_from_cctx(eap, cctx);
5594 
5595     eap->forceit = FALSE;
5596     lambda_name = vim_strsave(get_lambda_name());
5597     if (lambda_name == NULL)
5598 	return NULL;
5599     ufunc = define_function(eap, lambda_name);
5600 
5601     if (ufunc == NULL)
5602     {
5603 	r = eap->skip ? OK : FAIL;
5604 	goto theend;
5605     }
5606 
5607     // copy over the block scope IDs before compiling
5608     if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5609     {
5610 	int block_depth = cctx->ctx_ufunc->uf_block_depth;
5611 
5612 	ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5613 	if (ufunc->uf_block_ids != NULL)
5614 	{
5615 	    mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5616 						    sizeof(int) * block_depth);
5617 	    ufunc->uf_block_depth = block_depth;
5618 	}
5619     }
5620 
5621     if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
5622 	    && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), cctx)
5623 								       == FAIL)
5624     {
5625 	func_ptr_unref(ufunc);
5626 	goto theend;
5627     }
5628 
5629     if (is_global)
5630     {
5631 	char_u *func_name = vim_strnsave(name_start + 2,
5632 						    name_end - name_start - 2);
5633 
5634 	if (func_name == NULL)
5635 	    r = FAIL;
5636 	else
5637 	{
5638 	    r = generate_NEWFUNC(cctx, lambda_name, func_name);
5639 	    lambda_name = NULL;
5640 	}
5641     }
5642     else
5643     {
5644 	// Define a local variable for the function reference.
5645 	lvar_T	*lvar = reserve_local(cctx, name_start, name_end - name_start,
5646 						    TRUE, ufunc->uf_func_type);
5647 
5648 	if (lvar == NULL)
5649 	    goto theend;
5650 	if (generate_FUNCREF(cctx, ufunc) == FAIL)
5651 	    goto theend;
5652 	r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5653     }
5654     // TODO: warning for trailing text?
5655 
5656 theend:
5657     vim_free(lambda_name);
5658     return r == FAIL ? NULL : (char_u *)"";
5659 }
5660 
5661 /*
5662  * Return the length of an assignment operator, or zero if there isn't one.
5663  */
5664     int
5665 assignment_len(char_u *p, int *heredoc)
5666 {
5667     if (*p == '=')
5668     {
5669 	if (p[1] == '<' && p[2] == '<')
5670 	{
5671 	    *heredoc = TRUE;
5672 	    return 3;
5673 	}
5674 	return 1;
5675     }
5676     if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5677 	return 2;
5678     if (STRNCMP(p, "..=", 3) == 0)
5679 	return 3;
5680     return 0;
5681 }
5682 
5683 /*
5684  * Generate the load instruction for "name".
5685  */
5686     static void
5687 generate_loadvar(
5688 	cctx_T		*cctx,
5689 	assign_dest_T	dest,
5690 	char_u		*name,
5691 	lvar_T		*lvar,
5692 	type_T		*type)
5693 {
5694     switch (dest)
5695     {
5696 	case dest_option:
5697 	    // TODO: check the option exists
5698 	    generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5699 	    break;
5700 	case dest_global:
5701 	    if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5702 		generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5703 	    else
5704 		generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
5705 	    break;
5706 	case dest_buffer:
5707 	    generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5708 	    break;
5709 	case dest_window:
5710 	    generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5711 	    break;
5712 	case dest_tab:
5713 	    generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5714 	    break;
5715 	case dest_script:
5716 	    compile_load_scriptvar(cctx,
5717 		    name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5718 	    break;
5719 	case dest_env:
5720 	    // Include $ in the name here
5721 	    generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5722 	    break;
5723 	case dest_reg:
5724 	    generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5725 	    break;
5726 	case dest_vimvar:
5727 	    generate_LOADV(cctx, name + 2, TRUE);
5728 	    break;
5729 	case dest_local:
5730 	    if (lvar->lv_from_outer > 0)
5731 		generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5732 									 type);
5733 	    else
5734 		generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5735 	    break;
5736 	case dest_expr:
5737 	    // list or dict value should already be on the stack.
5738 	    break;
5739     }
5740 }
5741 
5742 /*
5743  * Skip over "[expr]" or ".member".
5744  * Does not check for any errors.
5745  */
5746     static char_u *
5747 skip_index(char_u *start)
5748 {
5749     char_u *p = start;
5750 
5751     if (*p == '[')
5752     {
5753 	p = skipwhite(p + 1);
5754 	(void)skip_expr(&p, NULL);
5755 	p = skipwhite(p);
5756 	if (*p == ']')
5757 	    return p + 1;
5758 	return p;
5759     }
5760     // if (*p == '.')
5761     return to_name_end(p + 1, TRUE);
5762 }
5763 
5764     void
5765 vim9_declare_error(char_u *name)
5766 {
5767     char *scope = "";
5768 
5769     switch (*name)
5770     {
5771 	case 'g': scope = _("global"); break;
5772 	case 'b': scope = _("buffer"); break;
5773 	case 'w': scope = _("window"); break;
5774 	case 't': scope = _("tab"); break;
5775 	case 'v': scope = "v:"; break;
5776 	case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
5777 		  return;
5778 	case '&': semsg(_(e_cannot_declare_an_option), name);
5779 		  return;
5780 	case '@': semsg(_(e_cannot_declare_a_register_str), name);
5781 		  return;
5782 	default: return;
5783     }
5784     semsg(_(e_cannot_declare_a_scope_variable), scope, name);
5785 }
5786 
5787 /*
5788  * For one assignment figure out the type of destination.  Return it in "dest".
5789  * When not recognized "dest" is not set.
5790  * For an option "opt_flags" is set.
5791  * For a v:var "vimvaridx" is set.
5792  * "type" is set to the destination type if known, unchanted otherwise.
5793  * Return FAIL if an error message was given.
5794  */
5795     static int
5796 get_var_dest(
5797 	char_u		*name,
5798 	assign_dest_T	*dest,
5799 	int		cmdidx,
5800 	int		*opt_flags,
5801 	int		*vimvaridx,
5802 	type_T		**type,
5803 	cctx_T		*cctx)
5804 {
5805     char_u *p;
5806 
5807     if (*name == '&')
5808     {
5809 	int		cc;
5810 	long		numval;
5811 	getoption_T	opt_type;
5812 
5813 	*dest = dest_option;
5814 	if (cmdidx == CMD_final || cmdidx == CMD_const)
5815 	{
5816 	    emsg(_(e_const_option));
5817 	    return FAIL;
5818 	}
5819 	p = name;
5820 	p = find_option_end(&p, opt_flags);
5821 	if (p == NULL)
5822 	{
5823 	    // cannot happen?
5824 	    emsg(_(e_unexpected_characters_in_assignment));
5825 	    return FAIL;
5826 	}
5827 	cc = *p;
5828 	*p = NUL;
5829 	opt_type = get_option_value(skip_option_env_lead(name),
5830 						    &numval, NULL, *opt_flags);
5831 	*p = cc;
5832 	switch (opt_type)
5833 	{
5834 	    case gov_unknown:
5835 		    semsg(_(e_unknown_option), name);
5836 		    return FAIL;
5837 	    case gov_string:
5838 	    case gov_hidden_string:
5839 		    *type = &t_string;
5840 		    break;
5841 	    case gov_bool:
5842 	    case gov_hidden_bool:
5843 		    *type = &t_bool;
5844 		    break;
5845 	    case gov_number:
5846 	    case gov_hidden_number:
5847 		    *type = &t_number;
5848 		    break;
5849 	}
5850     }
5851     else if (*name == '$')
5852     {
5853 	*dest = dest_env;
5854 	*type = &t_string;
5855     }
5856     else if (*name == '@')
5857     {
5858 	if (name[1] != '@'
5859 			&& (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
5860 	{
5861 	    emsg_invreg(name[1]);
5862 	    return FAIL;
5863 	}
5864 	*dest = dest_reg;
5865 	*type = name[1] == '#' ? &t_number_or_string : &t_string;
5866     }
5867     else if (STRNCMP(name, "g:", 2) == 0)
5868     {
5869 	*dest = dest_global;
5870     }
5871     else if (STRNCMP(name, "b:", 2) == 0)
5872     {
5873 	*dest = dest_buffer;
5874     }
5875     else if (STRNCMP(name, "w:", 2) == 0)
5876     {
5877 	*dest = dest_window;
5878     }
5879     else if (STRNCMP(name, "t:", 2) == 0)
5880     {
5881 	*dest = dest_tab;
5882     }
5883     else if (STRNCMP(name, "v:", 2) == 0)
5884     {
5885 	typval_T	*vtv;
5886 	int		di_flags;
5887 
5888 	*vimvaridx = find_vim_var(name + 2, &di_flags);
5889 	if (*vimvaridx < 0)
5890 	{
5891 	    semsg(_(e_variable_not_found_str), name);
5892 	    return FAIL;
5893 	}
5894 	// We use the current value of "sandbox" here, is that OK?
5895 	if (var_check_ro(di_flags, name, FALSE))
5896 	    return FAIL;
5897 	*dest = dest_vimvar;
5898 	vtv = get_vim_var_tv(*vimvaridx);
5899 	*type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5900     }
5901     return OK;
5902 }
5903 
5904 /*
5905  * Generate a STORE instruction for "dest", not being "dest_local".
5906  * Return FAIL when out of memory.
5907  */
5908     static int
5909 generate_store_var(
5910 	cctx_T		*cctx,
5911 	assign_dest_T	dest,
5912 	int		opt_flags,
5913 	int		vimvaridx,
5914 	int		scriptvar_idx,
5915 	int		scriptvar_sid,
5916 	type_T		*type,
5917 	char_u		*name)
5918 {
5919     switch (dest)
5920     {
5921 	case dest_option:
5922 	    return generate_STOREOPT(cctx, skip_option_env_lead(name),
5923 								    opt_flags);
5924 	case dest_global:
5925 	    // include g: with the name, easier to execute that way
5926 	    return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5927 					? ISN_STOREG : ISN_STOREAUTO, 0, name);
5928 	case dest_buffer:
5929 	    // include b: with the name, easier to execute that way
5930 	    return generate_STORE(cctx, ISN_STOREB, 0, name);
5931 	case dest_window:
5932 	    // include w: with the name, easier to execute that way
5933 	    return generate_STORE(cctx, ISN_STOREW, 0, name);
5934 	case dest_tab:
5935 	    // include t: with the name, easier to execute that way
5936 	    return generate_STORE(cctx, ISN_STORET, 0, name);
5937 	case dest_env:
5938 	    return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5939 	case dest_reg:
5940 	    return generate_STORE(cctx, ISN_STOREREG,
5941 					 name[1] == '@' ? '"' : name[1], NULL);
5942 	case dest_vimvar:
5943 	    return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5944 	case dest_script:
5945 	    if (scriptvar_idx < 0)
5946 		// "s:" may be included in the name.
5947 		return generate_OLDSCRIPT(cctx, ISN_STORES, name,
5948 							  scriptvar_sid, type);
5949 	    return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5950 					   scriptvar_sid, scriptvar_idx, type);
5951 	case dest_local:
5952 	case dest_expr:
5953 	    // cannot happen
5954 	    break;
5955     }
5956     return FAIL;
5957 }
5958 
5959     static int
5960 generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
5961 {
5962     if (lhs->lhs_dest != dest_local)
5963 	return generate_store_var(cctx, lhs->lhs_dest,
5964 			    lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
5965 			    lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
5966 			    lhs->lhs_type, lhs->lhs_name);
5967 
5968     if (lhs->lhs_lvar != NULL)
5969     {
5970 	garray_T	*instr = &cctx->ctx_instr;
5971 	isn_T		*isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
5972 
5973 	// optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
5974 	// ISN_STORENR
5975 	if (lhs->lhs_lvar->lv_from_outer == 0
5976 		&& instr->ga_len == instr_count + 1
5977 		&& isn->isn_type == ISN_PUSHNR)
5978 	{
5979 	    varnumber_T val = isn->isn_arg.number;
5980 	    garray_T    *stack = &cctx->ctx_type_stack;
5981 
5982 	    isn->isn_type = ISN_STORENR;
5983 	    isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
5984 	    isn->isn_arg.storenr.stnr_val = val;
5985 	    if (stack->ga_len > 0)
5986 		--stack->ga_len;
5987 	}
5988 	else if (lhs->lhs_lvar->lv_from_outer > 0)
5989 	    generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
5990 						 lhs->lhs_lvar->lv_from_outer);
5991 	else
5992 	    generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
5993     }
5994     return OK;
5995 }
5996 
5997     static int
5998 is_decl_command(int cmdidx)
5999 {
6000     return cmdidx == CMD_let || cmdidx == CMD_var
6001 				 || cmdidx == CMD_final || cmdidx == CMD_const;
6002 }
6003 
6004 /*
6005  * Figure out the LHS type and other properties for an assignment or one item
6006  * of ":unlet" with an index.
6007  * Returns OK or FAIL.
6008  */
6009     static int
6010 compile_lhs(
6011 	char_u	*var_start,
6012 	lhs_T	*lhs,
6013 	int	cmdidx,
6014 	int	heredoc,
6015 	int	oplen,
6016 	cctx_T	*cctx)
6017 {
6018     char_u	*var_end;
6019     int		is_decl = is_decl_command(cmdidx);
6020 
6021     CLEAR_POINTER(lhs);
6022     lhs->lhs_dest = dest_local;
6023     lhs->lhs_vimvaridx = -1;
6024     lhs->lhs_scriptvar_idx = -1;
6025 
6026     // "dest_end" is the end of the destination, including "[expr]" or
6027     // ".name".
6028     // "var_end" is the end of the variable/option/etc. name.
6029     lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
6030     if (*var_start == '@')
6031 	var_end = var_start + 2;
6032     else
6033     {
6034 	// skip over the leading "&", "&l:", "&g:" and "$"
6035 	var_end = skip_option_env_lead(var_start);
6036 	var_end = to_name_end(var_end, TRUE);
6037     }
6038 
6039     // "a: type" is declaring variable "a" with a type, not dict "a:".
6040     if (is_decl && lhs->lhs_dest_end == var_start + 2
6041 					       && lhs->lhs_dest_end[-1] == ':')
6042 	--lhs->lhs_dest_end;
6043     if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
6044 	--var_end;
6045 
6046     // compute the length of the destination without "[expr]" or ".name"
6047     lhs->lhs_varlen = var_end - var_start;
6048     lhs->lhs_varlen_total = lhs->lhs_varlen;
6049     lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
6050     if (lhs->lhs_name == NULL)
6051 	return FAIL;
6052 
6053     if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
6054 	// Something follows after the variable: "var[idx]" or "var.key".
6055 	lhs->lhs_has_index = TRUE;
6056 
6057     if (heredoc)
6058 	lhs->lhs_type = &t_list_string;
6059     else
6060 	lhs->lhs_type = &t_any;
6061 
6062     if (cctx->ctx_skip != SKIP_YES)
6063     {
6064 	int	    declare_error = FALSE;
6065 
6066 	if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
6067 				      &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
6068 						 &lhs->lhs_type, cctx) == FAIL)
6069 	    return FAIL;
6070 	if (lhs->lhs_dest != dest_local
6071 				 && cmdidx != CMD_const && cmdidx != CMD_final)
6072 	{
6073 	    // Specific kind of variable recognized.
6074 	    declare_error = is_decl;
6075 	}
6076 	else
6077 	{
6078 	    // No specific kind of variable recognized, just a name.
6079 	    if (check_reserved_name(lhs->lhs_name) == FAIL)
6080 		return FAIL;
6081 
6082 	    if (lookup_local(var_start, lhs->lhs_varlen,
6083 					     &lhs->lhs_local_lvar, cctx) == OK)
6084 		lhs->lhs_lvar = &lhs->lhs_local_lvar;
6085 	    else
6086 	    {
6087 		CLEAR_FIELD(lhs->lhs_arg_lvar);
6088 		if (arg_exists(var_start, lhs->lhs_varlen,
6089 			 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
6090 			    &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
6091 		{
6092 		    if (is_decl)
6093 		    {
6094 			semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
6095 			return FAIL;
6096 		    }
6097 		    lhs->lhs_lvar = &lhs->lhs_arg_lvar;
6098 		}
6099 	    }
6100 	    if (lhs->lhs_lvar != NULL)
6101 	    {
6102 		if (is_decl)
6103 		{
6104 		    semsg(_(e_variable_already_declared), lhs->lhs_name);
6105 		    return FAIL;
6106 		}
6107 	    }
6108 	    else
6109 	    {
6110 		int script_namespace = lhs->lhs_varlen > 1
6111 				       && STRNCMP(var_start, "s:", 2) == 0;
6112 		int script_var = (script_namespace
6113 			? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
6114 									  cctx)
6115 			  : script_var_exists(var_start, lhs->lhs_varlen,
6116 								  cctx)) == OK;
6117 		imported_T  *import =
6118 			       find_imported(var_start, lhs->lhs_varlen, cctx);
6119 
6120 		if (script_namespace || script_var || import != NULL)
6121 		{
6122 		    char_u	*rawname = lhs->lhs_name
6123 					   + (lhs->lhs_name[1] == ':' ? 2 : 0);
6124 
6125 		    if (is_decl)
6126 		    {
6127 			if (script_namespace)
6128 			    semsg(_(e_cannot_declare_script_variable_in_function),
6129 								lhs->lhs_name);
6130 			else
6131 			    semsg(_(e_variable_already_declared_in_script_str),
6132 								lhs->lhs_name);
6133 			return FAIL;
6134 		    }
6135 		    else if (cctx->ctx_ufunc->uf_script_ctx_version
6136 							 == SCRIPT_VERSION_VIM9
6137 				    && script_namespace
6138 				    && !script_var && import == NULL)
6139 		    {
6140 			semsg(_(e_unknown_variable_str), lhs->lhs_name);
6141 			return FAIL;
6142 		    }
6143 
6144 		    lhs->lhs_dest = dest_script;
6145 
6146 		    // existing script-local variables should have a type
6147 		    lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
6148 		    if (import != NULL)
6149 			lhs->lhs_scriptvar_sid = import->imp_sid;
6150 		    if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
6151 		    {
6152 			// Check writable only when no index follows.
6153 			lhs->lhs_scriptvar_idx = get_script_item_idx(
6154 					       lhs->lhs_scriptvar_sid, rawname,
6155 			      lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
6156 									 cctx);
6157 			if (lhs->lhs_scriptvar_idx >= 0)
6158 			{
6159 			    scriptitem_T *si = SCRIPT_ITEM(
6160 						       lhs->lhs_scriptvar_sid);
6161 			    svar_T	 *sv =
6162 					    ((svar_T *)si->sn_var_vals.ga_data)
6163 						      + lhs->lhs_scriptvar_idx;
6164 			    lhs->lhs_type = sv->sv_type;
6165 			}
6166 		    }
6167 		}
6168 		else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
6169 								       == FAIL)
6170 		    return FAIL;
6171 	    }
6172 	}
6173 
6174 	if (declare_error)
6175 	{
6176 	    vim9_declare_error(lhs->lhs_name);
6177 	    return FAIL;
6178 	}
6179     }
6180 
6181     // handle "a:name" as a name, not index "name" on "a"
6182     if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
6183 	var_end = lhs->lhs_dest_end;
6184 
6185     if (lhs->lhs_dest != dest_option)
6186     {
6187 	if (is_decl && *var_end == ':')
6188 	{
6189 	    char_u *p;
6190 
6191 	    // parse optional type: "let var: type = expr"
6192 	    if (!VIM_ISWHITE(var_end[1]))
6193 	    {
6194 		semsg(_(e_white_space_required_after_str_str), ":", var_end);
6195 		return FAIL;
6196 	    }
6197 	    p = skipwhite(var_end + 1);
6198 	    lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
6199 	    if (lhs->lhs_type == NULL)
6200 		return FAIL;
6201 	    lhs->lhs_has_type = TRUE;
6202 	}
6203 	else if (lhs->lhs_lvar != NULL)
6204 	    lhs->lhs_type = lhs->lhs_lvar->lv_type;
6205     }
6206 
6207     if (oplen == 3 && !heredoc
6208 		   && lhs->lhs_dest != dest_global
6209 		   && !lhs->lhs_has_index
6210 		   && lhs->lhs_type->tt_type != VAR_STRING
6211 		   && lhs->lhs_type->tt_type != VAR_ANY)
6212     {
6213 	emsg(_(e_can_only_concatenate_to_string));
6214 	return FAIL;
6215     }
6216 
6217     if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6218 						 && cctx->ctx_skip != SKIP_YES)
6219     {
6220 	if (oplen > 1 && !heredoc)
6221 	{
6222 	    // +=, /=, etc. require an existing variable
6223 	    semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6224 	    return FAIL;
6225 	}
6226 	if (!is_decl)
6227 	{
6228 	    semsg(_(e_unknown_variable_str), lhs->lhs_name);
6229 	    return FAIL;
6230 	}
6231 
6232 	// Check the name is valid for a funcref.
6233 	if ((lhs->lhs_type->tt_type == VAR_FUNC
6234 				      || lhs->lhs_type->tt_type == VAR_PARTIAL)
6235 		&& var_wrong_func_name(lhs->lhs_name, TRUE))
6236 	    return FAIL;
6237 
6238 	// New local variable.
6239 	lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6240 		    cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6241 	if (lhs->lhs_lvar == NULL)
6242 	    return FAIL;
6243 	lhs->lhs_new_local = TRUE;
6244     }
6245 
6246     lhs->lhs_member_type = lhs->lhs_type;
6247     if (lhs->lhs_has_index)
6248     {
6249 	// Something follows after the variable: "var[idx]" or "var.key".
6250 	// TODO: should we also handle "->func()" here?
6251 	if (is_decl)
6252 	{
6253 	    emsg(_(e_cannot_use_index_when_declaring_variable));
6254 	    return FAIL;
6255 	}
6256 
6257 	if (var_start[lhs->lhs_varlen] == '['
6258 					  || var_start[lhs->lhs_varlen] == '.')
6259 	{
6260 	    char_u	*after = var_start + lhs->lhs_varlen;
6261 	    char_u	*p;
6262 
6263 	    // Only the last index is used below, if there are others
6264 	    // before it generate code for the expression.  Thus for
6265 	    // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6266 	    for (;;)
6267 	    {
6268 		p = skip_index(after);
6269 		if (*p != '[' && *p != '.')
6270 		{
6271 		    lhs->lhs_varlen_total = p - var_start;
6272 		    break;
6273 		}
6274 		after = p;
6275 	    }
6276 	    if (after > var_start + lhs->lhs_varlen)
6277 	    {
6278 		lhs->lhs_varlen = after - var_start;
6279 		lhs->lhs_dest = dest_expr;
6280 		// We don't know the type before evaluating the expression,
6281 		// use "any" until then.
6282 		lhs->lhs_type = &t_any;
6283 	    }
6284 
6285 	    if (lhs->lhs_type->tt_member == NULL)
6286 		lhs->lhs_member_type = &t_any;
6287 	    else
6288 		lhs->lhs_member_type = lhs->lhs_type->tt_member;
6289 	}
6290 	else
6291 	{
6292 	    semsg("Not supported yet: %s", var_start);
6293 	    return FAIL;
6294 	}
6295     }
6296     return OK;
6297 }
6298 
6299 /*
6300  * Figure out the LHS and check a few errors.
6301  */
6302     static int
6303 compile_assign_lhs(
6304 	char_u	*var_start,
6305 	lhs_T	*lhs,
6306 	int	cmdidx,
6307 	int	is_decl,
6308 	int	heredoc,
6309 	int	oplen,
6310 	cctx_T	*cctx)
6311 {
6312     if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6313 	return FAIL;
6314 
6315     if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
6316     {
6317 	semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
6318 	return FAIL;
6319     }
6320     if (!is_decl && lhs->lhs_lvar != NULL
6321 			   && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
6322     {
6323 	semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
6324 	return FAIL;
6325     }
6326     return OK;
6327 }
6328 
6329 /*
6330  * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6331  * "var.key".
6332  */
6333     static int
6334 compile_assign_index(
6335 	char_u	*var_start,
6336 	lhs_T	*lhs,
6337 	int	*range,
6338 	cctx_T	*cctx)
6339 {
6340     size_t	varlen = lhs->lhs_varlen;
6341     char_u	*p;
6342     int		r = OK;
6343     int		need_white_before = TRUE;
6344     int		empty_second;
6345 
6346     p = var_start + varlen;
6347     if (*p == '[')
6348     {
6349 	p = skipwhite(p + 1);
6350 	if (*p == ':')
6351 	{
6352 	    // empty first index, push zero
6353 	    r = generate_PUSHNR(cctx, 0);
6354 	    need_white_before = FALSE;
6355 	}
6356 	else
6357 	    r = compile_expr0(&p, cctx);
6358 
6359 	if (r == OK && *skipwhite(p) == ':')
6360 	{
6361 	    // unlet var[idx : idx]
6362 	    // blob[idx : idx] = value
6363 	    *range = TRUE;
6364 	    p = skipwhite(p);
6365 	    empty_second = *skipwhite(p + 1) == ']';
6366 	    if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6367 		    || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
6368 	    {
6369 		semsg(_(e_white_space_required_before_and_after_str_at_str),
6370 								      ":", p);
6371 		return FAIL;
6372 	    }
6373 	    p = skipwhite(p + 1);
6374 	    if (*p == ']')
6375 		// empty second index, push "none"
6376 		r = generate_PUSHSPEC(cctx, VVAL_NONE);
6377 	    else
6378 		r = compile_expr0(&p, cctx);
6379 	}
6380 
6381 	if (r == OK && *skipwhite(p) != ']')
6382 	{
6383 	    // this should not happen
6384 	    emsg(_(e_missbrac));
6385 	    r = FAIL;
6386 	}
6387     }
6388     else // if (*p == '.')
6389     {
6390 	char_u *key_end = to_name_end(p + 1, TRUE);
6391 	char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6392 
6393 	r = generate_PUSHS(cctx, key);
6394     }
6395     return r;
6396 }
6397 
6398 /*
6399  * For a LHS with an index, load the variable to be indexed.
6400  */
6401     static int
6402 compile_load_lhs(
6403 	lhs_T	*lhs,
6404 	char_u	*var_start,
6405 	type_T	*rhs_type,
6406 	cctx_T	*cctx)
6407 {
6408     if (lhs->lhs_dest == dest_expr)
6409     {
6410 	size_t	    varlen = lhs->lhs_varlen;
6411 	int	    c = var_start[varlen];
6412 	char_u	    *p = var_start;
6413 	garray_T    *stack = &cctx->ctx_type_stack;
6414 
6415 	// Evaluate "ll[expr]" of "ll[expr][idx]"
6416 	var_start[varlen] = NUL;
6417 	if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6418 	{
6419 	    // this should not happen
6420 	    emsg(_(e_missbrac));
6421 	    var_start[varlen] = c;
6422 	    return FAIL;
6423 	}
6424 	var_start[varlen] = c;
6425 
6426 	lhs->lhs_type = stack->ga_len == 0 ? &t_void
6427 			      : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6428 	// now we can properly check the type
6429 	if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6430 		&& rhs_type != &t_void
6431 		&& need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6432 							 FALSE, FALSE) == FAIL)
6433 	    return FAIL;
6434     }
6435     else
6436 	generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6437 						 lhs->lhs_lvar, lhs->lhs_type);
6438     return OK;
6439 }
6440 
6441 /*
6442  * Produce code for loading "lhs" and also take care of an index.
6443  * Return OK/FAIL.
6444  */
6445     static int
6446 compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
6447 {
6448     compile_load_lhs(lhs, var_start, NULL, cctx);
6449 
6450     if (lhs->lhs_has_index)
6451     {
6452 	int range = FALSE;
6453 
6454 	// Get member from list or dict.  First compile the
6455 	// index value.
6456 	if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6457 	    return FAIL;
6458 	if (range)
6459 	{
6460 	    semsg(_(e_cannot_use_range_with_assignment_operator_str),
6461 								    var_start);
6462 	    return FAIL;
6463 	}
6464 
6465 	// Get the member.
6466 	if (compile_member(FALSE, cctx) == FAIL)
6467 	    return FAIL;
6468     }
6469     return OK;
6470 }
6471 
6472 /*
6473  * Assignment to a list or dict member, or ":unlet" for the item, using the
6474  * information in "lhs".
6475  * Returns OK or FAIL.
6476  */
6477     static int
6478 compile_assign_unlet(
6479 	char_u	*var_start,
6480 	lhs_T	*lhs,
6481 	int	is_assign,
6482 	type_T	*rhs_type,
6483 	cctx_T	*cctx)
6484 {
6485     vartype_T	dest_type;
6486     garray_T    *stack = &cctx->ctx_type_stack;
6487     int		range = FALSE;
6488 
6489     if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
6490 	return FAIL;
6491     if (is_assign && range && lhs->lhs_type != &t_blob
6492 						    && lhs->lhs_type != &t_any)
6493     {
6494 	semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6495 	return FAIL;
6496     }
6497 
6498     if (lhs->lhs_type == &t_any)
6499     {
6500 	// Index on variable of unknown type: check at runtime.
6501 	dest_type = VAR_ANY;
6502     }
6503     else
6504     {
6505 	dest_type = lhs->lhs_type->tt_type;
6506 	if (dest_type == VAR_DICT && range)
6507 	{
6508 	    emsg(e_cannot_use_range_with_dictionary);
6509 	    return FAIL;
6510 	}
6511 	if (dest_type == VAR_DICT
6512 			      && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
6513 	    return FAIL;
6514 	if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
6515 	{
6516 	    type_T *type;
6517 
6518 	    if (range)
6519 	    {
6520 		type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6521 		if (need_type(type, &t_number,
6522 					    -1, 0, cctx, FALSE, FALSE) == FAIL)
6523 		return FAIL;
6524 	    }
6525 	    type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6526 	    if ((dest_type != VAR_BLOB || type != &t_special)
6527 		    && need_type(type, &t_number,
6528 					    -1, 0, cctx, FALSE, FALSE) == FAIL)
6529 		return FAIL;
6530 	}
6531     }
6532 
6533     // Load the dict or list.  On the stack we then have:
6534     // - value (for assignment, not for :unlet)
6535     // - index
6536     // - for [a : b] second index
6537     // - variable
6538     if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6539 	return FAIL;
6540 
6541     if (dest_type == VAR_LIST || dest_type == VAR_DICT
6542 			      || dest_type == VAR_BLOB || dest_type == VAR_ANY)
6543     {
6544 	if (is_assign)
6545 	{
6546 	    if (range)
6547 	    {
6548 		if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6549 		    return FAIL;
6550 	    }
6551 	    else
6552 	    {
6553 		isn_T	*isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6554 
6555 		if (isn == NULL)
6556 		    return FAIL;
6557 		isn->isn_arg.vartype = dest_type;
6558 	    }
6559 	}
6560 	else if (range)
6561 	{
6562 	    if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6563 		return FAIL;
6564 	}
6565 	else
6566 	{
6567 	    if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6568 		return FAIL;
6569 	}
6570     }
6571     else
6572     {
6573 	emsg(_(e_indexable_type_required));
6574 	return FAIL;
6575     }
6576 
6577     return OK;
6578 }
6579 
6580 /*
6581  * Compile declaration and assignment:
6582  * "let name"
6583  * "var name = expr"
6584  * "final name = expr"
6585  * "const name = expr"
6586  * "name = expr"
6587  * "arg" points to "name".
6588  * "++arg" and "--arg"
6589  * Return NULL for an error.
6590  * Return "arg" if it does not look like a variable list.
6591  */
6592     static char_u *
6593 compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6594 {
6595     char_u	*var_start;
6596     char_u	*p;
6597     char_u	*end = arg;
6598     char_u	*ret = NULL;
6599     int		var_count = 0;
6600     int		var_idx;
6601     int		semicolon = 0;
6602     int		did_generate_slice = FALSE;
6603     garray_T	*instr = &cctx->ctx_instr;
6604     garray_T    *stack = &cctx->ctx_type_stack;
6605     char_u	*op;
6606     int		oplen = 0;
6607     int		heredoc = FALSE;
6608     int		incdec = FALSE;
6609     type_T	*rhs_type = &t_any;
6610     char_u	*sp;
6611     int		is_decl = is_decl_command(cmdidx);
6612     lhs_T	lhs;
6613     long	start_lnum = SOURCING_LNUM;
6614 
6615     // Skip over the "var" or "[var, var]" to get to any "=".
6616     p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6617     if (p == NULL)
6618 	return *arg == '[' ? arg : NULL;
6619 
6620     if (var_count > 0 && is_decl)
6621     {
6622 	// TODO: should we allow this, and figure out type inference from list
6623 	// members?
6624 	emsg(_(e_cannot_use_list_for_declaration));
6625 	return NULL;
6626     }
6627     lhs.lhs_name = NULL;
6628 
6629     sp = p;
6630     p = skipwhite(p);
6631     op = p;
6632     oplen = assignment_len(p, &heredoc);
6633 
6634     if (var_count > 0 && oplen == 0)
6635 	// can be something like "[1, 2]->func()"
6636 	return arg;
6637 
6638     if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
6639     {
6640 	error_white_both(op, oplen);
6641 	return NULL;
6642     }
6643     if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
6644     {
6645 	if (VIM_ISWHITE(eap->cmd[2]))
6646 	{
6647 	    semsg(_(e_no_white_space_allowed_after_str_str),
6648 			 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
6649 	    return NULL;
6650 	}
6651 	op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
6652 	oplen = 2;
6653 	incdec = TRUE;
6654     }
6655 
6656     if (heredoc)
6657     {
6658 	list_T	   *l;
6659 	listitem_T *li;
6660 
6661 	// [let] varname =<< [trim] {end}
6662 	eap->getline = exarg_getline;
6663 	eap->cookie = cctx;
6664 	l = heredoc_get(eap, op + 3, FALSE);
6665 	if (l == NULL)
6666 	    return NULL;
6667 
6668 	if (cctx->ctx_skip != SKIP_YES)
6669 	{
6670 	    // Push each line and the create the list.
6671 	    FOR_ALL_LIST_ITEMS(l, li)
6672 	    {
6673 		generate_PUSHS(cctx, li->li_tv.vval.v_string);
6674 		li->li_tv.vval.v_string = NULL;
6675 	    }
6676 	    generate_NEWLIST(cctx, l->lv_len);
6677 	}
6678 	list_free(l);
6679 	p += STRLEN(p);
6680 	end = p;
6681     }
6682     else if (var_count > 0)
6683     {
6684 	char_u *wp;
6685 
6686 	// for "[var, var] = expr" evaluate the expression here, loop over the
6687 	// list of variables below.
6688 	// A line break may follow the "=".
6689 
6690 	wp = op + oplen;
6691 	if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6692 	    return FAIL;
6693 	if (compile_expr0(&p, cctx) == FAIL)
6694 	    return NULL;
6695 	end = p;
6696 
6697 	if (cctx->ctx_skip != SKIP_YES)
6698 	{
6699 	    type_T	*stacktype;
6700 
6701 	    stacktype = stack->ga_len == 0 ? &t_void
6702 			      : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6703 	    if (stacktype->tt_type == VAR_VOID)
6704 	    {
6705 		emsg(_(e_cannot_use_void_value));
6706 		goto theend;
6707 	    }
6708 	    if (need_type(stacktype, &t_list_any, -1, 0, cctx,
6709 							 FALSE, FALSE) == FAIL)
6710 		goto theend;
6711 	    // TODO: check the length of a constant list here
6712 	    generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6713 								    semicolon);
6714 	    if (stacktype->tt_member != NULL)
6715 		rhs_type = stacktype->tt_member;
6716 	}
6717     }
6718 
6719     /*
6720      * Loop over variables in "[var, var] = expr".
6721      * For "var = expr" and "let var: type" this is done only once.
6722      */
6723     if (var_count > 0)
6724 	var_start = skipwhite(arg + 1);  // skip over the "["
6725     else
6726 	var_start = arg;
6727     for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6728     {
6729 	int		instr_count = -1;
6730 
6731 	if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6732 	{
6733 	    // Ignore underscore in "[a, _, b] = list".
6734 	    if (var_count > 0)
6735 	    {
6736 		var_start = skipwhite(var_start + 2);
6737 		continue;
6738 	    }
6739 	    emsg(_(e_cannot_use_underscore_here));
6740 	    goto theend;
6741 	}
6742 	vim_free(lhs.lhs_name);
6743 
6744 	/*
6745 	 * Figure out the LHS type and other properties.
6746 	 */
6747 	if (compile_assign_lhs(var_start, &lhs, cmdidx,
6748 					is_decl, heredoc, oplen, cctx) == FAIL)
6749 	    goto theend;
6750 	if (!heredoc)
6751 	{
6752 	    if (cctx->ctx_skip == SKIP_YES)
6753 	    {
6754 		if (oplen > 0 && var_count == 0)
6755 		{
6756 		    // skip over the "=" and the expression
6757 		    p = skipwhite(op + oplen);
6758 		    (void)compile_expr0(&p, cctx);
6759 		}
6760 	    }
6761 	    else if (oplen > 0)
6762 	    {
6763 		int	is_const = FALSE;
6764 		char_u	*wp;
6765 
6766 		// for "+=", "*=", "..=" etc. first load the current value
6767 		if (*op != '='
6768 			&& compile_load_lhs_with_index(&lhs, var_start,
6769 								 cctx) == FAIL)
6770 		    goto theend;
6771 
6772 		// For "var = expr" evaluate the expression.
6773 		if (var_count == 0)
6774 		{
6775 		    int	r;
6776 
6777 		    // Compile the expression.
6778 		    instr_count = instr->ga_len;
6779 		    if (incdec)
6780 		    {
6781 			r = generate_PUSHNR(cctx, 1);
6782 		    }
6783 		    else
6784 		    {
6785 			// Temporarily hide the new local variable here, it is
6786 			// not available to this expression.
6787 			if (lhs.lhs_new_local)
6788 			    --cctx->ctx_locals.ga_len;
6789 			wp = op + oplen;
6790 			if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6791 			{
6792 			    if (lhs.lhs_new_local)
6793 				++cctx->ctx_locals.ga_len;
6794 			    goto theend;
6795 			}
6796 			r = compile_expr0_ext(&p, cctx, &is_const);
6797 			if (lhs.lhs_new_local)
6798 			    ++cctx->ctx_locals.ga_len;
6799 			if (r == FAIL)
6800 			    goto theend;
6801 		    }
6802 		}
6803 		else if (semicolon && var_idx == var_count - 1)
6804 		{
6805 		    // For "[var; var] = expr" get the rest of the list
6806 		    did_generate_slice = TRUE;
6807 		    if (generate_SLICE(cctx, var_count - 1) == FAIL)
6808 			goto theend;
6809 		}
6810 		else
6811 		{
6812 		    // For "[var, var] = expr" get the "var_idx" item from the
6813 		    // list.
6814 		    if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
6815 			goto theend;
6816 		}
6817 
6818 		rhs_type = stack->ga_len == 0 ? &t_void
6819 			      : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6820 		if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
6821 		{
6822 		    if ((rhs_type->tt_type == VAR_FUNC
6823 				|| rhs_type->tt_type == VAR_PARTIAL)
6824 			    && !lhs.lhs_has_index
6825 			    && var_wrong_func_name(lhs.lhs_name, TRUE))
6826 			goto theend;
6827 
6828 		    if (lhs.lhs_new_local && !lhs.lhs_has_type)
6829 		    {
6830 			if (rhs_type->tt_type == VAR_VOID)
6831 			{
6832 			    emsg(_(e_cannot_use_void_value));
6833 			    goto theend;
6834 			}
6835 			else
6836 			{
6837 			    // An empty list or dict has a &t_unknown member,
6838 			    // for a variable that implies &t_any.
6839 			    if (rhs_type == &t_list_empty)
6840 				lhs.lhs_lvar->lv_type = &t_list_any;
6841 			    else if (rhs_type == &t_dict_empty)
6842 				lhs.lhs_lvar->lv_type = &t_dict_any;
6843 			    else if (rhs_type == &t_unknown)
6844 				lhs.lhs_lvar->lv_type = &t_any;
6845 			    else
6846 				lhs.lhs_lvar->lv_type = rhs_type;
6847 			}
6848 		    }
6849 		    else if (*op == '=')
6850 		    {
6851 			type_T *use_type = lhs.lhs_lvar->lv_type;
6852 
6853 			// Without operator check type here, otherwise below.
6854 			// Use the line number of the assignment.
6855 			SOURCING_LNUM = start_lnum;
6856 			if (lhs.lhs_has_index)
6857 			    use_type = lhs.lhs_member_type;
6858 			if (need_type(rhs_type, use_type, -1, 0, cctx,
6859 						      FALSE, is_const) == FAIL)
6860 			    goto theend;
6861 		    }
6862 		}
6863 		else
6864 		{
6865 		    type_T *lhs_type = lhs.lhs_member_type;
6866 
6867 		    // Special case: assigning to @# can use a number or a
6868 		    // string.
6869 		    if (lhs_type == &t_number_or_string
6870 					    && rhs_type->tt_type == VAR_NUMBER)
6871 			lhs_type = &t_number;
6872 		    if (*p != '=' && need_type(rhs_type, lhs_type,
6873 					    -1, 0, cctx, FALSE, FALSE) == FAIL)
6874 		    goto theend;
6875 		}
6876 	    }
6877 	    else if (cmdidx == CMD_final)
6878 	    {
6879 		emsg(_(e_final_requires_a_value));
6880 		goto theend;
6881 	    }
6882 	    else if (cmdidx == CMD_const)
6883 	    {
6884 		emsg(_(e_const_requires_a_value));
6885 		goto theend;
6886 	    }
6887 	    else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
6888 	    {
6889 		emsg(_(e_type_or_initialization_required));
6890 		goto theend;
6891 	    }
6892 	    else
6893 	    {
6894 		// variables are always initialized
6895 		if (ga_grow(instr, 1) == FAIL)
6896 		    goto theend;
6897 		switch (lhs.lhs_member_type->tt_type)
6898 		{
6899 		    case VAR_BOOL:
6900 			generate_PUSHBOOL(cctx, VVAL_FALSE);
6901 			break;
6902 		    case VAR_FLOAT:
6903 #ifdef FEAT_FLOAT
6904 			generate_PUSHF(cctx, 0.0);
6905 #endif
6906 			break;
6907 		    case VAR_STRING:
6908 			generate_PUSHS(cctx, NULL);
6909 			break;
6910 		    case VAR_BLOB:
6911 			generate_PUSHBLOB(cctx, blob_alloc());
6912 			break;
6913 		    case VAR_FUNC:
6914 			generate_PUSHFUNC(cctx, NULL, &t_func_void);
6915 			break;
6916 		    case VAR_LIST:
6917 			generate_NEWLIST(cctx, 0);
6918 			break;
6919 		    case VAR_DICT:
6920 			generate_NEWDICT(cctx, 0);
6921 			break;
6922 		    case VAR_JOB:
6923 			generate_PUSHJOB(cctx, NULL);
6924 			break;
6925 		    case VAR_CHANNEL:
6926 			generate_PUSHCHANNEL(cctx, NULL);
6927 			break;
6928 		    case VAR_NUMBER:
6929 		    case VAR_UNKNOWN:
6930 		    case VAR_ANY:
6931 		    case VAR_PARTIAL:
6932 		    case VAR_VOID:
6933 		    case VAR_INSTR:
6934 		    case VAR_SPECIAL:  // cannot happen
6935 			generate_PUSHNR(cctx, 0);
6936 			break;
6937 		}
6938 	    }
6939 	    if (var_count == 0)
6940 		end = p;
6941 	}
6942 
6943 	// no need to parse more when skipping
6944 	if (cctx->ctx_skip == SKIP_YES)
6945 	    break;
6946 
6947 	if (oplen > 0 && *op != '=')
6948 	{
6949 	    type_T	    *expected;
6950 	    type_T	    *stacktype;
6951 
6952 	    if (*op == '.')
6953 		expected = &t_string;
6954 	    else
6955 		expected = lhs.lhs_member_type;
6956 	    stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6957 	    if (
6958 #ifdef FEAT_FLOAT
6959 		// If variable is float operation with number is OK.
6960 		!(expected == &t_float && stacktype == &t_number) &&
6961 #endif
6962 		    need_type(stacktype, expected, -1, 0, cctx,
6963 							 FALSE, FALSE) == FAIL)
6964 		goto theend;
6965 
6966 	    if (*op == '.')
6967 	    {
6968 		if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6969 		    goto theend;
6970 	    }
6971 	    else if (*op == '+')
6972 	    {
6973 		if (generate_add_instr(cctx,
6974 			    operator_type(lhs.lhs_member_type, stacktype),
6975 				       lhs.lhs_member_type, stacktype) == FAIL)
6976 		    goto theend;
6977 	    }
6978 	    else if (generate_two_op(cctx, op) == FAIL)
6979 		goto theend;
6980 	}
6981 
6982 	if (lhs.lhs_has_index)
6983 	{
6984 	    // Use the info in "lhs" to store the value at the index in the
6985 	    // list or dict.
6986 	    if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6987 								       == FAIL)
6988 		goto theend;
6989 	}
6990 	else
6991 	{
6992 	    if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6993 						|| lhs.lhs_dest == dest_global
6994 						|| lhs.lhs_dest == dest_local))
6995 		// ":const var": lock the value, but not referenced variables
6996 		generate_LOCKCONST(cctx);
6997 
6998 	    if (is_decl
6999 		    && (lhs.lhs_type->tt_type == VAR_DICT
7000 					  || lhs.lhs_type->tt_type == VAR_LIST)
7001 		    && lhs.lhs_type->tt_member != NULL
7002 		    && lhs.lhs_type->tt_member != &t_any
7003 		    && lhs.lhs_type->tt_member != &t_unknown)
7004 		// Set the type in the list or dict, so that it can be checked,
7005 		// also in legacy script.
7006 		generate_SETTYPE(cctx, lhs.lhs_type);
7007 
7008 	    if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
7009 		goto theend;
7010 	}
7011 
7012 	if (var_idx + 1 < var_count)
7013 	    var_start = skipwhite(lhs.lhs_dest_end + 1);
7014     }
7015 
7016     // For "[var, var] = expr" drop the "expr" value.
7017     // Also for "[var, var; _] = expr".
7018     if (var_count > 0 && (!semicolon || !did_generate_slice))
7019     {
7020 	if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7021 	    goto theend;
7022     }
7023 
7024     ret = skipwhite(end);
7025 
7026 theend:
7027     vim_free(lhs.lhs_name);
7028     return ret;
7029 }
7030 
7031 /*
7032  * Check for an assignment at "eap->cmd", compile it if found.
7033  * Return NOTDONE if there is none, FAIL for failure, OK if done.
7034  */
7035     static int
7036 may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
7037 {
7038     char_u  *pskip;
7039     char_u  *p;
7040 
7041     // Assuming the command starts with a variable or function name,
7042     // find what follows.
7043     // Skip over "var.member", "var[idx]" and the like.
7044     // Also "&opt = val", "$ENV = val" and "@r = val".
7045     pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
7046 						 ? eap->cmd + 1 : eap->cmd;
7047     p = to_name_end(pskip, TRUE);
7048     if (p > eap->cmd && *p != NUL)
7049     {
7050 	char_u *var_end;
7051 	int	oplen;
7052 	int	heredoc;
7053 
7054 	if (eap->cmd[0] == '@')
7055 	    var_end = eap->cmd + 2;
7056 	else
7057 	    var_end = find_name_end(pskip, NULL, NULL,
7058 					FNE_CHECK_START | FNE_INCL_BR);
7059 	oplen = assignment_len(skipwhite(var_end), &heredoc);
7060 	if (oplen > 0)
7061 	{
7062 	    size_t len = p - eap->cmd;
7063 
7064 	    // Recognize an assignment if we recognize the variable
7065 	    // name:
7066 	    // "g:var = expr"
7067 	    // "local = expr"  where "local" is a local var.
7068 	    // "script = expr"  where "script" is a script-local var.
7069 	    // "import = expr"  where "import" is an imported var
7070 	    // "&opt = expr"
7071 	    // "$ENV = expr"
7072 	    // "@r = expr"
7073 	    if (*eap->cmd == '&'
7074 		    || *eap->cmd == '$'
7075 		    || *eap->cmd == '@'
7076 		    || ((len) > 2 && eap->cmd[1] == ':')
7077 		    || variable_exists(eap->cmd, len, cctx))
7078 	    {
7079 		*line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7080 		if (*line == NULL || *line == eap->cmd)
7081 		    return FAIL;
7082 		return OK;
7083 	    }
7084 	}
7085     }
7086 
7087     if (*eap->cmd == '[')
7088     {
7089 	// [var, var] = expr
7090 	*line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
7091 	if (*line == NULL)
7092 	    return FAIL;
7093 	if (*line != eap->cmd)
7094 	    return OK;
7095     }
7096     return NOTDONE;
7097 }
7098 
7099 /*
7100  * Check if "name" can be "unlet".
7101  */
7102     int
7103 check_vim9_unlet(char_u *name)
7104 {
7105     if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
7106     {
7107 	// "unlet s:var" is allowed in legacy script.
7108 	if (*name == 's' && !script_is_vim9())
7109 	    return OK;
7110 	semsg(_(e_cannot_unlet_str), name);
7111 	return FAIL;
7112     }
7113     return OK;
7114 }
7115 
7116 /*
7117  * Callback passed to ex_unletlock().
7118  */
7119     static int
7120 compile_unlet(
7121     lval_T  *lvp,
7122     char_u  *name_end,
7123     exarg_T *eap,
7124     int	    deep UNUSED,
7125     void    *coookie)
7126 {
7127     cctx_T	*cctx = coookie;
7128     char_u	*p = lvp->ll_name;
7129     int		cc = *name_end;
7130     int		ret = OK;
7131 
7132     if (cctx->ctx_skip == SKIP_YES)
7133 	return OK;
7134 
7135     *name_end = NUL;
7136     if (*p == '$')
7137     {
7138 	// :unlet $ENV_VAR
7139 	ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
7140     }
7141     else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
7142     {
7143 	lhs_T	    lhs;
7144 
7145 	// This is similar to assigning: lookup the list/dict, compile the
7146 	// idx/key.  Then instead of storing the value unlet the item.
7147 	// unlet {list}[idx]
7148 	// unlet {dict}[key]  dict.key
7149 	//
7150 	// Figure out the LHS type and other properties.
7151 	//
7152 	ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
7153 
7154 	// : unlet an indexed item
7155 	if (!lhs.lhs_has_index)
7156 	{
7157 	    iemsg("called compile_lhs() without an index");
7158 	    ret = FAIL;
7159 	}
7160 	else
7161 	{
7162 	    // Use the info in "lhs" to unlet the item at the index in the
7163 	    // list or dict.
7164 	    ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
7165 	}
7166 
7167 	vim_free(lhs.lhs_name);
7168     }
7169     else if (check_vim9_unlet(p) == FAIL)
7170     {
7171 	ret = FAIL;
7172     }
7173     else
7174     {
7175 	// Normal name.  Only supports g:, w:, t: and b: namespaces.
7176 	ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
7177     }
7178 
7179     *name_end = cc;
7180     return ret;
7181 }
7182 
7183 /*
7184  * Callback passed to ex_unletlock().
7185  */
7186     static int
7187 compile_lock_unlock(
7188     lval_T  *lvp,
7189     char_u  *name_end,
7190     exarg_T *eap,
7191     int	    deep UNUSED,
7192     void    *coookie)
7193 {
7194     cctx_T	*cctx = coookie;
7195     int		cc = *name_end;
7196     char_u	*p = lvp->ll_name;
7197     int		ret = OK;
7198     size_t	len;
7199     char_u	*buf;
7200 
7201     if (cctx->ctx_skip == SKIP_YES)
7202 	return OK;
7203 
7204     // Cannot use :lockvar and :unlockvar on local variables.
7205     if (p[1] != ':')
7206     {
7207 	char_u *end = skip_var_one(p, FALSE);
7208 
7209 	if (lookup_local(p, end - p, NULL, cctx) == OK)
7210 	{
7211 	    emsg(_(e_cannot_lock_unlock_local_variable));
7212 	    return FAIL;
7213 	}
7214     }
7215 
7216     // Checking is done at runtime.
7217     *name_end = NUL;
7218     len = name_end - p + 20;
7219     buf = alloc(len);
7220     if (buf == NULL)
7221 	ret = FAIL;
7222     else
7223     {
7224 	vim_snprintf((char *)buf, len, "%s %s",
7225 		eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
7226 		p);
7227 	ret = generate_EXEC(cctx, buf);
7228 
7229 	vim_free(buf);
7230 	*name_end = cc;
7231     }
7232     return ret;
7233 }
7234 
7235 /*
7236  * compile "unlet var", "lock var" and "unlock var"
7237  * "arg" points to "var".
7238  */
7239     static char_u *
7240 compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
7241 {
7242     ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
7243 	    eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
7244 	    cctx);
7245     return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
7246 }
7247 
7248 /*
7249  * Compile an :import command.
7250  */
7251     static char_u *
7252 compile_import(char_u *arg, cctx_T *cctx)
7253 {
7254     return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
7255 }
7256 
7257 /*
7258  * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7259  */
7260     static int
7261 compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7262 {
7263     garray_T	*instr = &cctx->ctx_instr;
7264     endlabel_T  *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7265 
7266     if (endlabel == NULL)
7267 	return FAIL;
7268     endlabel->el_next = *el;
7269     *el = endlabel;
7270     endlabel->el_end_label = instr->ga_len;
7271 
7272     generate_JUMP(cctx, when, 0);
7273     return OK;
7274 }
7275 
7276     static void
7277 compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
7278 {
7279     garray_T	*instr = &cctx->ctx_instr;
7280 
7281     while (*el != NULL)
7282     {
7283 	endlabel_T  *cur = (*el);
7284 	isn_T	    *isn;
7285 
7286 	isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
7287 	isn->isn_arg.jump.jump_where = jump_where;
7288 	*el = cur->el_next;
7289 	vim_free(cur);
7290     }
7291 }
7292 
7293     static void
7294 compile_free_jump_to_end(endlabel_T **el)
7295 {
7296     while (*el != NULL)
7297     {
7298 	endlabel_T  *cur = (*el);
7299 
7300 	*el = cur->el_next;
7301 	vim_free(cur);
7302     }
7303 }
7304 
7305 /*
7306  * Create a new scope and set up the generic items.
7307  */
7308     static scope_T *
7309 new_scope(cctx_T *cctx, scopetype_T type)
7310 {
7311     scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7312 
7313     if (scope == NULL)
7314 	return NULL;
7315     scope->se_outer = cctx->ctx_scope;
7316     cctx->ctx_scope = scope;
7317     scope->se_type = type;
7318     scope->se_local_count = cctx->ctx_locals.ga_len;
7319     return scope;
7320 }
7321 
7322 /*
7323  * Free the current scope and go back to the outer scope.
7324  */
7325     static void
7326 drop_scope(cctx_T *cctx)
7327 {
7328     scope_T *scope = cctx->ctx_scope;
7329 
7330     if (scope == NULL)
7331     {
7332 	iemsg("calling drop_scope() without a scope");
7333 	return;
7334     }
7335     cctx->ctx_scope = scope->se_outer;
7336     switch (scope->se_type)
7337     {
7338 	case IF_SCOPE:
7339 	    compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7340 	case FOR_SCOPE:
7341 	    compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7342 	case WHILE_SCOPE:
7343 	    compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7344 	case TRY_SCOPE:
7345 	    compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7346 	case NO_SCOPE:
7347 	case BLOCK_SCOPE:
7348 	    break;
7349     }
7350     vim_free(scope);
7351 }
7352 
7353 /*
7354  * compile "if expr"
7355  *
7356  * "if expr" Produces instructions:
7357  *	EVAL expr		Push result of "expr"
7358  *	JUMP_IF_FALSE end
7359  *	... body ...
7360  * end:
7361  *
7362  * "if expr | else" Produces instructions:
7363  *	EVAL expr		Push result of "expr"
7364  *	JUMP_IF_FALSE else
7365  *	... body ...
7366  *	JUMP_ALWAYS end
7367  * else:
7368  *	... body ...
7369  * end:
7370  *
7371  * "if expr1 | elseif expr2 | else" Produces instructions:
7372  *	EVAL expr		Push result of "expr"
7373  *	JUMP_IF_FALSE elseif
7374  *	... body ...
7375  *	JUMP_ALWAYS end
7376  * elseif:
7377  *	EVAL expr		Push result of "expr"
7378  *	JUMP_IF_FALSE else
7379  *	... body ...
7380  *	JUMP_ALWAYS end
7381  * else:
7382  *	... body ...
7383  * end:
7384  */
7385     static char_u *
7386 compile_if(char_u *arg, cctx_T *cctx)
7387 {
7388     char_u	*p = arg;
7389     garray_T	*instr = &cctx->ctx_instr;
7390     int		instr_count = instr->ga_len;
7391     scope_T	*scope;
7392     skip_T	skip_save = cctx->ctx_skip;
7393     ppconst_T	ppconst;
7394 
7395     CLEAR_FIELD(ppconst);
7396     if (compile_expr1(&p, cctx, &ppconst) == FAIL)
7397     {
7398 	clear_ppconst(&ppconst);
7399 	return NULL;
7400     }
7401     if (!ends_excmd2(arg, skipwhite(p)))
7402     {
7403 	semsg(_(e_trailing_arg), p);
7404 	return NULL;
7405     }
7406     if (cctx->ctx_skip == SKIP_YES)
7407 	clear_ppconst(&ppconst);
7408     else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
7409     {
7410 	int error = FALSE;
7411 	int v;
7412 
7413 	// The expression results in a constant.
7414 	v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7415 	clear_ppconst(&ppconst);
7416 	if (error)
7417 	    return NULL;
7418 	cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
7419     }
7420     else
7421     {
7422 	// Not a constant, generate instructions for the expression.
7423 	cctx->ctx_skip = SKIP_UNKNOWN;
7424 	if (generate_ppconst(cctx, &ppconst) == FAIL)
7425 	    return NULL;
7426 	if (bool_on_stack(cctx) == FAIL)
7427 	    return NULL;
7428     }
7429 
7430     // CMDMOD_REV must come before the jump
7431     generate_undo_cmdmods(cctx);
7432 
7433     scope = new_scope(cctx, IF_SCOPE);
7434     if (scope == NULL)
7435 	return NULL;
7436     scope->se_skip_save = skip_save;
7437     // "is_had_return" will be reset if any block does not end in :return
7438     scope->se_u.se_if.is_had_return = TRUE;
7439 
7440     if (cctx->ctx_skip == SKIP_UNKNOWN)
7441     {
7442 	// "where" is set when ":elseif", "else" or ":endif" is found
7443 	scope->se_u.se_if.is_if_label = instr->ga_len;
7444 	generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7445     }
7446     else
7447 	scope->se_u.se_if.is_if_label = -1;
7448 
7449 #ifdef FEAT_PROFILE
7450     if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
7451 						      && skip_save != SKIP_YES)
7452     {
7453 	// generated a profile start, need to generate a profile end, since it
7454 	// won't be done after returning
7455 	cctx->ctx_skip = SKIP_NOT;
7456 	generate_instr(cctx, ISN_PROF_END);
7457 	cctx->ctx_skip = SKIP_YES;
7458     }
7459 #endif
7460 
7461     return p;
7462 }
7463 
7464     static char_u *
7465 compile_elseif(char_u *arg, cctx_T *cctx)
7466 {
7467     char_u	*p = arg;
7468     garray_T	*instr = &cctx->ctx_instr;
7469     int		instr_count = instr->ga_len;
7470     isn_T	*isn;
7471     scope_T	*scope = cctx->ctx_scope;
7472     ppconst_T	ppconst;
7473     skip_T	save_skip = cctx->ctx_skip;
7474 
7475     if (scope == NULL || scope->se_type != IF_SCOPE)
7476     {
7477 	emsg(_(e_elseif_without_if));
7478 	return NULL;
7479     }
7480     unwind_locals(cctx, scope->se_local_count);
7481     if (!cctx->ctx_had_return)
7482 	scope->se_u.se_if.is_had_return = FALSE;
7483 
7484     if (cctx->ctx_skip == SKIP_NOT)
7485     {
7486 	// previous block was executed, this one and following will not
7487 	cctx->ctx_skip = SKIP_YES;
7488 	scope->se_u.se_if.is_seen_skip_not = TRUE;
7489     }
7490     if (scope->se_u.se_if.is_seen_skip_not)
7491     {
7492 	// A previous block was executed, skip over expression and bail out.
7493 	// Do not count the "elseif" for profiling and cmdmod
7494 	instr->ga_len = current_instr_idx(cctx);
7495 
7496 	skip_expr_cctx(&p, cctx);
7497 	return p;
7498     }
7499 
7500     if (cctx->ctx_skip == SKIP_UNKNOWN)
7501     {
7502 	int moved_cmdmod = FALSE;
7503 
7504 	// Move any CMDMOD instruction to after the jump
7505 	if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7506 	{
7507 	    if (ga_grow(instr, 1) == FAIL)
7508 		return NULL;
7509 	    ((isn_T *)instr->ga_data)[instr->ga_len] =
7510 				  ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7511 	    --instr->ga_len;
7512 	    moved_cmdmod = TRUE;
7513 	}
7514 
7515 	if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7516 						    JUMP_ALWAYS, cctx) == FAIL)
7517 	    return NULL;
7518 	// previous "if" or "elseif" jumps here
7519 	isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7520 	isn->isn_arg.jump.jump_where = instr->ga_len;
7521 	if (moved_cmdmod)
7522 	    ++instr->ga_len;
7523     }
7524 
7525     // compile "expr"; if we know it evaluates to FALSE skip the block
7526     CLEAR_FIELD(ppconst);
7527     if (cctx->ctx_skip == SKIP_YES)
7528     {
7529 	cctx->ctx_skip = SKIP_UNKNOWN;
7530 #ifdef FEAT_PROFILE
7531 	if (cctx->ctx_compile_type == CT_PROFILE)
7532 	{
7533 	    // the previous block was skipped, need to profile this line
7534 	    generate_instr(cctx, ISN_PROF_START);
7535 	    instr_count = instr->ga_len;
7536 	}
7537 #endif
7538 	if (cctx->ctx_compile_type == CT_DEBUG)
7539 	{
7540 	    // the previous block was skipped, may want to debug this line
7541 	    generate_instr_debug(cctx);
7542 	    instr_count = instr->ga_len;
7543 	}
7544     }
7545     if (compile_expr1(&p, cctx, &ppconst) == FAIL)
7546     {
7547 	clear_ppconst(&ppconst);
7548 	return NULL;
7549     }
7550     cctx->ctx_skip = save_skip;
7551     if (!ends_excmd2(arg, skipwhite(p)))
7552     {
7553 	semsg(_(e_trailing_arg), p);
7554 	return NULL;
7555     }
7556     if (scope->se_skip_save == SKIP_YES)
7557 	clear_ppconst(&ppconst);
7558     else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
7559     {
7560 	int error = FALSE;
7561 	int v;
7562 
7563 	// The expression results in a constant.
7564 	// TODO: how about nesting?
7565 	v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7566 	if (error)
7567 	    return NULL;
7568 	cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
7569 	clear_ppconst(&ppconst);
7570 	scope->se_u.se_if.is_if_label = -1;
7571     }
7572     else
7573     {
7574 	// Not a constant, generate instructions for the expression.
7575 	cctx->ctx_skip = SKIP_UNKNOWN;
7576 	if (generate_ppconst(cctx, &ppconst) == FAIL)
7577 	    return NULL;
7578 	if (bool_on_stack(cctx) == FAIL)
7579 	    return NULL;
7580 
7581 	// CMDMOD_REV must come before the jump
7582 	generate_undo_cmdmods(cctx);
7583 
7584 	// "where" is set when ":elseif", "else" or ":endif" is found
7585 	scope->se_u.se_if.is_if_label = instr->ga_len;
7586 	generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7587     }
7588 
7589     return p;
7590 }
7591 
7592     static char_u *
7593 compile_else(char_u *arg, cctx_T *cctx)
7594 {
7595     char_u	*p = arg;
7596     garray_T	*instr = &cctx->ctx_instr;
7597     isn_T	*isn;
7598     scope_T	*scope = cctx->ctx_scope;
7599 
7600     if (scope == NULL || scope->se_type != IF_SCOPE)
7601     {
7602 	emsg(_(e_else_without_if));
7603 	return NULL;
7604     }
7605     unwind_locals(cctx, scope->se_local_count);
7606     if (!cctx->ctx_had_return)
7607 	scope->se_u.se_if.is_had_return = FALSE;
7608     scope->se_u.se_if.is_seen_else = TRUE;
7609 
7610 #ifdef FEAT_PROFILE
7611     if (cctx->ctx_compile_type == CT_PROFILE)
7612     {
7613 	if (cctx->ctx_skip == SKIP_NOT
7614 		&& ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7615 						   .isn_type == ISN_PROF_START)
7616 	    // the previous block was executed, do not count "else" for
7617 	    // profiling
7618 	    --instr->ga_len;
7619 	if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7620 	{
7621 	    // the previous block was not executed, this one will, do count the
7622 	    // "else" for profiling
7623 	    cctx->ctx_skip = SKIP_NOT;
7624 	    generate_instr(cctx, ISN_PROF_END);
7625 	    generate_instr(cctx, ISN_PROF_START);
7626 	    cctx->ctx_skip = SKIP_YES;
7627 	}
7628     }
7629 #endif
7630 
7631     if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
7632     {
7633 	// jump from previous block to the end, unless the else block is empty
7634 	if (cctx->ctx_skip == SKIP_UNKNOWN)
7635 	{
7636 	    if (!cctx->ctx_had_return
7637 		    && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7638 						    JUMP_ALWAYS, cctx) == FAIL)
7639 		return NULL;
7640 	}
7641 
7642 	if (cctx->ctx_skip == SKIP_UNKNOWN)
7643 	{
7644 	    if (scope->se_u.se_if.is_if_label >= 0)
7645 	    {
7646 		// previous "if" or "elseif" jumps here
7647 		isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7648 		isn->isn_arg.jump.jump_where = instr->ga_len;
7649 		scope->se_u.se_if.is_if_label = -1;
7650 	    }
7651 	}
7652 
7653 	if (cctx->ctx_skip != SKIP_UNKNOWN)
7654 	    cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7655     }
7656 
7657     return p;
7658 }
7659 
7660     static char_u *
7661 compile_endif(char_u *arg, cctx_T *cctx)
7662 {
7663     scope_T	*scope = cctx->ctx_scope;
7664     ifscope_T	*ifscope;
7665     garray_T	*instr = &cctx->ctx_instr;
7666     isn_T	*isn;
7667 
7668     if (misplaced_cmdmod(cctx))
7669 	return NULL;
7670 
7671     if (scope == NULL || scope->se_type != IF_SCOPE)
7672     {
7673 	emsg(_(e_endif_without_if));
7674 	return NULL;
7675     }
7676     ifscope = &scope->se_u.se_if;
7677     unwind_locals(cctx, scope->se_local_count);
7678     if (!cctx->ctx_had_return)
7679 	ifscope->is_had_return = FALSE;
7680 
7681     if (scope->se_u.se_if.is_if_label >= 0)
7682     {
7683 	// previous "if" or "elseif" jumps here
7684 	isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7685 	isn->isn_arg.jump.jump_where = instr->ga_len;
7686     }
7687     // Fill in the "end" label in jumps at the end of the blocks.
7688     compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
7689 
7690 #ifdef FEAT_PROFILE
7691     // even when skipping we count the endif as executed, unless the block it's
7692     // in is skipped
7693     if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
7694 					    && scope->se_skip_save != SKIP_YES)
7695     {
7696 	cctx->ctx_skip = SKIP_NOT;
7697 	generate_instr(cctx, ISN_PROF_START);
7698     }
7699 #endif
7700     cctx->ctx_skip = scope->se_skip_save;
7701 
7702     // If all the blocks end in :return and there is an :else then the
7703     // had_return flag is set.
7704     cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
7705 
7706     drop_scope(cctx);
7707     return arg;
7708 }
7709 
7710 /*
7711  * Compile "for var in expr":
7712  *
7713  * Produces instructions:
7714  *       PUSHNR -1
7715  *       STORE loop-idx		Set index to -1
7716  *       EVAL expr		result of "expr" on top of stack
7717  * top:  FOR loop-idx, end	Increment index, use list on bottom of stack
7718  *				- if beyond end, jump to "end"
7719  *				- otherwise get item from list and push it
7720  *       STORE var		Store item in "var"
7721  *       ... body ...
7722  *       JUMP top		Jump back to repeat
7723  * end:	 DROP			Drop the result of "expr"
7724  *
7725  * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7726  *	 UNPACK 2		Split item in 2
7727  *       STORE var1		Store item in "var1"
7728  *       STORE var2		Store item in "var2"
7729  */
7730     static char_u *
7731 compile_for(char_u *arg_start, cctx_T *cctx)
7732 {
7733     char_u	*arg;
7734     char_u	*arg_end;
7735     char_u	*name = NULL;
7736     char_u	*p;
7737     char_u	*wp;
7738     int		var_count = 0;
7739     int		var_list = FALSE;
7740     int		semicolon = FALSE;
7741     size_t	varlen;
7742     garray_T	*stack = &cctx->ctx_type_stack;
7743     garray_T	*instr = &cctx->ctx_instr;
7744     scope_T	*scope;
7745     lvar_T	*loop_lvar;	// loop iteration variable
7746     lvar_T	*var_lvar;	// variable for "var"
7747     type_T	*vartype;
7748     type_T	*item_type = &t_any;
7749     int		idx;
7750 
7751     p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
7752     if (p == NULL)
7753 	return NULL;
7754     if (var_count == 0)
7755 	var_count = 1;
7756     else
7757 	var_list = TRUE;  // can also be a list of one variable
7758 
7759     // consume "in"
7760     wp = p;
7761     if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7762 	return NULL;
7763     if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
7764     {
7765 	emsg(_(e_missing_in));
7766 	return NULL;
7767     }
7768     wp = p + 2;
7769     if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7770 	return NULL;
7771 
7772     // Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
7773     // instruction.
7774     if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
7775 	    && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7776 							.isn_type == ISN_DEBUG)
7777 	--instr->ga_len;
7778 
7779     scope = new_scope(cctx, FOR_SCOPE);
7780     if (scope == NULL)
7781 	return NULL;
7782 
7783     // Reserve a variable to store the loop iteration counter and initialize it
7784     // to -1.
7785     loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7786     if (loop_lvar == NULL)
7787     {
7788 	// out of memory
7789 	drop_scope(cctx);
7790 	return NULL;
7791     }
7792     generate_STORENR(cctx, loop_lvar->lv_idx, -1);
7793 
7794     // compile "expr", it remains on the stack until "endfor"
7795     arg = p;
7796     if (compile_expr0(&arg, cctx) == FAIL)
7797     {
7798 	drop_scope(cctx);
7799 	return NULL;
7800     }
7801     arg_end = arg;
7802 
7803     // If we know the type of "var" and it is a not a supported type we can
7804     // give an error now.
7805     vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7806     if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
7807 		&& vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
7808     {
7809 	semsg(_(e_for_loop_on_str_not_supported),
7810 					       vartype_name(vartype->tt_type));
7811 	drop_scope(cctx);
7812 	return NULL;
7813     }
7814 
7815     if (vartype->tt_type == VAR_STRING)
7816 	item_type = &t_string;
7817     else if (vartype->tt_type == VAR_BLOB)
7818 	item_type = &t_number;
7819     else if (vartype->tt_type == VAR_LIST
7820 				     && vartype->tt_member->tt_type != VAR_ANY)
7821     {
7822 	if (!var_list)
7823 	    item_type = vartype->tt_member;
7824 	else if (vartype->tt_member->tt_type == VAR_LIST
7825 		      && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7826 	    // TODO: should get the type for each lhs
7827 	    item_type = vartype->tt_member->tt_member;
7828     }
7829 
7830     // CMDMOD_REV must come before the FOR instruction.
7831     generate_undo_cmdmods(cctx);
7832 
7833     // "for_end" is set when ":endfor" is found
7834     scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
7835 
7836     generate_FOR(cctx, loop_lvar->lv_idx);
7837 
7838     arg = arg_start;
7839     if (var_list)
7840     {
7841 	generate_UNPACK(cctx, var_count, semicolon);
7842 	arg = skipwhite(arg + 1);	// skip white after '['
7843 
7844 	// the list item is replaced by a number of items
7845 	if (ga_grow(stack, var_count - 1) == FAIL)
7846 	{
7847 	    drop_scope(cctx);
7848 	    return NULL;
7849 	}
7850 	--stack->ga_len;
7851 	for (idx = 0; idx < var_count; ++idx)
7852 	{
7853 	    ((type_T **)stack->ga_data)[stack->ga_len] =
7854 				(semicolon && idx == 0) ? vartype : item_type;
7855 	    ++stack->ga_len;
7856 	}
7857     }
7858 
7859     for (idx = 0; idx < var_count; ++idx)
7860     {
7861 	assign_dest_T	dest = dest_local;
7862 	int		opt_flags = 0;
7863 	int		vimvaridx = -1;
7864 	type_T		*type = &t_any;
7865 	type_T		*lhs_type = &t_any;
7866 	where_T		where;
7867 
7868 	p = skip_var_one(arg, FALSE);
7869 	varlen = p - arg;
7870 	name = vim_strnsave(arg, varlen);
7871 	if (name == NULL)
7872 	    goto failed;
7873 	if (*p == ':')
7874 	{
7875 	    p = skipwhite(p + 1);
7876 	    lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7877 	}
7878 
7879 	// TODO: script var not supported?
7880 	if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7881 					      &vimvaridx, &type, cctx) == FAIL)
7882 	    goto failed;
7883 	if (dest != dest_local)
7884 	{
7885 	    if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7886 						     0, 0, type, name) == FAIL)
7887 		goto failed;
7888 	}
7889 	else if (varlen == 1 && *arg == '_')
7890 	{
7891 	    // Assigning to "_": drop the value.
7892 	    if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7893 		goto failed;
7894 	}
7895 	else
7896 	{
7897 	    if (lookup_local(arg, varlen, NULL, cctx) == OK)
7898 	    {
7899 		semsg(_(e_variable_already_declared), arg);
7900 		goto failed;
7901 	    }
7902 
7903 	    if (STRNCMP(name, "s:", 2) == 0)
7904 	    {
7905 		semsg(_(e_cannot_declare_script_variable_in_function), name);
7906 		goto failed;
7907 	    }
7908 
7909 	    // Reserve a variable to store "var".
7910 	    where.wt_index = var_list ? idx + 1 : 0;
7911 	    where.wt_variable = TRUE;
7912 	    if (lhs_type == &t_any)
7913 		lhs_type = item_type;
7914 	    else if (item_type != &t_unknown
7915 		       && !(var_list && item_type == &t_any)
7916 		       && check_type(lhs_type, item_type, TRUE, where) == FAIL)
7917 		goto failed;
7918 	    var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
7919 	    if (var_lvar == NULL)
7920 		// out of memory or used as an argument
7921 		goto failed;
7922 
7923 	    if (semicolon && idx == var_count - 1)
7924 		var_lvar->lv_type = vartype;
7925 	    else
7926 		var_lvar->lv_type = item_type;
7927 	    generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7928 	}
7929 
7930 	if (*p == ',' || *p == ';')
7931 	    ++p;
7932 	arg = skipwhite(p);
7933 	vim_free(name);
7934     }
7935 
7936     if (cctx->ctx_compile_type == CT_DEBUG)
7937 	// Add ISN_DEBUG here, so that the loop variables can be inspected.
7938 	generate_instr_debug(cctx);
7939 
7940     return arg_end;
7941 
7942 failed:
7943     vim_free(name);
7944     drop_scope(cctx);
7945     return NULL;
7946 }
7947 
7948 /*
7949  * compile "endfor"
7950  */
7951     static char_u *
7952 compile_endfor(char_u *arg, cctx_T *cctx)
7953 {
7954     garray_T	*instr = &cctx->ctx_instr;
7955     scope_T	*scope = cctx->ctx_scope;
7956     forscope_T	*forscope;
7957     isn_T	*isn;
7958 
7959     if (misplaced_cmdmod(cctx))
7960 	return NULL;
7961 
7962     if (scope == NULL || scope->se_type != FOR_SCOPE)
7963     {
7964 	emsg(_(e_for));
7965 	return NULL;
7966     }
7967     forscope = &scope->se_u.se_for;
7968     cctx->ctx_scope = scope->se_outer;
7969     unwind_locals(cctx, scope->se_local_count);
7970 
7971     // At end of ":for" scope jump back to the FOR instruction.
7972     generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7973 
7974     // Fill in the "end" label in the FOR statement so it can jump here.
7975     isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7976     isn->isn_arg.forloop.for_end = instr->ga_len;
7977 
7978     // Fill in the "end" label any BREAK statements
7979     compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
7980 
7981     // Below the ":for" scope drop the "expr" list from the stack.
7982     if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7983 	return NULL;
7984 
7985     vim_free(scope);
7986 
7987     return arg;
7988 }
7989 
7990 /*
7991  * compile "while expr"
7992  *
7993  * Produces instructions:
7994  * top:  EVAL expr		Push result of "expr"
7995  *       JUMP_IF_FALSE end	jump if false
7996  *       ... body ...
7997  *       JUMP top		Jump back to repeat
7998  * end:
7999  *
8000  */
8001     static char_u *
8002 compile_while(char_u *arg, cctx_T *cctx)
8003 {
8004     char_u	*p = arg;
8005     scope_T	*scope;
8006 
8007     scope = new_scope(cctx, WHILE_SCOPE);
8008     if (scope == NULL)
8009 	return NULL;
8010 
8011     // "endwhile" jumps back here, one before when profiling or using cmdmods
8012     scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
8013 
8014     // compile "expr"
8015     if (compile_expr0(&p, cctx) == FAIL)
8016 	return NULL;
8017     if (!ends_excmd2(arg, skipwhite(p)))
8018     {
8019 	semsg(_(e_trailing_arg), p);
8020 	return NULL;
8021     }
8022 
8023     if (bool_on_stack(cctx) == FAIL)
8024 	return FAIL;
8025 
8026     // CMDMOD_REV must come before the jump
8027     generate_undo_cmdmods(cctx);
8028 
8029     // "while_end" is set when ":endwhile" is found
8030     if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
8031 						  JUMP_IF_FALSE, cctx) == FAIL)
8032 	return FAIL;
8033 
8034     return p;
8035 }
8036 
8037 /*
8038  * compile "endwhile"
8039  */
8040     static char_u *
8041 compile_endwhile(char_u *arg, cctx_T *cctx)
8042 {
8043     scope_T	*scope = cctx->ctx_scope;
8044     garray_T	*instr = &cctx->ctx_instr;
8045 
8046     if (misplaced_cmdmod(cctx))
8047 	return NULL;
8048     if (scope == NULL || scope->se_type != WHILE_SCOPE)
8049     {
8050 	emsg(_(e_while));
8051 	return NULL;
8052     }
8053     cctx->ctx_scope = scope->se_outer;
8054     unwind_locals(cctx, scope->se_local_count);
8055 
8056 #ifdef FEAT_PROFILE
8057     // count the endwhile before jumping
8058     may_generate_prof_end(cctx, cctx->ctx_lnum);
8059 #endif
8060 
8061     // At end of ":for" scope jump back to the FOR instruction.
8062     generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
8063 
8064     // Fill in the "end" label in the WHILE statement so it can jump here.
8065     // And in any jumps for ":break"
8066     compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
8067 							  instr->ga_len, cctx);
8068 
8069     vim_free(scope);
8070 
8071     return arg;
8072 }
8073 
8074 /*
8075  * compile "continue"
8076  */
8077     static char_u *
8078 compile_continue(char_u *arg, cctx_T *cctx)
8079 {
8080     scope_T	*scope = cctx->ctx_scope;
8081     int		try_scopes = 0;
8082     int		loop_label;
8083 
8084     for (;;)
8085     {
8086 	if (scope == NULL)
8087 	{
8088 	    emsg(_(e_continue));
8089 	    return NULL;
8090 	}
8091 	if (scope->se_type == FOR_SCOPE)
8092 	{
8093 	    loop_label = scope->se_u.se_for.fs_top_label;
8094 	    break;
8095 	}
8096 	if (scope->se_type == WHILE_SCOPE)
8097 	{
8098 	    loop_label = scope->se_u.se_while.ws_top_label;
8099 	    break;
8100 	}
8101 	if (scope->se_type == TRY_SCOPE)
8102 	    ++try_scopes;
8103 	scope = scope->se_outer;
8104     }
8105 
8106     if (try_scopes > 0)
8107 	// Inside one or more try/catch blocks we first need to jump to the
8108 	// "finally" or "endtry" to cleanup.
8109 	generate_TRYCONT(cctx, try_scopes, loop_label);
8110     else
8111 	// Jump back to the FOR or WHILE instruction.
8112 	generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
8113 
8114     return arg;
8115 }
8116 
8117 /*
8118  * compile "break"
8119  */
8120     static char_u *
8121 compile_break(char_u *arg, cctx_T *cctx)
8122 {
8123     scope_T	*scope = cctx->ctx_scope;
8124     endlabel_T	**el;
8125 
8126     for (;;)
8127     {
8128 	if (scope == NULL)
8129 	{
8130 	    emsg(_(e_break));
8131 	    return NULL;
8132 	}
8133 	if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
8134 	    break;
8135 	scope = scope->se_outer;
8136     }
8137 
8138     // Jump to the end of the FOR or WHILE loop.
8139     if (scope->se_type == FOR_SCOPE)
8140 	el = &scope->se_u.se_for.fs_end_label;
8141     else
8142 	el = &scope->se_u.se_while.ws_end_label;
8143     if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
8144 	return FAIL;
8145 
8146     return arg;
8147 }
8148 
8149 /*
8150  * compile "{" start of block
8151  */
8152     static char_u *
8153 compile_block(char_u *arg, cctx_T *cctx)
8154 {
8155     if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8156 	return NULL;
8157     return skipwhite(arg + 1);
8158 }
8159 
8160 /*
8161  * compile end of block: drop one scope
8162  */
8163     static void
8164 compile_endblock(cctx_T *cctx)
8165 {
8166     scope_T	*scope = cctx->ctx_scope;
8167 
8168     cctx->ctx_scope = scope->se_outer;
8169     unwind_locals(cctx, scope->se_local_count);
8170     vim_free(scope);
8171 }
8172 
8173 /*
8174  * compile "try"
8175  * Creates a new scope for the try-endtry, pointing to the first catch and
8176  * finally.
8177  * Creates another scope for the "try" block itself.
8178  * TRY instruction sets up exception handling at runtime.
8179  *
8180  *	"try"
8181  *	    TRY -> catch1, -> finally  push trystack entry
8182  *	    ... try block
8183  *	"throw {exception}"
8184  *	    EVAL {exception}
8185  *	    THROW		create exception
8186  *	    ... try block
8187  *	" catch {expr}"
8188  *	    JUMP -> finally
8189  * catch1:  PUSH exception
8190  *	    EVAL {expr}
8191  *	    MATCH
8192  *	    JUMP nomatch -> catch2
8193  *	    CATCH   remove exception
8194  *	    ... catch block
8195  *	" catch"
8196  *	    JUMP -> finally
8197  * catch2:  CATCH   remove exception
8198  *	    ... catch block
8199  *	" finally"
8200  * finally:
8201  *	    ... finally block
8202  *	" endtry"
8203  *	    ENDTRY  pop trystack entry, may rethrow
8204  */
8205     static char_u *
8206 compile_try(char_u *arg, cctx_T *cctx)
8207 {
8208     garray_T	*instr = &cctx->ctx_instr;
8209     scope_T	*try_scope;
8210     scope_T	*scope;
8211 
8212     if (misplaced_cmdmod(cctx))
8213 	return NULL;
8214 
8215     // scope that holds the jumps that go to catch/finally/endtry
8216     try_scope = new_scope(cctx, TRY_SCOPE);
8217     if (try_scope == NULL)
8218 	return NULL;
8219 
8220     if (cctx->ctx_skip != SKIP_YES)
8221     {
8222 	isn_T	*isn;
8223 
8224 	// "try_catch" is set when the first ":catch" is found or when no catch
8225 	// is found and ":finally" is found.
8226 	// "try_finally" is set when ":finally" is found
8227 	// "try_endtry" is set when ":endtry" is found
8228 	try_scope->se_u.se_try.ts_try_label = instr->ga_len;
8229 	if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
8230 	    return NULL;
8231 	isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
8232 	if (isn->isn_arg.try.try_ref == NULL)
8233 	    return NULL;
8234     }
8235 
8236     // scope for the try block itself
8237     scope = new_scope(cctx, BLOCK_SCOPE);
8238     if (scope == NULL)
8239 	return NULL;
8240 
8241     return arg;
8242 }
8243 
8244 /*
8245  * compile "catch {expr}"
8246  */
8247     static char_u *
8248 compile_catch(char_u *arg, cctx_T *cctx UNUSED)
8249 {
8250     scope_T	*scope = cctx->ctx_scope;
8251     garray_T	*instr = &cctx->ctx_instr;
8252     char_u	*p;
8253     isn_T	*isn;
8254 
8255     if (misplaced_cmdmod(cctx))
8256 	return NULL;
8257 
8258     // end block scope from :try or :catch
8259     if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8260 	compile_endblock(cctx);
8261     scope = cctx->ctx_scope;
8262 
8263     // Error if not in a :try scope
8264     if (scope == NULL || scope->se_type != TRY_SCOPE)
8265     {
8266 	emsg(_(e_catch));
8267 	return NULL;
8268     }
8269 
8270     if (scope->se_u.se_try.ts_caught_all)
8271     {
8272 	emsg(_(e_catch_unreachable_after_catch_all));
8273 	return NULL;
8274     }
8275 
8276     if (cctx->ctx_skip != SKIP_YES)
8277     {
8278 #ifdef FEAT_PROFILE
8279 	// the profile-start should be after the jump
8280 	if (cctx->ctx_compile_type == CT_PROFILE
8281 		&& instr->ga_len > 0
8282 		&& ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8283 						   .isn_type == ISN_PROF_START)
8284 	    --instr->ga_len;
8285 #endif
8286 	// Jump from end of previous block to :finally or :endtry
8287 	if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8288 						    JUMP_ALWAYS, cctx) == FAIL)
8289 	    return NULL;
8290 
8291 	// End :try or :catch scope: set value in ISN_TRY instruction
8292 	isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8293 	if (isn->isn_arg.try.try_ref->try_catch == 0)
8294 	    isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
8295 	if (scope->se_u.se_try.ts_catch_label != 0)
8296 	{
8297 	    // Previous catch without match jumps here
8298 	    isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8299 	    isn->isn_arg.jump.jump_where = instr->ga_len;
8300 	}
8301 #ifdef FEAT_PROFILE
8302 	if (cctx->ctx_compile_type == CT_PROFILE)
8303 	{
8304 	    // a "throw" that jumps here needs to be counted
8305 	    generate_instr(cctx, ISN_PROF_END);
8306 	    // the "catch" is also counted
8307 	    generate_instr(cctx, ISN_PROF_START);
8308 	}
8309 #endif
8310 	if (cctx->ctx_compile_type == CT_DEBUG)
8311 	    generate_instr_debug(cctx);
8312     }
8313 
8314     p = skipwhite(arg);
8315     if (ends_excmd2(arg, p))
8316     {
8317 	scope->se_u.se_try.ts_caught_all = TRUE;
8318 	scope->se_u.se_try.ts_catch_label = 0;
8319     }
8320     else
8321     {
8322 	char_u *end;
8323 	char_u *pat;
8324 	char_u *tofree = NULL;
8325 	int	dropped = 0;
8326 	int	len;
8327 
8328 	// Push v:exception, push {expr} and MATCH
8329 	generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8330 
8331 	end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
8332 	if (*end != *p)
8333 	{
8334 	    semsg(_(e_separator_mismatch_str), p);
8335 	    vim_free(tofree);
8336 	    return FAIL;
8337 	}
8338 	if (tofree == NULL)
8339 	    len = (int)(end - (p + 1));
8340 	else
8341 	    len = (int)(end - tofree);
8342 	pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
8343 	vim_free(tofree);
8344 	p += len + 2 + dropped;
8345 	if (pat == NULL)
8346 	    return FAIL;
8347 	if (generate_PUSHS(cctx, pat) == FAIL)
8348 	    return FAIL;
8349 
8350 	if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8351 	    return NULL;
8352 
8353 	scope->se_u.se_try.ts_catch_label = instr->ga_len;
8354 	if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8355 	    return NULL;
8356     }
8357 
8358     if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
8359 	return NULL;
8360 
8361     if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8362 	return NULL;
8363     return p;
8364 }
8365 
8366     static char_u *
8367 compile_finally(char_u *arg, cctx_T *cctx)
8368 {
8369     scope_T	*scope = cctx->ctx_scope;
8370     garray_T	*instr = &cctx->ctx_instr;
8371     isn_T	*isn;
8372     int		this_instr;
8373 
8374     if (misplaced_cmdmod(cctx))
8375 	return NULL;
8376 
8377     // end block scope from :try or :catch
8378     if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8379 	compile_endblock(cctx);
8380     scope = cctx->ctx_scope;
8381 
8382     // Error if not in a :try scope
8383     if (scope == NULL || scope->se_type != TRY_SCOPE)
8384     {
8385 	emsg(_(e_finally));
8386 	return NULL;
8387     }
8388 
8389     // End :catch or :finally scope: set value in ISN_TRY instruction
8390     isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8391     if (isn->isn_arg.try.try_ref->try_finally != 0)
8392     {
8393 	emsg(_(e_finally_dup));
8394 	return NULL;
8395     }
8396 
8397     this_instr = instr->ga_len;
8398 #ifdef FEAT_PROFILE
8399     if (cctx->ctx_compile_type == CT_PROFILE
8400 	    && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8401 						   .isn_type == ISN_PROF_START)
8402 	// jump to the profile start of the "finally"
8403 	--this_instr;
8404 #endif
8405 
8406     // Fill in the "end" label in jumps at the end of the blocks.
8407     compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8408 							     this_instr, cctx);
8409 
8410     // If there is no :catch then an exception jumps to :finally.
8411     if (isn->isn_arg.try.try_ref->try_catch == 0)
8412 	isn->isn_arg.try.try_ref->try_catch = this_instr;
8413     isn->isn_arg.try.try_ref->try_finally = this_instr;
8414     if (scope->se_u.se_try.ts_catch_label != 0)
8415     {
8416 	// Previous catch without match jumps here
8417 	isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8418 	isn->isn_arg.jump.jump_where = this_instr;
8419 	scope->se_u.se_try.ts_catch_label = 0;
8420     }
8421     if (generate_instr(cctx, ISN_FINALLY) == NULL)
8422 	return NULL;
8423 
8424     // TODO: set index in ts_finally_label jumps
8425 
8426     return arg;
8427 }
8428 
8429     static char_u *
8430 compile_endtry(char_u *arg, cctx_T *cctx)
8431 {
8432     scope_T	*scope = cctx->ctx_scope;
8433     garray_T	*instr = &cctx->ctx_instr;
8434     isn_T	*try_isn;
8435 
8436     if (misplaced_cmdmod(cctx))
8437 	return NULL;
8438 
8439     // end block scope from :catch or :finally
8440     if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8441 	compile_endblock(cctx);
8442     scope = cctx->ctx_scope;
8443 
8444     // Error if not in a :try scope
8445     if (scope == NULL || scope->se_type != TRY_SCOPE)
8446     {
8447 	if (scope == NULL)
8448 	    emsg(_(e_no_endtry));
8449 	else if (scope->se_type == WHILE_SCOPE)
8450 	    emsg(_(e_endwhile));
8451 	else if (scope->se_type == FOR_SCOPE)
8452 	    emsg(_(e_endfor));
8453 	else
8454 	    emsg(_(e_endif));
8455 	return NULL;
8456     }
8457 
8458     try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
8459     if (cctx->ctx_skip != SKIP_YES)
8460     {
8461 	if (try_isn->isn_arg.try.try_ref->try_catch == 0
8462 				      && try_isn->isn_arg.try.try_ref->try_finally == 0)
8463 	{
8464 	    emsg(_(e_missing_catch_or_finally));
8465 	    return NULL;
8466 	}
8467 
8468 #ifdef FEAT_PROFILE
8469 	if (cctx->ctx_compile_type == CT_PROFILE
8470 		&& ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8471 						.isn_type == ISN_PROF_START)
8472 	    // move the profile start after "endtry" so that it's not counted when
8473 	    // the exception is rethrown.
8474 	    --instr->ga_len;
8475 #endif
8476 
8477 	// Fill in the "end" label in jumps at the end of the blocks, if not
8478 	// done by ":finally".
8479 	compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8480 							  instr->ga_len, cctx);
8481 
8482 	if (scope->se_u.se_try.ts_catch_label != 0)
8483 	{
8484 	    // Last catch without match jumps here
8485 	    isn_T *isn = ((isn_T *)instr->ga_data)
8486 					   + scope->se_u.se_try.ts_catch_label;
8487 	    isn->isn_arg.jump.jump_where = instr->ga_len;
8488 	}
8489     }
8490 
8491     compile_endblock(cctx);
8492 
8493     if (cctx->ctx_skip != SKIP_YES)
8494     {
8495 	// End :catch or :finally scope: set instruction index in ISN_TRY
8496 	// instruction
8497 	try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
8498 	if (cctx->ctx_skip != SKIP_YES
8499 				   && generate_instr(cctx, ISN_ENDTRY) == NULL)
8500 	    return NULL;
8501 #ifdef FEAT_PROFILE
8502 	if (cctx->ctx_compile_type == CT_PROFILE)
8503 	    generate_instr(cctx, ISN_PROF_START);
8504 #endif
8505     }
8506     return arg;
8507 }
8508 
8509 /*
8510  * compile "throw {expr}"
8511  */
8512     static char_u *
8513 compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8514 {
8515     char_u *p = skipwhite(arg);
8516 
8517     if (compile_expr0(&p, cctx) == FAIL)
8518 	return NULL;
8519     if (cctx->ctx_skip == SKIP_YES)
8520 	return p;
8521     if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
8522 	return NULL;
8523     if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8524 	return NULL;
8525 
8526     return p;
8527 }
8528 
8529 /*
8530  * compile "echo expr"
8531  * compile "echomsg expr"
8532  * compile "echoerr expr"
8533  * compile "execute expr"
8534  */
8535     static char_u *
8536 compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
8537 {
8538     char_u	*p = arg;
8539     char_u	*prev = arg;
8540     char_u	*expr_start;
8541     int		count = 0;
8542     int		start_ctx_lnum = cctx->ctx_lnum;
8543     garray_T	*stack = &cctx->ctx_type_stack;
8544     type_T	*type;
8545 
8546     for (;;)
8547     {
8548 	if (ends_excmd2(prev, p))
8549 	    break;
8550 	expr_start = p;
8551 	if (compile_expr0(&p, cctx) == FAIL)
8552 	    return NULL;
8553 
8554 	if (cctx->ctx_skip != SKIP_YES)
8555 	{
8556 	    // check for non-void type
8557 	    type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8558 	    if (type->tt_type == VAR_VOID)
8559 	    {
8560 		semsg(_(e_expression_does_not_result_in_value_str), expr_start);
8561 		return NULL;
8562 	    }
8563 	}
8564 
8565 	++count;
8566 	prev = p;
8567 	p = skipwhite(p);
8568     }
8569 
8570     if (count > 0)
8571     {
8572 	long save_lnum = cctx->ctx_lnum;
8573 
8574 	// Use the line number where the command started.
8575 	cctx->ctx_lnum = start_ctx_lnum;
8576 
8577 	if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8578 	    generate_ECHO(cctx, cmdidx == CMD_echo, count);
8579 	else if (cmdidx == CMD_execute)
8580 	    generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8581 	else if (cmdidx == CMD_echomsg)
8582 	    generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8583 	else
8584 	    generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
8585 
8586 	cctx->ctx_lnum = save_lnum;
8587     }
8588     return p;
8589 }
8590 
8591 /*
8592  * If "eap" has a range that is not a constant generate an ISN_RANGE
8593  * instruction to compute it and return OK.
8594  * Otherwise return FAIL, the caller must deal with any range.
8595  */
8596     static int
8597 compile_variable_range(exarg_T *eap, cctx_T *cctx)
8598 {
8599     char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8600     char_u *p = skipdigits(eap->cmd);
8601 
8602     if (p == range_end)
8603 	return FAIL;
8604     return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8605 }
8606 
8607 /*
8608  * :put r
8609  * :put ={expr}
8610  */
8611     static char_u *
8612 compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8613 {
8614     char_u	*line = arg;
8615     linenr_T	lnum;
8616     char	*errormsg;
8617     int		above = eap->forceit;
8618 
8619     eap->regname = *line;
8620 
8621     if (eap->regname == '=')
8622     {
8623 	char_u *p = line + 1;
8624 
8625 	if (compile_expr0(&p, cctx) == FAIL)
8626 	    return NULL;
8627 	line = p;
8628     }
8629     else if (eap->regname != NUL)
8630 	++line;
8631 
8632     if (compile_variable_range(eap, cctx) == OK)
8633     {
8634 	lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8635     }
8636     else
8637     {
8638 	// Either no range or a number.
8639 	// "errormsg" will not be set because the range is ADDR_LINES.
8640 	if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
8641 	    // cannot happen
8642 	    return NULL;
8643 	if (eap->addr_count == 0)
8644 	    lnum = -1;
8645 	else
8646 	    lnum = eap->line2;
8647 	if (above)
8648 	    --lnum;
8649     }
8650 
8651     generate_PUT(cctx, eap->regname, lnum);
8652     return line;
8653 }
8654 
8655 /*
8656  * A command that is not compiled, execute with legacy code.
8657  */
8658     static char_u *
8659 compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8660 {
8661     char_u	*p;
8662     int		has_expr = FALSE;
8663     char_u	*nextcmd = (char_u *)"";
8664 
8665     if (cctx->ctx_skip == SKIP_YES)
8666 	goto theend;
8667 
8668     // If there was a prececing command modifier, drop it and include it in the
8669     // EXEC command.
8670     if (cctx->ctx_has_cmdmod)
8671     {
8672 	garray_T	*instr = &cctx->ctx_instr;
8673 	isn_T		*isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
8674 
8675 	if (isn->isn_type == ISN_CMDMOD)
8676 	{
8677 	    vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8678 					       ->cmod_filter_regmatch.regprog);
8679 	    vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8680 	    --instr->ga_len;
8681 	    cctx->ctx_has_cmdmod = FALSE;
8682 	}
8683     }
8684 
8685     if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
8686     {
8687 	long	argt = eap->argt;
8688 	int	usefilter = FALSE;
8689 
8690 	has_expr = argt & (EX_XFILE | EX_EXPAND);
8691 
8692 	// If the command can be followed by a bar, find the bar and truncate
8693 	// it, so that the following command can be compiled.
8694 	// The '|' is overwritten with a NUL, it is put back below.
8695 	if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8696 							   && *eap->arg == '!')
8697 	    // :w !filter or :r !filter or :r! filter
8698 	    usefilter = TRUE;
8699 	if ((argt & EX_TRLBAR) && !usefilter)
8700 	{
8701 	    eap->argt = argt;
8702 	    separate_nextcmd(eap);
8703 	    if (eap->nextcmd != NULL)
8704 		nextcmd = eap->nextcmd;
8705 	}
8706 	else if (eap->cmdidx == CMD_wincmd)
8707 	{
8708 	    p = eap->arg;
8709 	    if (*p != NUL)
8710 		++p;
8711 	    if (*p == 'g' || *p == Ctrl_G)
8712 		++p;
8713 	    p = skipwhite(p);
8714 	    if (*p == '|')
8715 	    {
8716 		*p = NUL;
8717 		nextcmd = p + 1;
8718 	    }
8719 	}
8720     }
8721 
8722     if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8723     {
8724 	// expand filename in "syntax include [@group] filename"
8725 	has_expr = TRUE;
8726 	eap->arg = skipwhite(eap->arg + 7);
8727 	if (*eap->arg == '@')
8728 	    eap->arg = skiptowhite(eap->arg);
8729     }
8730 
8731     if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8732 						       && STRLEN(eap->arg) > 4)
8733     {
8734 	int delim = *eap->arg;
8735 
8736 	p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
8737 	if (*p == delim)
8738 	{
8739 	    eap->arg = p + 1;
8740 	    has_expr = TRUE;
8741 	}
8742     }
8743 
8744     if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8745     {
8746 	// TODO: should only expand when appropriate for the command
8747 	eap->arg = skiptowhite(eap->arg);
8748 	has_expr = TRUE;
8749     }
8750 
8751     if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
8752     {
8753 	int	count = 0;
8754 	char_u	*start = skipwhite(line);
8755 
8756 	// :cmd xxx`=expr1`yyy`=expr2`zzz
8757 	// PUSHS ":cmd xxx"
8758 	// eval expr1
8759 	// PUSHS "yyy"
8760 	// eval expr2
8761 	// PUSHS "zzz"
8762 	// EXECCONCAT 5
8763 	for (;;)
8764 	{
8765 	    if (p > start)
8766 	    {
8767 		generate_PUSHS(cctx, vim_strnsave(start, p - start));
8768 		++count;
8769 	    }
8770 	    p += 2;
8771 	    if (compile_expr0(&p, cctx) == FAIL)
8772 		return NULL;
8773 	    may_generate_2STRING(-1, TRUE, cctx);
8774 	    ++count;
8775 	    p = skipwhite(p);
8776 	    if (*p != '`')
8777 	    {
8778 		emsg(_(e_missing_backtick));
8779 		return NULL;
8780 	    }
8781 	    start = p + 1;
8782 
8783 	    p = (char_u *)strstr((char *)start, "`=");
8784 	    if (p == NULL)
8785 	    {
8786 		if (*skipwhite(start) != NUL)
8787 		{
8788 		    generate_PUSHS(cctx, vim_strsave(start));
8789 		    ++count;
8790 		}
8791 		break;
8792 	    }
8793 	}
8794 	generate_EXECCONCAT(cctx, count);
8795     }
8796     else
8797 	generate_EXEC(cctx, line);
8798 
8799 theend:
8800     if (*nextcmd != NUL)
8801     {
8802 	// the parser expects a pointer to the bar, put it back
8803 	--nextcmd;
8804 	*nextcmd = '|';
8805     }
8806 
8807     return nextcmd;
8808 }
8809 
8810 /*
8811  * A script command with heredoc, e.g.
8812  *	ruby << EOF
8813  *	   command
8814  *	EOF
8815  * Has been turned into one long line with NL characters by
8816  * get_function_body():
8817  *	ruby << EOF<NL>   command<NL>EOF
8818  */
8819     static char_u *
8820 compile_script(char_u *line, cctx_T *cctx)
8821 {
8822     if (cctx->ctx_skip != SKIP_YES)
8823     {
8824 	isn_T	*isn;
8825 
8826 	if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
8827 	    return NULL;
8828 	isn->isn_arg.string = vim_strsave(line);
8829     }
8830     return (char_u *)"";
8831 }
8832 
8833 
8834 /*
8835  * :s/pat/repl/
8836  */
8837     static char_u *
8838 compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8839 {
8840     char_u  *cmd = eap->arg;
8841     char_u  *expr = (char_u *)strstr((char *)cmd, "\\=");
8842 
8843     if (expr != NULL)
8844     {
8845 	int delimiter = *cmd++;
8846 
8847 	// There is a \=expr, find it in the substitute part.
8848 	cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
8849 	if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8850 	{
8851 	    garray_T	save_ga = cctx->ctx_instr;
8852 	    char_u	*end;
8853 	    int		expr_res;
8854 	    int		trailing_error;
8855 	    int		instr_count;
8856 	    isn_T	*instr;
8857 	    isn_T	*isn;
8858 
8859 	    cmd += 3;
8860 	    end = skip_substitute(cmd, delimiter);
8861 
8862 	    // Temporarily reset the list of instructions so that the jump
8863 	    // labels are correct.
8864 	    cctx->ctx_instr.ga_len = 0;
8865 	    cctx->ctx_instr.ga_maxlen = 0;
8866 	    cctx->ctx_instr.ga_data = NULL;
8867 	    expr_res = compile_expr0(&cmd, cctx);
8868 	    if (end[-1] == NUL)
8869 		end[-1] = delimiter;
8870 	    cmd = skipwhite(cmd);
8871 	    trailing_error = *cmd != delimiter && *cmd != NUL;
8872 
8873 	    if (expr_res == FAIL || trailing_error
8874 				       || ga_grow(&cctx->ctx_instr, 1) == FAIL)
8875 	    {
8876 		if (trailing_error)
8877 		    semsg(_(e_trailing_arg), cmd);
8878 		clear_instr_ga(&cctx->ctx_instr);
8879 		cctx->ctx_instr = save_ga;
8880 		return NULL;
8881 	    }
8882 
8883 	    // Move the generated instructions into the ISN_SUBSTITUTE
8884 	    // instructions, then restore the list of instructions before
8885 	    // adding the ISN_SUBSTITUTE instruction.
8886 	    instr_count = cctx->ctx_instr.ga_len;
8887 	    instr = cctx->ctx_instr.ga_data;
8888 	    instr[instr_count].isn_type = ISN_FINISH;
8889 
8890 	    cctx->ctx_instr = save_ga;
8891 	    if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
8892 	    {
8893 		int idx;
8894 
8895 		for (idx = 0; idx < instr_count; ++idx)
8896 		    delete_instr(instr + idx);
8897 		vim_free(instr);
8898 		return NULL;
8899 	    }
8900 	    isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
8901 	    isn->isn_arg.subs.subs_instr = instr;
8902 
8903 	    // skip over flags
8904 	    if (*end == '&')
8905 		++end;
8906 	    while (ASCII_ISALPHA(*end) || *end == '#')
8907 		++end;
8908 	    return end;
8909 	}
8910     }
8911 
8912     return compile_exec(arg, eap, cctx);
8913 }
8914 
8915     static char_u *
8916 compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
8917 {
8918     char_u *arg = eap->arg;
8919     lhs_T	*lhs = &cctx->ctx_redir_lhs;
8920 
8921     if (lhs->lhs_name != NULL)
8922     {
8923 	if (STRNCMP(arg, "END", 3) == 0)
8924 	{
8925 	    if (lhs->lhs_append)
8926 	    {
8927 		// First load the current variable value.
8928 		if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
8929 								 cctx) == FAIL)
8930 		    return NULL;
8931 	    }
8932 
8933 	    // Gets the redirected text and put it on the stack, then store it
8934 	    // in the variable.
8935 	    generate_instr_type(cctx, ISN_REDIREND, &t_string);
8936 
8937 	    if (lhs->lhs_append)
8938 		generate_instr_drop(cctx, ISN_CONCAT, 1);
8939 
8940 	    if (lhs->lhs_has_index)
8941 	    {
8942 		// Use the info in "lhs" to store the value at the index in the
8943 		// list or dict.
8944 		if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
8945 						      &t_string, cctx) == FAIL)
8946 		    return NULL;
8947 	    }
8948 	    else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
8949 		return NULL;
8950 
8951 	    VIM_CLEAR(lhs->lhs_name);
8952 	    VIM_CLEAR(lhs->lhs_whole);
8953 	    return arg + 3;
8954 	}
8955 	emsg(_(e_cannot_nest_redir));
8956 	return NULL;
8957     }
8958 
8959     if (arg[0] == '=' && arg[1] == '>')
8960     {
8961 	int	    append = FALSE;
8962 
8963 	// redirect to a variable is compiled
8964 	arg += 2;
8965 	if (*arg == '>')
8966 	{
8967 	    ++arg;
8968 	    append = TRUE;
8969 	}
8970 	arg = skipwhite(arg);
8971 
8972 	if (compile_assign_lhs(arg, lhs, CMD_redir,
8973 						FALSE, FALSE, 1, cctx) == FAIL)
8974 	    return NULL;
8975 	generate_instr(cctx, ISN_REDIRSTART);
8976 	lhs->lhs_append = append;
8977 	if (lhs->lhs_has_index)
8978 	{
8979 	    lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
8980 	    if (lhs->lhs_whole == NULL)
8981 		return NULL;
8982 	}
8983 
8984 	return arg + lhs->lhs_varlen_total;
8985     }
8986 
8987     // other redirects are handled like at script level
8988     return compile_exec(line, eap, cctx);
8989 }
8990 
8991 #ifdef FEAT_QUICKFIX
8992     static char_u *
8993 compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
8994 {
8995     isn_T	*isn;
8996     char_u	*p;
8997 
8998     isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
8999     if (isn == NULL)
9000 	return NULL;
9001     isn->isn_arg.number = eap->cmdidx;
9002 
9003     p = eap->arg;
9004     if (compile_expr0(&p, cctx) == FAIL)
9005 	return NULL;
9006 
9007     isn = generate_instr(cctx, ISN_CEXPR_CORE);
9008     if (isn == NULL)
9009 	return NULL;
9010     isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
9011     if (isn->isn_arg.cexpr.cexpr_ref == NULL)
9012 	return NULL;
9013     isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
9014     isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
9015     isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
9016 
9017     return p;
9018 }
9019 #endif
9020 
9021 /*
9022  * Add a function to the list of :def functions.
9023  * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
9024  */
9025     static int
9026 add_def_function(ufunc_T *ufunc)
9027 {
9028     dfunc_T *dfunc;
9029 
9030     if (def_functions.ga_len == 0)
9031     {
9032 	// The first position is not used, so that a zero uf_dfunc_idx means it
9033 	// wasn't set.
9034 	if (ga_grow(&def_functions, 1) == FAIL)
9035 	    return FAIL;
9036 	++def_functions.ga_len;
9037     }
9038 
9039     // Add the function to "def_functions".
9040     if (ga_grow(&def_functions, 1) == FAIL)
9041 	return FAIL;
9042     dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
9043     CLEAR_POINTER(dfunc);
9044     dfunc->df_idx = def_functions.ga_len;
9045     ufunc->uf_dfunc_idx = dfunc->df_idx;
9046     dfunc->df_ufunc = ufunc;
9047     dfunc->df_name = vim_strsave(ufunc->uf_name);
9048     ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
9049     ++dfunc->df_refcount;
9050     ++def_functions.ga_len;
9051     return OK;
9052 }
9053 
9054 /*
9055  * After ex_function() has collected all the function lines: parse and compile
9056  * the lines into instructions.
9057  * Adds the function to "def_functions".
9058  * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
9059  * the return statement (used for lambda).  When uf_ret_type is already set
9060  * then check that it matches.
9061  * When "profiling" is true add ISN_PROF_START instructions.
9062  * "outer_cctx" is set for a nested function.
9063  * This can be used recursively through compile_lambda(), which may reallocate
9064  * "def_functions".
9065  * Returns OK or FAIL.
9066  */
9067     int
9068 compile_def_function(
9069 	ufunc_T		*ufunc,
9070 	int		check_return_type,
9071 	compiletype_T   compile_type,
9072 	cctx_T		*outer_cctx)
9073 {
9074     char_u	*line = NULL;
9075     char_u	*line_to_free = NULL;
9076     char_u	*p;
9077     char	*errormsg = NULL;	// error message
9078     cctx_T	cctx;
9079     garray_T	*instr;
9080     int		did_emsg_before = did_emsg;
9081     int		did_emsg_silent_before = did_emsg_silent;
9082     int		ret = FAIL;
9083     sctx_T	save_current_sctx = current_sctx;
9084     int		save_estack_compiling = estack_compiling;
9085     int		save_cmod_flags = cmdmod.cmod_flags;
9086     int		do_estack_push;
9087     int		new_def_function = FALSE;
9088 #ifdef FEAT_PROFILE
9089     int		prof_lnum = -1;
9090 #endif
9091     int		debug_lnum = -1;
9092 
9093     // When using a function that was compiled before: Free old instructions.
9094     // The index is reused.  Otherwise add a new entry in "def_functions".
9095     if (ufunc->uf_dfunc_idx > 0)
9096     {
9097 	dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9098 							 + ufunc->uf_dfunc_idx;
9099 	isn_T	*instr_dest = NULL;
9100 
9101 	switch (compile_type)
9102 	{
9103 	    case CT_PROFILE:
9104 #ifdef FEAT_PROFILE
9105 			    instr_dest = dfunc->df_instr_prof; break;
9106 #endif
9107 	    case CT_NONE:   instr_dest = dfunc->df_instr; break;
9108 	    case CT_DEBUG:  instr_dest = dfunc->df_instr_debug; break;
9109 	}
9110 	if (instr_dest != NULL)
9111 	    // Was compiled in this mode before: Free old instructions.
9112 	    delete_def_function_contents(dfunc, FALSE);
9113 	ga_clear_strings(&dfunc->df_var_names);
9114     }
9115     else
9116     {
9117 	if (add_def_function(ufunc) == FAIL)
9118 	    return FAIL;
9119 	new_def_function = TRUE;
9120     }
9121 
9122     ufunc->uf_def_status = UF_COMPILING;
9123 
9124     CLEAR_FIELD(cctx);
9125 
9126     cctx.ctx_compile_type = compile_type;
9127     cctx.ctx_ufunc = ufunc;
9128     cctx.ctx_lnum = -1;
9129     cctx.ctx_outer = outer_cctx;
9130     ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
9131     ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
9132     ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
9133     cctx.ctx_type_list = &ufunc->uf_type_list;
9134     ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
9135     instr = &cctx.ctx_instr;
9136 
9137     // Set the context to the function, it may be compiled when called from
9138     // another script.  Set the script version to the most modern one.
9139     // The line number will be set in next_line_from_context().
9140     current_sctx = ufunc->uf_script_ctx;
9141     current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9142 
9143     // Don't use the flag from ":legacy" here.
9144     cmdmod.cmod_flags &= ~CMOD_LEGACY;
9145 
9146     // Make sure error messages are OK.
9147     do_estack_push = !estack_top_is_ufunc(ufunc, 1);
9148     if (do_estack_push)
9149 	estack_push_ufunc(ufunc, 1);
9150     estack_compiling = TRUE;
9151 
9152     if (ufunc->uf_def_args.ga_len > 0)
9153     {
9154 	int	count = ufunc->uf_def_args.ga_len;
9155 	int	first_def_arg = ufunc->uf_args.ga_len - count;
9156 	int	uf_args_len = ufunc->uf_args.ga_len;
9157 	int	i;
9158 	char_u	*arg;
9159 	int	off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
9160 	int	did_set_arg_type = FALSE;
9161 
9162 	// Produce instructions for the default values of optional arguments.
9163 	SOURCING_LNUM = 0;  // line number unknown
9164 	for (i = 0; i < count; ++i)
9165 	{
9166 	    garray_T	*stack = &cctx.ctx_type_stack;
9167 	    type_T	*val_type;
9168 	    int		arg_idx = first_def_arg + i;
9169 	    where_T	where;
9170 	    int		r;
9171 	    int		jump_instr_idx = instr->ga_len;
9172 	    isn_T	*isn;
9173 
9174 	    // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
9175 	    if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
9176 		goto erret;
9177 
9178 	    // Make sure later arguments are not found.
9179 	    ufunc->uf_args.ga_len = i;
9180 
9181 	    arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
9182 	    r = compile_expr0(&arg, &cctx);
9183 
9184 	    ufunc->uf_args.ga_len = uf_args_len;
9185 	    if (r == FAIL)
9186 		goto erret;
9187 
9188 	    // If no type specified use the type of the default value.
9189 	    // Otherwise check that the default value type matches the
9190 	    // specified type.
9191 	    val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
9192 	    where.wt_index = arg_idx + 1;
9193 	    where.wt_variable = FALSE;
9194 	    if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
9195 	    {
9196 		did_set_arg_type = TRUE;
9197 		ufunc->uf_arg_types[arg_idx] = val_type;
9198 	    }
9199 	    else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
9200 							  TRUE, where) == FAIL)
9201 		goto erret;
9202 
9203 	    if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
9204 		goto erret;
9205 
9206 	    // set instruction index in JUMP_IF_ARG_SET to here
9207 	    isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
9208 	    isn->isn_arg.jumparg.jump_where = instr->ga_len;
9209 	}
9210 
9211 	if (did_set_arg_type)
9212 	    set_function_type(ufunc);
9213     }
9214 
9215     /*
9216      * Loop over all the lines of the function and generate instructions.
9217      */
9218     for (;;)
9219     {
9220 	exarg_T	    ea;
9221 	int	    starts_with_colon = FALSE;
9222 	char_u	    *cmd;
9223 	cmdmod_T    local_cmdmod;
9224 
9225 	// Bail out on the first error to avoid a flood of errors and report
9226 	// the right line number when inside try/catch.
9227 	if (did_emsg_before != did_emsg)
9228 	    goto erret;
9229 
9230 	if (line != NULL && *line == '|')
9231 	    // the line continues after a '|'
9232 	    ++line;
9233 	else if (line != NULL && *skipwhite(line) != NUL
9234 		&& !(*line == '#' && (line == cctx.ctx_line_start
9235 						    || VIM_ISWHITE(line[-1]))))
9236 	{
9237 	    semsg(_(e_trailing_arg), line);
9238 	    goto erret;
9239 	}
9240 	else if (line != NULL && vim9_bad_comment(skipwhite(line)))
9241 	    goto erret;
9242 	else
9243 	{
9244 	    line = next_line_from_context(&cctx, FALSE);
9245 	    if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
9246 	    {
9247 		// beyond the last line
9248 #ifdef FEAT_PROFILE
9249 		if (cctx.ctx_skip != SKIP_YES)
9250 		    may_generate_prof_end(&cctx, prof_lnum);
9251 #endif
9252 		break;
9253 	    }
9254 	    // Make a copy, splitting off nextcmd and removing trailing spaces
9255 	    // may change it.
9256 	    if (line != NULL)
9257 	    {
9258 		line = vim_strsave(line);
9259 		vim_free(line_to_free);
9260 		line_to_free = line;
9261 	    }
9262 	}
9263 
9264 	CLEAR_FIELD(ea);
9265 	ea.cmdlinep = &line;
9266 	ea.cmd = skipwhite(line);
9267 
9268 	if (*ea.cmd == '#')
9269 	{
9270 	    // "#" starts a comment
9271 	    line = (char_u *)"";
9272 	    continue;
9273 	}
9274 
9275 #ifdef FEAT_PROFILE
9276 	if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
9277 						  && cctx.ctx_skip != SKIP_YES)
9278 	{
9279 	    may_generate_prof_end(&cctx, prof_lnum);
9280 
9281 	    prof_lnum = cctx.ctx_lnum;
9282 	    generate_instr(&cctx, ISN_PROF_START);
9283 	}
9284 #endif
9285 	if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
9286 						  && cctx.ctx_skip != SKIP_YES)
9287 	{
9288 	    debug_lnum = cctx.ctx_lnum;
9289 	    generate_instr_debug(&cctx);
9290 	}
9291 	cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
9292 
9293 	// Some things can be recognized by the first character.
9294 	switch (*ea.cmd)
9295 	{
9296 	    case '}':
9297 		{
9298 		    // "}" ends a block scope
9299 		    scopetype_T stype = cctx.ctx_scope == NULL
9300 					  ? NO_SCOPE : cctx.ctx_scope->se_type;
9301 
9302 		    if (stype == BLOCK_SCOPE)
9303 		    {
9304 			compile_endblock(&cctx);
9305 			line = ea.cmd;
9306 		    }
9307 		    else
9308 		    {
9309 			emsg(_(e_using_rcurly_outside_if_block_scope));
9310 			goto erret;
9311 		    }
9312 		    if (line != NULL)
9313 			line = skipwhite(ea.cmd + 1);
9314 		    continue;
9315 		}
9316 
9317 	    case '{':
9318 		// "{" starts a block scope
9319 		// "{'a': 1}->func() is something else
9320 		if (ends_excmd(*skipwhite(ea.cmd + 1)))
9321 		{
9322 		    line = compile_block(ea.cmd, &cctx);
9323 		    continue;
9324 		}
9325 		break;
9326 	}
9327 
9328 	/*
9329 	 * COMMAND MODIFIERS
9330 	 */
9331 	cctx.ctx_has_cmdmod = FALSE;
9332 	if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
9333 								       == FAIL)
9334 	{
9335 	    if (errormsg != NULL)
9336 		goto erret;
9337 	    // empty line or comment
9338 	    line = (char_u *)"";
9339 	    continue;
9340 	}
9341 	generate_cmdmods(&cctx, &local_cmdmod);
9342 	undo_cmdmod(&local_cmdmod);
9343 
9344 	// Check if there was a colon after the last command modifier or before
9345 	// the current position.
9346 	for (p = ea.cmd; p >= line; --p)
9347 	{
9348 	    if (*p == ':')
9349 		starts_with_colon = TRUE;
9350 	    if (p < ea.cmd && !VIM_ISWHITE(*p))
9351 		break;
9352 	}
9353 
9354 	// Skip ":call" to get to the function name, unless using :legacy
9355 	p = ea.cmd;
9356 	if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
9357 	{
9358 	    if (checkforcmd(&ea.cmd, "call", 3))
9359 	    {
9360 		if (*ea.cmd == '(')
9361 		    // not for "call()"
9362 		    ea.cmd = p;
9363 		else
9364 		    ea.cmd = skipwhite(ea.cmd);
9365 	    }
9366 
9367 	    if (!starts_with_colon)
9368 	    {
9369 		int	    assign;
9370 
9371 		// Check for assignment after command modifiers.
9372 		assign = may_compile_assignment(&ea, &line, &cctx);
9373 		if (assign == OK)
9374 		    goto nextline;
9375 		if (assign == FAIL)
9376 		    goto erret;
9377 	    }
9378 	}
9379 
9380 	/*
9381 	 * COMMAND after range
9382 	 * 'text'->func() should not be confused with 'a mark
9383 	 * "++nr" and "--nr" are eval commands
9384 	 */
9385 	cmd = ea.cmd;
9386 	if (!(local_cmdmod.cmod_flags & CMOD_LEGACY)
9387 		&& (starts_with_colon || !(*cmd == '\''
9388 		       || (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
9389 	{
9390 	    ea.cmd = skip_range(ea.cmd, TRUE, NULL);
9391 	    if (ea.cmd > cmd)
9392 	    {
9393 		if (!starts_with_colon)
9394 		{
9395 		    semsg(_(e_colon_required_before_range_str), cmd);
9396 		    goto erret;
9397 		}
9398 		ea.addr_count = 1;
9399 		if (ends_excmd2(line, ea.cmd))
9400 		{
9401 		    // A range without a command: jump to the line.
9402 		    // TODO: compile to a more efficient command, possibly
9403 		    // calling parse_cmd_address().
9404 		    ea.cmdidx = CMD_SIZE;
9405 		    line = compile_exec(line, &ea, &cctx);
9406 		    goto nextline;
9407 		}
9408 	    }
9409 	}
9410 	p = find_ex_command(&ea, NULL, starts_with_colon
9411 						  ? NULL : item_exists, &cctx);
9412 
9413 	if (p == NULL)
9414 	{
9415 	    if (cctx.ctx_skip != SKIP_YES)
9416 		emsg(_(e_ambiguous_use_of_user_defined_command));
9417 	    goto erret;
9418 	}
9419 
9420 	// When using ":legacy cmd" always use compile_exec().
9421 	if (local_cmdmod.cmod_flags & CMOD_LEGACY)
9422 	{
9423 	    char_u *start = ea.cmd;
9424 
9425 	    switch (ea.cmdidx)
9426 	    {
9427 		case CMD_if:
9428 		case CMD_elseif:
9429 		case CMD_else:
9430 		case CMD_endif:
9431 		case CMD_for:
9432 		case CMD_endfor:
9433 		case CMD_continue:
9434 		case CMD_break:
9435 		case CMD_while:
9436 		case CMD_endwhile:
9437 		case CMD_try:
9438 		case CMD_catch:
9439 		case CMD_finally:
9440 		case CMD_endtry:
9441 			semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
9442 			goto erret;
9443 		default: break;
9444 	    }
9445 
9446 	    // ":legacy return expr" needs to be handled differently.
9447 	    if (checkforcmd(&start, "return", 4))
9448 		ea.cmdidx = CMD_return;
9449 	    else
9450 		ea.cmdidx = CMD_legacy;
9451 	}
9452 
9453 	if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
9454 	{
9455 	    if (cctx.ctx_skip == SKIP_YES)
9456 	    {
9457 		line += STRLEN(line);
9458 		goto nextline;
9459 	    }
9460 
9461 	    // Expression or function call.
9462 	    if (ea.cmdidx != CMD_eval)
9463 	    {
9464 		// CMD_var cannot happen, compile_assignment() above would be
9465 		// used.  Most likely an assignment to a non-existing variable.
9466 		semsg(_(e_command_not_recognized_str), ea.cmd);
9467 		goto erret;
9468 	    }
9469 	}
9470 
9471 	if (cctx.ctx_had_return
9472 		&& ea.cmdidx != CMD_elseif
9473 		&& ea.cmdidx != CMD_else
9474 		&& ea.cmdidx != CMD_endif
9475 		&& ea.cmdidx != CMD_endfor
9476 		&& ea.cmdidx != CMD_endwhile
9477 		&& ea.cmdidx != CMD_catch
9478 		&& ea.cmdidx != CMD_finally
9479 		&& ea.cmdidx != CMD_endtry)
9480 	{
9481 	    emsg(_(e_unreachable_code_after_return));
9482 	    goto erret;
9483 	}
9484 
9485 	p = skipwhite(p);
9486 	if (ea.cmdidx != CMD_SIZE
9487 			    && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
9488 	{
9489 	    if (ea.cmdidx >= 0)
9490 		ea.argt = excmd_get_argt(ea.cmdidx);
9491 	    if ((ea.argt & EX_BANG) && *p == '!')
9492 	    {
9493 		ea.forceit = TRUE;
9494 		p = skipwhite(p + 1);
9495 	    }
9496 	}
9497 
9498 	switch (ea.cmdidx)
9499 	{
9500 	    case CMD_def:
9501 		    ea.arg = p;
9502 		    line = compile_nested_function(&ea, &cctx);
9503 		    break;
9504 
9505 	    case CMD_function:
9506 		    // TODO: should we allow this, e.g. to declare a global
9507 		    // function?
9508 		    emsg(_(e_cannot_use_function_inside_def));
9509 		    goto erret;
9510 
9511 	    case CMD_return:
9512 		    line = compile_return(p, check_return_type,
9513 				 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
9514 		    cctx.ctx_had_return = TRUE;
9515 		    break;
9516 
9517 	    case CMD_let:
9518 		    emsg(_(e_cannot_use_let_in_vim9_script));
9519 		    break;
9520 	    case CMD_var:
9521 	    case CMD_final:
9522 	    case CMD_const:
9523 	    case CMD_increment:
9524 	    case CMD_decrement:
9525 		    line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
9526 		    if (line == p)
9527 			line = NULL;
9528 		    break;
9529 
9530 	    case CMD_unlet:
9531 	    case CMD_unlockvar:
9532 	    case CMD_lockvar:
9533 		    line = compile_unletlock(p, &ea, &cctx);
9534 		    break;
9535 
9536 	    case CMD_import:
9537 		    line = compile_import(p, &cctx);
9538 		    break;
9539 
9540 	    case CMD_if:
9541 		    line = compile_if(p, &cctx);
9542 		    break;
9543 	    case CMD_elseif:
9544 		    line = compile_elseif(p, &cctx);
9545 		    cctx.ctx_had_return = FALSE;
9546 		    break;
9547 	    case CMD_else:
9548 		    line = compile_else(p, &cctx);
9549 		    cctx.ctx_had_return = FALSE;
9550 		    break;
9551 	    case CMD_endif:
9552 		    line = compile_endif(p, &cctx);
9553 		    break;
9554 
9555 	    case CMD_while:
9556 		    line = compile_while(p, &cctx);
9557 		    break;
9558 	    case CMD_endwhile:
9559 		    line = compile_endwhile(p, &cctx);
9560 		    cctx.ctx_had_return = FALSE;
9561 		    break;
9562 
9563 	    case CMD_for:
9564 		    line = compile_for(p, &cctx);
9565 		    break;
9566 	    case CMD_endfor:
9567 		    line = compile_endfor(p, &cctx);
9568 		    cctx.ctx_had_return = FALSE;
9569 		    break;
9570 	    case CMD_continue:
9571 		    line = compile_continue(p, &cctx);
9572 		    break;
9573 	    case CMD_break:
9574 		    line = compile_break(p, &cctx);
9575 		    break;
9576 
9577 	    case CMD_try:
9578 		    line = compile_try(p, &cctx);
9579 		    break;
9580 	    case CMD_catch:
9581 		    line = compile_catch(p, &cctx);
9582 		    cctx.ctx_had_return = FALSE;
9583 		    break;
9584 	    case CMD_finally:
9585 		    line = compile_finally(p, &cctx);
9586 		    cctx.ctx_had_return = FALSE;
9587 		    break;
9588 	    case CMD_endtry:
9589 		    line = compile_endtry(p, &cctx);
9590 		    cctx.ctx_had_return = FALSE;
9591 		    break;
9592 	    case CMD_throw:
9593 		    line = compile_throw(p, &cctx);
9594 		    break;
9595 
9596 	    case CMD_eval:
9597 		    if (compile_expr0(&p, &cctx) == FAIL)
9598 			goto erret;
9599 
9600 		    // drop the result
9601 		    generate_instr_drop(&cctx, ISN_DROP, 1);
9602 
9603 		    line = skipwhite(p);
9604 		    break;
9605 
9606 	    case CMD_echo:
9607 	    case CMD_echon:
9608 	    case CMD_execute:
9609 	    case CMD_echomsg:
9610 	    case CMD_echoerr:
9611 		    line = compile_mult_expr(p, ea.cmdidx, &cctx);
9612 		    break;
9613 
9614 	    case CMD_put:
9615 		    ea.cmd = cmd;
9616 		    line = compile_put(p, &ea, &cctx);
9617 		    break;
9618 
9619 	    case CMD_substitute:
9620 		    if (cctx.ctx_skip == SKIP_YES)
9621 			line = (char_u *)"";
9622 		    else
9623 		    {
9624 			ea.arg = p;
9625 			line = compile_substitute(line, &ea, &cctx);
9626 		    }
9627 		    break;
9628 
9629 	    case CMD_redir:
9630 		    ea.arg = p;
9631 		    line = compile_redir(line, &ea, &cctx);
9632 		    break;
9633 
9634 	    case CMD_cexpr:
9635 	    case CMD_lexpr:
9636 	    case CMD_caddexpr:
9637 	    case CMD_laddexpr:
9638 	    case CMD_cgetexpr:
9639 	    case CMD_lgetexpr:
9640 #ifdef FEAT_QUICKFIX
9641 		    ea.arg = p;
9642 		    line = compile_cexpr(line, &ea, &cctx);
9643 #else
9644 		    ex_ni(&ea);
9645 		    line = NULL;
9646 #endif
9647 		    break;
9648 
9649 	    // TODO: any other commands with an expression argument?
9650 
9651 	    case CMD_append:
9652 	    case CMD_change:
9653 	    case CMD_insert:
9654 	    case CMD_k:
9655 	    case CMD_t:
9656 	    case CMD_xit:
9657 		    not_in_vim9(&ea);
9658 		    goto erret;
9659 
9660 	    case CMD_SIZE:
9661 		    if (cctx.ctx_skip != SKIP_YES)
9662 		    {
9663 			semsg(_(e_invalid_command_str), ea.cmd);
9664 			goto erret;
9665 		    }
9666 		    // We don't check for a next command here.
9667 		    line = (char_u *)"";
9668 		    break;
9669 
9670 	    case CMD_lua:
9671 	    case CMD_mzscheme:
9672 	    case CMD_perl:
9673 	    case CMD_py3:
9674 	    case CMD_python3:
9675 	    case CMD_python:
9676 	    case CMD_pythonx:
9677 	    case CMD_ruby:
9678 	    case CMD_tcl:
9679 		    ea.arg = p;
9680 		    if (vim_strchr(line, '\n') == NULL)
9681 			line = compile_exec(line, &ea, &cctx);
9682 		    else
9683 			// heredoc lines have been concatenated with NL
9684 			// characters in get_function_body()
9685 			line = compile_script(line, &cctx);
9686 		    break;
9687 
9688 	    default:
9689 		    // Not recognized, execute with do_cmdline_cmd().
9690 		    ea.arg = p;
9691 		    line = compile_exec(line, &ea, &cctx);
9692 		    break;
9693 	}
9694 nextline:
9695 	if (line == NULL)
9696 	    goto erret;
9697 	line = skipwhite(line);
9698 
9699 	// Undo any command modifiers.
9700 	generate_undo_cmdmods(&cctx);
9701 
9702 	if (cctx.ctx_type_stack.ga_len < 0)
9703 	{
9704 	    iemsg("Type stack underflow");
9705 	    goto erret;
9706 	}
9707     }
9708 
9709     if (cctx.ctx_scope != NULL)
9710     {
9711 	if (cctx.ctx_scope->se_type == IF_SCOPE)
9712 	    emsg(_(e_endif));
9713 	else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9714 	    emsg(_(e_endwhile));
9715 	else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9716 	    emsg(_(e_endfor));
9717 	else
9718 	    emsg(_(e_missing_rcurly));
9719 	goto erret;
9720     }
9721 
9722     if (!cctx.ctx_had_return)
9723     {
9724 	if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
9725 	    ufunc->uf_ret_type = &t_void;
9726 	else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
9727 	{
9728 	    emsg(_(e_missing_return_statement));
9729 	    goto erret;
9730 	}
9731 
9732 	// Return void if there is no return at the end.
9733 	generate_instr(&cctx, ISN_RETURN_VOID);
9734     }
9735 
9736     // When compiled with ":silent!" and there was an error don't consider the
9737     // function compiled.
9738     if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
9739     {
9740 	dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
9741 							 + ufunc->uf_dfunc_idx;
9742 	dfunc->df_deleted = FALSE;
9743 	dfunc->df_script_seq = current_sctx.sc_seq;
9744 #ifdef FEAT_PROFILE
9745 	if (cctx.ctx_compile_type == CT_PROFILE)
9746 	{
9747 	    dfunc->df_instr_prof = instr->ga_data;
9748 	    dfunc->df_instr_prof_count = instr->ga_len;
9749 	}
9750 	else
9751 #endif
9752 	if (cctx.ctx_compile_type == CT_DEBUG)
9753 	{
9754 	    dfunc->df_instr_debug = instr->ga_data;
9755 	    dfunc->df_instr_debug_count = instr->ga_len;
9756 	}
9757 	else
9758 	{
9759 	    dfunc->df_instr = instr->ga_data;
9760 	    dfunc->df_instr_count = instr->ga_len;
9761 	}
9762 	dfunc->df_varcount = dfunc->df_var_names.ga_len;
9763 	dfunc->df_has_closure = cctx.ctx_has_closure;
9764 	if (cctx.ctx_outer_used)
9765 	    ufunc->uf_flags |= FC_CLOSURE;
9766 	ufunc->uf_def_status = UF_COMPILED;
9767     }
9768 
9769     ret = OK;
9770 
9771 erret:
9772     if (ufunc->uf_def_status == UF_COMPILING)
9773     {
9774 	dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
9775 							 + ufunc->uf_dfunc_idx;
9776 
9777 	// Compiling aborted, free the generated instructions.
9778 	clear_instr_ga(instr);
9779 	VIM_CLEAR(dfunc->df_name);
9780 	ga_clear_strings(&dfunc->df_var_names);
9781 
9782 	// If using the last entry in the table and it was added above, we
9783 	// might as well remove it.
9784 	if (!dfunc->df_deleted && new_def_function
9785 			    && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
9786 	{
9787 	    --def_functions.ga_len;
9788 	    ufunc->uf_dfunc_idx = 0;
9789 	}
9790 	ufunc->uf_def_status = UF_COMPILE_ERROR;
9791 
9792 	while (cctx.ctx_scope != NULL)
9793 	    drop_scope(&cctx);
9794 
9795 	if (errormsg != NULL)
9796 	    emsg(errormsg);
9797 	else if (did_emsg == did_emsg_before)
9798 	    emsg(_(e_compiling_def_function_failed));
9799     }
9800 
9801     if (cctx.ctx_redir_lhs.lhs_name != NULL)
9802     {
9803 	if (ret == OK)
9804 	{
9805 	    emsg(_(e_missing_redir_end));
9806 	    ret = FAIL;
9807 	}
9808 	vim_free(cctx.ctx_redir_lhs.lhs_name);
9809 	vim_free(cctx.ctx_redir_lhs.lhs_whole);
9810     }
9811 
9812     current_sctx = save_current_sctx;
9813     estack_compiling = save_estack_compiling;
9814     cmdmod.cmod_flags =	save_cmod_flags;
9815     if (do_estack_push)
9816 	estack_pop();
9817 
9818     vim_free(line_to_free);
9819     free_imported(&cctx);
9820     free_locals(&cctx);
9821     ga_clear(&cctx.ctx_type_stack);
9822     return ret;
9823 }
9824 
9825     void
9826 set_function_type(ufunc_T *ufunc)
9827 {
9828     int varargs = ufunc->uf_va_name != NULL;
9829     int argcount = ufunc->uf_args.ga_len;
9830 
9831     // Create a type for the function, with the return type and any
9832     // argument types.
9833     // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9834     // The type is included in "tt_args".
9835     if (argcount > 0 || varargs)
9836     {
9837 	if (ufunc->uf_type_list.ga_itemsize == 0)
9838 	    ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
9839 	ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9840 					   argcount, &ufunc->uf_type_list);
9841 	// Add argument types to the function type.
9842 	if (func_type_add_arg_types(ufunc->uf_func_type,
9843 				    argcount + varargs,
9844 				    &ufunc->uf_type_list) == FAIL)
9845 	    return;
9846 	ufunc->uf_func_type->tt_argcount = argcount + varargs;
9847 	ufunc->uf_func_type->tt_min_argcount =
9848 				      argcount - ufunc->uf_def_args.ga_len;
9849 	if (ufunc->uf_arg_types == NULL)
9850 	{
9851 	    int i;
9852 
9853 	    // lambda does not have argument types.
9854 	    for (i = 0; i < argcount; ++i)
9855 		ufunc->uf_func_type->tt_args[i] = &t_any;
9856 	}
9857 	else
9858 	    mch_memmove(ufunc->uf_func_type->tt_args,
9859 			 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9860 	if (varargs)
9861 	{
9862 	    ufunc->uf_func_type->tt_args[argcount] =
9863 		   ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
9864 	    ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9865 	}
9866     }
9867     else
9868 	// No arguments, can use a predefined type.
9869 	ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9870 					   argcount, &ufunc->uf_type_list);
9871 }
9872 
9873 
9874 /*
9875  * Delete an instruction, free what it contains.
9876  */
9877     void
9878 delete_instr(isn_T *isn)
9879 {
9880     switch (isn->isn_type)
9881     {
9882 	case ISN_DEF:
9883 	case ISN_EXEC:
9884 	case ISN_EXEC_SPLIT:
9885 	case ISN_LEGACY_EVAL:
9886 	case ISN_LOADAUTO:
9887 	case ISN_LOADB:
9888 	case ISN_LOADENV:
9889 	case ISN_LOADG:
9890 	case ISN_LOADOPT:
9891 	case ISN_LOADT:
9892 	case ISN_LOADW:
9893 	case ISN_PUSHEXC:
9894 	case ISN_PUSHFUNC:
9895 	case ISN_PUSHS:
9896 	case ISN_RANGE:
9897 	case ISN_STOREAUTO:
9898 	case ISN_STOREB:
9899 	case ISN_STOREENV:
9900 	case ISN_STOREG:
9901 	case ISN_STORET:
9902 	case ISN_STOREW:
9903 	case ISN_STRINGMEMBER:
9904 	    vim_free(isn->isn_arg.string);
9905 	    break;
9906 
9907 	case ISN_SUBSTITUTE:
9908 	    {
9909 		int	idx;
9910 		isn_T	*list = isn->isn_arg.subs.subs_instr;
9911 
9912 		vim_free(isn->isn_arg.subs.subs_cmd);
9913 		for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9914 		    delete_instr(list + idx);
9915 		vim_free(list);
9916 	    }
9917 	    break;
9918 
9919 	case ISN_INSTR:
9920 	    {
9921 		int	idx;
9922 		isn_T	*list = isn->isn_arg.instr;
9923 
9924 		for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
9925 		    delete_instr(list + idx);
9926 		vim_free(list);
9927 	    }
9928 	    break;
9929 
9930 	case ISN_LOADS:
9931 	case ISN_STORES:
9932 	    vim_free(isn->isn_arg.loadstore.ls_name);
9933 	    break;
9934 
9935 	case ISN_UNLET:
9936 	case ISN_UNLETENV:
9937 	    vim_free(isn->isn_arg.unlet.ul_name);
9938 	    break;
9939 
9940 	case ISN_STOREOPT:
9941 	    vim_free(isn->isn_arg.storeopt.so_name);
9942 	    break;
9943 
9944 	case ISN_PUSHBLOB:   // push blob isn_arg.blob
9945 	    blob_unref(isn->isn_arg.blob);
9946 	    break;
9947 
9948 	case ISN_PUSHJOB:
9949 #ifdef FEAT_JOB_CHANNEL
9950 	    job_unref(isn->isn_arg.job);
9951 #endif
9952 	    break;
9953 
9954 	case ISN_PUSHCHANNEL:
9955 #ifdef FEAT_JOB_CHANNEL
9956 	    channel_unref(isn->isn_arg.channel);
9957 #endif
9958 	    break;
9959 
9960 	case ISN_UCALL:
9961 	    vim_free(isn->isn_arg.ufunc.cuf_name);
9962 	    break;
9963 
9964 	case ISN_FUNCREF:
9965 	    {
9966 		dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9967 					       + isn->isn_arg.funcref.fr_func;
9968 		ufunc_T *ufunc = dfunc->df_ufunc;
9969 
9970 		if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9971 		    func_ptr_unref(ufunc);
9972 	    }
9973 	    break;
9974 
9975 	case ISN_DCALL:
9976 	    {
9977 		dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9978 					       + isn->isn_arg.dfunc.cdf_idx;
9979 
9980 		if (dfunc->df_ufunc != NULL
9981 			       && func_name_refcount(dfunc->df_ufunc->uf_name))
9982 		    func_ptr_unref(dfunc->df_ufunc);
9983 	    }
9984 	    break;
9985 
9986 	case ISN_NEWFUNC:
9987 	    {
9988 		char_u  *lambda = isn->isn_arg.newfunc.nf_lambda;
9989 		ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9990 
9991 		if (ufunc != NULL)
9992 		{
9993 		    unlink_def_function(ufunc);
9994 		    func_ptr_unref(ufunc);
9995 		}
9996 
9997 		vim_free(lambda);
9998 		vim_free(isn->isn_arg.newfunc.nf_global);
9999 	    }
10000 	    break;
10001 
10002 	case ISN_CHECKTYPE:
10003 	case ISN_SETTYPE:
10004 	    free_type(isn->isn_arg.type.ct_type);
10005 	    break;
10006 
10007 	case ISN_CMDMOD:
10008 	    vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
10009 					       ->cmod_filter_regmatch.regprog);
10010 	    vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
10011 	    break;
10012 
10013 	case ISN_LOADSCRIPT:
10014 	case ISN_STORESCRIPT:
10015 	    vim_free(isn->isn_arg.script.scriptref);
10016 	    break;
10017 
10018 	case ISN_TRY:
10019 	    vim_free(isn->isn_arg.try.try_ref);
10020 	    break;
10021 
10022 	case ISN_CEXPR_CORE:
10023 	    vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
10024 	    vim_free(isn->isn_arg.cexpr.cexpr_ref);
10025 	    break;
10026 
10027 	case ISN_2BOOL:
10028 	case ISN_2STRING:
10029 	case ISN_2STRING_ANY:
10030 	case ISN_ADDBLOB:
10031 	case ISN_ADDLIST:
10032 	case ISN_ANYINDEX:
10033 	case ISN_ANYSLICE:
10034 	case ISN_BCALL:
10035 	case ISN_BLOBAPPEND:
10036 	case ISN_BLOBINDEX:
10037 	case ISN_BLOBSLICE:
10038 	case ISN_CATCH:
10039 	case ISN_CEXPR_AUCMD:
10040 	case ISN_CHECKLEN:
10041 	case ISN_CHECKNR:
10042 	case ISN_CMDMOD_REV:
10043 	case ISN_COMPAREANY:
10044 	case ISN_COMPAREBLOB:
10045 	case ISN_COMPAREBOOL:
10046 	case ISN_COMPAREDICT:
10047 	case ISN_COMPAREFLOAT:
10048 	case ISN_COMPAREFUNC:
10049 	case ISN_COMPARELIST:
10050 	case ISN_COMPARENR:
10051 	case ISN_COMPARESPECIAL:
10052 	case ISN_COMPARESTRING:
10053 	case ISN_CONCAT:
10054 	case ISN_COND2BOOL:
10055 	case ISN_DEBUG:
10056 	case ISN_DROP:
10057 	case ISN_ECHO:
10058 	case ISN_ECHOERR:
10059 	case ISN_ECHOMSG:
10060 	case ISN_ENDTRY:
10061 	case ISN_EXECCONCAT:
10062 	case ISN_EXECUTE:
10063 	case ISN_FINALLY:
10064 	case ISN_FINISH:
10065 	case ISN_FOR:
10066 	case ISN_GETITEM:
10067 	case ISN_JUMP:
10068 	case ISN_JUMP_IF_ARG_SET:
10069 	case ISN_LISTAPPEND:
10070 	case ISN_LISTINDEX:
10071 	case ISN_LISTSLICE:
10072 	case ISN_LOAD:
10073 	case ISN_LOADBDICT:
10074 	case ISN_LOADGDICT:
10075 	case ISN_LOADOUTER:
10076 	case ISN_LOADREG:
10077 	case ISN_LOADTDICT:
10078 	case ISN_LOADV:
10079 	case ISN_LOADWDICT:
10080 	case ISN_LOCKCONST:
10081 	case ISN_MEMBER:
10082 	case ISN_NEGATENR:
10083 	case ISN_NEWDICT:
10084 	case ISN_NEWLIST:
10085 	case ISN_OPANY:
10086 	case ISN_OPFLOAT:
10087 	case ISN_OPNR:
10088 	case ISN_PCALL:
10089 	case ISN_PCALL_END:
10090 	case ISN_PROF_END:
10091 	case ISN_PROF_START:
10092 	case ISN_PUSHBOOL:
10093 	case ISN_PUSHF:
10094 	case ISN_PUSHNR:
10095 	case ISN_PUSHSPEC:
10096 	case ISN_PUT:
10097 	case ISN_REDIREND:
10098 	case ISN_REDIRSTART:
10099 	case ISN_RETURN:
10100 	case ISN_RETURN_VOID:
10101 	case ISN_SHUFFLE:
10102 	case ISN_SLICE:
10103 	case ISN_STORE:
10104 	case ISN_STOREINDEX:
10105 	case ISN_STORENR:
10106 	case ISN_STOREOUTER:
10107 	case ISN_STORERANGE:
10108 	case ISN_STOREREG:
10109 	case ISN_STOREV:
10110 	case ISN_STRINDEX:
10111 	case ISN_STRSLICE:
10112 	case ISN_THROW:
10113 	case ISN_TRYCONT:
10114 	case ISN_UNLETINDEX:
10115 	case ISN_UNLETRANGE:
10116 	case ISN_UNPACK:
10117 	    // nothing allocated
10118 	    break;
10119     }
10120 }
10121 
10122 /*
10123  * Free all instructions for "dfunc" except df_name.
10124  */
10125     static void
10126 delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
10127 {
10128     int idx;
10129 
10130     ga_clear(&dfunc->df_def_args_isn);
10131     ga_clear_strings(&dfunc->df_var_names);
10132 
10133     if (dfunc->df_instr != NULL)
10134     {
10135 	for (idx = 0; idx < dfunc->df_instr_count; ++idx)
10136 	    delete_instr(dfunc->df_instr + idx);
10137 	VIM_CLEAR(dfunc->df_instr);
10138 	dfunc->df_instr = NULL;
10139     }
10140     if (dfunc->df_instr_debug != NULL)
10141     {
10142 	for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
10143 	    delete_instr(dfunc->df_instr_debug + idx);
10144 	VIM_CLEAR(dfunc->df_instr_debug);
10145 	dfunc->df_instr_debug = NULL;
10146     }
10147 #ifdef FEAT_PROFILE
10148     if (dfunc->df_instr_prof != NULL)
10149     {
10150 	for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
10151 	    delete_instr(dfunc->df_instr_prof + idx);
10152 	VIM_CLEAR(dfunc->df_instr_prof);
10153 	dfunc->df_instr_prof = NULL;
10154     }
10155 #endif
10156 
10157     if (mark_deleted)
10158 	dfunc->df_deleted = TRUE;
10159     if (dfunc->df_ufunc != NULL)
10160 	dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
10161 }
10162 
10163 /*
10164  * When a user function is deleted, clear the contents of any associated def
10165  * function, unless another user function still uses it.
10166  * The position in def_functions can be re-used.
10167  */
10168     void
10169 unlink_def_function(ufunc_T *ufunc)
10170 {
10171     if (ufunc->uf_dfunc_idx > 0)
10172     {
10173 	dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10174 							 + ufunc->uf_dfunc_idx;
10175 
10176 	if (--dfunc->df_refcount <= 0)
10177 	    delete_def_function_contents(dfunc, TRUE);
10178 	ufunc->uf_def_status = UF_NOT_COMPILED;
10179 	ufunc->uf_dfunc_idx = 0;
10180 	if (dfunc->df_ufunc == ufunc)
10181 	    dfunc->df_ufunc = NULL;
10182     }
10183 }
10184 
10185 /*
10186  * Used when a user function refers to an existing dfunc.
10187  */
10188     void
10189 link_def_function(ufunc_T *ufunc)
10190 {
10191     if (ufunc->uf_dfunc_idx > 0)
10192     {
10193 	dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
10194 							 + ufunc->uf_dfunc_idx;
10195 
10196 	++dfunc->df_refcount;
10197     }
10198 }
10199 
10200 #if defined(EXITFREE) || defined(PROTO)
10201 /*
10202  * Free all functions defined with ":def".
10203  */
10204     void
10205 free_def_functions(void)
10206 {
10207     int idx;
10208 
10209     for (idx = 0; idx < def_functions.ga_len; ++idx)
10210     {
10211 	dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
10212 
10213 	delete_def_function_contents(dfunc, TRUE);
10214 	vim_free(dfunc->df_name);
10215     }
10216 
10217     ga_clear(&def_functions);
10218 }
10219 #endif
10220 
10221 
10222 #endif // FEAT_EVAL
10223