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