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