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