blob: 5644c50358571ff0f42c756a65021579bdfed6be [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 {
1666 p = skip_type(p);
1667 if (*p == ',')
1668 p = skipwhite(p + 1);
1669 }
1670 if (*p == ')' && p[1] == ':')
1671 p = skip_type(skipwhite(p + 2));
1672 }
1673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674 return p;
1675}
1676
1677/*
1678 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001679 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 * Returns NULL in case of failure.
1681 */
1682 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001683parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684{
1685 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001686 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687
1688 if (**arg != '<')
1689 {
1690 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001691 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692 else
1693 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001694 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 }
1696 *arg = skipwhite(*arg + 1);
1697
Bram Moolenaard77a8522020-04-03 21:59:57 +02001698 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699
1700 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001701 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 {
1703 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001704 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705 }
1706 ++*arg;
1707
1708 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001709 return get_list_type(member_type, type_gap);
1710 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711}
1712
1713/*
1714 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001715 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 */
1717 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001718parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719{
1720 char_u *p = *arg;
1721 size_t len;
1722
1723 // skip over the first word
1724 while (ASCII_ISALNUM(*p) || *p == '_')
1725 ++p;
1726 len = p - *arg;
1727
1728 switch (**arg)
1729 {
1730 case 'a':
1731 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1732 {
1733 *arg += len;
1734 return &t_any;
1735 }
1736 break;
1737 case 'b':
1738 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1739 {
1740 *arg += len;
1741 return &t_bool;
1742 }
1743 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1744 {
1745 *arg += len;
1746 return &t_blob;
1747 }
1748 break;
1749 case 'c':
1750 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1751 {
1752 *arg += len;
1753 return &t_channel;
1754 }
1755 break;
1756 case 'd':
1757 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1758 {
1759 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001760 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 }
1762 break;
1763 case 'f':
1764 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1765 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001766#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767 *arg += len;
1768 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001769#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001770 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001771 return &t_any;
1772#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 }
1774 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1775 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001776 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001777 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001778 int argcount = -1;
1779 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001780 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001781 type_T *arg_type[MAX_FUNC_ARGS + 1];
1782
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001783 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001785 if (**arg == '(')
1786 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001787 // "func" may or may not return a value, "func()" does
1788 // not return a value.
1789 ret_type = &t_void;
1790
Bram Moolenaard77a8522020-04-03 21:59:57 +02001791 p = ++*arg;
1792 argcount = 0;
1793 while (*p != NUL && *p != ')')
1794 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001795 if (*p == '?')
1796 {
1797 if (first_optional == -1)
1798 first_optional = argcount;
1799 ++p;
1800 }
1801 else if (first_optional != -1)
1802 {
1803 emsg(_("E1007: mandatory argument after optional argument"));
1804 return &t_any;
1805 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001806 else if (STRNCMP(p, "...", 3) == 0)
1807 {
1808 flags |= TTFLAG_VARARGS;
1809 p += 3;
1810 }
1811
1812 arg_type[argcount++] = parse_type(&p, type_gap);
1813
1814 // Nothing comes after "...{type}".
1815 if (flags & TTFLAG_VARARGS)
1816 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001817
Bram Moolenaard77a8522020-04-03 21:59:57 +02001818 if (*p != ',' && *skipwhite(p) == ',')
1819 {
1820 semsg(_(e_no_white_before), ",");
1821 return &t_any;
1822 }
1823 if (*p == ',')
1824 {
1825 ++p;
1826 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001827 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001828 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001829 return &t_any;
1830 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001831 }
1832 p = skipwhite(p);
1833 if (argcount == MAX_FUNC_ARGS)
1834 {
1835 emsg(_("E740: Too many argument types"));
1836 return &t_any;
1837 }
1838 }
1839
1840 p = skipwhite(p);
1841 if (*p != ')')
1842 {
1843 emsg(_(e_missing_close));
1844 return &t_any;
1845 }
1846 *arg = p + 1;
1847 }
1848 if (**arg == ':')
1849 {
1850 // parse return type
1851 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001852 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001853 semsg(_(e_white_after), ":");
1854 *arg = skipwhite(*arg);
1855 ret_type = parse_type(arg, type_gap);
1856 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001857 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001858 type = get_func_type(ret_type, argcount, type_gap);
1859 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001860 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001861 type = alloc_func_type(ret_type, argcount, type_gap);
1862 type->tt_flags = flags;
1863 if (argcount > 0)
1864 {
1865 type->tt_argcount = argcount;
1866 type->tt_min_argcount = first_optional == -1
1867 ? argcount : first_optional;
1868 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001869 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001870 return &t_any;
1871 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001872 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001873 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001874 }
1875 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 }
1877 break;
1878 case 'j':
1879 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1880 {
1881 *arg += len;
1882 return &t_job;
1883 }
1884 break;
1885 case 'l':
1886 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1887 {
1888 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001889 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 }
1891 break;
1892 case 'n':
1893 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1894 {
1895 *arg += len;
1896 return &t_number;
1897 }
1898 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 case 's':
1900 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1901 {
1902 *arg += len;
1903 return &t_string;
1904 }
1905 break;
1906 case 'v':
1907 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1908 {
1909 *arg += len;
1910 return &t_void;
1911 }
1912 break;
1913 }
1914
1915 semsg(_("E1010: Type not recognized: %s"), *arg);
1916 return &t_any;
1917}
1918
1919/*
1920 * Check if "type1" and "type2" are exactly the same.
1921 */
1922 static int
1923equal_type(type_T *type1, type_T *type2)
1924{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001925 int i;
1926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927 if (type1->tt_type != type2->tt_type)
1928 return FALSE;
1929 switch (type1->tt_type)
1930 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001932 case VAR_ANY:
1933 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 case VAR_SPECIAL:
1935 case VAR_BOOL:
1936 case VAR_NUMBER:
1937 case VAR_FLOAT:
1938 case VAR_STRING:
1939 case VAR_BLOB:
1940 case VAR_JOB:
1941 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001942 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 case VAR_LIST:
1944 case VAR_DICT:
1945 return equal_type(type1->tt_member, type2->tt_member);
1946 case VAR_FUNC:
1947 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001948 if (!equal_type(type1->tt_member, type2->tt_member)
1949 || type1->tt_argcount != type2->tt_argcount)
1950 return FALSE;
1951 if (type1->tt_argcount < 0
1952 || type1->tt_args == NULL || type2->tt_args == NULL)
1953 return TRUE;
1954 for (i = 0; i < type1->tt_argcount; ++i)
1955 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
1956 return FALSE;
1957 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 }
1959 return TRUE;
1960}
1961
1962/*
1963 * Find the common type of "type1" and "type2" and put it in "dest".
1964 * "type2" and "dest" may be the same.
1965 */
1966 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02001967common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968{
1969 if (equal_type(type1, type2))
1970 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001971 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 return;
1973 }
1974
1975 if (type1->tt_type == type2->tt_type)
1976 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1978 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001979 type_T *common;
1980
Bram Moolenaard77a8522020-04-03 21:59:57 +02001981 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001982 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001983 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001984 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001985 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 return;
1987 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001988 if (type1->tt_type == VAR_FUNC)
1989 {
1990 type_T *common;
1991
1992 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
1993 if (type1->tt_argcount == type2->tt_argcount
1994 && type1->tt_argcount >= 0)
1995 {
1996 int argcount = type1->tt_argcount;
1997 int i;
1998
1999 *dest = alloc_func_type(common, argcount, type_gap);
2000 if (type1->tt_args != NULL && type2->tt_args != NULL)
2001 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002002 if (func_type_add_arg_types(*dest, argcount,
2003 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002004 for (i = 0; i < argcount; ++i)
2005 common_type(type1->tt_args[i], type2->tt_args[i],
2006 &(*dest)->tt_args[i], type_gap);
2007 }
2008 }
2009 else
2010 *dest = alloc_func_type(common, -1, type_gap);
2011 return;
2012 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 }
2014
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002015 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016}
2017
2018 char *
2019vartype_name(vartype_T type)
2020{
2021 switch (type)
2022 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002023 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002024 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 case VAR_SPECIAL: return "special";
2027 case VAR_BOOL: return "bool";
2028 case VAR_NUMBER: return "number";
2029 case VAR_FLOAT: return "float";
2030 case VAR_STRING: return "string";
2031 case VAR_BLOB: return "blob";
2032 case VAR_JOB: return "job";
2033 case VAR_CHANNEL: return "channel";
2034 case VAR_LIST: return "list";
2035 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002036
2037 case VAR_FUNC:
2038 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002040 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002041}
2042
2043/*
2044 * Return the name of a type.
2045 * The result may be in allocated memory, in which case "tofree" is set.
2046 */
2047 char *
2048type_name(type_T *type, char **tofree)
2049{
2050 char *name = vartype_name(type->tt_type);
2051
2052 *tofree = NULL;
2053 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2054 {
2055 char *member_free;
2056 char *member_name = type_name(type->tt_member, &member_free);
2057 size_t len;
2058
2059 len = STRLEN(name) + STRLEN(member_name) + 3;
2060 *tofree = alloc(len);
2061 if (*tofree != NULL)
2062 {
2063 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2064 vim_free(member_free);
2065 return *tofree;
2066 }
2067 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002068 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002069 {
2070 garray_T ga;
2071 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002072 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002073
2074 ga_init2(&ga, 1, 100);
2075 if (ga_grow(&ga, 20) == FAIL)
2076 return "[unknown]";
2077 *tofree = ga.ga_data;
2078 STRCPY(ga.ga_data, "func(");
2079 ga.ga_len += 5;
2080
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002081 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002082 {
2083 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002084 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002085 int len;
2086
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002087 if (type->tt_args == NULL)
2088 arg_type = "[unknown]";
2089 else
2090 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002091 if (i > 0)
2092 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002093 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002094 ga.ga_len += 2;
2095 }
2096 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002097 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002098 {
2099 vim_free(arg_free);
2100 return "[unknown]";
2101 }
2102 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002103 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002104 {
2105 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2106 ga.ga_len += 3;
2107 }
2108 else if (i >= type->tt_min_argcount)
2109 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002110 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002111 ga.ga_len += len;
2112 vim_free(arg_free);
2113 }
2114
2115 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002116 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002117 else
2118 {
2119 char *ret_free;
2120 char *ret_name = type_name(type->tt_member, &ret_free);
2121 int len;
2122
2123 len = (int)STRLEN(ret_name) + 4;
2124 if (ga_grow(&ga, len) == FAIL)
2125 {
2126 vim_free(ret_free);
2127 return "[unknown]";
2128 }
2129 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002130 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2131 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002132 vim_free(ret_free);
2133 }
2134 return ga.ga_data;
2135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136
2137 return name;
2138}
2139
2140/*
2141 * Find "name" in script-local items of script "sid".
2142 * Returns the index in "sn_var_vals" if found.
2143 * If found but not in "sn_var_vals" returns -1.
2144 * If not found returns -2.
2145 */
2146 int
2147get_script_item_idx(int sid, char_u *name, int check_writable)
2148{
2149 hashtab_T *ht;
2150 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002151 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002152 int idx;
2153
2154 // First look the name up in the hashtable.
2155 if (sid <= 0 || sid > script_items.ga_len)
2156 return -1;
2157 ht = &SCRIPT_VARS(sid);
2158 di = find_var_in_ht(ht, 0, name, TRUE);
2159 if (di == NULL)
2160 return -2;
2161
2162 // Now find the svar_T index in sn_var_vals.
2163 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2164 {
2165 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2166
2167 if (sv->sv_tv == &di->di_tv)
2168 {
2169 if (check_writable && sv->sv_const)
2170 semsg(_(e_readonlyvar), name);
2171 return idx;
2172 }
2173 }
2174 return -1;
2175}
2176
2177/*
2178 * Find "name" in imported items of the current script/
2179 */
2180 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002181find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002183 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184 int idx;
2185
2186 if (cctx != NULL)
2187 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2188 {
2189 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2190 + idx;
2191
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002192 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2193 : STRLEN(import->imp_name) == len
2194 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195 return import;
2196 }
2197
2198 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2199 {
2200 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2201
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002202 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2203 : STRLEN(import->imp_name) == len
2204 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 return import;
2206 }
2207 return NULL;
2208}
2209
2210/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002211 * Free all imported variables.
2212 */
2213 static void
2214free_imported(cctx_T *cctx)
2215{
2216 int idx;
2217
2218 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2219 {
2220 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2221
2222 vim_free(import->imp_name);
2223 }
2224 ga_clear(&cctx->ctx_imports);
2225}
2226
2227/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002228 * Get the next line of the function from "cctx".
2229 * Returns NULL when at the end.
2230 */
2231 static char_u *
2232next_line_from_context(cctx_T *cctx)
2233{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002234 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002235
2236 do
2237 {
2238 ++cctx->ctx_lnum;
2239 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002240 {
2241 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002242 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002243 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002244 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002245 cctx->ctx_line_start = line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002246 SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum
2247 + cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002248 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002249 return line;
2250}
2251
2252/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002253 * Return TRUE if "p" points at a "#" but not at "#{".
2254 */
2255 static int
2256comment_start(char_u *p)
2257{
2258 return p[0] == '#' && p[1] != '{';
2259}
2260
2261/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002262 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002263 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002264 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2265 */
2266 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002267may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002268{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002269 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002270 {
2271 char_u *next = next_line_from_context(cctx);
2272
2273 if (next == NULL)
2274 return FAIL;
2275 *arg = skipwhite(next);
2276 }
2277 return OK;
2278}
2279
2280/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002281 * Generate an instruction to load script-local variable "name", without the
2282 * leading "s:".
2283 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 */
2285 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002286compile_load_scriptvar(
2287 cctx_T *cctx,
2288 char_u *name, // variable NUL terminated
2289 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002290 char_u **end, // end of variable
2291 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002293 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2295 imported_T *import;
2296
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002297 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002299 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002300 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2301 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302 }
2303 if (idx >= 0)
2304 {
2305 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2306
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002307 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 current_sctx.sc_sid, idx, sv->sv_type);
2309 return OK;
2310 }
2311
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002312 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 if (import != NULL)
2314 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002315 if (import->imp_all)
2316 {
2317 char_u *p = skipwhite(*end);
2318 int name_len;
2319 ufunc_T *ufunc;
2320 type_T *type;
2321
2322 // Used "import * as Name", need to lookup the member.
2323 if (*p != '.')
2324 {
2325 semsg(_("E1060: expected dot after name: %s"), start);
2326 return FAIL;
2327 }
2328 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002329 if (VIM_ISWHITE(*p))
2330 {
2331 emsg(_("E1074: no white space allowed after dot"));
2332 return FAIL;
2333 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002334
2335 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2336 // TODO: what if it is a function?
2337 if (idx < 0)
2338 return FAIL;
2339 *end = p;
2340
2341 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2342 import->imp_sid,
2343 idx,
2344 type);
2345 }
2346 else
2347 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002348 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002349 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2350 import->imp_sid,
2351 import->imp_var_vals_idx,
2352 import->imp_type);
2353 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 return OK;
2355 }
2356
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002357 if (error)
2358 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 return FAIL;
2360}
2361
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002362 static int
2363generate_funcref(cctx_T *cctx, char_u *name)
2364{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002365 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002366
2367 if (ufunc == NULL)
2368 return FAIL;
2369
2370 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2371}
2372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373/*
2374 * Compile a variable name into a load instruction.
2375 * "end" points to just after the name.
2376 * When "error" is FALSE do not give an error when not found.
2377 */
2378 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002379compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380{
2381 type_T *type;
2382 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002383 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002385 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386
2387 if (*(*arg + 1) == ':')
2388 {
2389 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002390 if (end <= *arg + 2)
2391 name = vim_strsave((char_u *)"[empty]");
2392 else
2393 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 if (name == NULL)
2395 return FAIL;
2396
2397 if (**arg == 'v')
2398 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002399 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400 }
2401 else if (**arg == 'g')
2402 {
2403 // Global variables can be defined later, thus we don't check if it
2404 // exists, give error at runtime.
2405 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2406 }
2407 else if (**arg == 's')
2408 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002409 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002411 else if (**arg == 'b')
2412 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002413 // Buffer-local variables can be defined later, thus we don't check
2414 // if it exists, give error at runtime.
2415 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002416 }
2417 else if (**arg == 'w')
2418 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002419 // Window-local variables can be defined later, thus we don't check
2420 // if it exists, give error at runtime.
2421 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002422 }
2423 else if (**arg == 't')
2424 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002425 // Tabpage-local variables can be defined later, thus we don't
2426 // check if it exists, give error at runtime.
2427 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002428 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 else
2430 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002431 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 goto theend;
2433 }
2434 }
2435 else
2436 {
2437 size_t len = end - *arg;
2438 int idx;
2439 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002440 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441
2442 name = vim_strnsave(*arg, end - *arg);
2443 if (name == NULL)
2444 return FAIL;
2445
2446 idx = lookup_arg(*arg, len, cctx);
2447 if (idx >= 0)
2448 {
2449 if (cctx->ctx_ufunc->uf_arg_types != NULL)
2450 type = cctx->ctx_ufunc->uf_arg_types[idx];
2451 else
2452 type = &t_any;
2453
2454 // Arguments are located above the frame pointer.
2455 idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE;
2456 if (cctx->ctx_ufunc->uf_va_name != NULL)
2457 --idx;
2458 gen_load = TRUE;
2459 }
2460 else if (lookup_vararg(*arg, len, cctx))
2461 {
2462 // varargs is always the last argument
2463 idx = -STACK_FRAME_SIZE - 1;
2464 type = cctx->ctx_ufunc->uf_va_type;
2465 gen_load = TRUE;
2466 }
2467 else
2468 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002469 lvar_T *lvar = lookup_local(*arg, len, cctx);
2470
2471 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002473 type = lvar->lv_type;
2474 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002475 if (lvar->lv_from_outer)
2476 gen_load_outer = TRUE;
2477 else
2478 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 }
2480 else
2481 {
2482 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2483 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2484 res = generate_PUSHBOOL(cctx, **arg == 't'
2485 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002486 else
2487 {
2488 // "var" can be script-local even without using "s:" if it
2489 // already exists.
2490 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2491 == SCRIPT_VERSION_VIM9
2492 || lookup_script(*arg, len) == OK)
2493 res = compile_load_scriptvar(cctx, name, *arg, &end,
2494 FALSE);
2495
2496 // When the name starts with an uppercase letter or "x:" it
2497 // can be a user defined function.
2498 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2499 res = generate_funcref(cctx, name);
2500 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002501 }
2502 }
2503 if (gen_load)
2504 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002505 if (gen_load_outer)
2506 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 }
2508
2509 *arg = end;
2510
2511theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002512 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 semsg(_(e_var_notfound), name);
2514 vim_free(name);
2515 return res;
2516}
2517
2518/*
2519 * Compile the argument expressions.
2520 * "arg" points to just after the "(" and is advanced to after the ")"
2521 */
2522 static int
2523compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2524{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002525 char_u *p = *arg;
2526 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527
Bram Moolenaare6085c52020-04-12 20:19:16 +02002528 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002530 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002531 {
2532 p = next_line_from_context(cctx);
2533 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002534 goto failret;
2535 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002536 p = skipwhite(p);
2537 }
2538 if (*p == ')')
2539 {
2540 *arg = p + 1;
2541 return OK;
2542 }
2543
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 if (compile_expr1(&p, cctx) == FAIL)
2545 return FAIL;
2546 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002547
2548 if (*p != ',' && *skipwhite(p) == ',')
2549 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002550 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002551 p = skipwhite(p);
2552 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002554 {
2555 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002556 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002557 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002558 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002559 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002560 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002562failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002563 emsg(_(e_missing_close));
2564 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002565}
2566
2567/*
2568 * Compile a function call: name(arg1, arg2)
2569 * "arg" points to "name", "arg + varlen" to the "(".
2570 * "argcount_init" is 1 for "value->method()"
2571 * Instructions:
2572 * EVAL arg1
2573 * EVAL arg2
2574 * BCALL / DCALL / UCALL
2575 */
2576 static int
2577compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2578{
2579 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002580 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 int argcount = argcount_init;
2582 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002583 char_u fname_buf[FLEN_FIXED + 1];
2584 char_u *tofree = NULL;
2585 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002587 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588
2589 if (varlen >= sizeof(namebuf))
2590 {
2591 semsg(_("E1011: name too long: %s"), name);
2592 return FAIL;
2593 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002594 vim_strncpy(namebuf, *arg, varlen);
2595 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596
2597 *arg = skipwhite(*arg + varlen + 1);
2598 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002599 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002601 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 {
2603 int idx;
2604
2605 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002606 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002608 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002609 else
2610 semsg(_(e_unknownfunc), namebuf);
2611 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612 }
2613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002615 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002617 {
2618 res = generate_CALL(cctx, ufunc, argcount);
2619 goto theend;
2620 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621
2622 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002623 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002625 if (STRNCMP(namebuf, "g:", 2) != 0
2626 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002627 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002628 garray_T *stack = &cctx->ctx_type_stack;
2629 type_T *type;
2630
2631 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2632 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002633 goto theend;
2634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002636 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002637 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002638 if (STRNCMP(namebuf, "g:", 2) == 0)
2639 res = generate_UCALL(cctx, name, argcount);
2640 else
2641 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002642
2643theend:
2644 vim_free(tofree);
2645 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646}
2647
2648// like NAMESPACE_CHAR but with 'a' and 'l'.
2649#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2650
2651/*
2652 * Find the end of a variable or function name. Unlike find_name_end() this
2653 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002654 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 * Return a pointer to just after the name. Equal to "arg" if there is no
2656 * valid name.
2657 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002658 static char_u *
2659to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660{
2661 char_u *p;
2662
2663 // Quick check for valid starting character.
2664 if (!eval_isnamec1(*arg))
2665 return arg;
2666
2667 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2668 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2669 // and can be used in slice "[n:]".
2670 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002671 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2673 break;
2674 return p;
2675}
2676
2677/*
2678 * Like to_name_end() but also skip over a list or dict constant.
2679 */
2680 char_u *
2681to_name_const_end(char_u *arg)
2682{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002683 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 typval_T rettv;
2685
2686 if (p == arg && *arg == '[')
2687 {
2688
2689 // Can be "[1, 2, 3]->Func()".
2690 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2691 p = arg;
2692 }
2693 else if (p == arg && *arg == '#' && arg[1] == '{')
2694 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002695 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 ++p;
2697 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2698 p = arg;
2699 }
2700 else if (p == arg && *arg == '{')
2701 {
2702 int ret = get_lambda_tv(&p, &rettv, FALSE);
2703
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002704 // Can be "{x -> ret}()".
2705 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706 if (ret == NOTDONE)
2707 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2708 if (ret != OK)
2709 p = arg;
2710 }
2711
2712 return p;
2713}
2714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715/*
2716 * parse a list: [expr, expr]
2717 * "*arg" points to the '['.
2718 */
2719 static int
2720compile_list(char_u **arg, cctx_T *cctx)
2721{
2722 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002723 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 int count = 0;
2725
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002726 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002728 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002729 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002730 p = next_line_from_context(cctx);
2731 if (p == NULL)
2732 {
2733 semsg(_(e_list_end), *arg);
2734 return FAIL;
2735 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002736 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002737 p = skipwhite(p);
2738 }
2739 if (*p == ']')
2740 {
2741 ++p;
2742 // Allow for following comment, after at least one space.
2743 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
2744 p += STRLEN(p);
2745 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002746 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747 if (compile_expr1(&p, cctx) == FAIL)
2748 break;
2749 ++count;
2750 if (*p == ',')
2751 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002752 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 p = skipwhite(p);
2754 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002755 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756
2757 generate_NEWLIST(cctx, count);
2758 return OK;
2759}
2760
2761/*
2762 * parse a lambda: {arg, arg -> expr}
2763 * "*arg" points to the '{'.
2764 */
2765 static int
2766compile_lambda(char_u **arg, cctx_T *cctx)
2767{
2768 garray_T *instr = &cctx->ctx_instr;
2769 typval_T rettv;
2770 ufunc_T *ufunc;
2771
2772 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002773 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002777 ++ufunc->uf_refcount;
2778 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002779 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780
2781 // The function will have one line: "return {expr}".
2782 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002783 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784
2785 if (ufunc->uf_dfunc_idx >= 0)
2786 {
2787 if (ga_grow(instr, 1) == FAIL)
2788 return FAIL;
2789 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2790 return OK;
2791 }
2792 return FAIL;
2793}
2794
2795/*
2796 * Compile a lamda call: expr->{lambda}(args)
2797 * "arg" points to the "{".
2798 */
2799 static int
2800compile_lambda_call(char_u **arg, cctx_T *cctx)
2801{
2802 ufunc_T *ufunc;
2803 typval_T rettv;
2804 int argcount = 1;
2805 int ret = FAIL;
2806
2807 // Get the funcref in "rettv".
2808 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2809 return FAIL;
2810
2811 if (**arg != '(')
2812 {
2813 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002814 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 else
2816 semsg(_(e_missing_paren), "lambda");
2817 clear_tv(&rettv);
2818 return FAIL;
2819 }
2820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 ufunc = rettv.vval.v_partial->pt_func;
2822 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002823 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002824 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002825
2826 // The function will have one line: "return {expr}".
2827 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002828 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829
2830 // compile the arguments
2831 *arg = skipwhite(*arg + 1);
2832 if (compile_arguments(arg, cctx, &argcount) == OK)
2833 // call the compiled function
2834 ret = generate_CALL(cctx, ufunc, argcount);
2835
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 return ret;
2837}
2838
2839/*
2840 * parse a dict: {'key': val} or #{key: val}
2841 * "*arg" points to the '{'.
2842 */
2843 static int
2844compile_dict(char_u **arg, cctx_T *cctx, int literal)
2845{
2846 garray_T *instr = &cctx->ctx_instr;
2847 int count = 0;
2848 dict_T *d = dict_alloc();
2849 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002850 char_u *whitep = *arg;
2851 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852
2853 if (d == NULL)
2854 return FAIL;
2855 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002856 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 {
2858 char_u *key = NULL;
2859
Bram Moolenaar2c330432020-04-13 14:41:35 +02002860 while (**arg == NUL || (literal && **arg == '"')
2861 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002862 {
2863 *arg = next_line_from_context(cctx);
2864 if (*arg == NULL)
2865 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002866 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002867 *arg = skipwhite(*arg);
2868 }
2869
2870 if (**arg == '}')
2871 break;
2872
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 if (literal)
2874 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002875 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876
Bram Moolenaar2c330432020-04-13 14:41:35 +02002877 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 {
2879 semsg(_("E1014: Invalid key: %s"), *arg);
2880 return FAIL;
2881 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002882 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 if (generate_PUSHS(cctx, key) == FAIL)
2884 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002885 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 }
2887 else
2888 {
2889 isn_T *isn;
2890
2891 if (compile_expr1(arg, cctx) == FAIL)
2892 return FAIL;
2893 // TODO: check type is string
2894 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2895 if (isn->isn_type == ISN_PUSHS)
2896 key = isn->isn_arg.string;
2897 }
2898
2899 // Check for duplicate keys, if using string keys.
2900 if (key != NULL)
2901 {
2902 item = dict_find(d, key, -1);
2903 if (item != NULL)
2904 {
2905 semsg(_(e_duplicate_key), key);
2906 goto failret;
2907 }
2908 item = dictitem_alloc(key);
2909 if (item != NULL)
2910 {
2911 item->di_tv.v_type = VAR_UNKNOWN;
2912 item->di_tv.v_lock = 0;
2913 if (dict_add(d, item) == FAIL)
2914 dictitem_free(item);
2915 }
2916 }
2917
2918 *arg = skipwhite(*arg);
2919 if (**arg != ':')
2920 {
2921 semsg(_(e_missing_dict_colon), *arg);
2922 return FAIL;
2923 }
2924
Bram Moolenaar2c330432020-04-13 14:41:35 +02002925 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002927 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002928 {
2929 *arg = next_line_from_context(cctx);
2930 if (*arg == NULL)
2931 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002932 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002933 *arg = skipwhite(*arg);
2934 }
2935
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 if (compile_expr1(arg, cctx) == FAIL)
2937 return FAIL;
2938 ++count;
2939
Bram Moolenaar2c330432020-04-13 14:41:35 +02002940 whitep = *arg;
2941 p = skipwhite(*arg);
2942 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002943 {
2944 *arg = next_line_from_context(cctx);
2945 if (*arg == NULL)
2946 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002947 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002948 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002949 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002950 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 if (**arg == '}')
2952 break;
2953 if (**arg != ',')
2954 {
2955 semsg(_(e_missing_dict_comma), *arg);
2956 goto failret;
2957 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002958 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 *arg = skipwhite(*arg + 1);
2960 }
2961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 *arg = *arg + 1;
2963
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002964 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002965 p = skipwhite(*arg);
2966 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002967 *arg += STRLEN(*arg);
2968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 dict_unref(d);
2970 return generate_NEWDICT(cctx, count);
2971
2972failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002973 if (*arg == NULL)
2974 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 dict_unref(d);
2976 return FAIL;
2977}
2978
2979/*
2980 * Compile "&option".
2981 */
2982 static int
2983compile_get_option(char_u **arg, cctx_T *cctx)
2984{
2985 typval_T rettv;
2986 char_u *start = *arg;
2987 int ret;
2988
2989 // parse the option and get the current value to get the type.
2990 rettv.v_type = VAR_UNKNOWN;
2991 ret = get_option_tv(arg, &rettv, TRUE);
2992 if (ret == OK)
2993 {
2994 // include the '&' in the name, get_option_tv() expects it.
2995 char_u *name = vim_strnsave(start, *arg - start);
2996 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2997
2998 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2999 vim_free(name);
3000 }
3001 clear_tv(&rettv);
3002
3003 return ret;
3004}
3005
3006/*
3007 * Compile "$VAR".
3008 */
3009 static int
3010compile_get_env(char_u **arg, cctx_T *cctx)
3011{
3012 char_u *start = *arg;
3013 int len;
3014 int ret;
3015 char_u *name;
3016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 ++*arg;
3018 len = get_env_len(arg);
3019 if (len == 0)
3020 {
3021 semsg(_(e_syntax_at), start - 1);
3022 return FAIL;
3023 }
3024
3025 // include the '$' in the name, get_env_tv() expects it.
3026 name = vim_strnsave(start, len + 1);
3027 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3028 vim_free(name);
3029 return ret;
3030}
3031
3032/*
3033 * Compile "@r".
3034 */
3035 static int
3036compile_get_register(char_u **arg, cctx_T *cctx)
3037{
3038 int ret;
3039
3040 ++*arg;
3041 if (**arg == NUL)
3042 {
3043 semsg(_(e_syntax_at), *arg - 1);
3044 return FAIL;
3045 }
3046 if (!valid_yank_reg(**arg, TRUE))
3047 {
3048 emsg_invreg(**arg);
3049 return FAIL;
3050 }
3051 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3052 ++*arg;
3053 return ret;
3054}
3055
3056/*
3057 * Apply leading '!', '-' and '+' to constant "rettv".
3058 */
3059 static int
3060apply_leader(typval_T *rettv, char_u *start, char_u *end)
3061{
3062 char_u *p = end;
3063
3064 // this works from end to start
3065 while (p > start)
3066 {
3067 --p;
3068 if (*p == '-' || *p == '+')
3069 {
3070 // only '-' has an effect, for '+' we only check the type
3071#ifdef FEAT_FLOAT
3072 if (rettv->v_type == VAR_FLOAT)
3073 {
3074 if (*p == '-')
3075 rettv->vval.v_float = -rettv->vval.v_float;
3076 }
3077 else
3078#endif
3079 {
3080 varnumber_T val;
3081 int error = FALSE;
3082
3083 // tv_get_number_chk() accepts a string, but we don't want that
3084 // here
3085 if (check_not_string(rettv) == FAIL)
3086 return FAIL;
3087 val = tv_get_number_chk(rettv, &error);
3088 clear_tv(rettv);
3089 if (error)
3090 return FAIL;
3091 if (*p == '-')
3092 val = -val;
3093 rettv->v_type = VAR_NUMBER;
3094 rettv->vval.v_number = val;
3095 }
3096 }
3097 else
3098 {
3099 int v = tv2bool(rettv);
3100
3101 // '!' is permissive in the type.
3102 clear_tv(rettv);
3103 rettv->v_type = VAR_BOOL;
3104 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3105 }
3106 }
3107 return OK;
3108}
3109
3110/*
3111 * Recognize v: variables that are constants and set "rettv".
3112 */
3113 static void
3114get_vim_constant(char_u **arg, typval_T *rettv)
3115{
3116 if (STRNCMP(*arg, "v:true", 6) == 0)
3117 {
3118 rettv->v_type = VAR_BOOL;
3119 rettv->vval.v_number = VVAL_TRUE;
3120 *arg += 6;
3121 }
3122 else if (STRNCMP(*arg, "v:false", 7) == 0)
3123 {
3124 rettv->v_type = VAR_BOOL;
3125 rettv->vval.v_number = VVAL_FALSE;
3126 *arg += 7;
3127 }
3128 else if (STRNCMP(*arg, "v:null", 6) == 0)
3129 {
3130 rettv->v_type = VAR_SPECIAL;
3131 rettv->vval.v_number = VVAL_NULL;
3132 *arg += 6;
3133 }
3134 else if (STRNCMP(*arg, "v:none", 6) == 0)
3135 {
3136 rettv->v_type = VAR_SPECIAL;
3137 rettv->vval.v_number = VVAL_NONE;
3138 *arg += 6;
3139 }
3140}
3141
3142/*
3143 * Compile code to apply '-', '+' and '!'.
3144 */
3145 static int
3146compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3147{
3148 char_u *p = end;
3149
3150 // this works from end to start
3151 while (p > start)
3152 {
3153 --p;
3154 if (*p == '-' || *p == '+')
3155 {
3156 int negate = *p == '-';
3157 isn_T *isn;
3158
3159 // TODO: check type
3160 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3161 {
3162 --p;
3163 if (*p == '-')
3164 negate = !negate;
3165 }
3166 // only '-' has an effect, for '+' we only check the type
3167 if (negate)
3168 isn = generate_instr(cctx, ISN_NEGATENR);
3169 else
3170 isn = generate_instr(cctx, ISN_CHECKNR);
3171 if (isn == NULL)
3172 return FAIL;
3173 }
3174 else
3175 {
3176 int invert = TRUE;
3177
3178 while (p > start && p[-1] == '!')
3179 {
3180 --p;
3181 invert = !invert;
3182 }
3183 if (generate_2BOOL(cctx, invert) == FAIL)
3184 return FAIL;
3185 }
3186 }
3187 return OK;
3188}
3189
3190/*
3191 * Compile whatever comes after "name" or "name()".
3192 */
3193 static int
3194compile_subscript(
3195 char_u **arg,
3196 cctx_T *cctx,
3197 char_u **start_leader,
3198 char_u *end_leader)
3199{
3200 for (;;)
3201 {
3202 if (**arg == '(')
3203 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003204 garray_T *stack = &cctx->ctx_type_stack;
3205 type_T *type;
3206 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207
3208 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003209 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 *arg = skipwhite(*arg + 1);
3212 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3213 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003214 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 return FAIL;
3216 }
3217 else if (**arg == '-' && (*arg)[1] == '>')
3218 {
3219 char_u *p;
3220
3221 // something->method()
3222 // Apply the '!', '-' and '+' first:
3223 // -1.0->func() works like (-1.0)->func()
3224 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3225 return FAIL;
3226 *start_leader = end_leader; // don't apply again later
3227
3228 *arg = skipwhite(*arg + 2);
3229 if (**arg == '{')
3230 {
3231 // lambda call: list->{lambda}
3232 if (compile_lambda_call(arg, cctx) == FAIL)
3233 return FAIL;
3234 }
3235 else
3236 {
3237 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003238 p = *arg;
3239 if (ASCII_ISALPHA(*p) && p[1] == ':')
3240 p += 2;
3241 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 ;
3243 if (*p != '(')
3244 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003245 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 return FAIL;
3247 }
3248 // TODO: base value may not be the first argument
3249 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
3250 return FAIL;
3251 }
3252 }
3253 else if (**arg == '[')
3254 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01003255 garray_T *stack;
3256 type_T **typep;
3257
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 // list index: list[123]
3259 // TODO: more arguments
3260 // TODO: dict member dict['name']
3261 *arg = skipwhite(*arg + 1);
3262 if (compile_expr1(arg, cctx) == FAIL)
3263 return FAIL;
3264
3265 if (**arg != ']')
3266 {
3267 emsg(_(e_missbrac));
3268 return FAIL;
3269 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003270 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271
3272 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3273 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003274 stack = &cctx->ctx_type_stack;
3275 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
3276 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
3277 {
3278 emsg(_(e_listreq));
3279 return FAIL;
3280 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01003281 if ((*typep)->tt_type == VAR_LIST)
3282 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 }
3284 else if (**arg == '.' && (*arg)[1] != '.')
3285 {
3286 char_u *p;
3287
3288 ++*arg;
3289 p = *arg;
3290 // dictionary member: dict.name
3291 if (eval_isnamec1(*p))
3292 while (eval_isnamec(*p))
3293 MB_PTR_ADV(p);
3294 if (p == *arg)
3295 {
3296 semsg(_(e_syntax_at), *arg);
3297 return FAIL;
3298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
3300 return FAIL;
3301 *arg = p;
3302 }
3303 else
3304 break;
3305 }
3306
3307 // TODO - see handle_subscript():
3308 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3309 // Don't do this when "Func" is already a partial that was bound
3310 // explicitly (pt_auto is FALSE).
3311
3312 return OK;
3313}
3314
3315/*
3316 * Compile an expression at "*p" and add instructions to "instr".
3317 * "p" is advanced until after the expression, skipping white space.
3318 *
3319 * This is the equivalent of eval1(), eval2(), etc.
3320 */
3321
3322/*
3323 * number number constant
3324 * 0zFFFFFFFF Blob constant
3325 * "string" string constant
3326 * 'string' literal string constant
3327 * &option-name option value
3328 * @r register contents
3329 * identifier variable value
3330 * function() function call
3331 * $VAR environment variable
3332 * (expression) nested expression
3333 * [expr, expr] List
3334 * {key: val, key: val} Dictionary
3335 * #{key: val, key: val} Dictionary with literal keys
3336 *
3337 * Also handle:
3338 * ! in front logical NOT
3339 * - in front unary minus
3340 * + in front unary plus (ignored)
3341 * trailing (arg) funcref/partial call
3342 * trailing [] subscript in String or List
3343 * trailing .name entry in Dictionary
3344 * trailing ->name() method call
3345 */
3346 static int
3347compile_expr7(char_u **arg, cctx_T *cctx)
3348{
3349 typval_T rettv;
3350 char_u *start_leader, *end_leader;
3351 int ret = OK;
3352
3353 /*
3354 * Skip '!', '-' and '+' characters. They are handled later.
3355 */
3356 start_leader = *arg;
3357 while (**arg == '!' || **arg == '-' || **arg == '+')
3358 *arg = skipwhite(*arg + 1);
3359 end_leader = *arg;
3360
3361 rettv.v_type = VAR_UNKNOWN;
3362 switch (**arg)
3363 {
3364 /*
3365 * Number constant.
3366 */
3367 case '0': // also for blob starting with 0z
3368 case '1':
3369 case '2':
3370 case '3':
3371 case '4':
3372 case '5':
3373 case '6':
3374 case '7':
3375 case '8':
3376 case '9':
3377 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
3378 return FAIL;
3379 break;
3380
3381 /*
3382 * String constant: "string".
3383 */
3384 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
3385 return FAIL;
3386 break;
3387
3388 /*
3389 * Literal string constant: 'str''ing'.
3390 */
3391 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
3392 return FAIL;
3393 break;
3394
3395 /*
3396 * Constant Vim variable.
3397 */
3398 case 'v': get_vim_constant(arg, &rettv);
3399 ret = NOTDONE;
3400 break;
3401
3402 /*
3403 * List: [expr, expr]
3404 */
3405 case '[': ret = compile_list(arg, cctx);
3406 break;
3407
3408 /*
3409 * Dictionary: #{key: val, key: val}
3410 */
3411 case '#': if ((*arg)[1] == '{')
3412 {
3413 ++*arg;
3414 ret = compile_dict(arg, cctx, TRUE);
3415 }
3416 else
3417 ret = NOTDONE;
3418 break;
3419
3420 /*
3421 * Lambda: {arg, arg -> expr}
3422 * Dictionary: {'key': val, 'key': val}
3423 */
3424 case '{': {
3425 char_u *start = skipwhite(*arg + 1);
3426
3427 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003428 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003430 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 if (ret != FAIL && *start == '>')
3432 ret = compile_lambda(arg, cctx);
3433 else
3434 ret = compile_dict(arg, cctx, FALSE);
3435 }
3436 break;
3437
3438 /*
3439 * Option value: &name
3440 */
3441 case '&': ret = compile_get_option(arg, cctx);
3442 break;
3443
3444 /*
3445 * Environment variable: $VAR.
3446 */
3447 case '$': ret = compile_get_env(arg, cctx);
3448 break;
3449
3450 /*
3451 * Register contents: @r.
3452 */
3453 case '@': ret = compile_get_register(arg, cctx);
3454 break;
3455 /*
3456 * nested expression: (expression).
3457 */
3458 case '(': *arg = skipwhite(*arg + 1);
3459 ret = compile_expr1(arg, cctx); // recursive!
3460 *arg = skipwhite(*arg);
3461 if (**arg == ')')
3462 ++*arg;
3463 else if (ret == OK)
3464 {
3465 emsg(_(e_missing_close));
3466 ret = FAIL;
3467 }
3468 break;
3469
3470 default: ret = NOTDONE;
3471 break;
3472 }
3473 if (ret == FAIL)
3474 return FAIL;
3475
3476 if (rettv.v_type != VAR_UNKNOWN)
3477 {
3478 // apply the '!', '-' and '+' before the constant
3479 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
3480 {
3481 clear_tv(&rettv);
3482 return FAIL;
3483 }
3484 start_leader = end_leader; // don't apply again below
3485
3486 // push constant
3487 switch (rettv.v_type)
3488 {
3489 case VAR_BOOL:
3490 generate_PUSHBOOL(cctx, rettv.vval.v_number);
3491 break;
3492 case VAR_SPECIAL:
3493 generate_PUSHSPEC(cctx, rettv.vval.v_number);
3494 break;
3495 case VAR_NUMBER:
3496 generate_PUSHNR(cctx, rettv.vval.v_number);
3497 break;
3498#ifdef FEAT_FLOAT
3499 case VAR_FLOAT:
3500 generate_PUSHF(cctx, rettv.vval.v_float);
3501 break;
3502#endif
3503 case VAR_BLOB:
3504 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
3505 rettv.vval.v_blob = NULL;
3506 break;
3507 case VAR_STRING:
3508 generate_PUSHS(cctx, rettv.vval.v_string);
3509 rettv.vval.v_string = NULL;
3510 break;
3511 default:
3512 iemsg("constant type missing");
3513 return FAIL;
3514 }
3515 }
3516 else if (ret == NOTDONE)
3517 {
3518 char_u *p;
3519 int r;
3520
3521 if (!eval_isnamec1(**arg))
3522 {
3523 semsg(_("E1015: Name expected: %s"), *arg);
3524 return FAIL;
3525 }
3526
3527 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003528 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 if (*p == '(')
3530 r = compile_call(arg, p - *arg, cctx, 0);
3531 else
3532 r = compile_load(arg, p, cctx, TRUE);
3533 if (r == FAIL)
3534 return FAIL;
3535 }
3536
3537 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
3538 return FAIL;
3539
3540 // Now deal with prefixed '-', '+' and '!', if not done already.
3541 return compile_leader(cctx, start_leader, end_leader);
3542}
3543
3544/*
3545 * * number multiplication
3546 * / number division
3547 * % number modulo
3548 */
3549 static int
3550compile_expr6(char_u **arg, cctx_T *cctx)
3551{
3552 char_u *op;
3553
3554 // get the first variable
3555 if (compile_expr7(arg, cctx) == FAIL)
3556 return FAIL;
3557
3558 /*
3559 * Repeat computing, until no "*", "/" or "%" is following.
3560 */
3561 for (;;)
3562 {
3563 op = skipwhite(*arg);
3564 if (*op != '*' && *op != '/' && *op != '%')
3565 break;
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003566 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 {
3568 char_u buf[3];
3569
3570 vim_strncpy(buf, op, 1);
3571 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003572 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 }
3574 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003575 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003576 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577
3578 // get the second variable
3579 if (compile_expr7(arg, cctx) == FAIL)
3580 return FAIL;
3581
3582 generate_two_op(cctx, op);
3583 }
3584
3585 return OK;
3586}
3587
3588/*
3589 * + number addition
3590 * - number subtraction
3591 * .. string concatenation
3592 */
3593 static int
3594compile_expr5(char_u **arg, cctx_T *cctx)
3595{
3596 char_u *op;
3597 int oplen;
3598
3599 // get the first variable
3600 if (compile_expr6(arg, cctx) == FAIL)
3601 return FAIL;
3602
3603 /*
3604 * Repeat computing, until no "+", "-" or ".." is following.
3605 */
3606 for (;;)
3607 {
3608 op = skipwhite(*arg);
3609 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3610 break;
3611 oplen = (*op == '.' ? 2 : 1);
3612
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003613 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 {
3615 char_u buf[3];
3616
3617 vim_strncpy(buf, op, oplen);
3618 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003619 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 }
3621
3622 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003623 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003624 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625
3626 // get the second variable
3627 if (compile_expr6(arg, cctx) == FAIL)
3628 return FAIL;
3629
3630 if (*op == '.')
3631 {
3632 if (may_generate_2STRING(-2, cctx) == FAIL
3633 || may_generate_2STRING(-1, cctx) == FAIL)
3634 return FAIL;
3635 generate_instr_drop(cctx, ISN_CONCAT, 1);
3636 }
3637 else
3638 generate_two_op(cctx, op);
3639 }
3640
3641 return OK;
3642}
3643
Bram Moolenaar080457c2020-03-03 21:53:32 +01003644 static exptype_T
3645get_compare_type(char_u *p, int *len, int *type_is)
3646{
3647 exptype_T type = EXPR_UNKNOWN;
3648 int i;
3649
3650 switch (p[0])
3651 {
3652 case '=': if (p[1] == '=')
3653 type = EXPR_EQUAL;
3654 else if (p[1] == '~')
3655 type = EXPR_MATCH;
3656 break;
3657 case '!': if (p[1] == '=')
3658 type = EXPR_NEQUAL;
3659 else if (p[1] == '~')
3660 type = EXPR_NOMATCH;
3661 break;
3662 case '>': if (p[1] != '=')
3663 {
3664 type = EXPR_GREATER;
3665 *len = 1;
3666 }
3667 else
3668 type = EXPR_GEQUAL;
3669 break;
3670 case '<': if (p[1] != '=')
3671 {
3672 type = EXPR_SMALLER;
3673 *len = 1;
3674 }
3675 else
3676 type = EXPR_SEQUAL;
3677 break;
3678 case 'i': if (p[1] == 's')
3679 {
3680 // "is" and "isnot"; but not a prefix of a name
3681 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3682 *len = 5;
3683 i = p[*len];
3684 if (!isalnum(i) && i != '_')
3685 {
3686 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3687 *type_is = TRUE;
3688 }
3689 }
3690 break;
3691 }
3692 return type;
3693}
3694
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695/*
3696 * expr5a == expr5b
3697 * expr5a =~ expr5b
3698 * expr5a != expr5b
3699 * expr5a !~ expr5b
3700 * expr5a > expr5b
3701 * expr5a >= expr5b
3702 * expr5a < expr5b
3703 * expr5a <= expr5b
3704 * expr5a is expr5b
3705 * expr5a isnot expr5b
3706 *
3707 * Produces instructions:
3708 * EVAL expr5a Push result of "expr5a"
3709 * EVAL expr5b Push result of "expr5b"
3710 * COMPARE one of the compare instructions
3711 */
3712 static int
3713compile_expr4(char_u **arg, cctx_T *cctx)
3714{
3715 exptype_T type = EXPR_UNKNOWN;
3716 char_u *p;
3717 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 int type_is = FALSE;
3719
3720 // get the first variable
3721 if (compile_expr5(arg, cctx) == FAIL)
3722 return FAIL;
3723
3724 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003725 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726
3727 /*
3728 * If there is a comparative operator, use it.
3729 */
3730 if (type != EXPR_UNKNOWN)
3731 {
3732 int ic = FALSE; // Default: do not ignore case
3733
3734 if (type_is && (p[len] == '?' || p[len] == '#'))
3735 {
3736 semsg(_(e_invexpr2), *arg);
3737 return FAIL;
3738 }
3739 // extra question mark appended: ignore case
3740 if (p[len] == '?')
3741 {
3742 ic = TRUE;
3743 ++len;
3744 }
3745 // extra '#' appended: match case (ignored)
3746 else if (p[len] == '#')
3747 ++len;
3748 // nothing appended: match case
3749
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003750 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 {
3752 char_u buf[7];
3753
3754 vim_strncpy(buf, p, len);
3755 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003756 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 }
3758
3759 // get the second variable
3760 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003761 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003762 return FAIL;
3763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 if (compile_expr5(arg, cctx) == FAIL)
3765 return FAIL;
3766
3767 generate_COMPARE(cctx, type, ic);
3768 }
3769
3770 return OK;
3771}
3772
3773/*
3774 * Compile || or &&.
3775 */
3776 static int
3777compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3778{
3779 char_u *p = skipwhite(*arg);
3780 int opchar = *op;
3781
3782 if (p[0] == opchar && p[1] == opchar)
3783 {
3784 garray_T *instr = &cctx->ctx_instr;
3785 garray_T end_ga;
3786
3787 /*
3788 * Repeat until there is no following "||" or "&&"
3789 */
3790 ga_init2(&end_ga, sizeof(int), 10);
3791 while (p[0] == opchar && p[1] == opchar)
3792 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003793 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3794 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003796 return FAIL;
3797 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798
3799 if (ga_grow(&end_ga, 1) == FAIL)
3800 {
3801 ga_clear(&end_ga);
3802 return FAIL;
3803 }
3804 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3805 ++end_ga.ga_len;
3806 generate_JUMP(cctx, opchar == '|'
3807 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3808
3809 // eval the next expression
3810 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003811 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003812 return FAIL;
3813
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 if ((opchar == '|' ? compile_expr3(arg, cctx)
3815 : compile_expr4(arg, cctx)) == FAIL)
3816 {
3817 ga_clear(&end_ga);
3818 return FAIL;
3819 }
3820 p = skipwhite(*arg);
3821 }
3822
3823 // Fill in the end label in all jumps.
3824 while (end_ga.ga_len > 0)
3825 {
3826 isn_T *isn;
3827
3828 --end_ga.ga_len;
3829 isn = ((isn_T *)instr->ga_data)
3830 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3831 isn->isn_arg.jump.jump_where = instr->ga_len;
3832 }
3833 ga_clear(&end_ga);
3834 }
3835
3836 return OK;
3837}
3838
3839/*
3840 * expr4a && expr4a && expr4a logical AND
3841 *
3842 * Produces instructions:
3843 * EVAL expr4a Push result of "expr4a"
3844 * JUMP_AND_KEEP_IF_FALSE end
3845 * EVAL expr4b Push result of "expr4b"
3846 * JUMP_AND_KEEP_IF_FALSE end
3847 * EVAL expr4c Push result of "expr4c"
3848 * end:
3849 */
3850 static int
3851compile_expr3(char_u **arg, cctx_T *cctx)
3852{
3853 // get the first variable
3854 if (compile_expr4(arg, cctx) == FAIL)
3855 return FAIL;
3856
3857 // || and && work almost the same
3858 return compile_and_or(arg, cctx, "&&");
3859}
3860
3861/*
3862 * expr3a || expr3b || expr3c logical OR
3863 *
3864 * Produces instructions:
3865 * EVAL expr3a Push result of "expr3a"
3866 * JUMP_AND_KEEP_IF_TRUE end
3867 * EVAL expr3b Push result of "expr3b"
3868 * JUMP_AND_KEEP_IF_TRUE end
3869 * EVAL expr3c Push result of "expr3c"
3870 * end:
3871 */
3872 static int
3873compile_expr2(char_u **arg, cctx_T *cctx)
3874{
3875 // eval the first expression
3876 if (compile_expr3(arg, cctx) == FAIL)
3877 return FAIL;
3878
3879 // || and && work almost the same
3880 return compile_and_or(arg, cctx, "||");
3881}
3882
3883/*
3884 * Toplevel expression: expr2 ? expr1a : expr1b
3885 *
3886 * Produces instructions:
3887 * EVAL expr2 Push result of "expr"
3888 * JUMP_IF_FALSE alt jump if false
3889 * EVAL expr1a
3890 * JUMP_ALWAYS end
3891 * alt: EVAL expr1b
3892 * end:
3893 */
3894 static int
3895compile_expr1(char_u **arg, cctx_T *cctx)
3896{
3897 char_u *p;
3898
3899 // evaluate the first expression
3900 if (compile_expr2(arg, cctx) == FAIL)
3901 return FAIL;
3902
3903 p = skipwhite(*arg);
3904 if (*p == '?')
3905 {
3906 garray_T *instr = &cctx->ctx_instr;
3907 garray_T *stack = &cctx->ctx_type_stack;
3908 int alt_idx = instr->ga_len;
3909 int end_idx;
3910 isn_T *isn;
3911 type_T *type1;
3912 type_T *type2;
3913
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003914 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3915 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003917 return FAIL;
3918 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919
3920 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3921
3922 // evaluate the second expression; any type is accepted
3923 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003924 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003925 return FAIL;
3926
Bram Moolenaara6d53682020-01-28 23:04:06 +01003927 if (compile_expr1(arg, cctx) == FAIL)
3928 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929
3930 // remember the type and drop it
3931 --stack->ga_len;
3932 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3933
3934 end_idx = instr->ga_len;
3935 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3936
3937 // jump here from JUMP_IF_FALSE
3938 isn = ((isn_T *)instr->ga_data) + alt_idx;
3939 isn->isn_arg.jump.jump_where = instr->ga_len;
3940
3941 // Check for the ":".
3942 p = skipwhite(*arg);
3943 if (*p != ':')
3944 {
3945 emsg(_(e_missing_colon));
3946 return FAIL;
3947 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003948 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3949 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003951 return FAIL;
3952 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953
3954 // evaluate the third expression
3955 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003956 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003957 return FAIL;
3958
Bram Moolenaara6d53682020-01-28 23:04:06 +01003959 if (compile_expr1(arg, cctx) == FAIL)
3960 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961
3962 // If the types differ, the result has a more generic type.
3963 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003964 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965
3966 // jump here from JUMP_ALWAYS
3967 isn = ((isn_T *)instr->ga_data) + end_idx;
3968 isn->isn_arg.jump.jump_where = instr->ga_len;
3969 }
3970 return OK;
3971}
3972
3973/*
3974 * compile "return [expr]"
3975 */
3976 static char_u *
3977compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
3978{
3979 char_u *p = arg;
3980 garray_T *stack = &cctx->ctx_type_stack;
3981 type_T *stack_type;
3982
3983 if (*p != NUL && *p != '|' && *p != '\n')
3984 {
3985 // compile return argument into instructions
3986 if (compile_expr1(&p, cctx) == FAIL)
3987 return NULL;
3988
3989 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3990 if (set_return_type)
3991 cctx->ctx_ufunc->uf_ret_type = stack_type;
3992 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
3993 == FAIL)
3994 return NULL;
3995 }
3996 else
3997 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02003998 // "set_return_type" cannot be TRUE, only used for a lambda which
3999 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004000 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4001 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 {
4003 emsg(_("E1003: Missing return value"));
4004 return NULL;
4005 }
4006
4007 // No argument, return zero.
4008 generate_PUSHNR(cctx, 0);
4009 }
4010
4011 if (generate_instr(cctx, ISN_RETURN) == NULL)
4012 return NULL;
4013
4014 // "return val | endif" is possible
4015 return skipwhite(p);
4016}
4017
4018/*
4019 * Return the length of an assignment operator, or zero if there isn't one.
4020 */
4021 int
4022assignment_len(char_u *p, int *heredoc)
4023{
4024 if (*p == '=')
4025 {
4026 if (p[1] == '<' && p[2] == '<')
4027 {
4028 *heredoc = TRUE;
4029 return 3;
4030 }
4031 return 1;
4032 }
4033 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4034 return 2;
4035 if (STRNCMP(p, "..=", 3) == 0)
4036 return 3;
4037 return 0;
4038}
4039
4040// words that cannot be used as a variable
4041static char *reserved[] = {
4042 "true",
4043 "false",
4044 NULL
4045};
4046
4047/*
4048 * Get a line for "=<<".
4049 * Return a pointer to the line in allocated memory.
4050 * Return NULL for end-of-file or some error.
4051 */
4052 static char_u *
4053heredoc_getline(
4054 int c UNUSED,
4055 void *cookie,
4056 int indent UNUSED,
4057 int do_concat UNUSED)
4058{
4059 cctx_T *cctx = (cctx_T *)cookie;
4060
4061 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004062 {
4063 iemsg("Heredoc got to end");
4064 return NULL;
4065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066 ++cctx->ctx_lnum;
4067 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4068 [cctx->ctx_lnum]);
4069}
4070
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004071typedef enum {
4072 dest_local,
4073 dest_option,
4074 dest_env,
4075 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004076 dest_buffer,
4077 dest_window,
4078 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004079 dest_vimvar,
4080 dest_script,
4081 dest_reg,
4082} assign_dest_T;
4083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084/*
4085 * compile "let var [= expr]", "const var = expr" and "var = expr"
4086 * "arg" points to "var".
4087 */
4088 static char_u *
4089compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4090{
4091 char_u *p;
4092 char_u *ret = NULL;
4093 int var_count = 0;
4094 int semicolon = 0;
4095 size_t varlen;
4096 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004097 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004100 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004102 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 int oplen = 0;
4104 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004105 type_T *type = &t_any;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004106 lvar_T *lvar = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 char_u *name;
4108 char_u *sp;
4109 int has_type = FALSE;
4110 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
4111 int instr_count = -1;
4112
4113 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
4114 if (p == NULL)
4115 return NULL;
4116 if (var_count > 0)
4117 {
4118 // TODO: let [var, var] = list
4119 emsg("Cannot handle a list yet");
4120 return NULL;
4121 }
4122
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004123 // "a: type" is declaring variable "a" with a type, not "a:".
4124 if (is_decl && p == arg + 2 && p[-1] == ':')
4125 --p;
4126
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 varlen = p - arg;
4128 name = vim_strnsave(arg, (int)varlen);
4129 if (name == NULL)
4130 return NULL;
4131
Bram Moolenaar080457c2020-03-03 21:53:32 +01004132 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004134 if (*arg == '&')
4135 {
4136 int cc;
4137 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138
Bram Moolenaar080457c2020-03-03 21:53:32 +01004139 dest = dest_option;
4140 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004142 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004143 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 if (is_decl)
4146 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004147 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 goto theend;
4149 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004150 p = arg;
4151 p = find_option_end(&p, &opt_flags);
4152 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004154 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01004155 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004156 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004157 }
4158 cc = *p;
4159 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004160 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004161 *p = cc;
4162 if (opt_type == -3)
4163 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004164 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004165 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004166 }
4167 if (opt_type == -2 || opt_type == 0)
4168 type = &t_string;
4169 else
4170 type = &t_number; // both number and boolean option
4171 }
4172 else if (*arg == '$')
4173 {
4174 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004175 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004176 if (is_decl)
4177 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004178 semsg(_("E1065: Cannot declare an environment variable: %s"),
4179 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004180 goto theend;
4181 }
4182 }
4183 else if (*arg == '@')
4184 {
4185 if (!valid_yank_reg(arg[1], TRUE))
4186 {
4187 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004188 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004189 }
4190 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004191 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004192 if (is_decl)
4193 {
4194 semsg(_("E1066: Cannot declare a register: %s"), name);
4195 goto theend;
4196 }
4197 }
4198 else if (STRNCMP(arg, "g:", 2) == 0)
4199 {
4200 dest = dest_global;
4201 if (is_decl)
4202 {
4203 semsg(_("E1016: Cannot declare a global variable: %s"), name);
4204 goto theend;
4205 }
4206 }
Bram Moolenaard3aac292020-04-19 14:32:17 +02004207 else if (STRNCMP(arg, "b:", 2) == 0)
4208 {
4209 dest = dest_buffer;
4210 if (is_decl)
4211 {
4212 semsg(_("E1078: Cannot declare a buffer variable: %s"), name);
4213 goto theend;
4214 }
4215 }
4216 else if (STRNCMP(arg, "w:", 2) == 0)
4217 {
4218 dest = dest_window;
4219 if (is_decl)
4220 {
4221 semsg(_("E1079: Cannot declare a window variable: %s"), name);
4222 goto theend;
4223 }
4224 }
4225 else if (STRNCMP(arg, "t:", 2) == 0)
4226 {
4227 dest = dest_tab;
4228 if (is_decl)
4229 {
4230 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
4231 goto theend;
4232 }
4233 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004234 else if (STRNCMP(arg, "v:", 2) == 0)
4235 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004236 typval_T *vtv;
4237 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004238
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004239 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004240 if (vimvaridx < 0)
4241 {
4242 semsg(_(e_var_notfound), arg);
4243 goto theend;
4244 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004245 // We use the current value of "sandbox" here, is that OK?
4246 if (var_check_ro(di_flags, name, FALSE))
4247 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004248 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004249 vtv = get_vim_var_tv(vimvaridx);
4250 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004251 if (is_decl)
4252 {
4253 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
4254 goto theend;
4255 }
4256 }
4257 else
4258 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004259 int idx;
4260
Bram Moolenaar080457c2020-03-03 21:53:32 +01004261 for (idx = 0; reserved[idx] != NULL; ++idx)
4262 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004264 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 goto theend;
4266 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004267
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004268 lvar = lookup_local(arg, varlen, cctx);
4269 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004271 if (is_decl)
4272 {
4273 semsg(_("E1017: Variable already declared: %s"), name);
4274 goto theend;
4275 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004276 else if (lvar->lv_const)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004277 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004278 semsg(_("E1018: Cannot assign to a constant: %s"), name);
4279 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004280 }
4281 }
4282 else if (STRNCMP(arg, "s:", 2) == 0
4283 || lookup_script(arg, varlen) == OK
4284 || find_imported(arg, varlen, cctx) != NULL)
4285 {
4286 dest = dest_script;
4287 if (is_decl)
4288 {
4289 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004290 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004291 goto theend;
4292 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004294 else if (name[1] == ':' && name[2] != NUL)
4295 {
4296 semsg(_("E1082: Cannot use a namespaced variable: %s"), name);
4297 goto theend;
4298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 }
4300 }
4301
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004302 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 {
4304 if (is_decl && *p == ':')
4305 {
4306 // parse optional type: "let var: type = expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004307 if (!VIM_ISWHITE(p[1]))
4308 {
4309 semsg(_(e_white_after), ":");
4310 goto theend;
4311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 p = skipwhite(p + 1);
4313 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 has_type = TRUE;
4315 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004316 else if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 type = lvar->lv_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 }
4319
4320 sp = p;
4321 p = skipwhite(p);
4322 op = p;
4323 oplen = assignment_len(p, &heredoc);
4324 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4325 {
4326 char_u buf[4];
4327
4328 vim_strncpy(buf, op, oplen);
4329 semsg(_(e_white_both), buf);
4330 }
4331
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004332 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02004333 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004335 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 goto theend;
4337 }
4338
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004339 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 {
4341 if (oplen > 1 && !heredoc)
4342 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004343 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4345 name);
4346 goto theend;
4347 }
4348
4349 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02004350 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004351 goto theend;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004352 lvar = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4353 if (lvar == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004355 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 }
4357
4358 if (heredoc)
4359 {
4360 list_T *l;
4361 listitem_T *li;
4362
4363 // [let] varname =<< [trim] {end}
4364 eap->getline = heredoc_getline;
4365 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004366 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367
4368 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004369 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 {
4371 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4372 li->li_tv.vval.v_string = NULL;
4373 }
4374 generate_NEWLIST(cctx, l->lv_len);
4375 type = &t_list_string;
4376 list_free(l);
4377 p += STRLEN(p);
4378 }
4379 else if (oplen > 0)
4380 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004381 int r;
4382 type_T *stacktype;
4383 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004384
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 // for "+=", "*=", "..=" etc. first load the current value
4386 if (*op != '=')
4387 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004388 switch (dest)
4389 {
4390 case dest_option:
4391 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004392 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004393 break;
4394 case dest_global:
4395 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4396 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004397 case dest_buffer:
4398 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4399 break;
4400 case dest_window:
4401 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4402 break;
4403 case dest_tab:
4404 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4405 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004406 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01004407 compile_load_scriptvar(cctx,
4408 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004409 break;
4410 case dest_env:
4411 // Include $ in the name here
4412 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4413 break;
4414 case dest_reg:
4415 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
4416 break;
4417 case dest_vimvar:
4418 generate_LOADV(cctx, name + 2, TRUE);
4419 break;
4420 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004421 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004422 break;
4423 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 }
4425
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004426 // Compile the expression. Temporarily hide the new local variable
4427 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02004428 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004429 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430 instr_count = instr->ga_len;
4431 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004432 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02004433 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004434 ++cctx->ctx_locals.ga_len;
4435 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436 goto theend;
4437
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004438 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004440 stack = &cctx->ctx_type_stack;
4441 stacktype = stack->ga_len == 0 ? &t_void
4442 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004443 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004445 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004447 if (stacktype->tt_type == VAR_VOID)
4448 {
4449 emsg(_("E1031: Cannot use void value"));
4450 goto theend;
4451 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004452 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004453 {
4454 // An empty list or dict has a &t_void member, for a
4455 // variable that implies &t_any.
4456 if (stacktype == &t_list_empty)
4457 lvar->lv_type = &t_list_any;
4458 else if (stacktype == &t_dict_empty)
4459 lvar->lv_type = &t_dict_any;
4460 else
4461 lvar->lv_type = stacktype;
4462 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004463 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004464 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
4465 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004467 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004468 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469 }
4470 }
4471 else if (cmdidx == CMD_const)
4472 {
4473 emsg(_("E1021: const requires a value"));
4474 goto theend;
4475 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004476 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477 {
4478 emsg(_("E1022: type or initialization required"));
4479 goto theend;
4480 }
4481 else
4482 {
4483 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 if (ga_grow(instr, 1) == FAIL)
4485 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004486 switch (type->tt_type)
4487 {
4488 case VAR_BOOL:
4489 generate_PUSHBOOL(cctx, VVAL_FALSE);
4490 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004491 case VAR_FLOAT:
4492#ifdef FEAT_FLOAT
4493 generate_PUSHF(cctx, 0.0);
4494#endif
4495 break;
4496 case VAR_STRING:
4497 generate_PUSHS(cctx, NULL);
4498 break;
4499 case VAR_BLOB:
4500 generate_PUSHBLOB(cctx, NULL);
4501 break;
4502 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004503 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004504 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004505 case VAR_LIST:
4506 generate_NEWLIST(cctx, 0);
4507 break;
4508 case VAR_DICT:
4509 generate_NEWDICT(cctx, 0);
4510 break;
4511 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004512 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004513 break;
4514 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004515 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004516 break;
4517 case VAR_NUMBER:
4518 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004519 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02004520 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01004521 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02004522 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01004523 generate_PUSHNR(cctx, 0);
4524 break;
4525 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004526 }
4527
4528 if (oplen > 0 && *op != '=')
4529 {
4530 type_T *expected = &t_number;
4531 garray_T *stack = &cctx->ctx_type_stack;
4532 type_T *stacktype;
4533
4534 // TODO: if type is known use float or any operation
4535
4536 if (*op == '.')
4537 expected = &t_string;
4538 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4539 if (need_type(stacktype, expected, -1, cctx) == FAIL)
4540 goto theend;
4541
4542 if (*op == '.')
4543 generate_instr_drop(cctx, ISN_CONCAT, 1);
4544 else
4545 {
4546 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4547
4548 if (isn == NULL)
4549 goto theend;
4550 switch (*op)
4551 {
4552 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
4553 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4554 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4555 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4556 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4557 }
4558 }
4559 }
4560
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004561 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004563 case dest_option:
4564 generate_STOREOPT(cctx, name + 1, opt_flags);
4565 break;
4566 case dest_global:
4567 // include g: with the name, easier to execute that way
4568 generate_STORE(cctx, ISN_STOREG, 0, name);
4569 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004570 case dest_buffer:
4571 // include b: with the name, easier to execute that way
4572 generate_STORE(cctx, ISN_STOREB, 0, name);
4573 break;
4574 case dest_window:
4575 // include w: with the name, easier to execute that way
4576 generate_STORE(cctx, ISN_STOREW, 0, name);
4577 break;
4578 case dest_tab:
4579 // include t: with the name, easier to execute that way
4580 generate_STORE(cctx, ISN_STORET, 0, name);
4581 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004582 case dest_env:
4583 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
4584 break;
4585 case dest_reg:
4586 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
4587 break;
4588 case dest_vimvar:
4589 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
4590 break;
4591 case dest_script:
4592 {
4593 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4594 imported_T *import = NULL;
4595 int sid = current_sctx.sc_sid;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004596 int idx;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004597
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004598 if (name[1] != ':')
4599 {
4600 import = find_imported(name, 0, cctx);
4601 if (import != NULL)
4602 sid = import->imp_sid;
4603 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004605 idx = get_script_item_idx(sid, rawname, TRUE);
4606 // TODO: specific type
4607 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004608 {
4609 char_u *name_s = name;
4610
4611 // Include s: in the name for store_var()
4612 if (name[1] != ':')
4613 {
4614 int len = (int)STRLEN(name) + 3;
4615
4616 name_s = alloc(len);
4617 if (name_s == NULL)
4618 name_s = name;
4619 else
4620 vim_snprintf((char *)name_s, len, "s:%s", name);
4621 }
4622 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
4623 if (name_s != name)
4624 vim_free(name_s);
4625 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004626 else
4627 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
4628 sid, idx, &t_any);
4629 }
4630 break;
4631 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004632 if (lvar != NULL)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004633 {
4634 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004636 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
4637 // into ISN_STORENR
4638 if (instr->ga_len == instr_count + 1
4639 && isn->isn_type == ISN_PUSHNR)
4640 {
4641 varnumber_T val = isn->isn_arg.number;
4642 garray_T *stack = &cctx->ctx_type_stack;
4643
4644 isn->isn_type = ISN_STORENR;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004645 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004646 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004647 if (stack->ga_len > 0)
4648 --stack->ga_len;
4649 }
4650 else
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004651 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004652 }
4653 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 }
4655 ret = p;
4656
4657theend:
4658 vim_free(name);
4659 return ret;
4660}
4661
4662/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004663 * Check if "name" can be "unlet".
4664 */
4665 int
4666check_vim9_unlet(char_u *name)
4667{
4668 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
4669 {
4670 semsg(_("E1081: Cannot unlet %s"), name);
4671 return FAIL;
4672 }
4673 return OK;
4674}
4675
4676/*
4677 * Callback passed to ex_unletlock().
4678 */
4679 static int
4680compile_unlet(
4681 lval_T *lvp,
4682 char_u *name_end,
4683 exarg_T *eap,
4684 int deep UNUSED,
4685 void *coookie)
4686{
4687 cctx_T *cctx = coookie;
4688
4689 if (lvp->ll_tv == NULL)
4690 {
4691 char_u *p = lvp->ll_name;
4692 int cc = *name_end;
4693 int ret = OK;
4694
4695 // Normal name. Only supports g:, w:, t: and b: namespaces.
4696 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004697 if (*p == '$')
4698 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
4699 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004700 ret = FAIL;
4701 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004702 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004703
4704 *name_end = cc;
4705 return ret;
4706 }
4707
4708 // TODO: unlet {list}[idx]
4709 // TODO: unlet {dict}[key]
4710 emsg("Sorry, :unlet not fully implemented yet");
4711 return FAIL;
4712}
4713
4714/*
4715 * compile "unlet var", "lock var" and "unlock var"
4716 * "arg" points to "var".
4717 */
4718 static char_u *
4719compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
4720{
4721 char_u *p = arg;
4722
4723 if (eap->cmdidx != CMD_unlet)
4724 {
4725 emsg("Sorry, :lock and unlock not implemented yet");
4726 return NULL;
4727 }
4728
4729 if (*p == '!')
4730 {
4731 p = skipwhite(p + 1);
4732 eap->forceit = TRUE;
4733 }
4734
4735 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
4736 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4737}
4738
4739/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 * Compile an :import command.
4741 */
4742 static char_u *
4743compile_import(char_u *arg, cctx_T *cctx)
4744{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01004745 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746}
4747
4748/*
4749 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
4750 */
4751 static int
4752compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
4753{
4754 garray_T *instr = &cctx->ctx_instr;
4755 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
4756
4757 if (endlabel == NULL)
4758 return FAIL;
4759 endlabel->el_next = *el;
4760 *el = endlabel;
4761 endlabel->el_end_label = instr->ga_len;
4762
4763 generate_JUMP(cctx, when, 0);
4764 return OK;
4765}
4766
4767 static void
4768compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
4769{
4770 garray_T *instr = &cctx->ctx_instr;
4771
4772 while (*el != NULL)
4773 {
4774 endlabel_T *cur = (*el);
4775 isn_T *isn;
4776
4777 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
4778 isn->isn_arg.jump.jump_where = instr->ga_len;
4779 *el = cur->el_next;
4780 vim_free(cur);
4781 }
4782}
4783
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004784 static void
4785compile_free_jump_to_end(endlabel_T **el)
4786{
4787 while (*el != NULL)
4788 {
4789 endlabel_T *cur = (*el);
4790
4791 *el = cur->el_next;
4792 vim_free(cur);
4793 }
4794}
4795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796/*
4797 * Create a new scope and set up the generic items.
4798 */
4799 static scope_T *
4800new_scope(cctx_T *cctx, scopetype_T type)
4801{
4802 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
4803
4804 if (scope == NULL)
4805 return NULL;
4806 scope->se_outer = cctx->ctx_scope;
4807 cctx->ctx_scope = scope;
4808 scope->se_type = type;
4809 scope->se_local_count = cctx->ctx_locals.ga_len;
4810 return scope;
4811}
4812
4813/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004814 * Free the current scope and go back to the outer scope.
4815 */
4816 static void
4817drop_scope(cctx_T *cctx)
4818{
4819 scope_T *scope = cctx->ctx_scope;
4820
4821 if (scope == NULL)
4822 {
4823 iemsg("calling drop_scope() without a scope");
4824 return;
4825 }
4826 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004827 switch (scope->se_type)
4828 {
4829 case IF_SCOPE:
4830 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
4831 case FOR_SCOPE:
4832 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
4833 case WHILE_SCOPE:
4834 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
4835 case TRY_SCOPE:
4836 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
4837 case NO_SCOPE:
4838 case BLOCK_SCOPE:
4839 break;
4840 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004841 vim_free(scope);
4842}
4843
4844/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004845 * Evaluate an expression that is a constant:
4846 * has(arg)
4847 *
4848 * Also handle:
4849 * ! in front logical NOT
4850 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004851 * Return FAIL if the expression is not a constant.
4852 */
4853 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004854evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004855{
4856 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004857 char_u *start_leader, *end_leader;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004858 int has_call = FALSE;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004859
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004860 /*
4861 * Skip '!' characters. They are handled later.
4862 */
4863 start_leader = *arg;
4864 while (**arg == '!')
4865 *arg = skipwhite(*arg + 1);
4866 end_leader = *arg;
4867
4868 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004869 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004870 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004871 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4872 {
4873 tv->v_type = VAR_SPECIAL;
4874 tv->vval.v_number = VVAL_TRUE;
4875 *arg += 4;
4876 return OK;
4877 }
4878 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4879 {
4880 tv->v_type = VAR_SPECIAL;
4881 tv->vval.v_number = VVAL_FALSE;
4882 *arg += 5;
4883 return OK;
4884 }
4885
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004886 if (STRNCMP("has(", *arg, 4) == 0)
4887 {
4888 has_call = TRUE;
4889 *arg = skipwhite(*arg + 4);
4890 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004891
4892 if (**arg == '"')
4893 {
4894 if (get_string_tv(arg, tv, TRUE) == FAIL)
4895 return FAIL;
4896 }
4897 else if (**arg == '\'')
4898 {
4899 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4900 return FAIL;
4901 }
4902 else
4903 return FAIL;
4904
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004905 if (has_call)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004906 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004907 *arg = skipwhite(*arg);
4908 if (**arg != ')')
4909 return FAIL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004910 *arg = *arg + 1;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004911
4912 argvars[0] = *tv;
4913 argvars[1].v_type = VAR_UNKNOWN;
4914 tv->v_type = VAR_NUMBER;
4915 tv->vval.v_number = 0;
4916 f_has(argvars, tv);
4917 clear_tv(&argvars[0]);
4918
4919 while (start_leader < end_leader)
4920 {
4921 if (*start_leader == '!')
4922 tv->vval.v_number = !tv->vval.v_number;
4923 ++start_leader;
4924 }
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004925 }
4926
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004927 return OK;
4928}
4929
Bram Moolenaar080457c2020-03-03 21:53:32 +01004930 static int
4931evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4932{
4933 exptype_T type = EXPR_UNKNOWN;
4934 char_u *p;
4935 int len = 2;
4936 int type_is = FALSE;
4937
4938 // get the first variable
4939 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4940 return FAIL;
4941
4942 p = skipwhite(*arg);
4943 type = get_compare_type(p, &len, &type_is);
4944
4945 /*
4946 * If there is a comparative operator, use it.
4947 */
4948 if (type != EXPR_UNKNOWN)
4949 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004950 typval_T tv2;
4951 char_u *s1, *s2;
4952 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4953 int n;
4954
4955 // TODO: Only string == string is supported now
4956 if (tv->v_type != VAR_STRING)
4957 return FAIL;
4958 if (type != EXPR_EQUAL)
4959 return FAIL;
4960
4961 // get the second variable
Bram Moolenaar4227c782020-04-02 16:00:04 +02004962 init_tv(&tv2);
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004963 *arg = skipwhite(p + len);
4964 if (evaluate_const_expr7(arg, cctx, &tv2) == FAIL
4965 || tv2.v_type != VAR_STRING)
4966 {
4967 clear_tv(&tv2);
4968 return FAIL;
4969 }
4970 s1 = tv_get_string_buf(tv, buf1);
4971 s2 = tv_get_string_buf(&tv2, buf2);
4972 n = STRCMP(s1, s2);
4973 clear_tv(tv);
4974 clear_tv(&tv2);
4975 tv->v_type = VAR_BOOL;
4976 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004977 }
4978
4979 return OK;
4980}
4981
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004982static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
4983
4984/*
4985 * Compile constant || or &&.
4986 */
4987 static int
4988evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
4989{
4990 char_u *p = skipwhite(*arg);
4991 int opchar = *op;
4992
4993 if (p[0] == opchar && p[1] == opchar)
4994 {
4995 int val = tv2bool(tv);
4996
4997 /*
4998 * Repeat until there is no following "||" or "&&"
4999 */
5000 while (p[0] == opchar && p[1] == opchar)
5001 {
5002 typval_T tv2;
5003
5004 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
5005 return FAIL;
5006
5007 // eval the next expression
5008 *arg = skipwhite(p + 2);
5009 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01005010 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005011 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01005012 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005013 {
5014 clear_tv(&tv2);
5015 return FAIL;
5016 }
5017 if ((opchar == '&') == val)
5018 {
5019 // false || tv2 or true && tv2: use tv2
5020 clear_tv(tv);
5021 *tv = tv2;
5022 val = tv2bool(tv);
5023 }
5024 else
5025 clear_tv(&tv2);
5026 p = skipwhite(*arg);
5027 }
5028 }
5029
5030 return OK;
5031}
5032
5033/*
5034 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
5035 * Return FAIL if the expression is not a constant.
5036 */
5037 static int
5038evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
5039{
5040 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01005041 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005042 return FAIL;
5043
5044 // || and && work almost the same
5045 return evaluate_const_and_or(arg, cctx, "&&", tv);
5046}
5047
5048/*
5049 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
5050 * Return FAIL if the expression is not a constant.
5051 */
5052 static int
5053evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
5054{
5055 // evaluate the first expression
5056 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
5057 return FAIL;
5058
5059 // || and && work almost the same
5060 return evaluate_const_and_or(arg, cctx, "||", tv);
5061}
5062
5063/*
5064 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
5065 * E.g. for "has('feature')".
5066 * This does not produce error messages. "tv" should be cleared afterwards.
5067 * Return FAIL if the expression is not a constant.
5068 */
5069 static int
5070evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
5071{
5072 char_u *p;
5073
5074 // evaluate the first expression
5075 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
5076 return FAIL;
5077
5078 p = skipwhite(*arg);
5079 if (*p == '?')
5080 {
5081 int val = tv2bool(tv);
5082 typval_T tv2;
5083
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005084 // require space before and after the ?
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005085 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
5086 return FAIL;
5087
5088 // evaluate the second expression; any type is accepted
5089 clear_tv(tv);
5090 *arg = skipwhite(p + 1);
5091 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
5092 return FAIL;
5093
5094 // Check for the ":".
5095 p = skipwhite(*arg);
5096 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
5097 return FAIL;
5098
5099 // evaluate the third expression
5100 *arg = skipwhite(p + 1);
5101 tv2.v_type = VAR_UNKNOWN;
5102 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
5103 {
5104 clear_tv(&tv2);
5105 return FAIL;
5106 }
5107 if (val)
5108 {
5109 // use the expr after "?"
5110 clear_tv(&tv2);
5111 }
5112 else
5113 {
5114 // use the expr after ":"
5115 clear_tv(tv);
5116 *tv = tv2;
5117 }
5118 }
5119 return OK;
5120}
5121
5122/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005123 * compile "if expr"
5124 *
5125 * "if expr" Produces instructions:
5126 * EVAL expr Push result of "expr"
5127 * JUMP_IF_FALSE end
5128 * ... body ...
5129 * end:
5130 *
5131 * "if expr | else" Produces instructions:
5132 * EVAL expr Push result of "expr"
5133 * JUMP_IF_FALSE else
5134 * ... body ...
5135 * JUMP_ALWAYS end
5136 * else:
5137 * ... body ...
5138 * end:
5139 *
5140 * "if expr1 | elseif expr2 | else" Produces instructions:
5141 * EVAL expr Push result of "expr"
5142 * JUMP_IF_FALSE elseif
5143 * ... body ...
5144 * JUMP_ALWAYS end
5145 * elseif:
5146 * EVAL expr Push result of "expr"
5147 * JUMP_IF_FALSE else
5148 * ... body ...
5149 * JUMP_ALWAYS end
5150 * else:
5151 * ... body ...
5152 * end:
5153 */
5154 static char_u *
5155compile_if(char_u *arg, cctx_T *cctx)
5156{
5157 char_u *p = arg;
5158 garray_T *instr = &cctx->ctx_instr;
5159 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005160 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005161
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005162 // compile "expr"; if we know it evaluates to FALSE skip the block
5163 tv.v_type = VAR_UNKNOWN;
5164 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5165 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5166 else
5167 cctx->ctx_skip = MAYBE;
5168 clear_tv(&tv);
5169 if (cctx->ctx_skip == MAYBE)
5170 {
5171 p = arg;
5172 if (compile_expr1(&p, cctx) == FAIL)
5173 return NULL;
5174 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005175
5176 scope = new_scope(cctx, IF_SCOPE);
5177 if (scope == NULL)
5178 return NULL;
5179
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005180 if (cctx->ctx_skip == MAYBE)
5181 {
5182 // "where" is set when ":elseif", "else" or ":endif" is found
5183 scope->se_u.se_if.is_if_label = instr->ga_len;
5184 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5185 }
5186 else
5187 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005188
5189 return p;
5190}
5191
5192 static char_u *
5193compile_elseif(char_u *arg, cctx_T *cctx)
5194{
5195 char_u *p = arg;
5196 garray_T *instr = &cctx->ctx_instr;
5197 isn_T *isn;
5198 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005199 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200
5201 if (scope == NULL || scope->se_type != IF_SCOPE)
5202 {
5203 emsg(_(e_elseif_without_if));
5204 return NULL;
5205 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005206 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207
Bram Moolenaar158906c2020-02-06 20:39:45 +01005208 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005209 {
5210 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005212 return NULL;
5213 // previous "if" or "elseif" jumps here
5214 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5215 isn->isn_arg.jump.jump_where = instr->ga_len;
5216 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005217
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005218 // compile "expr"; if we know it evaluates to FALSE skip the block
5219 tv.v_type = VAR_UNKNOWN;
5220 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5221 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5222 else
5223 cctx->ctx_skip = MAYBE;
5224 clear_tv(&tv);
5225 if (cctx->ctx_skip == MAYBE)
5226 {
5227 p = arg;
5228 if (compile_expr1(&p, cctx) == FAIL)
5229 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005230
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005231 // "where" is set when ":elseif", "else" or ":endif" is found
5232 scope->se_u.se_if.is_if_label = instr->ga_len;
5233 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5234 }
5235 else
5236 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237
5238 return p;
5239}
5240
5241 static char_u *
5242compile_else(char_u *arg, cctx_T *cctx)
5243{
5244 char_u *p = arg;
5245 garray_T *instr = &cctx->ctx_instr;
5246 isn_T *isn;
5247 scope_T *scope = cctx->ctx_scope;
5248
5249 if (scope == NULL || scope->se_type != IF_SCOPE)
5250 {
5251 emsg(_(e_else_without_if));
5252 return NULL;
5253 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005254 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005255
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005256 // jump from previous block to the end, unless the else block is empty
5257 if (cctx->ctx_skip == MAYBE)
5258 {
5259 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005260 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005261 return NULL;
5262 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005263
Bram Moolenaar158906c2020-02-06 20:39:45 +01005264 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005265 {
5266 if (scope->se_u.se_if.is_if_label >= 0)
5267 {
5268 // previous "if" or "elseif" jumps here
5269 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5270 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005271 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005272 }
5273 }
5274
5275 if (cctx->ctx_skip != MAYBE)
5276 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005277
5278 return p;
5279}
5280
5281 static char_u *
5282compile_endif(char_u *arg, cctx_T *cctx)
5283{
5284 scope_T *scope = cctx->ctx_scope;
5285 ifscope_T *ifscope;
5286 garray_T *instr = &cctx->ctx_instr;
5287 isn_T *isn;
5288
5289 if (scope == NULL || scope->se_type != IF_SCOPE)
5290 {
5291 emsg(_(e_endif_without_if));
5292 return NULL;
5293 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005294 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005295 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005297 if (scope->se_u.se_if.is_if_label >= 0)
5298 {
5299 // previous "if" or "elseif" jumps here
5300 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5301 isn->isn_arg.jump.jump_where = instr->ga_len;
5302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005303 // Fill in the "end" label in jumps at the end of the blocks.
5304 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005305 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005306
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005307 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005308 return arg;
5309}
5310
5311/*
5312 * compile "for var in expr"
5313 *
5314 * Produces instructions:
5315 * PUSHNR -1
5316 * STORE loop-idx Set index to -1
5317 * EVAL expr Push result of "expr"
5318 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5319 * - if beyond end, jump to "end"
5320 * - otherwise get item from list and push it
5321 * STORE var Store item in "var"
5322 * ... body ...
5323 * JUMP top Jump back to repeat
5324 * end: DROP Drop the result of "expr"
5325 *
5326 */
5327 static char_u *
5328compile_for(char_u *arg, cctx_T *cctx)
5329{
5330 char_u *p;
5331 size_t varlen;
5332 garray_T *instr = &cctx->ctx_instr;
5333 garray_T *stack = &cctx->ctx_type_stack;
5334 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005335 lvar_T *loop_lvar; // loop iteration variable
5336 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005337 type_T *vartype;
5338
5339 // TODO: list of variables: "for [key, value] in dict"
5340 // parse "var"
5341 for (p = arg; eval_isnamec1(*p); ++p)
5342 ;
5343 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005344 var_lvar = lookup_local(arg, varlen, cctx);
5345 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005346 {
5347 semsg(_("E1023: variable already defined: %s"), arg);
5348 return NULL;
5349 }
5350
5351 // consume "in"
5352 p = skipwhite(p);
5353 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5354 {
5355 emsg(_(e_missing_in));
5356 return NULL;
5357 }
5358 p = skipwhite(p + 2);
5359
5360
5361 scope = new_scope(cctx, FOR_SCOPE);
5362 if (scope == NULL)
5363 return NULL;
5364
5365 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005366 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5367 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005368 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005369 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005370 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005371 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005372 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005373
5374 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005375 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5376 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005377 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005378 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005379 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005380 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005381 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005382
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005383 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005384
5385 // compile "expr", it remains on the stack until "endfor"
5386 arg = p;
5387 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005388 {
5389 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005390 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005391 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005392
5393 // now we know the type of "var"
5394 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5395 if (vartype->tt_type != VAR_LIST)
5396 {
5397 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005398 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005399 return NULL;
5400 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005401 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005402 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005403
5404 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005405 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005406
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005407 generate_FOR(cctx, loop_lvar->lv_idx);
5408 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005409
5410 return arg;
5411}
5412
5413/*
5414 * compile "endfor"
5415 */
5416 static char_u *
5417compile_endfor(char_u *arg, cctx_T *cctx)
5418{
5419 garray_T *instr = &cctx->ctx_instr;
5420 scope_T *scope = cctx->ctx_scope;
5421 forscope_T *forscope;
5422 isn_T *isn;
5423
5424 if (scope == NULL || scope->se_type != FOR_SCOPE)
5425 {
5426 emsg(_(e_for));
5427 return NULL;
5428 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005429 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005430 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005431 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005432
5433 // At end of ":for" scope jump back to the FOR instruction.
5434 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5435
5436 // Fill in the "end" label in the FOR statement so it can jump here
5437 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5438 isn->isn_arg.forloop.for_end = instr->ga_len;
5439
5440 // Fill in the "end" label any BREAK statements
5441 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5442
5443 // Below the ":for" scope drop the "expr" list from the stack.
5444 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5445 return NULL;
5446
5447 vim_free(scope);
5448
5449 return arg;
5450}
5451
5452/*
5453 * compile "while expr"
5454 *
5455 * Produces instructions:
5456 * top: EVAL expr Push result of "expr"
5457 * JUMP_IF_FALSE end jump if false
5458 * ... body ...
5459 * JUMP top Jump back to repeat
5460 * end:
5461 *
5462 */
5463 static char_u *
5464compile_while(char_u *arg, cctx_T *cctx)
5465{
5466 char_u *p = arg;
5467 garray_T *instr = &cctx->ctx_instr;
5468 scope_T *scope;
5469
5470 scope = new_scope(cctx, WHILE_SCOPE);
5471 if (scope == NULL)
5472 return NULL;
5473
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005474 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005475
5476 // compile "expr"
5477 if (compile_expr1(&p, cctx) == FAIL)
5478 return NULL;
5479
5480 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005481 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005482 JUMP_IF_FALSE, cctx) == FAIL)
5483 return FAIL;
5484
5485 return p;
5486}
5487
5488/*
5489 * compile "endwhile"
5490 */
5491 static char_u *
5492compile_endwhile(char_u *arg, cctx_T *cctx)
5493{
5494 scope_T *scope = cctx->ctx_scope;
5495
5496 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5497 {
5498 emsg(_(e_while));
5499 return NULL;
5500 }
5501 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005502 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005503
5504 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005505 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005506
5507 // Fill in the "end" label in the WHILE statement so it can jump here.
5508 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005509 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005510
5511 vim_free(scope);
5512
5513 return arg;
5514}
5515
5516/*
5517 * compile "continue"
5518 */
5519 static char_u *
5520compile_continue(char_u *arg, cctx_T *cctx)
5521{
5522 scope_T *scope = cctx->ctx_scope;
5523
5524 for (;;)
5525 {
5526 if (scope == NULL)
5527 {
5528 emsg(_(e_continue));
5529 return NULL;
5530 }
5531 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5532 break;
5533 scope = scope->se_outer;
5534 }
5535
5536 // Jump back to the FOR or WHILE instruction.
5537 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005538 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5539 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005540 return arg;
5541}
5542
5543/*
5544 * compile "break"
5545 */
5546 static char_u *
5547compile_break(char_u *arg, cctx_T *cctx)
5548{
5549 scope_T *scope = cctx->ctx_scope;
5550 endlabel_T **el;
5551
5552 for (;;)
5553 {
5554 if (scope == NULL)
5555 {
5556 emsg(_(e_break));
5557 return NULL;
5558 }
5559 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5560 break;
5561 scope = scope->se_outer;
5562 }
5563
5564 // Jump to the end of the FOR or WHILE loop.
5565 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005566 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005567 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005568 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005569 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5570 return FAIL;
5571
5572 return arg;
5573}
5574
5575/*
5576 * compile "{" start of block
5577 */
5578 static char_u *
5579compile_block(char_u *arg, cctx_T *cctx)
5580{
5581 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5582 return NULL;
5583 return skipwhite(arg + 1);
5584}
5585
5586/*
5587 * compile end of block: drop one scope
5588 */
5589 static void
5590compile_endblock(cctx_T *cctx)
5591{
5592 scope_T *scope = cctx->ctx_scope;
5593
5594 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005595 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005596 vim_free(scope);
5597}
5598
5599/*
5600 * compile "try"
5601 * Creates a new scope for the try-endtry, pointing to the first catch and
5602 * finally.
5603 * Creates another scope for the "try" block itself.
5604 * TRY instruction sets up exception handling at runtime.
5605 *
5606 * "try"
5607 * TRY -> catch1, -> finally push trystack entry
5608 * ... try block
5609 * "throw {exception}"
5610 * EVAL {exception}
5611 * THROW create exception
5612 * ... try block
5613 * " catch {expr}"
5614 * JUMP -> finally
5615 * catch1: PUSH exeception
5616 * EVAL {expr}
5617 * MATCH
5618 * JUMP nomatch -> catch2
5619 * CATCH remove exception
5620 * ... catch block
5621 * " catch"
5622 * JUMP -> finally
5623 * catch2: CATCH remove exception
5624 * ... catch block
5625 * " finally"
5626 * finally:
5627 * ... finally block
5628 * " endtry"
5629 * ENDTRY pop trystack entry, may rethrow
5630 */
5631 static char_u *
5632compile_try(char_u *arg, cctx_T *cctx)
5633{
5634 garray_T *instr = &cctx->ctx_instr;
5635 scope_T *try_scope;
5636 scope_T *scope;
5637
5638 // scope that holds the jumps that go to catch/finally/endtry
5639 try_scope = new_scope(cctx, TRY_SCOPE);
5640 if (try_scope == NULL)
5641 return NULL;
5642
5643 // "catch" is set when the first ":catch" is found.
5644 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005645 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005646 if (generate_instr(cctx, ISN_TRY) == NULL)
5647 return NULL;
5648
5649 // scope for the try block itself
5650 scope = new_scope(cctx, BLOCK_SCOPE);
5651 if (scope == NULL)
5652 return NULL;
5653
5654 return arg;
5655}
5656
5657/*
5658 * compile "catch {expr}"
5659 */
5660 static char_u *
5661compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5662{
5663 scope_T *scope = cctx->ctx_scope;
5664 garray_T *instr = &cctx->ctx_instr;
5665 char_u *p;
5666 isn_T *isn;
5667
5668 // end block scope from :try or :catch
5669 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5670 compile_endblock(cctx);
5671 scope = cctx->ctx_scope;
5672
5673 // Error if not in a :try scope
5674 if (scope == NULL || scope->se_type != TRY_SCOPE)
5675 {
5676 emsg(_(e_catch));
5677 return NULL;
5678 }
5679
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005680 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005681 {
5682 emsg(_("E1033: catch unreachable after catch-all"));
5683 return NULL;
5684 }
5685
5686 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005687 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005688 JUMP_ALWAYS, cctx) == FAIL)
5689 return NULL;
5690
5691 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005692 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005693 if (isn->isn_arg.try.try_catch == 0)
5694 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005695 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005696 {
5697 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005698 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005699 isn->isn_arg.jump.jump_where = instr->ga_len;
5700 }
5701
5702 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02005703 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005704 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005705 scope->se_u.se_try.ts_caught_all = TRUE;
5706 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005707 }
5708 else
5709 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005710 char_u *end;
5711 char_u *pat;
5712 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005713 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005714 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005715
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005716 // Push v:exception, push {expr} and MATCH
5717 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5718
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005719 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005720 if (*end != *p)
5721 {
5722 semsg(_("E1067: Separator mismatch: %s"), p);
5723 vim_free(tofree);
5724 return FAIL;
5725 }
5726 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005727 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005728 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005729 len = (int)(end - tofree);
5730 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005731 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005732 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005733 if (pat == NULL)
5734 return FAIL;
5735 if (generate_PUSHS(cctx, pat) == FAIL)
5736 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005738 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5739 return NULL;
5740
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005741 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005742 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5743 return NULL;
5744 }
5745
5746 if (generate_instr(cctx, ISN_CATCH) == NULL)
5747 return NULL;
5748
5749 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5750 return NULL;
5751 return p;
5752}
5753
5754 static char_u *
5755compile_finally(char_u *arg, cctx_T *cctx)
5756{
5757 scope_T *scope = cctx->ctx_scope;
5758 garray_T *instr = &cctx->ctx_instr;
5759 isn_T *isn;
5760
5761 // end block scope from :try or :catch
5762 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5763 compile_endblock(cctx);
5764 scope = cctx->ctx_scope;
5765
5766 // Error if not in a :try scope
5767 if (scope == NULL || scope->se_type != TRY_SCOPE)
5768 {
5769 emsg(_(e_finally));
5770 return NULL;
5771 }
5772
5773 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005774 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005775 if (isn->isn_arg.try.try_finally != 0)
5776 {
5777 emsg(_(e_finally_dup));
5778 return NULL;
5779 }
5780
5781 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005782 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005783
Bram Moolenaar585fea72020-04-02 22:33:21 +02005784 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005785 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005786 {
5787 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005788 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005789 isn->isn_arg.jump.jump_where = instr->ga_len;
5790 }
5791
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005792 // TODO: set index in ts_finally_label jumps
5793
5794 return arg;
5795}
5796
5797 static char_u *
5798compile_endtry(char_u *arg, cctx_T *cctx)
5799{
5800 scope_T *scope = cctx->ctx_scope;
5801 garray_T *instr = &cctx->ctx_instr;
5802 isn_T *isn;
5803
5804 // end block scope from :catch or :finally
5805 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5806 compile_endblock(cctx);
5807 scope = cctx->ctx_scope;
5808
5809 // Error if not in a :try scope
5810 if (scope == NULL || scope->se_type != TRY_SCOPE)
5811 {
5812 if (scope == NULL)
5813 emsg(_(e_no_endtry));
5814 else if (scope->se_type == WHILE_SCOPE)
5815 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01005816 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005817 emsg(_(e_endfor));
5818 else
5819 emsg(_(e_endif));
5820 return NULL;
5821 }
5822
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005823 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005824 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
5825 {
5826 emsg(_("E1032: missing :catch or :finally"));
5827 return NULL;
5828 }
5829
5830 // Fill in the "end" label in jumps at the end of the blocks, if not done
5831 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005832 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005833
5834 // End :catch or :finally scope: set value in ISN_TRY instruction
5835 if (isn->isn_arg.try.try_finally == 0)
5836 isn->isn_arg.try.try_finally = instr->ga_len;
5837 compile_endblock(cctx);
5838
5839 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
5840 return NULL;
5841 return arg;
5842}
5843
5844/*
5845 * compile "throw {expr}"
5846 */
5847 static char_u *
5848compile_throw(char_u *arg, cctx_T *cctx UNUSED)
5849{
5850 char_u *p = skipwhite(arg);
5851
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005852 if (compile_expr1(&p, cctx) == FAIL)
5853 return NULL;
5854 if (may_generate_2STRING(-1, cctx) == FAIL)
5855 return NULL;
5856 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5857 return NULL;
5858
5859 return p;
5860}
5861
5862/*
5863 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005864 * compile "echomsg expr"
5865 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01005866 * compile "execute expr"
5867 */
5868 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005869compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01005870{
5871 char_u *p = arg;
5872 int count = 0;
5873
5874 for (;;)
5875 {
5876 if (compile_expr1(&p, cctx) == FAIL)
5877 return NULL;
5878 ++count;
5879 p = skipwhite(p);
5880 if (ends_excmd(*p))
5881 break;
5882 }
5883
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005884 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
5885 generate_ECHO(cctx, cmdidx == CMD_echo, count);
5886 else if (cmdidx == CMD_execute)
5887 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
5888 else if (cmdidx == CMD_echomsg)
5889 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
5890 else
5891 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005892 return p;
5893}
5894
5895/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005896 * A command that is not compiled, execute with legacy code.
5897 */
5898 static char_u *
5899compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
5900{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005901 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02005902 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005903
5904 if (cctx->ctx_skip == TRUE)
5905 goto theend;
5906
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02005907 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
5908 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005909 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
5910 {
5911 // expand filename in "syntax include [@group] filename"
5912 has_expr = TRUE;
5913 eap->arg = skipwhite(eap->arg + 7);
5914 if (*eap->arg == '@')
5915 eap->arg = skiptowhite(eap->arg);
5916 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005917
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005918 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005919 {
5920 int count = 0;
5921 char_u *start = skipwhite(line);
5922
5923 // :cmd xxx`=expr1`yyy`=expr2`zzz
5924 // PUSHS ":cmd xxx"
5925 // eval expr1
5926 // PUSHS "yyy"
5927 // eval expr2
5928 // PUSHS "zzz"
5929 // EXECCONCAT 5
5930 for (;;)
5931 {
5932 if (p > start)
5933 {
5934 generate_PUSHS(cctx, vim_strnsave(start, (int)(p - start)));
5935 ++count;
5936 }
5937 p += 2;
5938 if (compile_expr1(&p, cctx) == FAIL)
5939 return NULL;
5940 may_generate_2STRING(-1, cctx);
5941 ++count;
5942 p = skipwhite(p);
5943 if (*p != '`')
5944 {
5945 emsg(_("E1083: missing backtick"));
5946 return NULL;
5947 }
5948 start = p + 1;
5949
5950 p = (char_u *)strstr((char *)start, "`=");
5951 if (p == NULL)
5952 {
5953 if (*skipwhite(start) != NUL)
5954 {
5955 generate_PUSHS(cctx, vim_strsave(start));
5956 ++count;
5957 }
5958 break;
5959 }
5960 }
5961 generate_EXECCONCAT(cctx, count);
5962 }
5963 else
5964 generate_EXEC(cctx, line);
5965
5966theend:
5967 return (char_u *)"";
5968}
5969
5970/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005971 * After ex_function() has collected all the function lines: parse and compile
5972 * the lines into instructions.
5973 * Adds the function to "def_functions".
5974 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
5975 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02005976 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005977 * This can be used recursively through compile_lambda(), which may reallocate
5978 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005979 */
5980 void
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02005981compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005982{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005983 char_u *line = NULL;
5984 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005985 char *errormsg = NULL; // error message
5986 int had_return = FALSE;
5987 cctx_T cctx;
5988 garray_T *instr;
5989 int called_emsg_before = called_emsg;
5990 int ret = FAIL;
5991 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005992 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005994 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005995 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01005996
Bram Moolenaar05afcee2020-03-31 23:32:31 +02005997 if (ufunc->uf_dfunc_idx >= 0)
5998 {
5999 // Redefining a function that was compiled before.
6000 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
6001
6002 // Free old instructions.
6003 delete_def_function_contents(dfunc);
6004 }
6005 else
6006 {
6007 // Add the function to "def_functions".
6008 if (ga_grow(&def_functions, 1) == FAIL)
6009 return;
6010 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006011 CLEAR_POINTER(dfunc);
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006012 dfunc->df_idx = def_functions.ga_len;
6013 ufunc->uf_dfunc_idx = dfunc->df_idx;
6014 dfunc->df_ufunc = ufunc;
6015 ++def_functions.ga_len;
6016 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006017 }
6018
Bram Moolenaara80faa82020-04-12 19:37:17 +02006019 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006020 cctx.ctx_ufunc = ufunc;
6021 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006022 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006023 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6024 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6025 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6026 cctx.ctx_type_list = &ufunc->uf_type_list;
6027 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6028 instr = &cctx.ctx_instr;
6029
6030 // Most modern script version.
6031 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6032
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006033 if (ufunc->uf_def_args.ga_len > 0)
6034 {
6035 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006036 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006037 int i;
6038 char_u *arg;
6039 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6040
6041 // Produce instructions for the default values of optional arguments.
6042 // Store the instruction index in uf_def_arg_idx[] so that we know
6043 // where to start when the function is called, depending on the number
6044 // of arguments.
6045 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6046 if (ufunc->uf_def_arg_idx == NULL)
6047 goto erret;
6048 for (i = 0; i < count; ++i)
6049 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006050 garray_T *stack = &cctx.ctx_type_stack;
6051 type_T *val_type;
6052 int arg_idx = first_def_arg + i;
6053
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006054 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6055 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006056 if (compile_expr1(&arg, &cctx) == FAIL)
6057 goto erret;
6058
6059 // If no type specified use the type of the default value.
6060 // Otherwise check that the default value type matches the
6061 // specified type.
6062 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6063 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6064 ufunc->uf_arg_types[arg_idx] = val_type;
6065 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6066 == FAIL)
6067 {
6068 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6069 arg_idx + 1);
6070 goto erret;
6071 }
6072
6073 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006074 goto erret;
6075 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006076 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6077 }
6078
6079 /*
6080 * Loop over all the lines of the function and generate instructions.
6081 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006082 for (;;)
6083 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006084 exarg_T ea;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006085 int is_ex_command = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006086
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006087 // Bail out on the first error to avoid a flood of errors and report
6088 // the right line number when inside try/catch.
6089 if (emsg_before != called_emsg)
6090 goto erret;
6091
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006092 if (line != NULL && *line == '|')
6093 // the line continues after a '|'
6094 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006095 else if (line != NULL && *line != NUL
6096 && !(*line == '#' && (line == cctx.ctx_line_start
6097 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006098 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006099 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006100 goto erret;
6101 }
6102 else
6103 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006104 line = next_line_from_context(&cctx);
6105 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006106 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006107 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006108 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006109 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006110
6111 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006112 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006113 ea.cmdlinep = &line;
6114 ea.cmd = skipwhite(line);
6115
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006116 // Some things can be recognized by the first character.
6117 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006118 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006119 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006120 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006121 if (ea.cmd[1] != '{')
6122 {
6123 line = (char_u *)"";
6124 continue;
6125 }
6126 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006127
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006128 case '}':
6129 {
6130 // "}" ends a block scope
6131 scopetype_T stype = cctx.ctx_scope == NULL
6132 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006133
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006134 if (stype == BLOCK_SCOPE)
6135 {
6136 compile_endblock(&cctx);
6137 line = ea.cmd;
6138 }
6139 else
6140 {
6141 emsg(_("E1025: using } outside of a block scope"));
6142 goto erret;
6143 }
6144 if (line != NULL)
6145 line = skipwhite(ea.cmd + 1);
6146 continue;
6147 }
6148
6149 case '{':
6150 // "{" starts a block scope
6151 // "{'a': 1}->func() is something else
6152 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6153 {
6154 line = compile_block(ea.cmd, &cctx);
6155 continue;
6156 }
6157 break;
6158
6159 case ':':
6160 is_ex_command = TRUE;
6161 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006162 }
6163
6164 /*
6165 * COMMAND MODIFIERS
6166 */
6167 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6168 {
6169 if (errormsg != NULL)
6170 goto erret;
6171 // empty line or comment
6172 line = (char_u *)"";
6173 continue;
6174 }
6175
6176 // Skip ":call" to get to the function name.
6177 if (checkforcmd(&ea.cmd, "call", 3))
6178 ea.cmd = skipwhite(ea.cmd);
6179
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006180 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006181 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006182 // Assuming the command starts with a variable or function name,
6183 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
6184 // val".
6185 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
6186 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006187 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006188 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006189 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006190 int oplen;
6191 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006192
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006193 oplen = assignment_len(skipwhite(p), &heredoc);
6194 if (oplen > 0)
6195 {
6196 // Recognize an assignment if we recognize the variable
6197 // name:
6198 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006199 // "local = expr" where "local" is a local var.
6200 // "script = expr" where "script" is a script-local var.
6201 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006202 // "&opt = expr"
6203 // "$ENV = expr"
6204 // "@r = expr"
6205 if (*ea.cmd == '&'
6206 || *ea.cmd == '$'
6207 || *ea.cmd == '@'
6208 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006209 || lookup_local(ea.cmd, p - ea.cmd, &cctx) != NULL
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006210 || lookup_script(ea.cmd, p - ea.cmd) == OK
6211 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
6212 {
6213 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6214 if (line == NULL)
6215 goto erret;
6216 continue;
6217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006218 }
6219 }
6220 }
6221
6222 /*
6223 * COMMAND after range
6224 */
6225 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006226 p = find_ex_command(&ea, NULL, is_ex_command ? NULL
6227 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006228 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006229
6230 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6231 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006232 if (cctx.ctx_skip == TRUE)
6233 {
6234 line += STRLEN(line);
6235 continue;
6236 }
6237
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006238 // Expression or function call.
6239 if (ea.cmdidx == CMD_eval)
6240 {
6241 p = ea.cmd;
6242 if (compile_expr1(&p, &cctx) == FAIL)
6243 goto erret;
6244
6245 // drop the return value
6246 generate_instr_drop(&cctx, ISN_DROP, 1);
6247 line = p;
6248 continue;
6249 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006250 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251 iemsg("Command from find_ex_command() not handled");
6252 goto erret;
6253 }
6254
6255 p = skipwhite(p);
6256
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006257 if (cctx.ctx_skip == TRUE
6258 && ea.cmdidx != CMD_elseif
6259 && ea.cmdidx != CMD_else
6260 && ea.cmdidx != CMD_endif)
6261 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006262 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006263 continue;
6264 }
6265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006266 switch (ea.cmdidx)
6267 {
6268 case CMD_def:
6269 case CMD_function:
6270 // TODO: Nested function
6271 emsg("Nested function not implemented yet");
6272 goto erret;
6273
6274 case CMD_return:
6275 line = compile_return(p, set_return_type, &cctx);
6276 had_return = TRUE;
6277 break;
6278
6279 case CMD_let:
6280 case CMD_const:
6281 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
6282 break;
6283
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006284 case CMD_unlet:
6285 case CMD_unlockvar:
6286 case CMD_lockvar:
6287 line = compile_unletlock(p, &ea, &cctx);
6288 break;
6289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006290 case CMD_import:
6291 line = compile_import(p, &cctx);
6292 break;
6293
6294 case CMD_if:
6295 line = compile_if(p, &cctx);
6296 break;
6297 case CMD_elseif:
6298 line = compile_elseif(p, &cctx);
6299 break;
6300 case CMD_else:
6301 line = compile_else(p, &cctx);
6302 break;
6303 case CMD_endif:
6304 line = compile_endif(p, &cctx);
6305 break;
6306
6307 case CMD_while:
6308 line = compile_while(p, &cctx);
6309 break;
6310 case CMD_endwhile:
6311 line = compile_endwhile(p, &cctx);
6312 break;
6313
6314 case CMD_for:
6315 line = compile_for(p, &cctx);
6316 break;
6317 case CMD_endfor:
6318 line = compile_endfor(p, &cctx);
6319 break;
6320 case CMD_continue:
6321 line = compile_continue(p, &cctx);
6322 break;
6323 case CMD_break:
6324 line = compile_break(p, &cctx);
6325 break;
6326
6327 case CMD_try:
6328 line = compile_try(p, &cctx);
6329 break;
6330 case CMD_catch:
6331 line = compile_catch(p, &cctx);
6332 break;
6333 case CMD_finally:
6334 line = compile_finally(p, &cctx);
6335 break;
6336 case CMD_endtry:
6337 line = compile_endtry(p, &cctx);
6338 break;
6339 case CMD_throw:
6340 line = compile_throw(p, &cctx);
6341 break;
6342
6343 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006344 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006345 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006346 case CMD_echomsg:
6347 case CMD_echoerr:
6348 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006349 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006350
6351 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006352 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006353 // Not recognized, execute with do_cmdline_cmd().
6354 ea.arg = p;
6355 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006356 break;
6357 }
6358 if (line == NULL)
6359 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006360 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006361
6362 if (cctx.ctx_type_stack.ga_len < 0)
6363 {
6364 iemsg("Type stack underflow");
6365 goto erret;
6366 }
6367 }
6368
6369 if (cctx.ctx_scope != NULL)
6370 {
6371 if (cctx.ctx_scope->se_type == IF_SCOPE)
6372 emsg(_(e_endif));
6373 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6374 emsg(_(e_endwhile));
6375 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6376 emsg(_(e_endfor));
6377 else
6378 emsg(_("E1026: Missing }"));
6379 goto erret;
6380 }
6381
6382 if (!had_return)
6383 {
6384 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6385 {
6386 emsg(_("E1027: Missing return statement"));
6387 goto erret;
6388 }
6389
6390 // Return zero if there is no return at the end.
6391 generate_PUSHNR(&cctx, 0);
6392 generate_instr(&cctx, ISN_RETURN);
6393 }
6394
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006395 {
6396 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6397 + ufunc->uf_dfunc_idx;
6398 dfunc->df_deleted = FALSE;
6399 dfunc->df_instr = instr->ga_data;
6400 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006401 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006402 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006403 if (cctx.ctx_outer_used)
6404 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006405 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006407 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006408 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006409 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006410
6411 // Create a type for the function, with the return type and any
6412 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006413 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6414 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006415 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006416 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006417 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6418 argcount, &ufunc->uf_type_list);
6419 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006420 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006421 argcount + varargs,
6422 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006423 {
6424 ret = FAIL;
6425 goto erret;
6426 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006427 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6428 ufunc->uf_func_type->tt_min_argcount =
6429 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006430 if (ufunc->uf_arg_types == NULL)
6431 {
6432 int i;
6433
6434 // lambda does not have argument types.
6435 for (i = 0; i < argcount; ++i)
6436 ufunc->uf_func_type->tt_args[i] = &t_any;
6437 }
6438 else
6439 mch_memmove(ufunc->uf_func_type->tt_args,
6440 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006441 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006442 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006443 ufunc->uf_func_type->tt_args[argcount] =
6444 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006445 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6446 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006447 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006448 else
6449 // No arguments, can use a predefined type.
6450 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6451 argcount, &ufunc->uf_type_list);
6452
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006453 }
6454
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006455 ret = OK;
6456
6457erret:
6458 if (ret == FAIL)
6459 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006460 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006461 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6462 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006463
6464 for (idx = 0; idx < instr->ga_len; ++idx)
6465 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006466 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006468 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006469 if (!dfunc->df_deleted)
6470 --def_functions.ga_len;
6471
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006472 while (cctx.ctx_scope != NULL)
6473 drop_scope(&cctx);
6474
Bram Moolenaar20431c92020-03-20 18:39:46 +01006475 // Don't execute this function body.
6476 ga_clear_strings(&ufunc->uf_lines);
6477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006478 if (errormsg != NULL)
6479 emsg(errormsg);
6480 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006481 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006482 }
6483
6484 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006485 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006486 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006487 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006488}
6489
6490/*
6491 * Delete an instruction, free what it contains.
6492 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006493 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006494delete_instr(isn_T *isn)
6495{
6496 switch (isn->isn_type)
6497 {
6498 case ISN_EXEC:
6499 case ISN_LOADENV:
6500 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006501 case ISN_LOADB:
6502 case ISN_LOADW:
6503 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006504 case ISN_LOADOPT:
6505 case ISN_MEMBER:
6506 case ISN_PUSHEXC:
6507 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006508 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006509 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006510 case ISN_STOREB:
6511 case ISN_STOREW:
6512 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006513 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006514 vim_free(isn->isn_arg.string);
6515 break;
6516
6517 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006518 case ISN_STORES:
6519 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006520 break;
6521
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006522 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006523 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006524 vim_free(isn->isn_arg.unlet.ul_name);
6525 break;
6526
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006527 case ISN_STOREOPT:
6528 vim_free(isn->isn_arg.storeopt.so_name);
6529 break;
6530
6531 case ISN_PUSHBLOB: // push blob isn_arg.blob
6532 blob_unref(isn->isn_arg.blob);
6533 break;
6534
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006535 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006536#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006537 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006538#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006539 break;
6540
6541 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006542#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006543 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006544#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006545 break;
6546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 case ISN_UCALL:
6548 vim_free(isn->isn_arg.ufunc.cuf_name);
6549 break;
6550
6551 case ISN_2BOOL:
6552 case ISN_2STRING:
6553 case ISN_ADDBLOB:
6554 case ISN_ADDLIST:
6555 case ISN_BCALL:
6556 case ISN_CATCH:
6557 case ISN_CHECKNR:
6558 case ISN_CHECKTYPE:
6559 case ISN_COMPAREANY:
6560 case ISN_COMPAREBLOB:
6561 case ISN_COMPAREBOOL:
6562 case ISN_COMPAREDICT:
6563 case ISN_COMPAREFLOAT:
6564 case ISN_COMPAREFUNC:
6565 case ISN_COMPARELIST:
6566 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006567 case ISN_COMPARESPECIAL:
6568 case ISN_COMPARESTRING:
6569 case ISN_CONCAT:
6570 case ISN_DCALL:
6571 case ISN_DROP:
6572 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006573 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006574 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006575 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006576 case ISN_EXECCONCAT:
6577 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006578 case ISN_FOR:
6579 case ISN_FUNCREF:
6580 case ISN_INDEX:
6581 case ISN_JUMP:
6582 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006583 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006584 case ISN_LOADSCRIPT:
6585 case ISN_LOADREG:
6586 case ISN_LOADV:
6587 case ISN_NEGATENR:
6588 case ISN_NEWDICT:
6589 case ISN_NEWLIST:
6590 case ISN_OPNR:
6591 case ISN_OPFLOAT:
6592 case ISN_OPANY:
6593 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006594 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006595 case ISN_PUSHF:
6596 case ISN_PUSHNR:
6597 case ISN_PUSHBOOL:
6598 case ISN_PUSHSPEC:
6599 case ISN_RETURN:
6600 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006601 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006603 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006604 case ISN_STORESCRIPT:
6605 case ISN_THROW:
6606 case ISN_TRY:
6607 // nothing allocated
6608 break;
6609 }
6610}
6611
6612/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006613 * Free all instructions for "dfunc".
6614 */
6615 static void
6616delete_def_function_contents(dfunc_T *dfunc)
6617{
6618 int idx;
6619
6620 ga_clear(&dfunc->df_def_args_isn);
6621
6622 if (dfunc->df_instr != NULL)
6623 {
6624 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6625 delete_instr(dfunc->df_instr + idx);
6626 VIM_CLEAR(dfunc->df_instr);
6627 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006628 if (dfunc->df_funcstack != NULL)
6629 {
6630 // Decrease the reference count for the context of a closure. If down
6631 // to zero free it and clear the variables on the stack.
6632 if (--dfunc->df_funcstack->fs_refcount == 0)
6633 {
6634 garray_T *gap = &dfunc->df_funcstack->fs_ga;
6635 typval_T *stack = gap->ga_data;
6636 int i;
6637
6638 for (i = 0; i < gap->ga_len; ++i)
6639 clear_tv(stack + i);
6640 ga_clear(gap);
6641 vim_free(dfunc->df_funcstack);
6642 }
6643 dfunc->df_funcstack = NULL;
6644 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006645
6646 dfunc->df_deleted = TRUE;
6647}
6648
6649/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006650 * When a user function is deleted, delete any associated def function.
6651 */
6652 void
6653delete_def_function(ufunc_T *ufunc)
6654{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006655 if (ufunc->uf_dfunc_idx >= 0)
6656 {
6657 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6658 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006659
Bram Moolenaar20431c92020-03-20 18:39:46 +01006660 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006661 }
6662}
6663
6664#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006665/*
6666 * Free all functions defined with ":def".
6667 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006668 void
6669free_def_functions(void)
6670{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006671 int idx;
6672
6673 for (idx = 0; idx < def_functions.ga_len; ++idx)
6674 {
6675 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6676
6677 delete_def_function_contents(dfunc);
6678 }
6679
6680 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006681}
6682#endif
6683
6684
6685#endif // FEAT_EVAL