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