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