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