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