blob: 300adfbec5adc593b87b858a4d1a78c887a471b5 [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/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200189 * Lookup an argument in the current function and an enclosing function.
190 * Returns the argument index in "idxp"
191 * Returns the argument type in "type"
192 * Sets "gen_load_outer" to TRUE if found in outer scope.
193 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100194 */
195 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200196lookup_arg(
197 char_u *name,
198 size_t len,
199 int *idxp,
200 type_T **type,
201 int *gen_load_outer,
202 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100203{
204 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200205 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100206
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100207 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200208 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100209 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
210 {
211 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
212
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200213 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
214 {
215 if (idxp != NULL)
216 {
217 // Arguments are located above the frame pointer. One further
218 // if there is a vararg argument
219 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
220 + STACK_FRAME_SIZE)
221 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
222
223 if (cctx->ctx_ufunc->uf_arg_types != NULL)
224 *type = cctx->ctx_ufunc->uf_arg_types[idx];
225 else
226 *type = &t_any;
227 }
228 return OK;
229 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100231
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200232 va_name = cctx->ctx_ufunc->uf_va_name;
233 if (va_name != NULL
234 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
235 {
236 if (idxp != NULL)
237 {
238 // varargs is always the last argument
239 *idxp = -STACK_FRAME_SIZE - 1;
240 *type = cctx->ctx_ufunc->uf_va_type;
241 }
242 return OK;
243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200245 if (cctx->ctx_outer != NULL)
246 {
247 // Lookup the name for an argument of the outer function.
248 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
249 == OK)
250 {
251 *gen_load_outer = TRUE;
252 return OK;
253 }
254 }
255
256 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257}
258
259/*
260 * Lookup a variable in the current script.
261 * Returns OK or FAIL.
262 */
263 static int
264lookup_script(char_u *name, size_t len)
265{
266 int cc;
267 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
268 dictitem_T *di;
269
270 cc = name[len];
271 name[len] = NUL;
272 di = find_var_in_ht(ht, 0, name, TRUE);
273 name[len] = cc;
274 return di == NULL ? FAIL: OK;
275}
276
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100277/*
278 * Check if "p[len]" is already defined, either in script "import_sid" or in
279 * compilation context "cctx".
280 * Return FAIL and give an error if it defined.
281 */
282 int
283check_defined(char_u *p, int len, cctx_T *cctx)
284{
285 if (lookup_script(p, len) == OK
286 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200287 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100288 || find_imported(p, len, cctx) != NULL)))
289 {
290 semsg("E1073: imported name already defined: %s", p);
291 return FAIL;
292 }
293 return OK;
294}
295
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200296/*
297 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
298 * be freed later.
299 */
300 static type_T *
301alloc_type(garray_T *type_gap)
302{
303 type_T *type;
304
305 if (ga_grow(type_gap, 1) == FAIL)
306 return NULL;
307 type = ALLOC_CLEAR_ONE(type_T);
308 if (type != NULL)
309 {
310 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
311 ++type_gap->ga_len;
312 }
313 return type;
314}
315
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100316 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200317get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318{
319 type_T *type;
320
321 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200322 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100323 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200324 if (member_type->tt_type == VAR_VOID
325 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100326 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100327 if (member_type->tt_type == VAR_BOOL)
328 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100329 if (member_type->tt_type == VAR_NUMBER)
330 return &t_list_number;
331 if (member_type->tt_type == VAR_STRING)
332 return &t_list_string;
333
334 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200335 type = alloc_type(type_gap);
336 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100337 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100338 type->tt_type = VAR_LIST;
339 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200340 type->tt_argcount = 0;
341 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100342 return type;
343}
344
345 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200346get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100347{
348 type_T *type;
349
350 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200351 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100352 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200353 if (member_type->tt_type == VAR_VOID
354 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100355 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100356 if (member_type->tt_type == VAR_BOOL)
357 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100358 if (member_type->tt_type == VAR_NUMBER)
359 return &t_dict_number;
360 if (member_type->tt_type == VAR_STRING)
361 return &t_dict_string;
362
363 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200364 type = alloc_type(type_gap);
365 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100366 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100367 type->tt_type = VAR_DICT;
368 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200369 type->tt_argcount = 0;
370 type->tt_args = NULL;
371 return type;
372}
373
374/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200375 * Allocate a new type for a function.
376 */
377 static type_T *
378alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
379{
380 type_T *type = alloc_type(type_gap);
381
382 if (type == NULL)
383 return &t_any;
384 type->tt_type = VAR_FUNC;
385 type->tt_member = ret_type;
386 type->tt_argcount = argcount;
387 type->tt_args = NULL;
388 return type;
389}
390
391/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200392 * Get a function type, based on the return type "ret_type".
393 * If "argcount" is -1 or 0 a predefined type can be used.
394 * If "argcount" > 0 always create a new type, so that arguments can be added.
395 */
396 static type_T *
397get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
398{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200399 // recognize commonly used types
400 if (argcount <= 0)
401 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200402 if (ret_type == &t_unknown)
403 {
404 // (argcount == 0) is not possible
405 return &t_func_unknown;
406 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200407 if (ret_type == &t_void)
408 {
409 if (argcount == 0)
410 return &t_func_0_void;
411 else
412 return &t_func_void;
413 }
414 if (ret_type == &t_any)
415 {
416 if (argcount == 0)
417 return &t_func_0_any;
418 else
419 return &t_func_any;
420 }
421 if (ret_type == &t_number)
422 {
423 if (argcount == 0)
424 return &t_func_0_number;
425 else
426 return &t_func_number;
427 }
428 if (ret_type == &t_string)
429 {
430 if (argcount == 0)
431 return &t_func_0_string;
432 else
433 return &t_func_string;
434 }
435 }
436
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200437 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100438}
439
Bram Moolenaara8c17702020-04-01 21:17:24 +0200440/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200441 * For a function type, reserve space for "argcount" argument types (including
442 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200443 */
444 static int
445func_type_add_arg_types(
446 type_T *functype,
447 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200448 garray_T *type_gap)
449{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200450 // To make it easy to free the space needed for the argument types, add the
451 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200452 if (ga_grow(type_gap, 1) == FAIL)
453 return FAIL;
454 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
455 if (functype->tt_args == NULL)
456 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200457 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
458 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200459 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200460 return OK;
461}
462
463/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200464 * Return the type_T for a typval. Only for primitive types.
465 */
466 static type_T *
467typval2type(typval_T *tv)
468{
469 if (tv->v_type == VAR_NUMBER)
470 return &t_number;
471 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200472 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200473 if (tv->v_type == VAR_STRING)
474 return &t_string;
475 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
476 return &t_list_string;
477 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
478 return &t_dict_any;
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200479 return &t_any; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200480}
481
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200482 static void
483type_mismatch(type_T *expected, type_T *actual)
484{
485 char *tofree1, *tofree2;
486
487 semsg(_("E1013: type mismatch, expected %s but got %s"),
488 type_name(expected, &tofree1), type_name(actual, &tofree2));
489 vim_free(tofree1);
490 vim_free(tofree2);
491}
492
493 static void
494arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
495{
496 char *tofree1, *tofree2;
497
498 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
499 argidx,
500 type_name(expected, &tofree1), type_name(actual, &tofree2));
501 vim_free(tofree1);
502 vim_free(tofree2);
503}
504
505/*
506 * Check if the expected and actual types match.
507 * Does not allow for assigning "any" to a specific type.
508 */
509 static int
510check_type(type_T *expected, type_T *actual, int give_msg)
511{
512 int ret = OK;
513
514 // When expected is "unknown" we accept any actual type.
515 // When expected is "any" we accept any actual type except "void".
516 if (expected->tt_type != VAR_UNKNOWN
517 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
518
519 {
520 if (expected->tt_type != actual->tt_type)
521 {
522 if (give_msg)
523 type_mismatch(expected, actual);
524 return FAIL;
525 }
526 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
527 {
528 // "unknown" is used for an empty list or dict
529 if (actual->tt_member != &t_unknown)
530 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
531 }
532 else if (expected->tt_type == VAR_FUNC)
533 {
534 if (expected->tt_member != &t_unknown)
535 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
536 if (ret == OK && expected->tt_argcount != -1
537 && (actual->tt_argcount < expected->tt_min_argcount
538 || actual->tt_argcount > expected->tt_argcount))
539 ret = FAIL;
540 }
541 if (ret == FAIL && give_msg)
542 type_mismatch(expected, actual);
543 }
544 return ret;
545}
546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100547/////////////////////////////////////////////////////////////////////
548// Following generate_ functions expect the caller to call ga_grow().
549
Bram Moolenaar080457c2020-03-03 21:53:32 +0100550#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL
551#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK
552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553/*
554 * Generate an instruction without arguments.
555 * Returns a pointer to the new instruction, NULL if failed.
556 */
557 static isn_T *
558generate_instr(cctx_T *cctx, isntype_T isn_type)
559{
560 garray_T *instr = &cctx->ctx_instr;
561 isn_T *isn;
562
Bram Moolenaar080457c2020-03-03 21:53:32 +0100563 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100564 if (ga_grow(instr, 1) == FAIL)
565 return NULL;
566 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
567 isn->isn_type = isn_type;
568 isn->isn_lnum = cctx->ctx_lnum + 1;
569 ++instr->ga_len;
570
571 return isn;
572}
573
574/*
575 * Generate an instruction without arguments.
576 * "drop" will be removed from the stack.
577 * Returns a pointer to the new instruction, NULL if failed.
578 */
579 static isn_T *
580generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
581{
582 garray_T *stack = &cctx->ctx_type_stack;
583
Bram Moolenaar080457c2020-03-03 21:53:32 +0100584 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 stack->ga_len -= drop;
586 return generate_instr(cctx, isn_type);
587}
588
589/*
590 * Generate instruction "isn_type" and put "type" on the type stack.
591 */
592 static isn_T *
593generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
594{
595 isn_T *isn;
596 garray_T *stack = &cctx->ctx_type_stack;
597
598 if ((isn = generate_instr(cctx, isn_type)) == NULL)
599 return NULL;
600
601 if (ga_grow(stack, 1) == FAIL)
602 return NULL;
603 ((type_T **)stack->ga_data)[stack->ga_len] = type;
604 ++stack->ga_len;
605
606 return isn;
607}
608
609/*
610 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
611 */
612 static int
613may_generate_2STRING(int offset, cctx_T *cctx)
614{
615 isn_T *isn;
616 garray_T *stack = &cctx->ctx_type_stack;
617 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
618
619 if ((*type)->tt_type == VAR_STRING)
620 return OK;
621 *type = &t_string;
622
623 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
624 return FAIL;
625 isn->isn_arg.number = offset;
626
627 return OK;
628}
629
630 static int
631check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
632{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200633 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200635 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 {
637 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100638 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100639 else
640 semsg(_("E1036: %c requires number or float arguments"), *op);
641 return FAIL;
642 }
643 return OK;
644}
645
646/*
647 * Generate an instruction with two arguments. The instruction depends on the
648 * type of the arguments.
649 */
650 static int
651generate_two_op(cctx_T *cctx, char_u *op)
652{
653 garray_T *stack = &cctx->ctx_type_stack;
654 type_T *type1;
655 type_T *type2;
656 vartype_T vartype;
657 isn_T *isn;
658
Bram Moolenaar080457c2020-03-03 21:53:32 +0100659 RETURN_OK_IF_SKIP(cctx);
660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100661 // Get the known type of the two items on the stack. If they are matching
662 // use a type-specific instruction. Otherwise fall back to runtime type
663 // checking.
664 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
665 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200666 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100667 if (type1->tt_type == type2->tt_type
668 && (type1->tt_type == VAR_NUMBER
669 || type1->tt_type == VAR_LIST
670#ifdef FEAT_FLOAT
671 || type1->tt_type == VAR_FLOAT
672#endif
673 || type1->tt_type == VAR_BLOB))
674 vartype = type1->tt_type;
675
676 switch (*op)
677 {
678 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200679 && type1->tt_type != VAR_ANY
680 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 && check_number_or_float(
682 type1->tt_type, type2->tt_type, op) == FAIL)
683 return FAIL;
684 isn = generate_instr_drop(cctx,
685 vartype == VAR_NUMBER ? ISN_OPNR
686 : vartype == VAR_LIST ? ISN_ADDLIST
687 : vartype == VAR_BLOB ? ISN_ADDBLOB
688#ifdef FEAT_FLOAT
689 : vartype == VAR_FLOAT ? ISN_OPFLOAT
690#endif
691 : ISN_OPANY, 1);
692 if (isn != NULL)
693 isn->isn_arg.op.op_type = EXPR_ADD;
694 break;
695
696 case '-':
697 case '*':
698 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
699 op) == FAIL)
700 return FAIL;
701 if (vartype == VAR_NUMBER)
702 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
703#ifdef FEAT_FLOAT
704 else if (vartype == VAR_FLOAT)
705 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
706#endif
707 else
708 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
709 if (isn != NULL)
710 isn->isn_arg.op.op_type = *op == '*'
711 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
712 break;
713
Bram Moolenaar4c683752020-04-05 21:38:23 +0200714 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200716 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717 && type2->tt_type != VAR_NUMBER))
718 {
719 emsg(_("E1035: % requires number arguments"));
720 return FAIL;
721 }
722 isn = generate_instr_drop(cctx,
723 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
724 if (isn != NULL)
725 isn->isn_arg.op.op_type = EXPR_REM;
726 break;
727 }
728
729 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200730 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100731 {
732 type_T *type = &t_any;
733
734#ifdef FEAT_FLOAT
735 // float+number and number+float results in float
736 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
737 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
738 type = &t_float;
739#endif
740 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
741 }
742
743 return OK;
744}
745
746/*
747 * Generate an ISN_COMPARE* instruction with a boolean result.
748 */
749 static int
750generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
751{
752 isntype_T isntype = ISN_DROP;
753 isn_T *isn;
754 garray_T *stack = &cctx->ctx_type_stack;
755 vartype_T type1;
756 vartype_T type2;
757
Bram Moolenaar080457c2020-03-03 21:53:32 +0100758 RETURN_OK_IF_SKIP(cctx);
759
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760 // Get the known type of the two items on the stack. If they are matching
761 // use a type-specific instruction. Otherwise fall back to runtime type
762 // checking.
763 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
764 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200765 if (type1 == VAR_UNKNOWN)
766 type1 = VAR_ANY;
767 if (type2 == VAR_UNKNOWN)
768 type2 = VAR_ANY;
769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 if (type1 == type2)
771 {
772 switch (type1)
773 {
774 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
775 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
776 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
777 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
778 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
779 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
780 case VAR_LIST: isntype = ISN_COMPARELIST; break;
781 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
782 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 default: isntype = ISN_COMPAREANY; break;
784 }
785 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200786 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100787 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
788 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
789 isntype = ISN_COMPAREANY;
790
791 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
792 && (isntype == ISN_COMPAREBOOL
793 || isntype == ISN_COMPARESPECIAL
794 || isntype == ISN_COMPARENR
795 || isntype == ISN_COMPAREFLOAT))
796 {
797 semsg(_("E1037: Cannot use \"%s\" with %s"),
798 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
799 return FAIL;
800 }
801 if (isntype == ISN_DROP
802 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
803 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
804 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
805 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
806 && exptype != EXPR_IS && exptype != EXPR_ISNOT
807 && (type1 == VAR_BLOB || type2 == VAR_BLOB
808 || type1 == VAR_LIST || type2 == VAR_LIST))))
809 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100810 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 vartype_name(type1), vartype_name(type2));
812 return FAIL;
813 }
814
815 if ((isn = generate_instr(cctx, isntype)) == NULL)
816 return FAIL;
817 isn->isn_arg.op.op_type = exptype;
818 isn->isn_arg.op.op_ic = ic;
819
820 // takes two arguments, puts one bool back
821 if (stack->ga_len >= 2)
822 {
823 --stack->ga_len;
824 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
825 }
826
827 return OK;
828}
829
830/*
831 * Generate an ISN_2BOOL instruction.
832 */
833 static int
834generate_2BOOL(cctx_T *cctx, int invert)
835{
836 isn_T *isn;
837 garray_T *stack = &cctx->ctx_type_stack;
838
Bram Moolenaar080457c2020-03-03 21:53:32 +0100839 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
841 return FAIL;
842 isn->isn_arg.number = invert;
843
844 // type becomes bool
845 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
846
847 return OK;
848}
849
850 static int
851generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
852{
853 isn_T *isn;
854 garray_T *stack = &cctx->ctx_type_stack;
855
Bram Moolenaar080457c2020-03-03 21:53:32 +0100856 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
858 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200859 // TODO: whole type, e.g. for a function also arg and return types
860 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861 isn->isn_arg.type.ct_off = offset;
862
863 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200864 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865
866 return OK;
867}
868
869/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200870 * Check that
871 * - "actual" is "expected" type or
872 * - "actual" is a type that can be "expected" type: add a runtime check; or
873 * - return FAIL.
874 */
875 static int
876need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
877{
878 if (check_type(expected, actual, FALSE) == OK)
879 return OK;
880 if (actual->tt_type != VAR_ANY
881 && actual->tt_type != VAR_UNKNOWN
882 && !(actual->tt_type == VAR_FUNC
883 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
884 {
885 type_mismatch(expected, actual);
886 return FAIL;
887 }
888 generate_TYPECHECK(cctx, expected, offset);
889 return OK;
890}
891
892/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893 * Generate an ISN_PUSHNR instruction.
894 */
895 static int
896generate_PUSHNR(cctx_T *cctx, varnumber_T number)
897{
898 isn_T *isn;
899
Bram Moolenaar080457c2020-03-03 21:53:32 +0100900 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
902 return FAIL;
903 isn->isn_arg.number = number;
904
905 return OK;
906}
907
908/*
909 * Generate an ISN_PUSHBOOL instruction.
910 */
911 static int
912generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
913{
914 isn_T *isn;
915
Bram Moolenaar080457c2020-03-03 21:53:32 +0100916 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
918 return FAIL;
919 isn->isn_arg.number = number;
920
921 return OK;
922}
923
924/*
925 * Generate an ISN_PUSHSPEC instruction.
926 */
927 static int
928generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
929{
930 isn_T *isn;
931
Bram Moolenaar080457c2020-03-03 21:53:32 +0100932 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
934 return FAIL;
935 isn->isn_arg.number = number;
936
937 return OK;
938}
939
940#ifdef FEAT_FLOAT
941/*
942 * Generate an ISN_PUSHF instruction.
943 */
944 static int
945generate_PUSHF(cctx_T *cctx, float_T fnumber)
946{
947 isn_T *isn;
948
Bram Moolenaar080457c2020-03-03 21:53:32 +0100949 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
951 return FAIL;
952 isn->isn_arg.fnumber = fnumber;
953
954 return OK;
955}
956#endif
957
958/*
959 * Generate an ISN_PUSHS instruction.
960 * Consumes "str".
961 */
962 static int
963generate_PUSHS(cctx_T *cctx, char_u *str)
964{
965 isn_T *isn;
966
Bram Moolenaar080457c2020-03-03 21:53:32 +0100967 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
969 return FAIL;
970 isn->isn_arg.string = str;
971
972 return OK;
973}
974
975/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100976 * Generate an ISN_PUSHCHANNEL instruction.
977 * Consumes "channel".
978 */
979 static int
980generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
981{
982 isn_T *isn;
983
Bram Moolenaar080457c2020-03-03 21:53:32 +0100984 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100985 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
986 return FAIL;
987 isn->isn_arg.channel = channel;
988
989 return OK;
990}
991
992/*
993 * Generate an ISN_PUSHJOB instruction.
994 * Consumes "job".
995 */
996 static int
997generate_PUSHJOB(cctx_T *cctx, job_T *job)
998{
999 isn_T *isn;
1000
Bram Moolenaar080457c2020-03-03 21:53:32 +01001001 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001002 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001003 return FAIL;
1004 isn->isn_arg.job = job;
1005
1006 return OK;
1007}
1008
1009/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010 * Generate an ISN_PUSHBLOB instruction.
1011 * Consumes "blob".
1012 */
1013 static int
1014generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1015{
1016 isn_T *isn;
1017
Bram Moolenaar080457c2020-03-03 21:53:32 +01001018 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1020 return FAIL;
1021 isn->isn_arg.blob = blob;
1022
1023 return OK;
1024}
1025
1026/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001027 * Generate an ISN_PUSHFUNC instruction with name "name".
1028 * Consumes "name".
1029 */
1030 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001031generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001032{
1033 isn_T *isn;
1034
Bram Moolenaar080457c2020-03-03 21:53:32 +01001035 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001036 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001037 return FAIL;
1038 isn->isn_arg.string = name;
1039
1040 return OK;
1041}
1042
1043/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 * Generate an ISN_STORE instruction.
1045 */
1046 static int
1047generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1048{
1049 isn_T *isn;
1050
Bram Moolenaar080457c2020-03-03 21:53:32 +01001051 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1053 return FAIL;
1054 if (name != NULL)
1055 isn->isn_arg.string = vim_strsave(name);
1056 else
1057 isn->isn_arg.number = idx;
1058
1059 return OK;
1060}
1061
1062/*
1063 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1064 */
1065 static int
1066generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1067{
1068 isn_T *isn;
1069
Bram Moolenaar080457c2020-03-03 21:53:32 +01001070 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001071 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1072 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001073 isn->isn_arg.storenr.stnr_idx = idx;
1074 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075
1076 return OK;
1077}
1078
1079/*
1080 * Generate an ISN_STOREOPT instruction
1081 */
1082 static int
1083generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1084{
1085 isn_T *isn;
1086
Bram Moolenaar080457c2020-03-03 21:53:32 +01001087 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1089 return FAIL;
1090 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1091 isn->isn_arg.storeopt.so_flags = opt_flags;
1092
1093 return OK;
1094}
1095
1096/*
1097 * Generate an ISN_LOAD or similar instruction.
1098 */
1099 static int
1100generate_LOAD(
1101 cctx_T *cctx,
1102 isntype_T isn_type,
1103 int idx,
1104 char_u *name,
1105 type_T *type)
1106{
1107 isn_T *isn;
1108
Bram Moolenaar080457c2020-03-03 21:53:32 +01001109 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1111 return FAIL;
1112 if (name != NULL)
1113 isn->isn_arg.string = vim_strsave(name);
1114 else
1115 isn->isn_arg.number = idx;
1116
1117 return OK;
1118}
1119
1120/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001121 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001122 */
1123 static int
1124generate_LOADV(
1125 cctx_T *cctx,
1126 char_u *name,
1127 int error)
1128{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001129 int di_flags;
1130 int vidx = find_vim_var(name, &di_flags);
1131 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001132
Bram Moolenaar080457c2020-03-03 21:53:32 +01001133 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001134 if (vidx < 0)
1135 {
1136 if (error)
1137 semsg(_(e_var_notfound), name);
1138 return FAIL;
1139 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001140 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001141
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001142 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001143}
1144
1145/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001146 * Generate an ISN_UNLET instruction.
1147 */
1148 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001149generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001150{
1151 isn_T *isn;
1152
1153 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001154 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001155 return FAIL;
1156 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1157 isn->isn_arg.unlet.ul_forceit = forceit;
1158
1159 return OK;
1160}
1161
1162/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163 * Generate an ISN_LOADS instruction.
1164 */
1165 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001166generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001168 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001169 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001170 int sid,
1171 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172{
1173 isn_T *isn;
1174
Bram Moolenaar080457c2020-03-03 21:53:32 +01001175 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001176 if (isn_type == ISN_LOADS)
1177 isn = generate_instr_type(cctx, isn_type, type);
1178 else
1179 isn = generate_instr_drop(cctx, isn_type, 1);
1180 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001182 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1183 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184
1185 return OK;
1186}
1187
1188/*
1189 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1190 */
1191 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001192generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193 cctx_T *cctx,
1194 isntype_T isn_type,
1195 int sid,
1196 int idx,
1197 type_T *type)
1198{
1199 isn_T *isn;
1200
Bram Moolenaar080457c2020-03-03 21:53:32 +01001201 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 if (isn_type == ISN_LOADSCRIPT)
1203 isn = generate_instr_type(cctx, isn_type, type);
1204 else
1205 isn = generate_instr_drop(cctx, isn_type, 1);
1206 if (isn == NULL)
1207 return FAIL;
1208 isn->isn_arg.script.script_sid = sid;
1209 isn->isn_arg.script.script_idx = idx;
1210 return OK;
1211}
1212
1213/*
1214 * Generate an ISN_NEWLIST instruction.
1215 */
1216 static int
1217generate_NEWLIST(cctx_T *cctx, int count)
1218{
1219 isn_T *isn;
1220 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221 type_T *type;
1222 type_T *member;
1223
Bram Moolenaar080457c2020-03-03 21:53:32 +01001224 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1226 return FAIL;
1227 isn->isn_arg.number = count;
1228
1229 // drop the value types
1230 stack->ga_len -= count;
1231
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001232 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001233 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234 if (count > 0)
1235 member = ((type_T **)stack->ga_data)[stack->ga_len];
1236 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001237 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001238 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239
1240 // add the list type to the type stack
1241 if (ga_grow(stack, 1) == FAIL)
1242 return FAIL;
1243 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1244 ++stack->ga_len;
1245
1246 return OK;
1247}
1248
1249/*
1250 * Generate an ISN_NEWDICT instruction.
1251 */
1252 static int
1253generate_NEWDICT(cctx_T *cctx, int count)
1254{
1255 isn_T *isn;
1256 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 type_T *type;
1258 type_T *member;
1259
Bram Moolenaar080457c2020-03-03 21:53:32 +01001260 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1262 return FAIL;
1263 isn->isn_arg.number = count;
1264
1265 // drop the key and value types
1266 stack->ga_len -= 2 * count;
1267
Bram Moolenaar436472f2020-02-20 22:54:43 +01001268 // Use the first value type for the list member type. Use "void" for an
1269 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 if (count > 0)
1271 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1272 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001273 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001274 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275
1276 // add the dict type to the type stack
1277 if (ga_grow(stack, 1) == FAIL)
1278 return FAIL;
1279 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1280 ++stack->ga_len;
1281
1282 return OK;
1283}
1284
1285/*
1286 * Generate an ISN_FUNCREF instruction.
1287 */
1288 static int
1289generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1290{
1291 isn_T *isn;
1292 garray_T *stack = &cctx->ctx_type_stack;
1293
Bram Moolenaar080457c2020-03-03 21:53:32 +01001294 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1296 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001297 isn->isn_arg.funcref.fr_func = dfunc_idx;
1298 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299
1300 if (ga_grow(stack, 1) == FAIL)
1301 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001302 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 // TODO: argument and return types
1304 ++stack->ga_len;
1305
1306 return OK;
1307}
1308
1309/*
1310 * Generate an ISN_JUMP instruction.
1311 */
1312 static int
1313generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1314{
1315 isn_T *isn;
1316 garray_T *stack = &cctx->ctx_type_stack;
1317
Bram Moolenaar080457c2020-03-03 21:53:32 +01001318 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1320 return FAIL;
1321 isn->isn_arg.jump.jump_when = when;
1322 isn->isn_arg.jump.jump_where = where;
1323
1324 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1325 --stack->ga_len;
1326
1327 return OK;
1328}
1329
1330 static int
1331generate_FOR(cctx_T *cctx, int loop_idx)
1332{
1333 isn_T *isn;
1334 garray_T *stack = &cctx->ctx_type_stack;
1335
Bram Moolenaar080457c2020-03-03 21:53:32 +01001336 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1338 return FAIL;
1339 isn->isn_arg.forloop.for_idx = loop_idx;
1340
1341 if (ga_grow(stack, 1) == FAIL)
1342 return FAIL;
1343 // type doesn't matter, will be stored next
1344 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1345 ++stack->ga_len;
1346
1347 return OK;
1348}
1349
1350/*
1351 * Generate an ISN_BCALL instruction.
1352 * Return FAIL if the number of arguments is wrong.
1353 */
1354 static int
1355generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1356{
1357 isn_T *isn;
1358 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001359 type_T *argtypes[MAX_FUNC_ARGS];
1360 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361
Bram Moolenaar080457c2020-03-03 21:53:32 +01001362 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 if (check_internal_func(func_idx, argcount) == FAIL)
1364 return FAIL;
1365
1366 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1367 return FAIL;
1368 isn->isn_arg.bfunc.cbf_idx = func_idx;
1369 isn->isn_arg.bfunc.cbf_argcount = argcount;
1370
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001371 for (i = 0; i < argcount; ++i)
1372 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 stack->ga_len -= argcount; // drop the arguments
1375 if (ga_grow(stack, 1) == FAIL)
1376 return FAIL;
1377 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001378 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 ++stack->ga_len; // add return value
1380
1381 return OK;
1382}
1383
1384/*
1385 * Generate an ISN_DCALL or ISN_UCALL instruction.
1386 * Return FAIL if the number of arguments is wrong.
1387 */
1388 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001389generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390{
1391 isn_T *isn;
1392 garray_T *stack = &cctx->ctx_type_stack;
1393 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001394 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395
Bram Moolenaar080457c2020-03-03 21:53:32 +01001396 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 if (argcount > regular_args && !has_varargs(ufunc))
1398 {
1399 semsg(_(e_toomanyarg), ufunc->uf_name);
1400 return FAIL;
1401 }
1402 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1403 {
1404 semsg(_(e_toofewarg), ufunc->uf_name);
1405 return FAIL;
1406 }
1407
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001408 if (ufunc->uf_dfunc_idx >= 0)
1409 {
1410 int i;
1411
1412 for (i = 0; i < argcount; ++i)
1413 {
1414 type_T *expected;
1415 type_T *actual;
1416
1417 if (i < regular_args)
1418 {
1419 if (ufunc->uf_arg_types == NULL)
1420 continue;
1421 expected = ufunc->uf_arg_types[i];
1422 }
1423 else
1424 expected = ufunc->uf_va_type->tt_member;
1425 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001426 if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001427 {
1428 arg_type_mismatch(expected, actual, i + 1);
1429 return FAIL;
1430 }
1431 }
1432 }
1433
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 if ((isn = generate_instr(cctx,
1435 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1436 return FAIL;
1437 if (ufunc->uf_dfunc_idx >= 0)
1438 {
1439 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1440 isn->isn_arg.dfunc.cdf_argcount = argcount;
1441 }
1442 else
1443 {
1444 // A user function may be deleted and redefined later, can't use the
1445 // ufunc pointer, need to look it up again at runtime.
1446 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1447 isn->isn_arg.ufunc.cuf_argcount = argcount;
1448 }
1449
1450 stack->ga_len -= argcount; // drop the arguments
1451 if (ga_grow(stack, 1) == FAIL)
1452 return FAIL;
1453 // add return value
1454 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1455 ++stack->ga_len;
1456
1457 return OK;
1458}
1459
1460/*
1461 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1462 */
1463 static int
1464generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1465{
1466 isn_T *isn;
1467 garray_T *stack = &cctx->ctx_type_stack;
1468
Bram Moolenaar080457c2020-03-03 21:53:32 +01001469 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1471 return FAIL;
1472 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1473 isn->isn_arg.ufunc.cuf_argcount = argcount;
1474
1475 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001476 if (ga_grow(stack, 1) == FAIL)
1477 return FAIL;
1478 // add return value
1479 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1480 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481
1482 return OK;
1483}
1484
1485/*
1486 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001487 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 */
1489 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001490generate_PCALL(
1491 cctx_T *cctx,
1492 int argcount,
1493 char_u *name,
1494 type_T *type,
1495 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496{
1497 isn_T *isn;
1498 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001499 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500
Bram Moolenaar080457c2020-03-03 21:53:32 +01001501 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001502
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001503 if (type->tt_type == VAR_ANY)
1504 ret_type = &t_any;
1505 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1506 ret_type = type->tt_member;
1507 else
1508 {
1509 semsg(_("E1085: Not a callable type: %s"), name);
1510 return FAIL;
1511 }
1512
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1514 return FAIL;
1515 isn->isn_arg.pfunc.cpf_top = at_top;
1516 isn->isn_arg.pfunc.cpf_argcount = argcount;
1517
1518 stack->ga_len -= argcount; // drop the arguments
1519
1520 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001521 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001523 // If partial is above the arguments it must be cleared and replaced with
1524 // the return value.
1525 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1526 return FAIL;
1527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 return OK;
1529}
1530
1531/*
1532 * Generate an ISN_MEMBER instruction.
1533 */
1534 static int
1535generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1536{
1537 isn_T *isn;
1538 garray_T *stack = &cctx->ctx_type_stack;
1539 type_T *type;
1540
Bram Moolenaar080457c2020-03-03 21:53:32 +01001541 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1543 return FAIL;
1544 isn->isn_arg.string = vim_strnsave(name, (int)len);
1545
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001546 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001548 if (type->tt_type != VAR_DICT && type != &t_any)
1549 {
1550 emsg(_(e_dictreq));
1551 return FAIL;
1552 }
1553 // change dict type to dict member type
1554 if (type->tt_type == VAR_DICT)
1555 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556
1557 return OK;
1558}
1559
1560/*
1561 * Generate an ISN_ECHO instruction.
1562 */
1563 static int
1564generate_ECHO(cctx_T *cctx, int with_white, int count)
1565{
1566 isn_T *isn;
1567
Bram Moolenaar080457c2020-03-03 21:53:32 +01001568 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1570 return FAIL;
1571 isn->isn_arg.echo.echo_with_white = with_white;
1572 isn->isn_arg.echo.echo_count = count;
1573
1574 return OK;
1575}
1576
Bram Moolenaarad39c092020-02-26 18:23:43 +01001577/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001578 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001579 */
1580 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001581generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001582{
1583 isn_T *isn;
1584
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001585 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001586 return FAIL;
1587 isn->isn_arg.number = count;
1588
1589 return OK;
1590}
1591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 static int
1593generate_EXEC(cctx_T *cctx, char_u *line)
1594{
1595 isn_T *isn;
1596
Bram Moolenaar080457c2020-03-03 21:53:32 +01001597 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1599 return FAIL;
1600 isn->isn_arg.string = vim_strsave(line);
1601 return OK;
1602}
1603
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001604 static int
1605generate_EXECCONCAT(cctx_T *cctx, int count)
1606{
1607 isn_T *isn;
1608
1609 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1610 return FAIL;
1611 isn->isn_arg.number = count;
1612 return OK;
1613}
1614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615/*
1616 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001617 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001619 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001620reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1621{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 lvar_T *lvar;
1623
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001624 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625 {
1626 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001627 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 }
1629
1630 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001631 return NULL;
1632 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001634 // Every local variable uses the next entry on the stack. We could re-use
1635 // the last ones when leaving a scope, but then variables used in a closure
1636 // might get overwritten. To keep things simple do not re-use stack
1637 // entries. This is less efficient, but memory is cheap these days.
1638 lvar->lv_idx = cctx->ctx_locals_count++;
1639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1641 lvar->lv_const = isConst;
1642 lvar->lv_type = type;
1643
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001644 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645}
1646
1647/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001648 * Remove local variables above "new_top".
1649 */
1650 static void
1651unwind_locals(cctx_T *cctx, int new_top)
1652{
1653 if (cctx->ctx_locals.ga_len > new_top)
1654 {
1655 int idx;
1656 lvar_T *lvar;
1657
1658 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1659 {
1660 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1661 vim_free(lvar->lv_name);
1662 }
1663 }
1664 cctx->ctx_locals.ga_len = new_top;
1665}
1666
1667/*
1668 * Free all local variables.
1669 */
1670 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001671free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001672{
1673 unwind_locals(cctx, 0);
1674 ga_clear(&cctx->ctx_locals);
1675}
1676
1677/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678 * Skip over a type definition and return a pointer to just after it.
1679 */
1680 char_u *
1681skip_type(char_u *start)
1682{
1683 char_u *p = start;
1684
1685 while (ASCII_ISALNUM(*p) || *p == '_')
1686 ++p;
1687
1688 // Skip over "<type>"; this is permissive about white space.
1689 if (*skipwhite(p) == '<')
1690 {
1691 p = skipwhite(p);
1692 p = skip_type(skipwhite(p + 1));
1693 p = skipwhite(p);
1694 if (*p == '>')
1695 ++p;
1696 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001697 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1698 {
1699 // handle func(args): type
1700 ++p;
1701 while (*p != ')' && *p != NUL)
1702 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001703 char_u *sp = p;
1704
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001705 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001706 if (p == sp)
1707 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001708 if (*p == ',')
1709 p = skipwhite(p + 1);
1710 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001711 if (*p == ')')
1712 {
1713 if (p[1] == ':')
1714 p = skip_type(skipwhite(p + 2));
1715 else
1716 p = skipwhite(p + 1);
1717 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001718 }
1719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 return p;
1721}
1722
1723/*
1724 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001725 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726 * Returns NULL in case of failure.
1727 */
1728 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001729parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730{
1731 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001732 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733
1734 if (**arg != '<')
1735 {
1736 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001737 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 else
1739 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001740 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 }
1742 *arg = skipwhite(*arg + 1);
1743
Bram Moolenaard77a8522020-04-03 21:59:57 +02001744 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745
1746 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001747 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 {
1749 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001750 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 }
1752 ++*arg;
1753
1754 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001755 return get_list_type(member_type, type_gap);
1756 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757}
1758
1759/*
1760 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001761 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 */
1763 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001764parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001765{
1766 char_u *p = *arg;
1767 size_t len;
1768
1769 // skip over the first word
1770 while (ASCII_ISALNUM(*p) || *p == '_')
1771 ++p;
1772 len = p - *arg;
1773
1774 switch (**arg)
1775 {
1776 case 'a':
1777 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1778 {
1779 *arg += len;
1780 return &t_any;
1781 }
1782 break;
1783 case 'b':
1784 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1785 {
1786 *arg += len;
1787 return &t_bool;
1788 }
1789 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1790 {
1791 *arg += len;
1792 return &t_blob;
1793 }
1794 break;
1795 case 'c':
1796 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1797 {
1798 *arg += len;
1799 return &t_channel;
1800 }
1801 break;
1802 case 'd':
1803 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1804 {
1805 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001806 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 }
1808 break;
1809 case 'f':
1810 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1811 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001812#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813 *arg += len;
1814 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001815#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001816 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001817 return &t_any;
1818#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 }
1820 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1821 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001822 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001823 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001824 int argcount = -1;
1825 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001826 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001827 type_T *arg_type[MAX_FUNC_ARGS + 1];
1828
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001829 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001831 if (**arg == '(')
1832 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001833 // "func" may or may not return a value, "func()" does
1834 // not return a value.
1835 ret_type = &t_void;
1836
Bram Moolenaard77a8522020-04-03 21:59:57 +02001837 p = ++*arg;
1838 argcount = 0;
1839 while (*p != NUL && *p != ')')
1840 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001841 if (*p == '?')
1842 {
1843 if (first_optional == -1)
1844 first_optional = argcount;
1845 ++p;
1846 }
1847 else if (first_optional != -1)
1848 {
1849 emsg(_("E1007: mandatory argument after optional argument"));
1850 return &t_any;
1851 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001852 else if (STRNCMP(p, "...", 3) == 0)
1853 {
1854 flags |= TTFLAG_VARARGS;
1855 p += 3;
1856 }
1857
1858 arg_type[argcount++] = parse_type(&p, type_gap);
1859
1860 // Nothing comes after "...{type}".
1861 if (flags & TTFLAG_VARARGS)
1862 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001863
Bram Moolenaard77a8522020-04-03 21:59:57 +02001864 if (*p != ',' && *skipwhite(p) == ',')
1865 {
1866 semsg(_(e_no_white_before), ",");
1867 return &t_any;
1868 }
1869 if (*p == ',')
1870 {
1871 ++p;
1872 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001873 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001874 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001875 return &t_any;
1876 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001877 }
1878 p = skipwhite(p);
1879 if (argcount == MAX_FUNC_ARGS)
1880 {
1881 emsg(_("E740: Too many argument types"));
1882 return &t_any;
1883 }
1884 }
1885
1886 p = skipwhite(p);
1887 if (*p != ')')
1888 {
1889 emsg(_(e_missing_close));
1890 return &t_any;
1891 }
1892 *arg = p + 1;
1893 }
1894 if (**arg == ':')
1895 {
1896 // parse return type
1897 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001898 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001899 semsg(_(e_white_after), ":");
1900 *arg = skipwhite(*arg);
1901 ret_type = parse_type(arg, type_gap);
1902 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001903 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001904 type = get_func_type(ret_type, argcount, type_gap);
1905 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001906 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001907 type = alloc_func_type(ret_type, argcount, type_gap);
1908 type->tt_flags = flags;
1909 if (argcount > 0)
1910 {
1911 type->tt_argcount = argcount;
1912 type->tt_min_argcount = first_optional == -1
1913 ? argcount : first_optional;
1914 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001915 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001916 return &t_any;
1917 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001918 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001919 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001920 }
1921 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 }
1923 break;
1924 case 'j':
1925 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1926 {
1927 *arg += len;
1928 return &t_job;
1929 }
1930 break;
1931 case 'l':
1932 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1933 {
1934 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001935 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001936 }
1937 break;
1938 case 'n':
1939 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1940 {
1941 *arg += len;
1942 return &t_number;
1943 }
1944 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 case 's':
1946 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1947 {
1948 *arg += len;
1949 return &t_string;
1950 }
1951 break;
1952 case 'v':
1953 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1954 {
1955 *arg += len;
1956 return &t_void;
1957 }
1958 break;
1959 }
1960
1961 semsg(_("E1010: Type not recognized: %s"), *arg);
1962 return &t_any;
1963}
1964
1965/*
1966 * Check if "type1" and "type2" are exactly the same.
1967 */
1968 static int
1969equal_type(type_T *type1, type_T *type2)
1970{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001971 int i;
1972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973 if (type1->tt_type != type2->tt_type)
1974 return FALSE;
1975 switch (type1->tt_type)
1976 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001978 case VAR_ANY:
1979 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 case VAR_SPECIAL:
1981 case VAR_BOOL:
1982 case VAR_NUMBER:
1983 case VAR_FLOAT:
1984 case VAR_STRING:
1985 case VAR_BLOB:
1986 case VAR_JOB:
1987 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001988 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 case VAR_LIST:
1990 case VAR_DICT:
1991 return equal_type(type1->tt_member, type2->tt_member);
1992 case VAR_FUNC:
1993 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001994 if (!equal_type(type1->tt_member, type2->tt_member)
1995 || type1->tt_argcount != type2->tt_argcount)
1996 return FALSE;
1997 if (type1->tt_argcount < 0
1998 || type1->tt_args == NULL || type2->tt_args == NULL)
1999 return TRUE;
2000 for (i = 0; i < type1->tt_argcount; ++i)
2001 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2002 return FALSE;
2003 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 }
2005 return TRUE;
2006}
2007
2008/*
2009 * Find the common type of "type1" and "type2" and put it in "dest".
2010 * "type2" and "dest" may be the same.
2011 */
2012 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002013common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014{
2015 if (equal_type(type1, type2))
2016 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002017 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002018 return;
2019 }
2020
2021 if (type1->tt_type == type2->tt_type)
2022 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2024 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002025 type_T *common;
2026
Bram Moolenaard77a8522020-04-03 21:59:57 +02002027 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002028 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002029 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002030 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002031 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 return;
2033 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002034 if (type1->tt_type == VAR_FUNC)
2035 {
2036 type_T *common;
2037
2038 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2039 if (type1->tt_argcount == type2->tt_argcount
2040 && type1->tt_argcount >= 0)
2041 {
2042 int argcount = type1->tt_argcount;
2043 int i;
2044
2045 *dest = alloc_func_type(common, argcount, type_gap);
2046 if (type1->tt_args != NULL && type2->tt_args != NULL)
2047 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002048 if (func_type_add_arg_types(*dest, argcount,
2049 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002050 for (i = 0; i < argcount; ++i)
2051 common_type(type1->tt_args[i], type2->tt_args[i],
2052 &(*dest)->tt_args[i], type_gap);
2053 }
2054 }
2055 else
2056 *dest = alloc_func_type(common, -1, type_gap);
2057 return;
2058 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059 }
2060
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002061 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062}
2063
2064 char *
2065vartype_name(vartype_T type)
2066{
2067 switch (type)
2068 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002069 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002070 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 case VAR_SPECIAL: return "special";
2073 case VAR_BOOL: return "bool";
2074 case VAR_NUMBER: return "number";
2075 case VAR_FLOAT: return "float";
2076 case VAR_STRING: return "string";
2077 case VAR_BLOB: return "blob";
2078 case VAR_JOB: return "job";
2079 case VAR_CHANNEL: return "channel";
2080 case VAR_LIST: return "list";
2081 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002082
2083 case VAR_FUNC:
2084 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002086 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087}
2088
2089/*
2090 * Return the name of a type.
2091 * The result may be in allocated memory, in which case "tofree" is set.
2092 */
2093 char *
2094type_name(type_T *type, char **tofree)
2095{
2096 char *name = vartype_name(type->tt_type);
2097
2098 *tofree = NULL;
2099 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2100 {
2101 char *member_free;
2102 char *member_name = type_name(type->tt_member, &member_free);
2103 size_t len;
2104
2105 len = STRLEN(name) + STRLEN(member_name) + 3;
2106 *tofree = alloc(len);
2107 if (*tofree != NULL)
2108 {
2109 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2110 vim_free(member_free);
2111 return *tofree;
2112 }
2113 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002114 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002115 {
2116 garray_T ga;
2117 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002118 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002119
2120 ga_init2(&ga, 1, 100);
2121 if (ga_grow(&ga, 20) == FAIL)
2122 return "[unknown]";
2123 *tofree = ga.ga_data;
2124 STRCPY(ga.ga_data, "func(");
2125 ga.ga_len += 5;
2126
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002127 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002128 {
2129 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002130 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002131 int len;
2132
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002133 if (type->tt_args == NULL)
2134 arg_type = "[unknown]";
2135 else
2136 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002137 if (i > 0)
2138 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002139 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002140 ga.ga_len += 2;
2141 }
2142 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002143 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002144 {
2145 vim_free(arg_free);
2146 return "[unknown]";
2147 }
2148 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002149 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002150 {
2151 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2152 ga.ga_len += 3;
2153 }
2154 else if (i >= type->tt_min_argcount)
2155 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002156 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002157 ga.ga_len += len;
2158 vim_free(arg_free);
2159 }
2160
2161 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002162 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002163 else
2164 {
2165 char *ret_free;
2166 char *ret_name = type_name(type->tt_member, &ret_free);
2167 int len;
2168
2169 len = (int)STRLEN(ret_name) + 4;
2170 if (ga_grow(&ga, len) == FAIL)
2171 {
2172 vim_free(ret_free);
2173 return "[unknown]";
2174 }
2175 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002176 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2177 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002178 vim_free(ret_free);
2179 }
2180 return ga.ga_data;
2181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182
2183 return name;
2184}
2185
2186/*
2187 * Find "name" in script-local items of script "sid".
2188 * Returns the index in "sn_var_vals" if found.
2189 * If found but not in "sn_var_vals" returns -1.
2190 * If not found returns -2.
2191 */
2192 int
2193get_script_item_idx(int sid, char_u *name, int check_writable)
2194{
2195 hashtab_T *ht;
2196 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002197 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198 int idx;
2199
2200 // First look the name up in the hashtable.
2201 if (sid <= 0 || sid > script_items.ga_len)
2202 return -1;
2203 ht = &SCRIPT_VARS(sid);
2204 di = find_var_in_ht(ht, 0, name, TRUE);
2205 if (di == NULL)
2206 return -2;
2207
2208 // Now find the svar_T index in sn_var_vals.
2209 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2210 {
2211 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2212
2213 if (sv->sv_tv == &di->di_tv)
2214 {
2215 if (check_writable && sv->sv_const)
2216 semsg(_(e_readonlyvar), name);
2217 return idx;
2218 }
2219 }
2220 return -1;
2221}
2222
2223/*
2224 * Find "name" in imported items of the current script/
2225 */
2226 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002227find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002229 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 int idx;
2231
2232 if (cctx != NULL)
2233 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2234 {
2235 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2236 + idx;
2237
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002238 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2239 : STRLEN(import->imp_name) == len
2240 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 return import;
2242 }
2243
2244 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2245 {
2246 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2247
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002248 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2249 : STRLEN(import->imp_name) == len
2250 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251 return import;
2252 }
2253 return NULL;
2254}
2255
2256/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002257 * Free all imported variables.
2258 */
2259 static void
2260free_imported(cctx_T *cctx)
2261{
2262 int idx;
2263
2264 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2265 {
2266 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2267
2268 vim_free(import->imp_name);
2269 }
2270 ga_clear(&cctx->ctx_imports);
2271}
2272
2273/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002274 * Get the next line of the function from "cctx".
2275 * Returns NULL when at the end.
2276 */
2277 static char_u *
2278next_line_from_context(cctx_T *cctx)
2279{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002280 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002281
2282 do
2283 {
2284 ++cctx->ctx_lnum;
2285 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002286 {
2287 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002288 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002289 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002290 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002291 cctx->ctx_line_start = line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002292 SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum
2293 + cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002294 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002295 return line;
2296}
2297
2298/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002299 * Return TRUE if "p" points at a "#" but not at "#{".
2300 */
2301 static int
2302comment_start(char_u *p)
2303{
2304 return p[0] == '#' && p[1] != '{';
2305}
2306
2307/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002308 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002309 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002310 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2311 */
2312 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002313may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002314{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002315 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002316 {
2317 char_u *next = next_line_from_context(cctx);
2318
2319 if (next == NULL)
2320 return FAIL;
2321 *arg = skipwhite(next);
2322 }
2323 return OK;
2324}
2325
2326/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002327 * Generate an instruction to load script-local variable "name", without the
2328 * leading "s:".
2329 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 */
2331 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002332compile_load_scriptvar(
2333 cctx_T *cctx,
2334 char_u *name, // variable NUL terminated
2335 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002336 char_u **end, // end of variable
2337 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002338{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002339 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2341 imported_T *import;
2342
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002343 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002345 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002346 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2347 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348 }
2349 if (idx >= 0)
2350 {
2351 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2352
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002353 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 current_sctx.sc_sid, idx, sv->sv_type);
2355 return OK;
2356 }
2357
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002358 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 if (import != NULL)
2360 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002361 if (import->imp_all)
2362 {
2363 char_u *p = skipwhite(*end);
2364 int name_len;
2365 ufunc_T *ufunc;
2366 type_T *type;
2367
2368 // Used "import * as Name", need to lookup the member.
2369 if (*p != '.')
2370 {
2371 semsg(_("E1060: expected dot after name: %s"), start);
2372 return FAIL;
2373 }
2374 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002375 if (VIM_ISWHITE(*p))
2376 {
2377 emsg(_("E1074: no white space allowed after dot"));
2378 return FAIL;
2379 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002380
2381 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2382 // TODO: what if it is a function?
2383 if (idx < 0)
2384 return FAIL;
2385 *end = p;
2386
2387 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2388 import->imp_sid,
2389 idx,
2390 type);
2391 }
2392 else
2393 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002394 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002395 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2396 import->imp_sid,
2397 import->imp_var_vals_idx,
2398 import->imp_type);
2399 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400 return OK;
2401 }
2402
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002403 if (error)
2404 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 return FAIL;
2406}
2407
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002408 static int
2409generate_funcref(cctx_T *cctx, char_u *name)
2410{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002411 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002412
2413 if (ufunc == NULL)
2414 return FAIL;
2415
2416 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2417}
2418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419/*
2420 * Compile a variable name into a load instruction.
2421 * "end" points to just after the name.
2422 * When "error" is FALSE do not give an error when not found.
2423 */
2424 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002425compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002426{
2427 type_T *type;
2428 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002429 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002431 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432
2433 if (*(*arg + 1) == ':')
2434 {
2435 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002436 if (end <= *arg + 2)
2437 name = vim_strsave((char_u *)"[empty]");
2438 else
2439 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 if (name == NULL)
2441 return FAIL;
2442
2443 if (**arg == 'v')
2444 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002445 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446 }
2447 else if (**arg == 'g')
2448 {
2449 // Global variables can be defined later, thus we don't check if it
2450 // exists, give error at runtime.
2451 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2452 }
2453 else if (**arg == 's')
2454 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002455 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002457 else if (**arg == 'b')
2458 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002459 // Buffer-local variables can be defined later, thus we don't check
2460 // if it exists, give error at runtime.
2461 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002462 }
2463 else if (**arg == 'w')
2464 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002465 // Window-local variables can be defined later, thus we don't check
2466 // if it exists, give error at runtime.
2467 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002468 }
2469 else if (**arg == 't')
2470 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002471 // Tabpage-local variables can be defined later, thus we don't
2472 // check if it exists, give error at runtime.
2473 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002474 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 else
2476 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002477 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478 goto theend;
2479 }
2480 }
2481 else
2482 {
2483 size_t len = end - *arg;
2484 int idx;
2485 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002486 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487
2488 name = vim_strnsave(*arg, end - *arg);
2489 if (name == NULL)
2490 return FAIL;
2491
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002492 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002494 if (!gen_load_outer)
2495 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 }
2497 else
2498 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002499 lvar_T *lvar = lookup_local(*arg, len, cctx);
2500
2501 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002503 type = lvar->lv_type;
2504 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002505 if (lvar->lv_from_outer)
2506 gen_load_outer = TRUE;
2507 else
2508 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 }
2510 else
2511 {
2512 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2513 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2514 res = generate_PUSHBOOL(cctx, **arg == 't'
2515 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002516 else
2517 {
2518 // "var" can be script-local even without using "s:" if it
2519 // already exists.
2520 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2521 == SCRIPT_VERSION_VIM9
2522 || lookup_script(*arg, len) == OK)
2523 res = compile_load_scriptvar(cctx, name, *arg, &end,
2524 FALSE);
2525
2526 // When the name starts with an uppercase letter or "x:" it
2527 // can be a user defined function.
2528 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2529 res = generate_funcref(cctx, name);
2530 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 }
2532 }
2533 if (gen_load)
2534 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002535 if (gen_load_outer)
2536 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 }
2538
2539 *arg = end;
2540
2541theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002542 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 semsg(_(e_var_notfound), name);
2544 vim_free(name);
2545 return res;
2546}
2547
2548/*
2549 * Compile the argument expressions.
2550 * "arg" points to just after the "(" and is advanced to after the ")"
2551 */
2552 static int
2553compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2554{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002555 char_u *p = *arg;
2556 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557
Bram Moolenaare6085c52020-04-12 20:19:16 +02002558 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002560 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002561 {
2562 p = next_line_from_context(cctx);
2563 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002564 goto failret;
2565 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002566 p = skipwhite(p);
2567 }
2568 if (*p == ')')
2569 {
2570 *arg = p + 1;
2571 return OK;
2572 }
2573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 if (compile_expr1(&p, cctx) == FAIL)
2575 return FAIL;
2576 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002577
2578 if (*p != ',' && *skipwhite(p) == ',')
2579 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002580 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002581 p = skipwhite(p);
2582 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002584 {
2585 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002586 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002587 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002588 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002589 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002590 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002592failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002593 emsg(_(e_missing_close));
2594 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595}
2596
2597/*
2598 * Compile a function call: name(arg1, arg2)
2599 * "arg" points to "name", "arg + varlen" to the "(".
2600 * "argcount_init" is 1 for "value->method()"
2601 * Instructions:
2602 * EVAL arg1
2603 * EVAL arg2
2604 * BCALL / DCALL / UCALL
2605 */
2606 static int
2607compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2608{
2609 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002610 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 int argcount = argcount_init;
2612 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002613 char_u fname_buf[FLEN_FIXED + 1];
2614 char_u *tofree = NULL;
2615 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002617 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618
2619 if (varlen >= sizeof(namebuf))
2620 {
2621 semsg(_("E1011: name too long: %s"), name);
2622 return FAIL;
2623 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002624 vim_strncpy(namebuf, *arg, varlen);
2625 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626
2627 *arg = skipwhite(*arg + varlen + 1);
2628 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002629 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002631 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 {
2633 int idx;
2634
2635 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002636 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002638 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002639 else
2640 semsg(_(e_unknownfunc), namebuf);
2641 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 }
2643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002645 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002647 {
2648 res = generate_CALL(cctx, ufunc, argcount);
2649 goto theend;
2650 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651
2652 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002653 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002655 if (STRNCMP(namebuf, "g:", 2) != 0
2656 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002657 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002658 garray_T *stack = &cctx->ctx_type_stack;
2659 type_T *type;
2660
2661 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2662 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002663 goto theend;
2664 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002666 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002667 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002668 if (STRNCMP(namebuf, "g:", 2) == 0)
2669 res = generate_UCALL(cctx, name, argcount);
2670 else
2671 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002672
2673theend:
2674 vim_free(tofree);
2675 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676}
2677
2678// like NAMESPACE_CHAR but with 'a' and 'l'.
2679#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2680
2681/*
2682 * Find the end of a variable or function name. Unlike find_name_end() this
2683 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002684 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 * Return a pointer to just after the name. Equal to "arg" if there is no
2686 * valid name.
2687 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002688 static char_u *
2689to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690{
2691 char_u *p;
2692
2693 // Quick check for valid starting character.
2694 if (!eval_isnamec1(*arg))
2695 return arg;
2696
2697 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2698 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2699 // and can be used in slice "[n:]".
2700 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002701 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2703 break;
2704 return p;
2705}
2706
2707/*
2708 * Like to_name_end() but also skip over a list or dict constant.
2709 */
2710 char_u *
2711to_name_const_end(char_u *arg)
2712{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002713 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714 typval_T rettv;
2715
2716 if (p == arg && *arg == '[')
2717 {
2718
2719 // Can be "[1, 2, 3]->Func()".
2720 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2721 p = arg;
2722 }
2723 else if (p == arg && *arg == '#' && arg[1] == '{')
2724 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002725 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 ++p;
2727 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2728 p = arg;
2729 }
2730 else if (p == arg && *arg == '{')
2731 {
2732 int ret = get_lambda_tv(&p, &rettv, FALSE);
2733
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002734 // Can be "{x -> ret}()".
2735 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736 if (ret == NOTDONE)
2737 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2738 if (ret != OK)
2739 p = arg;
2740 }
2741
2742 return p;
2743}
2744
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745/*
2746 * parse a list: [expr, expr]
2747 * "*arg" points to the '['.
2748 */
2749 static int
2750compile_list(char_u **arg, cctx_T *cctx)
2751{
2752 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002753 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 int count = 0;
2755
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002756 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002758 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002759 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002760 p = next_line_from_context(cctx);
2761 if (p == NULL)
2762 {
2763 semsg(_(e_list_end), *arg);
2764 return FAIL;
2765 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002766 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002767 p = skipwhite(p);
2768 }
2769 if (*p == ']')
2770 {
2771 ++p;
2772 // Allow for following comment, after at least one space.
2773 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
2774 p += STRLEN(p);
2775 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 if (compile_expr1(&p, cctx) == FAIL)
2778 break;
2779 ++count;
2780 if (*p == ',')
2781 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002782 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 p = skipwhite(p);
2784 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002785 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786
2787 generate_NEWLIST(cctx, count);
2788 return OK;
2789}
2790
2791/*
2792 * parse a lambda: {arg, arg -> expr}
2793 * "*arg" points to the '{'.
2794 */
2795 static int
2796compile_lambda(char_u **arg, cctx_T *cctx)
2797{
2798 garray_T *instr = &cctx->ctx_instr;
2799 typval_T rettv;
2800 ufunc_T *ufunc;
2801
2802 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002803 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002807 ++ufunc->uf_refcount;
2808 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002809 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810
2811 // The function will have one line: "return {expr}".
2812 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002813 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814
2815 if (ufunc->uf_dfunc_idx >= 0)
2816 {
2817 if (ga_grow(instr, 1) == FAIL)
2818 return FAIL;
2819 generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
2820 return OK;
2821 }
2822 return FAIL;
2823}
2824
2825/*
2826 * Compile a lamda call: expr->{lambda}(args)
2827 * "arg" points to the "{".
2828 */
2829 static int
2830compile_lambda_call(char_u **arg, cctx_T *cctx)
2831{
2832 ufunc_T *ufunc;
2833 typval_T rettv;
2834 int argcount = 1;
2835 int ret = FAIL;
2836
2837 // Get the funcref in "rettv".
2838 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2839 return FAIL;
2840
2841 if (**arg != '(')
2842 {
2843 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002844 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 else
2846 semsg(_(e_missing_paren), "lambda");
2847 clear_tv(&rettv);
2848 return FAIL;
2849 }
2850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 ufunc = rettv.vval.v_partial->pt_func;
2852 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002853 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002854 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002855
2856 // The function will have one line: "return {expr}".
2857 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002858 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859
2860 // compile the arguments
2861 *arg = skipwhite(*arg + 1);
2862 if (compile_arguments(arg, cctx, &argcount) == OK)
2863 // call the compiled function
2864 ret = generate_CALL(cctx, ufunc, argcount);
2865
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 return ret;
2867}
2868
2869/*
2870 * parse a dict: {'key': val} or #{key: val}
2871 * "*arg" points to the '{'.
2872 */
2873 static int
2874compile_dict(char_u **arg, cctx_T *cctx, int literal)
2875{
2876 garray_T *instr = &cctx->ctx_instr;
2877 int count = 0;
2878 dict_T *d = dict_alloc();
2879 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002880 char_u *whitep = *arg;
2881 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882
2883 if (d == NULL)
2884 return FAIL;
2885 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002886 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 {
2888 char_u *key = NULL;
2889
Bram Moolenaar2c330432020-04-13 14:41:35 +02002890 while (**arg == NUL || (literal && **arg == '"')
2891 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002892 {
2893 *arg = next_line_from_context(cctx);
2894 if (*arg == NULL)
2895 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002896 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002897 *arg = skipwhite(*arg);
2898 }
2899
2900 if (**arg == '}')
2901 break;
2902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 if (literal)
2904 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002905 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906
Bram Moolenaar2c330432020-04-13 14:41:35 +02002907 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 {
2909 semsg(_("E1014: Invalid key: %s"), *arg);
2910 return FAIL;
2911 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002912 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 if (generate_PUSHS(cctx, key) == FAIL)
2914 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002915 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 }
2917 else
2918 {
2919 isn_T *isn;
2920
2921 if (compile_expr1(arg, cctx) == FAIL)
2922 return FAIL;
2923 // TODO: check type is string
2924 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2925 if (isn->isn_type == ISN_PUSHS)
2926 key = isn->isn_arg.string;
2927 }
2928
2929 // Check for duplicate keys, if using string keys.
2930 if (key != NULL)
2931 {
2932 item = dict_find(d, key, -1);
2933 if (item != NULL)
2934 {
2935 semsg(_(e_duplicate_key), key);
2936 goto failret;
2937 }
2938 item = dictitem_alloc(key);
2939 if (item != NULL)
2940 {
2941 item->di_tv.v_type = VAR_UNKNOWN;
2942 item->di_tv.v_lock = 0;
2943 if (dict_add(d, item) == FAIL)
2944 dictitem_free(item);
2945 }
2946 }
2947
2948 *arg = skipwhite(*arg);
2949 if (**arg != ':')
2950 {
2951 semsg(_(e_missing_dict_colon), *arg);
2952 return FAIL;
2953 }
2954
Bram Moolenaar2c330432020-04-13 14:41:35 +02002955 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002957 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002958 {
2959 *arg = next_line_from_context(cctx);
2960 if (*arg == NULL)
2961 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002962 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002963 *arg = skipwhite(*arg);
2964 }
2965
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 if (compile_expr1(arg, cctx) == FAIL)
2967 return FAIL;
2968 ++count;
2969
Bram Moolenaar2c330432020-04-13 14:41:35 +02002970 whitep = *arg;
2971 p = skipwhite(*arg);
2972 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002973 {
2974 *arg = next_line_from_context(cctx);
2975 if (*arg == NULL)
2976 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002977 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002978 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002979 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002980 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 if (**arg == '}')
2982 break;
2983 if (**arg != ',')
2984 {
2985 semsg(_(e_missing_dict_comma), *arg);
2986 goto failret;
2987 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002988 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 *arg = skipwhite(*arg + 1);
2990 }
2991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 *arg = *arg + 1;
2993
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002994 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002995 p = skipwhite(*arg);
2996 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002997 *arg += STRLEN(*arg);
2998
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 dict_unref(d);
3000 return generate_NEWDICT(cctx, count);
3001
3002failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003003 if (*arg == NULL)
3004 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 dict_unref(d);
3006 return FAIL;
3007}
3008
3009/*
3010 * Compile "&option".
3011 */
3012 static int
3013compile_get_option(char_u **arg, cctx_T *cctx)
3014{
3015 typval_T rettv;
3016 char_u *start = *arg;
3017 int ret;
3018
3019 // parse the option and get the current value to get the type.
3020 rettv.v_type = VAR_UNKNOWN;
3021 ret = get_option_tv(arg, &rettv, TRUE);
3022 if (ret == OK)
3023 {
3024 // include the '&' in the name, get_option_tv() expects it.
3025 char_u *name = vim_strnsave(start, *arg - start);
3026 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3027
3028 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3029 vim_free(name);
3030 }
3031 clear_tv(&rettv);
3032
3033 return ret;
3034}
3035
3036/*
3037 * Compile "$VAR".
3038 */
3039 static int
3040compile_get_env(char_u **arg, cctx_T *cctx)
3041{
3042 char_u *start = *arg;
3043 int len;
3044 int ret;
3045 char_u *name;
3046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 ++*arg;
3048 len = get_env_len(arg);
3049 if (len == 0)
3050 {
3051 semsg(_(e_syntax_at), start - 1);
3052 return FAIL;
3053 }
3054
3055 // include the '$' in the name, get_env_tv() expects it.
3056 name = vim_strnsave(start, len + 1);
3057 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3058 vim_free(name);
3059 return ret;
3060}
3061
3062/*
3063 * Compile "@r".
3064 */
3065 static int
3066compile_get_register(char_u **arg, cctx_T *cctx)
3067{
3068 int ret;
3069
3070 ++*arg;
3071 if (**arg == NUL)
3072 {
3073 semsg(_(e_syntax_at), *arg - 1);
3074 return FAIL;
3075 }
3076 if (!valid_yank_reg(**arg, TRUE))
3077 {
3078 emsg_invreg(**arg);
3079 return FAIL;
3080 }
3081 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3082 ++*arg;
3083 return ret;
3084}
3085
3086/*
3087 * Apply leading '!', '-' and '+' to constant "rettv".
3088 */
3089 static int
3090apply_leader(typval_T *rettv, char_u *start, char_u *end)
3091{
3092 char_u *p = end;
3093
3094 // this works from end to start
3095 while (p > start)
3096 {
3097 --p;
3098 if (*p == '-' || *p == '+')
3099 {
3100 // only '-' has an effect, for '+' we only check the type
3101#ifdef FEAT_FLOAT
3102 if (rettv->v_type == VAR_FLOAT)
3103 {
3104 if (*p == '-')
3105 rettv->vval.v_float = -rettv->vval.v_float;
3106 }
3107 else
3108#endif
3109 {
3110 varnumber_T val;
3111 int error = FALSE;
3112
3113 // tv_get_number_chk() accepts a string, but we don't want that
3114 // here
3115 if (check_not_string(rettv) == FAIL)
3116 return FAIL;
3117 val = tv_get_number_chk(rettv, &error);
3118 clear_tv(rettv);
3119 if (error)
3120 return FAIL;
3121 if (*p == '-')
3122 val = -val;
3123 rettv->v_type = VAR_NUMBER;
3124 rettv->vval.v_number = val;
3125 }
3126 }
3127 else
3128 {
3129 int v = tv2bool(rettv);
3130
3131 // '!' is permissive in the type.
3132 clear_tv(rettv);
3133 rettv->v_type = VAR_BOOL;
3134 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3135 }
3136 }
3137 return OK;
3138}
3139
3140/*
3141 * Recognize v: variables that are constants and set "rettv".
3142 */
3143 static void
3144get_vim_constant(char_u **arg, typval_T *rettv)
3145{
3146 if (STRNCMP(*arg, "v:true", 6) == 0)
3147 {
3148 rettv->v_type = VAR_BOOL;
3149 rettv->vval.v_number = VVAL_TRUE;
3150 *arg += 6;
3151 }
3152 else if (STRNCMP(*arg, "v:false", 7) == 0)
3153 {
3154 rettv->v_type = VAR_BOOL;
3155 rettv->vval.v_number = VVAL_FALSE;
3156 *arg += 7;
3157 }
3158 else if (STRNCMP(*arg, "v:null", 6) == 0)
3159 {
3160 rettv->v_type = VAR_SPECIAL;
3161 rettv->vval.v_number = VVAL_NULL;
3162 *arg += 6;
3163 }
3164 else if (STRNCMP(*arg, "v:none", 6) == 0)
3165 {
3166 rettv->v_type = VAR_SPECIAL;
3167 rettv->vval.v_number = VVAL_NONE;
3168 *arg += 6;
3169 }
3170}
3171
3172/*
3173 * Compile code to apply '-', '+' and '!'.
3174 */
3175 static int
3176compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3177{
3178 char_u *p = end;
3179
3180 // this works from end to start
3181 while (p > start)
3182 {
3183 --p;
3184 if (*p == '-' || *p == '+')
3185 {
3186 int negate = *p == '-';
3187 isn_T *isn;
3188
3189 // TODO: check type
3190 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3191 {
3192 --p;
3193 if (*p == '-')
3194 negate = !negate;
3195 }
3196 // only '-' has an effect, for '+' we only check the type
3197 if (negate)
3198 isn = generate_instr(cctx, ISN_NEGATENR);
3199 else
3200 isn = generate_instr(cctx, ISN_CHECKNR);
3201 if (isn == NULL)
3202 return FAIL;
3203 }
3204 else
3205 {
3206 int invert = TRUE;
3207
3208 while (p > start && p[-1] == '!')
3209 {
3210 --p;
3211 invert = !invert;
3212 }
3213 if (generate_2BOOL(cctx, invert) == FAIL)
3214 return FAIL;
3215 }
3216 }
3217 return OK;
3218}
3219
3220/*
3221 * Compile whatever comes after "name" or "name()".
3222 */
3223 static int
3224compile_subscript(
3225 char_u **arg,
3226 cctx_T *cctx,
3227 char_u **start_leader,
3228 char_u *end_leader)
3229{
3230 for (;;)
3231 {
3232 if (**arg == '(')
3233 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003234 garray_T *stack = &cctx->ctx_type_stack;
3235 type_T *type;
3236 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237
3238 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003239 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3240
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 *arg = skipwhite(*arg + 1);
3242 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3243 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003244 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 return FAIL;
3246 }
3247 else if (**arg == '-' && (*arg)[1] == '>')
3248 {
3249 char_u *p;
3250
3251 // something->method()
3252 // Apply the '!', '-' and '+' first:
3253 // -1.0->func() works like (-1.0)->func()
3254 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3255 return FAIL;
3256 *start_leader = end_leader; // don't apply again later
3257
3258 *arg = skipwhite(*arg + 2);
3259 if (**arg == '{')
3260 {
3261 // lambda call: list->{lambda}
3262 if (compile_lambda_call(arg, cctx) == FAIL)
3263 return FAIL;
3264 }
3265 else
3266 {
3267 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003268 p = *arg;
3269 if (ASCII_ISALPHA(*p) && p[1] == ':')
3270 p += 2;
3271 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 ;
3273 if (*p != '(')
3274 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003275 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 return FAIL;
3277 }
3278 // TODO: base value may not be the first argument
3279 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
3280 return FAIL;
3281 }
3282 }
3283 else if (**arg == '[')
3284 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01003285 garray_T *stack;
3286 type_T **typep;
3287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 // list index: list[123]
3289 // TODO: more arguments
3290 // TODO: dict member dict['name']
3291 *arg = skipwhite(*arg + 1);
3292 if (compile_expr1(arg, cctx) == FAIL)
3293 return FAIL;
3294
3295 if (**arg != ']')
3296 {
3297 emsg(_(e_missbrac));
3298 return FAIL;
3299 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003300 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301
3302 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3303 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003304 stack = &cctx->ctx_type_stack;
3305 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
3306 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
3307 {
3308 emsg(_(e_listreq));
3309 return FAIL;
3310 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01003311 if ((*typep)->tt_type == VAR_LIST)
3312 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313 }
3314 else if (**arg == '.' && (*arg)[1] != '.')
3315 {
3316 char_u *p;
3317
3318 ++*arg;
3319 p = *arg;
3320 // dictionary member: dict.name
3321 if (eval_isnamec1(*p))
3322 while (eval_isnamec(*p))
3323 MB_PTR_ADV(p);
3324 if (p == *arg)
3325 {
3326 semsg(_(e_syntax_at), *arg);
3327 return FAIL;
3328 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
3330 return FAIL;
3331 *arg = p;
3332 }
3333 else
3334 break;
3335 }
3336
3337 // TODO - see handle_subscript():
3338 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3339 // Don't do this when "Func" is already a partial that was bound
3340 // explicitly (pt_auto is FALSE).
3341
3342 return OK;
3343}
3344
3345/*
3346 * Compile an expression at "*p" and add instructions to "instr".
3347 * "p" is advanced until after the expression, skipping white space.
3348 *
3349 * This is the equivalent of eval1(), eval2(), etc.
3350 */
3351
3352/*
3353 * number number constant
3354 * 0zFFFFFFFF Blob constant
3355 * "string" string constant
3356 * 'string' literal string constant
3357 * &option-name option value
3358 * @r register contents
3359 * identifier variable value
3360 * function() function call
3361 * $VAR environment variable
3362 * (expression) nested expression
3363 * [expr, expr] List
3364 * {key: val, key: val} Dictionary
3365 * #{key: val, key: val} Dictionary with literal keys
3366 *
3367 * Also handle:
3368 * ! in front logical NOT
3369 * - in front unary minus
3370 * + in front unary plus (ignored)
3371 * trailing (arg) funcref/partial call
3372 * trailing [] subscript in String or List
3373 * trailing .name entry in Dictionary
3374 * trailing ->name() method call
3375 */
3376 static int
3377compile_expr7(char_u **arg, cctx_T *cctx)
3378{
3379 typval_T rettv;
3380 char_u *start_leader, *end_leader;
3381 int ret = OK;
3382
3383 /*
3384 * Skip '!', '-' and '+' characters. They are handled later.
3385 */
3386 start_leader = *arg;
3387 while (**arg == '!' || **arg == '-' || **arg == '+')
3388 *arg = skipwhite(*arg + 1);
3389 end_leader = *arg;
3390
3391 rettv.v_type = VAR_UNKNOWN;
3392 switch (**arg)
3393 {
3394 /*
3395 * Number constant.
3396 */
3397 case '0': // also for blob starting with 0z
3398 case '1':
3399 case '2':
3400 case '3':
3401 case '4':
3402 case '5':
3403 case '6':
3404 case '7':
3405 case '8':
3406 case '9':
3407 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
3408 return FAIL;
3409 break;
3410
3411 /*
3412 * String constant: "string".
3413 */
3414 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
3415 return FAIL;
3416 break;
3417
3418 /*
3419 * Literal string constant: 'str''ing'.
3420 */
3421 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
3422 return FAIL;
3423 break;
3424
3425 /*
3426 * Constant Vim variable.
3427 */
3428 case 'v': get_vim_constant(arg, &rettv);
3429 ret = NOTDONE;
3430 break;
3431
3432 /*
3433 * List: [expr, expr]
3434 */
3435 case '[': ret = compile_list(arg, cctx);
3436 break;
3437
3438 /*
3439 * Dictionary: #{key: val, key: val}
3440 */
3441 case '#': if ((*arg)[1] == '{')
3442 {
3443 ++*arg;
3444 ret = compile_dict(arg, cctx, TRUE);
3445 }
3446 else
3447 ret = NOTDONE;
3448 break;
3449
3450 /*
3451 * Lambda: {arg, arg -> expr}
3452 * Dictionary: {'key': val, 'key': val}
3453 */
3454 case '{': {
3455 char_u *start = skipwhite(*arg + 1);
3456
3457 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003458 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003460 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 if (ret != FAIL && *start == '>')
3462 ret = compile_lambda(arg, cctx);
3463 else
3464 ret = compile_dict(arg, cctx, FALSE);
3465 }
3466 break;
3467
3468 /*
3469 * Option value: &name
3470 */
3471 case '&': ret = compile_get_option(arg, cctx);
3472 break;
3473
3474 /*
3475 * Environment variable: $VAR.
3476 */
3477 case '$': ret = compile_get_env(arg, cctx);
3478 break;
3479
3480 /*
3481 * Register contents: @r.
3482 */
3483 case '@': ret = compile_get_register(arg, cctx);
3484 break;
3485 /*
3486 * nested expression: (expression).
3487 */
3488 case '(': *arg = skipwhite(*arg + 1);
3489 ret = compile_expr1(arg, cctx); // recursive!
3490 *arg = skipwhite(*arg);
3491 if (**arg == ')')
3492 ++*arg;
3493 else if (ret == OK)
3494 {
3495 emsg(_(e_missing_close));
3496 ret = FAIL;
3497 }
3498 break;
3499
3500 default: ret = NOTDONE;
3501 break;
3502 }
3503 if (ret == FAIL)
3504 return FAIL;
3505
3506 if (rettv.v_type != VAR_UNKNOWN)
3507 {
3508 // apply the '!', '-' and '+' before the constant
3509 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
3510 {
3511 clear_tv(&rettv);
3512 return FAIL;
3513 }
3514 start_leader = end_leader; // don't apply again below
3515
3516 // push constant
3517 switch (rettv.v_type)
3518 {
3519 case VAR_BOOL:
3520 generate_PUSHBOOL(cctx, rettv.vval.v_number);
3521 break;
3522 case VAR_SPECIAL:
3523 generate_PUSHSPEC(cctx, rettv.vval.v_number);
3524 break;
3525 case VAR_NUMBER:
3526 generate_PUSHNR(cctx, rettv.vval.v_number);
3527 break;
3528#ifdef FEAT_FLOAT
3529 case VAR_FLOAT:
3530 generate_PUSHF(cctx, rettv.vval.v_float);
3531 break;
3532#endif
3533 case VAR_BLOB:
3534 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
3535 rettv.vval.v_blob = NULL;
3536 break;
3537 case VAR_STRING:
3538 generate_PUSHS(cctx, rettv.vval.v_string);
3539 rettv.vval.v_string = NULL;
3540 break;
3541 default:
3542 iemsg("constant type missing");
3543 return FAIL;
3544 }
3545 }
3546 else if (ret == NOTDONE)
3547 {
3548 char_u *p;
3549 int r;
3550
3551 if (!eval_isnamec1(**arg))
3552 {
3553 semsg(_("E1015: Name expected: %s"), *arg);
3554 return FAIL;
3555 }
3556
3557 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003558 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 if (*p == '(')
3560 r = compile_call(arg, p - *arg, cctx, 0);
3561 else
3562 r = compile_load(arg, p, cctx, TRUE);
3563 if (r == FAIL)
3564 return FAIL;
3565 }
3566
3567 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
3568 return FAIL;
3569
3570 // Now deal with prefixed '-', '+' and '!', if not done already.
3571 return compile_leader(cctx, start_leader, end_leader);
3572}
3573
3574/*
3575 * * number multiplication
3576 * / number division
3577 * % number modulo
3578 */
3579 static int
3580compile_expr6(char_u **arg, cctx_T *cctx)
3581{
3582 char_u *op;
3583
3584 // get the first variable
3585 if (compile_expr7(arg, cctx) == FAIL)
3586 return FAIL;
3587
3588 /*
3589 * Repeat computing, until no "*", "/" or "%" is following.
3590 */
3591 for (;;)
3592 {
3593 op = skipwhite(*arg);
3594 if (*op != '*' && *op != '/' && *op != '%')
3595 break;
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003596 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 {
3598 char_u buf[3];
3599
3600 vim_strncpy(buf, op, 1);
3601 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003602 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 }
3604 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003605 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003606 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607
3608 // get the second variable
3609 if (compile_expr7(arg, cctx) == FAIL)
3610 return FAIL;
3611
3612 generate_two_op(cctx, op);
3613 }
3614
3615 return OK;
3616}
3617
3618/*
3619 * + number addition
3620 * - number subtraction
3621 * .. string concatenation
3622 */
3623 static int
3624compile_expr5(char_u **arg, cctx_T *cctx)
3625{
3626 char_u *op;
3627 int oplen;
3628
3629 // get the first variable
3630 if (compile_expr6(arg, cctx) == FAIL)
3631 return FAIL;
3632
3633 /*
3634 * Repeat computing, until no "+", "-" or ".." is following.
3635 */
3636 for (;;)
3637 {
3638 op = skipwhite(*arg);
3639 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3640 break;
3641 oplen = (*op == '.' ? 2 : 1);
3642
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003643 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 {
3645 char_u buf[3];
3646
3647 vim_strncpy(buf, op, oplen);
3648 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003649 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 }
3651
3652 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003653 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003654 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655
3656 // get the second variable
3657 if (compile_expr6(arg, cctx) == FAIL)
3658 return FAIL;
3659
3660 if (*op == '.')
3661 {
3662 if (may_generate_2STRING(-2, cctx) == FAIL
3663 || may_generate_2STRING(-1, cctx) == FAIL)
3664 return FAIL;
3665 generate_instr_drop(cctx, ISN_CONCAT, 1);
3666 }
3667 else
3668 generate_two_op(cctx, op);
3669 }
3670
3671 return OK;
3672}
3673
Bram Moolenaar080457c2020-03-03 21:53:32 +01003674 static exptype_T
3675get_compare_type(char_u *p, int *len, int *type_is)
3676{
3677 exptype_T type = EXPR_UNKNOWN;
3678 int i;
3679
3680 switch (p[0])
3681 {
3682 case '=': if (p[1] == '=')
3683 type = EXPR_EQUAL;
3684 else if (p[1] == '~')
3685 type = EXPR_MATCH;
3686 break;
3687 case '!': if (p[1] == '=')
3688 type = EXPR_NEQUAL;
3689 else if (p[1] == '~')
3690 type = EXPR_NOMATCH;
3691 break;
3692 case '>': if (p[1] != '=')
3693 {
3694 type = EXPR_GREATER;
3695 *len = 1;
3696 }
3697 else
3698 type = EXPR_GEQUAL;
3699 break;
3700 case '<': if (p[1] != '=')
3701 {
3702 type = EXPR_SMALLER;
3703 *len = 1;
3704 }
3705 else
3706 type = EXPR_SEQUAL;
3707 break;
3708 case 'i': if (p[1] == 's')
3709 {
3710 // "is" and "isnot"; but not a prefix of a name
3711 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3712 *len = 5;
3713 i = p[*len];
3714 if (!isalnum(i) && i != '_')
3715 {
3716 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3717 *type_is = TRUE;
3718 }
3719 }
3720 break;
3721 }
3722 return type;
3723}
3724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725/*
3726 * expr5a == expr5b
3727 * expr5a =~ expr5b
3728 * expr5a != expr5b
3729 * expr5a !~ expr5b
3730 * expr5a > expr5b
3731 * expr5a >= expr5b
3732 * expr5a < expr5b
3733 * expr5a <= expr5b
3734 * expr5a is expr5b
3735 * expr5a isnot expr5b
3736 *
3737 * Produces instructions:
3738 * EVAL expr5a Push result of "expr5a"
3739 * EVAL expr5b Push result of "expr5b"
3740 * COMPARE one of the compare instructions
3741 */
3742 static int
3743compile_expr4(char_u **arg, cctx_T *cctx)
3744{
3745 exptype_T type = EXPR_UNKNOWN;
3746 char_u *p;
3747 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 int type_is = FALSE;
3749
3750 // get the first variable
3751 if (compile_expr5(arg, cctx) == FAIL)
3752 return FAIL;
3753
3754 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003755 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756
3757 /*
3758 * If there is a comparative operator, use it.
3759 */
3760 if (type != EXPR_UNKNOWN)
3761 {
3762 int ic = FALSE; // Default: do not ignore case
3763
3764 if (type_is && (p[len] == '?' || p[len] == '#'))
3765 {
3766 semsg(_(e_invexpr2), *arg);
3767 return FAIL;
3768 }
3769 // extra question mark appended: ignore case
3770 if (p[len] == '?')
3771 {
3772 ic = TRUE;
3773 ++len;
3774 }
3775 // extra '#' appended: match case (ignored)
3776 else if (p[len] == '#')
3777 ++len;
3778 // nothing appended: match case
3779
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003780 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 {
3782 char_u buf[7];
3783
3784 vim_strncpy(buf, p, len);
3785 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003786 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 }
3788
3789 // get the second variable
3790 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003791 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003792 return FAIL;
3793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 if (compile_expr5(arg, cctx) == FAIL)
3795 return FAIL;
3796
3797 generate_COMPARE(cctx, type, ic);
3798 }
3799
3800 return OK;
3801}
3802
3803/*
3804 * Compile || or &&.
3805 */
3806 static int
3807compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3808{
3809 char_u *p = skipwhite(*arg);
3810 int opchar = *op;
3811
3812 if (p[0] == opchar && p[1] == opchar)
3813 {
3814 garray_T *instr = &cctx->ctx_instr;
3815 garray_T end_ga;
3816
3817 /*
3818 * Repeat until there is no following "||" or "&&"
3819 */
3820 ga_init2(&end_ga, sizeof(int), 10);
3821 while (p[0] == opchar && p[1] == opchar)
3822 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003823 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3824 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003826 return FAIL;
3827 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828
3829 if (ga_grow(&end_ga, 1) == FAIL)
3830 {
3831 ga_clear(&end_ga);
3832 return FAIL;
3833 }
3834 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3835 ++end_ga.ga_len;
3836 generate_JUMP(cctx, opchar == '|'
3837 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3838
3839 // eval the next expression
3840 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003841 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003842 return FAIL;
3843
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 if ((opchar == '|' ? compile_expr3(arg, cctx)
3845 : compile_expr4(arg, cctx)) == FAIL)
3846 {
3847 ga_clear(&end_ga);
3848 return FAIL;
3849 }
3850 p = skipwhite(*arg);
3851 }
3852
3853 // Fill in the end label in all jumps.
3854 while (end_ga.ga_len > 0)
3855 {
3856 isn_T *isn;
3857
3858 --end_ga.ga_len;
3859 isn = ((isn_T *)instr->ga_data)
3860 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3861 isn->isn_arg.jump.jump_where = instr->ga_len;
3862 }
3863 ga_clear(&end_ga);
3864 }
3865
3866 return OK;
3867}
3868
3869/*
3870 * expr4a && expr4a && expr4a logical AND
3871 *
3872 * Produces instructions:
3873 * EVAL expr4a Push result of "expr4a"
3874 * JUMP_AND_KEEP_IF_FALSE end
3875 * EVAL expr4b Push result of "expr4b"
3876 * JUMP_AND_KEEP_IF_FALSE end
3877 * EVAL expr4c Push result of "expr4c"
3878 * end:
3879 */
3880 static int
3881compile_expr3(char_u **arg, cctx_T *cctx)
3882{
3883 // get the first variable
3884 if (compile_expr4(arg, cctx) == FAIL)
3885 return FAIL;
3886
3887 // || and && work almost the same
3888 return compile_and_or(arg, cctx, "&&");
3889}
3890
3891/*
3892 * expr3a || expr3b || expr3c logical OR
3893 *
3894 * Produces instructions:
3895 * EVAL expr3a Push result of "expr3a"
3896 * JUMP_AND_KEEP_IF_TRUE end
3897 * EVAL expr3b Push result of "expr3b"
3898 * JUMP_AND_KEEP_IF_TRUE end
3899 * EVAL expr3c Push result of "expr3c"
3900 * end:
3901 */
3902 static int
3903compile_expr2(char_u **arg, cctx_T *cctx)
3904{
3905 // eval the first expression
3906 if (compile_expr3(arg, cctx) == FAIL)
3907 return FAIL;
3908
3909 // || and && work almost the same
3910 return compile_and_or(arg, cctx, "||");
3911}
3912
3913/*
3914 * Toplevel expression: expr2 ? expr1a : expr1b
3915 *
3916 * Produces instructions:
3917 * EVAL expr2 Push result of "expr"
3918 * JUMP_IF_FALSE alt jump if false
3919 * EVAL expr1a
3920 * JUMP_ALWAYS end
3921 * alt: EVAL expr1b
3922 * end:
3923 */
3924 static int
3925compile_expr1(char_u **arg, cctx_T *cctx)
3926{
3927 char_u *p;
3928
Bram Moolenaar3df02f52020-05-03 15:47:33 +02003929 // TODO: Try parsing as a constant. If that works just one PUSH
3930 // instruction needs to be generated.
3931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 // evaluate the first expression
3933 if (compile_expr2(arg, cctx) == FAIL)
3934 return FAIL;
3935
3936 p = skipwhite(*arg);
3937 if (*p == '?')
3938 {
3939 garray_T *instr = &cctx->ctx_instr;
3940 garray_T *stack = &cctx->ctx_type_stack;
3941 int alt_idx = instr->ga_len;
3942 int end_idx;
3943 isn_T *isn;
3944 type_T *type1;
3945 type_T *type2;
3946
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003947 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3948 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003950 return FAIL;
3951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952
3953 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3954
3955 // evaluate the second expression; any type is accepted
3956 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003957 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003958 return FAIL;
3959
Bram Moolenaara6d53682020-01-28 23:04:06 +01003960 if (compile_expr1(arg, cctx) == FAIL)
3961 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962
3963 // remember the type and drop it
3964 --stack->ga_len;
3965 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3966
3967 end_idx = instr->ga_len;
3968 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3969
3970 // jump here from JUMP_IF_FALSE
3971 isn = ((isn_T *)instr->ga_data) + alt_idx;
3972 isn->isn_arg.jump.jump_where = instr->ga_len;
3973
3974 // Check for the ":".
3975 p = skipwhite(*arg);
3976 if (*p != ':')
3977 {
3978 emsg(_(e_missing_colon));
3979 return FAIL;
3980 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003981 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3982 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003984 return FAIL;
3985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986
3987 // evaluate the third expression
3988 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003989 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003990 return FAIL;
3991
Bram Moolenaara6d53682020-01-28 23:04:06 +01003992 if (compile_expr1(arg, cctx) == FAIL)
3993 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994
3995 // If the types differ, the result has a more generic type.
3996 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003997 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998
3999 // jump here from JUMP_ALWAYS
4000 isn = ((isn_T *)instr->ga_data) + end_idx;
4001 isn->isn_arg.jump.jump_where = instr->ga_len;
4002 }
4003 return OK;
4004}
4005
4006/*
4007 * compile "return [expr]"
4008 */
4009 static char_u *
4010compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4011{
4012 char_u *p = arg;
4013 garray_T *stack = &cctx->ctx_type_stack;
4014 type_T *stack_type;
4015
4016 if (*p != NUL && *p != '|' && *p != '\n')
4017 {
4018 // compile return argument into instructions
4019 if (compile_expr1(&p, cctx) == FAIL)
4020 return NULL;
4021
4022 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4023 if (set_return_type)
4024 cctx->ctx_ufunc->uf_ret_type = stack_type;
4025 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4026 == FAIL)
4027 return NULL;
4028 }
4029 else
4030 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004031 // "set_return_type" cannot be TRUE, only used for a lambda which
4032 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004033 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4034 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 {
4036 emsg(_("E1003: Missing return value"));
4037 return NULL;
4038 }
4039
4040 // No argument, return zero.
4041 generate_PUSHNR(cctx, 0);
4042 }
4043
4044 if (generate_instr(cctx, ISN_RETURN) == NULL)
4045 return NULL;
4046
4047 // "return val | endif" is possible
4048 return skipwhite(p);
4049}
4050
4051/*
4052 * Return the length of an assignment operator, or zero if there isn't one.
4053 */
4054 int
4055assignment_len(char_u *p, int *heredoc)
4056{
4057 if (*p == '=')
4058 {
4059 if (p[1] == '<' && p[2] == '<')
4060 {
4061 *heredoc = TRUE;
4062 return 3;
4063 }
4064 return 1;
4065 }
4066 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4067 return 2;
4068 if (STRNCMP(p, "..=", 3) == 0)
4069 return 3;
4070 return 0;
4071}
4072
4073// words that cannot be used as a variable
4074static char *reserved[] = {
4075 "true",
4076 "false",
4077 NULL
4078};
4079
4080/*
4081 * Get a line for "=<<".
4082 * Return a pointer to the line in allocated memory.
4083 * Return NULL for end-of-file or some error.
4084 */
4085 static char_u *
4086heredoc_getline(
4087 int c UNUSED,
4088 void *cookie,
4089 int indent UNUSED,
4090 int do_concat UNUSED)
4091{
4092 cctx_T *cctx = (cctx_T *)cookie;
4093
4094 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004095 {
4096 iemsg("Heredoc got to end");
4097 return NULL;
4098 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 ++cctx->ctx_lnum;
4100 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4101 [cctx->ctx_lnum]);
4102}
4103
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004104typedef enum {
4105 dest_local,
4106 dest_option,
4107 dest_env,
4108 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004109 dest_buffer,
4110 dest_window,
4111 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004112 dest_vimvar,
4113 dest_script,
4114 dest_reg,
4115} assign_dest_T;
4116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117/*
4118 * compile "let var [= expr]", "const var = expr" and "var = expr"
4119 * "arg" points to "var".
4120 */
4121 static char_u *
4122compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4123{
4124 char_u *p;
4125 char_u *ret = NULL;
4126 int var_count = 0;
4127 int semicolon = 0;
4128 size_t varlen;
4129 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004130 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004133 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004135 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 int oplen = 0;
4137 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004138 type_T *type = &t_any;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004139 lvar_T *lvar = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 char_u *name;
4141 char_u *sp;
4142 int has_type = FALSE;
4143 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
4144 int instr_count = -1;
4145
4146 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
4147 if (p == NULL)
4148 return NULL;
4149 if (var_count > 0)
4150 {
4151 // TODO: let [var, var] = list
4152 emsg("Cannot handle a list yet");
4153 return NULL;
4154 }
4155
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004156 // "a: type" is declaring variable "a" with a type, not "a:".
4157 if (is_decl && p == arg + 2 && p[-1] == ':')
4158 --p;
4159
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 varlen = p - arg;
4161 name = vim_strnsave(arg, (int)varlen);
4162 if (name == NULL)
4163 return NULL;
4164
Bram Moolenaar080457c2020-03-03 21:53:32 +01004165 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004167 if (*arg == '&')
4168 {
4169 int cc;
4170 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171
Bram Moolenaar080457c2020-03-03 21:53:32 +01004172 dest = dest_option;
4173 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004175 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004176 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 if (is_decl)
4179 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004180 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 goto theend;
4182 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004183 p = arg;
4184 p = find_option_end(&p, &opt_flags);
4185 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004187 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01004188 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004189 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004190 }
4191 cc = *p;
4192 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004193 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004194 *p = cc;
4195 if (opt_type == -3)
4196 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004197 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004198 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004199 }
4200 if (opt_type == -2 || opt_type == 0)
4201 type = &t_string;
4202 else
4203 type = &t_number; // both number and boolean option
4204 }
4205 else if (*arg == '$')
4206 {
4207 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004208 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004209 if (is_decl)
4210 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004211 semsg(_("E1065: Cannot declare an environment variable: %s"),
4212 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004213 goto theend;
4214 }
4215 }
4216 else if (*arg == '@')
4217 {
4218 if (!valid_yank_reg(arg[1], TRUE))
4219 {
4220 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004221 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004222 }
4223 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004224 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004225 if (is_decl)
4226 {
4227 semsg(_("E1066: Cannot declare a register: %s"), name);
4228 goto theend;
4229 }
4230 }
4231 else if (STRNCMP(arg, "g:", 2) == 0)
4232 {
4233 dest = dest_global;
4234 if (is_decl)
4235 {
4236 semsg(_("E1016: Cannot declare a global variable: %s"), name);
4237 goto theend;
4238 }
4239 }
Bram Moolenaard3aac292020-04-19 14:32:17 +02004240 else if (STRNCMP(arg, "b:", 2) == 0)
4241 {
4242 dest = dest_buffer;
4243 if (is_decl)
4244 {
4245 semsg(_("E1078: Cannot declare a buffer variable: %s"), name);
4246 goto theend;
4247 }
4248 }
4249 else if (STRNCMP(arg, "w:", 2) == 0)
4250 {
4251 dest = dest_window;
4252 if (is_decl)
4253 {
4254 semsg(_("E1079: Cannot declare a window variable: %s"), name);
4255 goto theend;
4256 }
4257 }
4258 else if (STRNCMP(arg, "t:", 2) == 0)
4259 {
4260 dest = dest_tab;
4261 if (is_decl)
4262 {
4263 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
4264 goto theend;
4265 }
4266 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004267 else if (STRNCMP(arg, "v:", 2) == 0)
4268 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004269 typval_T *vtv;
4270 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004271
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004272 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004273 if (vimvaridx < 0)
4274 {
4275 semsg(_(e_var_notfound), arg);
4276 goto theend;
4277 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004278 // We use the current value of "sandbox" here, is that OK?
4279 if (var_check_ro(di_flags, name, FALSE))
4280 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004281 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004282 vtv = get_vim_var_tv(vimvaridx);
4283 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004284 if (is_decl)
4285 {
4286 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
4287 goto theend;
4288 }
4289 }
4290 else
4291 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004292 int idx;
4293
Bram Moolenaar080457c2020-03-03 21:53:32 +01004294 for (idx = 0; reserved[idx] != NULL; ++idx)
4295 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004297 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 goto theend;
4299 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004300
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004301 lvar = lookup_local(arg, varlen, cctx);
4302 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004304 if (is_decl)
4305 {
4306 semsg(_("E1017: Variable already declared: %s"), name);
4307 goto theend;
4308 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004309 else if (lvar->lv_const)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004310 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004311 semsg(_("E1018: Cannot assign to a constant: %s"), name);
4312 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004313 }
4314 }
4315 else if (STRNCMP(arg, "s:", 2) == 0
4316 || lookup_script(arg, varlen) == OK
4317 || find_imported(arg, varlen, cctx) != NULL)
4318 {
4319 dest = dest_script;
4320 if (is_decl)
4321 {
4322 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004323 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004324 goto theend;
4325 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004327 else if (name[1] == ':' && name[2] != NUL)
4328 {
4329 semsg(_("E1082: Cannot use a namespaced variable: %s"), name);
4330 goto theend;
4331 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 }
4333 }
4334
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004335 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 {
4337 if (is_decl && *p == ':')
4338 {
4339 // parse optional type: "let var: type = expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004340 if (!VIM_ISWHITE(p[1]))
4341 {
4342 semsg(_(e_white_after), ":");
4343 goto theend;
4344 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 p = skipwhite(p + 1);
4346 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347 has_type = TRUE;
4348 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004349 else if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 type = lvar->lv_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 }
4352
4353 sp = p;
4354 p = skipwhite(p);
4355 op = p;
4356 oplen = assignment_len(p, &heredoc);
4357 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4358 {
4359 char_u buf[4];
4360
4361 vim_strncpy(buf, op, oplen);
4362 semsg(_(e_white_both), buf);
4363 }
4364
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004365 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02004366 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004368 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369 goto theend;
4370 }
4371
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004372 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 {
4374 if (oplen > 1 && !heredoc)
4375 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004376 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4378 name);
4379 goto theend;
4380 }
4381
4382 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02004383 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004384 goto theend;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004385 lvar = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4386 if (lvar == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004388 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 }
4390
4391 if (heredoc)
4392 {
4393 list_T *l;
4394 listitem_T *li;
4395
4396 // [let] varname =<< [trim] {end}
4397 eap->getline = heredoc_getline;
4398 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004399 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400
4401 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004402 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 {
4404 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4405 li->li_tv.vval.v_string = NULL;
4406 }
4407 generate_NEWLIST(cctx, l->lv_len);
4408 type = &t_list_string;
4409 list_free(l);
4410 p += STRLEN(p);
4411 }
4412 else if (oplen > 0)
4413 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004414 int r;
4415 type_T *stacktype;
4416 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004417
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 // for "+=", "*=", "..=" etc. first load the current value
4419 if (*op != '=')
4420 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004421 switch (dest)
4422 {
4423 case dest_option:
4424 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004425 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004426 break;
4427 case dest_global:
4428 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4429 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004430 case dest_buffer:
4431 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4432 break;
4433 case dest_window:
4434 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4435 break;
4436 case dest_tab:
4437 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4438 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004439 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01004440 compile_load_scriptvar(cctx,
4441 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004442 break;
4443 case dest_env:
4444 // Include $ in the name here
4445 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4446 break;
4447 case dest_reg:
4448 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
4449 break;
4450 case dest_vimvar:
4451 generate_LOADV(cctx, name + 2, TRUE);
4452 break;
4453 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004454 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004455 break;
4456 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457 }
4458
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004459 // Compile the expression. Temporarily hide the new local variable
4460 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02004461 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004462 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 instr_count = instr->ga_len;
4464 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004465 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02004466 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004467 ++cctx->ctx_locals.ga_len;
4468 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469 goto theend;
4470
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004471 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004473 stack = &cctx->ctx_type_stack;
4474 stacktype = stack->ga_len == 0 ? &t_void
4475 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004476 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004478 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004480 if (stacktype->tt_type == VAR_VOID)
4481 {
4482 emsg(_("E1031: Cannot use void value"));
4483 goto theend;
4484 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004485 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004486 {
4487 // An empty list or dict has a &t_void member, for a
4488 // variable that implies &t_any.
4489 if (stacktype == &t_list_empty)
4490 lvar->lv_type = &t_list_any;
4491 else if (stacktype == &t_dict_empty)
4492 lvar->lv_type = &t_dict_any;
4493 else
4494 lvar->lv_type = stacktype;
4495 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004496 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004497 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
4498 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004500 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004501 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502 }
4503 }
4504 else if (cmdidx == CMD_const)
4505 {
4506 emsg(_("E1021: const requires a value"));
4507 goto theend;
4508 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004509 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510 {
4511 emsg(_("E1022: type or initialization required"));
4512 goto theend;
4513 }
4514 else
4515 {
4516 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517 if (ga_grow(instr, 1) == FAIL)
4518 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004519 switch (type->tt_type)
4520 {
4521 case VAR_BOOL:
4522 generate_PUSHBOOL(cctx, VVAL_FALSE);
4523 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004524 case VAR_FLOAT:
4525#ifdef FEAT_FLOAT
4526 generate_PUSHF(cctx, 0.0);
4527#endif
4528 break;
4529 case VAR_STRING:
4530 generate_PUSHS(cctx, NULL);
4531 break;
4532 case VAR_BLOB:
4533 generate_PUSHBLOB(cctx, NULL);
4534 break;
4535 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004536 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004537 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004538 case VAR_LIST:
4539 generate_NEWLIST(cctx, 0);
4540 break;
4541 case VAR_DICT:
4542 generate_NEWDICT(cctx, 0);
4543 break;
4544 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004545 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004546 break;
4547 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004548 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004549 break;
4550 case VAR_NUMBER:
4551 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004552 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02004553 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01004554 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02004555 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01004556 generate_PUSHNR(cctx, 0);
4557 break;
4558 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559 }
4560
4561 if (oplen > 0 && *op != '=')
4562 {
4563 type_T *expected = &t_number;
4564 garray_T *stack = &cctx->ctx_type_stack;
4565 type_T *stacktype;
4566
4567 // TODO: if type is known use float or any operation
4568
4569 if (*op == '.')
4570 expected = &t_string;
4571 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4572 if (need_type(stacktype, expected, -1, cctx) == FAIL)
4573 goto theend;
4574
4575 if (*op == '.')
4576 generate_instr_drop(cctx, ISN_CONCAT, 1);
4577 else
4578 {
4579 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4580
4581 if (isn == NULL)
4582 goto theend;
4583 switch (*op)
4584 {
4585 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
4586 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4587 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4588 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4589 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4590 }
4591 }
4592 }
4593
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004594 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004596 case dest_option:
4597 generate_STOREOPT(cctx, name + 1, opt_flags);
4598 break;
4599 case dest_global:
4600 // include g: with the name, easier to execute that way
4601 generate_STORE(cctx, ISN_STOREG, 0, name);
4602 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004603 case dest_buffer:
4604 // include b: with the name, easier to execute that way
4605 generate_STORE(cctx, ISN_STOREB, 0, name);
4606 break;
4607 case dest_window:
4608 // include w: with the name, easier to execute that way
4609 generate_STORE(cctx, ISN_STOREW, 0, name);
4610 break;
4611 case dest_tab:
4612 // include t: with the name, easier to execute that way
4613 generate_STORE(cctx, ISN_STORET, 0, name);
4614 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004615 case dest_env:
4616 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
4617 break;
4618 case dest_reg:
4619 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
4620 break;
4621 case dest_vimvar:
4622 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
4623 break;
4624 case dest_script:
4625 {
4626 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4627 imported_T *import = NULL;
4628 int sid = current_sctx.sc_sid;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004629 int idx;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004630
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004631 if (name[1] != ':')
4632 {
4633 import = find_imported(name, 0, cctx);
4634 if (import != NULL)
4635 sid = import->imp_sid;
4636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004638 idx = get_script_item_idx(sid, rawname, TRUE);
4639 // TODO: specific type
4640 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004641 {
4642 char_u *name_s = name;
4643
4644 // Include s: in the name for store_var()
4645 if (name[1] != ':')
4646 {
4647 int len = (int)STRLEN(name) + 3;
4648
4649 name_s = alloc(len);
4650 if (name_s == NULL)
4651 name_s = name;
4652 else
4653 vim_snprintf((char *)name_s, len, "s:%s", name);
4654 }
4655 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
4656 if (name_s != name)
4657 vim_free(name_s);
4658 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004659 else
4660 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
4661 sid, idx, &t_any);
4662 }
4663 break;
4664 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004665 if (lvar != NULL)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004666 {
4667 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004669 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
4670 // into ISN_STORENR
4671 if (instr->ga_len == instr_count + 1
4672 && isn->isn_type == ISN_PUSHNR)
4673 {
4674 varnumber_T val = isn->isn_arg.number;
4675 garray_T *stack = &cctx->ctx_type_stack;
4676
4677 isn->isn_type = ISN_STORENR;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004678 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004679 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004680 if (stack->ga_len > 0)
4681 --stack->ga_len;
4682 }
4683 else
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004684 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004685 }
4686 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687 }
4688 ret = p;
4689
4690theend:
4691 vim_free(name);
4692 return ret;
4693}
4694
4695/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004696 * Check if "name" can be "unlet".
4697 */
4698 int
4699check_vim9_unlet(char_u *name)
4700{
4701 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
4702 {
4703 semsg(_("E1081: Cannot unlet %s"), name);
4704 return FAIL;
4705 }
4706 return OK;
4707}
4708
4709/*
4710 * Callback passed to ex_unletlock().
4711 */
4712 static int
4713compile_unlet(
4714 lval_T *lvp,
4715 char_u *name_end,
4716 exarg_T *eap,
4717 int deep UNUSED,
4718 void *coookie)
4719{
4720 cctx_T *cctx = coookie;
4721
4722 if (lvp->ll_tv == NULL)
4723 {
4724 char_u *p = lvp->ll_name;
4725 int cc = *name_end;
4726 int ret = OK;
4727
4728 // Normal name. Only supports g:, w:, t: and b: namespaces.
4729 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004730 if (*p == '$')
4731 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
4732 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004733 ret = FAIL;
4734 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004735 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004736
4737 *name_end = cc;
4738 return ret;
4739 }
4740
4741 // TODO: unlet {list}[idx]
4742 // TODO: unlet {dict}[key]
4743 emsg("Sorry, :unlet not fully implemented yet");
4744 return FAIL;
4745}
4746
4747/*
4748 * compile "unlet var", "lock var" and "unlock var"
4749 * "arg" points to "var".
4750 */
4751 static char_u *
4752compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
4753{
4754 char_u *p = arg;
4755
4756 if (eap->cmdidx != CMD_unlet)
4757 {
4758 emsg("Sorry, :lock and unlock not implemented yet");
4759 return NULL;
4760 }
4761
4762 if (*p == '!')
4763 {
4764 p = skipwhite(p + 1);
4765 eap->forceit = TRUE;
4766 }
4767
4768 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
4769 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4770}
4771
4772/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 * Compile an :import command.
4774 */
4775 static char_u *
4776compile_import(char_u *arg, cctx_T *cctx)
4777{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01004778 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779}
4780
4781/*
4782 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
4783 */
4784 static int
4785compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
4786{
4787 garray_T *instr = &cctx->ctx_instr;
4788 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
4789
4790 if (endlabel == NULL)
4791 return FAIL;
4792 endlabel->el_next = *el;
4793 *el = endlabel;
4794 endlabel->el_end_label = instr->ga_len;
4795
4796 generate_JUMP(cctx, when, 0);
4797 return OK;
4798}
4799
4800 static void
4801compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
4802{
4803 garray_T *instr = &cctx->ctx_instr;
4804
4805 while (*el != NULL)
4806 {
4807 endlabel_T *cur = (*el);
4808 isn_T *isn;
4809
4810 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
4811 isn->isn_arg.jump.jump_where = instr->ga_len;
4812 *el = cur->el_next;
4813 vim_free(cur);
4814 }
4815}
4816
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004817 static void
4818compile_free_jump_to_end(endlabel_T **el)
4819{
4820 while (*el != NULL)
4821 {
4822 endlabel_T *cur = (*el);
4823
4824 *el = cur->el_next;
4825 vim_free(cur);
4826 }
4827}
4828
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829/*
4830 * Create a new scope and set up the generic items.
4831 */
4832 static scope_T *
4833new_scope(cctx_T *cctx, scopetype_T type)
4834{
4835 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
4836
4837 if (scope == NULL)
4838 return NULL;
4839 scope->se_outer = cctx->ctx_scope;
4840 cctx->ctx_scope = scope;
4841 scope->se_type = type;
4842 scope->se_local_count = cctx->ctx_locals.ga_len;
4843 return scope;
4844}
4845
4846/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004847 * Free the current scope and go back to the outer scope.
4848 */
4849 static void
4850drop_scope(cctx_T *cctx)
4851{
4852 scope_T *scope = cctx->ctx_scope;
4853
4854 if (scope == NULL)
4855 {
4856 iemsg("calling drop_scope() without a scope");
4857 return;
4858 }
4859 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004860 switch (scope->se_type)
4861 {
4862 case IF_SCOPE:
4863 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
4864 case FOR_SCOPE:
4865 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
4866 case WHILE_SCOPE:
4867 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
4868 case TRY_SCOPE:
4869 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
4870 case NO_SCOPE:
4871 case BLOCK_SCOPE:
4872 break;
4873 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004874 vim_free(scope);
4875}
4876
4877/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004878 * Evaluate an expression that is a constant:
4879 * has(arg)
4880 *
4881 * Also handle:
4882 * ! in front logical NOT
4883 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004884 * Return FAIL if the expression is not a constant.
4885 */
4886 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004887evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004888{
4889 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004890 char_u *start_leader, *end_leader;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004891 int has_call = FALSE;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004892
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004893 /*
4894 * Skip '!' characters. They are handled later.
4895 */
4896 start_leader = *arg;
4897 while (**arg == '!')
4898 *arg = skipwhite(*arg + 1);
4899 end_leader = *arg;
4900
4901 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004902 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004903 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004904 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4905 {
4906 tv->v_type = VAR_SPECIAL;
4907 tv->vval.v_number = VVAL_TRUE;
4908 *arg += 4;
4909 return OK;
4910 }
4911 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4912 {
4913 tv->v_type = VAR_SPECIAL;
4914 tv->vval.v_number = VVAL_FALSE;
4915 *arg += 5;
4916 return OK;
4917 }
4918
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004919 if (STRNCMP("has(", *arg, 4) == 0)
4920 {
4921 has_call = TRUE;
4922 *arg = skipwhite(*arg + 4);
4923 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004924
4925 if (**arg == '"')
4926 {
4927 if (get_string_tv(arg, tv, TRUE) == FAIL)
4928 return FAIL;
4929 }
4930 else if (**arg == '\'')
4931 {
4932 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4933 return FAIL;
4934 }
4935 else
4936 return FAIL;
4937
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004938 if (has_call)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004939 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004940 *arg = skipwhite(*arg);
4941 if (**arg != ')')
4942 return FAIL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004943 *arg = *arg + 1;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004944
4945 argvars[0] = *tv;
4946 argvars[1].v_type = VAR_UNKNOWN;
4947 tv->v_type = VAR_NUMBER;
4948 tv->vval.v_number = 0;
4949 f_has(argvars, tv);
4950 clear_tv(&argvars[0]);
4951
4952 while (start_leader < end_leader)
4953 {
4954 if (*start_leader == '!')
4955 tv->vval.v_number = !tv->vval.v_number;
4956 ++start_leader;
4957 }
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004958 }
4959
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004960 return OK;
4961}
4962
Bram Moolenaar080457c2020-03-03 21:53:32 +01004963 static int
4964evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
4965{
4966 exptype_T type = EXPR_UNKNOWN;
4967 char_u *p;
4968 int len = 2;
4969 int type_is = FALSE;
4970
4971 // get the first variable
4972 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
4973 return FAIL;
4974
4975 p = skipwhite(*arg);
4976 type = get_compare_type(p, &len, &type_is);
4977
4978 /*
4979 * If there is a comparative operator, use it.
4980 */
4981 if (type != EXPR_UNKNOWN)
4982 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004983 typval_T tv2;
4984 char_u *s1, *s2;
4985 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4986 int n;
4987
4988 // TODO: Only string == string is supported now
4989 if (tv->v_type != VAR_STRING)
4990 return FAIL;
4991 if (type != EXPR_EQUAL)
4992 return FAIL;
4993
4994 // get the second variable
Bram Moolenaar4227c782020-04-02 16:00:04 +02004995 init_tv(&tv2);
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004996 *arg = skipwhite(p + len);
4997 if (evaluate_const_expr7(arg, cctx, &tv2) == FAIL
4998 || tv2.v_type != VAR_STRING)
4999 {
5000 clear_tv(&tv2);
5001 return FAIL;
5002 }
5003 s1 = tv_get_string_buf(tv, buf1);
5004 s2 = tv_get_string_buf(&tv2, buf2);
5005 n = STRCMP(s1, s2);
5006 clear_tv(tv);
5007 clear_tv(&tv2);
5008 tv->v_type = VAR_BOOL;
5009 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar080457c2020-03-03 21:53:32 +01005010 }
5011
5012 return OK;
5013}
5014
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005015static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
5016
5017/*
5018 * Compile constant || or &&.
5019 */
5020 static int
5021evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
5022{
5023 char_u *p = skipwhite(*arg);
5024 int opchar = *op;
5025
5026 if (p[0] == opchar && p[1] == opchar)
5027 {
5028 int val = tv2bool(tv);
5029
5030 /*
5031 * Repeat until there is no following "||" or "&&"
5032 */
5033 while (p[0] == opchar && p[1] == opchar)
5034 {
5035 typval_T tv2;
5036
5037 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
5038 return FAIL;
5039
5040 // eval the next expression
5041 *arg = skipwhite(p + 2);
5042 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01005043 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005044 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01005045 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005046 {
5047 clear_tv(&tv2);
5048 return FAIL;
5049 }
5050 if ((opchar == '&') == val)
5051 {
5052 // false || tv2 or true && tv2: use tv2
5053 clear_tv(tv);
5054 *tv = tv2;
5055 val = tv2bool(tv);
5056 }
5057 else
5058 clear_tv(&tv2);
5059 p = skipwhite(*arg);
5060 }
5061 }
5062
5063 return OK;
5064}
5065
5066/*
5067 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
5068 * Return FAIL if the expression is not a constant.
5069 */
5070 static int
5071evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
5072{
5073 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01005074 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005075 return FAIL;
5076
5077 // || and && work almost the same
5078 return evaluate_const_and_or(arg, cctx, "&&", tv);
5079}
5080
5081/*
5082 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
5083 * Return FAIL if the expression is not a constant.
5084 */
5085 static int
5086evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
5087{
5088 // evaluate the first expression
5089 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
5090 return FAIL;
5091
5092 // || and && work almost the same
5093 return evaluate_const_and_or(arg, cctx, "||", tv);
5094}
5095
5096/*
5097 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
5098 * E.g. for "has('feature')".
5099 * This does not produce error messages. "tv" should be cleared afterwards.
5100 * Return FAIL if the expression is not a constant.
5101 */
5102 static int
5103evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
5104{
5105 char_u *p;
5106
5107 // evaluate the first expression
5108 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
5109 return FAIL;
5110
5111 p = skipwhite(*arg);
5112 if (*p == '?')
5113 {
5114 int val = tv2bool(tv);
5115 typval_T tv2;
5116
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005117 // require space before and after the ?
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005118 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
5119 return FAIL;
5120
5121 // evaluate the second expression; any type is accepted
5122 clear_tv(tv);
5123 *arg = skipwhite(p + 1);
5124 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
5125 return FAIL;
5126
5127 // Check for the ":".
5128 p = skipwhite(*arg);
5129 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
5130 return FAIL;
5131
5132 // evaluate the third expression
5133 *arg = skipwhite(p + 1);
5134 tv2.v_type = VAR_UNKNOWN;
5135 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
5136 {
5137 clear_tv(&tv2);
5138 return FAIL;
5139 }
5140 if (val)
5141 {
5142 // use the expr after "?"
5143 clear_tv(&tv2);
5144 }
5145 else
5146 {
5147 // use the expr after ":"
5148 clear_tv(tv);
5149 *tv = tv2;
5150 }
5151 }
5152 return OK;
5153}
5154
5155/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005156 * compile "if expr"
5157 *
5158 * "if expr" Produces instructions:
5159 * EVAL expr Push result of "expr"
5160 * JUMP_IF_FALSE end
5161 * ... body ...
5162 * end:
5163 *
5164 * "if expr | else" Produces instructions:
5165 * EVAL expr Push result of "expr"
5166 * JUMP_IF_FALSE else
5167 * ... body ...
5168 * JUMP_ALWAYS end
5169 * else:
5170 * ... body ...
5171 * end:
5172 *
5173 * "if expr1 | elseif expr2 | else" Produces instructions:
5174 * EVAL expr Push result of "expr"
5175 * JUMP_IF_FALSE elseif
5176 * ... body ...
5177 * JUMP_ALWAYS end
5178 * elseif:
5179 * EVAL expr Push result of "expr"
5180 * JUMP_IF_FALSE else
5181 * ... body ...
5182 * JUMP_ALWAYS end
5183 * else:
5184 * ... body ...
5185 * end:
5186 */
5187 static char_u *
5188compile_if(char_u *arg, cctx_T *cctx)
5189{
5190 char_u *p = arg;
5191 garray_T *instr = &cctx->ctx_instr;
5192 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005193 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005194
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005195 // compile "expr"; if we know it evaluates to FALSE skip the block
5196 tv.v_type = VAR_UNKNOWN;
5197 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5198 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5199 else
5200 cctx->ctx_skip = MAYBE;
5201 clear_tv(&tv);
5202 if (cctx->ctx_skip == MAYBE)
5203 {
5204 p = arg;
5205 if (compile_expr1(&p, cctx) == FAIL)
5206 return NULL;
5207 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208
5209 scope = new_scope(cctx, IF_SCOPE);
5210 if (scope == NULL)
5211 return NULL;
5212
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005213 if (cctx->ctx_skip == MAYBE)
5214 {
5215 // "where" is set when ":elseif", "else" or ":endif" is found
5216 scope->se_u.se_if.is_if_label = instr->ga_len;
5217 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5218 }
5219 else
5220 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221
5222 return p;
5223}
5224
5225 static char_u *
5226compile_elseif(char_u *arg, cctx_T *cctx)
5227{
5228 char_u *p = arg;
5229 garray_T *instr = &cctx->ctx_instr;
5230 isn_T *isn;
5231 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005232 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233
5234 if (scope == NULL || scope->se_type != IF_SCOPE)
5235 {
5236 emsg(_(e_elseif_without_if));
5237 return NULL;
5238 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005239 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005240
Bram Moolenaar158906c2020-02-06 20:39:45 +01005241 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005242 {
5243 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005244 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005245 return NULL;
5246 // previous "if" or "elseif" jumps here
5247 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5248 isn->isn_arg.jump.jump_where = instr->ga_len;
5249 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005250
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005251 // compile "expr"; if we know it evaluates to FALSE skip the block
5252 tv.v_type = VAR_UNKNOWN;
5253 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5254 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5255 else
5256 cctx->ctx_skip = MAYBE;
5257 clear_tv(&tv);
5258 if (cctx->ctx_skip == MAYBE)
5259 {
5260 p = arg;
5261 if (compile_expr1(&p, cctx) == FAIL)
5262 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005263
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005264 // "where" is set when ":elseif", "else" or ":endif" is found
5265 scope->se_u.se_if.is_if_label = instr->ga_len;
5266 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5267 }
5268 else
5269 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005270
5271 return p;
5272}
5273
5274 static char_u *
5275compile_else(char_u *arg, cctx_T *cctx)
5276{
5277 char_u *p = arg;
5278 garray_T *instr = &cctx->ctx_instr;
5279 isn_T *isn;
5280 scope_T *scope = cctx->ctx_scope;
5281
5282 if (scope == NULL || scope->se_type != IF_SCOPE)
5283 {
5284 emsg(_(e_else_without_if));
5285 return NULL;
5286 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005287 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005288
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005289 // jump from previous block to the end, unless the else block is empty
5290 if (cctx->ctx_skip == MAYBE)
5291 {
5292 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005293 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005294 return NULL;
5295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296
Bram Moolenaar158906c2020-02-06 20:39:45 +01005297 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005298 {
5299 if (scope->se_u.se_if.is_if_label >= 0)
5300 {
5301 // previous "if" or "elseif" jumps here
5302 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5303 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005304 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005305 }
5306 }
5307
5308 if (cctx->ctx_skip != MAYBE)
5309 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005310
5311 return p;
5312}
5313
5314 static char_u *
5315compile_endif(char_u *arg, cctx_T *cctx)
5316{
5317 scope_T *scope = cctx->ctx_scope;
5318 ifscope_T *ifscope;
5319 garray_T *instr = &cctx->ctx_instr;
5320 isn_T *isn;
5321
5322 if (scope == NULL || scope->se_type != IF_SCOPE)
5323 {
5324 emsg(_(e_endif_without_if));
5325 return NULL;
5326 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005327 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005328 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005329
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005330 if (scope->se_u.se_if.is_if_label >= 0)
5331 {
5332 // previous "if" or "elseif" jumps here
5333 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5334 isn->isn_arg.jump.jump_where = instr->ga_len;
5335 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005336 // Fill in the "end" label in jumps at the end of the blocks.
5337 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005338 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005339
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005340 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341 return arg;
5342}
5343
5344/*
5345 * compile "for var in expr"
5346 *
5347 * Produces instructions:
5348 * PUSHNR -1
5349 * STORE loop-idx Set index to -1
5350 * EVAL expr Push result of "expr"
5351 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5352 * - if beyond end, jump to "end"
5353 * - otherwise get item from list and push it
5354 * STORE var Store item in "var"
5355 * ... body ...
5356 * JUMP top Jump back to repeat
5357 * end: DROP Drop the result of "expr"
5358 *
5359 */
5360 static char_u *
5361compile_for(char_u *arg, cctx_T *cctx)
5362{
5363 char_u *p;
5364 size_t varlen;
5365 garray_T *instr = &cctx->ctx_instr;
5366 garray_T *stack = &cctx->ctx_type_stack;
5367 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005368 lvar_T *loop_lvar; // loop iteration variable
5369 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370 type_T *vartype;
5371
5372 // TODO: list of variables: "for [key, value] in dict"
5373 // parse "var"
5374 for (p = arg; eval_isnamec1(*p); ++p)
5375 ;
5376 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005377 var_lvar = lookup_local(arg, varlen, cctx);
5378 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005379 {
5380 semsg(_("E1023: variable already defined: %s"), arg);
5381 return NULL;
5382 }
5383
5384 // consume "in"
5385 p = skipwhite(p);
5386 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5387 {
5388 emsg(_(e_missing_in));
5389 return NULL;
5390 }
5391 p = skipwhite(p + 2);
5392
5393
5394 scope = new_scope(cctx, FOR_SCOPE);
5395 if (scope == NULL)
5396 return NULL;
5397
5398 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005399 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5400 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005401 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005402 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005403 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005404 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005405 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005406
5407 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005408 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5409 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005410 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005411 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005412 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005413 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005414 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005415
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005416 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005417
5418 // compile "expr", it remains on the stack until "endfor"
5419 arg = p;
5420 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005421 {
5422 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005424 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005425
5426 // now we know the type of "var"
5427 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5428 if (vartype->tt_type != VAR_LIST)
5429 {
5430 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005431 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005432 return NULL;
5433 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005434 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005435 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005436
5437 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005438 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005439
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005440 generate_FOR(cctx, loop_lvar->lv_idx);
5441 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005442
5443 return arg;
5444}
5445
5446/*
5447 * compile "endfor"
5448 */
5449 static char_u *
5450compile_endfor(char_u *arg, cctx_T *cctx)
5451{
5452 garray_T *instr = &cctx->ctx_instr;
5453 scope_T *scope = cctx->ctx_scope;
5454 forscope_T *forscope;
5455 isn_T *isn;
5456
5457 if (scope == NULL || scope->se_type != FOR_SCOPE)
5458 {
5459 emsg(_(e_for));
5460 return NULL;
5461 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005462 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005463 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005464 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005465
5466 // At end of ":for" scope jump back to the FOR instruction.
5467 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5468
5469 // Fill in the "end" label in the FOR statement so it can jump here
5470 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5471 isn->isn_arg.forloop.for_end = instr->ga_len;
5472
5473 // Fill in the "end" label any BREAK statements
5474 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5475
5476 // Below the ":for" scope drop the "expr" list from the stack.
5477 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5478 return NULL;
5479
5480 vim_free(scope);
5481
5482 return arg;
5483}
5484
5485/*
5486 * compile "while expr"
5487 *
5488 * Produces instructions:
5489 * top: EVAL expr Push result of "expr"
5490 * JUMP_IF_FALSE end jump if false
5491 * ... body ...
5492 * JUMP top Jump back to repeat
5493 * end:
5494 *
5495 */
5496 static char_u *
5497compile_while(char_u *arg, cctx_T *cctx)
5498{
5499 char_u *p = arg;
5500 garray_T *instr = &cctx->ctx_instr;
5501 scope_T *scope;
5502
5503 scope = new_scope(cctx, WHILE_SCOPE);
5504 if (scope == NULL)
5505 return NULL;
5506
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005507 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005508
5509 // compile "expr"
5510 if (compile_expr1(&p, cctx) == FAIL)
5511 return NULL;
5512
5513 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005514 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005515 JUMP_IF_FALSE, cctx) == FAIL)
5516 return FAIL;
5517
5518 return p;
5519}
5520
5521/*
5522 * compile "endwhile"
5523 */
5524 static char_u *
5525compile_endwhile(char_u *arg, cctx_T *cctx)
5526{
5527 scope_T *scope = cctx->ctx_scope;
5528
5529 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5530 {
5531 emsg(_(e_while));
5532 return NULL;
5533 }
5534 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005535 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005536
5537 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005538 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005539
5540 // Fill in the "end" label in the WHILE statement so it can jump here.
5541 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005542 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005543
5544 vim_free(scope);
5545
5546 return arg;
5547}
5548
5549/*
5550 * compile "continue"
5551 */
5552 static char_u *
5553compile_continue(char_u *arg, cctx_T *cctx)
5554{
5555 scope_T *scope = cctx->ctx_scope;
5556
5557 for (;;)
5558 {
5559 if (scope == NULL)
5560 {
5561 emsg(_(e_continue));
5562 return NULL;
5563 }
5564 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5565 break;
5566 scope = scope->se_outer;
5567 }
5568
5569 // Jump back to the FOR or WHILE instruction.
5570 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005571 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5572 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005573 return arg;
5574}
5575
5576/*
5577 * compile "break"
5578 */
5579 static char_u *
5580compile_break(char_u *arg, cctx_T *cctx)
5581{
5582 scope_T *scope = cctx->ctx_scope;
5583 endlabel_T **el;
5584
5585 for (;;)
5586 {
5587 if (scope == NULL)
5588 {
5589 emsg(_(e_break));
5590 return NULL;
5591 }
5592 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5593 break;
5594 scope = scope->se_outer;
5595 }
5596
5597 // Jump to the end of the FOR or WHILE loop.
5598 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005599 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005600 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005601 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005602 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5603 return FAIL;
5604
5605 return arg;
5606}
5607
5608/*
5609 * compile "{" start of block
5610 */
5611 static char_u *
5612compile_block(char_u *arg, cctx_T *cctx)
5613{
5614 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5615 return NULL;
5616 return skipwhite(arg + 1);
5617}
5618
5619/*
5620 * compile end of block: drop one scope
5621 */
5622 static void
5623compile_endblock(cctx_T *cctx)
5624{
5625 scope_T *scope = cctx->ctx_scope;
5626
5627 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005628 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005629 vim_free(scope);
5630}
5631
5632/*
5633 * compile "try"
5634 * Creates a new scope for the try-endtry, pointing to the first catch and
5635 * finally.
5636 * Creates another scope for the "try" block itself.
5637 * TRY instruction sets up exception handling at runtime.
5638 *
5639 * "try"
5640 * TRY -> catch1, -> finally push trystack entry
5641 * ... try block
5642 * "throw {exception}"
5643 * EVAL {exception}
5644 * THROW create exception
5645 * ... try block
5646 * " catch {expr}"
5647 * JUMP -> finally
5648 * catch1: PUSH exeception
5649 * EVAL {expr}
5650 * MATCH
5651 * JUMP nomatch -> catch2
5652 * CATCH remove exception
5653 * ... catch block
5654 * " catch"
5655 * JUMP -> finally
5656 * catch2: CATCH remove exception
5657 * ... catch block
5658 * " finally"
5659 * finally:
5660 * ... finally block
5661 * " endtry"
5662 * ENDTRY pop trystack entry, may rethrow
5663 */
5664 static char_u *
5665compile_try(char_u *arg, cctx_T *cctx)
5666{
5667 garray_T *instr = &cctx->ctx_instr;
5668 scope_T *try_scope;
5669 scope_T *scope;
5670
5671 // scope that holds the jumps that go to catch/finally/endtry
5672 try_scope = new_scope(cctx, TRY_SCOPE);
5673 if (try_scope == NULL)
5674 return NULL;
5675
5676 // "catch" is set when the first ":catch" is found.
5677 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005678 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005679 if (generate_instr(cctx, ISN_TRY) == NULL)
5680 return NULL;
5681
5682 // scope for the try block itself
5683 scope = new_scope(cctx, BLOCK_SCOPE);
5684 if (scope == NULL)
5685 return NULL;
5686
5687 return arg;
5688}
5689
5690/*
5691 * compile "catch {expr}"
5692 */
5693 static char_u *
5694compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5695{
5696 scope_T *scope = cctx->ctx_scope;
5697 garray_T *instr = &cctx->ctx_instr;
5698 char_u *p;
5699 isn_T *isn;
5700
5701 // end block scope from :try or :catch
5702 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5703 compile_endblock(cctx);
5704 scope = cctx->ctx_scope;
5705
5706 // Error if not in a :try scope
5707 if (scope == NULL || scope->se_type != TRY_SCOPE)
5708 {
5709 emsg(_(e_catch));
5710 return NULL;
5711 }
5712
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005713 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005714 {
5715 emsg(_("E1033: catch unreachable after catch-all"));
5716 return NULL;
5717 }
5718
5719 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005720 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005721 JUMP_ALWAYS, cctx) == FAIL)
5722 return NULL;
5723
5724 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005725 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005726 if (isn->isn_arg.try.try_catch == 0)
5727 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005728 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005729 {
5730 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005731 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005732 isn->isn_arg.jump.jump_where = instr->ga_len;
5733 }
5734
5735 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02005736 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005737 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005738 scope->se_u.se_try.ts_caught_all = TRUE;
5739 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005740 }
5741 else
5742 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005743 char_u *end;
5744 char_u *pat;
5745 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005746 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005747 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005749 // Push v:exception, push {expr} and MATCH
5750 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5751
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005752 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005753 if (*end != *p)
5754 {
5755 semsg(_("E1067: Separator mismatch: %s"), p);
5756 vim_free(tofree);
5757 return FAIL;
5758 }
5759 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005760 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005761 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005762 len = (int)(end - tofree);
5763 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005764 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005765 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005766 if (pat == NULL)
5767 return FAIL;
5768 if (generate_PUSHS(cctx, pat) == FAIL)
5769 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005771 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5772 return NULL;
5773
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005774 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005775 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5776 return NULL;
5777 }
5778
5779 if (generate_instr(cctx, ISN_CATCH) == NULL)
5780 return NULL;
5781
5782 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5783 return NULL;
5784 return p;
5785}
5786
5787 static char_u *
5788compile_finally(char_u *arg, cctx_T *cctx)
5789{
5790 scope_T *scope = cctx->ctx_scope;
5791 garray_T *instr = &cctx->ctx_instr;
5792 isn_T *isn;
5793
5794 // end block scope from :try or :catch
5795 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5796 compile_endblock(cctx);
5797 scope = cctx->ctx_scope;
5798
5799 // Error if not in a :try scope
5800 if (scope == NULL || scope->se_type != TRY_SCOPE)
5801 {
5802 emsg(_(e_finally));
5803 return NULL;
5804 }
5805
5806 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005807 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005808 if (isn->isn_arg.try.try_finally != 0)
5809 {
5810 emsg(_(e_finally_dup));
5811 return NULL;
5812 }
5813
5814 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005815 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005816
Bram Moolenaar585fea72020-04-02 22:33:21 +02005817 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005818 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005819 {
5820 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005821 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005822 isn->isn_arg.jump.jump_where = instr->ga_len;
5823 }
5824
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005825 // TODO: set index in ts_finally_label jumps
5826
5827 return arg;
5828}
5829
5830 static char_u *
5831compile_endtry(char_u *arg, cctx_T *cctx)
5832{
5833 scope_T *scope = cctx->ctx_scope;
5834 garray_T *instr = &cctx->ctx_instr;
5835 isn_T *isn;
5836
5837 // end block scope from :catch or :finally
5838 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5839 compile_endblock(cctx);
5840 scope = cctx->ctx_scope;
5841
5842 // Error if not in a :try scope
5843 if (scope == NULL || scope->se_type != TRY_SCOPE)
5844 {
5845 if (scope == NULL)
5846 emsg(_(e_no_endtry));
5847 else if (scope->se_type == WHILE_SCOPE)
5848 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01005849 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005850 emsg(_(e_endfor));
5851 else
5852 emsg(_(e_endif));
5853 return NULL;
5854 }
5855
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005856 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005857 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
5858 {
5859 emsg(_("E1032: missing :catch or :finally"));
5860 return NULL;
5861 }
5862
5863 // Fill in the "end" label in jumps at the end of the blocks, if not done
5864 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005865 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005866
5867 // End :catch or :finally scope: set value in ISN_TRY instruction
5868 if (isn->isn_arg.try.try_finally == 0)
5869 isn->isn_arg.try.try_finally = instr->ga_len;
5870 compile_endblock(cctx);
5871
5872 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
5873 return NULL;
5874 return arg;
5875}
5876
5877/*
5878 * compile "throw {expr}"
5879 */
5880 static char_u *
5881compile_throw(char_u *arg, cctx_T *cctx UNUSED)
5882{
5883 char_u *p = skipwhite(arg);
5884
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005885 if (compile_expr1(&p, cctx) == FAIL)
5886 return NULL;
5887 if (may_generate_2STRING(-1, cctx) == FAIL)
5888 return NULL;
5889 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5890 return NULL;
5891
5892 return p;
5893}
5894
5895/*
5896 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005897 * compile "echomsg expr"
5898 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01005899 * compile "execute expr"
5900 */
5901 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005902compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01005903{
5904 char_u *p = arg;
5905 int count = 0;
5906
5907 for (;;)
5908 {
5909 if (compile_expr1(&p, cctx) == FAIL)
5910 return NULL;
5911 ++count;
5912 p = skipwhite(p);
5913 if (ends_excmd(*p))
5914 break;
5915 }
5916
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005917 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
5918 generate_ECHO(cctx, cmdidx == CMD_echo, count);
5919 else if (cmdidx == CMD_execute)
5920 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
5921 else if (cmdidx == CMD_echomsg)
5922 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
5923 else
5924 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005925 return p;
5926}
5927
5928/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005929 * A command that is not compiled, execute with legacy code.
5930 */
5931 static char_u *
5932compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
5933{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005934 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02005935 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005936
5937 if (cctx->ctx_skip == TRUE)
5938 goto theend;
5939
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02005940 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
5941 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005942 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
5943 {
5944 // expand filename in "syntax include [@group] filename"
5945 has_expr = TRUE;
5946 eap->arg = skipwhite(eap->arg + 7);
5947 if (*eap->arg == '@')
5948 eap->arg = skiptowhite(eap->arg);
5949 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005950
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005951 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005952 {
5953 int count = 0;
5954 char_u *start = skipwhite(line);
5955
5956 // :cmd xxx`=expr1`yyy`=expr2`zzz
5957 // PUSHS ":cmd xxx"
5958 // eval expr1
5959 // PUSHS "yyy"
5960 // eval expr2
5961 // PUSHS "zzz"
5962 // EXECCONCAT 5
5963 for (;;)
5964 {
5965 if (p > start)
5966 {
5967 generate_PUSHS(cctx, vim_strnsave(start, (int)(p - start)));
5968 ++count;
5969 }
5970 p += 2;
5971 if (compile_expr1(&p, cctx) == FAIL)
5972 return NULL;
5973 may_generate_2STRING(-1, cctx);
5974 ++count;
5975 p = skipwhite(p);
5976 if (*p != '`')
5977 {
5978 emsg(_("E1083: missing backtick"));
5979 return NULL;
5980 }
5981 start = p + 1;
5982
5983 p = (char_u *)strstr((char *)start, "`=");
5984 if (p == NULL)
5985 {
5986 if (*skipwhite(start) != NUL)
5987 {
5988 generate_PUSHS(cctx, vim_strsave(start));
5989 ++count;
5990 }
5991 break;
5992 }
5993 }
5994 generate_EXECCONCAT(cctx, count);
5995 }
5996 else
5997 generate_EXEC(cctx, line);
5998
5999theend:
6000 return (char_u *)"";
6001}
6002
6003/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006004 * After ex_function() has collected all the function lines: parse and compile
6005 * the lines into instructions.
6006 * Adds the function to "def_functions".
6007 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6008 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006009 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006010 * This can be used recursively through compile_lambda(), which may reallocate
6011 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006012 */
6013 void
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006014compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006016 char_u *line = NULL;
6017 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006018 char *errormsg = NULL; // error message
6019 int had_return = FALSE;
6020 cctx_T cctx;
6021 garray_T *instr;
6022 int called_emsg_before = called_emsg;
6023 int ret = FAIL;
6024 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006025 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006027 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006028 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01006029
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006030 if (ufunc->uf_dfunc_idx >= 0)
6031 {
6032 // Redefining a function that was compiled before.
6033 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
6034
6035 // Free old instructions.
6036 delete_def_function_contents(dfunc);
6037 }
6038 else
6039 {
6040 // Add the function to "def_functions".
6041 if (ga_grow(&def_functions, 1) == FAIL)
6042 return;
6043 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006044 CLEAR_POINTER(dfunc);
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006045 dfunc->df_idx = def_functions.ga_len;
6046 ufunc->uf_dfunc_idx = dfunc->df_idx;
6047 dfunc->df_ufunc = ufunc;
6048 ++def_functions.ga_len;
6049 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006050 }
6051
Bram Moolenaara80faa82020-04-12 19:37:17 +02006052 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006053 cctx.ctx_ufunc = ufunc;
6054 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006055 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006056 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6057 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6058 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6059 cctx.ctx_type_list = &ufunc->uf_type_list;
6060 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6061 instr = &cctx.ctx_instr;
6062
6063 // Most modern script version.
6064 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6065
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006066 if (ufunc->uf_def_args.ga_len > 0)
6067 {
6068 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006069 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006070 int i;
6071 char_u *arg;
6072 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6073
6074 // Produce instructions for the default values of optional arguments.
6075 // Store the instruction index in uf_def_arg_idx[] so that we know
6076 // where to start when the function is called, depending on the number
6077 // of arguments.
6078 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6079 if (ufunc->uf_def_arg_idx == NULL)
6080 goto erret;
6081 for (i = 0; i < count; ++i)
6082 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006083 garray_T *stack = &cctx.ctx_type_stack;
6084 type_T *val_type;
6085 int arg_idx = first_def_arg + i;
6086
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006087 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6088 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006089 if (compile_expr1(&arg, &cctx) == FAIL)
6090 goto erret;
6091
6092 // If no type specified use the type of the default value.
6093 // Otherwise check that the default value type matches the
6094 // specified type.
6095 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6096 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6097 ufunc->uf_arg_types[arg_idx] = val_type;
6098 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6099 == FAIL)
6100 {
6101 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6102 arg_idx + 1);
6103 goto erret;
6104 }
6105
6106 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006107 goto erret;
6108 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006109 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6110 }
6111
6112 /*
6113 * Loop over all the lines of the function and generate instructions.
6114 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006115 for (;;)
6116 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006117 exarg_T ea;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006118 int is_ex_command = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006119
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006120 // Bail out on the first error to avoid a flood of errors and report
6121 // the right line number when inside try/catch.
6122 if (emsg_before != called_emsg)
6123 goto erret;
6124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006125 if (line != NULL && *line == '|')
6126 // the line continues after a '|'
6127 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006128 else if (line != NULL && *line != NUL
6129 && !(*line == '#' && (line == cctx.ctx_line_start
6130 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006131 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006132 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006133 goto erret;
6134 }
6135 else
6136 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006137 line = next_line_from_context(&cctx);
6138 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006139 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006140 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006141 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006142 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006143
6144 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006145 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006146 ea.cmdlinep = &line;
6147 ea.cmd = skipwhite(line);
6148
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006149 // Some things can be recognized by the first character.
6150 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006151 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006152 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006153 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006154 if (ea.cmd[1] != '{')
6155 {
6156 line = (char_u *)"";
6157 continue;
6158 }
6159 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006160
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006161 case '}':
6162 {
6163 // "}" ends a block scope
6164 scopetype_T stype = cctx.ctx_scope == NULL
6165 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006166
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006167 if (stype == BLOCK_SCOPE)
6168 {
6169 compile_endblock(&cctx);
6170 line = ea.cmd;
6171 }
6172 else
6173 {
6174 emsg(_("E1025: using } outside of a block scope"));
6175 goto erret;
6176 }
6177 if (line != NULL)
6178 line = skipwhite(ea.cmd + 1);
6179 continue;
6180 }
6181
6182 case '{':
6183 // "{" starts a block scope
6184 // "{'a': 1}->func() is something else
6185 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6186 {
6187 line = compile_block(ea.cmd, &cctx);
6188 continue;
6189 }
6190 break;
6191
6192 case ':':
6193 is_ex_command = TRUE;
6194 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006195 }
6196
6197 /*
6198 * COMMAND MODIFIERS
6199 */
6200 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6201 {
6202 if (errormsg != NULL)
6203 goto erret;
6204 // empty line or comment
6205 line = (char_u *)"";
6206 continue;
6207 }
6208
6209 // Skip ":call" to get to the function name.
6210 if (checkforcmd(&ea.cmd, "call", 3))
6211 ea.cmd = skipwhite(ea.cmd);
6212
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006213 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006214 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006215 // Assuming the command starts with a variable or function name,
6216 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
6217 // val".
6218 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
6219 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006220 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006221 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006223 int oplen;
6224 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006225
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006226 oplen = assignment_len(skipwhite(p), &heredoc);
6227 if (oplen > 0)
6228 {
6229 // Recognize an assignment if we recognize the variable
6230 // name:
6231 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006232 // "local = expr" where "local" is a local var.
6233 // "script = expr" where "script" is a script-local var.
6234 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006235 // "&opt = expr"
6236 // "$ENV = expr"
6237 // "@r = expr"
6238 if (*ea.cmd == '&'
6239 || *ea.cmd == '$'
6240 || *ea.cmd == '@'
6241 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006242 || lookup_local(ea.cmd, p - ea.cmd, &cctx) != NULL
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006243 || lookup_script(ea.cmd, p - ea.cmd) == OK
6244 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
6245 {
6246 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6247 if (line == NULL)
6248 goto erret;
6249 continue;
6250 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251 }
6252 }
6253 }
6254
6255 /*
6256 * COMMAND after range
6257 */
6258 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006259 p = find_ex_command(&ea, NULL, is_ex_command ? NULL
6260 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006261 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006262
6263 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6264 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006265 if (cctx.ctx_skip == TRUE)
6266 {
6267 line += STRLEN(line);
6268 continue;
6269 }
6270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006271 // Expression or function call.
6272 if (ea.cmdidx == CMD_eval)
6273 {
6274 p = ea.cmd;
6275 if (compile_expr1(&p, &cctx) == FAIL)
6276 goto erret;
6277
6278 // drop the return value
6279 generate_instr_drop(&cctx, ISN_DROP, 1);
6280 line = p;
6281 continue;
6282 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006283 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006284 iemsg("Command from find_ex_command() not handled");
6285 goto erret;
6286 }
6287
6288 p = skipwhite(p);
6289
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006290 if (cctx.ctx_skip == TRUE
6291 && ea.cmdidx != CMD_elseif
6292 && ea.cmdidx != CMD_else
6293 && ea.cmdidx != CMD_endif)
6294 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006295 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006296 continue;
6297 }
6298
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006299 switch (ea.cmdidx)
6300 {
6301 case CMD_def:
6302 case CMD_function:
6303 // TODO: Nested function
6304 emsg("Nested function not implemented yet");
6305 goto erret;
6306
6307 case CMD_return:
6308 line = compile_return(p, set_return_type, &cctx);
6309 had_return = TRUE;
6310 break;
6311
6312 case CMD_let:
6313 case CMD_const:
6314 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
6315 break;
6316
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006317 case CMD_unlet:
6318 case CMD_unlockvar:
6319 case CMD_lockvar:
6320 line = compile_unletlock(p, &ea, &cctx);
6321 break;
6322
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006323 case CMD_import:
6324 line = compile_import(p, &cctx);
6325 break;
6326
6327 case CMD_if:
6328 line = compile_if(p, &cctx);
6329 break;
6330 case CMD_elseif:
6331 line = compile_elseif(p, &cctx);
6332 break;
6333 case CMD_else:
6334 line = compile_else(p, &cctx);
6335 break;
6336 case CMD_endif:
6337 line = compile_endif(p, &cctx);
6338 break;
6339
6340 case CMD_while:
6341 line = compile_while(p, &cctx);
6342 break;
6343 case CMD_endwhile:
6344 line = compile_endwhile(p, &cctx);
6345 break;
6346
6347 case CMD_for:
6348 line = compile_for(p, &cctx);
6349 break;
6350 case CMD_endfor:
6351 line = compile_endfor(p, &cctx);
6352 break;
6353 case CMD_continue:
6354 line = compile_continue(p, &cctx);
6355 break;
6356 case CMD_break:
6357 line = compile_break(p, &cctx);
6358 break;
6359
6360 case CMD_try:
6361 line = compile_try(p, &cctx);
6362 break;
6363 case CMD_catch:
6364 line = compile_catch(p, &cctx);
6365 break;
6366 case CMD_finally:
6367 line = compile_finally(p, &cctx);
6368 break;
6369 case CMD_endtry:
6370 line = compile_endtry(p, &cctx);
6371 break;
6372 case CMD_throw:
6373 line = compile_throw(p, &cctx);
6374 break;
6375
6376 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006377 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006378 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006379 case CMD_echomsg:
6380 case CMD_echoerr:
6381 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006382 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006383
6384 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006385 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006386 // Not recognized, execute with do_cmdline_cmd().
6387 ea.arg = p;
6388 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006389 break;
6390 }
6391 if (line == NULL)
6392 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006393 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006394
6395 if (cctx.ctx_type_stack.ga_len < 0)
6396 {
6397 iemsg("Type stack underflow");
6398 goto erret;
6399 }
6400 }
6401
6402 if (cctx.ctx_scope != NULL)
6403 {
6404 if (cctx.ctx_scope->se_type == IF_SCOPE)
6405 emsg(_(e_endif));
6406 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6407 emsg(_(e_endwhile));
6408 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6409 emsg(_(e_endfor));
6410 else
6411 emsg(_("E1026: Missing }"));
6412 goto erret;
6413 }
6414
6415 if (!had_return)
6416 {
6417 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6418 {
6419 emsg(_("E1027: Missing return statement"));
6420 goto erret;
6421 }
6422
6423 // Return zero if there is no return at the end.
6424 generate_PUSHNR(&cctx, 0);
6425 generate_instr(&cctx, ISN_RETURN);
6426 }
6427
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006428 {
6429 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6430 + ufunc->uf_dfunc_idx;
6431 dfunc->df_deleted = FALSE;
6432 dfunc->df_instr = instr->ga_data;
6433 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006434 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006435 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006436 if (cctx.ctx_outer_used)
6437 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006438 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006439
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006440 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006441 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006442 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006443
6444 // Create a type for the function, with the return type and any
6445 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006446 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6447 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006448 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006449 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006450 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6451 argcount, &ufunc->uf_type_list);
6452 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006453 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006454 argcount + varargs,
6455 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006456 {
6457 ret = FAIL;
6458 goto erret;
6459 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006460 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6461 ufunc->uf_func_type->tt_min_argcount =
6462 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006463 if (ufunc->uf_arg_types == NULL)
6464 {
6465 int i;
6466
6467 // lambda does not have argument types.
6468 for (i = 0; i < argcount; ++i)
6469 ufunc->uf_func_type->tt_args[i] = &t_any;
6470 }
6471 else
6472 mch_memmove(ufunc->uf_func_type->tt_args,
6473 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006474 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006475 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006476 ufunc->uf_func_type->tt_args[argcount] =
6477 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006478 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6479 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006480 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006481 else
6482 // No arguments, can use a predefined type.
6483 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6484 argcount, &ufunc->uf_type_list);
6485
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006486 }
6487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006488 ret = OK;
6489
6490erret:
6491 if (ret == FAIL)
6492 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006493 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006494 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6495 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006496
6497 for (idx = 0; idx < instr->ga_len; ++idx)
6498 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006499 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006500
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006501 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006502 if (!dfunc->df_deleted)
6503 --def_functions.ga_len;
6504
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006505 while (cctx.ctx_scope != NULL)
6506 drop_scope(&cctx);
6507
Bram Moolenaar20431c92020-03-20 18:39:46 +01006508 // Don't execute this function body.
6509 ga_clear_strings(&ufunc->uf_lines);
6510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511 if (errormsg != NULL)
6512 emsg(errormsg);
6513 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006514 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006515 }
6516
6517 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006518 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006519 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006520 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006521}
6522
6523/*
6524 * Delete an instruction, free what it contains.
6525 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006526 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006527delete_instr(isn_T *isn)
6528{
6529 switch (isn->isn_type)
6530 {
6531 case ISN_EXEC:
6532 case ISN_LOADENV:
6533 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006534 case ISN_LOADB:
6535 case ISN_LOADW:
6536 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006537 case ISN_LOADOPT:
6538 case ISN_MEMBER:
6539 case ISN_PUSHEXC:
6540 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006541 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006543 case ISN_STOREB:
6544 case ISN_STOREW:
6545 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006546 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 vim_free(isn->isn_arg.string);
6548 break;
6549
6550 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006551 case ISN_STORES:
6552 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006553 break;
6554
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006555 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006556 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006557 vim_free(isn->isn_arg.unlet.ul_name);
6558 break;
6559
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006560 case ISN_STOREOPT:
6561 vim_free(isn->isn_arg.storeopt.so_name);
6562 break;
6563
6564 case ISN_PUSHBLOB: // push blob isn_arg.blob
6565 blob_unref(isn->isn_arg.blob);
6566 break;
6567
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006568 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006569#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006570 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006571#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006572 break;
6573
6574 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006575#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006576 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006577#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006578 break;
6579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006580 case ISN_UCALL:
6581 vim_free(isn->isn_arg.ufunc.cuf_name);
6582 break;
6583
6584 case ISN_2BOOL:
6585 case ISN_2STRING:
6586 case ISN_ADDBLOB:
6587 case ISN_ADDLIST:
6588 case ISN_BCALL:
6589 case ISN_CATCH:
6590 case ISN_CHECKNR:
6591 case ISN_CHECKTYPE:
6592 case ISN_COMPAREANY:
6593 case ISN_COMPAREBLOB:
6594 case ISN_COMPAREBOOL:
6595 case ISN_COMPAREDICT:
6596 case ISN_COMPAREFLOAT:
6597 case ISN_COMPAREFUNC:
6598 case ISN_COMPARELIST:
6599 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600 case ISN_COMPARESPECIAL:
6601 case ISN_COMPARESTRING:
6602 case ISN_CONCAT:
6603 case ISN_DCALL:
6604 case ISN_DROP:
6605 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006606 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006607 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006609 case ISN_EXECCONCAT:
6610 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006611 case ISN_FOR:
6612 case ISN_FUNCREF:
6613 case ISN_INDEX:
6614 case ISN_JUMP:
6615 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006616 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617 case ISN_LOADSCRIPT:
6618 case ISN_LOADREG:
6619 case ISN_LOADV:
6620 case ISN_NEGATENR:
6621 case ISN_NEWDICT:
6622 case ISN_NEWLIST:
6623 case ISN_OPNR:
6624 case ISN_OPFLOAT:
6625 case ISN_OPANY:
6626 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006627 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628 case ISN_PUSHF:
6629 case ISN_PUSHNR:
6630 case ISN_PUSHBOOL:
6631 case ISN_PUSHSPEC:
6632 case ISN_RETURN:
6633 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006634 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006635 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006636 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637 case ISN_STORESCRIPT:
6638 case ISN_THROW:
6639 case ISN_TRY:
6640 // nothing allocated
6641 break;
6642 }
6643}
6644
6645/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006646 * Free all instructions for "dfunc".
6647 */
6648 static void
6649delete_def_function_contents(dfunc_T *dfunc)
6650{
6651 int idx;
6652
6653 ga_clear(&dfunc->df_def_args_isn);
6654
6655 if (dfunc->df_instr != NULL)
6656 {
6657 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6658 delete_instr(dfunc->df_instr + idx);
6659 VIM_CLEAR(dfunc->df_instr);
6660 }
6661
6662 dfunc->df_deleted = TRUE;
6663}
6664
6665/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006666 * When a user function is deleted, delete any associated def function.
6667 */
6668 void
6669delete_def_function(ufunc_T *ufunc)
6670{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006671 if (ufunc->uf_dfunc_idx >= 0)
6672 {
6673 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6674 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006675
Bram Moolenaar20431c92020-03-20 18:39:46 +01006676 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006677 }
6678}
6679
6680#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006681/*
6682 * Free all functions defined with ":def".
6683 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006684 void
6685free_def_functions(void)
6686{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006687 int idx;
6688
6689 for (idx = 0; idx < def_functions.ga_len; ++idx)
6690 {
6691 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6692
6693 delete_def_function_contents(dfunc);
6694 }
6695
6696 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006697}
6698#endif
6699
6700
6701#endif // FEAT_EVAL