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