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