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