blob: a4a71de1f37ec9d26e0210f9bf8787493ca4c337 [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;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200100 int lv_idx; // index of the variable on the stack
101 int lv_from_outer; // when TRUE using ctx_outer scope
102 int lv_const; // when TRUE cannot be assigned to
103 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104} lvar_T;
105
106/*
107 * Context for compiling lines of Vim script.
108 * Stores info about the local variables and condition stack.
109 */
110struct cctx_S {
111 ufunc_T *ctx_ufunc; // current function
112 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200113 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114 garray_T ctx_instr; // generated instructions
115
116 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200117 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100118
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200119 int ctx_closure_count; // number of closures created in the
120 // function
121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100122 garray_T ctx_imports; // imported items
123
Bram Moolenaara259d8d2020-01-31 20:10:50 +0100124 int ctx_skip; // when TRUE skip commands, when FALSE skip
125 // commands after "else"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100126 scope_T *ctx_scope; // current scope, NULL at toplevel
127
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200128 cctx_T *ctx_outer; // outer scope for lambda or nested
129 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200130 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200133 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100134};
135
136static char e_var_notfound[] = N_("E1001: variable not found: %s");
137static char e_syntax_at[] = N_("E1002: Syntax error at %s");
138
139static int compile_expr1(char_u **arg, cctx_T *cctx);
140static int compile_expr2(char_u **arg, cctx_T *cctx);
141static int compile_expr3(char_u **arg, cctx_T *cctx);
Bram Moolenaar20431c92020-03-20 18:39:46 +0100142static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200143static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
144static int check_type(type_T *expected, type_T *actual, int give_msg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145
146/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200147 * Lookup variable "name" in the local scope and return it.
148 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200150 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151lookup_local(char_u *name, size_t len, cctx_T *cctx)
152{
153 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200154 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100156 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200157 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200158
159 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100160 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
161 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200162 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163 if (STRNCMP(name, lvar->lv_name, len) == 0
164 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200165 {
166 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200167 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170
171 // Find local in outer function scope.
172 if (cctx->ctx_outer != NULL)
173 {
174 lvar = lookup_local(name, len, cctx->ctx_outer);
175 if (lvar != NULL)
176 {
177 // TODO: are there situations we should not mark the outer scope as
178 // used?
179 cctx->ctx_outer_used = TRUE;
180 lvar->lv_from_outer = TRUE;
181 return lvar;
182 }
183 }
184
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200185 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100186}
187
188/*
189 * Lookup an argument in the current function.
190 * Returns the argument index or -1 if not found.
191 */
192 static int
193lookup_arg(char_u *name, size_t len, cctx_T *cctx)
194{
195 int idx;
196
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100197 if (len == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100198 return -1;
199 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
200 {
201 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
202
203 if (STRNCMP(name, arg, len) == 0 && STRLEN(arg) == len)
204 return idx;
205 }
206 return -1;
207}
208
209/*
210 * Lookup a vararg argument in the current function.
211 * Returns TRUE if there is a match.
212 */
213 static int
214lookup_vararg(char_u *name, size_t len, cctx_T *cctx)
215{
216 char_u *va_name = cctx->ctx_ufunc->uf_va_name;
217
218 return len > 0 && va_name != NULL
219 && STRNCMP(name, va_name, len) == 0 && STRLEN(va_name) == len;
220}
221
222/*
223 * Lookup a variable in the current script.
224 * Returns OK or FAIL.
225 */
226 static int
227lookup_script(char_u *name, size_t len)
228{
229 int cc;
230 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
231 dictitem_T *di;
232
233 cc = name[len];
234 name[len] = NUL;
235 di = find_var_in_ht(ht, 0, name, TRUE);
236 name[len] = cc;
237 return di == NULL ? FAIL: OK;
238}
239
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100240/*
241 * Check if "p[len]" is already defined, either in script "import_sid" or in
242 * compilation context "cctx".
243 * Return FAIL and give an error if it defined.
244 */
245 int
246check_defined(char_u *p, int len, cctx_T *cctx)
247{
248 if (lookup_script(p, len) == OK
249 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200250 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100251 || find_imported(p, len, cctx) != NULL)))
252 {
253 semsg("E1073: imported name already defined: %s", p);
254 return FAIL;
255 }
256 return OK;
257}
258
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200259/*
260 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
261 * be freed later.
262 */
263 static type_T *
264alloc_type(garray_T *type_gap)
265{
266 type_T *type;
267
268 if (ga_grow(type_gap, 1) == FAIL)
269 return NULL;
270 type = ALLOC_CLEAR_ONE(type_T);
271 if (type != NULL)
272 {
273 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
274 ++type_gap->ga_len;
275 }
276 return type;
277}
278
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200280get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281{
282 type_T *type;
283
284 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200285 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100286 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200287 if (member_type->tt_type == VAR_VOID
288 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100289 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100290 if (member_type->tt_type == VAR_BOOL)
291 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100292 if (member_type->tt_type == VAR_NUMBER)
293 return &t_list_number;
294 if (member_type->tt_type == VAR_STRING)
295 return &t_list_string;
296
297 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200298 type = alloc_type(type_gap);
299 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100300 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100301 type->tt_type = VAR_LIST;
302 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200303 type->tt_argcount = 0;
304 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 return type;
306}
307
308 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200309get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100310{
311 type_T *type;
312
313 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200314 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100315 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200316 if (member_type->tt_type == VAR_VOID
317 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100318 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100319 if (member_type->tt_type == VAR_BOOL)
320 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100321 if (member_type->tt_type == VAR_NUMBER)
322 return &t_dict_number;
323 if (member_type->tt_type == VAR_STRING)
324 return &t_dict_string;
325
326 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200327 type = alloc_type(type_gap);
328 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100329 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100330 type->tt_type = VAR_DICT;
331 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200332 type->tt_argcount = 0;
333 type->tt_args = NULL;
334 return type;
335}
336
337/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200338 * Allocate a new type for a function.
339 */
340 static type_T *
341alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
342{
343 type_T *type = alloc_type(type_gap);
344
345 if (type == NULL)
346 return &t_any;
347 type->tt_type = VAR_FUNC;
348 type->tt_member = ret_type;
349 type->tt_argcount = argcount;
350 type->tt_args = NULL;
351 return type;
352}
353
354/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200355 * Get a function type, based on the return type "ret_type".
356 * If "argcount" is -1 or 0 a predefined type can be used.
357 * If "argcount" > 0 always create a new type, so that arguments can be added.
358 */
359 static type_T *
360get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
361{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200362 // recognize commonly used types
363 if (argcount <= 0)
364 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200365 if (ret_type == &t_unknown)
366 {
367 // (argcount == 0) is not possible
368 return &t_func_unknown;
369 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200370 if (ret_type == &t_void)
371 {
372 if (argcount == 0)
373 return &t_func_0_void;
374 else
375 return &t_func_void;
376 }
377 if (ret_type == &t_any)
378 {
379 if (argcount == 0)
380 return &t_func_0_any;
381 else
382 return &t_func_any;
383 }
384 if (ret_type == &t_number)
385 {
386 if (argcount == 0)
387 return &t_func_0_number;
388 else
389 return &t_func_number;
390 }
391 if (ret_type == &t_string)
392 {
393 if (argcount == 0)
394 return &t_func_0_string;
395 else
396 return &t_func_string;
397 }
398 }
399
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200400 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100401}
402
Bram Moolenaara8c17702020-04-01 21:17:24 +0200403/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200404 * For a function type, reserve space for "argcount" argument types (including
405 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200406 */
407 static int
408func_type_add_arg_types(
409 type_T *functype,
410 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200411 garray_T *type_gap)
412{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200413 // To make it easy to free the space needed for the argument types, add the
414 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200415 if (ga_grow(type_gap, 1) == FAIL)
416 return FAIL;
417 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
418 if (functype->tt_args == NULL)
419 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200420 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
421 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200422 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200423 return OK;
424}
425
426/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200427 * Return the type_T for a typval. Only for primitive types.
428 */
429 static type_T *
430typval2type(typval_T *tv)
431{
432 if (tv->v_type == VAR_NUMBER)
433 return &t_number;
434 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200435 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200436 if (tv->v_type == VAR_STRING)
437 return &t_string;
438 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
439 return &t_list_string;
440 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
441 return &t_dict_any;
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200442 return &t_any; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200443}
444
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200445 static void
446type_mismatch(type_T *expected, type_T *actual)
447{
448 char *tofree1, *tofree2;
449
450 semsg(_("E1013: type mismatch, expected %s but got %s"),
451 type_name(expected, &tofree1), type_name(actual, &tofree2));
452 vim_free(tofree1);
453 vim_free(tofree2);
454}
455
456 static void
457arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
458{
459 char *tofree1, *tofree2;
460
461 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
462 argidx,
463 type_name(expected, &tofree1), type_name(actual, &tofree2));
464 vim_free(tofree1);
465 vim_free(tofree2);
466}
467
468/*
469 * Check if the expected and actual types match.
470 * Does not allow for assigning "any" to a specific type.
471 */
472 static int
473check_type(type_T *expected, type_T *actual, int give_msg)
474{
475 int ret = OK;
476
477 // When expected is "unknown" we accept any actual type.
478 // When expected is "any" we accept any actual type except "void".
479 if (expected->tt_type != VAR_UNKNOWN
480 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
481
482 {
483 if (expected->tt_type != actual->tt_type)
484 {
485 if (give_msg)
486 type_mismatch(expected, actual);
487 return FAIL;
488 }
489 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
490 {
491 // "unknown" is used for an empty list or dict
492 if (actual->tt_member != &t_unknown)
493 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
494 }
495 else if (expected->tt_type == VAR_FUNC)
496 {
497 if (expected->tt_member != &t_unknown)
498 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
499 if (ret == OK && expected->tt_argcount != -1
500 && (actual->tt_argcount < expected->tt_min_argcount
501 || actual->tt_argcount > expected->tt_argcount))
502 ret = FAIL;
503 }
504 if (ret == FAIL && give_msg)
505 type_mismatch(expected, actual);
506 }
507 return ret;
508}
509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100510/////////////////////////////////////////////////////////////////////
511// Following generate_ functions expect the caller to call ga_grow().
512
Bram Moolenaar080457c2020-03-03 21:53:32 +0100513#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
514#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
515
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516/*
517 * Generate an instruction without arguments.
518 * Returns a pointer to the new instruction, NULL if failed.
519 */
520 static isn_T *
521generate_instr(cctx_T *cctx, isntype_T isn_type)
522{
523 garray_T *instr = &cctx->ctx_instr;
524 isn_T *isn;
525
Bram Moolenaar080457c2020-03-03 21:53:32 +0100526 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 if (ga_grow(instr, 1) == FAIL)
528 return NULL;
529 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
530 isn->isn_type = isn_type;
531 isn->isn_lnum = cctx->ctx_lnum + 1;
532 ++instr->ga_len;
533
534 return isn;
535}
536
537/*
538 * Generate an instruction without arguments.
539 * "drop" will be removed from the stack.
540 * Returns a pointer to the new instruction, NULL if failed.
541 */
542 static isn_T *
543generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
544{
545 garray_T *stack = &cctx->ctx_type_stack;
546
Bram Moolenaar080457c2020-03-03 21:53:32 +0100547 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548 stack->ga_len -= drop;
549 return generate_instr(cctx, isn_type);
550}
551
552/*
553 * Generate instruction "isn_type" and put "type" on the type stack.
554 */
555 static isn_T *
556generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
557{
558 isn_T *isn;
559 garray_T *stack = &cctx->ctx_type_stack;
560
561 if ((isn = generate_instr(cctx, isn_type)) == NULL)
562 return NULL;
563
564 if (ga_grow(stack, 1) == FAIL)
565 return NULL;
566 ((type_T **)stack->ga_data)[stack->ga_len] = type;
567 ++stack->ga_len;
568
569 return isn;
570}
571
572/*
573 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
574 */
575 static int
576may_generate_2STRING(int offset, cctx_T *cctx)
577{
578 isn_T *isn;
579 garray_T *stack = &cctx->ctx_type_stack;
580 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
581
582 if ((*type)->tt_type == VAR_STRING)
583 return OK;
584 *type = &t_string;
585
586 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
587 return FAIL;
588 isn->isn_arg.number = offset;
589
590 return OK;
591}
592
593 static int
594check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
595{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200596 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200598 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 {
600 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100601 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100602 else
603 semsg(_("E1036: %c requires number or float arguments"), *op);
604 return FAIL;
605 }
606 return OK;
607}
608
609/*
610 * Generate an instruction with two arguments. The instruction depends on the
611 * type of the arguments.
612 */
613 static int
614generate_two_op(cctx_T *cctx, char_u *op)
615{
616 garray_T *stack = &cctx->ctx_type_stack;
617 type_T *type1;
618 type_T *type2;
619 vartype_T vartype;
620 isn_T *isn;
621
Bram Moolenaar080457c2020-03-03 21:53:32 +0100622 RETURN_OK_IF_SKIP(cctx);
623
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 // Get the known type of the two items on the stack. If they are matching
625 // use a type-specific instruction. Otherwise fall back to runtime type
626 // checking.
627 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
628 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200629 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100630 if (type1->tt_type == type2->tt_type
631 && (type1->tt_type == VAR_NUMBER
632 || type1->tt_type == VAR_LIST
633#ifdef FEAT_FLOAT
634 || type1->tt_type == VAR_FLOAT
635#endif
636 || type1->tt_type == VAR_BLOB))
637 vartype = type1->tt_type;
638
639 switch (*op)
640 {
641 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200642 && type1->tt_type != VAR_ANY
643 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644 && check_number_or_float(
645 type1->tt_type, type2->tt_type, op) == FAIL)
646 return FAIL;
647 isn = generate_instr_drop(cctx,
648 vartype == VAR_NUMBER ? ISN_OPNR
649 : vartype == VAR_LIST ? ISN_ADDLIST
650 : vartype == VAR_BLOB ? ISN_ADDBLOB
651#ifdef FEAT_FLOAT
652 : vartype == VAR_FLOAT ? ISN_OPFLOAT
653#endif
654 : ISN_OPANY, 1);
655 if (isn != NULL)
656 isn->isn_arg.op.op_type = EXPR_ADD;
657 break;
658
659 case '-':
660 case '*':
661 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
662 op) == FAIL)
663 return FAIL;
664 if (vartype == VAR_NUMBER)
665 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
666#ifdef FEAT_FLOAT
667 else if (vartype == VAR_FLOAT)
668 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
669#endif
670 else
671 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
672 if (isn != NULL)
673 isn->isn_arg.op.op_type = *op == '*'
674 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
675 break;
676
Bram Moolenaar4c683752020-04-05 21:38:23 +0200677 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100678 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200679 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100680 && type2->tt_type != VAR_NUMBER))
681 {
682 emsg(_("E1035: % requires number arguments"));
683 return FAIL;
684 }
685 isn = generate_instr_drop(cctx,
686 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
687 if (isn != NULL)
688 isn->isn_arg.op.op_type = EXPR_REM;
689 break;
690 }
691
692 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200693 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 {
695 type_T *type = &t_any;
696
697#ifdef FEAT_FLOAT
698 // float+number and number+float results in float
699 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
700 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
701 type = &t_float;
702#endif
703 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
704 }
705
706 return OK;
707}
708
709/*
710 * Generate an ISN_COMPARE* instruction with a boolean result.
711 */
712 static int
713generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
714{
715 isntype_T isntype = ISN_DROP;
716 isn_T *isn;
717 garray_T *stack = &cctx->ctx_type_stack;
718 vartype_T type1;
719 vartype_T type2;
720
Bram Moolenaar080457c2020-03-03 21:53:32 +0100721 RETURN_OK_IF_SKIP(cctx);
722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723 // Get the known type of the two items on the stack. If they are matching
724 // use a type-specific instruction. Otherwise fall back to runtime type
725 // checking.
726 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
727 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200728 if (type1 == VAR_UNKNOWN)
729 type1 = VAR_ANY;
730 if (type2 == VAR_UNKNOWN)
731 type2 = VAR_ANY;
732
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733 if (type1 == type2)
734 {
735 switch (type1)
736 {
737 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
738 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
739 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
740 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
741 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
742 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
743 case VAR_LIST: isntype = ISN_COMPARELIST; break;
744 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
745 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100746 default: isntype = ISN_COMPAREANY; break;
747 }
748 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200749 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
751 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
752 isntype = ISN_COMPAREANY;
753
754 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
755 && (isntype == ISN_COMPAREBOOL
756 || isntype == ISN_COMPARESPECIAL
757 || isntype == ISN_COMPARENR
758 || isntype == ISN_COMPAREFLOAT))
759 {
760 semsg(_("E1037: Cannot use \"%s\" with %s"),
761 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
762 return FAIL;
763 }
764 if (isntype == ISN_DROP
765 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
766 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
767 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
768 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
769 && exptype != EXPR_IS && exptype != EXPR_ISNOT
770 && (type1 == VAR_BLOB || type2 == VAR_BLOB
771 || type1 == VAR_LIST || type2 == VAR_LIST))))
772 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100773 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 vartype_name(type1), vartype_name(type2));
775 return FAIL;
776 }
777
778 if ((isn = generate_instr(cctx, isntype)) == NULL)
779 return FAIL;
780 isn->isn_arg.op.op_type = exptype;
781 isn->isn_arg.op.op_ic = ic;
782
783 // takes two arguments, puts one bool back
784 if (stack->ga_len >= 2)
785 {
786 --stack->ga_len;
787 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
788 }
789
790 return OK;
791}
792
793/*
794 * Generate an ISN_2BOOL instruction.
795 */
796 static int
797generate_2BOOL(cctx_T *cctx, int invert)
798{
799 isn_T *isn;
800 garray_T *stack = &cctx->ctx_type_stack;
801
Bram Moolenaar080457c2020-03-03 21:53:32 +0100802 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
804 return FAIL;
805 isn->isn_arg.number = invert;
806
807 // type becomes bool
808 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
809
810 return OK;
811}
812
813 static int
814generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
815{
816 isn_T *isn;
817 garray_T *stack = &cctx->ctx_type_stack;
818
Bram Moolenaar080457c2020-03-03 21:53:32 +0100819 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
821 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200822 // TODO: whole type, e.g. for a function also arg and return types
823 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824 isn->isn_arg.type.ct_off = offset;
825
826 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200827 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100828
829 return OK;
830}
831
832/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200833 * Check that
834 * - "actual" is "expected" type or
835 * - "actual" is a type that can be "expected" type: add a runtime check; or
836 * - return FAIL.
837 */
838 static int
839need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
840{
841 if (check_type(expected, actual, FALSE) == OK)
842 return OK;
843 if (actual->tt_type != VAR_ANY
844 && actual->tt_type != VAR_UNKNOWN
845 && !(actual->tt_type == VAR_FUNC
846 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
847 {
848 type_mismatch(expected, actual);
849 return FAIL;
850 }
851 generate_TYPECHECK(cctx, expected, offset);
852 return OK;
853}
854
855/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856 * Generate an ISN_PUSHNR instruction.
857 */
858 static int
859generate_PUSHNR(cctx_T *cctx, varnumber_T number)
860{
861 isn_T *isn;
862
Bram Moolenaar080457c2020-03-03 21:53:32 +0100863 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
865 return FAIL;
866 isn->isn_arg.number = number;
867
868 return OK;
869}
870
871/*
872 * Generate an ISN_PUSHBOOL instruction.
873 */
874 static int
875generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
876{
877 isn_T *isn;
878
Bram Moolenaar080457c2020-03-03 21:53:32 +0100879 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
881 return FAIL;
882 isn->isn_arg.number = number;
883
884 return OK;
885}
886
887/*
888 * Generate an ISN_PUSHSPEC instruction.
889 */
890 static int
891generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
892{
893 isn_T *isn;
894
Bram Moolenaar080457c2020-03-03 21:53:32 +0100895 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
897 return FAIL;
898 isn->isn_arg.number = number;
899
900 return OK;
901}
902
903#ifdef FEAT_FLOAT
904/*
905 * Generate an ISN_PUSHF instruction.
906 */
907 static int
908generate_PUSHF(cctx_T *cctx, float_T fnumber)
909{
910 isn_T *isn;
911
Bram Moolenaar080457c2020-03-03 21:53:32 +0100912 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100913 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
914 return FAIL;
915 isn->isn_arg.fnumber = fnumber;
916
917 return OK;
918}
919#endif
920
921/*
922 * Generate an ISN_PUSHS instruction.
923 * Consumes "str".
924 */
925 static int
926generate_PUSHS(cctx_T *cctx, char_u *str)
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_type(cctx, ISN_PUSHS, &t_string)) == NULL)
932 return FAIL;
933 isn->isn_arg.string = str;
934
935 return OK;
936}
937
938/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100939 * Generate an ISN_PUSHCHANNEL instruction.
940 * Consumes "channel".
941 */
942 static int
943generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
944{
945 isn_T *isn;
946
Bram Moolenaar080457c2020-03-03 21:53:32 +0100947 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100948 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
949 return FAIL;
950 isn->isn_arg.channel = channel;
951
952 return OK;
953}
954
955/*
956 * Generate an ISN_PUSHJOB instruction.
957 * Consumes "job".
958 */
959 static int
960generate_PUSHJOB(cctx_T *cctx, job_T *job)
961{
962 isn_T *isn;
963
Bram Moolenaar080457c2020-03-03 21:53:32 +0100964 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100965 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100966 return FAIL;
967 isn->isn_arg.job = job;
968
969 return OK;
970}
971
972/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973 * Generate an ISN_PUSHBLOB instruction.
974 * Consumes "blob".
975 */
976 static int
977generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
978{
979 isn_T *isn;
980
Bram Moolenaar080457c2020-03-03 21:53:32 +0100981 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
983 return FAIL;
984 isn->isn_arg.blob = blob;
985
986 return OK;
987}
988
989/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100990 * Generate an ISN_PUSHFUNC instruction with name "name".
991 * Consumes "name".
992 */
993 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200994generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100995{
996 isn_T *isn;
997
Bram Moolenaar080457c2020-03-03 21:53:32 +0100998 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200999 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001000 return FAIL;
1001 isn->isn_arg.string = name;
1002
1003 return OK;
1004}
1005
1006/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 * Generate an ISN_STORE instruction.
1008 */
1009 static int
1010generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1011{
1012 isn_T *isn;
1013
Bram Moolenaar080457c2020-03-03 21:53:32 +01001014 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001015 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1016 return FAIL;
1017 if (name != NULL)
1018 isn->isn_arg.string = vim_strsave(name);
1019 else
1020 isn->isn_arg.number = idx;
1021
1022 return OK;
1023}
1024
1025/*
1026 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1027 */
1028 static int
1029generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1030{
1031 isn_T *isn;
1032
Bram Moolenaar080457c2020-03-03 21:53:32 +01001033 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001034 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1035 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001036 isn->isn_arg.storenr.stnr_idx = idx;
1037 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038
1039 return OK;
1040}
1041
1042/*
1043 * Generate an ISN_STOREOPT instruction
1044 */
1045 static int
1046generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1047{
1048 isn_T *isn;
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_STOREOPT)) == NULL)
1052 return FAIL;
1053 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1054 isn->isn_arg.storeopt.so_flags = opt_flags;
1055
1056 return OK;
1057}
1058
1059/*
1060 * Generate an ISN_LOAD or similar instruction.
1061 */
1062 static int
1063generate_LOAD(
1064 cctx_T *cctx,
1065 isntype_T isn_type,
1066 int idx,
1067 char_u *name,
1068 type_T *type)
1069{
1070 isn_T *isn;
1071
Bram Moolenaar080457c2020-03-03 21:53:32 +01001072 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1074 return FAIL;
1075 if (name != NULL)
1076 isn->isn_arg.string = vim_strsave(name);
1077 else
1078 isn->isn_arg.number = idx;
1079
1080 return OK;
1081}
1082
1083/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001084 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001085 */
1086 static int
1087generate_LOADV(
1088 cctx_T *cctx,
1089 char_u *name,
1090 int error)
1091{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001092 int di_flags;
1093 int vidx = find_vim_var(name, &di_flags);
1094 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001095
Bram Moolenaar080457c2020-03-03 21:53:32 +01001096 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001097 if (vidx < 0)
1098 {
1099 if (error)
1100 semsg(_(e_var_notfound), name);
1101 return FAIL;
1102 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001103 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001104
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001105 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001106}
1107
1108/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001109 * Generate an ISN_UNLET instruction.
1110 */
1111 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001112generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001113{
1114 isn_T *isn;
1115
1116 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001117 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001118 return FAIL;
1119 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1120 isn->isn_arg.unlet.ul_forceit = forceit;
1121
1122 return OK;
1123}
1124
1125/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 * Generate an ISN_LOADS instruction.
1127 */
1128 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001129generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001131 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001133 int sid,
1134 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135{
1136 isn_T *isn;
1137
Bram Moolenaar080457c2020-03-03 21:53:32 +01001138 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001139 if (isn_type == ISN_LOADS)
1140 isn = generate_instr_type(cctx, isn_type, type);
1141 else
1142 isn = generate_instr_drop(cctx, isn_type, 1);
1143 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001145 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1146 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001147
1148 return OK;
1149}
1150
1151/*
1152 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1153 */
1154 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001155generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156 cctx_T *cctx,
1157 isntype_T isn_type,
1158 int sid,
1159 int idx,
1160 type_T *type)
1161{
1162 isn_T *isn;
1163
Bram Moolenaar080457c2020-03-03 21:53:32 +01001164 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165 if (isn_type == ISN_LOADSCRIPT)
1166 isn = generate_instr_type(cctx, isn_type, type);
1167 else
1168 isn = generate_instr_drop(cctx, isn_type, 1);
1169 if (isn == NULL)
1170 return FAIL;
1171 isn->isn_arg.script.script_sid = sid;
1172 isn->isn_arg.script.script_idx = idx;
1173 return OK;
1174}
1175
1176/*
1177 * Generate an ISN_NEWLIST instruction.
1178 */
1179 static int
1180generate_NEWLIST(cctx_T *cctx, int count)
1181{
1182 isn_T *isn;
1183 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184 type_T *type;
1185 type_T *member;
1186
Bram Moolenaar080457c2020-03-03 21:53:32 +01001187 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1189 return FAIL;
1190 isn->isn_arg.number = count;
1191
1192 // drop the value types
1193 stack->ga_len -= count;
1194
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001195 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001196 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197 if (count > 0)
1198 member = ((type_T **)stack->ga_data)[stack->ga_len];
1199 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001200 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001201 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202
1203 // add the list type to the type stack
1204 if (ga_grow(stack, 1) == FAIL)
1205 return FAIL;
1206 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1207 ++stack->ga_len;
1208
1209 return OK;
1210}
1211
1212/*
1213 * Generate an ISN_NEWDICT instruction.
1214 */
1215 static int
1216generate_NEWDICT(cctx_T *cctx, int count)
1217{
1218 isn_T *isn;
1219 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 type_T *type;
1221 type_T *member;
1222
Bram Moolenaar080457c2020-03-03 21:53:32 +01001223 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1225 return FAIL;
1226 isn->isn_arg.number = count;
1227
1228 // drop the key and value types
1229 stack->ga_len -= 2 * count;
1230
Bram Moolenaar436472f2020-02-20 22:54:43 +01001231 // Use the first value type for the list member type. Use "void" for an
1232 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233 if (count > 0)
1234 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1235 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001236 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001237 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238
1239 // add the dict type to the type stack
1240 if (ga_grow(stack, 1) == FAIL)
1241 return FAIL;
1242 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1243 ++stack->ga_len;
1244
1245 return OK;
1246}
1247
1248/*
1249 * Generate an ISN_FUNCREF instruction.
1250 */
1251 static int
1252generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1253{
1254 isn_T *isn;
1255 garray_T *stack = &cctx->ctx_type_stack;
1256
Bram Moolenaar080457c2020-03-03 21:53:32 +01001257 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1259 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001260 isn->isn_arg.funcref.fr_func = dfunc_idx;
1261 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262
1263 if (ga_grow(stack, 1) == FAIL)
1264 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001265 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266 // TODO: argument and return types
1267 ++stack->ga_len;
1268
1269 return OK;
1270}
1271
1272/*
1273 * Generate an ISN_JUMP instruction.
1274 */
1275 static int
1276generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1277{
1278 isn_T *isn;
1279 garray_T *stack = &cctx->ctx_type_stack;
1280
Bram Moolenaar080457c2020-03-03 21:53:32 +01001281 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1283 return FAIL;
1284 isn->isn_arg.jump.jump_when = when;
1285 isn->isn_arg.jump.jump_where = where;
1286
1287 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1288 --stack->ga_len;
1289
1290 return OK;
1291}
1292
1293 static int
1294generate_FOR(cctx_T *cctx, int loop_idx)
1295{
1296 isn_T *isn;
1297 garray_T *stack = &cctx->ctx_type_stack;
1298
Bram Moolenaar080457c2020-03-03 21:53:32 +01001299 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1301 return FAIL;
1302 isn->isn_arg.forloop.for_idx = loop_idx;
1303
1304 if (ga_grow(stack, 1) == FAIL)
1305 return FAIL;
1306 // type doesn't matter, will be stored next
1307 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1308 ++stack->ga_len;
1309
1310 return OK;
1311}
1312
1313/*
1314 * Generate an ISN_BCALL instruction.
1315 * Return FAIL if the number of arguments is wrong.
1316 */
1317 static int
1318generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1319{
1320 isn_T *isn;
1321 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001322 type_T *argtypes[MAX_FUNC_ARGS];
1323 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324
Bram Moolenaar080457c2020-03-03 21:53:32 +01001325 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326 if (check_internal_func(func_idx, argcount) == FAIL)
1327 return FAIL;
1328
1329 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1330 return FAIL;
1331 isn->isn_arg.bfunc.cbf_idx = func_idx;
1332 isn->isn_arg.bfunc.cbf_argcount = argcount;
1333
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001334 for (i = 0; i < argcount; ++i)
1335 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1336
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 stack->ga_len -= argcount; // drop the arguments
1338 if (ga_grow(stack, 1) == FAIL)
1339 return FAIL;
1340 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001341 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 ++stack->ga_len; // add return value
1343
1344 return OK;
1345}
1346
1347/*
1348 * Generate an ISN_DCALL or ISN_UCALL instruction.
1349 * Return FAIL if the number of arguments is wrong.
1350 */
1351 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001352generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001353{
1354 isn_T *isn;
1355 garray_T *stack = &cctx->ctx_type_stack;
1356 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001357 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358
Bram Moolenaar080457c2020-03-03 21:53:32 +01001359 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001360 if (argcount > regular_args && !has_varargs(ufunc))
1361 {
1362 semsg(_(e_toomanyarg), ufunc->uf_name);
1363 return FAIL;
1364 }
1365 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1366 {
1367 semsg(_(e_toofewarg), ufunc->uf_name);
1368 return FAIL;
1369 }
1370
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001371 if (ufunc->uf_dfunc_idx >= 0)
1372 {
1373 int i;
1374
1375 for (i = 0; i < argcount; ++i)
1376 {
1377 type_T *expected;
1378 type_T *actual;
1379
1380 if (i < regular_args)
1381 {
1382 if (ufunc->uf_arg_types == NULL)
1383 continue;
1384 expected = ufunc->uf_arg_types[i];
1385 }
1386 else
1387 expected = ufunc->uf_va_type->tt_member;
1388 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001389 if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001390 {
1391 arg_type_mismatch(expected, actual, i + 1);
1392 return FAIL;
1393 }
1394 }
1395 }
1396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 if ((isn = generate_instr(cctx,
1398 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1399 return FAIL;
1400 if (ufunc->uf_dfunc_idx >= 0)
1401 {
1402 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1403 isn->isn_arg.dfunc.cdf_argcount = argcount;
1404 }
1405 else
1406 {
1407 // A user function may be deleted and redefined later, can't use the
1408 // ufunc pointer, need to look it up again at runtime.
1409 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1410 isn->isn_arg.ufunc.cuf_argcount = argcount;
1411 }
1412
1413 stack->ga_len -= argcount; // drop the arguments
1414 if (ga_grow(stack, 1) == FAIL)
1415 return FAIL;
1416 // add return value
1417 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1418 ++stack->ga_len;
1419
1420 return OK;
1421}
1422
1423/*
1424 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1425 */
1426 static int
1427generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1428{
1429 isn_T *isn;
1430 garray_T *stack = &cctx->ctx_type_stack;
1431
Bram Moolenaar080457c2020-03-03 21:53:32 +01001432 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1434 return FAIL;
1435 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1436 isn->isn_arg.ufunc.cuf_argcount = argcount;
1437
1438 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001439 if (ga_grow(stack, 1) == FAIL)
1440 return FAIL;
1441 // add return value
1442 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1443 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444
1445 return OK;
1446}
1447
1448/*
1449 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001450 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 */
1452 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001453generate_PCALL(
1454 cctx_T *cctx,
1455 int argcount,
1456 char_u *name,
1457 type_T *type,
1458 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459{
1460 isn_T *isn;
1461 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001462 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463
Bram Moolenaar080457c2020-03-03 21:53:32 +01001464 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001465
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001466 if (type->tt_type == VAR_ANY)
1467 ret_type = &t_any;
1468 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1469 ret_type = type->tt_member;
1470 else
1471 {
1472 semsg(_("E1085: Not a callable type: %s"), name);
1473 return FAIL;
1474 }
1475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1477 return FAIL;
1478 isn->isn_arg.pfunc.cpf_top = at_top;
1479 isn->isn_arg.pfunc.cpf_argcount = argcount;
1480
1481 stack->ga_len -= argcount; // drop the arguments
1482
1483 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001484 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001486 // If partial is above the arguments it must be cleared and replaced with
1487 // the return value.
1488 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1489 return FAIL;
1490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 return OK;
1492}
1493
1494/*
1495 * Generate an ISN_MEMBER instruction.
1496 */
1497 static int
1498generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1499{
1500 isn_T *isn;
1501 garray_T *stack = &cctx->ctx_type_stack;
1502 type_T *type;
1503
Bram Moolenaar080457c2020-03-03 21:53:32 +01001504 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1506 return FAIL;
1507 isn->isn_arg.string = vim_strnsave(name, (int)len);
1508
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001509 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001511 if (type->tt_type != VAR_DICT && type != &t_any)
1512 {
1513 emsg(_(e_dictreq));
1514 return FAIL;
1515 }
1516 // change dict type to dict member type
1517 if (type->tt_type == VAR_DICT)
1518 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519
1520 return OK;
1521}
1522
1523/*
1524 * Generate an ISN_ECHO instruction.
1525 */
1526 static int
1527generate_ECHO(cctx_T *cctx, int with_white, int count)
1528{
1529 isn_T *isn;
1530
Bram Moolenaar080457c2020-03-03 21:53:32 +01001531 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1533 return FAIL;
1534 isn->isn_arg.echo.echo_with_white = with_white;
1535 isn->isn_arg.echo.echo_count = count;
1536
1537 return OK;
1538}
1539
Bram Moolenaarad39c092020-02-26 18:23:43 +01001540/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001541 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001542 */
1543 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001544generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001545{
1546 isn_T *isn;
1547
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001548 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001549 return FAIL;
1550 isn->isn_arg.number = count;
1551
1552 return OK;
1553}
1554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 static int
1556generate_EXEC(cctx_T *cctx, char_u *line)
1557{
1558 isn_T *isn;
1559
Bram Moolenaar080457c2020-03-03 21:53:32 +01001560 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1562 return FAIL;
1563 isn->isn_arg.string = vim_strsave(line);
1564 return OK;
1565}
1566
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001567 static int
1568generate_EXECCONCAT(cctx_T *cctx, int count)
1569{
1570 isn_T *isn;
1571
1572 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1573 return FAIL;
1574 isn->isn_arg.number = count;
1575 return OK;
1576}
1577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578/*
1579 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001580 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001582 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1584{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 lvar_T *lvar;
1586
1587 if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx))
1588 {
1589 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001590 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 }
1592
1593 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001594 return NULL;
1595 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001597 // Every local variable uses the next entry on the stack. We could re-use
1598 // the last ones when leaving a scope, but then variables used in a closure
1599 // might get overwritten. To keep things simple do not re-use stack
1600 // entries. This is less efficient, but memory is cheap these days.
1601 lvar->lv_idx = cctx->ctx_locals_count++;
1602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1604 lvar->lv_const = isConst;
1605 lvar->lv_type = type;
1606
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001607 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608}
1609
1610/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001611 * Remove local variables above "new_top".
1612 */
1613 static void
1614unwind_locals(cctx_T *cctx, int new_top)
1615{
1616 if (cctx->ctx_locals.ga_len > new_top)
1617 {
1618 int idx;
1619 lvar_T *lvar;
1620
1621 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1622 {
1623 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1624 vim_free(lvar->lv_name);
1625 }
1626 }
1627 cctx->ctx_locals.ga_len = new_top;
1628}
1629
1630/*
1631 * Free all local variables.
1632 */
1633 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001634free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001635{
1636 unwind_locals(cctx, 0);
1637 ga_clear(&cctx->ctx_locals);
1638}
1639
1640/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 * Skip over a type definition and return a pointer to just after it.
1642 */
1643 char_u *
1644skip_type(char_u *start)
1645{
1646 char_u *p = start;
1647
1648 while (ASCII_ISALNUM(*p) || *p == '_')
1649 ++p;
1650
1651 // Skip over "<type>"; this is permissive about white space.
1652 if (*skipwhite(p) == '<')
1653 {
1654 p = skipwhite(p);
1655 p = skip_type(skipwhite(p + 1));
1656 p = skipwhite(p);
1657 if (*p == '>')
1658 ++p;
1659 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001660 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1661 {
1662 // handle func(args): type
1663 ++p;
1664 while (*p != ')' && *p != NUL)
1665 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001666 char_u *sp = p;
1667
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001668 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001669 if (p == sp)
1670 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001671 if (*p == ',')
1672 p = skipwhite(p + 1);
1673 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001674 if (*p == ')')
1675 {
1676 if (p[1] == ':')
1677 p = skip_type(skipwhite(p + 2));
1678 else
1679 p = skipwhite(p + 1);
1680 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001681 }
1682
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683 return p;
1684}
1685
1686/*
1687 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001688 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 * Returns NULL in case of failure.
1690 */
1691 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001692parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693{
1694 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001695 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001696
1697 if (**arg != '<')
1698 {
1699 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001700 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 else
1702 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001703 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 }
1705 *arg = skipwhite(*arg + 1);
1706
Bram Moolenaard77a8522020-04-03 21:59:57 +02001707 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708
1709 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001710 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 {
1712 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001713 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 }
1715 ++*arg;
1716
1717 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001718 return get_list_type(member_type, type_gap);
1719 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720}
1721
1722/*
1723 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001724 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 */
1726 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001727parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728{
1729 char_u *p = *arg;
1730 size_t len;
1731
1732 // skip over the first word
1733 while (ASCII_ISALNUM(*p) || *p == '_')
1734 ++p;
1735 len = p - *arg;
1736
1737 switch (**arg)
1738 {
1739 case 'a':
1740 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1741 {
1742 *arg += len;
1743 return &t_any;
1744 }
1745 break;
1746 case 'b':
1747 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1748 {
1749 *arg += len;
1750 return &t_bool;
1751 }
1752 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1753 {
1754 *arg += len;
1755 return &t_blob;
1756 }
1757 break;
1758 case 'c':
1759 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1760 {
1761 *arg += len;
1762 return &t_channel;
1763 }
1764 break;
1765 case 'd':
1766 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1767 {
1768 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001769 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 }
1771 break;
1772 case 'f':
1773 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1774 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001775#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 *arg += len;
1777 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001778#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001779 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001780 return &t_any;
1781#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 }
1783 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1784 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001785 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001786 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001787 int argcount = -1;
1788 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001789 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001790 type_T *arg_type[MAX_FUNC_ARGS + 1];
1791
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001792 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001794 if (**arg == '(')
1795 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001796 // "func" may or may not return a value, "func()" does
1797 // not return a value.
1798 ret_type = &t_void;
1799
Bram Moolenaard77a8522020-04-03 21:59:57 +02001800 p = ++*arg;
1801 argcount = 0;
1802 while (*p != NUL && *p != ')')
1803 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001804 if (*p == '?')
1805 {
1806 if (first_optional == -1)
1807 first_optional = argcount;
1808 ++p;
1809 }
1810 else if (first_optional != -1)
1811 {
1812 emsg(_("E1007: mandatory argument after optional argument"));
1813 return &t_any;
1814 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001815 else if (STRNCMP(p, "...", 3) == 0)
1816 {
1817 flags |= TTFLAG_VARARGS;
1818 p += 3;
1819 }
1820
1821 arg_type[argcount++] = parse_type(&p, type_gap);
1822
1823 // Nothing comes after "...{type}".
1824 if (flags & TTFLAG_VARARGS)
1825 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001826
Bram Moolenaard77a8522020-04-03 21:59:57 +02001827 if (*p != ',' && *skipwhite(p) == ',')
1828 {
1829 semsg(_(e_no_white_before), ",");
1830 return &t_any;
1831 }
1832 if (*p == ',')
1833 {
1834 ++p;
1835 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001836 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001837 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001838 return &t_any;
1839 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001840 }
1841 p = skipwhite(p);
1842 if (argcount == MAX_FUNC_ARGS)
1843 {
1844 emsg(_("E740: Too many argument types"));
1845 return &t_any;
1846 }
1847 }
1848
1849 p = skipwhite(p);
1850 if (*p != ')')
1851 {
1852 emsg(_(e_missing_close));
1853 return &t_any;
1854 }
1855 *arg = p + 1;
1856 }
1857 if (**arg == ':')
1858 {
1859 // parse return type
1860 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001861 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001862 semsg(_(e_white_after), ":");
1863 *arg = skipwhite(*arg);
1864 ret_type = parse_type(arg, type_gap);
1865 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001866 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001867 type = get_func_type(ret_type, argcount, type_gap);
1868 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001869 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001870 type = alloc_func_type(ret_type, argcount, type_gap);
1871 type->tt_flags = flags;
1872 if (argcount > 0)
1873 {
1874 type->tt_argcount = argcount;
1875 type->tt_min_argcount = first_optional == -1
1876 ? argcount : first_optional;
1877 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001878 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001879 return &t_any;
1880 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001881 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001882 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001883 }
1884 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001885 }
1886 break;
1887 case 'j':
1888 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1889 {
1890 *arg += len;
1891 return &t_job;
1892 }
1893 break;
1894 case 'l':
1895 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1896 {
1897 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001898 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 }
1900 break;
1901 case 'n':
1902 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1903 {
1904 *arg += len;
1905 return &t_number;
1906 }
1907 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908 case 's':
1909 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1910 {
1911 *arg += len;
1912 return &t_string;
1913 }
1914 break;
1915 case 'v':
1916 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1917 {
1918 *arg += len;
1919 return &t_void;
1920 }
1921 break;
1922 }
1923
1924 semsg(_("E1010: Type not recognized: %s"), *arg);
1925 return &t_any;
1926}
1927
1928/*
1929 * Check if "type1" and "type2" are exactly the same.
1930 */
1931 static int
1932equal_type(type_T *type1, type_T *type2)
1933{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001934 int i;
1935
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001936 if (type1->tt_type != type2->tt_type)
1937 return FALSE;
1938 switch (type1->tt_type)
1939 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001941 case VAR_ANY:
1942 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 case VAR_SPECIAL:
1944 case VAR_BOOL:
1945 case VAR_NUMBER:
1946 case VAR_FLOAT:
1947 case VAR_STRING:
1948 case VAR_BLOB:
1949 case VAR_JOB:
1950 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001951 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 case VAR_LIST:
1953 case VAR_DICT:
1954 return equal_type(type1->tt_member, type2->tt_member);
1955 case VAR_FUNC:
1956 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001957 if (!equal_type(type1->tt_member, type2->tt_member)
1958 || type1->tt_argcount != type2->tt_argcount)
1959 return FALSE;
1960 if (type1->tt_argcount < 0
1961 || type1->tt_args == NULL || type2->tt_args == NULL)
1962 return TRUE;
1963 for (i = 0; i < type1->tt_argcount; ++i)
1964 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
1965 return FALSE;
1966 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 }
1968 return TRUE;
1969}
1970
1971/*
1972 * Find the common type of "type1" and "type2" and put it in "dest".
1973 * "type2" and "dest" may be the same.
1974 */
1975 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02001976common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977{
1978 if (equal_type(type1, type2))
1979 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001980 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 return;
1982 }
1983
1984 if (type1->tt_type == type2->tt_type)
1985 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1987 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001988 type_T *common;
1989
Bram Moolenaard77a8522020-04-03 21:59:57 +02001990 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001991 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001992 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001993 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001994 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 return;
1996 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001997 if (type1->tt_type == VAR_FUNC)
1998 {
1999 type_T *common;
2000
2001 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2002 if (type1->tt_argcount == type2->tt_argcount
2003 && type1->tt_argcount >= 0)
2004 {
2005 int argcount = type1->tt_argcount;
2006 int i;
2007
2008 *dest = alloc_func_type(common, argcount, type_gap);
2009 if (type1->tt_args != NULL && type2->tt_args != NULL)
2010 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002011 if (func_type_add_arg_types(*dest, argcount,
2012 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002013 for (i = 0; i < argcount; ++i)
2014 common_type(type1->tt_args[i], type2->tt_args[i],
2015 &(*dest)->tt_args[i], type_gap);
2016 }
2017 }
2018 else
2019 *dest = alloc_func_type(common, -1, type_gap);
2020 return;
2021 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 }
2023
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002024 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025}
2026
2027 char *
2028vartype_name(vartype_T type)
2029{
2030 switch (type)
2031 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002032 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002033 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 case VAR_SPECIAL: return "special";
2036 case VAR_BOOL: return "bool";
2037 case VAR_NUMBER: return "number";
2038 case VAR_FLOAT: return "float";
2039 case VAR_STRING: return "string";
2040 case VAR_BLOB: return "blob";
2041 case VAR_JOB: return "job";
2042 case VAR_CHANNEL: return "channel";
2043 case VAR_LIST: return "list";
2044 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002045
2046 case VAR_FUNC:
2047 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002049 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050}
2051
2052/*
2053 * Return the name of a type.
2054 * The result may be in allocated memory, in which case "tofree" is set.
2055 */
2056 char *
2057type_name(type_T *type, char **tofree)
2058{
2059 char *name = vartype_name(type->tt_type);
2060
2061 *tofree = NULL;
2062 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2063 {
2064 char *member_free;
2065 char *member_name = type_name(type->tt_member, &member_free);
2066 size_t len;
2067
2068 len = STRLEN(name) + STRLEN(member_name) + 3;
2069 *tofree = alloc(len);
2070 if (*tofree != NULL)
2071 {
2072 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2073 vim_free(member_free);
2074 return *tofree;
2075 }
2076 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002077 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002078 {
2079 garray_T ga;
2080 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002081 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002082
2083 ga_init2(&ga, 1, 100);
2084 if (ga_grow(&ga, 20) == FAIL)
2085 return "[unknown]";
2086 *tofree = ga.ga_data;
2087 STRCPY(ga.ga_data, "func(");
2088 ga.ga_len += 5;
2089
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002090 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002091 {
2092 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002093 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002094 int len;
2095
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002096 if (type->tt_args == NULL)
2097 arg_type = "[unknown]";
2098 else
2099 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002100 if (i > 0)
2101 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002102 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002103 ga.ga_len += 2;
2104 }
2105 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002106 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002107 {
2108 vim_free(arg_free);
2109 return "[unknown]";
2110 }
2111 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002112 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002113 {
2114 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2115 ga.ga_len += 3;
2116 }
2117 else if (i >= type->tt_min_argcount)
2118 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002119 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002120 ga.ga_len += len;
2121 vim_free(arg_free);
2122 }
2123
2124 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002125 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002126 else
2127 {
2128 char *ret_free;
2129 char *ret_name = type_name(type->tt_member, &ret_free);
2130 int len;
2131
2132 len = (int)STRLEN(ret_name) + 4;
2133 if (ga_grow(&ga, len) == FAIL)
2134 {
2135 vim_free(ret_free);
2136 return "[unknown]";
2137 }
2138 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002139 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2140 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002141 vim_free(ret_free);
2142 }
2143 return ga.ga_data;
2144 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002145
2146 return name;
2147}
2148
2149/*
2150 * Find "name" in script-local items of script "sid".
2151 * Returns the index in "sn_var_vals" if found.
2152 * If found but not in "sn_var_vals" returns -1.
2153 * If not found returns -2.
2154 */
2155 int
2156get_script_item_idx(int sid, char_u *name, int check_writable)
2157{
2158 hashtab_T *ht;
2159 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002160 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 int idx;
2162
2163 // First look the name up in the hashtable.
2164 if (sid <= 0 || sid > script_items.ga_len)
2165 return -1;
2166 ht = &SCRIPT_VARS(sid);
2167 di = find_var_in_ht(ht, 0, name, TRUE);
2168 if (di == NULL)
2169 return -2;
2170
2171 // Now find the svar_T index in sn_var_vals.
2172 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2173 {
2174 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2175
2176 if (sv->sv_tv == &di->di_tv)
2177 {
2178 if (check_writable && sv->sv_const)
2179 semsg(_(e_readonlyvar), name);
2180 return idx;
2181 }
2182 }
2183 return -1;
2184}
2185
2186/*
2187 * Find "name" in imported items of the current script/
2188 */
2189 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002190find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002192 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 int idx;
2194
2195 if (cctx != NULL)
2196 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2197 {
2198 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2199 + idx;
2200
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002201 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2202 : STRLEN(import->imp_name) == len
2203 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204 return import;
2205 }
2206
2207 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2208 {
2209 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2210
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002211 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2212 : STRLEN(import->imp_name) == len
2213 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 return import;
2215 }
2216 return NULL;
2217}
2218
2219/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002220 * Free all imported variables.
2221 */
2222 static void
2223free_imported(cctx_T *cctx)
2224{
2225 int idx;
2226
2227 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2228 {
2229 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2230
2231 vim_free(import->imp_name);
2232 }
2233 ga_clear(&cctx->ctx_imports);
2234}
2235
2236/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002237 * Get the next line of the function from "cctx".
2238 * Returns NULL when at the end.
2239 */
2240 static char_u *
2241next_line_from_context(cctx_T *cctx)
2242{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002243 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002244
2245 do
2246 {
2247 ++cctx->ctx_lnum;
2248 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002249 {
2250 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002251 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002252 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002253 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002254 cctx->ctx_line_start = line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002255 SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum
2256 + cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002257 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002258 return line;
2259}
2260
2261/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002262 * Return TRUE if "p" points at a "#" but not at "#{".
2263 */
2264 static int
2265comment_start(char_u *p)
2266{
2267 return p[0] == '#' && p[1] != '{';
2268}
2269
2270/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002271 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002272 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002273 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2274 */
2275 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002276may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002277{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002278 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002279 {
2280 char_u *next = next_line_from_context(cctx);
2281
2282 if (next == NULL)
2283 return FAIL;
2284 *arg = skipwhite(next);
2285 }
2286 return OK;
2287}
2288
2289/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002290 * Generate an instruction to load script-local variable "name", without the
2291 * leading "s:".
2292 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002293 */
2294 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002295compile_load_scriptvar(
2296 cctx_T *cctx,
2297 char_u *name, // variable NUL terminated
2298 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002299 char_u **end, // end of variable
2300 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002302 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2304 imported_T *import;
2305
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002306 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002308 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002309 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2310 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 }
2312 if (idx >= 0)
2313 {
2314 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2315
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002316 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317 current_sctx.sc_sid, idx, sv->sv_type);
2318 return OK;
2319 }
2320
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002321 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 if (import != NULL)
2323 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002324 if (import->imp_all)
2325 {
2326 char_u *p = skipwhite(*end);
2327 int name_len;
2328 ufunc_T *ufunc;
2329 type_T *type;
2330
2331 // Used "import * as Name", need to lookup the member.
2332 if (*p != '.')
2333 {
2334 semsg(_("E1060: expected dot after name: %s"), start);
2335 return FAIL;
2336 }
2337 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002338 if (VIM_ISWHITE(*p))
2339 {
2340 emsg(_("E1074: no white space allowed after dot"));
2341 return FAIL;
2342 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002343
2344 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2345 // TODO: what if it is a function?
2346 if (idx < 0)
2347 return FAIL;
2348 *end = p;
2349
2350 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2351 import->imp_sid,
2352 idx,
2353 type);
2354 }
2355 else
2356 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002357 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002358 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2359 import->imp_sid,
2360 import->imp_var_vals_idx,
2361 import->imp_type);
2362 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 return OK;
2364 }
2365
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002366 if (error)
2367 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 return FAIL;
2369}
2370
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002371 static int
2372generate_funcref(cctx_T *cctx, char_u *name)
2373{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002374 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002375
2376 if (ufunc == NULL)
2377 return FAIL;
2378
2379 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2380}
2381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382/*
2383 * Compile a variable name into a load instruction.
2384 * "end" points to just after the name.
2385 * When "error" is FALSE do not give an error when not found.
2386 */
2387 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002388compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389{
2390 type_T *type;
2391 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002392 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002394 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395
2396 if (*(*arg + 1) == ':')
2397 {
2398 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002399 if (end <= *arg + 2)
2400 name = vim_strsave((char_u *)"[empty]");
2401 else
2402 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 if (name == NULL)
2404 return FAIL;
2405
2406 if (**arg == 'v')
2407 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002408 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409 }
2410 else if (**arg == 'g')
2411 {
2412 // Global variables can be defined later, thus we don't check if it
2413 // exists, give error at runtime.
2414 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2415 }
2416 else if (**arg == 's')
2417 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002418 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002420 else if (**arg == 'b')
2421 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002422 // Buffer-local variables can be defined later, thus we don't check
2423 // if it exists, give error at runtime.
2424 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002425 }
2426 else if (**arg == 'w')
2427 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002428 // Window-local variables can be defined later, thus we don't check
2429 // if it exists, give error at runtime.
2430 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002431 }
2432 else if (**arg == 't')
2433 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002434 // Tabpage-local variables can be defined later, thus we don't
2435 // check if it exists, give error at runtime.
2436 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002437 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 else
2439 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002440 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441 goto theend;
2442 }
2443 }
2444 else
2445 {
2446 size_t len = end - *arg;
2447 int idx;
2448 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002449 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450
2451 name = vim_strnsave(*arg, end - *arg);
2452 if (name == NULL)
2453 return FAIL;
2454
2455 idx = lookup_arg(*arg, len, cctx);
2456 if (idx >= 0)
2457 {
2458 if (cctx->ctx_ufunc->uf_arg_types != NULL)
2459 type = cctx->ctx_ufunc->uf_arg_types[idx];
2460 else
2461 type = &t_any;
2462
2463 // Arguments are located above the frame pointer.
2464 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
2465 if (cctx->ctx_ufunc->uf_va_name != NULL)
2466 --idx;
2467 gen_load = TRUE;
2468 }
2469 else if (lookup_vararg(*arg, len, cctx))
2470 {
2471 // varargs is always the last argument
2472 idx = -STACK_FRAME_SIZE - 1;
2473 type = cctx->ctx_ufunc->uf_va_type;
2474 gen_load = TRUE;
2475 }
2476 else
2477 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002478 lvar_T *lvar = lookup_local(*arg, len, cctx);
2479
2480 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002482 type = lvar->lv_type;
2483 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002484 if (lvar->lv_from_outer)
2485 gen_load_outer = TRUE;
2486 else
2487 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 }
2489 else
2490 {
2491 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2492 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2493 res = generate_PUSHBOOL(cctx, **arg == 't'
2494 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002495 else
2496 {
2497 // "var" can be script-local even without using "s:" if it
2498 // already exists.
2499 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2500 == SCRIPT_VERSION_VIM9
2501 || lookup_script(*arg, len) == OK)
2502 res = compile_load_scriptvar(cctx, name, *arg, &end,
2503 FALSE);
2504
2505 // When the name starts with an uppercase letter or "x:" it
2506 // can be a user defined function.
2507 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2508 res = generate_funcref(cctx, name);
2509 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 }
2511 }
2512 if (gen_load)
2513 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002514 if (gen_load_outer)
2515 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 }
2517
2518 *arg = end;
2519
2520theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002521 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 semsg(_(e_var_notfound), name);
2523 vim_free(name);
2524 return res;
2525}
2526
2527/*
2528 * Compile the argument expressions.
2529 * "arg" points to just after the "(" and is advanced to after the ")"
2530 */
2531 static int
2532compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2533{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002534 char_u *p = *arg;
2535 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536
Bram Moolenaare6085c52020-04-12 20:19:16 +02002537 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002539 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002540 {
2541 p = next_line_from_context(cctx);
2542 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002543 goto failret;
2544 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002545 p = skipwhite(p);
2546 }
2547 if (*p == ')')
2548 {
2549 *arg = p + 1;
2550 return OK;
2551 }
2552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 if (compile_expr1(&p, cctx) == FAIL)
2554 return FAIL;
2555 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002556
2557 if (*p != ',' && *skipwhite(p) == ',')
2558 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002559 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002560 p = skipwhite(p);
2561 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002563 {
2564 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002565 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002566 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002567 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002568 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002569 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002571failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002572 emsg(_(e_missing_close));
2573 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574}
2575
2576/*
2577 * Compile a function call: name(arg1, arg2)
2578 * "arg" points to "name", "arg + varlen" to the "(".
2579 * "argcount_init" is 1 for "value->method()"
2580 * Instructions:
2581 * EVAL arg1
2582 * EVAL arg2
2583 * BCALL / DCALL / UCALL
2584 */
2585 static int
2586compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2587{
2588 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002589 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 int argcount = argcount_init;
2591 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002592 char_u fname_buf[FLEN_FIXED + 1];
2593 char_u *tofree = NULL;
2594 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002596 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597
2598 if (varlen >= sizeof(namebuf))
2599 {
2600 semsg(_("E1011: name too long: %s"), name);
2601 return FAIL;
2602 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002603 vim_strncpy(namebuf, *arg, varlen);
2604 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605
2606 *arg = skipwhite(*arg + varlen + 1);
2607 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002608 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002610 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 {
2612 int idx;
2613
2614 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002615 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002617 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002618 else
2619 semsg(_(e_unknownfunc), namebuf);
2620 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 }
2622
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002624 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002626 {
2627 res = generate_CALL(cctx, ufunc, argcount);
2628 goto theend;
2629 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630
2631 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002632 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002634 if (STRNCMP(namebuf, "g:", 2) != 0
2635 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002636 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002637 garray_T *stack = &cctx->ctx_type_stack;
2638 type_T *type;
2639
2640 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2641 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002642 goto theend;
2643 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002645 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002646 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002647 if (STRNCMP(namebuf, "g:", 2) == 0)
2648 res = generate_UCALL(cctx, name, argcount);
2649 else
2650 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002651
2652theend:
2653 vim_free(tofree);
2654 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655}
2656
2657// like NAMESPACE_CHAR but with 'a' and 'l'.
2658#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2659
2660/*
2661 * Find the end of a variable or function name. Unlike find_name_end() this
2662 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002663 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 * Return a pointer to just after the name. Equal to "arg" if there is no
2665 * valid name.
2666 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002667 static char_u *
2668to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669{
2670 char_u *p;
2671
2672 // Quick check for valid starting character.
2673 if (!eval_isnamec1(*arg))
2674 return arg;
2675
2676 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2677 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2678 // and can be used in slice "[n:]".
2679 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002680 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2682 break;
2683 return p;
2684}
2685
2686/*
2687 * Like to_name_end() but also skip over a list or dict constant.
2688 */
2689 char_u *
2690to_name_const_end(char_u *arg)
2691{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002692 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 typval_T rettv;
2694
2695 if (p == arg && *arg == '[')
2696 {
2697
2698 // Can be "[1, 2, 3]->Func()".
2699 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2700 p = arg;
2701 }
2702 else if (p == arg && *arg == '#' && arg[1] == '{')
2703 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002704 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 ++p;
2706 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2707 p = arg;
2708 }
2709 else if (p == arg && *arg == '{')
2710 {
2711 int ret = get_lambda_tv(&p, &rettv, FALSE);
2712
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002713 // Can be "{x -> ret}()".
2714 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 if (ret == NOTDONE)
2716 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2717 if (ret != OK)
2718 p = arg;
2719 }
2720
2721 return p;
2722}
2723
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724/*
2725 * parse a list: [expr, expr]
2726 * "*arg" points to the '['.
2727 */
2728 static int
2729compile_list(char_u **arg, cctx_T *cctx)
2730{
2731 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002732 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 int count = 0;
2734
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002735 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002737 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002738 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002739 p = next_line_from_context(cctx);
2740 if (p == NULL)
2741 {
2742 semsg(_(e_list_end), *arg);
2743 return FAIL;
2744 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002745 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002746 p = skipwhite(p);
2747 }
2748 if (*p == ']')
2749 {
2750 ++p;
2751 // Allow for following comment, after at least one space.
2752 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
2753 p += STRLEN(p);
2754 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002755 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 if (compile_expr1(&p, cctx) == FAIL)
2757 break;
2758 ++count;
2759 if (*p == ',')
2760 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002761 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 p = skipwhite(p);
2763 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002764 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765
2766 generate_NEWLIST(cctx, count);
2767 return OK;
2768}
2769
2770/*
2771 * parse a lambda: {arg, arg -> expr}
2772 * "*arg" points to the '{'.
2773 */
2774 static int
2775compile_lambda(char_u **arg, cctx_T *cctx)
2776{
2777 garray_T *instr = &cctx->ctx_instr;
2778 typval_T rettv;
2779 ufunc_T *ufunc;
2780
2781 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002782 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002784
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002786 ++ufunc->uf_refcount;
2787 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002788 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789
2790 // The function will have one line: "return {expr}".
2791 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002792 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793
2794 if (ufunc->uf_dfunc_idx >= 0)
2795 {
2796 if (ga_grow(instr, 1) == FAIL)
2797 return FAIL;
2798 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2799 return OK;
2800 }
2801 return FAIL;
2802}
2803
2804/*
2805 * Compile a lamda call: expr->{lambda}(args)
2806 * "arg" points to the "{".
2807 */
2808 static int
2809compile_lambda_call(char_u **arg, cctx_T *cctx)
2810{
2811 ufunc_T *ufunc;
2812 typval_T rettv;
2813 int argcount = 1;
2814 int ret = FAIL;
2815
2816 // Get the funcref in "rettv".
2817 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2818 return FAIL;
2819
2820 if (**arg != '(')
2821 {
2822 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002823 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 else
2825 semsg(_(e_missing_paren), "lambda");
2826 clear_tv(&rettv);
2827 return FAIL;
2828 }
2829
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 ufunc = rettv.vval.v_partial->pt_func;
2831 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002832 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002833 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002834
2835 // The function will have one line: "return {expr}".
2836 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002837 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838
2839 // compile the arguments
2840 *arg = skipwhite(*arg + 1);
2841 if (compile_arguments(arg, cctx, &argcount) == OK)
2842 // call the compiled function
2843 ret = generate_CALL(cctx, ufunc, argcount);
2844
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 return ret;
2846}
2847
2848/*
2849 * parse a dict: {'key': val} or #{key: val}
2850 * "*arg" points to the '{'.
2851 */
2852 static int
2853compile_dict(char_u **arg, cctx_T *cctx, int literal)
2854{
2855 garray_T *instr = &cctx->ctx_instr;
2856 int count = 0;
2857 dict_T *d = dict_alloc();
2858 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002859 char_u *whitep = *arg;
2860 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861
2862 if (d == NULL)
2863 return FAIL;
2864 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002865 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 {
2867 char_u *key = NULL;
2868
Bram Moolenaar2c330432020-04-13 14:41:35 +02002869 while (**arg == NUL || (literal && **arg == '"')
2870 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002871 {
2872 *arg = next_line_from_context(cctx);
2873 if (*arg == NULL)
2874 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002875 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002876 *arg = skipwhite(*arg);
2877 }
2878
2879 if (**arg == '}')
2880 break;
2881
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 if (literal)
2883 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002884 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885
Bram Moolenaar2c330432020-04-13 14:41:35 +02002886 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 {
2888 semsg(_("E1014: Invalid key: %s"), *arg);
2889 return FAIL;
2890 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002891 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 if (generate_PUSHS(cctx, key) == FAIL)
2893 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002894 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 }
2896 else
2897 {
2898 isn_T *isn;
2899
2900 if (compile_expr1(arg, cctx) == FAIL)
2901 return FAIL;
2902 // TODO: check type is string
2903 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2904 if (isn->isn_type == ISN_PUSHS)
2905 key = isn->isn_arg.string;
2906 }
2907
2908 // Check for duplicate keys, if using string keys.
2909 if (key != NULL)
2910 {
2911 item = dict_find(d, key, -1);
2912 if (item != NULL)
2913 {
2914 semsg(_(e_duplicate_key), key);
2915 goto failret;
2916 }
2917 item = dictitem_alloc(key);
2918 if (item != NULL)
2919 {
2920 item->di_tv.v_type = VAR_UNKNOWN;
2921 item->di_tv.v_lock = 0;
2922 if (dict_add(d, item) == FAIL)
2923 dictitem_free(item);
2924 }
2925 }
2926
2927 *arg = skipwhite(*arg);
2928 if (**arg != ':')
2929 {
2930 semsg(_(e_missing_dict_colon), *arg);
2931 return FAIL;
2932 }
2933
Bram Moolenaar2c330432020-04-13 14:41:35 +02002934 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002936 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002937 {
2938 *arg = next_line_from_context(cctx);
2939 if (*arg == NULL)
2940 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002941 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002942 *arg = skipwhite(*arg);
2943 }
2944
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 if (compile_expr1(arg, cctx) == FAIL)
2946 return FAIL;
2947 ++count;
2948
Bram Moolenaar2c330432020-04-13 14:41:35 +02002949 whitep = *arg;
2950 p = skipwhite(*arg);
2951 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002952 {
2953 *arg = next_line_from_context(cctx);
2954 if (*arg == NULL)
2955 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002956 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002957 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002958 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002959 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 if (**arg == '}')
2961 break;
2962 if (**arg != ',')
2963 {
2964 semsg(_(e_missing_dict_comma), *arg);
2965 goto failret;
2966 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002967 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 *arg = skipwhite(*arg + 1);
2969 }
2970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 *arg = *arg + 1;
2972
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002973 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002974 p = skipwhite(*arg);
2975 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002976 *arg += STRLEN(*arg);
2977
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 dict_unref(d);
2979 return generate_NEWDICT(cctx, count);
2980
2981failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002982 if (*arg == NULL)
2983 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 dict_unref(d);
2985 return FAIL;
2986}
2987
2988/*
2989 * Compile "&option".
2990 */
2991 static int
2992compile_get_option(char_u **arg, cctx_T *cctx)
2993{
2994 typval_T rettv;
2995 char_u *start = *arg;
2996 int ret;
2997
2998 // parse the option and get the current value to get the type.
2999 rettv.v_type = VAR_UNKNOWN;
3000 ret = get_option_tv(arg, &rettv, TRUE);
3001 if (ret == OK)
3002 {
3003 // include the '&' in the name, get_option_tv() expects it.
3004 char_u *name = vim_strnsave(start, *arg - start);
3005 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3006
3007 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3008 vim_free(name);
3009 }
3010 clear_tv(&rettv);
3011
3012 return ret;
3013}
3014
3015/*
3016 * Compile "$VAR".
3017 */
3018 static int
3019compile_get_env(char_u **arg, cctx_T *cctx)
3020{
3021 char_u *start = *arg;
3022 int len;
3023 int ret;
3024 char_u *name;
3025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 ++*arg;
3027 len = get_env_len(arg);
3028 if (len == 0)
3029 {
3030 semsg(_(e_syntax_at), start - 1);
3031 return FAIL;
3032 }
3033
3034 // include the '$' in the name, get_env_tv() expects it.
3035 name = vim_strnsave(start, len + 1);
3036 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3037 vim_free(name);
3038 return ret;
3039}
3040
3041/*
3042 * Compile "@r".
3043 */
3044 static int
3045compile_get_register(char_u **arg, cctx_T *cctx)
3046{
3047 int ret;
3048
3049 ++*arg;
3050 if (**arg == NUL)
3051 {
3052 semsg(_(e_syntax_at), *arg - 1);
3053 return FAIL;
3054 }
3055 if (!valid_yank_reg(**arg, TRUE))
3056 {
3057 emsg_invreg(**arg);
3058 return FAIL;
3059 }
3060 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3061 ++*arg;
3062 return ret;
3063}
3064
3065/*
3066 * Apply leading '!', '-' and '+' to constant "rettv".
3067 */
3068 static int
3069apply_leader(typval_T *rettv, char_u *start, char_u *end)
3070{
3071 char_u *p = end;
3072
3073 // this works from end to start
3074 while (p > start)
3075 {
3076 --p;
3077 if (*p == '-' || *p == '+')
3078 {
3079 // only '-' has an effect, for '+' we only check the type
3080#ifdef FEAT_FLOAT
3081 if (rettv->v_type == VAR_FLOAT)
3082 {
3083 if (*p == '-')
3084 rettv->vval.v_float = -rettv->vval.v_float;
3085 }
3086 else
3087#endif
3088 {
3089 varnumber_T val;
3090 int error = FALSE;
3091
3092 // tv_get_number_chk() accepts a string, but we don't want that
3093 // here
3094 if (check_not_string(rettv) == FAIL)
3095 return FAIL;
3096 val = tv_get_number_chk(rettv, &error);
3097 clear_tv(rettv);
3098 if (error)
3099 return FAIL;
3100 if (*p == '-')
3101 val = -val;
3102 rettv->v_type = VAR_NUMBER;
3103 rettv->vval.v_number = val;
3104 }
3105 }
3106 else
3107 {
3108 int v = tv2bool(rettv);
3109
3110 // '!' is permissive in the type.
3111 clear_tv(rettv);
3112 rettv->v_type = VAR_BOOL;
3113 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3114 }
3115 }
3116 return OK;
3117}
3118
3119/*
3120 * Recognize v: variables that are constants and set "rettv".
3121 */
3122 static void
3123get_vim_constant(char_u **arg, typval_T *rettv)
3124{
3125 if (STRNCMP(*arg, "v:true", 6) == 0)
3126 {
3127 rettv->v_type = VAR_BOOL;
3128 rettv->vval.v_number = VVAL_TRUE;
3129 *arg += 6;
3130 }
3131 else if (STRNCMP(*arg, "v:false", 7) == 0)
3132 {
3133 rettv->v_type = VAR_BOOL;
3134 rettv->vval.v_number = VVAL_FALSE;
3135 *arg += 7;
3136 }
3137 else if (STRNCMP(*arg, "v:null", 6) == 0)
3138 {
3139 rettv->v_type = VAR_SPECIAL;
3140 rettv->vval.v_number = VVAL_NULL;
3141 *arg += 6;
3142 }
3143 else if (STRNCMP(*arg, "v:none", 6) == 0)
3144 {
3145 rettv->v_type = VAR_SPECIAL;
3146 rettv->vval.v_number = VVAL_NONE;
3147 *arg += 6;
3148 }
3149}
3150
3151/*
3152 * Compile code to apply '-', '+' and '!'.
3153 */
3154 static int
3155compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3156{
3157 char_u *p = end;
3158
3159 // this works from end to start
3160 while (p > start)
3161 {
3162 --p;
3163 if (*p == '-' || *p == '+')
3164 {
3165 int negate = *p == '-';
3166 isn_T *isn;
3167
3168 // TODO: check type
3169 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3170 {
3171 --p;
3172 if (*p == '-')
3173 negate = !negate;
3174 }
3175 // only '-' has an effect, for '+' we only check the type
3176 if (negate)
3177 isn = generate_instr(cctx, ISN_NEGATENR);
3178 else
3179 isn = generate_instr(cctx, ISN_CHECKNR);
3180 if (isn == NULL)
3181 return FAIL;
3182 }
3183 else
3184 {
3185 int invert = TRUE;
3186
3187 while (p > start && p[-1] == '!')
3188 {
3189 --p;
3190 invert = !invert;
3191 }
3192 if (generate_2BOOL(cctx, invert) == FAIL)
3193 return FAIL;
3194 }
3195 }
3196 return OK;
3197}
3198
3199/*
3200 * Compile whatever comes after "name" or "name()".
3201 */
3202 static int
3203compile_subscript(
3204 char_u **arg,
3205 cctx_T *cctx,
3206 char_u **start_leader,
3207 char_u *end_leader)
3208{
3209 for (;;)
3210 {
3211 if (**arg == '(')
3212 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003213 garray_T *stack = &cctx->ctx_type_stack;
3214 type_T *type;
3215 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216
3217 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003218 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 *arg = skipwhite(*arg + 1);
3221 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3222 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003223 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 return FAIL;
3225 }
3226 else if (**arg == '-' && (*arg)[1] == '>')
3227 {
3228 char_u *p;
3229
3230 // something->method()
3231 // Apply the '!', '-' and '+' first:
3232 // -1.0->func() works like (-1.0)->func()
3233 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3234 return FAIL;
3235 *start_leader = end_leader; // don't apply again later
3236
3237 *arg = skipwhite(*arg + 2);
3238 if (**arg == '{')
3239 {
3240 // lambda call: list->{lambda}
3241 if (compile_lambda_call(arg, cctx) == FAIL)
3242 return FAIL;
3243 }
3244 else
3245 {
3246 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003247 p = *arg;
3248 if (ASCII_ISALPHA(*p) && p[1] == ':')
3249 p += 2;
3250 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 ;
3252 if (*p != '(')
3253 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003254 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 return FAIL;
3256 }
3257 // TODO: base value may not be the first argument
3258 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
3259 return FAIL;
3260 }
3261 }
3262 else if (**arg == '[')
3263 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01003264 garray_T *stack;
3265 type_T **typep;
3266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 // list index: list[123]
3268 // TODO: more arguments
3269 // TODO: dict member dict['name']
3270 *arg = skipwhite(*arg + 1);
3271 if (compile_expr1(arg, cctx) == FAIL)
3272 return FAIL;
3273
3274 if (**arg != ']')
3275 {
3276 emsg(_(e_missbrac));
3277 return FAIL;
3278 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003279 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280
3281 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3282 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003283 stack = &cctx->ctx_type_stack;
3284 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
3285 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
3286 {
3287 emsg(_(e_listreq));
3288 return FAIL;
3289 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01003290 if ((*typep)->tt_type == VAR_LIST)
3291 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 }
3293 else if (**arg == '.' && (*arg)[1] != '.')
3294 {
3295 char_u *p;
3296
3297 ++*arg;
3298 p = *arg;
3299 // dictionary member: dict.name
3300 if (eval_isnamec1(*p))
3301 while (eval_isnamec(*p))
3302 MB_PTR_ADV(p);
3303 if (p == *arg)
3304 {
3305 semsg(_(e_syntax_at), *arg);
3306 return FAIL;
3307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
3309 return FAIL;
3310 *arg = p;
3311 }
3312 else
3313 break;
3314 }
3315
3316 // TODO - see handle_subscript():
3317 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3318 // Don't do this when "Func" is already a partial that was bound
3319 // explicitly (pt_auto is FALSE).
3320
3321 return OK;
3322}
3323
3324/*
3325 * Compile an expression at "*p" and add instructions to "instr".
3326 * "p" is advanced until after the expression, skipping white space.
3327 *
3328 * This is the equivalent of eval1(), eval2(), etc.
3329 */
3330
3331/*
3332 * number number constant
3333 * 0zFFFFFFFF Blob constant
3334 * "string" string constant
3335 * 'string' literal string constant
3336 * &option-name option value
3337 * @r register contents
3338 * identifier variable value
3339 * function() function call
3340 * $VAR environment variable
3341 * (expression) nested expression
3342 * [expr, expr] List
3343 * {key: val, key: val} Dictionary
3344 * #{key: val, key: val} Dictionary with literal keys
3345 *
3346 * Also handle:
3347 * ! in front logical NOT
3348 * - in front unary minus
3349 * + in front unary plus (ignored)
3350 * trailing (arg) funcref/partial call
3351 * trailing [] subscript in String or List
3352 * trailing .name entry in Dictionary
3353 * trailing ->name() method call
3354 */
3355 static int
3356compile_expr7(char_u **arg, cctx_T *cctx)
3357{
3358 typval_T rettv;
3359 char_u *start_leader, *end_leader;
3360 int ret = OK;
3361
3362 /*
3363 * Skip '!', '-' and '+' characters. They are handled later.
3364 */
3365 start_leader = *arg;
3366 while (**arg == '!' || **arg == '-' || **arg == '+')
3367 *arg = skipwhite(*arg + 1);
3368 end_leader = *arg;
3369
3370 rettv.v_type = VAR_UNKNOWN;
3371 switch (**arg)
3372 {
3373 /*
3374 * Number constant.
3375 */
3376 case '0': // also for blob starting with 0z
3377 case '1':
3378 case '2':
3379 case '3':
3380 case '4':
3381 case '5':
3382 case '6':
3383 case '7':
3384 case '8':
3385 case '9':
3386 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
3387 return FAIL;
3388 break;
3389
3390 /*
3391 * String constant: "string".
3392 */
3393 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
3394 return FAIL;
3395 break;
3396
3397 /*
3398 * Literal string constant: 'str''ing'.
3399 */
3400 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
3401 return FAIL;
3402 break;
3403
3404 /*
3405 * Constant Vim variable.
3406 */
3407 case 'v': get_vim_constant(arg, &rettv);
3408 ret = NOTDONE;
3409 break;
3410
3411 /*
3412 * List: [expr, expr]
3413 */
3414 case '[': ret = compile_list(arg, cctx);
3415 break;
3416
3417 /*
3418 * Dictionary: #{key: val, key: val}
3419 */
3420 case '#': if ((*arg)[1] == '{')
3421 {
3422 ++*arg;
3423 ret = compile_dict(arg, cctx, TRUE);
3424 }
3425 else
3426 ret = NOTDONE;
3427 break;
3428
3429 /*
3430 * Lambda: {arg, arg -> expr}
3431 * Dictionary: {'key': val, 'key': val}
3432 */
3433 case '{': {
3434 char_u *start = skipwhite(*arg + 1);
3435
3436 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003437 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003439 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 if (ret != FAIL && *start == '>')
3441 ret = compile_lambda(arg, cctx);
3442 else
3443 ret = compile_dict(arg, cctx, FALSE);
3444 }
3445 break;
3446
3447 /*
3448 * Option value: &name
3449 */
3450 case '&': ret = compile_get_option(arg, cctx);
3451 break;
3452
3453 /*
3454 * Environment variable: $VAR.
3455 */
3456 case '$': ret = compile_get_env(arg, cctx);
3457 break;
3458
3459 /*
3460 * Register contents: @r.
3461 */
3462 case '@': ret = compile_get_register(arg, cctx);
3463 break;
3464 /*
3465 * nested expression: (expression).
3466 */
3467 case '(': *arg = skipwhite(*arg + 1);
3468 ret = compile_expr1(arg, cctx); // recursive!
3469 *arg = skipwhite(*arg);
3470 if (**arg == ')')
3471 ++*arg;
3472 else if (ret == OK)
3473 {
3474 emsg(_(e_missing_close));
3475 ret = FAIL;
3476 }
3477 break;
3478
3479 default: ret = NOTDONE;
3480 break;
3481 }
3482 if (ret == FAIL)
3483 return FAIL;
3484
3485 if (rettv.v_type != VAR_UNKNOWN)
3486 {
3487 // apply the '!', '-' and '+' before the constant
3488 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
3489 {
3490 clear_tv(&rettv);
3491 return FAIL;
3492 }
3493 start_leader = end_leader; // don't apply again below
3494
3495 // push constant
3496 switch (rettv.v_type)
3497 {
3498 case VAR_BOOL:
3499 generate_PUSHBOOL(cctx, rettv.vval.v_number);
3500 break;
3501 case VAR_SPECIAL:
3502 generate_PUSHSPEC(cctx, rettv.vval.v_number);
3503 break;
3504 case VAR_NUMBER:
3505 generate_PUSHNR(cctx, rettv.vval.v_number);
3506 break;
3507#ifdef FEAT_FLOAT
3508 case VAR_FLOAT:
3509 generate_PUSHF(cctx, rettv.vval.v_float);
3510 break;
3511#endif
3512 case VAR_BLOB:
3513 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
3514 rettv.vval.v_blob = NULL;
3515 break;
3516 case VAR_STRING:
3517 generate_PUSHS(cctx, rettv.vval.v_string);
3518 rettv.vval.v_string = NULL;
3519 break;
3520 default:
3521 iemsg("constant type missing");
3522 return FAIL;
3523 }
3524 }
3525 else if (ret == NOTDONE)
3526 {
3527 char_u *p;
3528 int r;
3529
3530 if (!eval_isnamec1(**arg))
3531 {
3532 semsg(_("E1015: Name expected: %s"), *arg);
3533 return FAIL;
3534 }
3535
3536 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003537 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 if (*p == '(')
3539 r = compile_call(arg, p - *arg, cctx, 0);
3540 else
3541 r = compile_load(arg, p, cctx, TRUE);
3542 if (r == FAIL)
3543 return FAIL;
3544 }
3545
3546 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
3547 return FAIL;
3548
3549 // Now deal with prefixed '-', '+' and '!', if not done already.
3550 return compile_leader(cctx, start_leader, end_leader);
3551}
3552
3553/*
3554 * * number multiplication
3555 * / number division
3556 * % number modulo
3557 */
3558 static int
3559compile_expr6(char_u **arg, cctx_T *cctx)
3560{
3561 char_u *op;
3562
3563 // get the first variable
3564 if (compile_expr7(arg, cctx) == FAIL)
3565 return FAIL;
3566
3567 /*
3568 * Repeat computing, until no "*", "/" or "%" is following.
3569 */
3570 for (;;)
3571 {
3572 op = skipwhite(*arg);
3573 if (*op != '*' && *op != '/' && *op != '%')
3574 break;
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003575 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 {
3577 char_u buf[3];
3578
3579 vim_strncpy(buf, op, 1);
3580 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003581 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 }
3583 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003584 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003585 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586
3587 // get the second variable
3588 if (compile_expr7(arg, cctx) == FAIL)
3589 return FAIL;
3590
3591 generate_two_op(cctx, op);
3592 }
3593
3594 return OK;
3595}
3596
3597/*
3598 * + number addition
3599 * - number subtraction
3600 * .. string concatenation
3601 */
3602 static int
3603compile_expr5(char_u **arg, cctx_T *cctx)
3604{
3605 char_u *op;
3606 int oplen;
3607
3608 // get the first variable
3609 if (compile_expr6(arg, cctx) == FAIL)
3610 return FAIL;
3611
3612 /*
3613 * Repeat computing, until no "+", "-" or ".." is following.
3614 */
3615 for (;;)
3616 {
3617 op = skipwhite(*arg);
3618 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3619 break;
3620 oplen = (*op == '.' ? 2 : 1);
3621
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003622 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 {
3624 char_u buf[3];
3625
3626 vim_strncpy(buf, op, oplen);
3627 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003628 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 }
3630
3631 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003632 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003633 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634
3635 // get the second variable
3636 if (compile_expr6(arg, cctx) == FAIL)
3637 return FAIL;
3638
3639 if (*op == '.')
3640 {
3641 if (may_generate_2STRING(-2, cctx) == FAIL
3642 || may_generate_2STRING(-1, cctx) == FAIL)
3643 return FAIL;
3644 generate_instr_drop(cctx, ISN_CONCAT, 1);
3645 }
3646 else
3647 generate_two_op(cctx, op);
3648 }
3649
3650 return OK;
3651}
3652
Bram Moolenaar080457c2020-03-03 21:53:32 +01003653 static exptype_T
3654get_compare_type(char_u *p, int *len, int *type_is)
3655{
3656 exptype_T type = EXPR_UNKNOWN;
3657 int i;
3658
3659 switch (p[0])
3660 {
3661 case '=': if (p[1] == '=')
3662 type = EXPR_EQUAL;
3663 else if (p[1] == '~')
3664 type = EXPR_MATCH;
3665 break;
3666 case '!': if (p[1] == '=')
3667 type = EXPR_NEQUAL;
3668 else if (p[1] == '~')
3669 type = EXPR_NOMATCH;
3670 break;
3671 case '>': if (p[1] != '=')
3672 {
3673 type = EXPR_GREATER;
3674 *len = 1;
3675 }
3676 else
3677 type = EXPR_GEQUAL;
3678 break;
3679 case '<': if (p[1] != '=')
3680 {
3681 type = EXPR_SMALLER;
3682 *len = 1;
3683 }
3684 else
3685 type = EXPR_SEQUAL;
3686 break;
3687 case 'i': if (p[1] == 's')
3688 {
3689 // "is" and "isnot"; but not a prefix of a name
3690 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3691 *len = 5;
3692 i = p[*len];
3693 if (!isalnum(i) && i != '_')
3694 {
3695 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3696 *type_is = TRUE;
3697 }
3698 }
3699 break;
3700 }
3701 return type;
3702}
3703
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704/*
3705 * expr5a == expr5b
3706 * expr5a =~ expr5b
3707 * expr5a != expr5b
3708 * expr5a !~ expr5b
3709 * expr5a > expr5b
3710 * expr5a >= expr5b
3711 * expr5a < expr5b
3712 * expr5a <= expr5b
3713 * expr5a is expr5b
3714 * expr5a isnot expr5b
3715 *
3716 * Produces instructions:
3717 * EVAL expr5a Push result of "expr5a"
3718 * EVAL expr5b Push result of "expr5b"
3719 * COMPARE one of the compare instructions
3720 */
3721 static int
3722compile_expr4(char_u **arg, cctx_T *cctx)
3723{
3724 exptype_T type = EXPR_UNKNOWN;
3725 char_u *p;
3726 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 int type_is = FALSE;
3728
3729 // get the first variable
3730 if (compile_expr5(arg, cctx) == FAIL)
3731 return FAIL;
3732
3733 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003734 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735
3736 /*
3737 * If there is a comparative operator, use it.
3738 */
3739 if (type != EXPR_UNKNOWN)
3740 {
3741 int ic = FALSE; // Default: do not ignore case
3742
3743 if (type_is && (p[len] == '?' || p[len] == '#'))
3744 {
3745 semsg(_(e_invexpr2), *arg);
3746 return FAIL;
3747 }
3748 // extra question mark appended: ignore case
3749 if (p[len] == '?')
3750 {
3751 ic = TRUE;
3752 ++len;
3753 }
3754 // extra '#' appended: match case (ignored)
3755 else if (p[len] == '#')
3756 ++len;
3757 // nothing appended: match case
3758
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003759 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 {
3761 char_u buf[7];
3762
3763 vim_strncpy(buf, p, len);
3764 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003765 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 }
3767
3768 // get the second variable
3769 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003770 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003771 return FAIL;
3772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 if (compile_expr5(arg, cctx) == FAIL)
3774 return FAIL;
3775
3776 generate_COMPARE(cctx, type, ic);
3777 }
3778
3779 return OK;
3780}
3781
3782/*
3783 * Compile || or &&.
3784 */
3785 static int
3786compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3787{
3788 char_u *p = skipwhite(*arg);
3789 int opchar = *op;
3790
3791 if (p[0] == opchar && p[1] == opchar)
3792 {
3793 garray_T *instr = &cctx->ctx_instr;
3794 garray_T end_ga;
3795
3796 /*
3797 * Repeat until there is no following "||" or "&&"
3798 */
3799 ga_init2(&end_ga, sizeof(int), 10);
3800 while (p[0] == opchar && p[1] == opchar)
3801 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003802 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3803 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003805 return FAIL;
3806 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807
3808 if (ga_grow(&end_ga, 1) == FAIL)
3809 {
3810 ga_clear(&end_ga);
3811 return FAIL;
3812 }
3813 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3814 ++end_ga.ga_len;
3815 generate_JUMP(cctx, opchar == '|'
3816 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3817
3818 // eval the next expression
3819 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003820 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003821 return FAIL;
3822
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 if ((opchar == '|' ? compile_expr3(arg, cctx)
3824 : compile_expr4(arg, cctx)) == FAIL)
3825 {
3826 ga_clear(&end_ga);
3827 return FAIL;
3828 }
3829 p = skipwhite(*arg);
3830 }
3831
3832 // Fill in the end label in all jumps.
3833 while (end_ga.ga_len > 0)
3834 {
3835 isn_T *isn;
3836
3837 --end_ga.ga_len;
3838 isn = ((isn_T *)instr->ga_data)
3839 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3840 isn->isn_arg.jump.jump_where = instr->ga_len;
3841 }
3842 ga_clear(&end_ga);
3843 }
3844
3845 return OK;
3846}
3847
3848/*
3849 * expr4a && expr4a && expr4a logical AND
3850 *
3851 * Produces instructions:
3852 * EVAL expr4a Push result of "expr4a"
3853 * JUMP_AND_KEEP_IF_FALSE end
3854 * EVAL expr4b Push result of "expr4b"
3855 * JUMP_AND_KEEP_IF_FALSE end
3856 * EVAL expr4c Push result of "expr4c"
3857 * end:
3858 */
3859 static int
3860compile_expr3(char_u **arg, cctx_T *cctx)
3861{
3862 // get the first variable
3863 if (compile_expr4(arg, cctx) == FAIL)
3864 return FAIL;
3865
3866 // || and && work almost the same
3867 return compile_and_or(arg, cctx, "&&");
3868}
3869
3870/*
3871 * expr3a || expr3b || expr3c logical OR
3872 *
3873 * Produces instructions:
3874 * EVAL expr3a Push result of "expr3a"
3875 * JUMP_AND_KEEP_IF_TRUE end
3876 * EVAL expr3b Push result of "expr3b"
3877 * JUMP_AND_KEEP_IF_TRUE end
3878 * EVAL expr3c Push result of "expr3c"
3879 * end:
3880 */
3881 static int
3882compile_expr2(char_u **arg, cctx_T *cctx)
3883{
3884 // eval the first expression
3885 if (compile_expr3(arg, cctx) == FAIL)
3886 return FAIL;
3887
3888 // || and && work almost the same
3889 return compile_and_or(arg, cctx, "||");
3890}
3891
3892/*
3893 * Toplevel expression: expr2 ? expr1a : expr1b
3894 *
3895 * Produces instructions:
3896 * EVAL expr2 Push result of "expr"
3897 * JUMP_IF_FALSE alt jump if false
3898 * EVAL expr1a
3899 * JUMP_ALWAYS end
3900 * alt: EVAL expr1b
3901 * end:
3902 */
3903 static int
3904compile_expr1(char_u **arg, cctx_T *cctx)
3905{
3906 char_u *p;
3907
3908 // evaluate the first expression
3909 if (compile_expr2(arg, cctx) == FAIL)
3910 return FAIL;
3911
3912 p = skipwhite(*arg);
3913 if (*p == '?')
3914 {
3915 garray_T *instr = &cctx->ctx_instr;
3916 garray_T *stack = &cctx->ctx_type_stack;
3917 int alt_idx = instr->ga_len;
3918 int end_idx;
3919 isn_T *isn;
3920 type_T *type1;
3921 type_T *type2;
3922
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003923 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3924 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003926 return FAIL;
3927 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928
3929 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3930
3931 // evaluate the second expression; any type is accepted
3932 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003933 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003934 return FAIL;
3935
Bram Moolenaara6d53682020-01-28 23:04:06 +01003936 if (compile_expr1(arg, cctx) == FAIL)
3937 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938
3939 // remember the type and drop it
3940 --stack->ga_len;
3941 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3942
3943 end_idx = instr->ga_len;
3944 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3945
3946 // jump here from JUMP_IF_FALSE
3947 isn = ((isn_T *)instr->ga_data) + alt_idx;
3948 isn->isn_arg.jump.jump_where = instr->ga_len;
3949
3950 // Check for the ":".
3951 p = skipwhite(*arg);
3952 if (*p != ':')
3953 {
3954 emsg(_(e_missing_colon));
3955 return FAIL;
3956 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003957 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3958 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003960 return FAIL;
3961 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962
3963 // evaluate the third expression
3964 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003965 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003966 return FAIL;
3967
Bram Moolenaara6d53682020-01-28 23:04:06 +01003968 if (compile_expr1(arg, cctx) == FAIL)
3969 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970
3971 // If the types differ, the result has a more generic type.
3972 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003973 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974
3975 // jump here from JUMP_ALWAYS
3976 isn = ((isn_T *)instr->ga_data) + end_idx;
3977 isn->isn_arg.jump.jump_where = instr->ga_len;
3978 }
3979 return OK;
3980}
3981
3982/*
3983 * compile "return [expr]"
3984 */
3985 static char_u *
3986compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3987{
3988 char_u *p = arg;
3989 garray_T *stack = &cctx->ctx_type_stack;
3990 type_T *stack_type;
3991
3992 if (*p != NUL && *p != '|' && *p != '\n')
3993 {
3994 // compile return argument into instructions
3995 if (compile_expr1(&p, cctx) == FAIL)
3996 return NULL;
3997
3998 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3999 if (set_return_type)
4000 cctx->ctx_ufunc->uf_ret_type = stack_type;
4001 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4002 == FAIL)
4003 return NULL;
4004 }
4005 else
4006 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004007 // "set_return_type" cannot be TRUE, only used for a lambda which
4008 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004009 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4010 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 {
4012 emsg(_("E1003: Missing return value"));
4013 return NULL;
4014 }
4015
4016 // No argument, return zero.
4017 generate_PUSHNR(cctx, 0);
4018 }
4019
4020 if (generate_instr(cctx, ISN_RETURN) == NULL)
4021 return NULL;
4022
4023 // "return val | endif" is possible
4024 return skipwhite(p);
4025}
4026
4027/*
4028 * Return the length of an assignment operator, or zero if there isn't one.
4029 */
4030 int
4031assignment_len(char_u *p, int *heredoc)
4032{
4033 if (*p == '=')
4034 {
4035 if (p[1] == '<' && p[2] == '<')
4036 {
4037 *heredoc = TRUE;
4038 return 3;
4039 }
4040 return 1;
4041 }
4042 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4043 return 2;
4044 if (STRNCMP(p, "..=", 3) == 0)
4045 return 3;
4046 return 0;
4047}
4048
4049// words that cannot be used as a variable
4050static char *reserved[] = {
4051 "true",
4052 "false",
4053 NULL
4054};
4055
4056/*
4057 * Get a line for "=<<".
4058 * Return a pointer to the line in allocated memory.
4059 * Return NULL for end-of-file or some error.
4060 */
4061 static char_u *
4062heredoc_getline(
4063 int c UNUSED,
4064 void *cookie,
4065 int indent UNUSED,
4066 int do_concat UNUSED)
4067{
4068 cctx_T *cctx = (cctx_T *)cookie;
4069
4070 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004071 {
4072 iemsg("Heredoc got to end");
4073 return NULL;
4074 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 ++cctx->ctx_lnum;
4076 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4077 [cctx->ctx_lnum]);
4078}
4079
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004080typedef enum {
4081 dest_local,
4082 dest_option,
4083 dest_env,
4084 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004085 dest_buffer,
4086 dest_window,
4087 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004088 dest_vimvar,
4089 dest_script,
4090 dest_reg,
4091} assign_dest_T;
4092
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093/*
4094 * compile "let var [= expr]", "const var = expr" and "var = expr"
4095 * "arg" points to "var".
4096 */
4097 static char_u *
4098compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4099{
4100 char_u *p;
4101 char_u *ret = NULL;
4102 int var_count = 0;
4103 int semicolon = 0;
4104 size_t varlen;
4105 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004106 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004109 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004111 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 int oplen = 0;
4113 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004114 type_T *type = &t_any;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004115 lvar_T *lvar = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116 char_u *name;
4117 char_u *sp;
4118 int has_type = FALSE;
4119 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
4120 int instr_count = -1;
4121
4122 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
4123 if (p == NULL)
4124 return NULL;
4125 if (var_count > 0)
4126 {
4127 // TODO: let [var, var] = list
4128 emsg("Cannot handle a list yet");
4129 return NULL;
4130 }
4131
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004132 // "a: type" is declaring variable "a" with a type, not "a:".
4133 if (is_decl && p == arg + 2 && p[-1] == ':')
4134 --p;
4135
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 varlen = p - arg;
4137 name = vim_strnsave(arg, (int)varlen);
4138 if (name == NULL)
4139 return NULL;
4140
Bram Moolenaar080457c2020-03-03 21:53:32 +01004141 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004143 if (*arg == '&')
4144 {
4145 int cc;
4146 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147
Bram Moolenaar080457c2020-03-03 21:53:32 +01004148 dest = dest_option;
4149 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004151 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004152 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 if (is_decl)
4155 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004156 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 goto theend;
4158 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004159 p = arg;
4160 p = find_option_end(&p, &opt_flags);
4161 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004163 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01004164 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004165 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004166 }
4167 cc = *p;
4168 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004169 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004170 *p = cc;
4171 if (opt_type == -3)
4172 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004173 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004174 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004175 }
4176 if (opt_type == -2 || opt_type == 0)
4177 type = &t_string;
4178 else
4179 type = &t_number; // both number and boolean option
4180 }
4181 else if (*arg == '$')
4182 {
4183 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004184 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004185 if (is_decl)
4186 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004187 semsg(_("E1065: Cannot declare an environment variable: %s"),
4188 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004189 goto theend;
4190 }
4191 }
4192 else if (*arg == '@')
4193 {
4194 if (!valid_yank_reg(arg[1], TRUE))
4195 {
4196 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004197 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004198 }
4199 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004200 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004201 if (is_decl)
4202 {
4203 semsg(_("E1066: Cannot declare a register: %s"), name);
4204 goto theend;
4205 }
4206 }
4207 else if (STRNCMP(arg, "g:", 2) == 0)
4208 {
4209 dest = dest_global;
4210 if (is_decl)
4211 {
4212 semsg(_("E1016: Cannot declare a global variable: %s"), name);
4213 goto theend;
4214 }
4215 }
Bram Moolenaard3aac292020-04-19 14:32:17 +02004216 else if (STRNCMP(arg, "b:", 2) == 0)
4217 {
4218 dest = dest_buffer;
4219 if (is_decl)
4220 {
4221 semsg(_("E1078: Cannot declare a buffer variable: %s"), name);
4222 goto theend;
4223 }
4224 }
4225 else if (STRNCMP(arg, "w:", 2) == 0)
4226 {
4227 dest = dest_window;
4228 if (is_decl)
4229 {
4230 semsg(_("E1079: Cannot declare a window variable: %s"), name);
4231 goto theend;
4232 }
4233 }
4234 else if (STRNCMP(arg, "t:", 2) == 0)
4235 {
4236 dest = dest_tab;
4237 if (is_decl)
4238 {
4239 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
4240 goto theend;
4241 }
4242 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004243 else if (STRNCMP(arg, "v:", 2) == 0)
4244 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004245 typval_T *vtv;
4246 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004247
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004248 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004249 if (vimvaridx < 0)
4250 {
4251 semsg(_(e_var_notfound), arg);
4252 goto theend;
4253 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004254 // We use the current value of "sandbox" here, is that OK?
4255 if (var_check_ro(di_flags, name, FALSE))
4256 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004257 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004258 vtv = get_vim_var_tv(vimvaridx);
4259 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004260 if (is_decl)
4261 {
4262 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
4263 goto theend;
4264 }
4265 }
4266 else
4267 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004268 int idx;
4269
Bram Moolenaar080457c2020-03-03 21:53:32 +01004270 for (idx = 0; reserved[idx] != NULL; ++idx)
4271 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004273 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 goto theend;
4275 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004276
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004277 lvar = lookup_local(arg, varlen, cctx);
4278 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004280 if (is_decl)
4281 {
4282 semsg(_("E1017: Variable already declared: %s"), name);
4283 goto theend;
4284 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004285 else if (lvar->lv_const)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004286 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004287 semsg(_("E1018: Cannot assign to a constant: %s"), name);
4288 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004289 }
4290 }
4291 else if (STRNCMP(arg, "s:", 2) == 0
4292 || lookup_script(arg, varlen) == OK
4293 || find_imported(arg, varlen, cctx) != NULL)
4294 {
4295 dest = dest_script;
4296 if (is_decl)
4297 {
4298 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004299 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004300 goto theend;
4301 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004303 else if (name[1] == ':' && name[2] != NUL)
4304 {
4305 semsg(_("E1082: Cannot use a namespaced variable: %s"), name);
4306 goto theend;
4307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 }
4309 }
4310
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004311 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 {
4313 if (is_decl && *p == ':')
4314 {
4315 // parse optional type: "let var: type = expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004316 if (!VIM_ISWHITE(p[1]))
4317 {
4318 semsg(_(e_white_after), ":");
4319 goto theend;
4320 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 p = skipwhite(p + 1);
4322 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 has_type = TRUE;
4324 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004325 else if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 type = lvar->lv_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 }
4328
4329 sp = p;
4330 p = skipwhite(p);
4331 op = p;
4332 oplen = assignment_len(p, &heredoc);
4333 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4334 {
4335 char_u buf[4];
4336
4337 vim_strncpy(buf, op, oplen);
4338 semsg(_(e_white_both), buf);
4339 }
4340
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004341 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02004342 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004344 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 goto theend;
4346 }
4347
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004348 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 {
4350 if (oplen > 1 && !heredoc)
4351 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004352 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4354 name);
4355 goto theend;
4356 }
4357
4358 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02004359 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004360 goto theend;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004361 lvar = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4362 if (lvar == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004364 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365 }
4366
4367 if (heredoc)
4368 {
4369 list_T *l;
4370 listitem_T *li;
4371
4372 // [let] varname =<< [trim] {end}
4373 eap->getline = heredoc_getline;
4374 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004375 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376
4377 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004378 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 {
4380 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4381 li->li_tv.vval.v_string = NULL;
4382 }
4383 generate_NEWLIST(cctx, l->lv_len);
4384 type = &t_list_string;
4385 list_free(l);
4386 p += STRLEN(p);
4387 }
4388 else if (oplen > 0)
4389 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004390 int r;
4391 type_T *stacktype;
4392 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 // for "+=", "*=", "..=" etc. first load the current value
4395 if (*op != '=')
4396 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004397 switch (dest)
4398 {
4399 case dest_option:
4400 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004401 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004402 break;
4403 case dest_global:
4404 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4405 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004406 case dest_buffer:
4407 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4408 break;
4409 case dest_window:
4410 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4411 break;
4412 case dest_tab:
4413 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4414 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004415 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01004416 compile_load_scriptvar(cctx,
4417 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004418 break;
4419 case dest_env:
4420 // Include $ in the name here
4421 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4422 break;
4423 case dest_reg:
4424 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
4425 break;
4426 case dest_vimvar:
4427 generate_LOADV(cctx, name + 2, TRUE);
4428 break;
4429 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004430 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004431 break;
4432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 }
4434
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004435 // Compile the expression. Temporarily hide the new local variable
4436 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02004437 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004438 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 instr_count = instr->ga_len;
4440 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004441 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02004442 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004443 ++cctx->ctx_locals.ga_len;
4444 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 goto theend;
4446
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004447 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004449 stack = &cctx->ctx_type_stack;
4450 stacktype = stack->ga_len == 0 ? &t_void
4451 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004452 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004454 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004456 if (stacktype->tt_type == VAR_VOID)
4457 {
4458 emsg(_("E1031: Cannot use void value"));
4459 goto theend;
4460 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004461 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004462 {
4463 // An empty list or dict has a &t_void member, for a
4464 // variable that implies &t_any.
4465 if (stacktype == &t_list_empty)
4466 lvar->lv_type = &t_list_any;
4467 else if (stacktype == &t_dict_empty)
4468 lvar->lv_type = &t_dict_any;
4469 else
4470 lvar->lv_type = stacktype;
4471 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004472 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004473 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
4474 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004476 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004477 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 }
4479 }
4480 else if (cmdidx == CMD_const)
4481 {
4482 emsg(_("E1021: const requires a value"));
4483 goto theend;
4484 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004485 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 {
4487 emsg(_("E1022: type or initialization required"));
4488 goto theend;
4489 }
4490 else
4491 {
4492 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 if (ga_grow(instr, 1) == FAIL)
4494 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004495 switch (type->tt_type)
4496 {
4497 case VAR_BOOL:
4498 generate_PUSHBOOL(cctx, VVAL_FALSE);
4499 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004500 case VAR_FLOAT:
4501#ifdef FEAT_FLOAT
4502 generate_PUSHF(cctx, 0.0);
4503#endif
4504 break;
4505 case VAR_STRING:
4506 generate_PUSHS(cctx, NULL);
4507 break;
4508 case VAR_BLOB:
4509 generate_PUSHBLOB(cctx, NULL);
4510 break;
4511 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004512 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004513 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004514 case VAR_LIST:
4515 generate_NEWLIST(cctx, 0);
4516 break;
4517 case VAR_DICT:
4518 generate_NEWDICT(cctx, 0);
4519 break;
4520 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004521 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004522 break;
4523 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004524 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004525 break;
4526 case VAR_NUMBER:
4527 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004528 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02004529 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01004530 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02004531 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01004532 generate_PUSHNR(cctx, 0);
4533 break;
4534 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 }
4536
4537 if (oplen > 0 && *op != '=')
4538 {
4539 type_T *expected = &t_number;
4540 garray_T *stack = &cctx->ctx_type_stack;
4541 type_T *stacktype;
4542
4543 // TODO: if type is known use float or any operation
4544
4545 if (*op == '.')
4546 expected = &t_string;
4547 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4548 if (need_type(stacktype, expected, -1, cctx) == FAIL)
4549 goto theend;
4550
4551 if (*op == '.')
4552 generate_instr_drop(cctx, ISN_CONCAT, 1);
4553 else
4554 {
4555 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4556
4557 if (isn == NULL)
4558 goto theend;
4559 switch (*op)
4560 {
4561 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
4562 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4563 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4564 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4565 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4566 }
4567 }
4568 }
4569
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004570 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004572 case dest_option:
4573 generate_STOREOPT(cctx, name + 1, opt_flags);
4574 break;
4575 case dest_global:
4576 // include g: with the name, easier to execute that way
4577 generate_STORE(cctx, ISN_STOREG, 0, name);
4578 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004579 case dest_buffer:
4580 // include b: with the name, easier to execute that way
4581 generate_STORE(cctx, ISN_STOREB, 0, name);
4582 break;
4583 case dest_window:
4584 // include w: with the name, easier to execute that way
4585 generate_STORE(cctx, ISN_STOREW, 0, name);
4586 break;
4587 case dest_tab:
4588 // include t: with the name, easier to execute that way
4589 generate_STORE(cctx, ISN_STORET, 0, name);
4590 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004591 case dest_env:
4592 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
4593 break;
4594 case dest_reg:
4595 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
4596 break;
4597 case dest_vimvar:
4598 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
4599 break;
4600 case dest_script:
4601 {
4602 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4603 imported_T *import = NULL;
4604 int sid = current_sctx.sc_sid;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004605 int idx;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004606
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004607 if (name[1] != ':')
4608 {
4609 import = find_imported(name, 0, cctx);
4610 if (import != NULL)
4611 sid = import->imp_sid;
4612 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004614 idx = get_script_item_idx(sid, rawname, TRUE);
4615 // TODO: specific type
4616 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004617 {
4618 char_u *name_s = name;
4619
4620 // Include s: in the name for store_var()
4621 if (name[1] != ':')
4622 {
4623 int len = (int)STRLEN(name) + 3;
4624
4625 name_s = alloc(len);
4626 if (name_s == NULL)
4627 name_s = name;
4628 else
4629 vim_snprintf((char *)name_s, len, "s:%s", name);
4630 }
4631 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
4632 if (name_s != name)
4633 vim_free(name_s);
4634 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004635 else
4636 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
4637 sid, idx, &t_any);
4638 }
4639 break;
4640 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004641 if (lvar != NULL)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004642 {
4643 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004645 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
4646 // into ISN_STORENR
4647 if (instr->ga_len == instr_count + 1
4648 && isn->isn_type == ISN_PUSHNR)
4649 {
4650 varnumber_T val = isn->isn_arg.number;
4651 garray_T *stack = &cctx->ctx_type_stack;
4652
4653 isn->isn_type = ISN_STORENR;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004654 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004655 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004656 if (stack->ga_len > 0)
4657 --stack->ga_len;
4658 }
4659 else
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004660 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004661 }
4662 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663 }
4664 ret = p;
4665
4666theend:
4667 vim_free(name);
4668 return ret;
4669}
4670
4671/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004672 * Check if "name" can be "unlet".
4673 */
4674 int
4675check_vim9_unlet(char_u *name)
4676{
4677 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
4678 {
4679 semsg(_("E1081: Cannot unlet %s"), name);
4680 return FAIL;
4681 }
4682 return OK;
4683}
4684
4685/*
4686 * Callback passed to ex_unletlock().
4687 */
4688 static int
4689compile_unlet(
4690 lval_T *lvp,
4691 char_u *name_end,
4692 exarg_T *eap,
4693 int deep UNUSED,
4694 void *coookie)
4695{
4696 cctx_T *cctx = coookie;
4697
4698 if (lvp->ll_tv == NULL)
4699 {
4700 char_u *p = lvp->ll_name;
4701 int cc = *name_end;
4702 int ret = OK;
4703
4704 // Normal name. Only supports g:, w:, t: and b: namespaces.
4705 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004706 if (*p == '$')
4707 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
4708 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004709 ret = FAIL;
4710 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004711 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004712
4713 *name_end = cc;
4714 return ret;
4715 }
4716
4717 // TODO: unlet {list}[idx]
4718 // TODO: unlet {dict}[key]
4719 emsg("Sorry, :unlet not fully implemented yet");
4720 return FAIL;
4721}
4722
4723/*
4724 * compile "unlet var", "lock var" and "unlock var"
4725 * "arg" points to "var".
4726 */
4727 static char_u *
4728compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
4729{
4730 char_u *p = arg;
4731
4732 if (eap->cmdidx != CMD_unlet)
4733 {
4734 emsg("Sorry, :lock and unlock not implemented yet");
4735 return NULL;
4736 }
4737
4738 if (*p == '!')
4739 {
4740 p = skipwhite(p + 1);
4741 eap->forceit = TRUE;
4742 }
4743
4744 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
4745 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4746}
4747
4748/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 * Compile an :import command.
4750 */
4751 static char_u *
4752compile_import(char_u *arg, cctx_T *cctx)
4753{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01004754 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755}
4756
4757/*
4758 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
4759 */
4760 static int
4761compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
4762{
4763 garray_T *instr = &cctx->ctx_instr;
4764 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
4765
4766 if (endlabel == NULL)
4767 return FAIL;
4768 endlabel->el_next = *el;
4769 *el = endlabel;
4770 endlabel->el_end_label = instr->ga_len;
4771
4772 generate_JUMP(cctx, when, 0);
4773 return OK;
4774}
4775
4776 static void
4777compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
4778{
4779 garray_T *instr = &cctx->ctx_instr;
4780
4781 while (*el != NULL)
4782 {
4783 endlabel_T *cur = (*el);
4784 isn_T *isn;
4785
4786 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
4787 isn->isn_arg.jump.jump_where = instr->ga_len;
4788 *el = cur->el_next;
4789 vim_free(cur);
4790 }
4791}
4792
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004793 static void
4794compile_free_jump_to_end(endlabel_T **el)
4795{
4796 while (*el != NULL)
4797 {
4798 endlabel_T *cur = (*el);
4799
4800 *el = cur->el_next;
4801 vim_free(cur);
4802 }
4803}
4804
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805/*
4806 * Create a new scope and set up the generic items.
4807 */
4808 static scope_T *
4809new_scope(cctx_T *cctx, scopetype_T type)
4810{
4811 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
4812
4813 if (scope == NULL)
4814 return NULL;
4815 scope->se_outer = cctx->ctx_scope;
4816 cctx->ctx_scope = scope;
4817 scope->se_type = type;
4818 scope->se_local_count = cctx->ctx_locals.ga_len;
4819 return scope;
4820}
4821
4822/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004823 * Free the current scope and go back to the outer scope.
4824 */
4825 static void
4826drop_scope(cctx_T *cctx)
4827{
4828 scope_T *scope = cctx->ctx_scope;
4829
4830 if (scope == NULL)
4831 {
4832 iemsg("calling drop_scope() without a scope");
4833 return;
4834 }
4835 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004836 switch (scope->se_type)
4837 {
4838 case IF_SCOPE:
4839 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
4840 case FOR_SCOPE:
4841 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
4842 case WHILE_SCOPE:
4843 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
4844 case TRY_SCOPE:
4845 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
4846 case NO_SCOPE:
4847 case BLOCK_SCOPE:
4848 break;
4849 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004850 vim_free(scope);
4851}
4852
4853/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004854 * Evaluate an expression that is a constant:
4855 * has(arg)
4856 *
4857 * Also handle:
4858 * ! in front logical NOT
4859 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004860 * Return FAIL if the expression is not a constant.
4861 */
4862 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004863evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004864{
4865 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004866 char_u *start_leader, *end_leader;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004867 int has_call = FALSE;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004868
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004869 /*
4870 * Skip '!' characters. They are handled later.
4871 */
4872 start_leader = *arg;
4873 while (**arg == '!')
4874 *arg = skipwhite(*arg + 1);
4875 end_leader = *arg;
4876
4877 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004878 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004879 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004880 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4881 {
4882 tv->v_type = VAR_SPECIAL;
4883 tv->vval.v_number = VVAL_TRUE;
4884 *arg += 4;
4885 return OK;
4886 }
4887 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4888 {
4889 tv->v_type = VAR_SPECIAL;
4890 tv->vval.v_number = VVAL_FALSE;
4891 *arg += 5;
4892 return OK;
4893 }
4894
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004895 if (STRNCMP("has(", *arg, 4) == 0)
4896 {
4897 has_call = TRUE;
4898 *arg = skipwhite(*arg + 4);
4899 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004900
4901 if (**arg == '"')
4902 {
4903 if (get_string_tv(arg, tv, TRUE) == FAIL)
4904 return FAIL;
4905 }
4906 else if (**arg == '\'')
4907 {
4908 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4909 return FAIL;
4910 }
4911 else
4912 return FAIL;
4913
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004914 if (has_call)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004915 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004916 *arg = skipwhite(*arg);
4917 if (**arg != ')')
4918 return FAIL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004919 *arg = *arg + 1;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004920
4921 argvars[0] = *tv;
4922 argvars[1].v_type = VAR_UNKNOWN;
4923 tv->v_type = VAR_NUMBER;
4924 tv->vval.v_number = 0;
4925 f_has(argvars, tv);
4926 clear_tv(&argvars[0]);
4927
4928 while (start_leader < end_leader)
4929 {
4930 if (*start_leader == '!')
4931 tv->vval.v_number = !tv->vval.v_number;
4932 ++start_leader;
4933 }
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004934 }
4935
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004936 return OK;
4937}
4938
Bram Moolenaar080457c2020-03-03 21:53:32 +01004939 static int
4940evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4941{
4942 exptype_T type = EXPR_UNKNOWN;
4943 char_u *p;
4944 int len = 2;
4945 int type_is = FALSE;
4946
4947 // get the first variable
4948 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4949 return FAIL;
4950
4951 p = skipwhite(*arg);
4952 type = get_compare_type(p, &len, &type_is);
4953
4954 /*
4955 * If there is a comparative operator, use it.
4956 */
4957 if (type != EXPR_UNKNOWN)
4958 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004959 typval_T tv2;
4960 char_u *s1, *s2;
4961 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4962 int n;
4963
4964 // TODO: Only string == string is supported now
4965 if (tv->v_type != VAR_STRING)
4966 return FAIL;
4967 if (type != EXPR_EQUAL)
4968 return FAIL;
4969
4970 // get the second variable
Bram Moolenaar4227c782020-04-02 16:00:04 +02004971 init_tv(&tv2);
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004972 *arg = skipwhite(p + len);
4973 if (evaluate_const_expr7(arg, cctx, &tv2) == FAIL
4974 || tv2.v_type != VAR_STRING)
4975 {
4976 clear_tv(&tv2);
4977 return FAIL;
4978 }
4979 s1 = tv_get_string_buf(tv, buf1);
4980 s2 = tv_get_string_buf(&tv2, buf2);
4981 n = STRCMP(s1, s2);
4982 clear_tv(tv);
4983 clear_tv(&tv2);
4984 tv->v_type = VAR_BOOL;
4985 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004986 }
4987
4988 return OK;
4989}
4990
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004991static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
4992
4993/*
4994 * Compile constant || or &&.
4995 */
4996 static int
4997evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
4998{
4999 char_u *p = skipwhite(*arg);
5000 int opchar = *op;
5001
5002 if (p[0] == opchar && p[1] == opchar)
5003 {
5004 int val = tv2bool(tv);
5005
5006 /*
5007 * Repeat until there is no following "||" or "&&"
5008 */
5009 while (p[0] == opchar && p[1] == opchar)
5010 {
5011 typval_T tv2;
5012
5013 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
5014 return FAIL;
5015
5016 // eval the next expression
5017 *arg = skipwhite(p + 2);
5018 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01005019 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005020 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01005021 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005022 {
5023 clear_tv(&tv2);
5024 return FAIL;
5025 }
5026 if ((opchar == '&') == val)
5027 {
5028 // false || tv2 or true && tv2: use tv2
5029 clear_tv(tv);
5030 *tv = tv2;
5031 val = tv2bool(tv);
5032 }
5033 else
5034 clear_tv(&tv2);
5035 p = skipwhite(*arg);
5036 }
5037 }
5038
5039 return OK;
5040}
5041
5042/*
5043 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
5044 * Return FAIL if the expression is not a constant.
5045 */
5046 static int
5047evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
5048{
5049 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01005050 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005051 return FAIL;
5052
5053 // || and && work almost the same
5054 return evaluate_const_and_or(arg, cctx, "&&", tv);
5055}
5056
5057/*
5058 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
5059 * Return FAIL if the expression is not a constant.
5060 */
5061 static int
5062evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
5063{
5064 // evaluate the first expression
5065 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
5066 return FAIL;
5067
5068 // || and && work almost the same
5069 return evaluate_const_and_or(arg, cctx, "||", tv);
5070}
5071
5072/*
5073 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
5074 * E.g. for "has('feature')".
5075 * This does not produce error messages. "tv" should be cleared afterwards.
5076 * Return FAIL if the expression is not a constant.
5077 */
5078 static int
5079evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
5080{
5081 char_u *p;
5082
5083 // evaluate the first expression
5084 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
5085 return FAIL;
5086
5087 p = skipwhite(*arg);
5088 if (*p == '?')
5089 {
5090 int val = tv2bool(tv);
5091 typval_T tv2;
5092
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005093 // require space before and after the ?
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005094 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
5095 return FAIL;
5096
5097 // evaluate the second expression; any type is accepted
5098 clear_tv(tv);
5099 *arg = skipwhite(p + 1);
5100 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
5101 return FAIL;
5102
5103 // Check for the ":".
5104 p = skipwhite(*arg);
5105 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
5106 return FAIL;
5107
5108 // evaluate the third expression
5109 *arg = skipwhite(p + 1);
5110 tv2.v_type = VAR_UNKNOWN;
5111 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
5112 {
5113 clear_tv(&tv2);
5114 return FAIL;
5115 }
5116 if (val)
5117 {
5118 // use the expr after "?"
5119 clear_tv(&tv2);
5120 }
5121 else
5122 {
5123 // use the expr after ":"
5124 clear_tv(tv);
5125 *tv = tv2;
5126 }
5127 }
5128 return OK;
5129}
5130
5131/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005132 * compile "if expr"
5133 *
5134 * "if expr" Produces instructions:
5135 * EVAL expr Push result of "expr"
5136 * JUMP_IF_FALSE end
5137 * ... body ...
5138 * end:
5139 *
5140 * "if expr | else" Produces instructions:
5141 * EVAL expr Push result of "expr"
5142 * JUMP_IF_FALSE else
5143 * ... body ...
5144 * JUMP_ALWAYS end
5145 * else:
5146 * ... body ...
5147 * end:
5148 *
5149 * "if expr1 | elseif expr2 | else" Produces instructions:
5150 * EVAL expr Push result of "expr"
5151 * JUMP_IF_FALSE elseif
5152 * ... body ...
5153 * JUMP_ALWAYS end
5154 * elseif:
5155 * EVAL expr Push result of "expr"
5156 * JUMP_IF_FALSE else
5157 * ... body ...
5158 * JUMP_ALWAYS end
5159 * else:
5160 * ... body ...
5161 * end:
5162 */
5163 static char_u *
5164compile_if(char_u *arg, cctx_T *cctx)
5165{
5166 char_u *p = arg;
5167 garray_T *instr = &cctx->ctx_instr;
5168 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005169 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005170
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005171 // compile "expr"; if we know it evaluates to FALSE skip the block
5172 tv.v_type = VAR_UNKNOWN;
5173 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5174 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5175 else
5176 cctx->ctx_skip = MAYBE;
5177 clear_tv(&tv);
5178 if (cctx->ctx_skip == MAYBE)
5179 {
5180 p = arg;
5181 if (compile_expr1(&p, cctx) == FAIL)
5182 return NULL;
5183 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005184
5185 scope = new_scope(cctx, IF_SCOPE);
5186 if (scope == NULL)
5187 return NULL;
5188
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005189 if (cctx->ctx_skip == MAYBE)
5190 {
5191 // "where" is set when ":elseif", "else" or ":endif" is found
5192 scope->se_u.se_if.is_if_label = instr->ga_len;
5193 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5194 }
5195 else
5196 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197
5198 return p;
5199}
5200
5201 static char_u *
5202compile_elseif(char_u *arg, cctx_T *cctx)
5203{
5204 char_u *p = arg;
5205 garray_T *instr = &cctx->ctx_instr;
5206 isn_T *isn;
5207 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005208 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209
5210 if (scope == NULL || scope->se_type != IF_SCOPE)
5211 {
5212 emsg(_(e_elseif_without_if));
5213 return NULL;
5214 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005215 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216
Bram Moolenaar158906c2020-02-06 20:39:45 +01005217 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005218 {
5219 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005221 return NULL;
5222 // previous "if" or "elseif" jumps here
5223 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5224 isn->isn_arg.jump.jump_where = instr->ga_len;
5225 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005226
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005227 // compile "expr"; if we know it evaluates to FALSE skip the block
5228 tv.v_type = VAR_UNKNOWN;
5229 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5230 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5231 else
5232 cctx->ctx_skip = MAYBE;
5233 clear_tv(&tv);
5234 if (cctx->ctx_skip == MAYBE)
5235 {
5236 p = arg;
5237 if (compile_expr1(&p, cctx) == FAIL)
5238 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005240 // "where" is set when ":elseif", "else" or ":endif" is found
5241 scope->se_u.se_if.is_if_label = instr->ga_len;
5242 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5243 }
5244 else
5245 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005246
5247 return p;
5248}
5249
5250 static char_u *
5251compile_else(char_u *arg, cctx_T *cctx)
5252{
5253 char_u *p = arg;
5254 garray_T *instr = &cctx->ctx_instr;
5255 isn_T *isn;
5256 scope_T *scope = cctx->ctx_scope;
5257
5258 if (scope == NULL || scope->se_type != IF_SCOPE)
5259 {
5260 emsg(_(e_else_without_if));
5261 return NULL;
5262 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005263 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005264
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005265 // jump from previous block to the end, unless the else block is empty
5266 if (cctx->ctx_skip == MAYBE)
5267 {
5268 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005269 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005270 return NULL;
5271 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005272
Bram Moolenaar158906c2020-02-06 20:39:45 +01005273 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005274 {
5275 if (scope->se_u.se_if.is_if_label >= 0)
5276 {
5277 // previous "if" or "elseif" jumps here
5278 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5279 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005280 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005281 }
5282 }
5283
5284 if (cctx->ctx_skip != MAYBE)
5285 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005286
5287 return p;
5288}
5289
5290 static char_u *
5291compile_endif(char_u *arg, cctx_T *cctx)
5292{
5293 scope_T *scope = cctx->ctx_scope;
5294 ifscope_T *ifscope;
5295 garray_T *instr = &cctx->ctx_instr;
5296 isn_T *isn;
5297
5298 if (scope == NULL || scope->se_type != IF_SCOPE)
5299 {
5300 emsg(_(e_endif_without_if));
5301 return NULL;
5302 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005303 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005304 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005305
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005306 if (scope->se_u.se_if.is_if_label >= 0)
5307 {
5308 // previous "if" or "elseif" jumps here
5309 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5310 isn->isn_arg.jump.jump_where = instr->ga_len;
5311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005312 // Fill in the "end" label in jumps at the end of the blocks.
5313 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005314 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005315
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005316 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005317 return arg;
5318}
5319
5320/*
5321 * compile "for var in expr"
5322 *
5323 * Produces instructions:
5324 * PUSHNR -1
5325 * STORE loop-idx Set index to -1
5326 * EVAL expr Push result of "expr"
5327 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5328 * - if beyond end, jump to "end"
5329 * - otherwise get item from list and push it
5330 * STORE var Store item in "var"
5331 * ... body ...
5332 * JUMP top Jump back to repeat
5333 * end: DROP Drop the result of "expr"
5334 *
5335 */
5336 static char_u *
5337compile_for(char_u *arg, cctx_T *cctx)
5338{
5339 char_u *p;
5340 size_t varlen;
5341 garray_T *instr = &cctx->ctx_instr;
5342 garray_T *stack = &cctx->ctx_type_stack;
5343 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005344 lvar_T *loop_lvar; // loop iteration variable
5345 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005346 type_T *vartype;
5347
5348 // TODO: list of variables: "for [key, value] in dict"
5349 // parse "var"
5350 for (p = arg; eval_isnamec1(*p); ++p)
5351 ;
5352 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005353 var_lvar = lookup_local(arg, varlen, cctx);
5354 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005355 {
5356 semsg(_("E1023: variable already defined: %s"), arg);
5357 return NULL;
5358 }
5359
5360 // consume "in"
5361 p = skipwhite(p);
5362 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5363 {
5364 emsg(_(e_missing_in));
5365 return NULL;
5366 }
5367 p = skipwhite(p + 2);
5368
5369
5370 scope = new_scope(cctx, FOR_SCOPE);
5371 if (scope == NULL)
5372 return NULL;
5373
5374 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005375 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5376 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005377 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005378 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005379 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005380 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005381 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005382
5383 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005384 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5385 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005386 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005387 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005388 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005389 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005390 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005391
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005392 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005393
5394 // compile "expr", it remains on the stack until "endfor"
5395 arg = p;
5396 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005397 {
5398 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005399 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005400 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005401
5402 // now we know the type of "var"
5403 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5404 if (vartype->tt_type != VAR_LIST)
5405 {
5406 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005407 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005408 return NULL;
5409 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005410 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005411 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005412
5413 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005414 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005415
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005416 generate_FOR(cctx, loop_lvar->lv_idx);
5417 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005418
5419 return arg;
5420}
5421
5422/*
5423 * compile "endfor"
5424 */
5425 static char_u *
5426compile_endfor(char_u *arg, cctx_T *cctx)
5427{
5428 garray_T *instr = &cctx->ctx_instr;
5429 scope_T *scope = cctx->ctx_scope;
5430 forscope_T *forscope;
5431 isn_T *isn;
5432
5433 if (scope == NULL || scope->se_type != FOR_SCOPE)
5434 {
5435 emsg(_(e_for));
5436 return NULL;
5437 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005438 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005439 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005440 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005441
5442 // At end of ":for" scope jump back to the FOR instruction.
5443 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5444
5445 // Fill in the "end" label in the FOR statement so it can jump here
5446 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5447 isn->isn_arg.forloop.for_end = instr->ga_len;
5448
5449 // Fill in the "end" label any BREAK statements
5450 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5451
5452 // Below the ":for" scope drop the "expr" list from the stack.
5453 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5454 return NULL;
5455
5456 vim_free(scope);
5457
5458 return arg;
5459}
5460
5461/*
5462 * compile "while expr"
5463 *
5464 * Produces instructions:
5465 * top: EVAL expr Push result of "expr"
5466 * JUMP_IF_FALSE end jump if false
5467 * ... body ...
5468 * JUMP top Jump back to repeat
5469 * end:
5470 *
5471 */
5472 static char_u *
5473compile_while(char_u *arg, cctx_T *cctx)
5474{
5475 char_u *p = arg;
5476 garray_T *instr = &cctx->ctx_instr;
5477 scope_T *scope;
5478
5479 scope = new_scope(cctx, WHILE_SCOPE);
5480 if (scope == NULL)
5481 return NULL;
5482
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005483 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005484
5485 // compile "expr"
5486 if (compile_expr1(&p, cctx) == FAIL)
5487 return NULL;
5488
5489 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005490 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005491 JUMP_IF_FALSE, cctx) == FAIL)
5492 return FAIL;
5493
5494 return p;
5495}
5496
5497/*
5498 * compile "endwhile"
5499 */
5500 static char_u *
5501compile_endwhile(char_u *arg, cctx_T *cctx)
5502{
5503 scope_T *scope = cctx->ctx_scope;
5504
5505 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5506 {
5507 emsg(_(e_while));
5508 return NULL;
5509 }
5510 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005511 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005512
5513 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005514 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005515
5516 // Fill in the "end" label in the WHILE statement so it can jump here.
5517 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005518 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005519
5520 vim_free(scope);
5521
5522 return arg;
5523}
5524
5525/*
5526 * compile "continue"
5527 */
5528 static char_u *
5529compile_continue(char_u *arg, cctx_T *cctx)
5530{
5531 scope_T *scope = cctx->ctx_scope;
5532
5533 for (;;)
5534 {
5535 if (scope == NULL)
5536 {
5537 emsg(_(e_continue));
5538 return NULL;
5539 }
5540 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5541 break;
5542 scope = scope->se_outer;
5543 }
5544
5545 // Jump back to the FOR or WHILE instruction.
5546 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005547 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5548 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005549 return arg;
5550}
5551
5552/*
5553 * compile "break"
5554 */
5555 static char_u *
5556compile_break(char_u *arg, cctx_T *cctx)
5557{
5558 scope_T *scope = cctx->ctx_scope;
5559 endlabel_T **el;
5560
5561 for (;;)
5562 {
5563 if (scope == NULL)
5564 {
5565 emsg(_(e_break));
5566 return NULL;
5567 }
5568 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5569 break;
5570 scope = scope->se_outer;
5571 }
5572
5573 // Jump to the end of the FOR or WHILE loop.
5574 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005575 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005576 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005577 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005578 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5579 return FAIL;
5580
5581 return arg;
5582}
5583
5584/*
5585 * compile "{" start of block
5586 */
5587 static char_u *
5588compile_block(char_u *arg, cctx_T *cctx)
5589{
5590 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5591 return NULL;
5592 return skipwhite(arg + 1);
5593}
5594
5595/*
5596 * compile end of block: drop one scope
5597 */
5598 static void
5599compile_endblock(cctx_T *cctx)
5600{
5601 scope_T *scope = cctx->ctx_scope;
5602
5603 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005604 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605 vim_free(scope);
5606}
5607
5608/*
5609 * compile "try"
5610 * Creates a new scope for the try-endtry, pointing to the first catch and
5611 * finally.
5612 * Creates another scope for the "try" block itself.
5613 * TRY instruction sets up exception handling at runtime.
5614 *
5615 * "try"
5616 * TRY -> catch1, -> finally push trystack entry
5617 * ... try block
5618 * "throw {exception}"
5619 * EVAL {exception}
5620 * THROW create exception
5621 * ... try block
5622 * " catch {expr}"
5623 * JUMP -> finally
5624 * catch1: PUSH exeception
5625 * EVAL {expr}
5626 * MATCH
5627 * JUMP nomatch -> catch2
5628 * CATCH remove exception
5629 * ... catch block
5630 * " catch"
5631 * JUMP -> finally
5632 * catch2: CATCH remove exception
5633 * ... catch block
5634 * " finally"
5635 * finally:
5636 * ... finally block
5637 * " endtry"
5638 * ENDTRY pop trystack entry, may rethrow
5639 */
5640 static char_u *
5641compile_try(char_u *arg, cctx_T *cctx)
5642{
5643 garray_T *instr = &cctx->ctx_instr;
5644 scope_T *try_scope;
5645 scope_T *scope;
5646
5647 // scope that holds the jumps that go to catch/finally/endtry
5648 try_scope = new_scope(cctx, TRY_SCOPE);
5649 if (try_scope == NULL)
5650 return NULL;
5651
5652 // "catch" is set when the first ":catch" is found.
5653 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005654 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005655 if (generate_instr(cctx, ISN_TRY) == NULL)
5656 return NULL;
5657
5658 // scope for the try block itself
5659 scope = new_scope(cctx, BLOCK_SCOPE);
5660 if (scope == NULL)
5661 return NULL;
5662
5663 return arg;
5664}
5665
5666/*
5667 * compile "catch {expr}"
5668 */
5669 static char_u *
5670compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5671{
5672 scope_T *scope = cctx->ctx_scope;
5673 garray_T *instr = &cctx->ctx_instr;
5674 char_u *p;
5675 isn_T *isn;
5676
5677 // end block scope from :try or :catch
5678 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5679 compile_endblock(cctx);
5680 scope = cctx->ctx_scope;
5681
5682 // Error if not in a :try scope
5683 if (scope == NULL || scope->se_type != TRY_SCOPE)
5684 {
5685 emsg(_(e_catch));
5686 return NULL;
5687 }
5688
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005689 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005690 {
5691 emsg(_("E1033: catch unreachable after catch-all"));
5692 return NULL;
5693 }
5694
5695 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005696 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005697 JUMP_ALWAYS, cctx) == FAIL)
5698 return NULL;
5699
5700 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005701 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005702 if (isn->isn_arg.try.try_catch == 0)
5703 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005704 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005705 {
5706 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005707 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005708 isn->isn_arg.jump.jump_where = instr->ga_len;
5709 }
5710
5711 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02005712 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005713 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005714 scope->se_u.se_try.ts_caught_all = TRUE;
5715 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005716 }
5717 else
5718 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005719 char_u *end;
5720 char_u *pat;
5721 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005722 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005723 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005725 // Push v:exception, push {expr} and MATCH
5726 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5727
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005728 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005729 if (*end != *p)
5730 {
5731 semsg(_("E1067: Separator mismatch: %s"), p);
5732 vim_free(tofree);
5733 return FAIL;
5734 }
5735 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005736 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005737 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005738 len = (int)(end - tofree);
5739 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005740 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005741 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005742 if (pat == NULL)
5743 return FAIL;
5744 if (generate_PUSHS(cctx, pat) == FAIL)
5745 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005747 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5748 return NULL;
5749
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005750 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005751 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5752 return NULL;
5753 }
5754
5755 if (generate_instr(cctx, ISN_CATCH) == NULL)
5756 return NULL;
5757
5758 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5759 return NULL;
5760 return p;
5761}
5762
5763 static char_u *
5764compile_finally(char_u *arg, cctx_T *cctx)
5765{
5766 scope_T *scope = cctx->ctx_scope;
5767 garray_T *instr = &cctx->ctx_instr;
5768 isn_T *isn;
5769
5770 // end block scope from :try or :catch
5771 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5772 compile_endblock(cctx);
5773 scope = cctx->ctx_scope;
5774
5775 // Error if not in a :try scope
5776 if (scope == NULL || scope->se_type != TRY_SCOPE)
5777 {
5778 emsg(_(e_finally));
5779 return NULL;
5780 }
5781
5782 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005783 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005784 if (isn->isn_arg.try.try_finally != 0)
5785 {
5786 emsg(_(e_finally_dup));
5787 return NULL;
5788 }
5789
5790 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005791 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005792
Bram Moolenaar585fea72020-04-02 22:33:21 +02005793 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005794 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005795 {
5796 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005797 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005798 isn->isn_arg.jump.jump_where = instr->ga_len;
5799 }
5800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005801 // TODO: set index in ts_finally_label jumps
5802
5803 return arg;
5804}
5805
5806 static char_u *
5807compile_endtry(char_u *arg, cctx_T *cctx)
5808{
5809 scope_T *scope = cctx->ctx_scope;
5810 garray_T *instr = &cctx->ctx_instr;
5811 isn_T *isn;
5812
5813 // end block scope from :catch or :finally
5814 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5815 compile_endblock(cctx);
5816 scope = cctx->ctx_scope;
5817
5818 // Error if not in a :try scope
5819 if (scope == NULL || scope->se_type != TRY_SCOPE)
5820 {
5821 if (scope == NULL)
5822 emsg(_(e_no_endtry));
5823 else if (scope->se_type == WHILE_SCOPE)
5824 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01005825 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005826 emsg(_(e_endfor));
5827 else
5828 emsg(_(e_endif));
5829 return NULL;
5830 }
5831
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005832 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005833 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
5834 {
5835 emsg(_("E1032: missing :catch or :finally"));
5836 return NULL;
5837 }
5838
5839 // Fill in the "end" label in jumps at the end of the blocks, if not done
5840 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005841 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005842
5843 // End :catch or :finally scope: set value in ISN_TRY instruction
5844 if (isn->isn_arg.try.try_finally == 0)
5845 isn->isn_arg.try.try_finally = instr->ga_len;
5846 compile_endblock(cctx);
5847
5848 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
5849 return NULL;
5850 return arg;
5851}
5852
5853/*
5854 * compile "throw {expr}"
5855 */
5856 static char_u *
5857compile_throw(char_u *arg, cctx_T *cctx UNUSED)
5858{
5859 char_u *p = skipwhite(arg);
5860
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005861 if (compile_expr1(&p, cctx) == FAIL)
5862 return NULL;
5863 if (may_generate_2STRING(-1, cctx) == FAIL)
5864 return NULL;
5865 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5866 return NULL;
5867
5868 return p;
5869}
5870
5871/*
5872 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005873 * compile "echomsg expr"
5874 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01005875 * compile "execute expr"
5876 */
5877 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005878compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01005879{
5880 char_u *p = arg;
5881 int count = 0;
5882
5883 for (;;)
5884 {
5885 if (compile_expr1(&p, cctx) == FAIL)
5886 return NULL;
5887 ++count;
5888 p = skipwhite(p);
5889 if (ends_excmd(*p))
5890 break;
5891 }
5892
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005893 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
5894 generate_ECHO(cctx, cmdidx == CMD_echo, count);
5895 else if (cmdidx == CMD_execute)
5896 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
5897 else if (cmdidx == CMD_echomsg)
5898 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
5899 else
5900 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005901 return p;
5902}
5903
5904/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005905 * A command that is not compiled, execute with legacy code.
5906 */
5907 static char_u *
5908compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
5909{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005910 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02005911 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005912
5913 if (cctx->ctx_skip == TRUE)
5914 goto theend;
5915
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02005916 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
5917 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005918 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
5919 {
5920 // expand filename in "syntax include [@group] filename"
5921 has_expr = TRUE;
5922 eap->arg = skipwhite(eap->arg + 7);
5923 if (*eap->arg == '@')
5924 eap->arg = skiptowhite(eap->arg);
5925 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005926
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005927 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005928 {
5929 int count = 0;
5930 char_u *start = skipwhite(line);
5931
5932 // :cmd xxx`=expr1`yyy`=expr2`zzz
5933 // PUSHS ":cmd xxx"
5934 // eval expr1
5935 // PUSHS "yyy"
5936 // eval expr2
5937 // PUSHS "zzz"
5938 // EXECCONCAT 5
5939 for (;;)
5940 {
5941 if (p > start)
5942 {
5943 generate_PUSHS(cctx, vim_strnsave(start, (int)(p - start)));
5944 ++count;
5945 }
5946 p += 2;
5947 if (compile_expr1(&p, cctx) == FAIL)
5948 return NULL;
5949 may_generate_2STRING(-1, cctx);
5950 ++count;
5951 p = skipwhite(p);
5952 if (*p != '`')
5953 {
5954 emsg(_("E1083: missing backtick"));
5955 return NULL;
5956 }
5957 start = p + 1;
5958
5959 p = (char_u *)strstr((char *)start, "`=");
5960 if (p == NULL)
5961 {
5962 if (*skipwhite(start) != NUL)
5963 {
5964 generate_PUSHS(cctx, vim_strsave(start));
5965 ++count;
5966 }
5967 break;
5968 }
5969 }
5970 generate_EXECCONCAT(cctx, count);
5971 }
5972 else
5973 generate_EXEC(cctx, line);
5974
5975theend:
5976 return (char_u *)"";
5977}
5978
5979/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005980 * After ex_function() has collected all the function lines: parse and compile
5981 * the lines into instructions.
5982 * Adds the function to "def_functions".
5983 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
5984 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02005985 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005986 * This can be used recursively through compile_lambda(), which may reallocate
5987 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005988 */
5989 void
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02005990compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005991{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005992 char_u *line = NULL;
5993 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005994 char *errormsg = NULL; // error message
5995 int had_return = FALSE;
5996 cctx_T cctx;
5997 garray_T *instr;
5998 int called_emsg_before = called_emsg;
5999 int ret = FAIL;
6000 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006001 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006003 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006004 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01006005
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006006 if (ufunc->uf_dfunc_idx >= 0)
6007 {
6008 // Redefining a function that was compiled before.
6009 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
6010
6011 // Free old instructions.
6012 delete_def_function_contents(dfunc);
6013 }
6014 else
6015 {
6016 // Add the function to "def_functions".
6017 if (ga_grow(&def_functions, 1) == FAIL)
6018 return;
6019 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006020 CLEAR_POINTER(dfunc);
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006021 dfunc->df_idx = def_functions.ga_len;
6022 ufunc->uf_dfunc_idx = dfunc->df_idx;
6023 dfunc->df_ufunc = ufunc;
6024 ++def_functions.ga_len;
6025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026 }
6027
Bram Moolenaara80faa82020-04-12 19:37:17 +02006028 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006029 cctx.ctx_ufunc = ufunc;
6030 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006031 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006032 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6033 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6034 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6035 cctx.ctx_type_list = &ufunc->uf_type_list;
6036 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6037 instr = &cctx.ctx_instr;
6038
6039 // Most modern script version.
6040 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6041
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006042 if (ufunc->uf_def_args.ga_len > 0)
6043 {
6044 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006045 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006046 int i;
6047 char_u *arg;
6048 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6049
6050 // Produce instructions for the default values of optional arguments.
6051 // Store the instruction index in uf_def_arg_idx[] so that we know
6052 // where to start when the function is called, depending on the number
6053 // of arguments.
6054 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6055 if (ufunc->uf_def_arg_idx == NULL)
6056 goto erret;
6057 for (i = 0; i < count; ++i)
6058 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006059 garray_T *stack = &cctx.ctx_type_stack;
6060 type_T *val_type;
6061 int arg_idx = first_def_arg + i;
6062
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006063 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6064 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006065 if (compile_expr1(&arg, &cctx) == FAIL)
6066 goto erret;
6067
6068 // If no type specified use the type of the default value.
6069 // Otherwise check that the default value type matches the
6070 // specified type.
6071 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6072 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6073 ufunc->uf_arg_types[arg_idx] = val_type;
6074 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6075 == FAIL)
6076 {
6077 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6078 arg_idx + 1);
6079 goto erret;
6080 }
6081
6082 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006083 goto erret;
6084 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006085 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6086 }
6087
6088 /*
6089 * Loop over all the lines of the function and generate instructions.
6090 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006091 for (;;)
6092 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006093 exarg_T ea;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006094 int is_ex_command = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006095
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006096 // Bail out on the first error to avoid a flood of errors and report
6097 // the right line number when inside try/catch.
6098 if (emsg_before != called_emsg)
6099 goto erret;
6100
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006101 if (line != NULL && *line == '|')
6102 // the line continues after a '|'
6103 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006104 else if (line != NULL && *line != NUL
6105 && !(*line == '#' && (line == cctx.ctx_line_start
6106 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006107 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006108 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006109 goto erret;
6110 }
6111 else
6112 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006113 line = next_line_from_context(&cctx);
6114 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006115 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006116 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006117 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006118 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006119
6120 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006121 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006122 ea.cmdlinep = &line;
6123 ea.cmd = skipwhite(line);
6124
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006125 // Some things can be recognized by the first character.
6126 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006127 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006128 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006129 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006130 if (ea.cmd[1] != '{')
6131 {
6132 line = (char_u *)"";
6133 continue;
6134 }
6135 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006136
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006137 case '}':
6138 {
6139 // "}" ends a block scope
6140 scopetype_T stype = cctx.ctx_scope == NULL
6141 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006142
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006143 if (stype == BLOCK_SCOPE)
6144 {
6145 compile_endblock(&cctx);
6146 line = ea.cmd;
6147 }
6148 else
6149 {
6150 emsg(_("E1025: using } outside of a block scope"));
6151 goto erret;
6152 }
6153 if (line != NULL)
6154 line = skipwhite(ea.cmd + 1);
6155 continue;
6156 }
6157
6158 case '{':
6159 // "{" starts a block scope
6160 // "{'a': 1}->func() is something else
6161 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6162 {
6163 line = compile_block(ea.cmd, &cctx);
6164 continue;
6165 }
6166 break;
6167
6168 case ':':
6169 is_ex_command = TRUE;
6170 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006171 }
6172
6173 /*
6174 * COMMAND MODIFIERS
6175 */
6176 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6177 {
6178 if (errormsg != NULL)
6179 goto erret;
6180 // empty line or comment
6181 line = (char_u *)"";
6182 continue;
6183 }
6184
6185 // Skip ":call" to get to the function name.
6186 if (checkforcmd(&ea.cmd, "call", 3))
6187 ea.cmd = skipwhite(ea.cmd);
6188
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006189 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006190 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006191 // Assuming the command starts with a variable or function name,
6192 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
6193 // val".
6194 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
6195 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006196 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006197 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006198 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006199 int oplen;
6200 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006201
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006202 oplen = assignment_len(skipwhite(p), &heredoc);
6203 if (oplen > 0)
6204 {
6205 // Recognize an assignment if we recognize the variable
6206 // name:
6207 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006208 // "local = expr" where "local" is a local var.
6209 // "script = expr" where "script" is a script-local var.
6210 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006211 // "&opt = expr"
6212 // "$ENV = expr"
6213 // "@r = expr"
6214 if (*ea.cmd == '&'
6215 || *ea.cmd == '$'
6216 || *ea.cmd == '@'
6217 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006218 || lookup_local(ea.cmd, p - ea.cmd, &cctx) != NULL
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006219 || lookup_script(ea.cmd, p - ea.cmd) == OK
6220 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
6221 {
6222 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6223 if (line == NULL)
6224 goto erret;
6225 continue;
6226 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006227 }
6228 }
6229 }
6230
6231 /*
6232 * COMMAND after range
6233 */
6234 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006235 p = find_ex_command(&ea, NULL, is_ex_command ? NULL
6236 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006237 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006238
6239 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6240 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006241 if (cctx.ctx_skip == TRUE)
6242 {
6243 line += STRLEN(line);
6244 continue;
6245 }
6246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006247 // Expression or function call.
6248 if (ea.cmdidx == CMD_eval)
6249 {
6250 p = ea.cmd;
6251 if (compile_expr1(&p, &cctx) == FAIL)
6252 goto erret;
6253
6254 // drop the return value
6255 generate_instr_drop(&cctx, ISN_DROP, 1);
6256 line = p;
6257 continue;
6258 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006259 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006260 iemsg("Command from find_ex_command() not handled");
6261 goto erret;
6262 }
6263
6264 p = skipwhite(p);
6265
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006266 if (cctx.ctx_skip == TRUE
6267 && ea.cmdidx != CMD_elseif
6268 && ea.cmdidx != CMD_else
6269 && ea.cmdidx != CMD_endif)
6270 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006271 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006272 continue;
6273 }
6274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006275 switch (ea.cmdidx)
6276 {
6277 case CMD_def:
6278 case CMD_function:
6279 // TODO: Nested function
6280 emsg("Nested function not implemented yet");
6281 goto erret;
6282
6283 case CMD_return:
6284 line = compile_return(p, set_return_type, &cctx);
6285 had_return = TRUE;
6286 break;
6287
6288 case CMD_let:
6289 case CMD_const:
6290 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
6291 break;
6292
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006293 case CMD_unlet:
6294 case CMD_unlockvar:
6295 case CMD_lockvar:
6296 line = compile_unletlock(p, &ea, &cctx);
6297 break;
6298
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006299 case CMD_import:
6300 line = compile_import(p, &cctx);
6301 break;
6302
6303 case CMD_if:
6304 line = compile_if(p, &cctx);
6305 break;
6306 case CMD_elseif:
6307 line = compile_elseif(p, &cctx);
6308 break;
6309 case CMD_else:
6310 line = compile_else(p, &cctx);
6311 break;
6312 case CMD_endif:
6313 line = compile_endif(p, &cctx);
6314 break;
6315
6316 case CMD_while:
6317 line = compile_while(p, &cctx);
6318 break;
6319 case CMD_endwhile:
6320 line = compile_endwhile(p, &cctx);
6321 break;
6322
6323 case CMD_for:
6324 line = compile_for(p, &cctx);
6325 break;
6326 case CMD_endfor:
6327 line = compile_endfor(p, &cctx);
6328 break;
6329 case CMD_continue:
6330 line = compile_continue(p, &cctx);
6331 break;
6332 case CMD_break:
6333 line = compile_break(p, &cctx);
6334 break;
6335
6336 case CMD_try:
6337 line = compile_try(p, &cctx);
6338 break;
6339 case CMD_catch:
6340 line = compile_catch(p, &cctx);
6341 break;
6342 case CMD_finally:
6343 line = compile_finally(p, &cctx);
6344 break;
6345 case CMD_endtry:
6346 line = compile_endtry(p, &cctx);
6347 break;
6348 case CMD_throw:
6349 line = compile_throw(p, &cctx);
6350 break;
6351
6352 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006354 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006355 case CMD_echomsg:
6356 case CMD_echoerr:
6357 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006358 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006359
6360 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006361 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006362 // Not recognized, execute with do_cmdline_cmd().
6363 ea.arg = p;
6364 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006365 break;
6366 }
6367 if (line == NULL)
6368 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006369 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006370
6371 if (cctx.ctx_type_stack.ga_len < 0)
6372 {
6373 iemsg("Type stack underflow");
6374 goto erret;
6375 }
6376 }
6377
6378 if (cctx.ctx_scope != NULL)
6379 {
6380 if (cctx.ctx_scope->se_type == IF_SCOPE)
6381 emsg(_(e_endif));
6382 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6383 emsg(_(e_endwhile));
6384 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6385 emsg(_(e_endfor));
6386 else
6387 emsg(_("E1026: Missing }"));
6388 goto erret;
6389 }
6390
6391 if (!had_return)
6392 {
6393 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6394 {
6395 emsg(_("E1027: Missing return statement"));
6396 goto erret;
6397 }
6398
6399 // Return zero if there is no return at the end.
6400 generate_PUSHNR(&cctx, 0);
6401 generate_instr(&cctx, ISN_RETURN);
6402 }
6403
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006404 {
6405 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6406 + ufunc->uf_dfunc_idx;
6407 dfunc->df_deleted = FALSE;
6408 dfunc->df_instr = instr->ga_data;
6409 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006410 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006411 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006412 if (cctx.ctx_outer_used)
6413 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006414 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006415
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006416 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006417 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006418 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006419
6420 // Create a type for the function, with the return type and any
6421 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006422 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6423 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006424 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006425 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006426 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6427 argcount, &ufunc->uf_type_list);
6428 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006429 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006430 argcount + varargs,
6431 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006432 {
6433 ret = FAIL;
6434 goto erret;
6435 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006436 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6437 ufunc->uf_func_type->tt_min_argcount =
6438 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006439 if (ufunc->uf_arg_types == NULL)
6440 {
6441 int i;
6442
6443 // lambda does not have argument types.
6444 for (i = 0; i < argcount; ++i)
6445 ufunc->uf_func_type->tt_args[i] = &t_any;
6446 }
6447 else
6448 mch_memmove(ufunc->uf_func_type->tt_args,
6449 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006450 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006451 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006452 ufunc->uf_func_type->tt_args[argcount] =
6453 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006454 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6455 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006456 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006457 else
6458 // No arguments, can use a predefined type.
6459 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6460 argcount, &ufunc->uf_type_list);
6461
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006462 }
6463
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006464 ret = OK;
6465
6466erret:
6467 if (ret == FAIL)
6468 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006469 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006470 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6471 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006472
6473 for (idx = 0; idx < instr->ga_len; ++idx)
6474 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006475 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006477 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006478 if (!dfunc->df_deleted)
6479 --def_functions.ga_len;
6480
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006481 while (cctx.ctx_scope != NULL)
6482 drop_scope(&cctx);
6483
Bram Moolenaar20431c92020-03-20 18:39:46 +01006484 // Don't execute this function body.
6485 ga_clear_strings(&ufunc->uf_lines);
6486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006487 if (errormsg != NULL)
6488 emsg(errormsg);
6489 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006490 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006491 }
6492
6493 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006494 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006495 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006496 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006497}
6498
6499/*
6500 * Delete an instruction, free what it contains.
6501 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006502 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006503delete_instr(isn_T *isn)
6504{
6505 switch (isn->isn_type)
6506 {
6507 case ISN_EXEC:
6508 case ISN_LOADENV:
6509 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006510 case ISN_LOADB:
6511 case ISN_LOADW:
6512 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006513 case ISN_LOADOPT:
6514 case ISN_MEMBER:
6515 case ISN_PUSHEXC:
6516 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006517 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006518 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006519 case ISN_STOREB:
6520 case ISN_STOREW:
6521 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006522 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523 vim_free(isn->isn_arg.string);
6524 break;
6525
6526 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006527 case ISN_STORES:
6528 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006529 break;
6530
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006531 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006532 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006533 vim_free(isn->isn_arg.unlet.ul_name);
6534 break;
6535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006536 case ISN_STOREOPT:
6537 vim_free(isn->isn_arg.storeopt.so_name);
6538 break;
6539
6540 case ISN_PUSHBLOB: // push blob isn_arg.blob
6541 blob_unref(isn->isn_arg.blob);
6542 break;
6543
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006544 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006545#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006546 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006547#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006548 break;
6549
6550 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006551#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006552 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006553#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006554 break;
6555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006556 case ISN_UCALL:
6557 vim_free(isn->isn_arg.ufunc.cuf_name);
6558 break;
6559
6560 case ISN_2BOOL:
6561 case ISN_2STRING:
6562 case ISN_ADDBLOB:
6563 case ISN_ADDLIST:
6564 case ISN_BCALL:
6565 case ISN_CATCH:
6566 case ISN_CHECKNR:
6567 case ISN_CHECKTYPE:
6568 case ISN_COMPAREANY:
6569 case ISN_COMPAREBLOB:
6570 case ISN_COMPAREBOOL:
6571 case ISN_COMPAREDICT:
6572 case ISN_COMPAREFLOAT:
6573 case ISN_COMPAREFUNC:
6574 case ISN_COMPARELIST:
6575 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006576 case ISN_COMPARESPECIAL:
6577 case ISN_COMPARESTRING:
6578 case ISN_CONCAT:
6579 case ISN_DCALL:
6580 case ISN_DROP:
6581 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006582 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006583 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006584 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006585 case ISN_EXECCONCAT:
6586 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587 case ISN_FOR:
6588 case ISN_FUNCREF:
6589 case ISN_INDEX:
6590 case ISN_JUMP:
6591 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006592 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006593 case ISN_LOADSCRIPT:
6594 case ISN_LOADREG:
6595 case ISN_LOADV:
6596 case ISN_NEGATENR:
6597 case ISN_NEWDICT:
6598 case ISN_NEWLIST:
6599 case ISN_OPNR:
6600 case ISN_OPFLOAT:
6601 case ISN_OPANY:
6602 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006603 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006604 case ISN_PUSHF:
6605 case ISN_PUSHNR:
6606 case ISN_PUSHBOOL:
6607 case ISN_PUSHSPEC:
6608 case ISN_RETURN:
6609 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006610 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006611 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006612 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006613 case ISN_STORESCRIPT:
6614 case ISN_THROW:
6615 case ISN_TRY:
6616 // nothing allocated
6617 break;
6618 }
6619}
6620
6621/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006622 * Free all instructions for "dfunc".
6623 */
6624 static void
6625delete_def_function_contents(dfunc_T *dfunc)
6626{
6627 int idx;
6628
6629 ga_clear(&dfunc->df_def_args_isn);
6630
6631 if (dfunc->df_instr != NULL)
6632 {
6633 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6634 delete_instr(dfunc->df_instr + idx);
6635 VIM_CLEAR(dfunc->df_instr);
6636 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006637 if (dfunc->df_funcstack != NULL)
6638 {
6639 // Decrease the reference count for the context of a closure. If down
6640 // to zero free it and clear the variables on the stack.
6641 if (--dfunc->df_funcstack->fs_refcount == 0)
6642 {
6643 garray_T *gap = &dfunc->df_funcstack->fs_ga;
6644 typval_T *stack = gap->ga_data;
6645 int i;
6646
6647 for (i = 0; i < gap->ga_len; ++i)
6648 clear_tv(stack + i);
6649 ga_clear(gap);
6650 vim_free(dfunc->df_funcstack);
6651 }
6652 dfunc->df_funcstack = NULL;
6653 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006654
6655 dfunc->df_deleted = TRUE;
6656}
6657
6658/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006659 * When a user function is deleted, delete any associated def function.
6660 */
6661 void
6662delete_def_function(ufunc_T *ufunc)
6663{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664 if (ufunc->uf_dfunc_idx >= 0)
6665 {
6666 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6667 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006668
Bram Moolenaar20431c92020-03-20 18:39:46 +01006669 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006670 }
6671}
6672
6673#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006674/*
6675 * Free all functions defined with ":def".
6676 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006677 void
6678free_def_functions(void)
6679{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006680 int idx;
6681
6682 for (idx = 0; idx < def_functions.ga_len; ++idx)
6683 {
6684 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6685
6686 delete_def_function_contents(dfunc);
6687 }
6688
6689 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006690}
6691#endif
6692
6693
6694#endif // FEAT_EVAL