blob: eb84e3590904a849f5212b82ff5a0369421e9b4f [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
827 ((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype;
828
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 }
1674 if (*p == ')' && p[1] == ':')
1675 p = skip_type(skipwhite(p + 2));
1676 }
1677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678 return p;
1679}
1680
1681/*
1682 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001683 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 * Returns NULL in case of failure.
1685 */
1686 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001687parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688{
1689 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001690 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691
1692 if (**arg != '<')
1693 {
1694 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001695 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001696 else
1697 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001698 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 }
1700 *arg = skipwhite(*arg + 1);
1701
Bram Moolenaard77a8522020-04-03 21:59:57 +02001702 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703
1704 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001705 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706 {
1707 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001708 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 }
1710 ++*arg;
1711
1712 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001713 return get_list_type(member_type, type_gap);
1714 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715}
1716
1717/*
1718 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001719 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 */
1721 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001722parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723{
1724 char_u *p = *arg;
1725 size_t len;
1726
1727 // skip over the first word
1728 while (ASCII_ISALNUM(*p) || *p == '_')
1729 ++p;
1730 len = p - *arg;
1731
1732 switch (**arg)
1733 {
1734 case 'a':
1735 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1736 {
1737 *arg += len;
1738 return &t_any;
1739 }
1740 break;
1741 case 'b':
1742 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1743 {
1744 *arg += len;
1745 return &t_bool;
1746 }
1747 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1748 {
1749 *arg += len;
1750 return &t_blob;
1751 }
1752 break;
1753 case 'c':
1754 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1755 {
1756 *arg += len;
1757 return &t_channel;
1758 }
1759 break;
1760 case 'd':
1761 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1762 {
1763 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001764 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001765 }
1766 break;
1767 case 'f':
1768 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1769 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001770#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 *arg += len;
1772 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001773#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001774 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001775 return &t_any;
1776#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001777 }
1778 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1779 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001780 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001781 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001782 int argcount = -1;
1783 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001784 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001785 type_T *arg_type[MAX_FUNC_ARGS + 1];
1786
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001787 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001789 if (**arg == '(')
1790 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001791 // "func" may or may not return a value, "func()" does
1792 // not return a value.
1793 ret_type = &t_void;
1794
Bram Moolenaard77a8522020-04-03 21:59:57 +02001795 p = ++*arg;
1796 argcount = 0;
1797 while (*p != NUL && *p != ')')
1798 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001799 if (*p == '?')
1800 {
1801 if (first_optional == -1)
1802 first_optional = argcount;
1803 ++p;
1804 }
1805 else if (first_optional != -1)
1806 {
1807 emsg(_("E1007: mandatory argument after optional argument"));
1808 return &t_any;
1809 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001810 else if (STRNCMP(p, "...", 3) == 0)
1811 {
1812 flags |= TTFLAG_VARARGS;
1813 p += 3;
1814 }
1815
1816 arg_type[argcount++] = parse_type(&p, type_gap);
1817
1818 // Nothing comes after "...{type}".
1819 if (flags & TTFLAG_VARARGS)
1820 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001821
Bram Moolenaard77a8522020-04-03 21:59:57 +02001822 if (*p != ',' && *skipwhite(p) == ',')
1823 {
1824 semsg(_(e_no_white_before), ",");
1825 return &t_any;
1826 }
1827 if (*p == ',')
1828 {
1829 ++p;
1830 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001831 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001832 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001833 return &t_any;
1834 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001835 }
1836 p = skipwhite(p);
1837 if (argcount == MAX_FUNC_ARGS)
1838 {
1839 emsg(_("E740: Too many argument types"));
1840 return &t_any;
1841 }
1842 }
1843
1844 p = skipwhite(p);
1845 if (*p != ')')
1846 {
1847 emsg(_(e_missing_close));
1848 return &t_any;
1849 }
1850 *arg = p + 1;
1851 }
1852 if (**arg == ':')
1853 {
1854 // parse return type
1855 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001856 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001857 semsg(_(e_white_after), ":");
1858 *arg = skipwhite(*arg);
1859 ret_type = parse_type(arg, type_gap);
1860 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001861 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001862 type = get_func_type(ret_type, argcount, type_gap);
1863 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001864 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001865 type = alloc_func_type(ret_type, argcount, type_gap);
1866 type->tt_flags = flags;
1867 if (argcount > 0)
1868 {
1869 type->tt_argcount = argcount;
1870 type->tt_min_argcount = first_optional == -1
1871 ? argcount : first_optional;
1872 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001873 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001874 return &t_any;
1875 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001876 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001877 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001878 }
1879 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 }
1881 break;
1882 case 'j':
1883 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1884 {
1885 *arg += len;
1886 return &t_job;
1887 }
1888 break;
1889 case 'l':
1890 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1891 {
1892 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001893 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 }
1895 break;
1896 case 'n':
1897 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1898 {
1899 *arg += len;
1900 return &t_number;
1901 }
1902 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001903 case 's':
1904 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1905 {
1906 *arg += len;
1907 return &t_string;
1908 }
1909 break;
1910 case 'v':
1911 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1912 {
1913 *arg += len;
1914 return &t_void;
1915 }
1916 break;
1917 }
1918
1919 semsg(_("E1010: Type not recognized: %s"), *arg);
1920 return &t_any;
1921}
1922
1923/*
1924 * Check if "type1" and "type2" are exactly the same.
1925 */
1926 static int
1927equal_type(type_T *type1, type_T *type2)
1928{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001929 int i;
1930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931 if (type1->tt_type != type2->tt_type)
1932 return FALSE;
1933 switch (type1->tt_type)
1934 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001936 case VAR_ANY:
1937 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938 case VAR_SPECIAL:
1939 case VAR_BOOL:
1940 case VAR_NUMBER:
1941 case VAR_FLOAT:
1942 case VAR_STRING:
1943 case VAR_BLOB:
1944 case VAR_JOB:
1945 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001946 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 case VAR_LIST:
1948 case VAR_DICT:
1949 return equal_type(type1->tt_member, type2->tt_member);
1950 case VAR_FUNC:
1951 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001952 if (!equal_type(type1->tt_member, type2->tt_member)
1953 || type1->tt_argcount != type2->tt_argcount)
1954 return FALSE;
1955 if (type1->tt_argcount < 0
1956 || type1->tt_args == NULL || type2->tt_args == NULL)
1957 return TRUE;
1958 for (i = 0; i < type1->tt_argcount; ++i)
1959 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
1960 return FALSE;
1961 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962 }
1963 return TRUE;
1964}
1965
1966/*
1967 * Find the common type of "type1" and "type2" and put it in "dest".
1968 * "type2" and "dest" may be the same.
1969 */
1970 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02001971common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972{
1973 if (equal_type(type1, type2))
1974 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001975 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 return;
1977 }
1978
1979 if (type1->tt_type == type2->tt_type)
1980 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1982 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001983 type_T *common;
1984
Bram Moolenaard77a8522020-04-03 21:59:57 +02001985 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001986 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001987 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001988 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001989 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 return;
1991 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001992 if (type1->tt_type == VAR_FUNC)
1993 {
1994 type_T *common;
1995
1996 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
1997 if (type1->tt_argcount == type2->tt_argcount
1998 && type1->tt_argcount >= 0)
1999 {
2000 int argcount = type1->tt_argcount;
2001 int i;
2002
2003 *dest = alloc_func_type(common, argcount, type_gap);
2004 if (type1->tt_args != NULL && type2->tt_args != NULL)
2005 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002006 if (func_type_add_arg_types(*dest, argcount,
2007 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002008 for (i = 0; i < argcount; ++i)
2009 common_type(type1->tt_args[i], type2->tt_args[i],
2010 &(*dest)->tt_args[i], type_gap);
2011 }
2012 }
2013 else
2014 *dest = alloc_func_type(common, -1, type_gap);
2015 return;
2016 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 }
2018
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002019 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020}
2021
2022 char *
2023vartype_name(vartype_T type)
2024{
2025 switch (type)
2026 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002027 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002028 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030 case VAR_SPECIAL: return "special";
2031 case VAR_BOOL: return "bool";
2032 case VAR_NUMBER: return "number";
2033 case VAR_FLOAT: return "float";
2034 case VAR_STRING: return "string";
2035 case VAR_BLOB: return "blob";
2036 case VAR_JOB: return "job";
2037 case VAR_CHANNEL: return "channel";
2038 case VAR_LIST: return "list";
2039 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002040
2041 case VAR_FUNC:
2042 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002044 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045}
2046
2047/*
2048 * Return the name of a type.
2049 * The result may be in allocated memory, in which case "tofree" is set.
2050 */
2051 char *
2052type_name(type_T *type, char **tofree)
2053{
2054 char *name = vartype_name(type->tt_type);
2055
2056 *tofree = NULL;
2057 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2058 {
2059 char *member_free;
2060 char *member_name = type_name(type->tt_member, &member_free);
2061 size_t len;
2062
2063 len = STRLEN(name) + STRLEN(member_name) + 3;
2064 *tofree = alloc(len);
2065 if (*tofree != NULL)
2066 {
2067 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2068 vim_free(member_free);
2069 return *tofree;
2070 }
2071 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002072 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002073 {
2074 garray_T ga;
2075 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002076 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002077
2078 ga_init2(&ga, 1, 100);
2079 if (ga_grow(&ga, 20) == FAIL)
2080 return "[unknown]";
2081 *tofree = ga.ga_data;
2082 STRCPY(ga.ga_data, "func(");
2083 ga.ga_len += 5;
2084
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002085 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002086 {
2087 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002088 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002089 int len;
2090
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002091 if (type->tt_args == NULL)
2092 arg_type = "[unknown]";
2093 else
2094 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002095 if (i > 0)
2096 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002097 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002098 ga.ga_len += 2;
2099 }
2100 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002101 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002102 {
2103 vim_free(arg_free);
2104 return "[unknown]";
2105 }
2106 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002107 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002108 {
2109 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2110 ga.ga_len += 3;
2111 }
2112 else if (i >= type->tt_min_argcount)
2113 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002114 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002115 ga.ga_len += len;
2116 vim_free(arg_free);
2117 }
2118
2119 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002120 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002121 else
2122 {
2123 char *ret_free;
2124 char *ret_name = type_name(type->tt_member, &ret_free);
2125 int len;
2126
2127 len = (int)STRLEN(ret_name) + 4;
2128 if (ga_grow(&ga, len) == FAIL)
2129 {
2130 vim_free(ret_free);
2131 return "[unknown]";
2132 }
2133 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002134 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2135 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002136 vim_free(ret_free);
2137 }
2138 return ga.ga_data;
2139 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140
2141 return name;
2142}
2143
2144/*
2145 * Find "name" in script-local items of script "sid".
2146 * Returns the index in "sn_var_vals" if found.
2147 * If found but not in "sn_var_vals" returns -1.
2148 * If not found returns -2.
2149 */
2150 int
2151get_script_item_idx(int sid, char_u *name, int check_writable)
2152{
2153 hashtab_T *ht;
2154 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002155 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156 int idx;
2157
2158 // First look the name up in the hashtable.
2159 if (sid <= 0 || sid > script_items.ga_len)
2160 return -1;
2161 ht = &SCRIPT_VARS(sid);
2162 di = find_var_in_ht(ht, 0, name, TRUE);
2163 if (di == NULL)
2164 return -2;
2165
2166 // Now find the svar_T index in sn_var_vals.
2167 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2168 {
2169 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2170
2171 if (sv->sv_tv == &di->di_tv)
2172 {
2173 if (check_writable && sv->sv_const)
2174 semsg(_(e_readonlyvar), name);
2175 return idx;
2176 }
2177 }
2178 return -1;
2179}
2180
2181/*
2182 * Find "name" in imported items of the current script/
2183 */
2184 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002185find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002187 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002188 int idx;
2189
2190 if (cctx != NULL)
2191 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2192 {
2193 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2194 + idx;
2195
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002196 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2197 : STRLEN(import->imp_name) == len
2198 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199 return import;
2200 }
2201
2202 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2203 {
2204 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2205
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002206 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2207 : STRLEN(import->imp_name) == len
2208 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209 return import;
2210 }
2211 return NULL;
2212}
2213
2214/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002215 * Free all imported variables.
2216 */
2217 static void
2218free_imported(cctx_T *cctx)
2219{
2220 int idx;
2221
2222 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2223 {
2224 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2225
2226 vim_free(import->imp_name);
2227 }
2228 ga_clear(&cctx->ctx_imports);
2229}
2230
2231/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002232 * Get the next line of the function from "cctx".
2233 * Returns NULL when at the end.
2234 */
2235 static char_u *
2236next_line_from_context(cctx_T *cctx)
2237{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002238 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002239
2240 do
2241 {
2242 ++cctx->ctx_lnum;
2243 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002244 {
2245 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002246 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002247 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002248 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002249 cctx->ctx_line_start = line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002250 SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum
2251 + cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002252 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002253 return line;
2254}
2255
2256/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002257 * Return TRUE if "p" points at a "#" but not at "#{".
2258 */
2259 static int
2260comment_start(char_u *p)
2261{
2262 return p[0] == '#' && p[1] != '{';
2263}
2264
2265/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002266 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002267 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002268 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2269 */
2270 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002271may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002272{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002273 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002274 {
2275 char_u *next = next_line_from_context(cctx);
2276
2277 if (next == NULL)
2278 return FAIL;
2279 *arg = skipwhite(next);
2280 }
2281 return OK;
2282}
2283
2284/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002285 * Generate an instruction to load script-local variable "name", without the
2286 * leading "s:".
2287 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288 */
2289 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002290compile_load_scriptvar(
2291 cctx_T *cctx,
2292 char_u *name, // variable NUL terminated
2293 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002294 char_u **end, // end of variable
2295 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002297 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2299 imported_T *import;
2300
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002301 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002303 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002304 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2305 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 }
2307 if (idx >= 0)
2308 {
2309 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2310
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002311 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 current_sctx.sc_sid, idx, sv->sv_type);
2313 return OK;
2314 }
2315
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002316 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317 if (import != NULL)
2318 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002319 if (import->imp_all)
2320 {
2321 char_u *p = skipwhite(*end);
2322 int name_len;
2323 ufunc_T *ufunc;
2324 type_T *type;
2325
2326 // Used "import * as Name", need to lookup the member.
2327 if (*p != '.')
2328 {
2329 semsg(_("E1060: expected dot after name: %s"), start);
2330 return FAIL;
2331 }
2332 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002333 if (VIM_ISWHITE(*p))
2334 {
2335 emsg(_("E1074: no white space allowed after dot"));
2336 return FAIL;
2337 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002338
2339 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2340 // TODO: what if it is a function?
2341 if (idx < 0)
2342 return FAIL;
2343 *end = p;
2344
2345 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2346 import->imp_sid,
2347 idx,
2348 type);
2349 }
2350 else
2351 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002352 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002353 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2354 import->imp_sid,
2355 import->imp_var_vals_idx,
2356 import->imp_type);
2357 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002358 return OK;
2359 }
2360
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002361 if (error)
2362 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 return FAIL;
2364}
2365
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002366 static int
2367generate_funcref(cctx_T *cctx, char_u *name)
2368{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002369 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002370
2371 if (ufunc == NULL)
2372 return FAIL;
2373
2374 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2375}
2376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377/*
2378 * Compile a variable name into a load instruction.
2379 * "end" points to just after the name.
2380 * When "error" is FALSE do not give an error when not found.
2381 */
2382 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002383compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384{
2385 type_T *type;
2386 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002387 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002389 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390
2391 if (*(*arg + 1) == ':')
2392 {
2393 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002394 if (end <= *arg + 2)
2395 name = vim_strsave((char_u *)"[empty]");
2396 else
2397 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 if (name == NULL)
2399 return FAIL;
2400
2401 if (**arg == 'v')
2402 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002403 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404 }
2405 else if (**arg == 'g')
2406 {
2407 // Global variables can be defined later, thus we don't check if it
2408 // exists, give error at runtime.
2409 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2410 }
2411 else if (**arg == 's')
2412 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002413 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002415 else if (**arg == 'b')
2416 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002417 // Buffer-local variables can be defined later, thus we don't check
2418 // if it exists, give error at runtime.
2419 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002420 }
2421 else if (**arg == 'w')
2422 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002423 // Window-local variables can be defined later, thus we don't check
2424 // if it exists, give error at runtime.
2425 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002426 }
2427 else if (**arg == 't')
2428 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002429 // Tabpage-local variables can be defined later, thus we don't
2430 // check if it exists, give error at runtime.
2431 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433 else
2434 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002435 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 goto theend;
2437 }
2438 }
2439 else
2440 {
2441 size_t len = end - *arg;
2442 int idx;
2443 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002444 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445
2446 name = vim_strnsave(*arg, end - *arg);
2447 if (name == NULL)
2448 return FAIL;
2449
2450 idx = lookup_arg(*arg, len, cctx);
2451 if (idx >= 0)
2452 {
2453 if (cctx->ctx_ufunc->uf_arg_types != NULL)
2454 type = cctx->ctx_ufunc->uf_arg_types[idx];
2455 else
2456 type = &t_any;
2457
2458 // Arguments are located above the frame pointer.
2459 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
2460 if (cctx->ctx_ufunc->uf_va_name != NULL)
2461 --idx;
2462 gen_load = TRUE;
2463 }
2464 else if (lookup_vararg(*arg, len, cctx))
2465 {
2466 // varargs is always the last argument
2467 idx = -STACK_FRAME_SIZE - 1;
2468 type = cctx->ctx_ufunc->uf_va_type;
2469 gen_load = TRUE;
2470 }
2471 else
2472 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002473 lvar_T *lvar = lookup_local(*arg, len, cctx);
2474
2475 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002477 type = lvar->lv_type;
2478 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002479 if (lvar->lv_from_outer)
2480 gen_load_outer = TRUE;
2481 else
2482 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483 }
2484 else
2485 {
2486 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2487 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2488 res = generate_PUSHBOOL(cctx, **arg == 't'
2489 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002490 else
2491 {
2492 // "var" can be script-local even without using "s:" if it
2493 // already exists.
2494 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2495 == SCRIPT_VERSION_VIM9
2496 || lookup_script(*arg, len) == OK)
2497 res = compile_load_scriptvar(cctx, name, *arg, &end,
2498 FALSE);
2499
2500 // When the name starts with an uppercase letter or "x:" it
2501 // can be a user defined function.
2502 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2503 res = generate_funcref(cctx, name);
2504 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 }
2506 }
2507 if (gen_load)
2508 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002509 if (gen_load_outer)
2510 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 }
2512
2513 *arg = end;
2514
2515theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002516 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 semsg(_(e_var_notfound), name);
2518 vim_free(name);
2519 return res;
2520}
2521
2522/*
2523 * Compile the argument expressions.
2524 * "arg" points to just after the "(" and is advanced to after the ")"
2525 */
2526 static int
2527compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2528{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002529 char_u *p = *arg;
2530 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531
Bram Moolenaare6085c52020-04-12 20:19:16 +02002532 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002534 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002535 {
2536 p = next_line_from_context(cctx);
2537 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002538 goto failret;
2539 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002540 p = skipwhite(p);
2541 }
2542 if (*p == ')')
2543 {
2544 *arg = p + 1;
2545 return OK;
2546 }
2547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 if (compile_expr1(&p, cctx) == FAIL)
2549 return FAIL;
2550 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002551
2552 if (*p != ',' && *skipwhite(p) == ',')
2553 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002554 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002555 p = skipwhite(p);
2556 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002558 {
2559 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002560 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002561 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002562 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002563 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002564 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002565 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002566failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002567 emsg(_(e_missing_close));
2568 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569}
2570
2571/*
2572 * Compile a function call: name(arg1, arg2)
2573 * "arg" points to "name", "arg + varlen" to the "(".
2574 * "argcount_init" is 1 for "value->method()"
2575 * Instructions:
2576 * EVAL arg1
2577 * EVAL arg2
2578 * BCALL / DCALL / UCALL
2579 */
2580 static int
2581compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2582{
2583 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002584 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 int argcount = argcount_init;
2586 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002587 char_u fname_buf[FLEN_FIXED + 1];
2588 char_u *tofree = NULL;
2589 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002591 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592
2593 if (varlen >= sizeof(namebuf))
2594 {
2595 semsg(_("E1011: name too long: %s"), name);
2596 return FAIL;
2597 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002598 vim_strncpy(namebuf, *arg, varlen);
2599 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600
2601 *arg = skipwhite(*arg + varlen + 1);
2602 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002603 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002605 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 {
2607 int idx;
2608
2609 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002610 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002612 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002613 else
2614 semsg(_(e_unknownfunc), namebuf);
2615 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 }
2617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002619 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002621 {
2622 res = generate_CALL(cctx, ufunc, argcount);
2623 goto theend;
2624 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625
2626 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002627 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002629 if (STRNCMP(namebuf, "g:", 2) != 0
2630 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002631 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002632 garray_T *stack = &cctx->ctx_type_stack;
2633 type_T *type;
2634
2635 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2636 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002637 goto theend;
2638 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002640 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002641 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002642 if (STRNCMP(namebuf, "g:", 2) == 0)
2643 res = generate_UCALL(cctx, name, argcount);
2644 else
2645 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002646
2647theend:
2648 vim_free(tofree);
2649 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650}
2651
2652// like NAMESPACE_CHAR but with 'a' and 'l'.
2653#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2654
2655/*
2656 * Find the end of a variable or function name. Unlike find_name_end() this
2657 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002658 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 * Return a pointer to just after the name. Equal to "arg" if there is no
2660 * valid name.
2661 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002662 static char_u *
2663to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664{
2665 char_u *p;
2666
2667 // Quick check for valid starting character.
2668 if (!eval_isnamec1(*arg))
2669 return arg;
2670
2671 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2672 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2673 // and can be used in slice "[n:]".
2674 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002675 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2677 break;
2678 return p;
2679}
2680
2681/*
2682 * Like to_name_end() but also skip over a list or dict constant.
2683 */
2684 char_u *
2685to_name_const_end(char_u *arg)
2686{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002687 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 typval_T rettv;
2689
2690 if (p == arg && *arg == '[')
2691 {
2692
2693 // Can be "[1, 2, 3]->Func()".
2694 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2695 p = arg;
2696 }
2697 else if (p == arg && *arg == '#' && arg[1] == '{')
2698 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002699 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700 ++p;
2701 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2702 p = arg;
2703 }
2704 else if (p == arg && *arg == '{')
2705 {
2706 int ret = get_lambda_tv(&p, &rettv, FALSE);
2707
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002708 // Can be "{x -> ret}()".
2709 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 if (ret == NOTDONE)
2711 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2712 if (ret != OK)
2713 p = arg;
2714 }
2715
2716 return p;
2717}
2718
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719/*
2720 * parse a list: [expr, expr]
2721 * "*arg" points to the '['.
2722 */
2723 static int
2724compile_list(char_u **arg, cctx_T *cctx)
2725{
2726 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002727 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 int count = 0;
2729
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002730 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002732 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002733 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002734 p = next_line_from_context(cctx);
2735 if (p == NULL)
2736 {
2737 semsg(_(e_list_end), *arg);
2738 return FAIL;
2739 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002740 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002741 p = skipwhite(p);
2742 }
2743 if (*p == ']')
2744 {
2745 ++p;
2746 // Allow for following comment, after at least one space.
2747 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
2748 p += STRLEN(p);
2749 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002750 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 if (compile_expr1(&p, cctx) == FAIL)
2752 break;
2753 ++count;
2754 if (*p == ',')
2755 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002756 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 p = skipwhite(p);
2758 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002759 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760
2761 generate_NEWLIST(cctx, count);
2762 return OK;
2763}
2764
2765/*
2766 * parse a lambda: {arg, arg -> expr}
2767 * "*arg" points to the '{'.
2768 */
2769 static int
2770compile_lambda(char_u **arg, cctx_T *cctx)
2771{
2772 garray_T *instr = &cctx->ctx_instr;
2773 typval_T rettv;
2774 ufunc_T *ufunc;
2775
2776 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002777 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002779
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002781 ++ufunc->uf_refcount;
2782 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002783 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784
2785 // The function will have one line: "return {expr}".
2786 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002787 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788
2789 if (ufunc->uf_dfunc_idx >= 0)
2790 {
2791 if (ga_grow(instr, 1) == FAIL)
2792 return FAIL;
2793 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2794 return OK;
2795 }
2796 return FAIL;
2797}
2798
2799/*
2800 * Compile a lamda call: expr->{lambda}(args)
2801 * "arg" points to the "{".
2802 */
2803 static int
2804compile_lambda_call(char_u **arg, cctx_T *cctx)
2805{
2806 ufunc_T *ufunc;
2807 typval_T rettv;
2808 int argcount = 1;
2809 int ret = FAIL;
2810
2811 // Get the funcref in "rettv".
2812 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2813 return FAIL;
2814
2815 if (**arg != '(')
2816 {
2817 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002818 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 else
2820 semsg(_(e_missing_paren), "lambda");
2821 clear_tv(&rettv);
2822 return FAIL;
2823 }
2824
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 ufunc = rettv.vval.v_partial->pt_func;
2826 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002827 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002828 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002829
2830 // The function will have one line: "return {expr}".
2831 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002832 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833
2834 // compile the arguments
2835 *arg = skipwhite(*arg + 1);
2836 if (compile_arguments(arg, cctx, &argcount) == OK)
2837 // call the compiled function
2838 ret = generate_CALL(cctx, ufunc, argcount);
2839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 return ret;
2841}
2842
2843/*
2844 * parse a dict: {'key': val} or #{key: val}
2845 * "*arg" points to the '{'.
2846 */
2847 static int
2848compile_dict(char_u **arg, cctx_T *cctx, int literal)
2849{
2850 garray_T *instr = &cctx->ctx_instr;
2851 int count = 0;
2852 dict_T *d = dict_alloc();
2853 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002854 char_u *whitep = *arg;
2855 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856
2857 if (d == NULL)
2858 return FAIL;
2859 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002860 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861 {
2862 char_u *key = NULL;
2863
Bram Moolenaar2c330432020-04-13 14:41:35 +02002864 while (**arg == NUL || (literal && **arg == '"')
2865 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002866 {
2867 *arg = next_line_from_context(cctx);
2868 if (*arg == NULL)
2869 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002870 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002871 *arg = skipwhite(*arg);
2872 }
2873
2874 if (**arg == '}')
2875 break;
2876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 if (literal)
2878 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002879 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880
Bram Moolenaar2c330432020-04-13 14:41:35 +02002881 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 {
2883 semsg(_("E1014: Invalid key: %s"), *arg);
2884 return FAIL;
2885 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002886 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 if (generate_PUSHS(cctx, key) == FAIL)
2888 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002889 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 }
2891 else
2892 {
2893 isn_T *isn;
2894
2895 if (compile_expr1(arg, cctx) == FAIL)
2896 return FAIL;
2897 // TODO: check type is string
2898 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2899 if (isn->isn_type == ISN_PUSHS)
2900 key = isn->isn_arg.string;
2901 }
2902
2903 // Check for duplicate keys, if using string keys.
2904 if (key != NULL)
2905 {
2906 item = dict_find(d, key, -1);
2907 if (item != NULL)
2908 {
2909 semsg(_(e_duplicate_key), key);
2910 goto failret;
2911 }
2912 item = dictitem_alloc(key);
2913 if (item != NULL)
2914 {
2915 item->di_tv.v_type = VAR_UNKNOWN;
2916 item->di_tv.v_lock = 0;
2917 if (dict_add(d, item) == FAIL)
2918 dictitem_free(item);
2919 }
2920 }
2921
2922 *arg = skipwhite(*arg);
2923 if (**arg != ':')
2924 {
2925 semsg(_(e_missing_dict_colon), *arg);
2926 return FAIL;
2927 }
2928
Bram Moolenaar2c330432020-04-13 14:41:35 +02002929 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002931 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002932 {
2933 *arg = next_line_from_context(cctx);
2934 if (*arg == NULL)
2935 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002936 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002937 *arg = skipwhite(*arg);
2938 }
2939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 if (compile_expr1(arg, cctx) == FAIL)
2941 return FAIL;
2942 ++count;
2943
Bram Moolenaar2c330432020-04-13 14:41:35 +02002944 whitep = *arg;
2945 p = skipwhite(*arg);
2946 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002947 {
2948 *arg = next_line_from_context(cctx);
2949 if (*arg == NULL)
2950 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002951 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002952 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002953 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002954 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 if (**arg == '}')
2956 break;
2957 if (**arg != ',')
2958 {
2959 semsg(_(e_missing_dict_comma), *arg);
2960 goto failret;
2961 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002962 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 *arg = skipwhite(*arg + 1);
2964 }
2965
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 *arg = *arg + 1;
2967
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002968 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002969 p = skipwhite(*arg);
2970 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002971 *arg += STRLEN(*arg);
2972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 dict_unref(d);
2974 return generate_NEWDICT(cctx, count);
2975
2976failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002977 if (*arg == NULL)
2978 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 dict_unref(d);
2980 return FAIL;
2981}
2982
2983/*
2984 * Compile "&option".
2985 */
2986 static int
2987compile_get_option(char_u **arg, cctx_T *cctx)
2988{
2989 typval_T rettv;
2990 char_u *start = *arg;
2991 int ret;
2992
2993 // parse the option and get the current value to get the type.
2994 rettv.v_type = VAR_UNKNOWN;
2995 ret = get_option_tv(arg, &rettv, TRUE);
2996 if (ret == OK)
2997 {
2998 // include the '&' in the name, get_option_tv() expects it.
2999 char_u *name = vim_strnsave(start, *arg - start);
3000 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3001
3002 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3003 vim_free(name);
3004 }
3005 clear_tv(&rettv);
3006
3007 return ret;
3008}
3009
3010/*
3011 * Compile "$VAR".
3012 */
3013 static int
3014compile_get_env(char_u **arg, cctx_T *cctx)
3015{
3016 char_u *start = *arg;
3017 int len;
3018 int ret;
3019 char_u *name;
3020
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 ++*arg;
3022 len = get_env_len(arg);
3023 if (len == 0)
3024 {
3025 semsg(_(e_syntax_at), start - 1);
3026 return FAIL;
3027 }
3028
3029 // include the '$' in the name, get_env_tv() expects it.
3030 name = vim_strnsave(start, len + 1);
3031 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3032 vim_free(name);
3033 return ret;
3034}
3035
3036/*
3037 * Compile "@r".
3038 */
3039 static int
3040compile_get_register(char_u **arg, cctx_T *cctx)
3041{
3042 int ret;
3043
3044 ++*arg;
3045 if (**arg == NUL)
3046 {
3047 semsg(_(e_syntax_at), *arg - 1);
3048 return FAIL;
3049 }
3050 if (!valid_yank_reg(**arg, TRUE))
3051 {
3052 emsg_invreg(**arg);
3053 return FAIL;
3054 }
3055 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3056 ++*arg;
3057 return ret;
3058}
3059
3060/*
3061 * Apply leading '!', '-' and '+' to constant "rettv".
3062 */
3063 static int
3064apply_leader(typval_T *rettv, char_u *start, char_u *end)
3065{
3066 char_u *p = end;
3067
3068 // this works from end to start
3069 while (p > start)
3070 {
3071 --p;
3072 if (*p == '-' || *p == '+')
3073 {
3074 // only '-' has an effect, for '+' we only check the type
3075#ifdef FEAT_FLOAT
3076 if (rettv->v_type == VAR_FLOAT)
3077 {
3078 if (*p == '-')
3079 rettv->vval.v_float = -rettv->vval.v_float;
3080 }
3081 else
3082#endif
3083 {
3084 varnumber_T val;
3085 int error = FALSE;
3086
3087 // tv_get_number_chk() accepts a string, but we don't want that
3088 // here
3089 if (check_not_string(rettv) == FAIL)
3090 return FAIL;
3091 val = tv_get_number_chk(rettv, &error);
3092 clear_tv(rettv);
3093 if (error)
3094 return FAIL;
3095 if (*p == '-')
3096 val = -val;
3097 rettv->v_type = VAR_NUMBER;
3098 rettv->vval.v_number = val;
3099 }
3100 }
3101 else
3102 {
3103 int v = tv2bool(rettv);
3104
3105 // '!' is permissive in the type.
3106 clear_tv(rettv);
3107 rettv->v_type = VAR_BOOL;
3108 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3109 }
3110 }
3111 return OK;
3112}
3113
3114/*
3115 * Recognize v: variables that are constants and set "rettv".
3116 */
3117 static void
3118get_vim_constant(char_u **arg, typval_T *rettv)
3119{
3120 if (STRNCMP(*arg, "v:true", 6) == 0)
3121 {
3122 rettv->v_type = VAR_BOOL;
3123 rettv->vval.v_number = VVAL_TRUE;
3124 *arg += 6;
3125 }
3126 else if (STRNCMP(*arg, "v:false", 7) == 0)
3127 {
3128 rettv->v_type = VAR_BOOL;
3129 rettv->vval.v_number = VVAL_FALSE;
3130 *arg += 7;
3131 }
3132 else if (STRNCMP(*arg, "v:null", 6) == 0)
3133 {
3134 rettv->v_type = VAR_SPECIAL;
3135 rettv->vval.v_number = VVAL_NULL;
3136 *arg += 6;
3137 }
3138 else if (STRNCMP(*arg, "v:none", 6) == 0)
3139 {
3140 rettv->v_type = VAR_SPECIAL;
3141 rettv->vval.v_number = VVAL_NONE;
3142 *arg += 6;
3143 }
3144}
3145
3146/*
3147 * Compile code to apply '-', '+' and '!'.
3148 */
3149 static int
3150compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3151{
3152 char_u *p = end;
3153
3154 // this works from end to start
3155 while (p > start)
3156 {
3157 --p;
3158 if (*p == '-' || *p == '+')
3159 {
3160 int negate = *p == '-';
3161 isn_T *isn;
3162
3163 // TODO: check type
3164 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3165 {
3166 --p;
3167 if (*p == '-')
3168 negate = !negate;
3169 }
3170 // only '-' has an effect, for '+' we only check the type
3171 if (negate)
3172 isn = generate_instr(cctx, ISN_NEGATENR);
3173 else
3174 isn = generate_instr(cctx, ISN_CHECKNR);
3175 if (isn == NULL)
3176 return FAIL;
3177 }
3178 else
3179 {
3180 int invert = TRUE;
3181
3182 while (p > start && p[-1] == '!')
3183 {
3184 --p;
3185 invert = !invert;
3186 }
3187 if (generate_2BOOL(cctx, invert) == FAIL)
3188 return FAIL;
3189 }
3190 }
3191 return OK;
3192}
3193
3194/*
3195 * Compile whatever comes after "name" or "name()".
3196 */
3197 static int
3198compile_subscript(
3199 char_u **arg,
3200 cctx_T *cctx,
3201 char_u **start_leader,
3202 char_u *end_leader)
3203{
3204 for (;;)
3205 {
3206 if (**arg == '(')
3207 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003208 garray_T *stack = &cctx->ctx_type_stack;
3209 type_T *type;
3210 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211
3212 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003213 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3214
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 *arg = skipwhite(*arg + 1);
3216 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3217 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003218 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 return FAIL;
3220 }
3221 else if (**arg == '-' && (*arg)[1] == '>')
3222 {
3223 char_u *p;
3224
3225 // something->method()
3226 // Apply the '!', '-' and '+' first:
3227 // -1.0->func() works like (-1.0)->func()
3228 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3229 return FAIL;
3230 *start_leader = end_leader; // don't apply again later
3231
3232 *arg = skipwhite(*arg + 2);
3233 if (**arg == '{')
3234 {
3235 // lambda call: list->{lambda}
3236 if (compile_lambda_call(arg, cctx) == FAIL)
3237 return FAIL;
3238 }
3239 else
3240 {
3241 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003242 p = *arg;
3243 if (ASCII_ISALPHA(*p) && p[1] == ':')
3244 p += 2;
3245 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 ;
3247 if (*p != '(')
3248 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003249 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 return FAIL;
3251 }
3252 // TODO: base value may not be the first argument
3253 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
3254 return FAIL;
3255 }
3256 }
3257 else if (**arg == '[')
3258 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01003259 garray_T *stack;
3260 type_T **typep;
3261
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 // list index: list[123]
3263 // TODO: more arguments
3264 // TODO: dict member dict['name']
3265 *arg = skipwhite(*arg + 1);
3266 if (compile_expr1(arg, cctx) == FAIL)
3267 return FAIL;
3268
3269 if (**arg != ']')
3270 {
3271 emsg(_(e_missbrac));
3272 return FAIL;
3273 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003274 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275
3276 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3277 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003278 stack = &cctx->ctx_type_stack;
3279 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
3280 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
3281 {
3282 emsg(_(e_listreq));
3283 return FAIL;
3284 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01003285 if ((*typep)->tt_type == VAR_LIST)
3286 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 }
3288 else if (**arg == '.' && (*arg)[1] != '.')
3289 {
3290 char_u *p;
3291
3292 ++*arg;
3293 p = *arg;
3294 // dictionary member: dict.name
3295 if (eval_isnamec1(*p))
3296 while (eval_isnamec(*p))
3297 MB_PTR_ADV(p);
3298 if (p == *arg)
3299 {
3300 semsg(_(e_syntax_at), *arg);
3301 return FAIL;
3302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
3304 return FAIL;
3305 *arg = p;
3306 }
3307 else
3308 break;
3309 }
3310
3311 // TODO - see handle_subscript():
3312 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3313 // Don't do this when "Func" is already a partial that was bound
3314 // explicitly (pt_auto is FALSE).
3315
3316 return OK;
3317}
3318
3319/*
3320 * Compile an expression at "*p" and add instructions to "instr".
3321 * "p" is advanced until after the expression, skipping white space.
3322 *
3323 * This is the equivalent of eval1(), eval2(), etc.
3324 */
3325
3326/*
3327 * number number constant
3328 * 0zFFFFFFFF Blob constant
3329 * "string" string constant
3330 * 'string' literal string constant
3331 * &option-name option value
3332 * @r register contents
3333 * identifier variable value
3334 * function() function call
3335 * $VAR environment variable
3336 * (expression) nested expression
3337 * [expr, expr] List
3338 * {key: val, key: val} Dictionary
3339 * #{key: val, key: val} Dictionary with literal keys
3340 *
3341 * Also handle:
3342 * ! in front logical NOT
3343 * - in front unary minus
3344 * + in front unary plus (ignored)
3345 * trailing (arg) funcref/partial call
3346 * trailing [] subscript in String or List
3347 * trailing .name entry in Dictionary
3348 * trailing ->name() method call
3349 */
3350 static int
3351compile_expr7(char_u **arg, cctx_T *cctx)
3352{
3353 typval_T rettv;
3354 char_u *start_leader, *end_leader;
3355 int ret = OK;
3356
3357 /*
3358 * Skip '!', '-' and '+' characters. They are handled later.
3359 */
3360 start_leader = *arg;
3361 while (**arg == '!' || **arg == '-' || **arg == '+')
3362 *arg = skipwhite(*arg + 1);
3363 end_leader = *arg;
3364
3365 rettv.v_type = VAR_UNKNOWN;
3366 switch (**arg)
3367 {
3368 /*
3369 * Number constant.
3370 */
3371 case '0': // also for blob starting with 0z
3372 case '1':
3373 case '2':
3374 case '3':
3375 case '4':
3376 case '5':
3377 case '6':
3378 case '7':
3379 case '8':
3380 case '9':
3381 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
3382 return FAIL;
3383 break;
3384
3385 /*
3386 * String constant: "string".
3387 */
3388 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
3389 return FAIL;
3390 break;
3391
3392 /*
3393 * Literal string constant: 'str''ing'.
3394 */
3395 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
3396 return FAIL;
3397 break;
3398
3399 /*
3400 * Constant Vim variable.
3401 */
3402 case 'v': get_vim_constant(arg, &rettv);
3403 ret = NOTDONE;
3404 break;
3405
3406 /*
3407 * List: [expr, expr]
3408 */
3409 case '[': ret = compile_list(arg, cctx);
3410 break;
3411
3412 /*
3413 * Dictionary: #{key: val, key: val}
3414 */
3415 case '#': if ((*arg)[1] == '{')
3416 {
3417 ++*arg;
3418 ret = compile_dict(arg, cctx, TRUE);
3419 }
3420 else
3421 ret = NOTDONE;
3422 break;
3423
3424 /*
3425 * Lambda: {arg, arg -> expr}
3426 * Dictionary: {'key': val, 'key': val}
3427 */
3428 case '{': {
3429 char_u *start = skipwhite(*arg + 1);
3430
3431 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003432 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003434 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 if (ret != FAIL && *start == '>')
3436 ret = compile_lambda(arg, cctx);
3437 else
3438 ret = compile_dict(arg, cctx, FALSE);
3439 }
3440 break;
3441
3442 /*
3443 * Option value: &name
3444 */
3445 case '&': ret = compile_get_option(arg, cctx);
3446 break;
3447
3448 /*
3449 * Environment variable: $VAR.
3450 */
3451 case '$': ret = compile_get_env(arg, cctx);
3452 break;
3453
3454 /*
3455 * Register contents: @r.
3456 */
3457 case '@': ret = compile_get_register(arg, cctx);
3458 break;
3459 /*
3460 * nested expression: (expression).
3461 */
3462 case '(': *arg = skipwhite(*arg + 1);
3463 ret = compile_expr1(arg, cctx); // recursive!
3464 *arg = skipwhite(*arg);
3465 if (**arg == ')')
3466 ++*arg;
3467 else if (ret == OK)
3468 {
3469 emsg(_(e_missing_close));
3470 ret = FAIL;
3471 }
3472 break;
3473
3474 default: ret = NOTDONE;
3475 break;
3476 }
3477 if (ret == FAIL)
3478 return FAIL;
3479
3480 if (rettv.v_type != VAR_UNKNOWN)
3481 {
3482 // apply the '!', '-' and '+' before the constant
3483 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
3484 {
3485 clear_tv(&rettv);
3486 return FAIL;
3487 }
3488 start_leader = end_leader; // don't apply again below
3489
3490 // push constant
3491 switch (rettv.v_type)
3492 {
3493 case VAR_BOOL:
3494 generate_PUSHBOOL(cctx, rettv.vval.v_number);
3495 break;
3496 case VAR_SPECIAL:
3497 generate_PUSHSPEC(cctx, rettv.vval.v_number);
3498 break;
3499 case VAR_NUMBER:
3500 generate_PUSHNR(cctx, rettv.vval.v_number);
3501 break;
3502#ifdef FEAT_FLOAT
3503 case VAR_FLOAT:
3504 generate_PUSHF(cctx, rettv.vval.v_float);
3505 break;
3506#endif
3507 case VAR_BLOB:
3508 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
3509 rettv.vval.v_blob = NULL;
3510 break;
3511 case VAR_STRING:
3512 generate_PUSHS(cctx, rettv.vval.v_string);
3513 rettv.vval.v_string = NULL;
3514 break;
3515 default:
3516 iemsg("constant type missing");
3517 return FAIL;
3518 }
3519 }
3520 else if (ret == NOTDONE)
3521 {
3522 char_u *p;
3523 int r;
3524
3525 if (!eval_isnamec1(**arg))
3526 {
3527 semsg(_("E1015: Name expected: %s"), *arg);
3528 return FAIL;
3529 }
3530
3531 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003532 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 if (*p == '(')
3534 r = compile_call(arg, p - *arg, cctx, 0);
3535 else
3536 r = compile_load(arg, p, cctx, TRUE);
3537 if (r == FAIL)
3538 return FAIL;
3539 }
3540
3541 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
3542 return FAIL;
3543
3544 // Now deal with prefixed '-', '+' and '!', if not done already.
3545 return compile_leader(cctx, start_leader, end_leader);
3546}
3547
3548/*
3549 * * number multiplication
3550 * / number division
3551 * % number modulo
3552 */
3553 static int
3554compile_expr6(char_u **arg, cctx_T *cctx)
3555{
3556 char_u *op;
3557
3558 // get the first variable
3559 if (compile_expr7(arg, cctx) == FAIL)
3560 return FAIL;
3561
3562 /*
3563 * Repeat computing, until no "*", "/" or "%" is following.
3564 */
3565 for (;;)
3566 {
3567 op = skipwhite(*arg);
3568 if (*op != '*' && *op != '/' && *op != '%')
3569 break;
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003570 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 {
3572 char_u buf[3];
3573
3574 vim_strncpy(buf, op, 1);
3575 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003576 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 }
3578 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003579 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003580 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581
3582 // get the second variable
3583 if (compile_expr7(arg, cctx) == FAIL)
3584 return FAIL;
3585
3586 generate_two_op(cctx, op);
3587 }
3588
3589 return OK;
3590}
3591
3592/*
3593 * + number addition
3594 * - number subtraction
3595 * .. string concatenation
3596 */
3597 static int
3598compile_expr5(char_u **arg, cctx_T *cctx)
3599{
3600 char_u *op;
3601 int oplen;
3602
3603 // get the first variable
3604 if (compile_expr6(arg, cctx) == FAIL)
3605 return FAIL;
3606
3607 /*
3608 * Repeat computing, until no "+", "-" or ".." is following.
3609 */
3610 for (;;)
3611 {
3612 op = skipwhite(*arg);
3613 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3614 break;
3615 oplen = (*op == '.' ? 2 : 1);
3616
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003617 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 {
3619 char_u buf[3];
3620
3621 vim_strncpy(buf, op, oplen);
3622 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003623 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 }
3625
3626 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003627 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003628 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629
3630 // get the second variable
3631 if (compile_expr6(arg, cctx) == FAIL)
3632 return FAIL;
3633
3634 if (*op == '.')
3635 {
3636 if (may_generate_2STRING(-2, cctx) == FAIL
3637 || may_generate_2STRING(-1, cctx) == FAIL)
3638 return FAIL;
3639 generate_instr_drop(cctx, ISN_CONCAT, 1);
3640 }
3641 else
3642 generate_two_op(cctx, op);
3643 }
3644
3645 return OK;
3646}
3647
Bram Moolenaar080457c2020-03-03 21:53:32 +01003648 static exptype_T
3649get_compare_type(char_u *p, int *len, int *type_is)
3650{
3651 exptype_T type = EXPR_UNKNOWN;
3652 int i;
3653
3654 switch (p[0])
3655 {
3656 case '=': if (p[1] == '=')
3657 type = EXPR_EQUAL;
3658 else if (p[1] == '~')
3659 type = EXPR_MATCH;
3660 break;
3661 case '!': if (p[1] == '=')
3662 type = EXPR_NEQUAL;
3663 else if (p[1] == '~')
3664 type = EXPR_NOMATCH;
3665 break;
3666 case '>': if (p[1] != '=')
3667 {
3668 type = EXPR_GREATER;
3669 *len = 1;
3670 }
3671 else
3672 type = EXPR_GEQUAL;
3673 break;
3674 case '<': if (p[1] != '=')
3675 {
3676 type = EXPR_SMALLER;
3677 *len = 1;
3678 }
3679 else
3680 type = EXPR_SEQUAL;
3681 break;
3682 case 'i': if (p[1] == 's')
3683 {
3684 // "is" and "isnot"; but not a prefix of a name
3685 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3686 *len = 5;
3687 i = p[*len];
3688 if (!isalnum(i) && i != '_')
3689 {
3690 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3691 *type_is = TRUE;
3692 }
3693 }
3694 break;
3695 }
3696 return type;
3697}
3698
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699/*
3700 * expr5a == expr5b
3701 * expr5a =~ expr5b
3702 * expr5a != expr5b
3703 * expr5a !~ expr5b
3704 * expr5a > expr5b
3705 * expr5a >= expr5b
3706 * expr5a < expr5b
3707 * expr5a <= expr5b
3708 * expr5a is expr5b
3709 * expr5a isnot expr5b
3710 *
3711 * Produces instructions:
3712 * EVAL expr5a Push result of "expr5a"
3713 * EVAL expr5b Push result of "expr5b"
3714 * COMPARE one of the compare instructions
3715 */
3716 static int
3717compile_expr4(char_u **arg, cctx_T *cctx)
3718{
3719 exptype_T type = EXPR_UNKNOWN;
3720 char_u *p;
3721 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 int type_is = FALSE;
3723
3724 // get the first variable
3725 if (compile_expr5(arg, cctx) == FAIL)
3726 return FAIL;
3727
3728 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003729 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730
3731 /*
3732 * If there is a comparative operator, use it.
3733 */
3734 if (type != EXPR_UNKNOWN)
3735 {
3736 int ic = FALSE; // Default: do not ignore case
3737
3738 if (type_is && (p[len] == '?' || p[len] == '#'))
3739 {
3740 semsg(_(e_invexpr2), *arg);
3741 return FAIL;
3742 }
3743 // extra question mark appended: ignore case
3744 if (p[len] == '?')
3745 {
3746 ic = TRUE;
3747 ++len;
3748 }
3749 // extra '#' appended: match case (ignored)
3750 else if (p[len] == '#')
3751 ++len;
3752 // nothing appended: match case
3753
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003754 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 {
3756 char_u buf[7];
3757
3758 vim_strncpy(buf, p, len);
3759 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003760 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 }
3762
3763 // get the second variable
3764 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003765 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003766 return FAIL;
3767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 if (compile_expr5(arg, cctx) == FAIL)
3769 return FAIL;
3770
3771 generate_COMPARE(cctx, type, ic);
3772 }
3773
3774 return OK;
3775}
3776
3777/*
3778 * Compile || or &&.
3779 */
3780 static int
3781compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3782{
3783 char_u *p = skipwhite(*arg);
3784 int opchar = *op;
3785
3786 if (p[0] == opchar && p[1] == opchar)
3787 {
3788 garray_T *instr = &cctx->ctx_instr;
3789 garray_T end_ga;
3790
3791 /*
3792 * Repeat until there is no following "||" or "&&"
3793 */
3794 ga_init2(&end_ga, sizeof(int), 10);
3795 while (p[0] == opchar && p[1] == opchar)
3796 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003797 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3798 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003800 return FAIL;
3801 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802
3803 if (ga_grow(&end_ga, 1) == FAIL)
3804 {
3805 ga_clear(&end_ga);
3806 return FAIL;
3807 }
3808 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3809 ++end_ga.ga_len;
3810 generate_JUMP(cctx, opchar == '|'
3811 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3812
3813 // eval the next expression
3814 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003815 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003816 return FAIL;
3817
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818 if ((opchar == '|' ? compile_expr3(arg, cctx)
3819 : compile_expr4(arg, cctx)) == FAIL)
3820 {
3821 ga_clear(&end_ga);
3822 return FAIL;
3823 }
3824 p = skipwhite(*arg);
3825 }
3826
3827 // Fill in the end label in all jumps.
3828 while (end_ga.ga_len > 0)
3829 {
3830 isn_T *isn;
3831
3832 --end_ga.ga_len;
3833 isn = ((isn_T *)instr->ga_data)
3834 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3835 isn->isn_arg.jump.jump_where = instr->ga_len;
3836 }
3837 ga_clear(&end_ga);
3838 }
3839
3840 return OK;
3841}
3842
3843/*
3844 * expr4a && expr4a && expr4a logical AND
3845 *
3846 * Produces instructions:
3847 * EVAL expr4a Push result of "expr4a"
3848 * JUMP_AND_KEEP_IF_FALSE end
3849 * EVAL expr4b Push result of "expr4b"
3850 * JUMP_AND_KEEP_IF_FALSE end
3851 * EVAL expr4c Push result of "expr4c"
3852 * end:
3853 */
3854 static int
3855compile_expr3(char_u **arg, cctx_T *cctx)
3856{
3857 // get the first variable
3858 if (compile_expr4(arg, cctx) == FAIL)
3859 return FAIL;
3860
3861 // || and && work almost the same
3862 return compile_and_or(arg, cctx, "&&");
3863}
3864
3865/*
3866 * expr3a || expr3b || expr3c logical OR
3867 *
3868 * Produces instructions:
3869 * EVAL expr3a Push result of "expr3a"
3870 * JUMP_AND_KEEP_IF_TRUE end
3871 * EVAL expr3b Push result of "expr3b"
3872 * JUMP_AND_KEEP_IF_TRUE end
3873 * EVAL expr3c Push result of "expr3c"
3874 * end:
3875 */
3876 static int
3877compile_expr2(char_u **arg, cctx_T *cctx)
3878{
3879 // eval the first expression
3880 if (compile_expr3(arg, cctx) == FAIL)
3881 return FAIL;
3882
3883 // || and && work almost the same
3884 return compile_and_or(arg, cctx, "||");
3885}
3886
3887/*
3888 * Toplevel expression: expr2 ? expr1a : expr1b
3889 *
3890 * Produces instructions:
3891 * EVAL expr2 Push result of "expr"
3892 * JUMP_IF_FALSE alt jump if false
3893 * EVAL expr1a
3894 * JUMP_ALWAYS end
3895 * alt: EVAL expr1b
3896 * end:
3897 */
3898 static int
3899compile_expr1(char_u **arg, cctx_T *cctx)
3900{
3901 char_u *p;
3902
3903 // evaluate the first expression
3904 if (compile_expr2(arg, cctx) == FAIL)
3905 return FAIL;
3906
3907 p = skipwhite(*arg);
3908 if (*p == '?')
3909 {
3910 garray_T *instr = &cctx->ctx_instr;
3911 garray_T *stack = &cctx->ctx_type_stack;
3912 int alt_idx = instr->ga_len;
3913 int end_idx;
3914 isn_T *isn;
3915 type_T *type1;
3916 type_T *type2;
3917
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003918 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3919 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003921 return FAIL;
3922 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923
3924 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3925
3926 // evaluate the second expression; any type is accepted
3927 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003928 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003929 return FAIL;
3930
Bram Moolenaara6d53682020-01-28 23:04:06 +01003931 if (compile_expr1(arg, cctx) == FAIL)
3932 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933
3934 // remember the type and drop it
3935 --stack->ga_len;
3936 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3937
3938 end_idx = instr->ga_len;
3939 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3940
3941 // jump here from JUMP_IF_FALSE
3942 isn = ((isn_T *)instr->ga_data) + alt_idx;
3943 isn->isn_arg.jump.jump_where = instr->ga_len;
3944
3945 // Check for the ":".
3946 p = skipwhite(*arg);
3947 if (*p != ':')
3948 {
3949 emsg(_(e_missing_colon));
3950 return FAIL;
3951 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003952 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3953 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003955 return FAIL;
3956 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957
3958 // evaluate the third expression
3959 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003960 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003961 return FAIL;
3962
Bram Moolenaara6d53682020-01-28 23:04:06 +01003963 if (compile_expr1(arg, cctx) == FAIL)
3964 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965
3966 // If the types differ, the result has a more generic type.
3967 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003968 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969
3970 // jump here from JUMP_ALWAYS
3971 isn = ((isn_T *)instr->ga_data) + end_idx;
3972 isn->isn_arg.jump.jump_where = instr->ga_len;
3973 }
3974 return OK;
3975}
3976
3977/*
3978 * compile "return [expr]"
3979 */
3980 static char_u *
3981compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3982{
3983 char_u *p = arg;
3984 garray_T *stack = &cctx->ctx_type_stack;
3985 type_T *stack_type;
3986
3987 if (*p != NUL && *p != '|' && *p != '\n')
3988 {
3989 // compile return argument into instructions
3990 if (compile_expr1(&p, cctx) == FAIL)
3991 return NULL;
3992
3993 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3994 if (set_return_type)
3995 cctx->ctx_ufunc->uf_ret_type = stack_type;
3996 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3997 == FAIL)
3998 return NULL;
3999 }
4000 else
4001 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004002 // "set_return_type" cannot be TRUE, only used for a lambda which
4003 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004004 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4005 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 {
4007 emsg(_("E1003: Missing return value"));
4008 return NULL;
4009 }
4010
4011 // No argument, return zero.
4012 generate_PUSHNR(cctx, 0);
4013 }
4014
4015 if (generate_instr(cctx, ISN_RETURN) == NULL)
4016 return NULL;
4017
4018 // "return val | endif" is possible
4019 return skipwhite(p);
4020}
4021
4022/*
4023 * Return the length of an assignment operator, or zero if there isn't one.
4024 */
4025 int
4026assignment_len(char_u *p, int *heredoc)
4027{
4028 if (*p == '=')
4029 {
4030 if (p[1] == '<' && p[2] == '<')
4031 {
4032 *heredoc = TRUE;
4033 return 3;
4034 }
4035 return 1;
4036 }
4037 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4038 return 2;
4039 if (STRNCMP(p, "..=", 3) == 0)
4040 return 3;
4041 return 0;
4042}
4043
4044// words that cannot be used as a variable
4045static char *reserved[] = {
4046 "true",
4047 "false",
4048 NULL
4049};
4050
4051/*
4052 * Get a line for "=<<".
4053 * Return a pointer to the line in allocated memory.
4054 * Return NULL for end-of-file or some error.
4055 */
4056 static char_u *
4057heredoc_getline(
4058 int c UNUSED,
4059 void *cookie,
4060 int indent UNUSED,
4061 int do_concat UNUSED)
4062{
4063 cctx_T *cctx = (cctx_T *)cookie;
4064
4065 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004066 {
4067 iemsg("Heredoc got to end");
4068 return NULL;
4069 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 ++cctx->ctx_lnum;
4071 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4072 [cctx->ctx_lnum]);
4073}
4074
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004075typedef enum {
4076 dest_local,
4077 dest_option,
4078 dest_env,
4079 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004080 dest_buffer,
4081 dest_window,
4082 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004083 dest_vimvar,
4084 dest_script,
4085 dest_reg,
4086} assign_dest_T;
4087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088/*
4089 * compile "let var [= expr]", "const var = expr" and "var = expr"
4090 * "arg" points to "var".
4091 */
4092 static char_u *
4093compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4094{
4095 char_u *p;
4096 char_u *ret = NULL;
4097 int var_count = 0;
4098 int semicolon = 0;
4099 size_t varlen;
4100 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004101 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004104 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004106 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 int oplen = 0;
4108 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004109 type_T *type = &t_any;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004110 lvar_T *lvar = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 char_u *name;
4112 char_u *sp;
4113 int has_type = FALSE;
4114 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
4115 int instr_count = -1;
4116
4117 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
4118 if (p == NULL)
4119 return NULL;
4120 if (var_count > 0)
4121 {
4122 // TODO: let [var, var] = list
4123 emsg("Cannot handle a list yet");
4124 return NULL;
4125 }
4126
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004127 // "a: type" is declaring variable "a" with a type, not "a:".
4128 if (is_decl && p == arg + 2 && p[-1] == ':')
4129 --p;
4130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 varlen = p - arg;
4132 name = vim_strnsave(arg, (int)varlen);
4133 if (name == NULL)
4134 return NULL;
4135
Bram Moolenaar080457c2020-03-03 21:53:32 +01004136 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004138 if (*arg == '&')
4139 {
4140 int cc;
4141 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142
Bram Moolenaar080457c2020-03-03 21:53:32 +01004143 dest = dest_option;
4144 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004146 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004147 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 if (is_decl)
4150 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004151 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 goto theend;
4153 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004154 p = arg;
4155 p = find_option_end(&p, &opt_flags);
4156 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004158 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01004159 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004160 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004161 }
4162 cc = *p;
4163 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004164 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004165 *p = cc;
4166 if (opt_type == -3)
4167 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004168 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004169 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004170 }
4171 if (opt_type == -2 || opt_type == 0)
4172 type = &t_string;
4173 else
4174 type = &t_number; // both number and boolean option
4175 }
4176 else if (*arg == '$')
4177 {
4178 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004179 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004180 if (is_decl)
4181 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004182 semsg(_("E1065: Cannot declare an environment variable: %s"),
4183 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004184 goto theend;
4185 }
4186 }
4187 else if (*arg == '@')
4188 {
4189 if (!valid_yank_reg(arg[1], TRUE))
4190 {
4191 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004192 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004193 }
4194 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004195 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004196 if (is_decl)
4197 {
4198 semsg(_("E1066: Cannot declare a register: %s"), name);
4199 goto theend;
4200 }
4201 }
4202 else if (STRNCMP(arg, "g:", 2) == 0)
4203 {
4204 dest = dest_global;
4205 if (is_decl)
4206 {
4207 semsg(_("E1016: Cannot declare a global variable: %s"), name);
4208 goto theend;
4209 }
4210 }
Bram Moolenaard3aac292020-04-19 14:32:17 +02004211 else if (STRNCMP(arg, "b:", 2) == 0)
4212 {
4213 dest = dest_buffer;
4214 if (is_decl)
4215 {
4216 semsg(_("E1078: Cannot declare a buffer variable: %s"), name);
4217 goto theend;
4218 }
4219 }
4220 else if (STRNCMP(arg, "w:", 2) == 0)
4221 {
4222 dest = dest_window;
4223 if (is_decl)
4224 {
4225 semsg(_("E1079: Cannot declare a window variable: %s"), name);
4226 goto theend;
4227 }
4228 }
4229 else if (STRNCMP(arg, "t:", 2) == 0)
4230 {
4231 dest = dest_tab;
4232 if (is_decl)
4233 {
4234 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
4235 goto theend;
4236 }
4237 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004238 else if (STRNCMP(arg, "v:", 2) == 0)
4239 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004240 typval_T *vtv;
4241 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004242
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004243 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004244 if (vimvaridx < 0)
4245 {
4246 semsg(_(e_var_notfound), arg);
4247 goto theend;
4248 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004249 // We use the current value of "sandbox" here, is that OK?
4250 if (var_check_ro(di_flags, name, FALSE))
4251 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004252 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004253 vtv = get_vim_var_tv(vimvaridx);
4254 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004255 if (is_decl)
4256 {
4257 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
4258 goto theend;
4259 }
4260 }
4261 else
4262 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004263 int idx;
4264
Bram Moolenaar080457c2020-03-03 21:53:32 +01004265 for (idx = 0; reserved[idx] != NULL; ++idx)
4266 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004268 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 goto theend;
4270 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004271
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004272 lvar = lookup_local(arg, varlen, cctx);
4273 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004275 if (is_decl)
4276 {
4277 semsg(_("E1017: Variable already declared: %s"), name);
4278 goto theend;
4279 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004280 else if (lvar->lv_const)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004281 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004282 semsg(_("E1018: Cannot assign to a constant: %s"), name);
4283 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004284 }
4285 }
4286 else if (STRNCMP(arg, "s:", 2) == 0
4287 || lookup_script(arg, varlen) == OK
4288 || find_imported(arg, varlen, cctx) != NULL)
4289 {
4290 dest = dest_script;
4291 if (is_decl)
4292 {
4293 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004294 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004295 goto theend;
4296 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004298 else if (name[1] == ':' && name[2] != NUL)
4299 {
4300 semsg(_("E1082: Cannot use a namespaced variable: %s"), name);
4301 goto theend;
4302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 }
4304 }
4305
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004306 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 {
4308 if (is_decl && *p == ':')
4309 {
4310 // parse optional type: "let var: type = expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004311 if (!VIM_ISWHITE(p[1]))
4312 {
4313 semsg(_(e_white_after), ":");
4314 goto theend;
4315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 p = skipwhite(p + 1);
4317 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 has_type = TRUE;
4319 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004320 else if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 type = lvar->lv_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 }
4323
4324 sp = p;
4325 p = skipwhite(p);
4326 op = p;
4327 oplen = assignment_len(p, &heredoc);
4328 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4329 {
4330 char_u buf[4];
4331
4332 vim_strncpy(buf, op, oplen);
4333 semsg(_(e_white_both), buf);
4334 }
4335
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004336 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02004337 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004339 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 goto theend;
4341 }
4342
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004343 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 {
4345 if (oplen > 1 && !heredoc)
4346 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004347 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4349 name);
4350 goto theend;
4351 }
4352
4353 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02004354 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004355 goto theend;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004356 lvar = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4357 if (lvar == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004359 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360 }
4361
4362 if (heredoc)
4363 {
4364 list_T *l;
4365 listitem_T *li;
4366
4367 // [let] varname =<< [trim] {end}
4368 eap->getline = heredoc_getline;
4369 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004370 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371
4372 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004373 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374 {
4375 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4376 li->li_tv.vval.v_string = NULL;
4377 }
4378 generate_NEWLIST(cctx, l->lv_len);
4379 type = &t_list_string;
4380 list_free(l);
4381 p += STRLEN(p);
4382 }
4383 else if (oplen > 0)
4384 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004385 int r;
4386 type_T *stacktype;
4387 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004388
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 // for "+=", "*=", "..=" etc. first load the current value
4390 if (*op != '=')
4391 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004392 switch (dest)
4393 {
4394 case dest_option:
4395 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004396 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004397 break;
4398 case dest_global:
4399 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4400 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004401 case dest_buffer:
4402 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4403 break;
4404 case dest_window:
4405 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4406 break;
4407 case dest_tab:
4408 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4409 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004410 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01004411 compile_load_scriptvar(cctx,
4412 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004413 break;
4414 case dest_env:
4415 // Include $ in the name here
4416 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4417 break;
4418 case dest_reg:
4419 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
4420 break;
4421 case dest_vimvar:
4422 generate_LOADV(cctx, name + 2, TRUE);
4423 break;
4424 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004425 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004426 break;
4427 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004428 }
4429
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004430 // Compile the expression. Temporarily hide the new local variable
4431 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02004432 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004433 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 instr_count = instr->ga_len;
4435 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004436 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02004437 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004438 ++cctx->ctx_locals.ga_len;
4439 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 goto theend;
4441
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004442 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004444 stack = &cctx->ctx_type_stack;
4445 stacktype = stack->ga_len == 0 ? &t_void
4446 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004447 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004449 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004451 if (stacktype->tt_type == VAR_VOID)
4452 {
4453 emsg(_("E1031: Cannot use void value"));
4454 goto theend;
4455 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004456 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004457 {
4458 // An empty list or dict has a &t_void member, for a
4459 // variable that implies &t_any.
4460 if (stacktype == &t_list_empty)
4461 lvar->lv_type = &t_list_any;
4462 else if (stacktype == &t_dict_empty)
4463 lvar->lv_type = &t_dict_any;
4464 else
4465 lvar->lv_type = stacktype;
4466 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004467 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004468 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
4469 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004471 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004472 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 }
4474 }
4475 else if (cmdidx == CMD_const)
4476 {
4477 emsg(_("E1021: const requires a value"));
4478 goto theend;
4479 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004480 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 {
4482 emsg(_("E1022: type or initialization required"));
4483 goto theend;
4484 }
4485 else
4486 {
4487 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 if (ga_grow(instr, 1) == FAIL)
4489 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004490 switch (type->tt_type)
4491 {
4492 case VAR_BOOL:
4493 generate_PUSHBOOL(cctx, VVAL_FALSE);
4494 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004495 case VAR_FLOAT:
4496#ifdef FEAT_FLOAT
4497 generate_PUSHF(cctx, 0.0);
4498#endif
4499 break;
4500 case VAR_STRING:
4501 generate_PUSHS(cctx, NULL);
4502 break;
4503 case VAR_BLOB:
4504 generate_PUSHBLOB(cctx, NULL);
4505 break;
4506 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004507 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004508 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004509 case VAR_LIST:
4510 generate_NEWLIST(cctx, 0);
4511 break;
4512 case VAR_DICT:
4513 generate_NEWDICT(cctx, 0);
4514 break;
4515 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004516 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004517 break;
4518 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004519 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004520 break;
4521 case VAR_NUMBER:
4522 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004523 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02004524 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01004525 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02004526 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01004527 generate_PUSHNR(cctx, 0);
4528 break;
4529 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 }
4531
4532 if (oplen > 0 && *op != '=')
4533 {
4534 type_T *expected = &t_number;
4535 garray_T *stack = &cctx->ctx_type_stack;
4536 type_T *stacktype;
4537
4538 // TODO: if type is known use float or any operation
4539
4540 if (*op == '.')
4541 expected = &t_string;
4542 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4543 if (need_type(stacktype, expected, -1, cctx) == FAIL)
4544 goto theend;
4545
4546 if (*op == '.')
4547 generate_instr_drop(cctx, ISN_CONCAT, 1);
4548 else
4549 {
4550 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4551
4552 if (isn == NULL)
4553 goto theend;
4554 switch (*op)
4555 {
4556 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
4557 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4558 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4559 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4560 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4561 }
4562 }
4563 }
4564
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004565 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004567 case dest_option:
4568 generate_STOREOPT(cctx, name + 1, opt_flags);
4569 break;
4570 case dest_global:
4571 // include g: with the name, easier to execute that way
4572 generate_STORE(cctx, ISN_STOREG, 0, name);
4573 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004574 case dest_buffer:
4575 // include b: with the name, easier to execute that way
4576 generate_STORE(cctx, ISN_STOREB, 0, name);
4577 break;
4578 case dest_window:
4579 // include w: with the name, easier to execute that way
4580 generate_STORE(cctx, ISN_STOREW, 0, name);
4581 break;
4582 case dest_tab:
4583 // include t: with the name, easier to execute that way
4584 generate_STORE(cctx, ISN_STORET, 0, name);
4585 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004586 case dest_env:
4587 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
4588 break;
4589 case dest_reg:
4590 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
4591 break;
4592 case dest_vimvar:
4593 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
4594 break;
4595 case dest_script:
4596 {
4597 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4598 imported_T *import = NULL;
4599 int sid = current_sctx.sc_sid;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004600 int idx;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004601
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004602 if (name[1] != ':')
4603 {
4604 import = find_imported(name, 0, cctx);
4605 if (import != NULL)
4606 sid = import->imp_sid;
4607 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004609 idx = get_script_item_idx(sid, rawname, TRUE);
4610 // TODO: specific type
4611 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004612 {
4613 char_u *name_s = name;
4614
4615 // Include s: in the name for store_var()
4616 if (name[1] != ':')
4617 {
4618 int len = (int)STRLEN(name) + 3;
4619
4620 name_s = alloc(len);
4621 if (name_s == NULL)
4622 name_s = name;
4623 else
4624 vim_snprintf((char *)name_s, len, "s:%s", name);
4625 }
4626 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
4627 if (name_s != name)
4628 vim_free(name_s);
4629 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004630 else
4631 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
4632 sid, idx, &t_any);
4633 }
4634 break;
4635 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004636 if (lvar != NULL)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004637 {
4638 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004640 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
4641 // into ISN_STORENR
4642 if (instr->ga_len == instr_count + 1
4643 && isn->isn_type == ISN_PUSHNR)
4644 {
4645 varnumber_T val = isn->isn_arg.number;
4646 garray_T *stack = &cctx->ctx_type_stack;
4647
4648 isn->isn_type = ISN_STORENR;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004649 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004650 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004651 if (stack->ga_len > 0)
4652 --stack->ga_len;
4653 }
4654 else
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004655 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004656 }
4657 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658 }
4659 ret = p;
4660
4661theend:
4662 vim_free(name);
4663 return ret;
4664}
4665
4666/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004667 * Check if "name" can be "unlet".
4668 */
4669 int
4670check_vim9_unlet(char_u *name)
4671{
4672 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
4673 {
4674 semsg(_("E1081: Cannot unlet %s"), name);
4675 return FAIL;
4676 }
4677 return OK;
4678}
4679
4680/*
4681 * Callback passed to ex_unletlock().
4682 */
4683 static int
4684compile_unlet(
4685 lval_T *lvp,
4686 char_u *name_end,
4687 exarg_T *eap,
4688 int deep UNUSED,
4689 void *coookie)
4690{
4691 cctx_T *cctx = coookie;
4692
4693 if (lvp->ll_tv == NULL)
4694 {
4695 char_u *p = lvp->ll_name;
4696 int cc = *name_end;
4697 int ret = OK;
4698
4699 // Normal name. Only supports g:, w:, t: and b: namespaces.
4700 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004701 if (*p == '$')
4702 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
4703 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004704 ret = FAIL;
4705 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004706 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004707
4708 *name_end = cc;
4709 return ret;
4710 }
4711
4712 // TODO: unlet {list}[idx]
4713 // TODO: unlet {dict}[key]
4714 emsg("Sorry, :unlet not fully implemented yet");
4715 return FAIL;
4716}
4717
4718/*
4719 * compile "unlet var", "lock var" and "unlock var"
4720 * "arg" points to "var".
4721 */
4722 static char_u *
4723compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
4724{
4725 char_u *p = arg;
4726
4727 if (eap->cmdidx != CMD_unlet)
4728 {
4729 emsg("Sorry, :lock and unlock not implemented yet");
4730 return NULL;
4731 }
4732
4733 if (*p == '!')
4734 {
4735 p = skipwhite(p + 1);
4736 eap->forceit = TRUE;
4737 }
4738
4739 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
4740 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4741}
4742
4743/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 * Compile an :import command.
4745 */
4746 static char_u *
4747compile_import(char_u *arg, cctx_T *cctx)
4748{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01004749 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004750}
4751
4752/*
4753 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
4754 */
4755 static int
4756compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
4757{
4758 garray_T *instr = &cctx->ctx_instr;
4759 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
4760
4761 if (endlabel == NULL)
4762 return FAIL;
4763 endlabel->el_next = *el;
4764 *el = endlabel;
4765 endlabel->el_end_label = instr->ga_len;
4766
4767 generate_JUMP(cctx, when, 0);
4768 return OK;
4769}
4770
4771 static void
4772compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
4773{
4774 garray_T *instr = &cctx->ctx_instr;
4775
4776 while (*el != NULL)
4777 {
4778 endlabel_T *cur = (*el);
4779 isn_T *isn;
4780
4781 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
4782 isn->isn_arg.jump.jump_where = instr->ga_len;
4783 *el = cur->el_next;
4784 vim_free(cur);
4785 }
4786}
4787
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004788 static void
4789compile_free_jump_to_end(endlabel_T **el)
4790{
4791 while (*el != NULL)
4792 {
4793 endlabel_T *cur = (*el);
4794
4795 *el = cur->el_next;
4796 vim_free(cur);
4797 }
4798}
4799
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800/*
4801 * Create a new scope and set up the generic items.
4802 */
4803 static scope_T *
4804new_scope(cctx_T *cctx, scopetype_T type)
4805{
4806 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
4807
4808 if (scope == NULL)
4809 return NULL;
4810 scope->se_outer = cctx->ctx_scope;
4811 cctx->ctx_scope = scope;
4812 scope->se_type = type;
4813 scope->se_local_count = cctx->ctx_locals.ga_len;
4814 return scope;
4815}
4816
4817/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004818 * Free the current scope and go back to the outer scope.
4819 */
4820 static void
4821drop_scope(cctx_T *cctx)
4822{
4823 scope_T *scope = cctx->ctx_scope;
4824
4825 if (scope == NULL)
4826 {
4827 iemsg("calling drop_scope() without a scope");
4828 return;
4829 }
4830 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004831 switch (scope->se_type)
4832 {
4833 case IF_SCOPE:
4834 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
4835 case FOR_SCOPE:
4836 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
4837 case WHILE_SCOPE:
4838 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
4839 case TRY_SCOPE:
4840 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
4841 case NO_SCOPE:
4842 case BLOCK_SCOPE:
4843 break;
4844 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004845 vim_free(scope);
4846}
4847
4848/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004849 * Evaluate an expression that is a constant:
4850 * has(arg)
4851 *
4852 * Also handle:
4853 * ! in front logical NOT
4854 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004855 * Return FAIL if the expression is not a constant.
4856 */
4857 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004858evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004859{
4860 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004861 char_u *start_leader, *end_leader;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004862 int has_call = FALSE;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004863
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004864 /*
4865 * Skip '!' characters. They are handled later.
4866 */
4867 start_leader = *arg;
4868 while (**arg == '!')
4869 *arg = skipwhite(*arg + 1);
4870 end_leader = *arg;
4871
4872 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004873 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004874 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004875 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4876 {
4877 tv->v_type = VAR_SPECIAL;
4878 tv->vval.v_number = VVAL_TRUE;
4879 *arg += 4;
4880 return OK;
4881 }
4882 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4883 {
4884 tv->v_type = VAR_SPECIAL;
4885 tv->vval.v_number = VVAL_FALSE;
4886 *arg += 5;
4887 return OK;
4888 }
4889
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004890 if (STRNCMP("has(", *arg, 4) == 0)
4891 {
4892 has_call = TRUE;
4893 *arg = skipwhite(*arg + 4);
4894 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004895
4896 if (**arg == '"')
4897 {
4898 if (get_string_tv(arg, tv, TRUE) == FAIL)
4899 return FAIL;
4900 }
4901 else if (**arg == '\'')
4902 {
4903 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4904 return FAIL;
4905 }
4906 else
4907 return FAIL;
4908
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004909 if (has_call)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004910 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004911 *arg = skipwhite(*arg);
4912 if (**arg != ')')
4913 return FAIL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004914 *arg = *arg + 1;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004915
4916 argvars[0] = *tv;
4917 argvars[1].v_type = VAR_UNKNOWN;
4918 tv->v_type = VAR_NUMBER;
4919 tv->vval.v_number = 0;
4920 f_has(argvars, tv);
4921 clear_tv(&argvars[0]);
4922
4923 while (start_leader < end_leader)
4924 {
4925 if (*start_leader == '!')
4926 tv->vval.v_number = !tv->vval.v_number;
4927 ++start_leader;
4928 }
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004929 }
4930
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004931 return OK;
4932}
4933
Bram Moolenaar080457c2020-03-03 21:53:32 +01004934 static int
4935evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4936{
4937 exptype_T type = EXPR_UNKNOWN;
4938 char_u *p;
4939 int len = 2;
4940 int type_is = FALSE;
4941
4942 // get the first variable
4943 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4944 return FAIL;
4945
4946 p = skipwhite(*arg);
4947 type = get_compare_type(p, &len, &type_is);
4948
4949 /*
4950 * If there is a comparative operator, use it.
4951 */
4952 if (type != EXPR_UNKNOWN)
4953 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004954 typval_T tv2;
4955 char_u *s1, *s2;
4956 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4957 int n;
4958
4959 // TODO: Only string == string is supported now
4960 if (tv->v_type != VAR_STRING)
4961 return FAIL;
4962 if (type != EXPR_EQUAL)
4963 return FAIL;
4964
4965 // get the second variable
Bram Moolenaar4227c782020-04-02 16:00:04 +02004966 init_tv(&tv2);
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004967 *arg = skipwhite(p + len);
4968 if (evaluate_const_expr7(arg, cctx, &tv2) == FAIL
4969 || tv2.v_type != VAR_STRING)
4970 {
4971 clear_tv(&tv2);
4972 return FAIL;
4973 }
4974 s1 = tv_get_string_buf(tv, buf1);
4975 s2 = tv_get_string_buf(&tv2, buf2);
4976 n = STRCMP(s1, s2);
4977 clear_tv(tv);
4978 clear_tv(&tv2);
4979 tv->v_type = VAR_BOOL;
4980 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004981 }
4982
4983 return OK;
4984}
4985
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004986static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
4987
4988/*
4989 * Compile constant || or &&.
4990 */
4991 static int
4992evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
4993{
4994 char_u *p = skipwhite(*arg);
4995 int opchar = *op;
4996
4997 if (p[0] == opchar && p[1] == opchar)
4998 {
4999 int val = tv2bool(tv);
5000
5001 /*
5002 * Repeat until there is no following "||" or "&&"
5003 */
5004 while (p[0] == opchar && p[1] == opchar)
5005 {
5006 typval_T tv2;
5007
5008 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
5009 return FAIL;
5010
5011 // eval the next expression
5012 *arg = skipwhite(p + 2);
5013 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01005014 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005015 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01005016 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005017 {
5018 clear_tv(&tv2);
5019 return FAIL;
5020 }
5021 if ((opchar == '&') == val)
5022 {
5023 // false || tv2 or true && tv2: use tv2
5024 clear_tv(tv);
5025 *tv = tv2;
5026 val = tv2bool(tv);
5027 }
5028 else
5029 clear_tv(&tv2);
5030 p = skipwhite(*arg);
5031 }
5032 }
5033
5034 return OK;
5035}
5036
5037/*
5038 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
5039 * Return FAIL if the expression is not a constant.
5040 */
5041 static int
5042evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
5043{
5044 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01005045 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005046 return FAIL;
5047
5048 // || and && work almost the same
5049 return evaluate_const_and_or(arg, cctx, "&&", tv);
5050}
5051
5052/*
5053 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
5054 * Return FAIL if the expression is not a constant.
5055 */
5056 static int
5057evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
5058{
5059 // evaluate the first expression
5060 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
5061 return FAIL;
5062
5063 // || and && work almost the same
5064 return evaluate_const_and_or(arg, cctx, "||", tv);
5065}
5066
5067/*
5068 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
5069 * E.g. for "has('feature')".
5070 * This does not produce error messages. "tv" should be cleared afterwards.
5071 * Return FAIL if the expression is not a constant.
5072 */
5073 static int
5074evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
5075{
5076 char_u *p;
5077
5078 // evaluate the first expression
5079 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
5080 return FAIL;
5081
5082 p = skipwhite(*arg);
5083 if (*p == '?')
5084 {
5085 int val = tv2bool(tv);
5086 typval_T tv2;
5087
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005088 // require space before and after the ?
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005089 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
5090 return FAIL;
5091
5092 // evaluate the second expression; any type is accepted
5093 clear_tv(tv);
5094 *arg = skipwhite(p + 1);
5095 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
5096 return FAIL;
5097
5098 // Check for the ":".
5099 p = skipwhite(*arg);
5100 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
5101 return FAIL;
5102
5103 // evaluate the third expression
5104 *arg = skipwhite(p + 1);
5105 tv2.v_type = VAR_UNKNOWN;
5106 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
5107 {
5108 clear_tv(&tv2);
5109 return FAIL;
5110 }
5111 if (val)
5112 {
5113 // use the expr after "?"
5114 clear_tv(&tv2);
5115 }
5116 else
5117 {
5118 // use the expr after ":"
5119 clear_tv(tv);
5120 *tv = tv2;
5121 }
5122 }
5123 return OK;
5124}
5125
5126/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005127 * compile "if expr"
5128 *
5129 * "if expr" Produces instructions:
5130 * EVAL expr Push result of "expr"
5131 * JUMP_IF_FALSE end
5132 * ... body ...
5133 * end:
5134 *
5135 * "if expr | else" Produces instructions:
5136 * EVAL expr Push result of "expr"
5137 * JUMP_IF_FALSE else
5138 * ... body ...
5139 * JUMP_ALWAYS end
5140 * else:
5141 * ... body ...
5142 * end:
5143 *
5144 * "if expr1 | elseif expr2 | else" Produces instructions:
5145 * EVAL expr Push result of "expr"
5146 * JUMP_IF_FALSE elseif
5147 * ... body ...
5148 * JUMP_ALWAYS end
5149 * elseif:
5150 * EVAL expr Push result of "expr"
5151 * JUMP_IF_FALSE else
5152 * ... body ...
5153 * JUMP_ALWAYS end
5154 * else:
5155 * ... body ...
5156 * end:
5157 */
5158 static char_u *
5159compile_if(char_u *arg, cctx_T *cctx)
5160{
5161 char_u *p = arg;
5162 garray_T *instr = &cctx->ctx_instr;
5163 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005164 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005165
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005166 // compile "expr"; if we know it evaluates to FALSE skip the block
5167 tv.v_type = VAR_UNKNOWN;
5168 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5169 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5170 else
5171 cctx->ctx_skip = MAYBE;
5172 clear_tv(&tv);
5173 if (cctx->ctx_skip == MAYBE)
5174 {
5175 p = arg;
5176 if (compile_expr1(&p, cctx) == FAIL)
5177 return NULL;
5178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179
5180 scope = new_scope(cctx, IF_SCOPE);
5181 if (scope == NULL)
5182 return NULL;
5183
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005184 if (cctx->ctx_skip == MAYBE)
5185 {
5186 // "where" is set when ":elseif", "else" or ":endif" is found
5187 scope->se_u.se_if.is_if_label = instr->ga_len;
5188 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5189 }
5190 else
5191 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005192
5193 return p;
5194}
5195
5196 static char_u *
5197compile_elseif(char_u *arg, cctx_T *cctx)
5198{
5199 char_u *p = arg;
5200 garray_T *instr = &cctx->ctx_instr;
5201 isn_T *isn;
5202 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005203 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204
5205 if (scope == NULL || scope->se_type != IF_SCOPE)
5206 {
5207 emsg(_(e_elseif_without_if));
5208 return NULL;
5209 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005210 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211
Bram Moolenaar158906c2020-02-06 20:39:45 +01005212 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005213 {
5214 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005215 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005216 return NULL;
5217 // previous "if" or "elseif" jumps here
5218 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5219 isn->isn_arg.jump.jump_where = instr->ga_len;
5220 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005222 // compile "expr"; if we know it evaluates to FALSE skip the block
5223 tv.v_type = VAR_UNKNOWN;
5224 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5225 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5226 else
5227 cctx->ctx_skip = MAYBE;
5228 clear_tv(&tv);
5229 if (cctx->ctx_skip == MAYBE)
5230 {
5231 p = arg;
5232 if (compile_expr1(&p, cctx) == FAIL)
5233 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005234
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005235 // "where" is set when ":elseif", "else" or ":endif" is found
5236 scope->se_u.se_if.is_if_label = instr->ga_len;
5237 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5238 }
5239 else
5240 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005241
5242 return p;
5243}
5244
5245 static char_u *
5246compile_else(char_u *arg, cctx_T *cctx)
5247{
5248 char_u *p = arg;
5249 garray_T *instr = &cctx->ctx_instr;
5250 isn_T *isn;
5251 scope_T *scope = cctx->ctx_scope;
5252
5253 if (scope == NULL || scope->se_type != IF_SCOPE)
5254 {
5255 emsg(_(e_else_without_if));
5256 return NULL;
5257 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005258 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005259
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005260 // jump from previous block to the end, unless the else block is empty
5261 if (cctx->ctx_skip == MAYBE)
5262 {
5263 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005264 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005265 return NULL;
5266 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005267
Bram Moolenaar158906c2020-02-06 20:39:45 +01005268 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005269 {
5270 if (scope->se_u.se_if.is_if_label >= 0)
5271 {
5272 // previous "if" or "elseif" jumps here
5273 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5274 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005275 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005276 }
5277 }
5278
5279 if (cctx->ctx_skip != MAYBE)
5280 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005281
5282 return p;
5283}
5284
5285 static char_u *
5286compile_endif(char_u *arg, cctx_T *cctx)
5287{
5288 scope_T *scope = cctx->ctx_scope;
5289 ifscope_T *ifscope;
5290 garray_T *instr = &cctx->ctx_instr;
5291 isn_T *isn;
5292
5293 if (scope == NULL || scope->se_type != IF_SCOPE)
5294 {
5295 emsg(_(e_endif_without_if));
5296 return NULL;
5297 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005298 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005299 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005300
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005301 if (scope->se_u.se_if.is_if_label >= 0)
5302 {
5303 // previous "if" or "elseif" jumps here
5304 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5305 isn->isn_arg.jump.jump_where = instr->ga_len;
5306 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005307 // Fill in the "end" label in jumps at the end of the blocks.
5308 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005309 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005310
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005311 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005312 return arg;
5313}
5314
5315/*
5316 * compile "for var in expr"
5317 *
5318 * Produces instructions:
5319 * PUSHNR -1
5320 * STORE loop-idx Set index to -1
5321 * EVAL expr Push result of "expr"
5322 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5323 * - if beyond end, jump to "end"
5324 * - otherwise get item from list and push it
5325 * STORE var Store item in "var"
5326 * ... body ...
5327 * JUMP top Jump back to repeat
5328 * end: DROP Drop the result of "expr"
5329 *
5330 */
5331 static char_u *
5332compile_for(char_u *arg, cctx_T *cctx)
5333{
5334 char_u *p;
5335 size_t varlen;
5336 garray_T *instr = &cctx->ctx_instr;
5337 garray_T *stack = &cctx->ctx_type_stack;
5338 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005339 lvar_T *loop_lvar; // loop iteration variable
5340 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341 type_T *vartype;
5342
5343 // TODO: list of variables: "for [key, value] in dict"
5344 // parse "var"
5345 for (p = arg; eval_isnamec1(*p); ++p)
5346 ;
5347 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005348 var_lvar = lookup_local(arg, varlen, cctx);
5349 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005350 {
5351 semsg(_("E1023: variable already defined: %s"), arg);
5352 return NULL;
5353 }
5354
5355 // consume "in"
5356 p = skipwhite(p);
5357 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5358 {
5359 emsg(_(e_missing_in));
5360 return NULL;
5361 }
5362 p = skipwhite(p + 2);
5363
5364
5365 scope = new_scope(cctx, FOR_SCOPE);
5366 if (scope == NULL)
5367 return NULL;
5368
5369 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005370 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5371 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005372 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005373 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005374 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005375 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005377
5378 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005379 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5380 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005381 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005382 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005383 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005384 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005385 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005386
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005387 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005388
5389 // compile "expr", it remains on the stack until "endfor"
5390 arg = p;
5391 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005392 {
5393 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005394 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005395 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005396
5397 // now we know the type of "var"
5398 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5399 if (vartype->tt_type != VAR_LIST)
5400 {
5401 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005402 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005403 return NULL;
5404 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005405 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005406 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005407
5408 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005409 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005410
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005411 generate_FOR(cctx, loop_lvar->lv_idx);
5412 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005413
5414 return arg;
5415}
5416
5417/*
5418 * compile "endfor"
5419 */
5420 static char_u *
5421compile_endfor(char_u *arg, cctx_T *cctx)
5422{
5423 garray_T *instr = &cctx->ctx_instr;
5424 scope_T *scope = cctx->ctx_scope;
5425 forscope_T *forscope;
5426 isn_T *isn;
5427
5428 if (scope == NULL || scope->se_type != FOR_SCOPE)
5429 {
5430 emsg(_(e_for));
5431 return NULL;
5432 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005433 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005434 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005435 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005436
5437 // At end of ":for" scope jump back to the FOR instruction.
5438 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5439
5440 // Fill in the "end" label in the FOR statement so it can jump here
5441 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5442 isn->isn_arg.forloop.for_end = instr->ga_len;
5443
5444 // Fill in the "end" label any BREAK statements
5445 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5446
5447 // Below the ":for" scope drop the "expr" list from the stack.
5448 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5449 return NULL;
5450
5451 vim_free(scope);
5452
5453 return arg;
5454}
5455
5456/*
5457 * compile "while expr"
5458 *
5459 * Produces instructions:
5460 * top: EVAL expr Push result of "expr"
5461 * JUMP_IF_FALSE end jump if false
5462 * ... body ...
5463 * JUMP top Jump back to repeat
5464 * end:
5465 *
5466 */
5467 static char_u *
5468compile_while(char_u *arg, cctx_T *cctx)
5469{
5470 char_u *p = arg;
5471 garray_T *instr = &cctx->ctx_instr;
5472 scope_T *scope;
5473
5474 scope = new_scope(cctx, WHILE_SCOPE);
5475 if (scope == NULL)
5476 return NULL;
5477
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005478 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005479
5480 // compile "expr"
5481 if (compile_expr1(&p, cctx) == FAIL)
5482 return NULL;
5483
5484 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005485 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005486 JUMP_IF_FALSE, cctx) == FAIL)
5487 return FAIL;
5488
5489 return p;
5490}
5491
5492/*
5493 * compile "endwhile"
5494 */
5495 static char_u *
5496compile_endwhile(char_u *arg, cctx_T *cctx)
5497{
5498 scope_T *scope = cctx->ctx_scope;
5499
5500 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5501 {
5502 emsg(_(e_while));
5503 return NULL;
5504 }
5505 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005506 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005507
5508 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005509 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005510
5511 // Fill in the "end" label in the WHILE statement so it can jump here.
5512 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005513 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005514
5515 vim_free(scope);
5516
5517 return arg;
5518}
5519
5520/*
5521 * compile "continue"
5522 */
5523 static char_u *
5524compile_continue(char_u *arg, cctx_T *cctx)
5525{
5526 scope_T *scope = cctx->ctx_scope;
5527
5528 for (;;)
5529 {
5530 if (scope == NULL)
5531 {
5532 emsg(_(e_continue));
5533 return NULL;
5534 }
5535 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5536 break;
5537 scope = scope->se_outer;
5538 }
5539
5540 // Jump back to the FOR or WHILE instruction.
5541 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005542 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5543 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005544 return arg;
5545}
5546
5547/*
5548 * compile "break"
5549 */
5550 static char_u *
5551compile_break(char_u *arg, cctx_T *cctx)
5552{
5553 scope_T *scope = cctx->ctx_scope;
5554 endlabel_T **el;
5555
5556 for (;;)
5557 {
5558 if (scope == NULL)
5559 {
5560 emsg(_(e_break));
5561 return NULL;
5562 }
5563 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5564 break;
5565 scope = scope->se_outer;
5566 }
5567
5568 // Jump to the end of the FOR or WHILE loop.
5569 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005570 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005571 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005572 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005573 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5574 return FAIL;
5575
5576 return arg;
5577}
5578
5579/*
5580 * compile "{" start of block
5581 */
5582 static char_u *
5583compile_block(char_u *arg, cctx_T *cctx)
5584{
5585 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5586 return NULL;
5587 return skipwhite(arg + 1);
5588}
5589
5590/*
5591 * compile end of block: drop one scope
5592 */
5593 static void
5594compile_endblock(cctx_T *cctx)
5595{
5596 scope_T *scope = cctx->ctx_scope;
5597
5598 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005599 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005600 vim_free(scope);
5601}
5602
5603/*
5604 * compile "try"
5605 * Creates a new scope for the try-endtry, pointing to the first catch and
5606 * finally.
5607 * Creates another scope for the "try" block itself.
5608 * TRY instruction sets up exception handling at runtime.
5609 *
5610 * "try"
5611 * TRY -> catch1, -> finally push trystack entry
5612 * ... try block
5613 * "throw {exception}"
5614 * EVAL {exception}
5615 * THROW create exception
5616 * ... try block
5617 * " catch {expr}"
5618 * JUMP -> finally
5619 * catch1: PUSH exeception
5620 * EVAL {expr}
5621 * MATCH
5622 * JUMP nomatch -> catch2
5623 * CATCH remove exception
5624 * ... catch block
5625 * " catch"
5626 * JUMP -> finally
5627 * catch2: CATCH remove exception
5628 * ... catch block
5629 * " finally"
5630 * finally:
5631 * ... finally block
5632 * " endtry"
5633 * ENDTRY pop trystack entry, may rethrow
5634 */
5635 static char_u *
5636compile_try(char_u *arg, cctx_T *cctx)
5637{
5638 garray_T *instr = &cctx->ctx_instr;
5639 scope_T *try_scope;
5640 scope_T *scope;
5641
5642 // scope that holds the jumps that go to catch/finally/endtry
5643 try_scope = new_scope(cctx, TRY_SCOPE);
5644 if (try_scope == NULL)
5645 return NULL;
5646
5647 // "catch" is set when the first ":catch" is found.
5648 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005649 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005650 if (generate_instr(cctx, ISN_TRY) == NULL)
5651 return NULL;
5652
5653 // scope for the try block itself
5654 scope = new_scope(cctx, BLOCK_SCOPE);
5655 if (scope == NULL)
5656 return NULL;
5657
5658 return arg;
5659}
5660
5661/*
5662 * compile "catch {expr}"
5663 */
5664 static char_u *
5665compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5666{
5667 scope_T *scope = cctx->ctx_scope;
5668 garray_T *instr = &cctx->ctx_instr;
5669 char_u *p;
5670 isn_T *isn;
5671
5672 // end block scope from :try or :catch
5673 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5674 compile_endblock(cctx);
5675 scope = cctx->ctx_scope;
5676
5677 // Error if not in a :try scope
5678 if (scope == NULL || scope->se_type != TRY_SCOPE)
5679 {
5680 emsg(_(e_catch));
5681 return NULL;
5682 }
5683
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005684 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005685 {
5686 emsg(_("E1033: catch unreachable after catch-all"));
5687 return NULL;
5688 }
5689
5690 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005691 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005692 JUMP_ALWAYS, cctx) == FAIL)
5693 return NULL;
5694
5695 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005696 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005697 if (isn->isn_arg.try.try_catch == 0)
5698 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005699 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005700 {
5701 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005702 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005703 isn->isn_arg.jump.jump_where = instr->ga_len;
5704 }
5705
5706 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02005707 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005708 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005709 scope->se_u.se_try.ts_caught_all = TRUE;
5710 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005711 }
5712 else
5713 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005714 char_u *end;
5715 char_u *pat;
5716 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005717 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005718 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005720 // Push v:exception, push {expr} and MATCH
5721 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5722
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005723 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005724 if (*end != *p)
5725 {
5726 semsg(_("E1067: Separator mismatch: %s"), p);
5727 vim_free(tofree);
5728 return FAIL;
5729 }
5730 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005731 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005732 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005733 len = (int)(end - tofree);
5734 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005735 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005736 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005737 if (pat == NULL)
5738 return FAIL;
5739 if (generate_PUSHS(cctx, pat) == FAIL)
5740 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005741
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005742 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5743 return NULL;
5744
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005745 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005746 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5747 return NULL;
5748 }
5749
5750 if (generate_instr(cctx, ISN_CATCH) == NULL)
5751 return NULL;
5752
5753 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5754 return NULL;
5755 return p;
5756}
5757
5758 static char_u *
5759compile_finally(char_u *arg, cctx_T *cctx)
5760{
5761 scope_T *scope = cctx->ctx_scope;
5762 garray_T *instr = &cctx->ctx_instr;
5763 isn_T *isn;
5764
5765 // end block scope from :try or :catch
5766 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5767 compile_endblock(cctx);
5768 scope = cctx->ctx_scope;
5769
5770 // Error if not in a :try scope
5771 if (scope == NULL || scope->se_type != TRY_SCOPE)
5772 {
5773 emsg(_(e_finally));
5774 return NULL;
5775 }
5776
5777 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005778 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005779 if (isn->isn_arg.try.try_finally != 0)
5780 {
5781 emsg(_(e_finally_dup));
5782 return NULL;
5783 }
5784
5785 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005786 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005787
Bram Moolenaar585fea72020-04-02 22:33:21 +02005788 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005789 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005790 {
5791 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005792 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005793 isn->isn_arg.jump.jump_where = instr->ga_len;
5794 }
5795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005796 // TODO: set index in ts_finally_label jumps
5797
5798 return arg;
5799}
5800
5801 static char_u *
5802compile_endtry(char_u *arg, cctx_T *cctx)
5803{
5804 scope_T *scope = cctx->ctx_scope;
5805 garray_T *instr = &cctx->ctx_instr;
5806 isn_T *isn;
5807
5808 // end block scope from :catch or :finally
5809 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5810 compile_endblock(cctx);
5811 scope = cctx->ctx_scope;
5812
5813 // Error if not in a :try scope
5814 if (scope == NULL || scope->se_type != TRY_SCOPE)
5815 {
5816 if (scope == NULL)
5817 emsg(_(e_no_endtry));
5818 else if (scope->se_type == WHILE_SCOPE)
5819 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01005820 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005821 emsg(_(e_endfor));
5822 else
5823 emsg(_(e_endif));
5824 return NULL;
5825 }
5826
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005827 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005828 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
5829 {
5830 emsg(_("E1032: missing :catch or :finally"));
5831 return NULL;
5832 }
5833
5834 // Fill in the "end" label in jumps at the end of the blocks, if not done
5835 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005836 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005837
5838 // End :catch or :finally scope: set value in ISN_TRY instruction
5839 if (isn->isn_arg.try.try_finally == 0)
5840 isn->isn_arg.try.try_finally = instr->ga_len;
5841 compile_endblock(cctx);
5842
5843 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
5844 return NULL;
5845 return arg;
5846}
5847
5848/*
5849 * compile "throw {expr}"
5850 */
5851 static char_u *
5852compile_throw(char_u *arg, cctx_T *cctx UNUSED)
5853{
5854 char_u *p = skipwhite(arg);
5855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005856 if (compile_expr1(&p, cctx) == FAIL)
5857 return NULL;
5858 if (may_generate_2STRING(-1, cctx) == FAIL)
5859 return NULL;
5860 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5861 return NULL;
5862
5863 return p;
5864}
5865
5866/*
5867 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005868 * compile "echomsg expr"
5869 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01005870 * compile "execute expr"
5871 */
5872 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005873compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01005874{
5875 char_u *p = arg;
5876 int count = 0;
5877
5878 for (;;)
5879 {
5880 if (compile_expr1(&p, cctx) == FAIL)
5881 return NULL;
5882 ++count;
5883 p = skipwhite(p);
5884 if (ends_excmd(*p))
5885 break;
5886 }
5887
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005888 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
5889 generate_ECHO(cctx, cmdidx == CMD_echo, count);
5890 else if (cmdidx == CMD_execute)
5891 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
5892 else if (cmdidx == CMD_echomsg)
5893 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
5894 else
5895 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005896 return p;
5897}
5898
5899/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005900 * A command that is not compiled, execute with legacy code.
5901 */
5902 static char_u *
5903compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
5904{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005905 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02005906 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005907
5908 if (cctx->ctx_skip == TRUE)
5909 goto theend;
5910
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02005911 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
5912 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005913 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
5914 {
5915 // expand filename in "syntax include [@group] filename"
5916 has_expr = TRUE;
5917 eap->arg = skipwhite(eap->arg + 7);
5918 if (*eap->arg == '@')
5919 eap->arg = skiptowhite(eap->arg);
5920 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005921
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005922 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005923 {
5924 int count = 0;
5925 char_u *start = skipwhite(line);
5926
5927 // :cmd xxx`=expr1`yyy`=expr2`zzz
5928 // PUSHS ":cmd xxx"
5929 // eval expr1
5930 // PUSHS "yyy"
5931 // eval expr2
5932 // PUSHS "zzz"
5933 // EXECCONCAT 5
5934 for (;;)
5935 {
5936 if (p > start)
5937 {
5938 generate_PUSHS(cctx, vim_strnsave(start, (int)(p - start)));
5939 ++count;
5940 }
5941 p += 2;
5942 if (compile_expr1(&p, cctx) == FAIL)
5943 return NULL;
5944 may_generate_2STRING(-1, cctx);
5945 ++count;
5946 p = skipwhite(p);
5947 if (*p != '`')
5948 {
5949 emsg(_("E1083: missing backtick"));
5950 return NULL;
5951 }
5952 start = p + 1;
5953
5954 p = (char_u *)strstr((char *)start, "`=");
5955 if (p == NULL)
5956 {
5957 if (*skipwhite(start) != NUL)
5958 {
5959 generate_PUSHS(cctx, vim_strsave(start));
5960 ++count;
5961 }
5962 break;
5963 }
5964 }
5965 generate_EXECCONCAT(cctx, count);
5966 }
5967 else
5968 generate_EXEC(cctx, line);
5969
5970theend:
5971 return (char_u *)"";
5972}
5973
5974/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005975 * After ex_function() has collected all the function lines: parse and compile
5976 * the lines into instructions.
5977 * Adds the function to "def_functions".
5978 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
5979 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02005980 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005981 * This can be used recursively through compile_lambda(), which may reallocate
5982 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005983 */
5984 void
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02005985compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005986{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005987 char_u *line = NULL;
5988 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005989 char *errormsg = NULL; // error message
5990 int had_return = FALSE;
5991 cctx_T cctx;
5992 garray_T *instr;
5993 int called_emsg_before = called_emsg;
5994 int ret = FAIL;
5995 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005996 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005998 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005999 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01006000
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006001 if (ufunc->uf_dfunc_idx >= 0)
6002 {
6003 // Redefining a function that was compiled before.
6004 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
6005
6006 // Free old instructions.
6007 delete_def_function_contents(dfunc);
6008 }
6009 else
6010 {
6011 // Add the function to "def_functions".
6012 if (ga_grow(&def_functions, 1) == FAIL)
6013 return;
6014 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006015 CLEAR_POINTER(dfunc);
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006016 dfunc->df_idx = def_functions.ga_len;
6017 ufunc->uf_dfunc_idx = dfunc->df_idx;
6018 dfunc->df_ufunc = ufunc;
6019 ++def_functions.ga_len;
6020 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021 }
6022
Bram Moolenaara80faa82020-04-12 19:37:17 +02006023 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006024 cctx.ctx_ufunc = ufunc;
6025 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006026 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006027 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6028 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6029 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6030 cctx.ctx_type_list = &ufunc->uf_type_list;
6031 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6032 instr = &cctx.ctx_instr;
6033
6034 // Most modern script version.
6035 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6036
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006037 if (ufunc->uf_def_args.ga_len > 0)
6038 {
6039 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006040 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006041 int i;
6042 char_u *arg;
6043 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6044
6045 // Produce instructions for the default values of optional arguments.
6046 // Store the instruction index in uf_def_arg_idx[] so that we know
6047 // where to start when the function is called, depending on the number
6048 // of arguments.
6049 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6050 if (ufunc->uf_def_arg_idx == NULL)
6051 goto erret;
6052 for (i = 0; i < count; ++i)
6053 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006054 garray_T *stack = &cctx.ctx_type_stack;
6055 type_T *val_type;
6056 int arg_idx = first_def_arg + i;
6057
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006058 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6059 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006060 if (compile_expr1(&arg, &cctx) == FAIL)
6061 goto erret;
6062
6063 // If no type specified use the type of the default value.
6064 // Otherwise check that the default value type matches the
6065 // specified type.
6066 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6067 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6068 ufunc->uf_arg_types[arg_idx] = val_type;
6069 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6070 == FAIL)
6071 {
6072 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6073 arg_idx + 1);
6074 goto erret;
6075 }
6076
6077 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006078 goto erret;
6079 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006080 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6081 }
6082
6083 /*
6084 * Loop over all the lines of the function and generate instructions.
6085 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006086 for (;;)
6087 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006088 exarg_T ea;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006089 int is_ex_command = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006090
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006091 // Bail out on the first error to avoid a flood of errors and report
6092 // the right line number when inside try/catch.
6093 if (emsg_before != called_emsg)
6094 goto erret;
6095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096 if (line != NULL && *line == '|')
6097 // the line continues after a '|'
6098 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006099 else if (line != NULL && *line != NUL
6100 && !(*line == '#' && (line == cctx.ctx_line_start
6101 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006102 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006103 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006104 goto erret;
6105 }
6106 else
6107 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006108 line = next_line_from_context(&cctx);
6109 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006110 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006111 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006112 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006113 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006114
6115 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006116 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006117 ea.cmdlinep = &line;
6118 ea.cmd = skipwhite(line);
6119
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006120 // Some things can be recognized by the first character.
6121 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006122 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006123 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006124 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006125 if (ea.cmd[1] != '{')
6126 {
6127 line = (char_u *)"";
6128 continue;
6129 }
6130 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006131
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006132 case '}':
6133 {
6134 // "}" ends a block scope
6135 scopetype_T stype = cctx.ctx_scope == NULL
6136 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006138 if (stype == BLOCK_SCOPE)
6139 {
6140 compile_endblock(&cctx);
6141 line = ea.cmd;
6142 }
6143 else
6144 {
6145 emsg(_("E1025: using } outside of a block scope"));
6146 goto erret;
6147 }
6148 if (line != NULL)
6149 line = skipwhite(ea.cmd + 1);
6150 continue;
6151 }
6152
6153 case '{':
6154 // "{" starts a block scope
6155 // "{'a': 1}->func() is something else
6156 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6157 {
6158 line = compile_block(ea.cmd, &cctx);
6159 continue;
6160 }
6161 break;
6162
6163 case ':':
6164 is_ex_command = TRUE;
6165 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006166 }
6167
6168 /*
6169 * COMMAND MODIFIERS
6170 */
6171 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6172 {
6173 if (errormsg != NULL)
6174 goto erret;
6175 // empty line or comment
6176 line = (char_u *)"";
6177 continue;
6178 }
6179
6180 // Skip ":call" to get to the function name.
6181 if (checkforcmd(&ea.cmd, "call", 3))
6182 ea.cmd = skipwhite(ea.cmd);
6183
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006184 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006185 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006186 // Assuming the command starts with a variable or function name,
6187 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
6188 // val".
6189 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
6190 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006191 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006192 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006193 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006194 int oplen;
6195 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006196
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006197 oplen = assignment_len(skipwhite(p), &heredoc);
6198 if (oplen > 0)
6199 {
6200 // Recognize an assignment if we recognize the variable
6201 // name:
6202 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006203 // "local = expr" where "local" is a local var.
6204 // "script = expr" where "script" is a script-local var.
6205 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006206 // "&opt = expr"
6207 // "$ENV = expr"
6208 // "@r = expr"
6209 if (*ea.cmd == '&'
6210 || *ea.cmd == '$'
6211 || *ea.cmd == '@'
6212 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006213 || lookup_local(ea.cmd, p - ea.cmd, &cctx) != NULL
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006214 || lookup_script(ea.cmd, p - ea.cmd) == OK
6215 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
6216 {
6217 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6218 if (line == NULL)
6219 goto erret;
6220 continue;
6221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 }
6223 }
6224 }
6225
6226 /*
6227 * COMMAND after range
6228 */
6229 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006230 p = find_ex_command(&ea, NULL, is_ex_command ? NULL
6231 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006232 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233
6234 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6235 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006236 if (cctx.ctx_skip == TRUE)
6237 {
6238 line += STRLEN(line);
6239 continue;
6240 }
6241
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242 // Expression or function call.
6243 if (ea.cmdidx == CMD_eval)
6244 {
6245 p = ea.cmd;
6246 if (compile_expr1(&p, &cctx) == FAIL)
6247 goto erret;
6248
6249 // drop the return value
6250 generate_instr_drop(&cctx, ISN_DROP, 1);
6251 line = p;
6252 continue;
6253 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006254 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006255 iemsg("Command from find_ex_command() not handled");
6256 goto erret;
6257 }
6258
6259 p = skipwhite(p);
6260
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006261 if (cctx.ctx_skip == TRUE
6262 && ea.cmdidx != CMD_elseif
6263 && ea.cmdidx != CMD_else
6264 && ea.cmdidx != CMD_endif)
6265 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006266 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006267 continue;
6268 }
6269
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006270 switch (ea.cmdidx)
6271 {
6272 case CMD_def:
6273 case CMD_function:
6274 // TODO: Nested function
6275 emsg("Nested function not implemented yet");
6276 goto erret;
6277
6278 case CMD_return:
6279 line = compile_return(p, set_return_type, &cctx);
6280 had_return = TRUE;
6281 break;
6282
6283 case CMD_let:
6284 case CMD_const:
6285 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
6286 break;
6287
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006288 case CMD_unlet:
6289 case CMD_unlockvar:
6290 case CMD_lockvar:
6291 line = compile_unletlock(p, &ea, &cctx);
6292 break;
6293
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006294 case CMD_import:
6295 line = compile_import(p, &cctx);
6296 break;
6297
6298 case CMD_if:
6299 line = compile_if(p, &cctx);
6300 break;
6301 case CMD_elseif:
6302 line = compile_elseif(p, &cctx);
6303 break;
6304 case CMD_else:
6305 line = compile_else(p, &cctx);
6306 break;
6307 case CMD_endif:
6308 line = compile_endif(p, &cctx);
6309 break;
6310
6311 case CMD_while:
6312 line = compile_while(p, &cctx);
6313 break;
6314 case CMD_endwhile:
6315 line = compile_endwhile(p, &cctx);
6316 break;
6317
6318 case CMD_for:
6319 line = compile_for(p, &cctx);
6320 break;
6321 case CMD_endfor:
6322 line = compile_endfor(p, &cctx);
6323 break;
6324 case CMD_continue:
6325 line = compile_continue(p, &cctx);
6326 break;
6327 case CMD_break:
6328 line = compile_break(p, &cctx);
6329 break;
6330
6331 case CMD_try:
6332 line = compile_try(p, &cctx);
6333 break;
6334 case CMD_catch:
6335 line = compile_catch(p, &cctx);
6336 break;
6337 case CMD_finally:
6338 line = compile_finally(p, &cctx);
6339 break;
6340 case CMD_endtry:
6341 line = compile_endtry(p, &cctx);
6342 break;
6343 case CMD_throw:
6344 line = compile_throw(p, &cctx);
6345 break;
6346
6347 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006348 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006349 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006350 case CMD_echomsg:
6351 case CMD_echoerr:
6352 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006353 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006354
6355 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006356 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006357 // Not recognized, execute with do_cmdline_cmd().
6358 ea.arg = p;
6359 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006360 break;
6361 }
6362 if (line == NULL)
6363 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006364 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006365
6366 if (cctx.ctx_type_stack.ga_len < 0)
6367 {
6368 iemsg("Type stack underflow");
6369 goto erret;
6370 }
6371 }
6372
6373 if (cctx.ctx_scope != NULL)
6374 {
6375 if (cctx.ctx_scope->se_type == IF_SCOPE)
6376 emsg(_(e_endif));
6377 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6378 emsg(_(e_endwhile));
6379 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6380 emsg(_(e_endfor));
6381 else
6382 emsg(_("E1026: Missing }"));
6383 goto erret;
6384 }
6385
6386 if (!had_return)
6387 {
6388 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6389 {
6390 emsg(_("E1027: Missing return statement"));
6391 goto erret;
6392 }
6393
6394 // Return zero if there is no return at the end.
6395 generate_PUSHNR(&cctx, 0);
6396 generate_instr(&cctx, ISN_RETURN);
6397 }
6398
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006399 {
6400 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6401 + ufunc->uf_dfunc_idx;
6402 dfunc->df_deleted = FALSE;
6403 dfunc->df_instr = instr->ga_data;
6404 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006405 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006406 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006407 if (cctx.ctx_outer_used)
6408 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006409 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006410
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006411 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006412 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006413 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006414
6415 // Create a type for the function, with the return type and any
6416 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006417 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6418 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006419 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006420 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006421 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6422 argcount, &ufunc->uf_type_list);
6423 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006424 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006425 argcount + varargs,
6426 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006427 {
6428 ret = FAIL;
6429 goto erret;
6430 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006431 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6432 ufunc->uf_func_type->tt_min_argcount =
6433 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006434 if (ufunc->uf_arg_types == NULL)
6435 {
6436 int i;
6437
6438 // lambda does not have argument types.
6439 for (i = 0; i < argcount; ++i)
6440 ufunc->uf_func_type->tt_args[i] = &t_any;
6441 }
6442 else
6443 mch_memmove(ufunc->uf_func_type->tt_args,
6444 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006445 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006446 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006447 ufunc->uf_func_type->tt_args[argcount] =
6448 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006449 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6450 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006451 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006452 else
6453 // No arguments, can use a predefined type.
6454 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6455 argcount, &ufunc->uf_type_list);
6456
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006457 }
6458
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006459 ret = OK;
6460
6461erret:
6462 if (ret == FAIL)
6463 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006464 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006465 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6466 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006467
6468 for (idx = 0; idx < instr->ga_len; ++idx)
6469 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006470 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006472 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006473 if (!dfunc->df_deleted)
6474 --def_functions.ga_len;
6475
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006476 while (cctx.ctx_scope != NULL)
6477 drop_scope(&cctx);
6478
Bram Moolenaar20431c92020-03-20 18:39:46 +01006479 // Don't execute this function body.
6480 ga_clear_strings(&ufunc->uf_lines);
6481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006482 if (errormsg != NULL)
6483 emsg(errormsg);
6484 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006485 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486 }
6487
6488 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006489 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006490 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006491 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006492}
6493
6494/*
6495 * Delete an instruction, free what it contains.
6496 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006497 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006498delete_instr(isn_T *isn)
6499{
6500 switch (isn->isn_type)
6501 {
6502 case ISN_EXEC:
6503 case ISN_LOADENV:
6504 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006505 case ISN_LOADB:
6506 case ISN_LOADW:
6507 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006508 case ISN_LOADOPT:
6509 case ISN_MEMBER:
6510 case ISN_PUSHEXC:
6511 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006512 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006513 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006514 case ISN_STOREB:
6515 case ISN_STOREW:
6516 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006517 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006518 vim_free(isn->isn_arg.string);
6519 break;
6520
6521 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006522 case ISN_STORES:
6523 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006524 break;
6525
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006526 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006527 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006528 vim_free(isn->isn_arg.unlet.ul_name);
6529 break;
6530
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006531 case ISN_STOREOPT:
6532 vim_free(isn->isn_arg.storeopt.so_name);
6533 break;
6534
6535 case ISN_PUSHBLOB: // push blob isn_arg.blob
6536 blob_unref(isn->isn_arg.blob);
6537 break;
6538
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006539 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006540#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006541 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006542#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006543 break;
6544
6545 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006546#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006547 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006548#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006549 break;
6550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006551 case ISN_UCALL:
6552 vim_free(isn->isn_arg.ufunc.cuf_name);
6553 break;
6554
6555 case ISN_2BOOL:
6556 case ISN_2STRING:
6557 case ISN_ADDBLOB:
6558 case ISN_ADDLIST:
6559 case ISN_BCALL:
6560 case ISN_CATCH:
6561 case ISN_CHECKNR:
6562 case ISN_CHECKTYPE:
6563 case ISN_COMPAREANY:
6564 case ISN_COMPAREBLOB:
6565 case ISN_COMPAREBOOL:
6566 case ISN_COMPAREDICT:
6567 case ISN_COMPAREFLOAT:
6568 case ISN_COMPAREFUNC:
6569 case ISN_COMPARELIST:
6570 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006571 case ISN_COMPARESPECIAL:
6572 case ISN_COMPARESTRING:
6573 case ISN_CONCAT:
6574 case ISN_DCALL:
6575 case ISN_DROP:
6576 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006577 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006578 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006579 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006580 case ISN_EXECCONCAT:
6581 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006582 case ISN_FOR:
6583 case ISN_FUNCREF:
6584 case ISN_INDEX:
6585 case ISN_JUMP:
6586 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006587 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006588 case ISN_LOADSCRIPT:
6589 case ISN_LOADREG:
6590 case ISN_LOADV:
6591 case ISN_NEGATENR:
6592 case ISN_NEWDICT:
6593 case ISN_NEWLIST:
6594 case ISN_OPNR:
6595 case ISN_OPFLOAT:
6596 case ISN_OPANY:
6597 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006598 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 case ISN_PUSHF:
6600 case ISN_PUSHNR:
6601 case ISN_PUSHBOOL:
6602 case ISN_PUSHSPEC:
6603 case ISN_RETURN:
6604 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006605 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006607 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608 case ISN_STORESCRIPT:
6609 case ISN_THROW:
6610 case ISN_TRY:
6611 // nothing allocated
6612 break;
6613 }
6614}
6615
6616/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006617 * Free all instructions for "dfunc".
6618 */
6619 static void
6620delete_def_function_contents(dfunc_T *dfunc)
6621{
6622 int idx;
6623
6624 ga_clear(&dfunc->df_def_args_isn);
6625
6626 if (dfunc->df_instr != NULL)
6627 {
6628 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6629 delete_instr(dfunc->df_instr + idx);
6630 VIM_CLEAR(dfunc->df_instr);
6631 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006632 if (dfunc->df_funcstack != NULL)
6633 {
6634 // Decrease the reference count for the context of a closure. If down
6635 // to zero free it and clear the variables on the stack.
6636 if (--dfunc->df_funcstack->fs_refcount == 0)
6637 {
6638 garray_T *gap = &dfunc->df_funcstack->fs_ga;
6639 typval_T *stack = gap->ga_data;
6640 int i;
6641
6642 for (i = 0; i < gap->ga_len; ++i)
6643 clear_tv(stack + i);
6644 ga_clear(gap);
6645 vim_free(dfunc->df_funcstack);
6646 }
6647 dfunc->df_funcstack = NULL;
6648 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006649
6650 dfunc->df_deleted = TRUE;
6651}
6652
6653/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006654 * When a user function is deleted, delete any associated def function.
6655 */
6656 void
6657delete_def_function(ufunc_T *ufunc)
6658{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006659 if (ufunc->uf_dfunc_idx >= 0)
6660 {
6661 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6662 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006663
Bram Moolenaar20431c92020-03-20 18:39:46 +01006664 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665 }
6666}
6667
6668#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006669/*
6670 * Free all functions defined with ":def".
6671 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006672 void
6673free_def_functions(void)
6674{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006675 int idx;
6676
6677 for (idx = 0; idx < def_functions.ga_len; ++idx)
6678 {
6679 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6680
6681 delete_def_function_contents(dfunc);
6682 }
6683
6684 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685}
6686#endif
6687
6688
6689#endif // FEAT_EVAL