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