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