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