blob: 33c4a6cdf5bfc579edac3a7a9957d5d817dd36ee [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
26/*
27 * Chain of jump instructions where the end label needs to be set.
28 */
29typedef struct endlabel_S endlabel_T;
30struct endlabel_S {
31 endlabel_T *el_next; // chain end_label locations
32 int el_end_label; // instruction idx where to set end
33};
34
35/*
36 * info specific for the scope of :if / elseif / else
37 */
38typedef struct {
39 int is_if_label; // instruction idx at IF or ELSEIF
40 endlabel_T *is_end_label; // instructions to set end label
41} ifscope_T;
42
43/*
44 * info specific for the scope of :while
45 */
46typedef struct {
47 int ws_top_label; // instruction idx at WHILE
48 endlabel_T *ws_end_label; // instructions to set end
49} whilescope_T;
50
51/*
52 * info specific for the scope of :for
53 */
54typedef struct {
55 int fs_top_label; // instruction idx at FOR
56 endlabel_T *fs_end_label; // break instructions
57} forscope_T;
58
59/*
60 * info specific for the scope of :try
61 */
62typedef struct {
63 int ts_try_label; // instruction idx at TRY
64 endlabel_T *ts_end_label; // jump to :finally or :endtry
65 int ts_catch_label; // instruction idx of last CATCH
66 int ts_caught_all; // "catch" without argument encountered
67} tryscope_T;
68
69typedef enum {
70 NO_SCOPE,
71 IF_SCOPE,
72 WHILE_SCOPE,
73 FOR_SCOPE,
74 TRY_SCOPE,
75 BLOCK_SCOPE
76} scopetype_T;
77
78/*
79 * Info for one scope, pointed to by "ctx_scope".
80 */
81typedef struct scope_S scope_T;
82struct scope_S {
83 scope_T *se_outer; // scope containing this one
84 scopetype_T se_type;
85 int se_local_count; // ctx_locals.ga_len before scope
86 union {
87 ifscope_T se_if;
88 whilescope_T se_while;
89 forscope_T se_for;
90 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +010091 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092};
93
94/*
95 * Entry for "ctx_locals". Used for arguments and local variables.
96 */
97typedef struct {
98 char_u *lv_name;
99 type_T *lv_type;
100 int lv_const; // when TRUE cannot be assigned to
101 int lv_arg; // when TRUE this is an argument
102} lvar_T;
103
104/*
105 * Context for compiling lines of Vim script.
106 * Stores info about the local variables and condition stack.
107 */
108struct cctx_S {
109 ufunc_T *ctx_ufunc; // current function
110 int ctx_lnum; // line number in current function
111 garray_T ctx_instr; // generated instructions
112
113 garray_T ctx_locals; // currently visible local variables
114 int ctx_max_local; // maximum number of locals at one time
115
116 garray_T ctx_imports; // imported items
117
Bram Moolenaara259d8d2020-01-31 20:10:50 +0100118 int ctx_skip; // when TRUE skip commands, when FALSE skip
119 // commands after "else"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100120 scope_T *ctx_scope; // current scope, NULL at toplevel
121
122 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200123 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124};
125
126static char e_var_notfound[] = N_("E1001: variable not found: %s");
127static char e_syntax_at[] = N_("E1002: Syntax error at %s");
128
129static int compile_expr1(char_u **arg, cctx_T *cctx);
130static int compile_expr2(char_u **arg, cctx_T *cctx);
131static int compile_expr3(char_u **arg, cctx_T *cctx);
Bram Moolenaar20431c92020-03-20 18:39:46 +0100132static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200133static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
134static int check_type(type_T *expected, type_T *actual, int give_msg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135
136/*
137 * Lookup variable "name" in the local scope and return the index.
138 */
139 static int
140lookup_local(char_u *name, size_t len, cctx_T *cctx)
141{
142 int idx;
143
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100144 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145 return -1;
146 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
147 {
148 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
149
150 if (STRNCMP(name, lvar->lv_name, len) == 0
151 && STRLEN(lvar->lv_name) == len)
152 return idx;
153 }
154 return -1;
155}
156
157/*
158 * Lookup an argument in the current function.
159 * Returns the argument index or -1 if not found.
160 */
161 static int
162lookup_arg(char_u *name, size_t len, cctx_T *cctx)
163{
164 int idx;
165
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100166 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167 return -1;
168 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
169 {
170 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
171
172 if (STRNCMP(name, arg, len) == 0 && STRLEN(arg) == len)
173 return idx;
174 }
175 return -1;
176}
177
178/*
179 * Lookup a vararg argument in the current function.
180 * Returns TRUE if there is a match.
181 */
182 static int
183lookup_vararg(char_u *name, size_t len, cctx_T *cctx)
184{
185 char_u *va_name = cctx->ctx_ufunc->uf_va_name;
186
187 return len > 0 && va_name != NULL
188 && STRNCMP(name, va_name, len) == 0 && STRLEN(va_name) == len;
189}
190
191/*
192 * Lookup a variable in the current script.
193 * Returns OK or FAIL.
194 */
195 static int
196lookup_script(char_u *name, size_t len)
197{
198 int cc;
199 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
200 dictitem_T *di;
201
202 cc = name[len];
203 name[len] = NUL;
204 di = find_var_in_ht(ht, 0, name, TRUE);
205 name[len] = cc;
206 return di == NULL ? FAIL: OK;
207}
208
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100209/*
210 * Check if "p[len]" is already defined, either in script "import_sid" or in
211 * compilation context "cctx".
212 * Return FAIL and give an error if it defined.
213 */
214 int
215check_defined(char_u *p, int len, cctx_T *cctx)
216{
217 if (lookup_script(p, len) == OK
218 || (cctx != NULL
219 && (lookup_local(p, len, cctx) >= 0
220 || find_imported(p, len, cctx) != NULL)))
221 {
222 semsg("E1073: imported name already defined: %s", p);
223 return FAIL;
224 }
225 return OK;
226}
227
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200228/*
229 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
230 * be freed later.
231 */
232 static type_T *
233alloc_type(garray_T *type_gap)
234{
235 type_T *type;
236
237 if (ga_grow(type_gap, 1) == FAIL)
238 return NULL;
239 type = ALLOC_CLEAR_ONE(type_T);
240 if (type != NULL)
241 {
242 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
243 ++type_gap->ga_len;
244 }
245 return type;
246}
247
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200249get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100250{
251 type_T *type;
252
253 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200254 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200256 if (member_type->tt_type == VAR_VOID
257 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100258 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100259 if (member_type->tt_type == VAR_BOOL)
260 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100261 if (member_type->tt_type == VAR_NUMBER)
262 return &t_list_number;
263 if (member_type->tt_type == VAR_STRING)
264 return &t_list_string;
265
266 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200267 type = alloc_type(type_gap);
268 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100269 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100270 type->tt_type = VAR_LIST;
271 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200272 type->tt_argcount = 0;
273 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 return type;
275}
276
277 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200278get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279{
280 type_T *type;
281
282 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200283 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100284 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200285 if (member_type->tt_type == VAR_VOID
286 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100287 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100288 if (member_type->tt_type == VAR_BOOL)
289 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100290 if (member_type->tt_type == VAR_NUMBER)
291 return &t_dict_number;
292 if (member_type->tt_type == VAR_STRING)
293 return &t_dict_string;
294
295 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200296 type = alloc_type(type_gap);
297 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100298 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299 type->tt_type = VAR_DICT;
300 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200301 type->tt_argcount = 0;
302 type->tt_args = NULL;
303 return type;
304}
305
306/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200307 * Allocate a new type for a function.
308 */
309 static type_T *
310alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
311{
312 type_T *type = alloc_type(type_gap);
313
314 if (type == NULL)
315 return &t_any;
316 type->tt_type = VAR_FUNC;
317 type->tt_member = ret_type;
318 type->tt_argcount = argcount;
319 type->tt_args = NULL;
320 return type;
321}
322
323/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200324 * Get a function type, based on the return type "ret_type".
325 * If "argcount" is -1 or 0 a predefined type can be used.
326 * If "argcount" > 0 always create a new type, so that arguments can be added.
327 */
328 static type_T *
329get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
330{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200331 // recognize commonly used types
332 if (argcount <= 0)
333 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200334 if (ret_type == &t_unknown)
335 {
336 // (argcount == 0) is not possible
337 return &t_func_unknown;
338 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200339 if (ret_type == &t_void)
340 {
341 if (argcount == 0)
342 return &t_func_0_void;
343 else
344 return &t_func_void;
345 }
346 if (ret_type == &t_any)
347 {
348 if (argcount == 0)
349 return &t_func_0_any;
350 else
351 return &t_func_any;
352 }
353 if (ret_type == &t_number)
354 {
355 if (argcount == 0)
356 return &t_func_0_number;
357 else
358 return &t_func_number;
359 }
360 if (ret_type == &t_string)
361 {
362 if (argcount == 0)
363 return &t_func_0_string;
364 else
365 return &t_func_string;
366 }
367 }
368
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200369 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370}
371
Bram Moolenaara8c17702020-04-01 21:17:24 +0200372/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200373 * For a function type, reserve space for "argcount" argument types (including
374 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200375 */
376 static int
377func_type_add_arg_types(
378 type_T *functype,
379 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200380 garray_T *type_gap)
381{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200382 // To make it easy to free the space needed for the argument types, add the
383 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200384 if (ga_grow(type_gap, 1) == FAIL)
385 return FAIL;
386 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
387 if (functype->tt_args == NULL)
388 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200389 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
390 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200391 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200392 return OK;
393}
394
395/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200396 * Return the type_T for a typval. Only for primitive types.
397 */
398 static type_T *
399typval2type(typval_T *tv)
400{
401 if (tv->v_type == VAR_NUMBER)
402 return &t_number;
403 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200404 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200405 if (tv->v_type == VAR_STRING)
406 return &t_string;
407 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
408 return &t_list_string;
409 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
410 return &t_dict_any;
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200411 return &t_any; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200412}
413
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414/////////////////////////////////////////////////////////////////////
415// Following generate_ functions expect the caller to call ga_grow().
416
Bram Moolenaar080457c2020-03-03 21:53:32 +0100417#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
418#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420/*
421 * Generate an instruction without arguments.
422 * Returns a pointer to the new instruction, NULL if failed.
423 */
424 static isn_T *
425generate_instr(cctx_T *cctx, isntype_T isn_type)
426{
427 garray_T *instr = &cctx->ctx_instr;
428 isn_T *isn;
429
Bram Moolenaar080457c2020-03-03 21:53:32 +0100430 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100431 if (ga_grow(instr, 1) == FAIL)
432 return NULL;
433 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
434 isn->isn_type = isn_type;
435 isn->isn_lnum = cctx->ctx_lnum + 1;
436 ++instr->ga_len;
437
438 return isn;
439}
440
441/*
442 * Generate an instruction without arguments.
443 * "drop" will be removed from the stack.
444 * Returns a pointer to the new instruction, NULL if failed.
445 */
446 static isn_T *
447generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
448{
449 garray_T *stack = &cctx->ctx_type_stack;
450
Bram Moolenaar080457c2020-03-03 21:53:32 +0100451 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100452 stack->ga_len -= drop;
453 return generate_instr(cctx, isn_type);
454}
455
456/*
457 * Generate instruction "isn_type" and put "type" on the type stack.
458 */
459 static isn_T *
460generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
461{
462 isn_T *isn;
463 garray_T *stack = &cctx->ctx_type_stack;
464
465 if ((isn = generate_instr(cctx, isn_type)) == NULL)
466 return NULL;
467
468 if (ga_grow(stack, 1) == FAIL)
469 return NULL;
470 ((type_T **)stack->ga_data)[stack->ga_len] = type;
471 ++stack->ga_len;
472
473 return isn;
474}
475
476/*
477 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
478 */
479 static int
480may_generate_2STRING(int offset, cctx_T *cctx)
481{
482 isn_T *isn;
483 garray_T *stack = &cctx->ctx_type_stack;
484 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
485
486 if ((*type)->tt_type == VAR_STRING)
487 return OK;
488 *type = &t_string;
489
490 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
491 return FAIL;
492 isn->isn_arg.number = offset;
493
494 return OK;
495}
496
497 static int
498check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
499{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200500 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100501 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200502 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503 {
504 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100505 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506 else
507 semsg(_("E1036: %c requires number or float arguments"), *op);
508 return FAIL;
509 }
510 return OK;
511}
512
513/*
514 * Generate an instruction with two arguments. The instruction depends on the
515 * type of the arguments.
516 */
517 static int
518generate_two_op(cctx_T *cctx, char_u *op)
519{
520 garray_T *stack = &cctx->ctx_type_stack;
521 type_T *type1;
522 type_T *type2;
523 vartype_T vartype;
524 isn_T *isn;
525
Bram Moolenaar080457c2020-03-03 21:53:32 +0100526 RETURN_OK_IF_SKIP(cctx);
527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100528 // Get the known type of the two items on the stack. If they are matching
529 // use a type-specific instruction. Otherwise fall back to runtime type
530 // checking.
531 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
532 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200533 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534 if (type1->tt_type == type2->tt_type
535 && (type1->tt_type == VAR_NUMBER
536 || type1->tt_type == VAR_LIST
537#ifdef FEAT_FLOAT
538 || type1->tt_type == VAR_FLOAT
539#endif
540 || type1->tt_type == VAR_BLOB))
541 vartype = type1->tt_type;
542
543 switch (*op)
544 {
545 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200546 && type1->tt_type != VAR_ANY
547 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548 && check_number_or_float(
549 type1->tt_type, type2->tt_type, op) == FAIL)
550 return FAIL;
551 isn = generate_instr_drop(cctx,
552 vartype == VAR_NUMBER ? ISN_OPNR
553 : vartype == VAR_LIST ? ISN_ADDLIST
554 : vartype == VAR_BLOB ? ISN_ADDBLOB
555#ifdef FEAT_FLOAT
556 : vartype == VAR_FLOAT ? ISN_OPFLOAT
557#endif
558 : ISN_OPANY, 1);
559 if (isn != NULL)
560 isn->isn_arg.op.op_type = EXPR_ADD;
561 break;
562
563 case '-':
564 case '*':
565 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
566 op) == FAIL)
567 return FAIL;
568 if (vartype == VAR_NUMBER)
569 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
570#ifdef FEAT_FLOAT
571 else if (vartype == VAR_FLOAT)
572 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
573#endif
574 else
575 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
576 if (isn != NULL)
577 isn->isn_arg.op.op_type = *op == '*'
578 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
579 break;
580
Bram Moolenaar4c683752020-04-05 21:38:23 +0200581 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200583 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584 && type2->tt_type != VAR_NUMBER))
585 {
586 emsg(_("E1035: % requires number arguments"));
587 return FAIL;
588 }
589 isn = generate_instr_drop(cctx,
590 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
591 if (isn != NULL)
592 isn->isn_arg.op.op_type = EXPR_REM;
593 break;
594 }
595
596 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200597 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598 {
599 type_T *type = &t_any;
600
601#ifdef FEAT_FLOAT
602 // float+number and number+float results in float
603 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
604 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
605 type = &t_float;
606#endif
607 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
608 }
609
610 return OK;
611}
612
613/*
614 * Generate an ISN_COMPARE* instruction with a boolean result.
615 */
616 static int
617generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
618{
619 isntype_T isntype = ISN_DROP;
620 isn_T *isn;
621 garray_T *stack = &cctx->ctx_type_stack;
622 vartype_T type1;
623 vartype_T type2;
624
Bram Moolenaar080457c2020-03-03 21:53:32 +0100625 RETURN_OK_IF_SKIP(cctx);
626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627 // Get the known type of the two items on the stack. If they are matching
628 // use a type-specific instruction. Otherwise fall back to runtime type
629 // checking.
630 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
631 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200632 if (type1 == VAR_UNKNOWN)
633 type1 = VAR_ANY;
634 if (type2 == VAR_UNKNOWN)
635 type2 = VAR_ANY;
636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 if (type1 == type2)
638 {
639 switch (type1)
640 {
641 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
642 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
643 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
644 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
645 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
646 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
647 case VAR_LIST: isntype = ISN_COMPARELIST; break;
648 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
649 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650 default: isntype = ISN_COMPAREANY; break;
651 }
652 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200653 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100654 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
655 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
656 isntype = ISN_COMPAREANY;
657
658 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
659 && (isntype == ISN_COMPAREBOOL
660 || isntype == ISN_COMPARESPECIAL
661 || isntype == ISN_COMPARENR
662 || isntype == ISN_COMPAREFLOAT))
663 {
664 semsg(_("E1037: Cannot use \"%s\" with %s"),
665 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
666 return FAIL;
667 }
668 if (isntype == ISN_DROP
669 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
670 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
671 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
672 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
673 && exptype != EXPR_IS && exptype != EXPR_ISNOT
674 && (type1 == VAR_BLOB || type2 == VAR_BLOB
675 || type1 == VAR_LIST || type2 == VAR_LIST))))
676 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100677 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100678 vartype_name(type1), vartype_name(type2));
679 return FAIL;
680 }
681
682 if ((isn = generate_instr(cctx, isntype)) == NULL)
683 return FAIL;
684 isn->isn_arg.op.op_type = exptype;
685 isn->isn_arg.op.op_ic = ic;
686
687 // takes two arguments, puts one bool back
688 if (stack->ga_len >= 2)
689 {
690 --stack->ga_len;
691 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
692 }
693
694 return OK;
695}
696
697/*
698 * Generate an ISN_2BOOL instruction.
699 */
700 static int
701generate_2BOOL(cctx_T *cctx, int invert)
702{
703 isn_T *isn;
704 garray_T *stack = &cctx->ctx_type_stack;
705
Bram Moolenaar080457c2020-03-03 21:53:32 +0100706 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
708 return FAIL;
709 isn->isn_arg.number = invert;
710
711 // type becomes bool
712 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
713
714 return OK;
715}
716
717 static int
718generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
719{
720 isn_T *isn;
721 garray_T *stack = &cctx->ctx_type_stack;
722
Bram Moolenaar080457c2020-03-03 21:53:32 +0100723 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
725 return FAIL;
726 isn->isn_arg.type.ct_type = vartype->tt_type; // TODO: whole type
727 isn->isn_arg.type.ct_off = offset;
728
729 // type becomes vartype
730 ((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype;
731
732 return OK;
733}
734
735/*
736 * Generate an ISN_PUSHNR instruction.
737 */
738 static int
739generate_PUSHNR(cctx_T *cctx, varnumber_T number)
740{
741 isn_T *isn;
742
Bram Moolenaar080457c2020-03-03 21:53:32 +0100743 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
745 return FAIL;
746 isn->isn_arg.number = number;
747
748 return OK;
749}
750
751/*
752 * Generate an ISN_PUSHBOOL instruction.
753 */
754 static int
755generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
756{
757 isn_T *isn;
758
Bram Moolenaar080457c2020-03-03 21:53:32 +0100759 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
761 return FAIL;
762 isn->isn_arg.number = number;
763
764 return OK;
765}
766
767/*
768 * Generate an ISN_PUSHSPEC instruction.
769 */
770 static int
771generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
772{
773 isn_T *isn;
774
Bram Moolenaar080457c2020-03-03 21:53:32 +0100775 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
777 return FAIL;
778 isn->isn_arg.number = number;
779
780 return OK;
781}
782
783#ifdef FEAT_FLOAT
784/*
785 * Generate an ISN_PUSHF instruction.
786 */
787 static int
788generate_PUSHF(cctx_T *cctx, float_T fnumber)
789{
790 isn_T *isn;
791
Bram Moolenaar080457c2020-03-03 21:53:32 +0100792 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
794 return FAIL;
795 isn->isn_arg.fnumber = fnumber;
796
797 return OK;
798}
799#endif
800
801/*
802 * Generate an ISN_PUSHS instruction.
803 * Consumes "str".
804 */
805 static int
806generate_PUSHS(cctx_T *cctx, char_u *str)
807{
808 isn_T *isn;
809
Bram Moolenaar080457c2020-03-03 21:53:32 +0100810 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
812 return FAIL;
813 isn->isn_arg.string = str;
814
815 return OK;
816}
817
818/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100819 * Generate an ISN_PUSHCHANNEL instruction.
820 * Consumes "channel".
821 */
822 static int
823generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
824{
825 isn_T *isn;
826
Bram Moolenaar080457c2020-03-03 21:53:32 +0100827 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100828 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
829 return FAIL;
830 isn->isn_arg.channel = channel;
831
832 return OK;
833}
834
835/*
836 * Generate an ISN_PUSHJOB instruction.
837 * Consumes "job".
838 */
839 static int
840generate_PUSHJOB(cctx_T *cctx, job_T *job)
841{
842 isn_T *isn;
843
Bram Moolenaar080457c2020-03-03 21:53:32 +0100844 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100845 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100846 return FAIL;
847 isn->isn_arg.job = job;
848
849 return OK;
850}
851
852/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 * Generate an ISN_PUSHBLOB instruction.
854 * Consumes "blob".
855 */
856 static int
857generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
858{
859 isn_T *isn;
860
Bram Moolenaar080457c2020-03-03 21:53:32 +0100861 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
863 return FAIL;
864 isn->isn_arg.blob = blob;
865
866 return OK;
867}
868
869/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100870 * Generate an ISN_PUSHFUNC instruction with name "name".
871 * Consumes "name".
872 */
873 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200874generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100875{
876 isn_T *isn;
877
Bram Moolenaar080457c2020-03-03 21:53:32 +0100878 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200879 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100880 return FAIL;
881 isn->isn_arg.string = name;
882
883 return OK;
884}
885
886/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 * Generate an ISN_STORE instruction.
888 */
889 static int
890generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
891{
892 isn_T *isn;
893
Bram Moolenaar080457c2020-03-03 21:53:32 +0100894 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
896 return FAIL;
897 if (name != NULL)
898 isn->isn_arg.string = vim_strsave(name);
899 else
900 isn->isn_arg.number = idx;
901
902 return OK;
903}
904
905/*
906 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
907 */
908 static int
909generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
910{
911 isn_T *isn;
912
Bram Moolenaar080457c2020-03-03 21:53:32 +0100913 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
915 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100916 isn->isn_arg.storenr.stnr_idx = idx;
917 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100918
919 return OK;
920}
921
922/*
923 * Generate an ISN_STOREOPT instruction
924 */
925 static int
926generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
927{
928 isn_T *isn;
929
Bram Moolenaar080457c2020-03-03 21:53:32 +0100930 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
932 return FAIL;
933 isn->isn_arg.storeopt.so_name = vim_strsave(name);
934 isn->isn_arg.storeopt.so_flags = opt_flags;
935
936 return OK;
937}
938
939/*
940 * Generate an ISN_LOAD or similar instruction.
941 */
942 static int
943generate_LOAD(
944 cctx_T *cctx,
945 isntype_T isn_type,
946 int idx,
947 char_u *name,
948 type_T *type)
949{
950 isn_T *isn;
951
Bram Moolenaar080457c2020-03-03 21:53:32 +0100952 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
954 return FAIL;
955 if (name != NULL)
956 isn->isn_arg.string = vim_strsave(name);
957 else
958 isn->isn_arg.number = idx;
959
960 return OK;
961}
962
963/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200964 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100965 */
966 static int
967generate_LOADV(
968 cctx_T *cctx,
969 char_u *name,
970 int error)
971{
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200972 int di_flags;
973 int vidx = find_vim_var(name, &di_flags);
974 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100975
Bram Moolenaar080457c2020-03-03 21:53:32 +0100976 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100977 if (vidx < 0)
978 {
979 if (error)
980 semsg(_(e_var_notfound), name);
981 return FAIL;
982 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200983 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100984
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200985 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100986}
987
988/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100989 * Generate an ISN_LOADS instruction.
990 */
991 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100992generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100994 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100996 int sid,
997 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998{
999 isn_T *isn;
1000
Bram Moolenaar080457c2020-03-03 21:53:32 +01001001 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001002 if (isn_type == ISN_LOADS)
1003 isn = generate_instr_type(cctx, isn_type, type);
1004 else
1005 isn = generate_instr_drop(cctx, isn_type, 1);
1006 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001008 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1009 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010
1011 return OK;
1012}
1013
1014/*
1015 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1016 */
1017 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001018generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 cctx_T *cctx,
1020 isntype_T isn_type,
1021 int sid,
1022 int idx,
1023 type_T *type)
1024{
1025 isn_T *isn;
1026
Bram Moolenaar080457c2020-03-03 21:53:32 +01001027 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028 if (isn_type == ISN_LOADSCRIPT)
1029 isn = generate_instr_type(cctx, isn_type, type);
1030 else
1031 isn = generate_instr_drop(cctx, isn_type, 1);
1032 if (isn == NULL)
1033 return FAIL;
1034 isn->isn_arg.script.script_sid = sid;
1035 isn->isn_arg.script.script_idx = idx;
1036 return OK;
1037}
1038
1039/*
1040 * Generate an ISN_NEWLIST instruction.
1041 */
1042 static int
1043generate_NEWLIST(cctx_T *cctx, int count)
1044{
1045 isn_T *isn;
1046 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047 type_T *type;
1048 type_T *member;
1049
Bram Moolenaar080457c2020-03-03 21:53:32 +01001050 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1052 return FAIL;
1053 isn->isn_arg.number = count;
1054
1055 // drop the value types
1056 stack->ga_len -= count;
1057
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001058 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001059 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060 if (count > 0)
1061 member = ((type_T **)stack->ga_data)[stack->ga_len];
1062 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001063 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001064 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065
1066 // add the list type to the type stack
1067 if (ga_grow(stack, 1) == FAIL)
1068 return FAIL;
1069 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1070 ++stack->ga_len;
1071
1072 return OK;
1073}
1074
1075/*
1076 * Generate an ISN_NEWDICT instruction.
1077 */
1078 static int
1079generate_NEWDICT(cctx_T *cctx, int count)
1080{
1081 isn_T *isn;
1082 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 type_T *type;
1084 type_T *member;
1085
Bram Moolenaar080457c2020-03-03 21:53:32 +01001086 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1088 return FAIL;
1089 isn->isn_arg.number = count;
1090
1091 // drop the key and value types
1092 stack->ga_len -= 2 * count;
1093
Bram Moolenaar436472f2020-02-20 22:54:43 +01001094 // Use the first value type for the list member type. Use "void" for an
1095 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 if (count > 0)
1097 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1098 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001099 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001100 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101
1102 // add the dict type to the type stack
1103 if (ga_grow(stack, 1) == FAIL)
1104 return FAIL;
1105 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1106 ++stack->ga_len;
1107
1108 return OK;
1109}
1110
1111/*
1112 * Generate an ISN_FUNCREF instruction.
1113 */
1114 static int
1115generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1116{
1117 isn_T *isn;
1118 garray_T *stack = &cctx->ctx_type_stack;
1119
Bram Moolenaar080457c2020-03-03 21:53:32 +01001120 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1122 return FAIL;
1123 isn->isn_arg.number = dfunc_idx;
1124
1125 if (ga_grow(stack, 1) == FAIL)
1126 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001127 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 // TODO: argument and return types
1129 ++stack->ga_len;
1130
1131 return OK;
1132}
1133
1134/*
1135 * Generate an ISN_JUMP instruction.
1136 */
1137 static int
1138generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1139{
1140 isn_T *isn;
1141 garray_T *stack = &cctx->ctx_type_stack;
1142
Bram Moolenaar080457c2020-03-03 21:53:32 +01001143 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1145 return FAIL;
1146 isn->isn_arg.jump.jump_when = when;
1147 isn->isn_arg.jump.jump_where = where;
1148
1149 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1150 --stack->ga_len;
1151
1152 return OK;
1153}
1154
1155 static int
1156generate_FOR(cctx_T *cctx, int loop_idx)
1157{
1158 isn_T *isn;
1159 garray_T *stack = &cctx->ctx_type_stack;
1160
Bram Moolenaar080457c2020-03-03 21:53:32 +01001161 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1163 return FAIL;
1164 isn->isn_arg.forloop.for_idx = loop_idx;
1165
1166 if (ga_grow(stack, 1) == FAIL)
1167 return FAIL;
1168 // type doesn't matter, will be stored next
1169 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1170 ++stack->ga_len;
1171
1172 return OK;
1173}
1174
1175/*
1176 * Generate an ISN_BCALL instruction.
1177 * Return FAIL if the number of arguments is wrong.
1178 */
1179 static int
1180generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1181{
1182 isn_T *isn;
1183 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001184 type_T *argtypes[MAX_FUNC_ARGS];
1185 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001186
Bram Moolenaar080457c2020-03-03 21:53:32 +01001187 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188 if (check_internal_func(func_idx, argcount) == FAIL)
1189 return FAIL;
1190
1191 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1192 return FAIL;
1193 isn->isn_arg.bfunc.cbf_idx = func_idx;
1194 isn->isn_arg.bfunc.cbf_argcount = argcount;
1195
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001196 for (i = 0; i < argcount; ++i)
1197 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1198
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199 stack->ga_len -= argcount; // drop the arguments
1200 if (ga_grow(stack, 1) == FAIL)
1201 return FAIL;
1202 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001203 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 ++stack->ga_len; // add return value
1205
1206 return OK;
1207}
1208
1209/*
1210 * Generate an ISN_DCALL or ISN_UCALL instruction.
1211 * Return FAIL if the number of arguments is wrong.
1212 */
1213 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001214generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215{
1216 isn_T *isn;
1217 garray_T *stack = &cctx->ctx_type_stack;
1218 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001219 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220
Bram Moolenaar080457c2020-03-03 21:53:32 +01001221 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 if (argcount > regular_args && !has_varargs(ufunc))
1223 {
1224 semsg(_(e_toomanyarg), ufunc->uf_name);
1225 return FAIL;
1226 }
1227 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1228 {
1229 semsg(_(e_toofewarg), ufunc->uf_name);
1230 return FAIL;
1231 }
1232
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001233 if (ufunc->uf_dfunc_idx >= 0)
1234 {
1235 int i;
1236
1237 for (i = 0; i < argcount; ++i)
1238 {
1239 type_T *expected;
1240 type_T *actual;
1241
1242 if (i < regular_args)
1243 {
1244 if (ufunc->uf_arg_types == NULL)
1245 continue;
1246 expected = ufunc->uf_arg_types[i];
1247 }
1248 else
1249 expected = ufunc->uf_va_type->tt_member;
1250 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1251 if (check_type(expected, actual, FALSE) == FAIL)
1252 {
1253 arg_type_mismatch(expected, actual, i + 1);
1254 return FAIL;
1255 }
1256 }
1257 }
1258
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259 if ((isn = generate_instr(cctx,
1260 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1261 return FAIL;
1262 if (ufunc->uf_dfunc_idx >= 0)
1263 {
1264 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1265 isn->isn_arg.dfunc.cdf_argcount = argcount;
1266 }
1267 else
1268 {
1269 // A user function may be deleted and redefined later, can't use the
1270 // ufunc pointer, need to look it up again at runtime.
1271 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1272 isn->isn_arg.ufunc.cuf_argcount = argcount;
1273 }
1274
1275 stack->ga_len -= argcount; // drop the arguments
1276 if (ga_grow(stack, 1) == FAIL)
1277 return FAIL;
1278 // add return value
1279 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1280 ++stack->ga_len;
1281
1282 return OK;
1283}
1284
1285/*
1286 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1287 */
1288 static int
1289generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1290{
1291 isn_T *isn;
1292 garray_T *stack = &cctx->ctx_type_stack;
1293
Bram Moolenaar080457c2020-03-03 21:53:32 +01001294 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1296 return FAIL;
1297 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1298 isn->isn_arg.ufunc.cuf_argcount = argcount;
1299
1300 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001301 if (ga_grow(stack, 1) == FAIL)
1302 return FAIL;
1303 // add return value
1304 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1305 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306
1307 return OK;
1308}
1309
1310/*
1311 * Generate an ISN_PCALL instruction.
1312 */
1313 static int
1314generate_PCALL(cctx_T *cctx, int argcount, int at_top)
1315{
1316 isn_T *isn;
1317 garray_T *stack = &cctx->ctx_type_stack;
1318
Bram Moolenaar080457c2020-03-03 21:53:32 +01001319 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1322 return FAIL;
1323 isn->isn_arg.pfunc.cpf_top = at_top;
1324 isn->isn_arg.pfunc.cpf_argcount = argcount;
1325
1326 stack->ga_len -= argcount; // drop the arguments
1327
1328 // drop the funcref/partial, get back the return value
1329 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any;
1330
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001331 // If partial is above the arguments it must be cleared and replaced with
1332 // the return value.
1333 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1334 return FAIL;
1335
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 return OK;
1337}
1338
1339/*
1340 * Generate an ISN_MEMBER instruction.
1341 */
1342 static int
1343generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1344{
1345 isn_T *isn;
1346 garray_T *stack = &cctx->ctx_type_stack;
1347 type_T *type;
1348
Bram Moolenaar080457c2020-03-03 21:53:32 +01001349 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1351 return FAIL;
1352 isn->isn_arg.string = vim_strnsave(name, (int)len);
1353
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001354 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001356 if (type->tt_type != VAR_DICT && type != &t_any)
1357 {
1358 emsg(_(e_dictreq));
1359 return FAIL;
1360 }
1361 // change dict type to dict member type
1362 if (type->tt_type == VAR_DICT)
1363 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364
1365 return OK;
1366}
1367
1368/*
1369 * Generate an ISN_ECHO instruction.
1370 */
1371 static int
1372generate_ECHO(cctx_T *cctx, int with_white, int count)
1373{
1374 isn_T *isn;
1375
Bram Moolenaar080457c2020-03-03 21:53:32 +01001376 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1378 return FAIL;
1379 isn->isn_arg.echo.echo_with_white = with_white;
1380 isn->isn_arg.echo.echo_count = count;
1381
1382 return OK;
1383}
1384
Bram Moolenaarad39c092020-02-26 18:23:43 +01001385/*
1386 * Generate an ISN_EXECUTE instruction.
1387 */
1388 static int
1389generate_EXECUTE(cctx_T *cctx, int count)
1390{
1391 isn_T *isn;
1392
1393 if ((isn = generate_instr_drop(cctx, ISN_EXECUTE, count)) == NULL)
1394 return FAIL;
1395 isn->isn_arg.number = count;
1396
1397 return OK;
1398}
1399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 static int
1401generate_EXEC(cctx_T *cctx, char_u *line)
1402{
1403 isn_T *isn;
1404
Bram Moolenaar080457c2020-03-03 21:53:32 +01001405 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1407 return FAIL;
1408 isn->isn_arg.string = vim_strsave(line);
1409 return OK;
1410}
1411
1412static char e_white_both[] =
1413 N_("E1004: white space required before and after '%s'");
Bram Moolenaard77a8522020-04-03 21:59:57 +02001414static char e_white_after[] = N_("E1069: white space required after '%s'");
1415static char e_no_white_before[] = N_("E1068: No white space allowed before '%s'");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416
1417/*
1418 * Reserve space for a local variable.
1419 * Return the index or -1 if it failed.
1420 */
1421 static int
1422reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1423{
1424 int idx;
1425 lvar_T *lvar;
1426
1427 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1428 {
1429 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
1430 return -1;
1431 }
1432
1433 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
1434 return -1;
1435 idx = cctx->ctx_locals.ga_len;
1436 if (cctx->ctx_max_local < idx + 1)
1437 cctx->ctx_max_local = idx + 1;
1438 ++cctx->ctx_locals.ga_len;
1439
1440 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1441 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1442 lvar->lv_const = isConst;
1443 lvar->lv_type = type;
1444
1445 return idx;
1446}
1447
1448/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001449 * Remove local variables above "new_top".
1450 */
1451 static void
1452unwind_locals(cctx_T *cctx, int new_top)
1453{
1454 if (cctx->ctx_locals.ga_len > new_top)
1455 {
1456 int idx;
1457 lvar_T *lvar;
1458
1459 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1460 {
1461 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1462 vim_free(lvar->lv_name);
1463 }
1464 }
1465 cctx->ctx_locals.ga_len = new_top;
1466}
1467
1468/*
1469 * Free all local variables.
1470 */
1471 static void
1472free_local(cctx_T *cctx)
1473{
1474 unwind_locals(cctx, 0);
1475 ga_clear(&cctx->ctx_locals);
1476}
1477
1478/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 * Skip over a type definition and return a pointer to just after it.
1480 */
1481 char_u *
1482skip_type(char_u *start)
1483{
1484 char_u *p = start;
1485
1486 while (ASCII_ISALNUM(*p) || *p == '_')
1487 ++p;
1488
1489 // Skip over "<type>"; this is permissive about white space.
1490 if (*skipwhite(p) == '<')
1491 {
1492 p = skipwhite(p);
1493 p = skip_type(skipwhite(p + 1));
1494 p = skipwhite(p);
1495 if (*p == '>')
1496 ++p;
1497 }
1498 return p;
1499}
1500
1501/*
1502 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001503 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 * Returns NULL in case of failure.
1505 */
1506 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001507parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508{
1509 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001510 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511
1512 if (**arg != '<')
1513 {
1514 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001515 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001516 else
1517 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001518 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 }
1520 *arg = skipwhite(*arg + 1);
1521
Bram Moolenaard77a8522020-04-03 21:59:57 +02001522 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523
1524 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001525 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526 {
1527 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001528 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001529 }
1530 ++*arg;
1531
1532 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001533 return get_list_type(member_type, type_gap);
1534 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535}
1536
1537/*
1538 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001539 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540 */
1541 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001542parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543{
1544 char_u *p = *arg;
1545 size_t len;
1546
1547 // skip over the first word
1548 while (ASCII_ISALNUM(*p) || *p == '_')
1549 ++p;
1550 len = p - *arg;
1551
1552 switch (**arg)
1553 {
1554 case 'a':
1555 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1556 {
1557 *arg += len;
1558 return &t_any;
1559 }
1560 break;
1561 case 'b':
1562 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1563 {
1564 *arg += len;
1565 return &t_bool;
1566 }
1567 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1568 {
1569 *arg += len;
1570 return &t_blob;
1571 }
1572 break;
1573 case 'c':
1574 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1575 {
1576 *arg += len;
1577 return &t_channel;
1578 }
1579 break;
1580 case 'd':
1581 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1582 {
1583 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001584 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 }
1586 break;
1587 case 'f':
1588 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1589 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001590#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 *arg += len;
1592 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001593#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001594 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001595 return &t_any;
1596#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 }
1598 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1599 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001600 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001601 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001602 int argcount = -1;
1603 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001604 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001605 type_T *arg_type[MAX_FUNC_ARGS + 1];
1606
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001607 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001609 if (**arg == '(')
1610 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001611 // "func" may or may not return a value, "func()" does
1612 // not return a value.
1613 ret_type = &t_void;
1614
Bram Moolenaard77a8522020-04-03 21:59:57 +02001615 p = ++*arg;
1616 argcount = 0;
1617 while (*p != NUL && *p != ')')
1618 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001619 if (*p == '?')
1620 {
1621 if (first_optional == -1)
1622 first_optional = argcount;
1623 ++p;
1624 }
1625 else if (first_optional != -1)
1626 {
1627 emsg(_("E1007: mandatory argument after optional argument"));
1628 return &t_any;
1629 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001630 else if (STRNCMP(p, "...", 3) == 0)
1631 {
1632 flags |= TTFLAG_VARARGS;
1633 p += 3;
1634 }
1635
1636 arg_type[argcount++] = parse_type(&p, type_gap);
1637
1638 // Nothing comes after "...{type}".
1639 if (flags & TTFLAG_VARARGS)
1640 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001641
Bram Moolenaard77a8522020-04-03 21:59:57 +02001642 if (*p != ',' && *skipwhite(p) == ',')
1643 {
1644 semsg(_(e_no_white_before), ",");
1645 return &t_any;
1646 }
1647 if (*p == ',')
1648 {
1649 ++p;
1650 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001651 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001652 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001653 return &t_any;
1654 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001655 }
1656 p = skipwhite(p);
1657 if (argcount == MAX_FUNC_ARGS)
1658 {
1659 emsg(_("E740: Too many argument types"));
1660 return &t_any;
1661 }
1662 }
1663
1664 p = skipwhite(p);
1665 if (*p != ')')
1666 {
1667 emsg(_(e_missing_close));
1668 return &t_any;
1669 }
1670 *arg = p + 1;
1671 }
1672 if (**arg == ':')
1673 {
1674 // parse return type
1675 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001676 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001677 semsg(_(e_white_after), ":");
1678 *arg = skipwhite(*arg);
1679 ret_type = parse_type(arg, type_gap);
1680 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001681 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001682 type = get_func_type(ret_type, argcount, type_gap);
1683 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001684 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001685 type = alloc_func_type(ret_type, argcount, type_gap);
1686 type->tt_flags = flags;
1687 if (argcount > 0)
1688 {
1689 type->tt_argcount = argcount;
1690 type->tt_min_argcount = first_optional == -1
1691 ? argcount : first_optional;
1692 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001693 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001694 return &t_any;
1695 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001696 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001697 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001698 }
1699 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 }
1701 break;
1702 case 'j':
1703 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1704 {
1705 *arg += len;
1706 return &t_job;
1707 }
1708 break;
1709 case 'l':
1710 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1711 {
1712 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001713 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 }
1715 break;
1716 case 'n':
1717 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1718 {
1719 *arg += len;
1720 return &t_number;
1721 }
1722 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 case 's':
1724 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1725 {
1726 *arg += len;
1727 return &t_string;
1728 }
1729 break;
1730 case 'v':
1731 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1732 {
1733 *arg += len;
1734 return &t_void;
1735 }
1736 break;
1737 }
1738
1739 semsg(_("E1010: Type not recognized: %s"), *arg);
1740 return &t_any;
1741}
1742
1743/*
1744 * Check if "type1" and "type2" are exactly the same.
1745 */
1746 static int
1747equal_type(type_T *type1, type_T *type2)
1748{
1749 if (type1->tt_type != type2->tt_type)
1750 return FALSE;
1751 switch (type1->tt_type)
1752 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001754 case VAR_ANY:
1755 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 case VAR_SPECIAL:
1757 case VAR_BOOL:
1758 case VAR_NUMBER:
1759 case VAR_FLOAT:
1760 case VAR_STRING:
1761 case VAR_BLOB:
1762 case VAR_JOB:
1763 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001764 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001765 case VAR_LIST:
1766 case VAR_DICT:
1767 return equal_type(type1->tt_member, type2->tt_member);
1768 case VAR_FUNC:
1769 case VAR_PARTIAL:
1770 // TODO; check argument types.
1771 return equal_type(type1->tt_member, type2->tt_member)
1772 && type1->tt_argcount == type2->tt_argcount;
1773 }
1774 return TRUE;
1775}
1776
1777/*
1778 * Find the common type of "type1" and "type2" and put it in "dest".
1779 * "type2" and "dest" may be the same.
1780 */
1781 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02001782common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783{
1784 if (equal_type(type1, type2))
1785 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001786 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 return;
1788 }
1789
1790 if (type1->tt_type == type2->tt_type)
1791 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1793 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001794 type_T *common;
1795
Bram Moolenaard77a8522020-04-03 21:59:57 +02001796 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001797 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001798 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001799 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001800 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 return;
1802 }
1803 // TODO: VAR_FUNC and VAR_PARTIAL
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001804 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 }
1806
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001807 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808}
1809
1810 char *
1811vartype_name(vartype_T type)
1812{
1813 switch (type)
1814 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01001815 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02001816 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 case VAR_SPECIAL: return "special";
1819 case VAR_BOOL: return "bool";
1820 case VAR_NUMBER: return "number";
1821 case VAR_FLOAT: return "float";
1822 case VAR_STRING: return "string";
1823 case VAR_BLOB: return "blob";
1824 case VAR_JOB: return "job";
1825 case VAR_CHANNEL: return "channel";
1826 case VAR_LIST: return "list";
1827 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001828
1829 case VAR_FUNC:
1830 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02001832 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833}
1834
1835/*
1836 * Return the name of a type.
1837 * The result may be in allocated memory, in which case "tofree" is set.
1838 */
1839 char *
1840type_name(type_T *type, char **tofree)
1841{
1842 char *name = vartype_name(type->tt_type);
1843
1844 *tofree = NULL;
1845 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1846 {
1847 char *member_free;
1848 char *member_name = type_name(type->tt_member, &member_free);
1849 size_t len;
1850
1851 len = STRLEN(name) + STRLEN(member_name) + 3;
1852 *tofree = alloc(len);
1853 if (*tofree != NULL)
1854 {
1855 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1856 vim_free(member_free);
1857 return *tofree;
1858 }
1859 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001860 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001861 {
1862 garray_T ga;
1863 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001864 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001865
1866 ga_init2(&ga, 1, 100);
1867 if (ga_grow(&ga, 20) == FAIL)
1868 return "[unknown]";
1869 *tofree = ga.ga_data;
1870 STRCPY(ga.ga_data, "func(");
1871 ga.ga_len += 5;
1872
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001873 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001874 {
1875 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001876 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001877 int len;
1878
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001879 if (type->tt_args == NULL)
1880 arg_type = "[unknown]";
1881 else
1882 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001883 if (i > 0)
1884 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001885 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001886 ga.ga_len += 2;
1887 }
1888 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001889 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001890 {
1891 vim_free(arg_free);
1892 return "[unknown]";
1893 }
1894 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001895 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001896 {
1897 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
1898 ga.ga_len += 3;
1899 }
1900 else if (i >= type->tt_min_argcount)
1901 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001902 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001903 ga.ga_len += len;
1904 vim_free(arg_free);
1905 }
1906
1907 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001908 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001909 else
1910 {
1911 char *ret_free;
1912 char *ret_name = type_name(type->tt_member, &ret_free);
1913 int len;
1914
1915 len = (int)STRLEN(ret_name) + 4;
1916 if (ga_grow(&ga, len) == FAIL)
1917 {
1918 vim_free(ret_free);
1919 return "[unknown]";
1920 }
1921 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02001922 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
1923 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001924 vim_free(ret_free);
1925 }
1926 return ga.ga_data;
1927 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928
1929 return name;
1930}
1931
1932/*
1933 * Find "name" in script-local items of script "sid".
1934 * Returns the index in "sn_var_vals" if found.
1935 * If found but not in "sn_var_vals" returns -1.
1936 * If not found returns -2.
1937 */
1938 int
1939get_script_item_idx(int sid, char_u *name, int check_writable)
1940{
1941 hashtab_T *ht;
1942 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001943 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 int idx;
1945
1946 // First look the name up in the hashtable.
1947 if (sid <= 0 || sid > script_items.ga_len)
1948 return -1;
1949 ht = &SCRIPT_VARS(sid);
1950 di = find_var_in_ht(ht, 0, name, TRUE);
1951 if (di == NULL)
1952 return -2;
1953
1954 // Now find the svar_T index in sn_var_vals.
1955 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1956 {
1957 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1958
1959 if (sv->sv_tv == &di->di_tv)
1960 {
1961 if (check_writable && sv->sv_const)
1962 semsg(_(e_readonlyvar), name);
1963 return idx;
1964 }
1965 }
1966 return -1;
1967}
1968
1969/*
1970 * Find "name" in imported items of the current script/
1971 */
1972 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001973find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001975 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 int idx;
1977
1978 if (cctx != NULL)
1979 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1980 {
1981 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1982 + idx;
1983
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001984 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1985 : STRLEN(import->imp_name) == len
1986 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 return import;
1988 }
1989
1990 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1991 {
1992 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1993
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001994 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1995 : STRLEN(import->imp_name) == len
1996 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 return import;
1998 }
1999 return NULL;
2000}
2001
2002/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002003 * Free all imported variables.
2004 */
2005 static void
2006free_imported(cctx_T *cctx)
2007{
2008 int idx;
2009
2010 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2011 {
2012 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2013
2014 vim_free(import->imp_name);
2015 }
2016 ga_clear(&cctx->ctx_imports);
2017}
2018
2019/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002020 * Generate an instruction to load script-local variable "name", without the
2021 * leading "s:".
2022 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023 */
2024 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002025compile_load_scriptvar(
2026 cctx_T *cctx,
2027 char_u *name, // variable NUL terminated
2028 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002029 char_u **end, // end of variable
2030 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002032 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2034 imported_T *import;
2035
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002036 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002038 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002039 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2040 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002041 }
2042 if (idx >= 0)
2043 {
2044 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2045
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002046 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047 current_sctx.sc_sid, idx, sv->sv_type);
2048 return OK;
2049 }
2050
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002051 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 if (import != NULL)
2053 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002054 if (import->imp_all)
2055 {
2056 char_u *p = skipwhite(*end);
2057 int name_len;
2058 ufunc_T *ufunc;
2059 type_T *type;
2060
2061 // Used "import * as Name", need to lookup the member.
2062 if (*p != '.')
2063 {
2064 semsg(_("E1060: expected dot after name: %s"), start);
2065 return FAIL;
2066 }
2067 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002068 if (VIM_ISWHITE(*p))
2069 {
2070 emsg(_("E1074: no white space allowed after dot"));
2071 return FAIL;
2072 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002073
2074 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2075 // TODO: what if it is a function?
2076 if (idx < 0)
2077 return FAIL;
2078 *end = p;
2079
2080 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2081 import->imp_sid,
2082 idx,
2083 type);
2084 }
2085 else
2086 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002087 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002088 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2089 import->imp_sid,
2090 import->imp_var_vals_idx,
2091 import->imp_type);
2092 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093 return OK;
2094 }
2095
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002096 if (error)
2097 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 return FAIL;
2099}
2100
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002101 static int
2102generate_funcref(cctx_T *cctx, char_u *name)
2103{
2104 ufunc_T *ufunc = find_func(name, cctx);
2105
2106 if (ufunc == NULL)
2107 return FAIL;
2108
2109 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2110}
2111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112/*
2113 * Compile a variable name into a load instruction.
2114 * "end" points to just after the name.
2115 * When "error" is FALSE do not give an error when not found.
2116 */
2117 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002118compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119{
2120 type_T *type;
2121 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002122 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002124 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125
2126 if (*(*arg + 1) == ':')
2127 {
2128 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002129 if (end <= *arg + 2)
2130 name = vim_strsave((char_u *)"[empty]");
2131 else
2132 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 if (name == NULL)
2134 return FAIL;
2135
2136 if (**arg == 'v')
2137 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002138 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002139 }
2140 else if (**arg == 'g')
2141 {
2142 // Global variables can be defined later, thus we don't check if it
2143 // exists, give error at runtime.
2144 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2145 }
2146 else if (**arg == 's')
2147 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002148 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002150 else if (**arg == 'b')
2151 {
2152 semsg("Namespace b: not supported yet: %s", *arg);
2153 goto theend;
2154 }
2155 else if (**arg == 'w')
2156 {
2157 semsg("Namespace w: not supported yet: %s", *arg);
2158 goto theend;
2159 }
2160 else if (**arg == 't')
2161 {
2162 semsg("Namespace t: not supported yet: %s", *arg);
2163 goto theend;
2164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002165 else
2166 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002167 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 goto theend;
2169 }
2170 }
2171 else
2172 {
2173 size_t len = end - *arg;
2174 int idx;
2175 int gen_load = FALSE;
2176
2177 name = vim_strnsave(*arg, end - *arg);
2178 if (name == NULL)
2179 return FAIL;
2180
2181 idx = lookup_arg(*arg, len, cctx);
2182 if (idx >= 0)
2183 {
2184 if (cctx->ctx_ufunc->uf_arg_types != NULL)
2185 type = cctx->ctx_ufunc->uf_arg_types[idx];
2186 else
2187 type = &t_any;
2188
2189 // Arguments are located above the frame pointer.
2190 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
2191 if (cctx->ctx_ufunc->uf_va_name != NULL)
2192 --idx;
2193 gen_load = TRUE;
2194 }
2195 else if (lookup_vararg(*arg, len, cctx))
2196 {
2197 // varargs is always the last argument
2198 idx = -STACK_FRAME_SIZE - 1;
2199 type = cctx->ctx_ufunc->uf_va_type;
2200 gen_load = TRUE;
2201 }
2202 else
2203 {
2204 idx = lookup_local(*arg, len, cctx);
2205 if (idx >= 0)
2206 {
2207 type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type;
2208 gen_load = TRUE;
2209 }
2210 else
2211 {
2212 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2213 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2214 res = generate_PUSHBOOL(cctx, **arg == 't'
2215 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002216 else
2217 {
2218 // "var" can be script-local even without using "s:" if it
2219 // already exists.
2220 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2221 == SCRIPT_VERSION_VIM9
2222 || lookup_script(*arg, len) == OK)
2223 res = compile_load_scriptvar(cctx, name, *arg, &end,
2224 FALSE);
2225
2226 // When the name starts with an uppercase letter or "x:" it
2227 // can be a user defined function.
2228 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2229 res = generate_funcref(cctx, name);
2230 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 }
2232 }
2233 if (gen_load)
2234 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
2235 }
2236
2237 *arg = end;
2238
2239theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002240 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 semsg(_(e_var_notfound), name);
2242 vim_free(name);
2243 return res;
2244}
2245
2246/*
2247 * Compile the argument expressions.
2248 * "arg" points to just after the "(" and is advanced to after the ")"
2249 */
2250 static int
2251compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2252{
2253 char_u *p = *arg;
2254
2255 while (*p != NUL && *p != ')')
2256 {
2257 if (compile_expr1(&p, cctx) == FAIL)
2258 return FAIL;
2259 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002260
2261 if (*p != ',' && *skipwhite(p) == ',')
2262 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002263 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002264 p = skipwhite(p);
2265 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002267 {
2268 ++p;
2269 if (!VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002270 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002271 }
2272 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 }
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002274 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275 if (*p != ')')
2276 {
2277 emsg(_(e_missing_close));
2278 return FAIL;
2279 }
2280 *arg = p + 1;
2281 return OK;
2282}
2283
2284/*
2285 * Compile a function call: name(arg1, arg2)
2286 * "arg" points to "name", "arg + varlen" to the "(".
2287 * "argcount_init" is 1 for "value->method()"
2288 * Instructions:
2289 * EVAL arg1
2290 * EVAL arg2
2291 * BCALL / DCALL / UCALL
2292 */
2293 static int
2294compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2295{
2296 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002297 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 int argcount = argcount_init;
2299 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002300 char_u fname_buf[FLEN_FIXED + 1];
2301 char_u *tofree = NULL;
2302 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002304 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305
2306 if (varlen >= sizeof(namebuf))
2307 {
2308 semsg(_("E1011: name too long: %s"), name);
2309 return FAIL;
2310 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002311 vim_strncpy(namebuf, *arg, varlen);
2312 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313
2314 *arg = skipwhite(*arg + varlen + 1);
2315 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002316 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002318 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319 {
2320 int idx;
2321
2322 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002323 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002325 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002326 else
2327 semsg(_(e_unknownfunc), namebuf);
2328 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329 }
2330
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002331 // If we can find the function by name generate the right call.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002332 ufunc = find_func(name, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002334 {
2335 res = generate_CALL(cctx, ufunc, argcount);
2336 goto theend;
2337 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002338
2339 // If the name is a variable, load it and use PCALL.
2340 p = namebuf;
2341 if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002342 {
2343 res = generate_PCALL(cctx, argcount, FALSE);
2344 goto theend;
2345 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002346
2347 // The function may be defined only later. Need to figure out at runtime.
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002348 res = generate_UCALL(cctx, name, argcount);
2349
2350theend:
2351 vim_free(tofree);
2352 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353}
2354
2355// like NAMESPACE_CHAR but with 'a' and 'l'.
2356#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2357
2358/*
2359 * Find the end of a variable or function name. Unlike find_name_end() this
2360 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002361 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 * Return a pointer to just after the name. Equal to "arg" if there is no
2363 * valid name.
2364 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002365 static char_u *
2366to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002367{
2368 char_u *p;
2369
2370 // Quick check for valid starting character.
2371 if (!eval_isnamec1(*arg))
2372 return arg;
2373
2374 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2375 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2376 // and can be used in slice "[n:]".
2377 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002378 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2380 break;
2381 return p;
2382}
2383
2384/*
2385 * Like to_name_end() but also skip over a list or dict constant.
2386 */
2387 char_u *
2388to_name_const_end(char_u *arg)
2389{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002390 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 typval_T rettv;
2392
2393 if (p == arg && *arg == '[')
2394 {
2395
2396 // Can be "[1, 2, 3]->Func()".
2397 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2398 p = arg;
2399 }
2400 else if (p == arg && *arg == '#' && arg[1] == '{')
2401 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002402 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 ++p;
2404 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2405 p = arg;
2406 }
2407 else if (p == arg && *arg == '{')
2408 {
2409 int ret = get_lambda_tv(&p, &rettv, FALSE);
2410
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002411 // Can be "{x -> ret}()".
2412 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 if (ret == NOTDONE)
2414 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2415 if (ret != OK)
2416 p = arg;
2417 }
2418
2419 return p;
2420}
2421
2422 static void
2423type_mismatch(type_T *expected, type_T *actual)
2424{
2425 char *tofree1, *tofree2;
2426
2427 semsg(_("E1013: type mismatch, expected %s but got %s"),
2428 type_name(expected, &tofree1), type_name(actual, &tofree2));
2429 vim_free(tofree1);
2430 vim_free(tofree2);
2431}
2432
Bram Moolenaar0b76b422020-04-07 22:05:08 +02002433 static void
2434arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
2435{
2436 char *tofree1, *tofree2;
2437
2438 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
2439 argidx,
2440 type_name(expected, &tofree1), type_name(actual, &tofree2));
2441 vim_free(tofree1);
2442 vim_free(tofree2);
2443}
2444
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445/*
2446 * Check if the expected and actual types match.
2447 */
2448 static int
2449check_type(type_T *expected, type_T *actual, int give_msg)
2450{
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002451 int ret = OK;
2452
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002453 // When expected is "unknown" we accept any actual type.
2454 // When expected is "any" we accept any actual type except "void".
2455 if (expected->tt_type != VAR_UNKNOWN
2456 && (expected->tt_type != VAR_ANY || actual->tt_type == VAR_VOID))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457 {
2458 if (expected->tt_type != actual->tt_type)
2459 {
2460 if (give_msg)
2461 type_mismatch(expected, actual);
2462 return FAIL;
2463 }
2464 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
2465 {
Bram Moolenaar4c683752020-04-05 21:38:23 +02002466 // "unknown" is used for an empty list or dict
2467 if (actual->tt_member != &t_unknown)
Bram Moolenaar436472f2020-02-20 22:54:43 +01002468 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002470 else if (expected->tt_type == VAR_FUNC)
2471 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002472 if (expected->tt_member != &t_unknown)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002473 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
2474 if (ret == OK && expected->tt_argcount != -1
2475 && (actual->tt_argcount < expected->tt_min_argcount
2476 || actual->tt_argcount > expected->tt_argcount))
2477 ret = FAIL;
2478 }
2479 if (ret == FAIL && give_msg)
2480 type_mismatch(expected, actual);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 }
Bram Moolenaar89228602020-04-05 22:14:54 +02002482 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483}
2484
2485/*
2486 * Check that
2487 * - "actual" is "expected" type or
2488 * - "actual" is a type that can be "expected" type: add a runtime check; or
2489 * - return FAIL.
2490 */
2491 static int
2492need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
2493{
Bram Moolenaar89228602020-04-05 22:14:54 +02002494 if (check_type(expected, actual, FALSE) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 return OK;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002496 if (actual->tt_type != VAR_ANY && actual->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 {
2498 type_mismatch(expected, actual);
2499 return FAIL;
2500 }
2501 generate_TYPECHECK(cctx, expected, offset);
2502 return OK;
2503}
2504
2505/*
2506 * parse a list: [expr, expr]
2507 * "*arg" points to the '['.
2508 */
2509 static int
2510compile_list(char_u **arg, cctx_T *cctx)
2511{
2512 char_u *p = skipwhite(*arg + 1);
2513 int count = 0;
2514
2515 while (*p != ']')
2516 {
2517 if (*p == NUL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002518 {
2519 semsg(_(e_list_end), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 return FAIL;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002521 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 if (compile_expr1(&p, cctx) == FAIL)
2523 break;
2524 ++count;
2525 if (*p == ',')
2526 ++p;
2527 p = skipwhite(p);
2528 }
2529 *arg = p + 1;
2530
2531 generate_NEWLIST(cctx, count);
2532 return OK;
2533}
2534
2535/*
2536 * parse a lambda: {arg, arg -> expr}
2537 * "*arg" points to the '{'.
2538 */
2539 static int
2540compile_lambda(char_u **arg, cctx_T *cctx)
2541{
2542 garray_T *instr = &cctx->ctx_instr;
2543 typval_T rettv;
2544 ufunc_T *ufunc;
2545
2546 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002547 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002551 ++ufunc->uf_refcount;
2552 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002553 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554
2555 // The function will have one line: "return {expr}".
2556 // Compile it into instructions.
2557 compile_def_function(ufunc, TRUE);
2558
2559 if (ufunc->uf_dfunc_idx >= 0)
2560 {
2561 if (ga_grow(instr, 1) == FAIL)
2562 return FAIL;
2563 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2564 return OK;
2565 }
2566 return FAIL;
2567}
2568
2569/*
2570 * Compile a lamda call: expr->{lambda}(args)
2571 * "arg" points to the "{".
2572 */
2573 static int
2574compile_lambda_call(char_u **arg, cctx_T *cctx)
2575{
2576 ufunc_T *ufunc;
2577 typval_T rettv;
2578 int argcount = 1;
2579 int ret = FAIL;
2580
2581 // Get the funcref in "rettv".
2582 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2583 return FAIL;
2584
2585 if (**arg != '(')
2586 {
2587 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002588 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 else
2590 semsg(_(e_missing_paren), "lambda");
2591 clear_tv(&rettv);
2592 return FAIL;
2593 }
2594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 ufunc = rettv.vval.v_partial->pt_func;
2596 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002597 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002598 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002599
2600 // The function will have one line: "return {expr}".
2601 // Compile it into instructions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 compile_def_function(ufunc, TRUE);
2603
2604 // compile the arguments
2605 *arg = skipwhite(*arg + 1);
2606 if (compile_arguments(arg, cctx, &argcount) == OK)
2607 // call the compiled function
2608 ret = generate_CALL(cctx, ufunc, argcount);
2609
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 return ret;
2611}
2612
2613/*
2614 * parse a dict: {'key': val} or #{key: val}
2615 * "*arg" points to the '{'.
2616 */
2617 static int
2618compile_dict(char_u **arg, cctx_T *cctx, int literal)
2619{
2620 garray_T *instr = &cctx->ctx_instr;
2621 int count = 0;
2622 dict_T *d = dict_alloc();
2623 dictitem_T *item;
2624
2625 if (d == NULL)
2626 return FAIL;
2627 *arg = skipwhite(*arg + 1);
2628 while (**arg != '}' && **arg != NUL)
2629 {
2630 char_u *key = NULL;
2631
2632 if (literal)
2633 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002634 char_u *p = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635
2636 if (p == *arg)
2637 {
2638 semsg(_("E1014: Invalid key: %s"), *arg);
2639 return FAIL;
2640 }
2641 key = vim_strnsave(*arg, p - *arg);
2642 if (generate_PUSHS(cctx, key) == FAIL)
2643 return FAIL;
2644 *arg = p;
2645 }
2646 else
2647 {
2648 isn_T *isn;
2649
2650 if (compile_expr1(arg, cctx) == FAIL)
2651 return FAIL;
2652 // TODO: check type is string
2653 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2654 if (isn->isn_type == ISN_PUSHS)
2655 key = isn->isn_arg.string;
2656 }
2657
2658 // Check for duplicate keys, if using string keys.
2659 if (key != NULL)
2660 {
2661 item = dict_find(d, key, -1);
2662 if (item != NULL)
2663 {
2664 semsg(_(e_duplicate_key), key);
2665 goto failret;
2666 }
2667 item = dictitem_alloc(key);
2668 if (item != NULL)
2669 {
2670 item->di_tv.v_type = VAR_UNKNOWN;
2671 item->di_tv.v_lock = 0;
2672 if (dict_add(d, item) == FAIL)
2673 dictitem_free(item);
2674 }
2675 }
2676
2677 *arg = skipwhite(*arg);
2678 if (**arg != ':')
2679 {
2680 semsg(_(e_missing_dict_colon), *arg);
2681 return FAIL;
2682 }
2683
2684 *arg = skipwhite(*arg + 1);
2685 if (compile_expr1(arg, cctx) == FAIL)
2686 return FAIL;
2687 ++count;
2688
2689 if (**arg == '}')
2690 break;
2691 if (**arg != ',')
2692 {
2693 semsg(_(e_missing_dict_comma), *arg);
2694 goto failret;
2695 }
2696 *arg = skipwhite(*arg + 1);
2697 }
2698
2699 if (**arg != '}')
2700 {
2701 semsg(_(e_missing_dict_end), *arg);
2702 goto failret;
2703 }
2704 *arg = *arg + 1;
2705
2706 dict_unref(d);
2707 return generate_NEWDICT(cctx, count);
2708
2709failret:
2710 dict_unref(d);
2711 return FAIL;
2712}
2713
2714/*
2715 * Compile "&option".
2716 */
2717 static int
2718compile_get_option(char_u **arg, cctx_T *cctx)
2719{
2720 typval_T rettv;
2721 char_u *start = *arg;
2722 int ret;
2723
2724 // parse the option and get the current value to get the type.
2725 rettv.v_type = VAR_UNKNOWN;
2726 ret = get_option_tv(arg, &rettv, TRUE);
2727 if (ret == OK)
2728 {
2729 // include the '&' in the name, get_option_tv() expects it.
2730 char_u *name = vim_strnsave(start, *arg - start);
2731 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2732
2733 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2734 vim_free(name);
2735 }
2736 clear_tv(&rettv);
2737
2738 return ret;
2739}
2740
2741/*
2742 * Compile "$VAR".
2743 */
2744 static int
2745compile_get_env(char_u **arg, cctx_T *cctx)
2746{
2747 char_u *start = *arg;
2748 int len;
2749 int ret;
2750 char_u *name;
2751
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 ++*arg;
2753 len = get_env_len(arg);
2754 if (len == 0)
2755 {
2756 semsg(_(e_syntax_at), start - 1);
2757 return FAIL;
2758 }
2759
2760 // include the '$' in the name, get_env_tv() expects it.
2761 name = vim_strnsave(start, len + 1);
2762 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2763 vim_free(name);
2764 return ret;
2765}
2766
2767/*
2768 * Compile "@r".
2769 */
2770 static int
2771compile_get_register(char_u **arg, cctx_T *cctx)
2772{
2773 int ret;
2774
2775 ++*arg;
2776 if (**arg == NUL)
2777 {
2778 semsg(_(e_syntax_at), *arg - 1);
2779 return FAIL;
2780 }
2781 if (!valid_yank_reg(**arg, TRUE))
2782 {
2783 emsg_invreg(**arg);
2784 return FAIL;
2785 }
2786 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2787 ++*arg;
2788 return ret;
2789}
2790
2791/*
2792 * Apply leading '!', '-' and '+' to constant "rettv".
2793 */
2794 static int
2795apply_leader(typval_T *rettv, char_u *start, char_u *end)
2796{
2797 char_u *p = end;
2798
2799 // this works from end to start
2800 while (p > start)
2801 {
2802 --p;
2803 if (*p == '-' || *p == '+')
2804 {
2805 // only '-' has an effect, for '+' we only check the type
2806#ifdef FEAT_FLOAT
2807 if (rettv->v_type == VAR_FLOAT)
2808 {
2809 if (*p == '-')
2810 rettv->vval.v_float = -rettv->vval.v_float;
2811 }
2812 else
2813#endif
2814 {
2815 varnumber_T val;
2816 int error = FALSE;
2817
2818 // tv_get_number_chk() accepts a string, but we don't want that
2819 // here
2820 if (check_not_string(rettv) == FAIL)
2821 return FAIL;
2822 val = tv_get_number_chk(rettv, &error);
2823 clear_tv(rettv);
2824 if (error)
2825 return FAIL;
2826 if (*p == '-')
2827 val = -val;
2828 rettv->v_type = VAR_NUMBER;
2829 rettv->vval.v_number = val;
2830 }
2831 }
2832 else
2833 {
2834 int v = tv2bool(rettv);
2835
2836 // '!' is permissive in the type.
2837 clear_tv(rettv);
2838 rettv->v_type = VAR_BOOL;
2839 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2840 }
2841 }
2842 return OK;
2843}
2844
2845/*
2846 * Recognize v: variables that are constants and set "rettv".
2847 */
2848 static void
2849get_vim_constant(char_u **arg, typval_T *rettv)
2850{
2851 if (STRNCMP(*arg, "v:true", 6) == 0)
2852 {
2853 rettv->v_type = VAR_BOOL;
2854 rettv->vval.v_number = VVAL_TRUE;
2855 *arg += 6;
2856 }
2857 else if (STRNCMP(*arg, "v:false", 7) == 0)
2858 {
2859 rettv->v_type = VAR_BOOL;
2860 rettv->vval.v_number = VVAL_FALSE;
2861 *arg += 7;
2862 }
2863 else if (STRNCMP(*arg, "v:null", 6) == 0)
2864 {
2865 rettv->v_type = VAR_SPECIAL;
2866 rettv->vval.v_number = VVAL_NULL;
2867 *arg += 6;
2868 }
2869 else if (STRNCMP(*arg, "v:none", 6) == 0)
2870 {
2871 rettv->v_type = VAR_SPECIAL;
2872 rettv->vval.v_number = VVAL_NONE;
2873 *arg += 6;
2874 }
2875}
2876
2877/*
2878 * Compile code to apply '-', '+' and '!'.
2879 */
2880 static int
2881compile_leader(cctx_T *cctx, char_u *start, char_u *end)
2882{
2883 char_u *p = end;
2884
2885 // this works from end to start
2886 while (p > start)
2887 {
2888 --p;
2889 if (*p == '-' || *p == '+')
2890 {
2891 int negate = *p == '-';
2892 isn_T *isn;
2893
2894 // TODO: check type
2895 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2896 {
2897 --p;
2898 if (*p == '-')
2899 negate = !negate;
2900 }
2901 // only '-' has an effect, for '+' we only check the type
2902 if (negate)
2903 isn = generate_instr(cctx, ISN_NEGATENR);
2904 else
2905 isn = generate_instr(cctx, ISN_CHECKNR);
2906 if (isn == NULL)
2907 return FAIL;
2908 }
2909 else
2910 {
2911 int invert = TRUE;
2912
2913 while (p > start && p[-1] == '!')
2914 {
2915 --p;
2916 invert = !invert;
2917 }
2918 if (generate_2BOOL(cctx, invert) == FAIL)
2919 return FAIL;
2920 }
2921 }
2922 return OK;
2923}
2924
2925/*
2926 * Compile whatever comes after "name" or "name()".
2927 */
2928 static int
2929compile_subscript(
2930 char_u **arg,
2931 cctx_T *cctx,
2932 char_u **start_leader,
2933 char_u *end_leader)
2934{
2935 for (;;)
2936 {
2937 if (**arg == '(')
2938 {
2939 int argcount = 0;
2940
2941 // funcref(arg)
2942 *arg = skipwhite(*arg + 1);
2943 if (compile_arguments(arg, cctx, &argcount) == FAIL)
2944 return FAIL;
2945 if (generate_PCALL(cctx, argcount, TRUE) == FAIL)
2946 return FAIL;
2947 }
2948 else if (**arg == '-' && (*arg)[1] == '>')
2949 {
2950 char_u *p;
2951
2952 // something->method()
2953 // Apply the '!', '-' and '+' first:
2954 // -1.0->func() works like (-1.0)->func()
2955 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
2956 return FAIL;
2957 *start_leader = end_leader; // don't apply again later
2958
2959 *arg = skipwhite(*arg + 2);
2960 if (**arg == '{')
2961 {
2962 // lambda call: list->{lambda}
2963 if (compile_lambda_call(arg, cctx) == FAIL)
2964 return FAIL;
2965 }
2966 else
2967 {
2968 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002969 p = *arg;
2970 if (ASCII_ISALPHA(*p) && p[1] == ':')
2971 p += 2;
2972 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 ;
2974 if (*p != '(')
2975 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02002976 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 return FAIL;
2978 }
2979 // TODO: base value may not be the first argument
2980 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
2981 return FAIL;
2982 }
2983 }
2984 else if (**arg == '[')
2985 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01002986 garray_T *stack;
2987 type_T **typep;
2988
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 // list index: list[123]
2990 // TODO: more arguments
2991 // TODO: dict member dict['name']
2992 *arg = skipwhite(*arg + 1);
2993 if (compile_expr1(arg, cctx) == FAIL)
2994 return FAIL;
2995
2996 if (**arg != ']')
2997 {
2998 emsg(_(e_missbrac));
2999 return FAIL;
3000 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003001 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002
3003 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3004 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003005 stack = &cctx->ctx_type_stack;
3006 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
3007 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
3008 {
3009 emsg(_(e_listreq));
3010 return FAIL;
3011 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01003012 if ((*typep)->tt_type == VAR_LIST)
3013 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 }
3015 else if (**arg == '.' && (*arg)[1] != '.')
3016 {
3017 char_u *p;
3018
3019 ++*arg;
3020 p = *arg;
3021 // dictionary member: dict.name
3022 if (eval_isnamec1(*p))
3023 while (eval_isnamec(*p))
3024 MB_PTR_ADV(p);
3025 if (p == *arg)
3026 {
3027 semsg(_(e_syntax_at), *arg);
3028 return FAIL;
3029 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
3031 return FAIL;
3032 *arg = p;
3033 }
3034 else
3035 break;
3036 }
3037
3038 // TODO - see handle_subscript():
3039 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3040 // Don't do this when "Func" is already a partial that was bound
3041 // explicitly (pt_auto is FALSE).
3042
3043 return OK;
3044}
3045
3046/*
3047 * Compile an expression at "*p" and add instructions to "instr".
3048 * "p" is advanced until after the expression, skipping white space.
3049 *
3050 * This is the equivalent of eval1(), eval2(), etc.
3051 */
3052
3053/*
3054 * number number constant
3055 * 0zFFFFFFFF Blob constant
3056 * "string" string constant
3057 * 'string' literal string constant
3058 * &option-name option value
3059 * @r register contents
3060 * identifier variable value
3061 * function() function call
3062 * $VAR environment variable
3063 * (expression) nested expression
3064 * [expr, expr] List
3065 * {key: val, key: val} Dictionary
3066 * #{key: val, key: val} Dictionary with literal keys
3067 *
3068 * Also handle:
3069 * ! in front logical NOT
3070 * - in front unary minus
3071 * + in front unary plus (ignored)
3072 * trailing (arg) funcref/partial call
3073 * trailing [] subscript in String or List
3074 * trailing .name entry in Dictionary
3075 * trailing ->name() method call
3076 */
3077 static int
3078compile_expr7(char_u **arg, cctx_T *cctx)
3079{
3080 typval_T rettv;
3081 char_u *start_leader, *end_leader;
3082 int ret = OK;
3083
3084 /*
3085 * Skip '!', '-' and '+' characters. They are handled later.
3086 */
3087 start_leader = *arg;
3088 while (**arg == '!' || **arg == '-' || **arg == '+')
3089 *arg = skipwhite(*arg + 1);
3090 end_leader = *arg;
3091
3092 rettv.v_type = VAR_UNKNOWN;
3093 switch (**arg)
3094 {
3095 /*
3096 * Number constant.
3097 */
3098 case '0': // also for blob starting with 0z
3099 case '1':
3100 case '2':
3101 case '3':
3102 case '4':
3103 case '5':
3104 case '6':
3105 case '7':
3106 case '8':
3107 case '9':
3108 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
3109 return FAIL;
3110 break;
3111
3112 /*
3113 * String constant: "string".
3114 */
3115 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
3116 return FAIL;
3117 break;
3118
3119 /*
3120 * Literal string constant: 'str''ing'.
3121 */
3122 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
3123 return FAIL;
3124 break;
3125
3126 /*
3127 * Constant Vim variable.
3128 */
3129 case 'v': get_vim_constant(arg, &rettv);
3130 ret = NOTDONE;
3131 break;
3132
3133 /*
3134 * List: [expr, expr]
3135 */
3136 case '[': ret = compile_list(arg, cctx);
3137 break;
3138
3139 /*
3140 * Dictionary: #{key: val, key: val}
3141 */
3142 case '#': if ((*arg)[1] == '{')
3143 {
3144 ++*arg;
3145 ret = compile_dict(arg, cctx, TRUE);
3146 }
3147 else
3148 ret = NOTDONE;
3149 break;
3150
3151 /*
3152 * Lambda: {arg, arg -> expr}
3153 * Dictionary: {'key': val, 'key': val}
3154 */
3155 case '{': {
3156 char_u *start = skipwhite(*arg + 1);
3157
3158 // Find out what comes after the arguments.
3159 ret = get_function_args(&start, '-', NULL,
3160 NULL, NULL, NULL, TRUE);
3161 if (ret != FAIL && *start == '>')
3162 ret = compile_lambda(arg, cctx);
3163 else
3164 ret = compile_dict(arg, cctx, FALSE);
3165 }
3166 break;
3167
3168 /*
3169 * Option value: &name
3170 */
3171 case '&': ret = compile_get_option(arg, cctx);
3172 break;
3173
3174 /*
3175 * Environment variable: $VAR.
3176 */
3177 case '$': ret = compile_get_env(arg, cctx);
3178 break;
3179
3180 /*
3181 * Register contents: @r.
3182 */
3183 case '@': ret = compile_get_register(arg, cctx);
3184 break;
3185 /*
3186 * nested expression: (expression).
3187 */
3188 case '(': *arg = skipwhite(*arg + 1);
3189 ret = compile_expr1(arg, cctx); // recursive!
3190 *arg = skipwhite(*arg);
3191 if (**arg == ')')
3192 ++*arg;
3193 else if (ret == OK)
3194 {
3195 emsg(_(e_missing_close));
3196 ret = FAIL;
3197 }
3198 break;
3199
3200 default: ret = NOTDONE;
3201 break;
3202 }
3203 if (ret == FAIL)
3204 return FAIL;
3205
3206 if (rettv.v_type != VAR_UNKNOWN)
3207 {
3208 // apply the '!', '-' and '+' before the constant
3209 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
3210 {
3211 clear_tv(&rettv);
3212 return FAIL;
3213 }
3214 start_leader = end_leader; // don't apply again below
3215
3216 // push constant
3217 switch (rettv.v_type)
3218 {
3219 case VAR_BOOL:
3220 generate_PUSHBOOL(cctx, rettv.vval.v_number);
3221 break;
3222 case VAR_SPECIAL:
3223 generate_PUSHSPEC(cctx, rettv.vval.v_number);
3224 break;
3225 case VAR_NUMBER:
3226 generate_PUSHNR(cctx, rettv.vval.v_number);
3227 break;
3228#ifdef FEAT_FLOAT
3229 case VAR_FLOAT:
3230 generate_PUSHF(cctx, rettv.vval.v_float);
3231 break;
3232#endif
3233 case VAR_BLOB:
3234 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
3235 rettv.vval.v_blob = NULL;
3236 break;
3237 case VAR_STRING:
3238 generate_PUSHS(cctx, rettv.vval.v_string);
3239 rettv.vval.v_string = NULL;
3240 break;
3241 default:
3242 iemsg("constant type missing");
3243 return FAIL;
3244 }
3245 }
3246 else if (ret == NOTDONE)
3247 {
3248 char_u *p;
3249 int r;
3250
3251 if (!eval_isnamec1(**arg))
3252 {
3253 semsg(_("E1015: Name expected: %s"), *arg);
3254 return FAIL;
3255 }
3256
3257 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003258 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 if (*p == '(')
3260 r = compile_call(arg, p - *arg, cctx, 0);
3261 else
3262 r = compile_load(arg, p, cctx, TRUE);
3263 if (r == FAIL)
3264 return FAIL;
3265 }
3266
3267 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
3268 return FAIL;
3269
3270 // Now deal with prefixed '-', '+' and '!', if not done already.
3271 return compile_leader(cctx, start_leader, end_leader);
3272}
3273
3274/*
3275 * * number multiplication
3276 * / number division
3277 * % number modulo
3278 */
3279 static int
3280compile_expr6(char_u **arg, cctx_T *cctx)
3281{
3282 char_u *op;
3283
3284 // get the first variable
3285 if (compile_expr7(arg, cctx) == FAIL)
3286 return FAIL;
3287
3288 /*
3289 * Repeat computing, until no "*", "/" or "%" is following.
3290 */
3291 for (;;)
3292 {
3293 op = skipwhite(*arg);
3294 if (*op != '*' && *op != '/' && *op != '%')
3295 break;
3296 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1]))
3297 {
3298 char_u buf[3];
3299
3300 vim_strncpy(buf, op, 1);
3301 semsg(_(e_white_both), buf);
3302 }
3303 *arg = skipwhite(op + 1);
3304
3305 // get the second variable
3306 if (compile_expr7(arg, cctx) == FAIL)
3307 return FAIL;
3308
3309 generate_two_op(cctx, op);
3310 }
3311
3312 return OK;
3313}
3314
3315/*
3316 * + number addition
3317 * - number subtraction
3318 * .. string concatenation
3319 */
3320 static int
3321compile_expr5(char_u **arg, cctx_T *cctx)
3322{
3323 char_u *op;
3324 int oplen;
3325
3326 // get the first variable
3327 if (compile_expr6(arg, cctx) == FAIL)
3328 return FAIL;
3329
3330 /*
3331 * Repeat computing, until no "+", "-" or ".." is following.
3332 */
3333 for (;;)
3334 {
3335 op = skipwhite(*arg);
3336 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3337 break;
3338 oplen = (*op == '.' ? 2 : 1);
3339
3340 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen]))
3341 {
3342 char_u buf[3];
3343
3344 vim_strncpy(buf, op, oplen);
3345 semsg(_(e_white_both), buf);
3346 }
3347
3348 *arg = skipwhite(op + oplen);
3349
3350 // get the second variable
3351 if (compile_expr6(arg, cctx) == FAIL)
3352 return FAIL;
3353
3354 if (*op == '.')
3355 {
3356 if (may_generate_2STRING(-2, cctx) == FAIL
3357 || may_generate_2STRING(-1, cctx) == FAIL)
3358 return FAIL;
3359 generate_instr_drop(cctx, ISN_CONCAT, 1);
3360 }
3361 else
3362 generate_two_op(cctx, op);
3363 }
3364
3365 return OK;
3366}
3367
Bram Moolenaar080457c2020-03-03 21:53:32 +01003368 static exptype_T
3369get_compare_type(char_u *p, int *len, int *type_is)
3370{
3371 exptype_T type = EXPR_UNKNOWN;
3372 int i;
3373
3374 switch (p[0])
3375 {
3376 case '=': if (p[1] == '=')
3377 type = EXPR_EQUAL;
3378 else if (p[1] == '~')
3379 type = EXPR_MATCH;
3380 break;
3381 case '!': if (p[1] == '=')
3382 type = EXPR_NEQUAL;
3383 else if (p[1] == '~')
3384 type = EXPR_NOMATCH;
3385 break;
3386 case '>': if (p[1] != '=')
3387 {
3388 type = EXPR_GREATER;
3389 *len = 1;
3390 }
3391 else
3392 type = EXPR_GEQUAL;
3393 break;
3394 case '<': if (p[1] != '=')
3395 {
3396 type = EXPR_SMALLER;
3397 *len = 1;
3398 }
3399 else
3400 type = EXPR_SEQUAL;
3401 break;
3402 case 'i': if (p[1] == 's')
3403 {
3404 // "is" and "isnot"; but not a prefix of a name
3405 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3406 *len = 5;
3407 i = p[*len];
3408 if (!isalnum(i) && i != '_')
3409 {
3410 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3411 *type_is = TRUE;
3412 }
3413 }
3414 break;
3415 }
3416 return type;
3417}
3418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419/*
3420 * expr5a == expr5b
3421 * expr5a =~ expr5b
3422 * expr5a != expr5b
3423 * expr5a !~ expr5b
3424 * expr5a > expr5b
3425 * expr5a >= expr5b
3426 * expr5a < expr5b
3427 * expr5a <= expr5b
3428 * expr5a is expr5b
3429 * expr5a isnot expr5b
3430 *
3431 * Produces instructions:
3432 * EVAL expr5a Push result of "expr5a"
3433 * EVAL expr5b Push result of "expr5b"
3434 * COMPARE one of the compare instructions
3435 */
3436 static int
3437compile_expr4(char_u **arg, cctx_T *cctx)
3438{
3439 exptype_T type = EXPR_UNKNOWN;
3440 char_u *p;
3441 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 int type_is = FALSE;
3443
3444 // get the first variable
3445 if (compile_expr5(arg, cctx) == FAIL)
3446 return FAIL;
3447
3448 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003449 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450
3451 /*
3452 * If there is a comparative operator, use it.
3453 */
3454 if (type != EXPR_UNKNOWN)
3455 {
3456 int ic = FALSE; // Default: do not ignore case
3457
3458 if (type_is && (p[len] == '?' || p[len] == '#'))
3459 {
3460 semsg(_(e_invexpr2), *arg);
3461 return FAIL;
3462 }
3463 // extra question mark appended: ignore case
3464 if (p[len] == '?')
3465 {
3466 ic = TRUE;
3467 ++len;
3468 }
3469 // extra '#' appended: match case (ignored)
3470 else if (p[len] == '#')
3471 ++len;
3472 // nothing appended: match case
3473
3474 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len]))
3475 {
3476 char_u buf[7];
3477
3478 vim_strncpy(buf, p, len);
3479 semsg(_(e_white_both), buf);
3480 }
3481
3482 // get the second variable
3483 *arg = skipwhite(p + len);
3484 if (compile_expr5(arg, cctx) == FAIL)
3485 return FAIL;
3486
3487 generate_COMPARE(cctx, type, ic);
3488 }
3489
3490 return OK;
3491}
3492
3493/*
3494 * Compile || or &&.
3495 */
3496 static int
3497compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3498{
3499 char_u *p = skipwhite(*arg);
3500 int opchar = *op;
3501
3502 if (p[0] == opchar && p[1] == opchar)
3503 {
3504 garray_T *instr = &cctx->ctx_instr;
3505 garray_T end_ga;
3506
3507 /*
3508 * Repeat until there is no following "||" or "&&"
3509 */
3510 ga_init2(&end_ga, sizeof(int), 10);
3511 while (p[0] == opchar && p[1] == opchar)
3512 {
3513 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3514 semsg(_(e_white_both), op);
3515
3516 if (ga_grow(&end_ga, 1) == FAIL)
3517 {
3518 ga_clear(&end_ga);
3519 return FAIL;
3520 }
3521 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3522 ++end_ga.ga_len;
3523 generate_JUMP(cctx, opchar == '|'
3524 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3525
3526 // eval the next expression
3527 *arg = skipwhite(p + 2);
3528 if ((opchar == '|' ? compile_expr3(arg, cctx)
3529 : compile_expr4(arg, cctx)) == FAIL)
3530 {
3531 ga_clear(&end_ga);
3532 return FAIL;
3533 }
3534 p = skipwhite(*arg);
3535 }
3536
3537 // Fill in the end label in all jumps.
3538 while (end_ga.ga_len > 0)
3539 {
3540 isn_T *isn;
3541
3542 --end_ga.ga_len;
3543 isn = ((isn_T *)instr->ga_data)
3544 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3545 isn->isn_arg.jump.jump_where = instr->ga_len;
3546 }
3547 ga_clear(&end_ga);
3548 }
3549
3550 return OK;
3551}
3552
3553/*
3554 * expr4a && expr4a && expr4a logical AND
3555 *
3556 * Produces instructions:
3557 * EVAL expr4a Push result of "expr4a"
3558 * JUMP_AND_KEEP_IF_FALSE end
3559 * EVAL expr4b Push result of "expr4b"
3560 * JUMP_AND_KEEP_IF_FALSE end
3561 * EVAL expr4c Push result of "expr4c"
3562 * end:
3563 */
3564 static int
3565compile_expr3(char_u **arg, cctx_T *cctx)
3566{
3567 // get the first variable
3568 if (compile_expr4(arg, cctx) == FAIL)
3569 return FAIL;
3570
3571 // || and && work almost the same
3572 return compile_and_or(arg, cctx, "&&");
3573}
3574
3575/*
3576 * expr3a || expr3b || expr3c logical OR
3577 *
3578 * Produces instructions:
3579 * EVAL expr3a Push result of "expr3a"
3580 * JUMP_AND_KEEP_IF_TRUE end
3581 * EVAL expr3b Push result of "expr3b"
3582 * JUMP_AND_KEEP_IF_TRUE end
3583 * EVAL expr3c Push result of "expr3c"
3584 * end:
3585 */
3586 static int
3587compile_expr2(char_u **arg, cctx_T *cctx)
3588{
3589 // eval the first expression
3590 if (compile_expr3(arg, cctx) == FAIL)
3591 return FAIL;
3592
3593 // || and && work almost the same
3594 return compile_and_or(arg, cctx, "||");
3595}
3596
3597/*
3598 * Toplevel expression: expr2 ? expr1a : expr1b
3599 *
3600 * Produces instructions:
3601 * EVAL expr2 Push result of "expr"
3602 * JUMP_IF_FALSE alt jump if false
3603 * EVAL expr1a
3604 * JUMP_ALWAYS end
3605 * alt: EVAL expr1b
3606 * end:
3607 */
3608 static int
3609compile_expr1(char_u **arg, cctx_T *cctx)
3610{
3611 char_u *p;
3612
3613 // evaluate the first expression
3614 if (compile_expr2(arg, cctx) == FAIL)
3615 return FAIL;
3616
3617 p = skipwhite(*arg);
3618 if (*p == '?')
3619 {
3620 garray_T *instr = &cctx->ctx_instr;
3621 garray_T *stack = &cctx->ctx_type_stack;
3622 int alt_idx = instr->ga_len;
3623 int end_idx;
3624 isn_T *isn;
3625 type_T *type1;
3626 type_T *type2;
3627
3628 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3629 semsg(_(e_white_both), "?");
3630
3631 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3632
3633 // evaluate the second expression; any type is accepted
3634 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003635 if (compile_expr1(arg, cctx) == FAIL)
3636 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637
3638 // remember the type and drop it
3639 --stack->ga_len;
3640 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3641
3642 end_idx = instr->ga_len;
3643 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3644
3645 // jump here from JUMP_IF_FALSE
3646 isn = ((isn_T *)instr->ga_data) + alt_idx;
3647 isn->isn_arg.jump.jump_where = instr->ga_len;
3648
3649 // Check for the ":".
3650 p = skipwhite(*arg);
3651 if (*p != ':')
3652 {
3653 emsg(_(e_missing_colon));
3654 return FAIL;
3655 }
3656 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3657 semsg(_(e_white_both), ":");
3658
3659 // evaluate the third expression
3660 *arg = skipwhite(p + 1);
Bram Moolenaara6d53682020-01-28 23:04:06 +01003661 if (compile_expr1(arg, cctx) == FAIL)
3662 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663
3664 // If the types differ, the result has a more generic type.
3665 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003666 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667
3668 // jump here from JUMP_ALWAYS
3669 isn = ((isn_T *)instr->ga_data) + end_idx;
3670 isn->isn_arg.jump.jump_where = instr->ga_len;
3671 }
3672 return OK;
3673}
3674
3675/*
3676 * compile "return [expr]"
3677 */
3678 static char_u *
3679compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3680{
3681 char_u *p = arg;
3682 garray_T *stack = &cctx->ctx_type_stack;
3683 type_T *stack_type;
3684
3685 if (*p != NUL && *p != '|' && *p != '\n')
3686 {
3687 // compile return argument into instructions
3688 if (compile_expr1(&p, cctx) == FAIL)
3689 return NULL;
3690
3691 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3692 if (set_return_type)
3693 cctx->ctx_ufunc->uf_ret_type = stack_type;
3694 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3695 == FAIL)
3696 return NULL;
3697 }
3698 else
3699 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003700 // "set_return_type" cannot be TRUE, only used for a lambda which
3701 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02003702 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
3703 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 {
3705 emsg(_("E1003: Missing return value"));
3706 return NULL;
3707 }
3708
3709 // No argument, return zero.
3710 generate_PUSHNR(cctx, 0);
3711 }
3712
3713 if (generate_instr(cctx, ISN_RETURN) == NULL)
3714 return NULL;
3715
3716 // "return val | endif" is possible
3717 return skipwhite(p);
3718}
3719
3720/*
3721 * Return the length of an assignment operator, or zero if there isn't one.
3722 */
3723 int
3724assignment_len(char_u *p, int *heredoc)
3725{
3726 if (*p == '=')
3727 {
3728 if (p[1] == '<' && p[2] == '<')
3729 {
3730 *heredoc = TRUE;
3731 return 3;
3732 }
3733 return 1;
3734 }
3735 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
3736 return 2;
3737 if (STRNCMP(p, "..=", 3) == 0)
3738 return 3;
3739 return 0;
3740}
3741
3742// words that cannot be used as a variable
3743static char *reserved[] = {
3744 "true",
3745 "false",
3746 NULL
3747};
3748
3749/*
3750 * Get a line for "=<<".
3751 * Return a pointer to the line in allocated memory.
3752 * Return NULL for end-of-file or some error.
3753 */
3754 static char_u *
3755heredoc_getline(
3756 int c UNUSED,
3757 void *cookie,
3758 int indent UNUSED,
3759 int do_concat UNUSED)
3760{
3761 cctx_T *cctx = (cctx_T *)cookie;
3762
3763 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003764 {
3765 iemsg("Heredoc got to end");
3766 return NULL;
3767 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 ++cctx->ctx_lnum;
3769 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
3770 [cctx->ctx_lnum]);
3771}
3772
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003773typedef enum {
3774 dest_local,
3775 dest_option,
3776 dest_env,
3777 dest_global,
3778 dest_vimvar,
3779 dest_script,
3780 dest_reg,
3781} assign_dest_T;
3782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783/*
3784 * compile "let var [= expr]", "const var = expr" and "var = expr"
3785 * "arg" points to "var".
3786 */
3787 static char_u *
3788compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
3789{
3790 char_u *p;
3791 char_u *ret = NULL;
3792 int var_count = 0;
3793 int semicolon = 0;
3794 size_t varlen;
3795 garray_T *instr = &cctx->ctx_instr;
3796 int idx = -1;
Bram Moolenaar01b38622020-03-30 21:28:39 +02003797 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003800 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003802 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 int oplen = 0;
3804 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003805 type_T *type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 lvar_T *lvar;
3807 char_u *name;
3808 char_u *sp;
3809 int has_type = FALSE;
3810 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
3811 int instr_count = -1;
3812
3813 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
3814 if (p == NULL)
3815 return NULL;
3816 if (var_count > 0)
3817 {
3818 // TODO: let [var, var] = list
3819 emsg("Cannot handle a list yet");
3820 return NULL;
3821 }
3822
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003823 // "a: type" is declaring variable "a" with a type, not "a:".
3824 if (is_decl && p == arg + 2 && p[-1] == ':')
3825 --p;
3826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 varlen = p - arg;
3828 name = vim_strnsave(arg, (int)varlen);
3829 if (name == NULL)
3830 return NULL;
3831
Bram Moolenaar080457c2020-03-03 21:53:32 +01003832 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003834 if (*arg == '&')
3835 {
3836 int cc;
3837 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838
Bram Moolenaar080457c2020-03-03 21:53:32 +01003839 dest = dest_option;
3840 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003842 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003843 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 if (is_decl)
3846 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003847 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 goto theend;
3849 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003850 p = arg;
3851 p = find_option_end(&p, &opt_flags);
3852 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003854 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01003855 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003856 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003857 }
3858 cc = *p;
3859 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003860 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003861 *p = cc;
3862 if (opt_type == -3)
3863 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003864 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003865 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003866 }
3867 if (opt_type == -2 || opt_type == 0)
3868 type = &t_string;
3869 else
3870 type = &t_number; // both number and boolean option
3871 }
3872 else if (*arg == '$')
3873 {
3874 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003875 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003876 if (is_decl)
3877 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003878 semsg(_("E1065: Cannot declare an environment variable: %s"),
3879 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003880 goto theend;
3881 }
3882 }
3883 else if (*arg == '@')
3884 {
3885 if (!valid_yank_reg(arg[1], TRUE))
3886 {
3887 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02003888 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003889 }
3890 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003891 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003892 if (is_decl)
3893 {
3894 semsg(_("E1066: Cannot declare a register: %s"), name);
3895 goto theend;
3896 }
3897 }
3898 else if (STRNCMP(arg, "g:", 2) == 0)
3899 {
3900 dest = dest_global;
3901 if (is_decl)
3902 {
3903 semsg(_("E1016: Cannot declare a global variable: %s"), name);
3904 goto theend;
3905 }
3906 }
3907 else if (STRNCMP(arg, "v:", 2) == 0)
3908 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02003909 typval_T *vtv;
3910 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003911
Bram Moolenaar5da356e2020-04-09 19:34:43 +02003912 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003913 if (vimvaridx < 0)
3914 {
3915 semsg(_(e_var_notfound), arg);
3916 goto theend;
3917 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02003918 // We use the current value of "sandbox" here, is that OK?
3919 if (var_check_ro(di_flags, name, FALSE))
3920 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01003921 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02003922 vtv = get_vim_var_tv(vimvaridx);
3923 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003924 if (is_decl)
3925 {
3926 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
3927 goto theend;
3928 }
3929 }
3930 else
3931 {
3932 for (idx = 0; reserved[idx] != NULL; ++idx)
3933 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003935 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 goto theend;
3937 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01003938
3939 idx = lookup_local(arg, varlen, cctx);
3940 if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01003942 if (is_decl)
3943 {
3944 semsg(_("E1017: Variable already declared: %s"), name);
3945 goto theend;
3946 }
3947 else
3948 {
3949 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3950 if (lvar->lv_const)
3951 {
3952 semsg(_("E1018: Cannot assign to a constant: %s"), name);
3953 goto theend;
3954 }
3955 }
3956 }
3957 else if (STRNCMP(arg, "s:", 2) == 0
3958 || lookup_script(arg, varlen) == OK
3959 || find_imported(arg, varlen, cctx) != NULL)
3960 {
3961 dest = dest_script;
3962 if (is_decl)
3963 {
3964 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003965 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003966 goto theend;
3967 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 }
3969 }
3970 }
3971
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01003972 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 {
3974 if (is_decl && *p == ':')
3975 {
3976 // parse optional type: "let var: type = expr"
3977 p = skipwhite(p + 1);
3978 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 has_type = TRUE;
3980 }
Bram Moolenaara8c17702020-04-01 21:17:24 +02003981 else if (idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 {
3983 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
3984 type = lvar->lv_type;
3985 }
3986 }
3987
3988 sp = p;
3989 p = skipwhite(p);
3990 op = p;
3991 oplen = assignment_len(p, &heredoc);
3992 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
3993 {
3994 char_u buf[4];
3995
3996 vim_strncpy(buf, op, oplen);
3997 semsg(_(e_white_both), buf);
3998 }
3999
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004000 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02004001 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004003 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 goto theend;
4005 }
4006
Bram Moolenaar080457c2020-03-03 21:53:32 +01004007 if (idx < 0 && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 {
4009 if (oplen > 1 && !heredoc)
4010 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004011 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4013 name);
4014 goto theend;
4015 }
4016
4017 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02004018 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004019 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4021 if (idx < 0)
4022 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004023 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 }
4025
4026 if (heredoc)
4027 {
4028 list_T *l;
4029 listitem_T *li;
4030
4031 // [let] varname =<< [trim] {end}
4032 eap->getline = heredoc_getline;
4033 eap->cookie = cctx;
4034 l = heredoc_get(eap, op + 3);
4035
4036 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004037 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 {
4039 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4040 li->li_tv.vval.v_string = NULL;
4041 }
4042 generate_NEWLIST(cctx, l->lv_len);
4043 type = &t_list_string;
4044 list_free(l);
4045 p += STRLEN(p);
4046 }
4047 else if (oplen > 0)
4048 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004049 int r;
4050 type_T *stacktype;
4051 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 // for "+=", "*=", "..=" etc. first load the current value
4054 if (*op != '=')
4055 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004056 switch (dest)
4057 {
4058 case dest_option:
4059 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004060 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004061 break;
4062 case dest_global:
4063 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4064 break;
4065 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01004066 compile_load_scriptvar(cctx,
4067 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004068 break;
4069 case dest_env:
4070 // Include $ in the name here
4071 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4072 break;
4073 case dest_reg:
4074 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
4075 break;
4076 case dest_vimvar:
4077 generate_LOADV(cctx, name + 2, TRUE);
4078 break;
4079 case dest_local:
4080 generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
4081 break;
4082 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083 }
4084
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004085 // Compile the expression. Temporarily hide the new local variable
4086 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02004087 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004088 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 instr_count = instr->ga_len;
4090 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004091 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02004092 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004093 ++cctx->ctx_locals.ga_len;
4094 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 goto theend;
4096
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004097 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004099 stack = &cctx->ctx_type_stack;
4100 stacktype = stack->ga_len == 0 ? &t_void
4101 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
4102 if (idx >= 0 && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004104 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
4105 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004107 if (stacktype->tt_type == VAR_VOID)
4108 {
4109 emsg(_("E1031: Cannot use void value"));
4110 goto theend;
4111 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004112 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004113 {
4114 // An empty list or dict has a &t_void member, for a
4115 // variable that implies &t_any.
4116 if (stacktype == &t_list_empty)
4117 lvar->lv_type = &t_list_any;
4118 else if (stacktype == &t_dict_empty)
4119 lvar->lv_type = &t_dict_any;
4120 else
4121 lvar->lv_type = stacktype;
4122 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004123 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004124 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
4125 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004127 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004128 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 }
4130 }
4131 else if (cmdidx == CMD_const)
4132 {
4133 emsg(_("E1021: const requires a value"));
4134 goto theend;
4135 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004136 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 {
4138 emsg(_("E1022: type or initialization required"));
4139 goto theend;
4140 }
4141 else
4142 {
4143 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 if (ga_grow(instr, 1) == FAIL)
4145 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004146 switch (type->tt_type)
4147 {
4148 case VAR_BOOL:
4149 generate_PUSHBOOL(cctx, VVAL_FALSE);
4150 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004151 case VAR_FLOAT:
4152#ifdef FEAT_FLOAT
4153 generate_PUSHF(cctx, 0.0);
4154#endif
4155 break;
4156 case VAR_STRING:
4157 generate_PUSHS(cctx, NULL);
4158 break;
4159 case VAR_BLOB:
4160 generate_PUSHBLOB(cctx, NULL);
4161 break;
4162 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004163 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004164 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004165 case VAR_LIST:
4166 generate_NEWLIST(cctx, 0);
4167 break;
4168 case VAR_DICT:
4169 generate_NEWDICT(cctx, 0);
4170 break;
4171 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004172 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004173 break;
4174 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004175 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004176 break;
4177 case VAR_NUMBER:
4178 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004179 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02004180 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01004181 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02004182 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01004183 generate_PUSHNR(cctx, 0);
4184 break;
4185 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 }
4187
4188 if (oplen > 0 && *op != '=')
4189 {
4190 type_T *expected = &t_number;
4191 garray_T *stack = &cctx->ctx_type_stack;
4192 type_T *stacktype;
4193
4194 // TODO: if type is known use float or any operation
4195
4196 if (*op == '.')
4197 expected = &t_string;
4198 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4199 if (need_type(stacktype, expected, -1, cctx) == FAIL)
4200 goto theend;
4201
4202 if (*op == '.')
4203 generate_instr_drop(cctx, ISN_CONCAT, 1);
4204 else
4205 {
4206 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4207
4208 if (isn == NULL)
4209 goto theend;
4210 switch (*op)
4211 {
4212 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
4213 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4214 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4215 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4216 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4217 }
4218 }
4219 }
4220
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004221 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004223 case dest_option:
4224 generate_STOREOPT(cctx, name + 1, opt_flags);
4225 break;
4226 case dest_global:
4227 // include g: with the name, easier to execute that way
4228 generate_STORE(cctx, ISN_STOREG, 0, name);
4229 break;
4230 case dest_env:
4231 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
4232 break;
4233 case dest_reg:
4234 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
4235 break;
4236 case dest_vimvar:
4237 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
4238 break;
4239 case dest_script:
4240 {
4241 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4242 imported_T *import = NULL;
4243 int sid = current_sctx.sc_sid;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004244
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004245 if (name[1] != ':')
4246 {
4247 import = find_imported(name, 0, cctx);
4248 if (import != NULL)
4249 sid = import->imp_sid;
4250 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004252 idx = get_script_item_idx(sid, rawname, TRUE);
4253 // TODO: specific type
4254 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004255 {
4256 char_u *name_s = name;
4257
4258 // Include s: in the name for store_var()
4259 if (name[1] != ':')
4260 {
4261 int len = (int)STRLEN(name) + 3;
4262
4263 name_s = alloc(len);
4264 if (name_s == NULL)
4265 name_s = name;
4266 else
4267 vim_snprintf((char *)name_s, len, "s:%s", name);
4268 }
4269 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
4270 if (name_s != name)
4271 vim_free(name_s);
4272 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004273 else
4274 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
4275 sid, idx, &t_any);
4276 }
4277 break;
4278 case dest_local:
4279 {
4280 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004282 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
4283 // into ISN_STORENR
4284 if (instr->ga_len == instr_count + 1
4285 && isn->isn_type == ISN_PUSHNR)
4286 {
4287 varnumber_T val = isn->isn_arg.number;
4288 garray_T *stack = &cctx->ctx_type_stack;
4289
4290 isn->isn_type = ISN_STORENR;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004291 isn->isn_arg.storenr.stnr_idx = idx;
4292 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004293 if (stack->ga_len > 0)
4294 --stack->ga_len;
4295 }
4296 else
4297 generate_STORE(cctx, ISN_STORE, idx, NULL);
4298 }
4299 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 }
4301 ret = p;
4302
4303theend:
4304 vim_free(name);
4305 return ret;
4306}
4307
4308/*
4309 * Compile an :import command.
4310 */
4311 static char_u *
4312compile_import(char_u *arg, cctx_T *cctx)
4313{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01004314 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315}
4316
4317/*
4318 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
4319 */
4320 static int
4321compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
4322{
4323 garray_T *instr = &cctx->ctx_instr;
4324 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
4325
4326 if (endlabel == NULL)
4327 return FAIL;
4328 endlabel->el_next = *el;
4329 *el = endlabel;
4330 endlabel->el_end_label = instr->ga_len;
4331
4332 generate_JUMP(cctx, when, 0);
4333 return OK;
4334}
4335
4336 static void
4337compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
4338{
4339 garray_T *instr = &cctx->ctx_instr;
4340
4341 while (*el != NULL)
4342 {
4343 endlabel_T *cur = (*el);
4344 isn_T *isn;
4345
4346 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
4347 isn->isn_arg.jump.jump_where = instr->ga_len;
4348 *el = cur->el_next;
4349 vim_free(cur);
4350 }
4351}
4352
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004353 static void
4354compile_free_jump_to_end(endlabel_T **el)
4355{
4356 while (*el != NULL)
4357 {
4358 endlabel_T *cur = (*el);
4359
4360 *el = cur->el_next;
4361 vim_free(cur);
4362 }
4363}
4364
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365/*
4366 * Create a new scope and set up the generic items.
4367 */
4368 static scope_T *
4369new_scope(cctx_T *cctx, scopetype_T type)
4370{
4371 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
4372
4373 if (scope == NULL)
4374 return NULL;
4375 scope->se_outer = cctx->ctx_scope;
4376 cctx->ctx_scope = scope;
4377 scope->se_type = type;
4378 scope->se_local_count = cctx->ctx_locals.ga_len;
4379 return scope;
4380}
4381
4382/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004383 * Free the current scope and go back to the outer scope.
4384 */
4385 static void
4386drop_scope(cctx_T *cctx)
4387{
4388 scope_T *scope = cctx->ctx_scope;
4389
4390 if (scope == NULL)
4391 {
4392 iemsg("calling drop_scope() without a scope");
4393 return;
4394 }
4395 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004396 switch (scope->se_type)
4397 {
4398 case IF_SCOPE:
4399 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
4400 case FOR_SCOPE:
4401 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
4402 case WHILE_SCOPE:
4403 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
4404 case TRY_SCOPE:
4405 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
4406 case NO_SCOPE:
4407 case BLOCK_SCOPE:
4408 break;
4409 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004410 vim_free(scope);
4411}
4412
4413/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004414 * Evaluate an expression that is a constant:
4415 * has(arg)
4416 *
4417 * Also handle:
4418 * ! in front logical NOT
4419 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004420 * Return FAIL if the expression is not a constant.
4421 */
4422 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004423evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004424{
4425 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004426 char_u *start_leader, *end_leader;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004427 int has_call = FALSE;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004428
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004429 /*
4430 * Skip '!' characters. They are handled later.
4431 */
4432 start_leader = *arg;
4433 while (**arg == '!')
4434 *arg = skipwhite(*arg + 1);
4435 end_leader = *arg;
4436
4437 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004438 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004439 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004440 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4441 {
4442 tv->v_type = VAR_SPECIAL;
4443 tv->vval.v_number = VVAL_TRUE;
4444 *arg += 4;
4445 return OK;
4446 }
4447 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4448 {
4449 tv->v_type = VAR_SPECIAL;
4450 tv->vval.v_number = VVAL_FALSE;
4451 *arg += 5;
4452 return OK;
4453 }
4454
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004455 if (STRNCMP("has(", *arg, 4) == 0)
4456 {
4457 has_call = TRUE;
4458 *arg = skipwhite(*arg + 4);
4459 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004460
4461 if (**arg == '"')
4462 {
4463 if (get_string_tv(arg, tv, TRUE) == FAIL)
4464 return FAIL;
4465 }
4466 else if (**arg == '\'')
4467 {
4468 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4469 return FAIL;
4470 }
4471 else
4472 return FAIL;
4473
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004474 if (has_call)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004475 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004476 *arg = skipwhite(*arg);
4477 if (**arg != ')')
4478 return FAIL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004479 *arg = *arg + 1;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004480
4481 argvars[0] = *tv;
4482 argvars[1].v_type = VAR_UNKNOWN;
4483 tv->v_type = VAR_NUMBER;
4484 tv->vval.v_number = 0;
4485 f_has(argvars, tv);
4486 clear_tv(&argvars[0]);
4487
4488 while (start_leader < end_leader)
4489 {
4490 if (*start_leader == '!')
4491 tv->vval.v_number = !tv->vval.v_number;
4492 ++start_leader;
4493 }
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004494 }
4495
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004496 return OK;
4497}
4498
Bram Moolenaar080457c2020-03-03 21:53:32 +01004499 static int
4500evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4501{
4502 exptype_T type = EXPR_UNKNOWN;
4503 char_u *p;
4504 int len = 2;
4505 int type_is = FALSE;
4506
4507 // get the first variable
4508 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4509 return FAIL;
4510
4511 p = skipwhite(*arg);
4512 type = get_compare_type(p, &len, &type_is);
4513
4514 /*
4515 * If there is a comparative operator, use it.
4516 */
4517 if (type != EXPR_UNKNOWN)
4518 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004519 typval_T tv2;
4520 char_u *s1, *s2;
4521 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4522 int n;
4523
4524 // TODO: Only string == string is supported now
4525 if (tv->v_type != VAR_STRING)
4526 return FAIL;
4527 if (type != EXPR_EQUAL)
4528 return FAIL;
4529
4530 // get the second variable
Bram Moolenaar4227c782020-04-02 16:00:04 +02004531 init_tv(&tv2);
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004532 *arg = skipwhite(p + len);
4533 if (evaluate_const_expr7(arg, cctx, &tv2) == FAIL
4534 || tv2.v_type != VAR_STRING)
4535 {
4536 clear_tv(&tv2);
4537 return FAIL;
4538 }
4539 s1 = tv_get_string_buf(tv, buf1);
4540 s2 = tv_get_string_buf(&tv2, buf2);
4541 n = STRCMP(s1, s2);
4542 clear_tv(tv);
4543 clear_tv(&tv2);
4544 tv->v_type = VAR_BOOL;
4545 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004546 }
4547
4548 return OK;
4549}
4550
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004551static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
4552
4553/*
4554 * Compile constant || or &&.
4555 */
4556 static int
4557evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
4558{
4559 char_u *p = skipwhite(*arg);
4560 int opchar = *op;
4561
4562 if (p[0] == opchar && p[1] == opchar)
4563 {
4564 int val = tv2bool(tv);
4565
4566 /*
4567 * Repeat until there is no following "||" or "&&"
4568 */
4569 while (p[0] == opchar && p[1] == opchar)
4570 {
4571 typval_T tv2;
4572
4573 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
4574 return FAIL;
4575
4576 // eval the next expression
4577 *arg = skipwhite(p + 2);
4578 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01004579 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004580 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004581 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004582 {
4583 clear_tv(&tv2);
4584 return FAIL;
4585 }
4586 if ((opchar == '&') == val)
4587 {
4588 // false || tv2 or true && tv2: use tv2
4589 clear_tv(tv);
4590 *tv = tv2;
4591 val = tv2bool(tv);
4592 }
4593 else
4594 clear_tv(&tv2);
4595 p = skipwhite(*arg);
4596 }
4597 }
4598
4599 return OK;
4600}
4601
4602/*
4603 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
4604 * Return FAIL if the expression is not a constant.
4605 */
4606 static int
4607evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
4608{
4609 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01004610 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004611 return FAIL;
4612
4613 // || and && work almost the same
4614 return evaluate_const_and_or(arg, cctx, "&&", tv);
4615}
4616
4617/*
4618 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
4619 * Return FAIL if the expression is not a constant.
4620 */
4621 static int
4622evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
4623{
4624 // evaluate the first expression
4625 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
4626 return FAIL;
4627
4628 // || and && work almost the same
4629 return evaluate_const_and_or(arg, cctx, "||", tv);
4630}
4631
4632/*
4633 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
4634 * E.g. for "has('feature')".
4635 * This does not produce error messages. "tv" should be cleared afterwards.
4636 * Return FAIL if the expression is not a constant.
4637 */
4638 static int
4639evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
4640{
4641 char_u *p;
4642
4643 // evaluate the first expression
4644 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
4645 return FAIL;
4646
4647 p = skipwhite(*arg);
4648 if (*p == '?')
4649 {
4650 int val = tv2bool(tv);
4651 typval_T tv2;
4652
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004653 // require space before and after the ?
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004654 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4655 return FAIL;
4656
4657 // evaluate the second expression; any type is accepted
4658 clear_tv(tv);
4659 *arg = skipwhite(p + 1);
4660 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
4661 return FAIL;
4662
4663 // Check for the ":".
4664 p = skipwhite(*arg);
4665 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
4666 return FAIL;
4667
4668 // evaluate the third expression
4669 *arg = skipwhite(p + 1);
4670 tv2.v_type = VAR_UNKNOWN;
4671 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
4672 {
4673 clear_tv(&tv2);
4674 return FAIL;
4675 }
4676 if (val)
4677 {
4678 // use the expr after "?"
4679 clear_tv(&tv2);
4680 }
4681 else
4682 {
4683 // use the expr after ":"
4684 clear_tv(tv);
4685 *tv = tv2;
4686 }
4687 }
4688 return OK;
4689}
4690
4691/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 * compile "if expr"
4693 *
4694 * "if expr" Produces instructions:
4695 * EVAL expr Push result of "expr"
4696 * JUMP_IF_FALSE end
4697 * ... body ...
4698 * end:
4699 *
4700 * "if expr | else" Produces instructions:
4701 * EVAL expr Push result of "expr"
4702 * JUMP_IF_FALSE else
4703 * ... body ...
4704 * JUMP_ALWAYS end
4705 * else:
4706 * ... body ...
4707 * end:
4708 *
4709 * "if expr1 | elseif expr2 | else" Produces instructions:
4710 * EVAL expr Push result of "expr"
4711 * JUMP_IF_FALSE elseif
4712 * ... body ...
4713 * JUMP_ALWAYS end
4714 * elseif:
4715 * EVAL expr Push result of "expr"
4716 * JUMP_IF_FALSE else
4717 * ... body ...
4718 * JUMP_ALWAYS end
4719 * else:
4720 * ... body ...
4721 * end:
4722 */
4723 static char_u *
4724compile_if(char_u *arg, cctx_T *cctx)
4725{
4726 char_u *p = arg;
4727 garray_T *instr = &cctx->ctx_instr;
4728 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004729 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004731 // compile "expr"; if we know it evaluates to FALSE skip the block
4732 tv.v_type = VAR_UNKNOWN;
4733 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4734 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4735 else
4736 cctx->ctx_skip = MAYBE;
4737 clear_tv(&tv);
4738 if (cctx->ctx_skip == MAYBE)
4739 {
4740 p = arg;
4741 if (compile_expr1(&p, cctx) == FAIL)
4742 return NULL;
4743 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744
4745 scope = new_scope(cctx, IF_SCOPE);
4746 if (scope == NULL)
4747 return NULL;
4748
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004749 if (cctx->ctx_skip == MAYBE)
4750 {
4751 // "where" is set when ":elseif", "else" or ":endif" is found
4752 scope->se_u.se_if.is_if_label = instr->ga_len;
4753 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4754 }
4755 else
4756 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757
4758 return p;
4759}
4760
4761 static char_u *
4762compile_elseif(char_u *arg, cctx_T *cctx)
4763{
4764 char_u *p = arg;
4765 garray_T *instr = &cctx->ctx_instr;
4766 isn_T *isn;
4767 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004768 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769
4770 if (scope == NULL || scope->se_type != IF_SCOPE)
4771 {
4772 emsg(_(e_elseif_without_if));
4773 return NULL;
4774 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004775 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776
Bram Moolenaar158906c2020-02-06 20:39:45 +01004777 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004778 {
4779 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004781 return NULL;
4782 // previous "if" or "elseif" jumps here
4783 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4784 isn->isn_arg.jump.jump_where = instr->ga_len;
4785 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004787 // compile "expr"; if we know it evaluates to FALSE skip the block
4788 tv.v_type = VAR_UNKNOWN;
4789 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
4790 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
4791 else
4792 cctx->ctx_skip = MAYBE;
4793 clear_tv(&tv);
4794 if (cctx->ctx_skip == MAYBE)
4795 {
4796 p = arg;
4797 if (compile_expr1(&p, cctx) == FAIL)
4798 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004800 // "where" is set when ":elseif", "else" or ":endif" is found
4801 scope->se_u.se_if.is_if_label = instr->ga_len;
4802 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4803 }
4804 else
4805 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806
4807 return p;
4808}
4809
4810 static char_u *
4811compile_else(char_u *arg, cctx_T *cctx)
4812{
4813 char_u *p = arg;
4814 garray_T *instr = &cctx->ctx_instr;
4815 isn_T *isn;
4816 scope_T *scope = cctx->ctx_scope;
4817
4818 if (scope == NULL || scope->se_type != IF_SCOPE)
4819 {
4820 emsg(_(e_else_without_if));
4821 return NULL;
4822 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01004823 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004825 // jump from previous block to the end, unless the else block is empty
4826 if (cctx->ctx_skip == MAYBE)
4827 {
4828 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004830 return NULL;
4831 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832
Bram Moolenaar158906c2020-02-06 20:39:45 +01004833 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004834 {
4835 if (scope->se_u.se_if.is_if_label >= 0)
4836 {
4837 // previous "if" or "elseif" jumps here
4838 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4839 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01004840 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004841 }
4842 }
4843
4844 if (cctx->ctx_skip != MAYBE)
4845 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846
4847 return p;
4848}
4849
4850 static char_u *
4851compile_endif(char_u *arg, cctx_T *cctx)
4852{
4853 scope_T *scope = cctx->ctx_scope;
4854 ifscope_T *ifscope;
4855 garray_T *instr = &cctx->ctx_instr;
4856 isn_T *isn;
4857
4858 if (scope == NULL || scope->se_type != IF_SCOPE)
4859 {
4860 emsg(_(e_endif_without_if));
4861 return NULL;
4862 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004863 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004864 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004866 if (scope->se_u.se_if.is_if_label >= 0)
4867 {
4868 // previous "if" or "elseif" jumps here
4869 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
4870 isn->isn_arg.jump.jump_where = instr->ga_len;
4871 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 // Fill in the "end" label in jumps at the end of the blocks.
4873 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004874 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004876 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 return arg;
4878}
4879
4880/*
4881 * compile "for var in expr"
4882 *
4883 * Produces instructions:
4884 * PUSHNR -1
4885 * STORE loop-idx Set index to -1
4886 * EVAL expr Push result of "expr"
4887 * top: FOR loop-idx, end Increment index, use list on bottom of stack
4888 * - if beyond end, jump to "end"
4889 * - otherwise get item from list and push it
4890 * STORE var Store item in "var"
4891 * ... body ...
4892 * JUMP top Jump back to repeat
4893 * end: DROP Drop the result of "expr"
4894 *
4895 */
4896 static char_u *
4897compile_for(char_u *arg, cctx_T *cctx)
4898{
4899 char_u *p;
4900 size_t varlen;
4901 garray_T *instr = &cctx->ctx_instr;
4902 garray_T *stack = &cctx->ctx_type_stack;
4903 scope_T *scope;
4904 int loop_idx; // index of loop iteration variable
4905 int var_idx; // index of "var"
4906 type_T *vartype;
4907
4908 // TODO: list of variables: "for [key, value] in dict"
4909 // parse "var"
4910 for (p = arg; eval_isnamec1(*p); ++p)
4911 ;
4912 varlen = p - arg;
4913 var_idx = lookup_local(arg, varlen, cctx);
4914 if (var_idx >= 0)
4915 {
4916 semsg(_("E1023: variable already defined: %s"), arg);
4917 return NULL;
4918 }
4919
4920 // consume "in"
4921 p = skipwhite(p);
4922 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
4923 {
4924 emsg(_(e_missing_in));
4925 return NULL;
4926 }
4927 p = skipwhite(p + 2);
4928
4929
4930 scope = new_scope(cctx, FOR_SCOPE);
4931 if (scope == NULL)
4932 return NULL;
4933
4934 // Reserve a variable to store the loop iteration counter.
4935 loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
4936 if (loop_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004937 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004938 // only happens when out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004939 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004941 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942
4943 // Reserve a variable to store "var"
4944 var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any);
4945 if (var_idx < 0)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004946 {
4947 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004949 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950
4951 generate_STORENR(cctx, loop_idx, -1);
4952
4953 // compile "expr", it remains on the stack until "endfor"
4954 arg = p;
4955 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004956 {
4957 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004959 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960
4961 // now we know the type of "var"
4962 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4963 if (vartype->tt_type != VAR_LIST)
4964 {
4965 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004966 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967 return NULL;
4968 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02004969 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970 {
4971 lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx;
4972
4973 lvar->lv_type = vartype->tt_member;
4974 }
4975
4976 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004977 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978
4979 generate_FOR(cctx, loop_idx);
4980 generate_STORE(cctx, ISN_STORE, var_idx, NULL);
4981
4982 return arg;
4983}
4984
4985/*
4986 * compile "endfor"
4987 */
4988 static char_u *
4989compile_endfor(char_u *arg, cctx_T *cctx)
4990{
4991 garray_T *instr = &cctx->ctx_instr;
4992 scope_T *scope = cctx->ctx_scope;
4993 forscope_T *forscope;
4994 isn_T *isn;
4995
4996 if (scope == NULL || scope->se_type != FOR_SCOPE)
4997 {
4998 emsg(_(e_for));
4999 return NULL;
5000 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005001 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005003 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004
5005 // At end of ":for" scope jump back to the FOR instruction.
5006 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5007
5008 // Fill in the "end" label in the FOR statement so it can jump here
5009 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5010 isn->isn_arg.forloop.for_end = instr->ga_len;
5011
5012 // Fill in the "end" label any BREAK statements
5013 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5014
5015 // Below the ":for" scope drop the "expr" list from the stack.
5016 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5017 return NULL;
5018
5019 vim_free(scope);
5020
5021 return arg;
5022}
5023
5024/*
5025 * compile "while expr"
5026 *
5027 * Produces instructions:
5028 * top: EVAL expr Push result of "expr"
5029 * JUMP_IF_FALSE end jump if false
5030 * ... body ...
5031 * JUMP top Jump back to repeat
5032 * end:
5033 *
5034 */
5035 static char_u *
5036compile_while(char_u *arg, cctx_T *cctx)
5037{
5038 char_u *p = arg;
5039 garray_T *instr = &cctx->ctx_instr;
5040 scope_T *scope;
5041
5042 scope = new_scope(cctx, WHILE_SCOPE);
5043 if (scope == NULL)
5044 return NULL;
5045
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005046 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005047
5048 // compile "expr"
5049 if (compile_expr1(&p, cctx) == FAIL)
5050 return NULL;
5051
5052 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005053 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054 JUMP_IF_FALSE, cctx) == FAIL)
5055 return FAIL;
5056
5057 return p;
5058}
5059
5060/*
5061 * compile "endwhile"
5062 */
5063 static char_u *
5064compile_endwhile(char_u *arg, cctx_T *cctx)
5065{
5066 scope_T *scope = cctx->ctx_scope;
5067
5068 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5069 {
5070 emsg(_(e_while));
5071 return NULL;
5072 }
5073 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005074 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075
5076 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005077 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078
5079 // Fill in the "end" label in the WHILE statement so it can jump here.
5080 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005081 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082
5083 vim_free(scope);
5084
5085 return arg;
5086}
5087
5088/*
5089 * compile "continue"
5090 */
5091 static char_u *
5092compile_continue(char_u *arg, cctx_T *cctx)
5093{
5094 scope_T *scope = cctx->ctx_scope;
5095
5096 for (;;)
5097 {
5098 if (scope == NULL)
5099 {
5100 emsg(_(e_continue));
5101 return NULL;
5102 }
5103 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5104 break;
5105 scope = scope->se_outer;
5106 }
5107
5108 // Jump back to the FOR or WHILE instruction.
5109 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005110 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5111 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112 return arg;
5113}
5114
5115/*
5116 * compile "break"
5117 */
5118 static char_u *
5119compile_break(char_u *arg, cctx_T *cctx)
5120{
5121 scope_T *scope = cctx->ctx_scope;
5122 endlabel_T **el;
5123
5124 for (;;)
5125 {
5126 if (scope == NULL)
5127 {
5128 emsg(_(e_break));
5129 return NULL;
5130 }
5131 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5132 break;
5133 scope = scope->se_outer;
5134 }
5135
5136 // Jump to the end of the FOR or WHILE loop.
5137 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005138 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005140 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005141 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5142 return FAIL;
5143
5144 return arg;
5145}
5146
5147/*
5148 * compile "{" start of block
5149 */
5150 static char_u *
5151compile_block(char_u *arg, cctx_T *cctx)
5152{
5153 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5154 return NULL;
5155 return skipwhite(arg + 1);
5156}
5157
5158/*
5159 * compile end of block: drop one scope
5160 */
5161 static void
5162compile_endblock(cctx_T *cctx)
5163{
5164 scope_T *scope = cctx->ctx_scope;
5165
5166 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005167 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005168 vim_free(scope);
5169}
5170
5171/*
5172 * compile "try"
5173 * Creates a new scope for the try-endtry, pointing to the first catch and
5174 * finally.
5175 * Creates another scope for the "try" block itself.
5176 * TRY instruction sets up exception handling at runtime.
5177 *
5178 * "try"
5179 * TRY -> catch1, -> finally push trystack entry
5180 * ... try block
5181 * "throw {exception}"
5182 * EVAL {exception}
5183 * THROW create exception
5184 * ... try block
5185 * " catch {expr}"
5186 * JUMP -> finally
5187 * catch1: PUSH exeception
5188 * EVAL {expr}
5189 * MATCH
5190 * JUMP nomatch -> catch2
5191 * CATCH remove exception
5192 * ... catch block
5193 * " catch"
5194 * JUMP -> finally
5195 * catch2: CATCH remove exception
5196 * ... catch block
5197 * " finally"
5198 * finally:
5199 * ... finally block
5200 * " endtry"
5201 * ENDTRY pop trystack entry, may rethrow
5202 */
5203 static char_u *
5204compile_try(char_u *arg, cctx_T *cctx)
5205{
5206 garray_T *instr = &cctx->ctx_instr;
5207 scope_T *try_scope;
5208 scope_T *scope;
5209
5210 // scope that holds the jumps that go to catch/finally/endtry
5211 try_scope = new_scope(cctx, TRY_SCOPE);
5212 if (try_scope == NULL)
5213 return NULL;
5214
5215 // "catch" is set when the first ":catch" is found.
5216 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005217 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 if (generate_instr(cctx, ISN_TRY) == NULL)
5219 return NULL;
5220
5221 // scope for the try block itself
5222 scope = new_scope(cctx, BLOCK_SCOPE);
5223 if (scope == NULL)
5224 return NULL;
5225
5226 return arg;
5227}
5228
5229/*
5230 * compile "catch {expr}"
5231 */
5232 static char_u *
5233compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5234{
5235 scope_T *scope = cctx->ctx_scope;
5236 garray_T *instr = &cctx->ctx_instr;
5237 char_u *p;
5238 isn_T *isn;
5239
5240 // end block scope from :try or :catch
5241 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5242 compile_endblock(cctx);
5243 scope = cctx->ctx_scope;
5244
5245 // Error if not in a :try scope
5246 if (scope == NULL || scope->se_type != TRY_SCOPE)
5247 {
5248 emsg(_(e_catch));
5249 return NULL;
5250 }
5251
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005252 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005253 {
5254 emsg(_("E1033: catch unreachable after catch-all"));
5255 return NULL;
5256 }
5257
5258 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005259 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005260 JUMP_ALWAYS, cctx) == FAIL)
5261 return NULL;
5262
5263 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005264 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005265 if (isn->isn_arg.try.try_catch == 0)
5266 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005267 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005268 {
5269 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005270 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005271 isn->isn_arg.jump.jump_where = instr->ga_len;
5272 }
5273
5274 p = skipwhite(arg);
5275 if (ends_excmd(*p))
5276 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005277 scope->se_u.se_try.ts_caught_all = TRUE;
5278 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005279 }
5280 else
5281 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005282 char_u *end;
5283 char_u *pat;
5284 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005285 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005286 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005288 // Push v:exception, push {expr} and MATCH
5289 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5290
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005291 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005292 if (*end != *p)
5293 {
5294 semsg(_("E1067: Separator mismatch: %s"), p);
5295 vim_free(tofree);
5296 return FAIL;
5297 }
5298 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005299 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005300 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005301 len = (int)(end - tofree);
5302 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005303 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005304 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005305 if (pat == NULL)
5306 return FAIL;
5307 if (generate_PUSHS(cctx, pat) == FAIL)
5308 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005309
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005310 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5311 return NULL;
5312
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005313 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005314 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5315 return NULL;
5316 }
5317
5318 if (generate_instr(cctx, ISN_CATCH) == NULL)
5319 return NULL;
5320
5321 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5322 return NULL;
5323 return p;
5324}
5325
5326 static char_u *
5327compile_finally(char_u *arg, cctx_T *cctx)
5328{
5329 scope_T *scope = cctx->ctx_scope;
5330 garray_T *instr = &cctx->ctx_instr;
5331 isn_T *isn;
5332
5333 // end block scope from :try or :catch
5334 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5335 compile_endblock(cctx);
5336 scope = cctx->ctx_scope;
5337
5338 // Error if not in a :try scope
5339 if (scope == NULL || scope->se_type != TRY_SCOPE)
5340 {
5341 emsg(_(e_finally));
5342 return NULL;
5343 }
5344
5345 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005346 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005347 if (isn->isn_arg.try.try_finally != 0)
5348 {
5349 emsg(_(e_finally_dup));
5350 return NULL;
5351 }
5352
5353 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005354 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005355
Bram Moolenaar585fea72020-04-02 22:33:21 +02005356 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005357 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005358 {
5359 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005360 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361 isn->isn_arg.jump.jump_where = instr->ga_len;
5362 }
5363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005364 // TODO: set index in ts_finally_label jumps
5365
5366 return arg;
5367}
5368
5369 static char_u *
5370compile_endtry(char_u *arg, cctx_T *cctx)
5371{
5372 scope_T *scope = cctx->ctx_scope;
5373 garray_T *instr = &cctx->ctx_instr;
5374 isn_T *isn;
5375
5376 // end block scope from :catch or :finally
5377 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5378 compile_endblock(cctx);
5379 scope = cctx->ctx_scope;
5380
5381 // Error if not in a :try scope
5382 if (scope == NULL || scope->se_type != TRY_SCOPE)
5383 {
5384 if (scope == NULL)
5385 emsg(_(e_no_endtry));
5386 else if (scope->se_type == WHILE_SCOPE)
5387 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01005388 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005389 emsg(_(e_endfor));
5390 else
5391 emsg(_(e_endif));
5392 return NULL;
5393 }
5394
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005395 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005396 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
5397 {
5398 emsg(_("E1032: missing :catch or :finally"));
5399 return NULL;
5400 }
5401
5402 // Fill in the "end" label in jumps at the end of the blocks, if not done
5403 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005404 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005405
5406 // End :catch or :finally scope: set value in ISN_TRY instruction
5407 if (isn->isn_arg.try.try_finally == 0)
5408 isn->isn_arg.try.try_finally = instr->ga_len;
5409 compile_endblock(cctx);
5410
5411 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
5412 return NULL;
5413 return arg;
5414}
5415
5416/*
5417 * compile "throw {expr}"
5418 */
5419 static char_u *
5420compile_throw(char_u *arg, cctx_T *cctx UNUSED)
5421{
5422 char_u *p = skipwhite(arg);
5423
5424 if (ends_excmd(*p))
5425 {
5426 emsg(_(e_argreq));
5427 return NULL;
5428 }
5429 if (compile_expr1(&p, cctx) == FAIL)
5430 return NULL;
5431 if (may_generate_2STRING(-1, cctx) == FAIL)
5432 return NULL;
5433 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5434 return NULL;
5435
5436 return p;
5437}
5438
5439/*
5440 * compile "echo expr"
5441 */
5442 static char_u *
5443compile_echo(char_u *arg, int with_white, cctx_T *cctx)
5444{
5445 char_u *p = arg;
5446 int count = 0;
5447
Bram Moolenaarad39c092020-02-26 18:23:43 +01005448 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449 {
5450 if (compile_expr1(&p, cctx) == FAIL)
5451 return NULL;
5452 ++count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005453 p = skipwhite(p);
5454 if (ends_excmd(*p))
5455 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005456 }
5457
5458 generate_ECHO(cctx, with_white, count);
Bram Moolenaarad39c092020-02-26 18:23:43 +01005459 return p;
5460}
5461
5462/*
5463 * compile "execute expr"
5464 */
5465 static char_u *
5466compile_execute(char_u *arg, cctx_T *cctx)
5467{
5468 char_u *p = arg;
5469 int count = 0;
5470
5471 for (;;)
5472 {
5473 if (compile_expr1(&p, cctx) == FAIL)
5474 return NULL;
5475 ++count;
5476 p = skipwhite(p);
5477 if (ends_excmd(*p))
5478 break;
5479 }
5480
5481 generate_EXECUTE(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005482
5483 return p;
5484}
5485
5486/*
5487 * After ex_function() has collected all the function lines: parse and compile
5488 * the lines into instructions.
5489 * Adds the function to "def_functions".
5490 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
5491 * return statement (used for lambda).
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005492 * This can be used recursively through compile_lambda(), which may reallocate
5493 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005494 */
5495 void
5496compile_def_function(ufunc_T *ufunc, int set_return_type)
5497{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005498 char_u *line = NULL;
5499 char_u *p;
5500 exarg_T ea;
5501 char *errormsg = NULL; // error message
5502 int had_return = FALSE;
5503 cctx_T cctx;
5504 garray_T *instr;
5505 int called_emsg_before = called_emsg;
5506 int ret = FAIL;
5507 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005508 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005510 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005511 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01005512
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005513 if (ufunc->uf_dfunc_idx >= 0)
5514 {
5515 // Redefining a function that was compiled before.
5516 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
5517
5518 // Free old instructions.
5519 delete_def_function_contents(dfunc);
5520 }
5521 else
5522 {
5523 // Add the function to "def_functions".
5524 if (ga_grow(&def_functions, 1) == FAIL)
5525 return;
5526 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
5527 vim_memset(dfunc, 0, sizeof(dfunc_T));
5528 dfunc->df_idx = def_functions.ga_len;
5529 ufunc->uf_dfunc_idx = dfunc->df_idx;
5530 dfunc->df_ufunc = ufunc;
5531 ++def_functions.ga_len;
5532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005533 }
5534
5535 vim_memset(&cctx, 0, sizeof(cctx));
5536 cctx.ctx_ufunc = ufunc;
5537 cctx.ctx_lnum = -1;
5538 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
5539 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
5540 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
5541 cctx.ctx_type_list = &ufunc->uf_type_list;
5542 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
5543 instr = &cctx.ctx_instr;
5544
5545 // Most modern script version.
5546 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5547
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005548 if (ufunc->uf_def_args.ga_len > 0)
5549 {
5550 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005551 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005552 int i;
5553 char_u *arg;
5554 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
5555
5556 // Produce instructions for the default values of optional arguments.
5557 // Store the instruction index in uf_def_arg_idx[] so that we know
5558 // where to start when the function is called, depending on the number
5559 // of arguments.
5560 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
5561 if (ufunc->uf_def_arg_idx == NULL)
5562 goto erret;
5563 for (i = 0; i < count; ++i)
5564 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005565 garray_T *stack = &cctx.ctx_type_stack;
5566 type_T *val_type;
5567 int arg_idx = first_def_arg + i;
5568
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005569 ufunc->uf_def_arg_idx[i] = instr->ga_len;
5570 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02005571 if (compile_expr1(&arg, &cctx) == FAIL)
5572 goto erret;
5573
5574 // If no type specified use the type of the default value.
5575 // Otherwise check that the default value type matches the
5576 // specified type.
5577 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5578 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
5579 ufunc->uf_arg_types[arg_idx] = val_type;
5580 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
5581 == FAIL)
5582 {
5583 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
5584 arg_idx + 1);
5585 goto erret;
5586 }
5587
5588 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005589 goto erret;
5590 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01005591 ufunc->uf_def_arg_idx[count] = instr->ga_len;
5592 }
5593
5594 /*
5595 * Loop over all the lines of the function and generate instructions.
5596 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005597 for (;;)
5598 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005599 int is_ex_command;
5600
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005601 // Bail out on the first error to avoid a flood of errors and report
5602 // the right line number when inside try/catch.
5603 if (emsg_before != called_emsg)
5604 goto erret;
5605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005606 if (line != NULL && *line == '|')
5607 // the line continues after a '|'
5608 ++line;
5609 else if (line != NULL && *line != NUL)
5610 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005611 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005612 goto erret;
5613 }
5614 else
5615 {
5616 do
5617 {
5618 ++cctx.ctx_lnum;
5619 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5620 break;
5621 line = ((char_u **)ufunc->uf_lines.ga_data)[cctx.ctx_lnum];
5622 } while (line == NULL);
5623 if (cctx.ctx_lnum == ufunc->uf_lines.ga_len)
5624 break;
5625 SOURCING_LNUM = ufunc->uf_script_ctx.sc_lnum + cctx.ctx_lnum + 1;
5626 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005627 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005628
5629 had_return = FALSE;
5630 vim_memset(&ea, 0, sizeof(ea));
5631 ea.cmdlinep = &line;
5632 ea.cmd = skipwhite(line);
5633
5634 // "}" ends a block scope
5635 if (*ea.cmd == '}')
5636 {
5637 scopetype_T stype = cctx.ctx_scope == NULL
5638 ? NO_SCOPE : cctx.ctx_scope->se_type;
5639
5640 if (stype == BLOCK_SCOPE)
5641 {
5642 compile_endblock(&cctx);
5643 line = ea.cmd;
5644 }
5645 else
5646 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005647 emsg(_("E1025: using } outside of a block scope"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005648 goto erret;
5649 }
5650 if (line != NULL)
5651 line = skipwhite(ea.cmd + 1);
5652 continue;
5653 }
5654
5655 // "{" starts a block scope
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01005656 // "{'a': 1}->func() is something else
5657 if (*ea.cmd == '{' && ends_excmd(*skipwhite(ea.cmd + 1)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005658 {
5659 line = compile_block(ea.cmd, &cctx);
5660 continue;
5661 }
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005662 is_ex_command = *ea.cmd == ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005663
5664 /*
5665 * COMMAND MODIFIERS
5666 */
5667 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
5668 {
5669 if (errormsg != NULL)
5670 goto erret;
5671 // empty line or comment
5672 line = (char_u *)"";
5673 continue;
5674 }
5675
5676 // Skip ":call" to get to the function name.
5677 if (checkforcmd(&ea.cmd, "call", 3))
5678 ea.cmd = skipwhite(ea.cmd);
5679
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005680 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005681 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005682 // Assuming the command starts with a variable or function name,
5683 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
5684 // val".
5685 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
5686 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005687 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02005688 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005689 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005690 int oplen;
5691 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005692
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005693 oplen = assignment_len(skipwhite(p), &heredoc);
5694 if (oplen > 0)
5695 {
5696 // Recognize an assignment if we recognize the variable
5697 // name:
5698 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01005699 // "local = expr" where "local" is a local var.
5700 // "script = expr" where "script" is a script-local var.
5701 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005702 // "&opt = expr"
5703 // "$ENV = expr"
5704 // "@r = expr"
5705 if (*ea.cmd == '&'
5706 || *ea.cmd == '$'
5707 || *ea.cmd == '@'
5708 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
5709 || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0
5710 || lookup_script(ea.cmd, p - ea.cmd) == OK
5711 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
5712 {
5713 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
5714 if (line == NULL)
5715 goto erret;
5716 continue;
5717 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005718 }
5719 }
5720 }
5721
5722 /*
5723 * COMMAND after range
5724 */
5725 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01005726 p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local,
5727 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005728
5729 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
5730 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005731 if (cctx.ctx_skip == TRUE)
5732 {
5733 line += STRLEN(line);
5734 continue;
5735 }
5736
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005737 // Expression or function call.
5738 if (ea.cmdidx == CMD_eval)
5739 {
5740 p = ea.cmd;
5741 if (compile_expr1(&p, &cctx) == FAIL)
5742 goto erret;
5743
5744 // drop the return value
5745 generate_instr_drop(&cctx, ISN_DROP, 1);
5746 line = p;
5747 continue;
5748 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02005749 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005750 iemsg("Command from find_ex_command() not handled");
5751 goto erret;
5752 }
5753
5754 p = skipwhite(p);
5755
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005756 if (cctx.ctx_skip == TRUE
5757 && ea.cmdidx != CMD_elseif
5758 && ea.cmdidx != CMD_else
5759 && ea.cmdidx != CMD_endif)
5760 {
5761 line += STRLEN(line);
5762 continue;
5763 }
5764
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005765 switch (ea.cmdidx)
5766 {
5767 case CMD_def:
5768 case CMD_function:
5769 // TODO: Nested function
5770 emsg("Nested function not implemented yet");
5771 goto erret;
5772
5773 case CMD_return:
5774 line = compile_return(p, set_return_type, &cctx);
5775 had_return = TRUE;
5776 break;
5777
5778 case CMD_let:
5779 case CMD_const:
5780 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
5781 break;
5782
5783 case CMD_import:
5784 line = compile_import(p, &cctx);
5785 break;
5786
5787 case CMD_if:
5788 line = compile_if(p, &cctx);
5789 break;
5790 case CMD_elseif:
5791 line = compile_elseif(p, &cctx);
5792 break;
5793 case CMD_else:
5794 line = compile_else(p, &cctx);
5795 break;
5796 case CMD_endif:
5797 line = compile_endif(p, &cctx);
5798 break;
5799
5800 case CMD_while:
5801 line = compile_while(p, &cctx);
5802 break;
5803 case CMD_endwhile:
5804 line = compile_endwhile(p, &cctx);
5805 break;
5806
5807 case CMD_for:
5808 line = compile_for(p, &cctx);
5809 break;
5810 case CMD_endfor:
5811 line = compile_endfor(p, &cctx);
5812 break;
5813 case CMD_continue:
5814 line = compile_continue(p, &cctx);
5815 break;
5816 case CMD_break:
5817 line = compile_break(p, &cctx);
5818 break;
5819
5820 case CMD_try:
5821 line = compile_try(p, &cctx);
5822 break;
5823 case CMD_catch:
5824 line = compile_catch(p, &cctx);
5825 break;
5826 case CMD_finally:
5827 line = compile_finally(p, &cctx);
5828 break;
5829 case CMD_endtry:
5830 line = compile_endtry(p, &cctx);
5831 break;
5832 case CMD_throw:
5833 line = compile_throw(p, &cctx);
5834 break;
5835
5836 case CMD_echo:
5837 line = compile_echo(p, TRUE, &cctx);
5838 break;
5839 case CMD_echon:
5840 line = compile_echo(p, FALSE, &cctx);
5841 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01005842 case CMD_execute:
5843 line = compile_execute(p, &cctx);
5844 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005845
5846 default:
5847 // Not recognized, execute with do_cmdline_cmd().
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005848 // TODO:
5849 // CMD_echomsg
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01005850 // etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005851 generate_EXEC(&cctx, line);
5852 line = (char_u *)"";
5853 break;
5854 }
5855 if (line == NULL)
5856 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02005857 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005858
5859 if (cctx.ctx_type_stack.ga_len < 0)
5860 {
5861 iemsg("Type stack underflow");
5862 goto erret;
5863 }
5864 }
5865
5866 if (cctx.ctx_scope != NULL)
5867 {
5868 if (cctx.ctx_scope->se_type == IF_SCOPE)
5869 emsg(_(e_endif));
5870 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
5871 emsg(_(e_endwhile));
5872 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
5873 emsg(_(e_endfor));
5874 else
5875 emsg(_("E1026: Missing }"));
5876 goto erret;
5877 }
5878
5879 if (!had_return)
5880 {
5881 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
5882 {
5883 emsg(_("E1027: Missing return statement"));
5884 goto erret;
5885 }
5886
5887 // Return zero if there is no return at the end.
5888 generate_PUSHNR(&cctx, 0);
5889 generate_instr(&cctx, ISN_RETURN);
5890 }
5891
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005892 {
5893 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5894 + ufunc->uf_dfunc_idx;
5895 dfunc->df_deleted = FALSE;
5896 dfunc->df_instr = instr->ga_data;
5897 dfunc->df_instr_count = instr->ga_len;
5898 dfunc->df_varcount = cctx.ctx_max_local;
5899 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005900
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005901 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005902 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005903 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005904
5905 // Create a type for the function, with the return type and any
5906 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005907 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
5908 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005909 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005910 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005911 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
5912 argcount, &ufunc->uf_type_list);
5913 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005914 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005915 argcount + varargs,
5916 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005917 {
5918 ret = FAIL;
5919 goto erret;
5920 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005921 ufunc->uf_func_type->tt_argcount = argcount + varargs;
5922 ufunc->uf_func_type->tt_min_argcount =
5923 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005924 if (ufunc->uf_arg_types == NULL)
5925 {
5926 int i;
5927
5928 // lambda does not have argument types.
5929 for (i = 0; i < argcount; ++i)
5930 ufunc->uf_func_type->tt_args[i] = &t_any;
5931 }
5932 else
5933 mch_memmove(ufunc->uf_func_type->tt_args,
5934 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005935 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005936 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02005937 ufunc->uf_func_type->tt_args[argcount] =
5938 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005939 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
5940 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005941 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02005942 else
5943 // No arguments, can use a predefined type.
5944 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
5945 argcount, &ufunc->uf_type_list);
5946
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005947 }
5948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005949 ret = OK;
5950
5951erret:
5952 if (ret == FAIL)
5953 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01005954 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005955 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5956 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005957
5958 for (idx = 0; idx < instr->ga_len; ++idx)
5959 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005960 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005962 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005963 if (!dfunc->df_deleted)
5964 --def_functions.ga_len;
5965
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005966 while (cctx.ctx_scope != NULL)
5967 drop_scope(&cctx);
5968
Bram Moolenaar20431c92020-03-20 18:39:46 +01005969 // Don't execute this function body.
5970 ga_clear_strings(&ufunc->uf_lines);
5971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005972 if (errormsg != NULL)
5973 emsg(errormsg);
5974 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01005975 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005976 }
5977
5978 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005979 free_imported(&cctx);
5980 free_local(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005981 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005982}
5983
5984/*
5985 * Delete an instruction, free what it contains.
5986 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01005987 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005988delete_instr(isn_T *isn)
5989{
5990 switch (isn->isn_type)
5991 {
5992 case ISN_EXEC:
5993 case ISN_LOADENV:
5994 case ISN_LOADG:
5995 case ISN_LOADOPT:
5996 case ISN_MEMBER:
5997 case ISN_PUSHEXC:
5998 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005999 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006000 case ISN_STOREG:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006001 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006002 vim_free(isn->isn_arg.string);
6003 break;
6004
6005 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006006 case ISN_STORES:
6007 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006008 break;
6009
6010 case ISN_STOREOPT:
6011 vim_free(isn->isn_arg.storeopt.so_name);
6012 break;
6013
6014 case ISN_PUSHBLOB: // push blob isn_arg.blob
6015 blob_unref(isn->isn_arg.blob);
6016 break;
6017
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006018 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006019#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006020 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006021#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006022 break;
6023
6024 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006025#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006026 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006027#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006028 break;
6029
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006030 case ISN_UCALL:
6031 vim_free(isn->isn_arg.ufunc.cuf_name);
6032 break;
6033
6034 case ISN_2BOOL:
6035 case ISN_2STRING:
6036 case ISN_ADDBLOB:
6037 case ISN_ADDLIST:
6038 case ISN_BCALL:
6039 case ISN_CATCH:
6040 case ISN_CHECKNR:
6041 case ISN_CHECKTYPE:
6042 case ISN_COMPAREANY:
6043 case ISN_COMPAREBLOB:
6044 case ISN_COMPAREBOOL:
6045 case ISN_COMPAREDICT:
6046 case ISN_COMPAREFLOAT:
6047 case ISN_COMPAREFUNC:
6048 case ISN_COMPARELIST:
6049 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006050 case ISN_COMPARESPECIAL:
6051 case ISN_COMPARESTRING:
6052 case ISN_CONCAT:
6053 case ISN_DCALL:
6054 case ISN_DROP:
6055 case ISN_ECHO:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006056 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006057 case ISN_ENDTRY:
6058 case ISN_FOR:
6059 case ISN_FUNCREF:
6060 case ISN_INDEX:
6061 case ISN_JUMP:
6062 case ISN_LOAD:
6063 case ISN_LOADSCRIPT:
6064 case ISN_LOADREG:
6065 case ISN_LOADV:
6066 case ISN_NEGATENR:
6067 case ISN_NEWDICT:
6068 case ISN_NEWLIST:
6069 case ISN_OPNR:
6070 case ISN_OPFLOAT:
6071 case ISN_OPANY:
6072 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006073 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006074 case ISN_PUSHF:
6075 case ISN_PUSHNR:
6076 case ISN_PUSHBOOL:
6077 case ISN_PUSHSPEC:
6078 case ISN_RETURN:
6079 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006080 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006081 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006082 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006083 case ISN_STORESCRIPT:
6084 case ISN_THROW:
6085 case ISN_TRY:
6086 // nothing allocated
6087 break;
6088 }
6089}
6090
6091/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006092 * Free all instructions for "dfunc".
6093 */
6094 static void
6095delete_def_function_contents(dfunc_T *dfunc)
6096{
6097 int idx;
6098
6099 ga_clear(&dfunc->df_def_args_isn);
6100
6101 if (dfunc->df_instr != NULL)
6102 {
6103 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6104 delete_instr(dfunc->df_instr + idx);
6105 VIM_CLEAR(dfunc->df_instr);
6106 }
6107
6108 dfunc->df_deleted = TRUE;
6109}
6110
6111/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006112 * When a user function is deleted, delete any associated def function.
6113 */
6114 void
6115delete_def_function(ufunc_T *ufunc)
6116{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006117 if (ufunc->uf_dfunc_idx >= 0)
6118 {
6119 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6120 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006121
Bram Moolenaar20431c92020-03-20 18:39:46 +01006122 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006123 }
6124}
6125
6126#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006127/*
6128 * Free all functions defined with ":def".
6129 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006130 void
6131free_def_functions(void)
6132{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006133 int idx;
6134
6135 for (idx = 0; idx < def_functions.ga_len; ++idx)
6136 {
6137 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6138
6139 delete_def_function_contents(dfunc);
6140 }
6141
6142 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006143}
6144#endif
6145
6146
6147#endif // FEAT_EVAL