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