blob: de2fb96b516abfb783f1d25376fd6725401a8c9f [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)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001506 {
1507 if (type->tt_argcount != -1)
1508 {
1509 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1510
1511 if (argcount < type->tt_min_argcount - varargs)
1512 {
1513 semsg(_(e_toofewarg), "[reference]");
1514 return FAIL;
1515 }
1516 if (!varargs && argcount > type->tt_argcount)
1517 {
1518 semsg(_(e_toomanyarg), "[reference]");
1519 return FAIL;
1520 }
1521 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001522 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001523 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001524 else
1525 {
1526 semsg(_("E1085: Not a callable type: %s"), name);
1527 return FAIL;
1528 }
1529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1531 return FAIL;
1532 isn->isn_arg.pfunc.cpf_top = at_top;
1533 isn->isn_arg.pfunc.cpf_argcount = argcount;
1534
1535 stack->ga_len -= argcount; // drop the arguments
1536
1537 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001538 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001540 // If partial is above the arguments it must be cleared and replaced with
1541 // the return value.
1542 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1543 return FAIL;
1544
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545 return OK;
1546}
1547
1548/*
1549 * Generate an ISN_MEMBER instruction.
1550 */
1551 static int
1552generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1553{
1554 isn_T *isn;
1555 garray_T *stack = &cctx->ctx_type_stack;
1556 type_T *type;
1557
Bram Moolenaar080457c2020-03-03 21:53:32 +01001558 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1560 return FAIL;
1561 isn->isn_arg.string = vim_strnsave(name, (int)len);
1562
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001563 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001565 if (type->tt_type != VAR_DICT && type != &t_any)
1566 {
1567 emsg(_(e_dictreq));
1568 return FAIL;
1569 }
1570 // change dict type to dict member type
1571 if (type->tt_type == VAR_DICT)
1572 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573
1574 return OK;
1575}
1576
1577/*
1578 * Generate an ISN_ECHO instruction.
1579 */
1580 static int
1581generate_ECHO(cctx_T *cctx, int with_white, int count)
1582{
1583 isn_T *isn;
1584
Bram Moolenaar080457c2020-03-03 21:53:32 +01001585 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1587 return FAIL;
1588 isn->isn_arg.echo.echo_with_white = with_white;
1589 isn->isn_arg.echo.echo_count = count;
1590
1591 return OK;
1592}
1593
Bram Moolenaarad39c092020-02-26 18:23:43 +01001594/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001595 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001596 */
1597 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001598generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001599{
1600 isn_T *isn;
1601
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001602 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001603 return FAIL;
1604 isn->isn_arg.number = count;
1605
1606 return OK;
1607}
1608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 static int
1610generate_EXEC(cctx_T *cctx, char_u *line)
1611{
1612 isn_T *isn;
1613
Bram Moolenaar080457c2020-03-03 21:53:32 +01001614 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1616 return FAIL;
1617 isn->isn_arg.string = vim_strsave(line);
1618 return OK;
1619}
1620
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001621 static int
1622generate_EXECCONCAT(cctx_T *cctx, int count)
1623{
1624 isn_T *isn;
1625
1626 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1627 return FAIL;
1628 isn->isn_arg.number = count;
1629 return OK;
1630}
1631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632/*
1633 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001634 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001636 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1638{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 lvar_T *lvar;
1640
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001641 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 {
1643 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001644 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 }
1646
1647 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001648 return NULL;
1649 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001651 // Every local variable uses the next entry on the stack. We could re-use
1652 // the last ones when leaving a scope, but then variables used in a closure
1653 // might get overwritten. To keep things simple do not re-use stack
1654 // entries. This is less efficient, but memory is cheap these days.
1655 lvar->lv_idx = cctx->ctx_locals_count++;
1656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1658 lvar->lv_const = isConst;
1659 lvar->lv_type = type;
1660
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001661 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001662}
1663
1664/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001665 * Remove local variables above "new_top".
1666 */
1667 static void
1668unwind_locals(cctx_T *cctx, int new_top)
1669{
1670 if (cctx->ctx_locals.ga_len > new_top)
1671 {
1672 int idx;
1673 lvar_T *lvar;
1674
1675 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1676 {
1677 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1678 vim_free(lvar->lv_name);
1679 }
1680 }
1681 cctx->ctx_locals.ga_len = new_top;
1682}
1683
1684/*
1685 * Free all local variables.
1686 */
1687 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001688free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001689{
1690 unwind_locals(cctx, 0);
1691 ga_clear(&cctx->ctx_locals);
1692}
1693
1694/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 * Skip over a type definition and return a pointer to just after it.
1696 */
1697 char_u *
1698skip_type(char_u *start)
1699{
1700 char_u *p = start;
1701
1702 while (ASCII_ISALNUM(*p) || *p == '_')
1703 ++p;
1704
1705 // Skip over "<type>"; this is permissive about white space.
1706 if (*skipwhite(p) == '<')
1707 {
1708 p = skipwhite(p);
1709 p = skip_type(skipwhite(p + 1));
1710 p = skipwhite(p);
1711 if (*p == '>')
1712 ++p;
1713 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001714 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1715 {
1716 // handle func(args): type
1717 ++p;
1718 while (*p != ')' && *p != NUL)
1719 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001720 char_u *sp = p;
1721
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001722 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001723 if (p == sp)
1724 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001725 if (*p == ',')
1726 p = skipwhite(p + 1);
1727 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001728 if (*p == ')')
1729 {
1730 if (p[1] == ':')
1731 p = skip_type(skipwhite(p + 2));
1732 else
1733 p = skipwhite(p + 1);
1734 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001735 }
1736
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 return p;
1738}
1739
1740/*
1741 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001742 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743 * Returns NULL in case of failure.
1744 */
1745 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001746parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747{
1748 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001749 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750
1751 if (**arg != '<')
1752 {
1753 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001754 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755 else
1756 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001757 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 }
1759 *arg = skipwhite(*arg + 1);
1760
Bram Moolenaard77a8522020-04-03 21:59:57 +02001761 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762
1763 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001764 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001765 {
1766 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001767 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 }
1769 ++*arg;
1770
1771 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001772 return get_list_type(member_type, type_gap);
1773 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774}
1775
1776/*
1777 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001778 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 */
1780 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001781parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782{
1783 char_u *p = *arg;
1784 size_t len;
1785
1786 // skip over the first word
1787 while (ASCII_ISALNUM(*p) || *p == '_')
1788 ++p;
1789 len = p - *arg;
1790
1791 switch (**arg)
1792 {
1793 case 'a':
1794 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1795 {
1796 *arg += len;
1797 return &t_any;
1798 }
1799 break;
1800 case 'b':
1801 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1802 {
1803 *arg += len;
1804 return &t_bool;
1805 }
1806 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1807 {
1808 *arg += len;
1809 return &t_blob;
1810 }
1811 break;
1812 case 'c':
1813 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1814 {
1815 *arg += len;
1816 return &t_channel;
1817 }
1818 break;
1819 case 'd':
1820 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1821 {
1822 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001823 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 }
1825 break;
1826 case 'f':
1827 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1828 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001829#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 *arg += len;
1831 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001832#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001833 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001834 return &t_any;
1835#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836 }
1837 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1838 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001839 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001840 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001841 int argcount = -1;
1842 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001843 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001844 type_T *arg_type[MAX_FUNC_ARGS + 1];
1845
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001846 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001848 if (**arg == '(')
1849 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001850 // "func" may or may not return a value, "func()" does
1851 // not return a value.
1852 ret_type = &t_void;
1853
Bram Moolenaard77a8522020-04-03 21:59:57 +02001854 p = ++*arg;
1855 argcount = 0;
1856 while (*p != NUL && *p != ')')
1857 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001858 if (*p == '?')
1859 {
1860 if (first_optional == -1)
1861 first_optional = argcount;
1862 ++p;
1863 }
1864 else if (first_optional != -1)
1865 {
1866 emsg(_("E1007: mandatory argument after optional argument"));
1867 return &t_any;
1868 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001869 else if (STRNCMP(p, "...", 3) == 0)
1870 {
1871 flags |= TTFLAG_VARARGS;
1872 p += 3;
1873 }
1874
1875 arg_type[argcount++] = parse_type(&p, type_gap);
1876
1877 // Nothing comes after "...{type}".
1878 if (flags & TTFLAG_VARARGS)
1879 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001880
Bram Moolenaard77a8522020-04-03 21:59:57 +02001881 if (*p != ',' && *skipwhite(p) == ',')
1882 {
1883 semsg(_(e_no_white_before), ",");
1884 return &t_any;
1885 }
1886 if (*p == ',')
1887 {
1888 ++p;
1889 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001890 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001891 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001892 return &t_any;
1893 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001894 }
1895 p = skipwhite(p);
1896 if (argcount == MAX_FUNC_ARGS)
1897 {
1898 emsg(_("E740: Too many argument types"));
1899 return &t_any;
1900 }
1901 }
1902
1903 p = skipwhite(p);
1904 if (*p != ')')
1905 {
1906 emsg(_(e_missing_close));
1907 return &t_any;
1908 }
1909 *arg = p + 1;
1910 }
1911 if (**arg == ':')
1912 {
1913 // parse return type
1914 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001915 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001916 semsg(_(e_white_after), ":");
1917 *arg = skipwhite(*arg);
1918 ret_type = parse_type(arg, type_gap);
1919 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001920 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001921 type = get_func_type(ret_type, argcount, type_gap);
1922 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001923 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001924 type = alloc_func_type(ret_type, argcount, type_gap);
1925 type->tt_flags = flags;
1926 if (argcount > 0)
1927 {
1928 type->tt_argcount = argcount;
1929 type->tt_min_argcount = first_optional == -1
1930 ? argcount : first_optional;
1931 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001932 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001933 return &t_any;
1934 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001935 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001936 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001937 }
1938 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 }
1940 break;
1941 case 'j':
1942 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1943 {
1944 *arg += len;
1945 return &t_job;
1946 }
1947 break;
1948 case 'l':
1949 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1950 {
1951 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001952 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953 }
1954 break;
1955 case 'n':
1956 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1957 {
1958 *arg += len;
1959 return &t_number;
1960 }
1961 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962 case 's':
1963 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
1964 {
1965 *arg += len;
1966 return &t_string;
1967 }
1968 break;
1969 case 'v':
1970 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
1971 {
1972 *arg += len;
1973 return &t_void;
1974 }
1975 break;
1976 }
1977
1978 semsg(_("E1010: Type not recognized: %s"), *arg);
1979 return &t_any;
1980}
1981
1982/*
1983 * Check if "type1" and "type2" are exactly the same.
1984 */
1985 static int
1986equal_type(type_T *type1, type_T *type2)
1987{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02001988 int i;
1989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 if (type1->tt_type != type2->tt_type)
1991 return FALSE;
1992 switch (type1->tt_type)
1993 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001995 case VAR_ANY:
1996 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 case VAR_SPECIAL:
1998 case VAR_BOOL:
1999 case VAR_NUMBER:
2000 case VAR_FLOAT:
2001 case VAR_STRING:
2002 case VAR_BLOB:
2003 case VAR_JOB:
2004 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002005 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 case VAR_LIST:
2007 case VAR_DICT:
2008 return equal_type(type1->tt_member, type2->tt_member);
2009 case VAR_FUNC:
2010 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002011 if (!equal_type(type1->tt_member, type2->tt_member)
2012 || type1->tt_argcount != type2->tt_argcount)
2013 return FALSE;
2014 if (type1->tt_argcount < 0
2015 || type1->tt_args == NULL || type2->tt_args == NULL)
2016 return TRUE;
2017 for (i = 0; i < type1->tt_argcount; ++i)
2018 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2019 return FALSE;
2020 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 }
2022 return TRUE;
2023}
2024
2025/*
2026 * Find the common type of "type1" and "type2" and put it in "dest".
2027 * "type2" and "dest" may be the same.
2028 */
2029 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002030common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031{
2032 if (equal_type(type1, type2))
2033 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002034 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 return;
2036 }
2037
2038 if (type1->tt_type == type2->tt_type)
2039 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2041 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002042 type_T *common;
2043
Bram Moolenaard77a8522020-04-03 21:59:57 +02002044 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002045 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002046 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002047 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002048 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049 return;
2050 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002051 if (type1->tt_type == VAR_FUNC)
2052 {
2053 type_T *common;
2054
2055 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2056 if (type1->tt_argcount == type2->tt_argcount
2057 && type1->tt_argcount >= 0)
2058 {
2059 int argcount = type1->tt_argcount;
2060 int i;
2061
2062 *dest = alloc_func_type(common, argcount, type_gap);
2063 if (type1->tt_args != NULL && type2->tt_args != NULL)
2064 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002065 if (func_type_add_arg_types(*dest, argcount,
2066 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002067 for (i = 0; i < argcount; ++i)
2068 common_type(type1->tt_args[i], type2->tt_args[i],
2069 &(*dest)->tt_args[i], type_gap);
2070 }
2071 }
2072 else
2073 *dest = alloc_func_type(common, -1, type_gap);
2074 return;
2075 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076 }
2077
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002078 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079}
2080
2081 char *
2082vartype_name(vartype_T type)
2083{
2084 switch (type)
2085 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002086 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002087 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002088 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 case VAR_SPECIAL: return "special";
2090 case VAR_BOOL: return "bool";
2091 case VAR_NUMBER: return "number";
2092 case VAR_FLOAT: return "float";
2093 case VAR_STRING: return "string";
2094 case VAR_BLOB: return "blob";
2095 case VAR_JOB: return "job";
2096 case VAR_CHANNEL: return "channel";
2097 case VAR_LIST: return "list";
2098 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002099
2100 case VAR_FUNC:
2101 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002103 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104}
2105
2106/*
2107 * Return the name of a type.
2108 * The result may be in allocated memory, in which case "tofree" is set.
2109 */
2110 char *
2111type_name(type_T *type, char **tofree)
2112{
2113 char *name = vartype_name(type->tt_type);
2114
2115 *tofree = NULL;
2116 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2117 {
2118 char *member_free;
2119 char *member_name = type_name(type->tt_member, &member_free);
2120 size_t len;
2121
2122 len = STRLEN(name) + STRLEN(member_name) + 3;
2123 *tofree = alloc(len);
2124 if (*tofree != NULL)
2125 {
2126 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2127 vim_free(member_free);
2128 return *tofree;
2129 }
2130 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002131 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002132 {
2133 garray_T ga;
2134 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002135 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002136
2137 ga_init2(&ga, 1, 100);
2138 if (ga_grow(&ga, 20) == FAIL)
2139 return "[unknown]";
2140 *tofree = ga.ga_data;
2141 STRCPY(ga.ga_data, "func(");
2142 ga.ga_len += 5;
2143
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002144 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002145 {
2146 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002147 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002148 int len;
2149
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002150 if (type->tt_args == NULL)
2151 arg_type = "[unknown]";
2152 else
2153 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002154 if (i > 0)
2155 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002156 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002157 ga.ga_len += 2;
2158 }
2159 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002160 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002161 {
2162 vim_free(arg_free);
2163 return "[unknown]";
2164 }
2165 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002166 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002167 {
2168 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2169 ga.ga_len += 3;
2170 }
2171 else if (i >= type->tt_min_argcount)
2172 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002173 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002174 ga.ga_len += len;
2175 vim_free(arg_free);
2176 }
2177
2178 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002179 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002180 else
2181 {
2182 char *ret_free;
2183 char *ret_name = type_name(type->tt_member, &ret_free);
2184 int len;
2185
2186 len = (int)STRLEN(ret_name) + 4;
2187 if (ga_grow(&ga, len) == FAIL)
2188 {
2189 vim_free(ret_free);
2190 return "[unknown]";
2191 }
2192 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002193 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2194 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002195 vim_free(ret_free);
2196 }
2197 return ga.ga_data;
2198 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199
2200 return name;
2201}
2202
2203/*
2204 * Find "name" in script-local items of script "sid".
2205 * Returns the index in "sn_var_vals" if found.
2206 * If found but not in "sn_var_vals" returns -1.
2207 * If not found returns -2.
2208 */
2209 int
2210get_script_item_idx(int sid, char_u *name, int check_writable)
2211{
2212 hashtab_T *ht;
2213 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002214 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 int idx;
2216
2217 // First look the name up in the hashtable.
2218 if (sid <= 0 || sid > script_items.ga_len)
2219 return -1;
2220 ht = &SCRIPT_VARS(sid);
2221 di = find_var_in_ht(ht, 0, name, TRUE);
2222 if (di == NULL)
2223 return -2;
2224
2225 // Now find the svar_T index in sn_var_vals.
2226 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2227 {
2228 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2229
2230 if (sv->sv_tv == &di->di_tv)
2231 {
2232 if (check_writable && sv->sv_const)
2233 semsg(_(e_readonlyvar), name);
2234 return idx;
2235 }
2236 }
2237 return -1;
2238}
2239
2240/*
2241 * Find "name" in imported items of the current script/
2242 */
2243 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002244find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002246 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247 int idx;
2248
2249 if (cctx != NULL)
2250 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2251 {
2252 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2253 + idx;
2254
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002255 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2256 : STRLEN(import->imp_name) == len
2257 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 return import;
2259 }
2260
2261 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2262 {
2263 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2264
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002265 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2266 : STRLEN(import->imp_name) == len
2267 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 return import;
2269 }
2270 return NULL;
2271}
2272
2273/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002274 * Free all imported variables.
2275 */
2276 static void
2277free_imported(cctx_T *cctx)
2278{
2279 int idx;
2280
2281 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2282 {
2283 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2284
2285 vim_free(import->imp_name);
2286 }
2287 ga_clear(&cctx->ctx_imports);
2288}
2289
2290/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002291 * Get the next line of the function from "cctx".
2292 * Returns NULL when at the end.
2293 */
2294 static char_u *
2295next_line_from_context(cctx_T *cctx)
2296{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002297 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002298
2299 do
2300 {
2301 ++cctx->ctx_lnum;
2302 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002303 {
2304 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002305 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002306 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002307 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002308 cctx->ctx_line_start = line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002309 SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum
2310 + cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002311 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002312 return line;
2313}
2314
2315/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002316 * Return TRUE if "p" points at a "#" but not at "#{".
2317 */
2318 static int
2319comment_start(char_u *p)
2320{
2321 return p[0] == '#' && p[1] != '{';
2322}
2323
2324/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002325 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002326 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002327 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2328 */
2329 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002330may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002331{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002332 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002333 {
2334 char_u *next = next_line_from_context(cctx);
2335
2336 if (next == NULL)
2337 return FAIL;
2338 *arg = skipwhite(next);
2339 }
2340 return OK;
2341}
2342
2343/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002344 * Generate an instruction to load script-local variable "name", without the
2345 * leading "s:".
2346 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 */
2348 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002349compile_load_scriptvar(
2350 cctx_T *cctx,
2351 char_u *name, // variable NUL terminated
2352 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002353 char_u **end, // end of variable
2354 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002356 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002357 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2358 imported_T *import;
2359
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002360 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002362 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002363 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2364 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 }
2366 if (idx >= 0)
2367 {
2368 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2369
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002370 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 current_sctx.sc_sid, idx, sv->sv_type);
2372 return OK;
2373 }
2374
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002375 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376 if (import != NULL)
2377 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002378 if (import->imp_all)
2379 {
2380 char_u *p = skipwhite(*end);
2381 int name_len;
2382 ufunc_T *ufunc;
2383 type_T *type;
2384
2385 // Used "import * as Name", need to lookup the member.
2386 if (*p != '.')
2387 {
2388 semsg(_("E1060: expected dot after name: %s"), start);
2389 return FAIL;
2390 }
2391 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002392 if (VIM_ISWHITE(*p))
2393 {
2394 emsg(_("E1074: no white space allowed after dot"));
2395 return FAIL;
2396 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002397
2398 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2399 // TODO: what if it is a function?
2400 if (idx < 0)
2401 return FAIL;
2402 *end = p;
2403
2404 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2405 import->imp_sid,
2406 idx,
2407 type);
2408 }
2409 else
2410 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002411 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002412 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2413 import->imp_sid,
2414 import->imp_var_vals_idx,
2415 import->imp_type);
2416 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 return OK;
2418 }
2419
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002420 if (error)
2421 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 return FAIL;
2423}
2424
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002425 static int
2426generate_funcref(cctx_T *cctx, char_u *name)
2427{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002428 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002429
2430 if (ufunc == NULL)
2431 return FAIL;
2432
2433 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2434}
2435
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436/*
2437 * Compile a variable name into a load instruction.
2438 * "end" points to just after the name.
2439 * When "error" is FALSE do not give an error when not found.
2440 */
2441 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002442compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443{
2444 type_T *type;
2445 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002446 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002448 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449
2450 if (*(*arg + 1) == ':')
2451 {
2452 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002453 if (end <= *arg + 2)
2454 name = vim_strsave((char_u *)"[empty]");
2455 else
2456 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457 if (name == NULL)
2458 return FAIL;
2459
2460 if (**arg == 'v')
2461 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002462 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 }
2464 else if (**arg == 'g')
2465 {
2466 // Global variables can be defined later, thus we don't check if it
2467 // exists, give error at runtime.
2468 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2469 }
2470 else if (**arg == 's')
2471 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002472 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002474 else if (**arg == 'b')
2475 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002476 // Buffer-local variables can be defined later, thus we don't check
2477 // if it exists, give error at runtime.
2478 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002479 }
2480 else if (**arg == 'w')
2481 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002482 // Window-local variables can be defined later, thus we don't check
2483 // if it exists, give error at runtime.
2484 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002485 }
2486 else if (**arg == 't')
2487 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002488 // Tabpage-local variables can be defined later, thus we don't
2489 // check if it exists, give error at runtime.
2490 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002491 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 else
2493 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002494 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 goto theend;
2496 }
2497 }
2498 else
2499 {
2500 size_t len = end - *arg;
2501 int idx;
2502 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002503 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504
2505 name = vim_strnsave(*arg, end - *arg);
2506 if (name == NULL)
2507 return FAIL;
2508
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002509 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002511 if (!gen_load_outer)
2512 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 }
2514 else
2515 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002516 lvar_T *lvar = lookup_local(*arg, len, cctx);
2517
2518 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002520 type = lvar->lv_type;
2521 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002522 if (lvar->lv_from_outer)
2523 gen_load_outer = TRUE;
2524 else
2525 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 }
2527 else
2528 {
2529 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2530 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2531 res = generate_PUSHBOOL(cctx, **arg == 't'
2532 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002533 else
2534 {
2535 // "var" can be script-local even without using "s:" if it
2536 // already exists.
2537 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2538 == SCRIPT_VERSION_VIM9
2539 || lookup_script(*arg, len) == OK)
2540 res = compile_load_scriptvar(cctx, name, *arg, &end,
2541 FALSE);
2542
2543 // When the name starts with an uppercase letter or "x:" it
2544 // can be a user defined function.
2545 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2546 res = generate_funcref(cctx, name);
2547 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 }
2549 }
2550 if (gen_load)
2551 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002552 if (gen_load_outer)
2553 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 }
2555
2556 *arg = end;
2557
2558theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002559 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 semsg(_(e_var_notfound), name);
2561 vim_free(name);
2562 return res;
2563}
2564
2565/*
2566 * Compile the argument expressions.
2567 * "arg" points to just after the "(" and is advanced to after the ")"
2568 */
2569 static int
2570compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2571{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002572 char_u *p = *arg;
2573 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574
Bram Moolenaare6085c52020-04-12 20:19:16 +02002575 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002577 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002578 {
2579 p = next_line_from_context(cctx);
2580 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002581 goto failret;
2582 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002583 p = skipwhite(p);
2584 }
2585 if (*p == ')')
2586 {
2587 *arg = p + 1;
2588 return OK;
2589 }
2590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 if (compile_expr1(&p, cctx) == FAIL)
2592 return FAIL;
2593 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002594
2595 if (*p != ',' && *skipwhite(p) == ',')
2596 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002597 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002598 p = skipwhite(p);
2599 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002601 {
2602 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002603 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002604 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002605 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002606 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002607 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002609failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002610 emsg(_(e_missing_close));
2611 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612}
2613
2614/*
2615 * Compile a function call: name(arg1, arg2)
2616 * "arg" points to "name", "arg + varlen" to the "(".
2617 * "argcount_init" is 1 for "value->method()"
2618 * Instructions:
2619 * EVAL arg1
2620 * EVAL arg2
2621 * BCALL / DCALL / UCALL
2622 */
2623 static int
2624compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2625{
2626 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002627 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 int argcount = argcount_init;
2629 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002630 char_u fname_buf[FLEN_FIXED + 1];
2631 char_u *tofree = NULL;
2632 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002634 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635
2636 if (varlen >= sizeof(namebuf))
2637 {
2638 semsg(_("E1011: name too long: %s"), name);
2639 return FAIL;
2640 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002641 vim_strncpy(namebuf, *arg, varlen);
2642 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643
2644 *arg = skipwhite(*arg + varlen + 1);
2645 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002646 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002648 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 {
2650 int idx;
2651
2652 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002653 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002655 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002656 else
2657 semsg(_(e_unknownfunc), namebuf);
2658 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 }
2660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002662 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002664 {
2665 res = generate_CALL(cctx, ufunc, argcount);
2666 goto theend;
2667 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668
2669 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002670 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002672 if (STRNCMP(namebuf, "g:", 2) != 0
2673 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002674 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002675 garray_T *stack = &cctx->ctx_type_stack;
2676 type_T *type;
2677
2678 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2679 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002680 goto theend;
2681 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002683 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002684 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002685 if (STRNCMP(namebuf, "g:", 2) == 0)
2686 res = generate_UCALL(cctx, name, argcount);
2687 else
2688 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002689
2690theend:
2691 vim_free(tofree);
2692 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693}
2694
2695// like NAMESPACE_CHAR but with 'a' and 'l'.
2696#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2697
2698/*
2699 * Find the end of a variable or function name. Unlike find_name_end() this
2700 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002701 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 * Return a pointer to just after the name. Equal to "arg" if there is no
2703 * valid name.
2704 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002705 static char_u *
2706to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707{
2708 char_u *p;
2709
2710 // Quick check for valid starting character.
2711 if (!eval_isnamec1(*arg))
2712 return arg;
2713
2714 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2715 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2716 // and can be used in slice "[n:]".
2717 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002718 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2720 break;
2721 return p;
2722}
2723
2724/*
2725 * Like to_name_end() but also skip over a list or dict constant.
2726 */
2727 char_u *
2728to_name_const_end(char_u *arg)
2729{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002730 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731 typval_T rettv;
2732
2733 if (p == arg && *arg == '[')
2734 {
2735
2736 // Can be "[1, 2, 3]->Func()".
2737 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2738 p = arg;
2739 }
2740 else if (p == arg && *arg == '#' && arg[1] == '{')
2741 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002742 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 ++p;
2744 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2745 p = arg;
2746 }
2747 else if (p == arg && *arg == '{')
2748 {
2749 int ret = get_lambda_tv(&p, &rettv, FALSE);
2750
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002751 // Can be "{x -> ret}()".
2752 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 if (ret == NOTDONE)
2754 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2755 if (ret != OK)
2756 p = arg;
2757 }
2758
2759 return p;
2760}
2761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762/*
2763 * parse a list: [expr, expr]
2764 * "*arg" points to the '['.
2765 */
2766 static int
2767compile_list(char_u **arg, cctx_T *cctx)
2768{
2769 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002770 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 int count = 0;
2772
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002773 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002775 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002776 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002777 p = next_line_from_context(cctx);
2778 if (p == NULL)
2779 {
2780 semsg(_(e_list_end), *arg);
2781 return FAIL;
2782 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002783 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002784 p = skipwhite(p);
2785 }
2786 if (*p == ']')
2787 {
2788 ++p;
2789 // Allow for following comment, after at least one space.
2790 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
2791 p += STRLEN(p);
2792 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002793 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 if (compile_expr1(&p, cctx) == FAIL)
2795 break;
2796 ++count;
2797 if (*p == ',')
2798 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002799 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 p = skipwhite(p);
2801 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002802 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803
2804 generate_NEWLIST(cctx, count);
2805 return OK;
2806}
2807
2808/*
2809 * parse a lambda: {arg, arg -> expr}
2810 * "*arg" points to the '{'.
2811 */
2812 static int
2813compile_lambda(char_u **arg, cctx_T *cctx)
2814{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 typval_T rettv;
2816 ufunc_T *ufunc;
2817
2818 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002819 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002821
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002823 ++ufunc->uf_refcount;
2824 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002825 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826
2827 // The function will have one line: "return {expr}".
2828 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002829 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830
2831 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002832 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 return FAIL;
2834}
2835
2836/*
2837 * Compile a lamda call: expr->{lambda}(args)
2838 * "arg" points to the "{".
2839 */
2840 static int
2841compile_lambda_call(char_u **arg, cctx_T *cctx)
2842{
2843 ufunc_T *ufunc;
2844 typval_T rettv;
2845 int argcount = 1;
2846 int ret = FAIL;
2847
2848 // Get the funcref in "rettv".
2849 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2850 return FAIL;
2851
2852 if (**arg != '(')
2853 {
2854 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002855 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 else
2857 semsg(_(e_missing_paren), "lambda");
2858 clear_tv(&rettv);
2859 return FAIL;
2860 }
2861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 ufunc = rettv.vval.v_partial->pt_func;
2863 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002864 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002865 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002866
2867 // The function will have one line: "return {expr}".
2868 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002869 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870
2871 // compile the arguments
2872 *arg = skipwhite(*arg + 1);
2873 if (compile_arguments(arg, cctx, &argcount) == OK)
2874 // call the compiled function
2875 ret = generate_CALL(cctx, ufunc, argcount);
2876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 return ret;
2878}
2879
2880/*
2881 * parse a dict: {'key': val} or #{key: val}
2882 * "*arg" points to the '{'.
2883 */
2884 static int
2885compile_dict(char_u **arg, cctx_T *cctx, int literal)
2886{
2887 garray_T *instr = &cctx->ctx_instr;
2888 int count = 0;
2889 dict_T *d = dict_alloc();
2890 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002891 char_u *whitep = *arg;
2892 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893
2894 if (d == NULL)
2895 return FAIL;
2896 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002897 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898 {
2899 char_u *key = NULL;
2900
Bram Moolenaar2c330432020-04-13 14:41:35 +02002901 while (**arg == NUL || (literal && **arg == '"')
2902 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002903 {
2904 *arg = next_line_from_context(cctx);
2905 if (*arg == NULL)
2906 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002907 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002908 *arg = skipwhite(*arg);
2909 }
2910
2911 if (**arg == '}')
2912 break;
2913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 if (literal)
2915 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002916 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917
Bram Moolenaar2c330432020-04-13 14:41:35 +02002918 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 {
2920 semsg(_("E1014: Invalid key: %s"), *arg);
2921 return FAIL;
2922 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002923 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 if (generate_PUSHS(cctx, key) == FAIL)
2925 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002926 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 }
2928 else
2929 {
2930 isn_T *isn;
2931
2932 if (compile_expr1(arg, cctx) == FAIL)
2933 return FAIL;
2934 // TODO: check type is string
2935 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2936 if (isn->isn_type == ISN_PUSHS)
2937 key = isn->isn_arg.string;
2938 }
2939
2940 // Check for duplicate keys, if using string keys.
2941 if (key != NULL)
2942 {
2943 item = dict_find(d, key, -1);
2944 if (item != NULL)
2945 {
2946 semsg(_(e_duplicate_key), key);
2947 goto failret;
2948 }
2949 item = dictitem_alloc(key);
2950 if (item != NULL)
2951 {
2952 item->di_tv.v_type = VAR_UNKNOWN;
2953 item->di_tv.v_lock = 0;
2954 if (dict_add(d, item) == FAIL)
2955 dictitem_free(item);
2956 }
2957 }
2958
2959 *arg = skipwhite(*arg);
2960 if (**arg != ':')
2961 {
2962 semsg(_(e_missing_dict_colon), *arg);
2963 return FAIL;
2964 }
2965
Bram Moolenaar2c330432020-04-13 14:41:35 +02002966 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002968 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002969 {
2970 *arg = next_line_from_context(cctx);
2971 if (*arg == NULL)
2972 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002973 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002974 *arg = skipwhite(*arg);
2975 }
2976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 if (compile_expr1(arg, cctx) == FAIL)
2978 return FAIL;
2979 ++count;
2980
Bram Moolenaar2c330432020-04-13 14:41:35 +02002981 whitep = *arg;
2982 p = skipwhite(*arg);
2983 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002984 {
2985 *arg = next_line_from_context(cctx);
2986 if (*arg == NULL)
2987 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002988 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002989 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002990 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002991 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 if (**arg == '}')
2993 break;
2994 if (**arg != ',')
2995 {
2996 semsg(_(e_missing_dict_comma), *arg);
2997 goto failret;
2998 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002999 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 *arg = skipwhite(*arg + 1);
3001 }
3002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 *arg = *arg + 1;
3004
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003005 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003006 p = skipwhite(*arg);
3007 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003008 *arg += STRLEN(*arg);
3009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 dict_unref(d);
3011 return generate_NEWDICT(cctx, count);
3012
3013failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003014 if (*arg == NULL)
3015 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 dict_unref(d);
3017 return FAIL;
3018}
3019
3020/*
3021 * Compile "&option".
3022 */
3023 static int
3024compile_get_option(char_u **arg, cctx_T *cctx)
3025{
3026 typval_T rettv;
3027 char_u *start = *arg;
3028 int ret;
3029
3030 // parse the option and get the current value to get the type.
3031 rettv.v_type = VAR_UNKNOWN;
3032 ret = get_option_tv(arg, &rettv, TRUE);
3033 if (ret == OK)
3034 {
3035 // include the '&' in the name, get_option_tv() expects it.
3036 char_u *name = vim_strnsave(start, *arg - start);
3037 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3038
3039 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3040 vim_free(name);
3041 }
3042 clear_tv(&rettv);
3043
3044 return ret;
3045}
3046
3047/*
3048 * Compile "$VAR".
3049 */
3050 static int
3051compile_get_env(char_u **arg, cctx_T *cctx)
3052{
3053 char_u *start = *arg;
3054 int len;
3055 int ret;
3056 char_u *name;
3057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 ++*arg;
3059 len = get_env_len(arg);
3060 if (len == 0)
3061 {
3062 semsg(_(e_syntax_at), start - 1);
3063 return FAIL;
3064 }
3065
3066 // include the '$' in the name, get_env_tv() expects it.
3067 name = vim_strnsave(start, len + 1);
3068 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3069 vim_free(name);
3070 return ret;
3071}
3072
3073/*
3074 * Compile "@r".
3075 */
3076 static int
3077compile_get_register(char_u **arg, cctx_T *cctx)
3078{
3079 int ret;
3080
3081 ++*arg;
3082 if (**arg == NUL)
3083 {
3084 semsg(_(e_syntax_at), *arg - 1);
3085 return FAIL;
3086 }
3087 if (!valid_yank_reg(**arg, TRUE))
3088 {
3089 emsg_invreg(**arg);
3090 return FAIL;
3091 }
3092 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3093 ++*arg;
3094 return ret;
3095}
3096
3097/*
3098 * Apply leading '!', '-' and '+' to constant "rettv".
3099 */
3100 static int
3101apply_leader(typval_T *rettv, char_u *start, char_u *end)
3102{
3103 char_u *p = end;
3104
3105 // this works from end to start
3106 while (p > start)
3107 {
3108 --p;
3109 if (*p == '-' || *p == '+')
3110 {
3111 // only '-' has an effect, for '+' we only check the type
3112#ifdef FEAT_FLOAT
3113 if (rettv->v_type == VAR_FLOAT)
3114 {
3115 if (*p == '-')
3116 rettv->vval.v_float = -rettv->vval.v_float;
3117 }
3118 else
3119#endif
3120 {
3121 varnumber_T val;
3122 int error = FALSE;
3123
3124 // tv_get_number_chk() accepts a string, but we don't want that
3125 // here
3126 if (check_not_string(rettv) == FAIL)
3127 return FAIL;
3128 val = tv_get_number_chk(rettv, &error);
3129 clear_tv(rettv);
3130 if (error)
3131 return FAIL;
3132 if (*p == '-')
3133 val = -val;
3134 rettv->v_type = VAR_NUMBER;
3135 rettv->vval.v_number = val;
3136 }
3137 }
3138 else
3139 {
3140 int v = tv2bool(rettv);
3141
3142 // '!' is permissive in the type.
3143 clear_tv(rettv);
3144 rettv->v_type = VAR_BOOL;
3145 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3146 }
3147 }
3148 return OK;
3149}
3150
3151/*
3152 * Recognize v: variables that are constants and set "rettv".
3153 */
3154 static void
3155get_vim_constant(char_u **arg, typval_T *rettv)
3156{
3157 if (STRNCMP(*arg, "v:true", 6) == 0)
3158 {
3159 rettv->v_type = VAR_BOOL;
3160 rettv->vval.v_number = VVAL_TRUE;
3161 *arg += 6;
3162 }
3163 else if (STRNCMP(*arg, "v:false", 7) == 0)
3164 {
3165 rettv->v_type = VAR_BOOL;
3166 rettv->vval.v_number = VVAL_FALSE;
3167 *arg += 7;
3168 }
3169 else if (STRNCMP(*arg, "v:null", 6) == 0)
3170 {
3171 rettv->v_type = VAR_SPECIAL;
3172 rettv->vval.v_number = VVAL_NULL;
3173 *arg += 6;
3174 }
3175 else if (STRNCMP(*arg, "v:none", 6) == 0)
3176 {
3177 rettv->v_type = VAR_SPECIAL;
3178 rettv->vval.v_number = VVAL_NONE;
3179 *arg += 6;
3180 }
3181}
3182
3183/*
3184 * Compile code to apply '-', '+' and '!'.
3185 */
3186 static int
3187compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3188{
3189 char_u *p = end;
3190
3191 // this works from end to start
3192 while (p > start)
3193 {
3194 --p;
3195 if (*p == '-' || *p == '+')
3196 {
3197 int negate = *p == '-';
3198 isn_T *isn;
3199
3200 // TODO: check type
3201 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3202 {
3203 --p;
3204 if (*p == '-')
3205 negate = !negate;
3206 }
3207 // only '-' has an effect, for '+' we only check the type
3208 if (negate)
3209 isn = generate_instr(cctx, ISN_NEGATENR);
3210 else
3211 isn = generate_instr(cctx, ISN_CHECKNR);
3212 if (isn == NULL)
3213 return FAIL;
3214 }
3215 else
3216 {
3217 int invert = TRUE;
3218
3219 while (p > start && p[-1] == '!')
3220 {
3221 --p;
3222 invert = !invert;
3223 }
3224 if (generate_2BOOL(cctx, invert) == FAIL)
3225 return FAIL;
3226 }
3227 }
3228 return OK;
3229}
3230
3231/*
3232 * Compile whatever comes after "name" or "name()".
3233 */
3234 static int
3235compile_subscript(
3236 char_u **arg,
3237 cctx_T *cctx,
3238 char_u **start_leader,
3239 char_u *end_leader)
3240{
3241 for (;;)
3242 {
3243 if (**arg == '(')
3244 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003245 garray_T *stack = &cctx->ctx_type_stack;
3246 type_T *type;
3247 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248
3249 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003250 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 *arg = skipwhite(*arg + 1);
3253 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3254 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003255 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 return FAIL;
3257 }
3258 else if (**arg == '-' && (*arg)[1] == '>')
3259 {
3260 char_u *p;
3261
3262 // something->method()
3263 // Apply the '!', '-' and '+' first:
3264 // -1.0->func() works like (-1.0)->func()
3265 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3266 return FAIL;
3267 *start_leader = end_leader; // don't apply again later
3268
3269 *arg = skipwhite(*arg + 2);
3270 if (**arg == '{')
3271 {
3272 // lambda call: list->{lambda}
3273 if (compile_lambda_call(arg, cctx) == FAIL)
3274 return FAIL;
3275 }
3276 else
3277 {
3278 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003279 p = *arg;
3280 if (ASCII_ISALPHA(*p) && p[1] == ':')
3281 p += 2;
3282 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 ;
3284 if (*p != '(')
3285 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003286 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 return FAIL;
3288 }
3289 // TODO: base value may not be the first argument
3290 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
3291 return FAIL;
3292 }
3293 }
3294 else if (**arg == '[')
3295 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01003296 garray_T *stack;
3297 type_T **typep;
3298
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 // list index: list[123]
3300 // TODO: more arguments
3301 // TODO: dict member dict['name']
3302 *arg = skipwhite(*arg + 1);
3303 if (compile_expr1(arg, cctx) == FAIL)
3304 return FAIL;
3305
3306 if (**arg != ']')
3307 {
3308 emsg(_(e_missbrac));
3309 return FAIL;
3310 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003311 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312
3313 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3314 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003315 stack = &cctx->ctx_type_stack;
3316 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
3317 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
3318 {
3319 emsg(_(e_listreq));
3320 return FAIL;
3321 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01003322 if ((*typep)->tt_type == VAR_LIST)
3323 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 }
3325 else if (**arg == '.' && (*arg)[1] != '.')
3326 {
3327 char_u *p;
3328
3329 ++*arg;
3330 p = *arg;
3331 // dictionary member: dict.name
3332 if (eval_isnamec1(*p))
3333 while (eval_isnamec(*p))
3334 MB_PTR_ADV(p);
3335 if (p == *arg)
3336 {
3337 semsg(_(e_syntax_at), *arg);
3338 return FAIL;
3339 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
3341 return FAIL;
3342 *arg = p;
3343 }
3344 else
3345 break;
3346 }
3347
3348 // TODO - see handle_subscript():
3349 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3350 // Don't do this when "Func" is already a partial that was bound
3351 // explicitly (pt_auto is FALSE).
3352
3353 return OK;
3354}
3355
3356/*
3357 * Compile an expression at "*p" and add instructions to "instr".
3358 * "p" is advanced until after the expression, skipping white space.
3359 *
3360 * This is the equivalent of eval1(), eval2(), etc.
3361 */
3362
3363/*
3364 * number number constant
3365 * 0zFFFFFFFF Blob constant
3366 * "string" string constant
3367 * 'string' literal string constant
3368 * &option-name option value
3369 * @r register contents
3370 * identifier variable value
3371 * function() function call
3372 * $VAR environment variable
3373 * (expression) nested expression
3374 * [expr, expr] List
3375 * {key: val, key: val} Dictionary
3376 * #{key: val, key: val} Dictionary with literal keys
3377 *
3378 * Also handle:
3379 * ! in front logical NOT
3380 * - in front unary minus
3381 * + in front unary plus (ignored)
3382 * trailing (arg) funcref/partial call
3383 * trailing [] subscript in String or List
3384 * trailing .name entry in Dictionary
3385 * trailing ->name() method call
3386 */
3387 static int
3388compile_expr7(char_u **arg, cctx_T *cctx)
3389{
3390 typval_T rettv;
3391 char_u *start_leader, *end_leader;
3392 int ret = OK;
3393
3394 /*
3395 * Skip '!', '-' and '+' characters. They are handled later.
3396 */
3397 start_leader = *arg;
3398 while (**arg == '!' || **arg == '-' || **arg == '+')
3399 *arg = skipwhite(*arg + 1);
3400 end_leader = *arg;
3401
3402 rettv.v_type = VAR_UNKNOWN;
3403 switch (**arg)
3404 {
3405 /*
3406 * Number constant.
3407 */
3408 case '0': // also for blob starting with 0z
3409 case '1':
3410 case '2':
3411 case '3':
3412 case '4':
3413 case '5':
3414 case '6':
3415 case '7':
3416 case '8':
3417 case '9':
3418 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
3419 return FAIL;
3420 break;
3421
3422 /*
3423 * String constant: "string".
3424 */
3425 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
3426 return FAIL;
3427 break;
3428
3429 /*
3430 * Literal string constant: 'str''ing'.
3431 */
3432 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
3433 return FAIL;
3434 break;
3435
3436 /*
3437 * Constant Vim variable.
3438 */
3439 case 'v': get_vim_constant(arg, &rettv);
3440 ret = NOTDONE;
3441 break;
3442
3443 /*
3444 * List: [expr, expr]
3445 */
3446 case '[': ret = compile_list(arg, cctx);
3447 break;
3448
3449 /*
3450 * Dictionary: #{key: val, key: val}
3451 */
3452 case '#': if ((*arg)[1] == '{')
3453 {
3454 ++*arg;
3455 ret = compile_dict(arg, cctx, TRUE);
3456 }
3457 else
3458 ret = NOTDONE;
3459 break;
3460
3461 /*
3462 * Lambda: {arg, arg -> expr}
3463 * Dictionary: {'key': val, 'key': val}
3464 */
3465 case '{': {
3466 char_u *start = skipwhite(*arg + 1);
3467
3468 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003469 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003471 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 if (ret != FAIL && *start == '>')
3473 ret = compile_lambda(arg, cctx);
3474 else
3475 ret = compile_dict(arg, cctx, FALSE);
3476 }
3477 break;
3478
3479 /*
3480 * Option value: &name
3481 */
3482 case '&': ret = compile_get_option(arg, cctx);
3483 break;
3484
3485 /*
3486 * Environment variable: $VAR.
3487 */
3488 case '$': ret = compile_get_env(arg, cctx);
3489 break;
3490
3491 /*
3492 * Register contents: @r.
3493 */
3494 case '@': ret = compile_get_register(arg, cctx);
3495 break;
3496 /*
3497 * nested expression: (expression).
3498 */
3499 case '(': *arg = skipwhite(*arg + 1);
3500 ret = compile_expr1(arg, cctx); // recursive!
3501 *arg = skipwhite(*arg);
3502 if (**arg == ')')
3503 ++*arg;
3504 else if (ret == OK)
3505 {
3506 emsg(_(e_missing_close));
3507 ret = FAIL;
3508 }
3509 break;
3510
3511 default: ret = NOTDONE;
3512 break;
3513 }
3514 if (ret == FAIL)
3515 return FAIL;
3516
3517 if (rettv.v_type != VAR_UNKNOWN)
3518 {
3519 // apply the '!', '-' and '+' before the constant
3520 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
3521 {
3522 clear_tv(&rettv);
3523 return FAIL;
3524 }
3525 start_leader = end_leader; // don't apply again below
3526
3527 // push constant
3528 switch (rettv.v_type)
3529 {
3530 case VAR_BOOL:
3531 generate_PUSHBOOL(cctx, rettv.vval.v_number);
3532 break;
3533 case VAR_SPECIAL:
3534 generate_PUSHSPEC(cctx, rettv.vval.v_number);
3535 break;
3536 case VAR_NUMBER:
3537 generate_PUSHNR(cctx, rettv.vval.v_number);
3538 break;
3539#ifdef FEAT_FLOAT
3540 case VAR_FLOAT:
3541 generate_PUSHF(cctx, rettv.vval.v_float);
3542 break;
3543#endif
3544 case VAR_BLOB:
3545 generate_PUSHBLOB(cctx, rettv.vval.v_blob);
3546 rettv.vval.v_blob = NULL;
3547 break;
3548 case VAR_STRING:
3549 generate_PUSHS(cctx, rettv.vval.v_string);
3550 rettv.vval.v_string = NULL;
3551 break;
3552 default:
3553 iemsg("constant type missing");
3554 return FAIL;
3555 }
3556 }
3557 else if (ret == NOTDONE)
3558 {
3559 char_u *p;
3560 int r;
3561
3562 if (!eval_isnamec1(**arg))
3563 {
3564 semsg(_("E1015: Name expected: %s"), *arg);
3565 return FAIL;
3566 }
3567
3568 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003569 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 if (*p == '(')
3571 r = compile_call(arg, p - *arg, cctx, 0);
3572 else
3573 r = compile_load(arg, p, cctx, TRUE);
3574 if (r == FAIL)
3575 return FAIL;
3576 }
3577
3578 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
3579 return FAIL;
3580
3581 // Now deal with prefixed '-', '+' and '!', if not done already.
3582 return compile_leader(cctx, start_leader, end_leader);
3583}
3584
3585/*
3586 * * number multiplication
3587 * / number division
3588 * % number modulo
3589 */
3590 static int
3591compile_expr6(char_u **arg, cctx_T *cctx)
3592{
3593 char_u *op;
3594
3595 // get the first variable
3596 if (compile_expr7(arg, cctx) == FAIL)
3597 return FAIL;
3598
3599 /*
3600 * Repeat computing, until no "*", "/" or "%" is following.
3601 */
3602 for (;;)
3603 {
3604 op = skipwhite(*arg);
3605 if (*op != '*' && *op != '/' && *op != '%')
3606 break;
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003607 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 {
3609 char_u buf[3];
3610
3611 vim_strncpy(buf, op, 1);
3612 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003613 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 }
3615 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003616 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003617 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618
3619 // get the second variable
3620 if (compile_expr7(arg, cctx) == FAIL)
3621 return FAIL;
3622
3623 generate_two_op(cctx, op);
3624 }
3625
3626 return OK;
3627}
3628
3629/*
3630 * + number addition
3631 * - number subtraction
3632 * .. string concatenation
3633 */
3634 static int
3635compile_expr5(char_u **arg, cctx_T *cctx)
3636{
3637 char_u *op;
3638 int oplen;
3639
3640 // get the first variable
3641 if (compile_expr6(arg, cctx) == FAIL)
3642 return FAIL;
3643
3644 /*
3645 * Repeat computing, until no "+", "-" or ".." is following.
3646 */
3647 for (;;)
3648 {
3649 op = skipwhite(*arg);
3650 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3651 break;
3652 oplen = (*op == '.' ? 2 : 1);
3653
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003654 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 {
3656 char_u buf[3];
3657
3658 vim_strncpy(buf, op, oplen);
3659 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003660 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 }
3662
3663 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003664 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003665 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666
3667 // get the second variable
3668 if (compile_expr6(arg, cctx) == FAIL)
3669 return FAIL;
3670
3671 if (*op == '.')
3672 {
3673 if (may_generate_2STRING(-2, cctx) == FAIL
3674 || may_generate_2STRING(-1, cctx) == FAIL)
3675 return FAIL;
3676 generate_instr_drop(cctx, ISN_CONCAT, 1);
3677 }
3678 else
3679 generate_two_op(cctx, op);
3680 }
3681
3682 return OK;
3683}
3684
Bram Moolenaar080457c2020-03-03 21:53:32 +01003685 static exptype_T
3686get_compare_type(char_u *p, int *len, int *type_is)
3687{
3688 exptype_T type = EXPR_UNKNOWN;
3689 int i;
3690
3691 switch (p[0])
3692 {
3693 case '=': if (p[1] == '=')
3694 type = EXPR_EQUAL;
3695 else if (p[1] == '~')
3696 type = EXPR_MATCH;
3697 break;
3698 case '!': if (p[1] == '=')
3699 type = EXPR_NEQUAL;
3700 else if (p[1] == '~')
3701 type = EXPR_NOMATCH;
3702 break;
3703 case '>': if (p[1] != '=')
3704 {
3705 type = EXPR_GREATER;
3706 *len = 1;
3707 }
3708 else
3709 type = EXPR_GEQUAL;
3710 break;
3711 case '<': if (p[1] != '=')
3712 {
3713 type = EXPR_SMALLER;
3714 *len = 1;
3715 }
3716 else
3717 type = EXPR_SEQUAL;
3718 break;
3719 case 'i': if (p[1] == 's')
3720 {
3721 // "is" and "isnot"; but not a prefix of a name
3722 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3723 *len = 5;
3724 i = p[*len];
3725 if (!isalnum(i) && i != '_')
3726 {
3727 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3728 *type_is = TRUE;
3729 }
3730 }
3731 break;
3732 }
3733 return type;
3734}
3735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736/*
3737 * expr5a == expr5b
3738 * expr5a =~ expr5b
3739 * expr5a != expr5b
3740 * expr5a !~ expr5b
3741 * expr5a > expr5b
3742 * expr5a >= expr5b
3743 * expr5a < expr5b
3744 * expr5a <= expr5b
3745 * expr5a is expr5b
3746 * expr5a isnot expr5b
3747 *
3748 * Produces instructions:
3749 * EVAL expr5a Push result of "expr5a"
3750 * EVAL expr5b Push result of "expr5b"
3751 * COMPARE one of the compare instructions
3752 */
3753 static int
3754compile_expr4(char_u **arg, cctx_T *cctx)
3755{
3756 exptype_T type = EXPR_UNKNOWN;
3757 char_u *p;
3758 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 int type_is = FALSE;
3760
3761 // get the first variable
3762 if (compile_expr5(arg, cctx) == FAIL)
3763 return FAIL;
3764
3765 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003766 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767
3768 /*
3769 * If there is a comparative operator, use it.
3770 */
3771 if (type != EXPR_UNKNOWN)
3772 {
3773 int ic = FALSE; // Default: do not ignore case
3774
3775 if (type_is && (p[len] == '?' || p[len] == '#'))
3776 {
3777 semsg(_(e_invexpr2), *arg);
3778 return FAIL;
3779 }
3780 // extra question mark appended: ignore case
3781 if (p[len] == '?')
3782 {
3783 ic = TRUE;
3784 ++len;
3785 }
3786 // extra '#' appended: match case (ignored)
3787 else if (p[len] == '#')
3788 ++len;
3789 // nothing appended: match case
3790
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003791 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 {
3793 char_u buf[7];
3794
3795 vim_strncpy(buf, p, len);
3796 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003797 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 }
3799
3800 // get the second variable
3801 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003802 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003803 return FAIL;
3804
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 if (compile_expr5(arg, cctx) == FAIL)
3806 return FAIL;
3807
3808 generate_COMPARE(cctx, type, ic);
3809 }
3810
3811 return OK;
3812}
3813
3814/*
3815 * Compile || or &&.
3816 */
3817 static int
3818compile_and_or(char_u **arg, cctx_T *cctx, char *op)
3819{
3820 char_u *p = skipwhite(*arg);
3821 int opchar = *op;
3822
3823 if (p[0] == opchar && p[1] == opchar)
3824 {
3825 garray_T *instr = &cctx->ctx_instr;
3826 garray_T end_ga;
3827
3828 /*
3829 * Repeat until there is no following "||" or "&&"
3830 */
3831 ga_init2(&end_ga, sizeof(int), 10);
3832 while (p[0] == opchar && p[1] == opchar)
3833 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003834 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3835 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003837 return FAIL;
3838 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839
3840 if (ga_grow(&end_ga, 1) == FAIL)
3841 {
3842 ga_clear(&end_ga);
3843 return FAIL;
3844 }
3845 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3846 ++end_ga.ga_len;
3847 generate_JUMP(cctx, opchar == '|'
3848 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3849
3850 // eval the next expression
3851 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003852 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003853 return FAIL;
3854
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 if ((opchar == '|' ? compile_expr3(arg, cctx)
3856 : compile_expr4(arg, cctx)) == FAIL)
3857 {
3858 ga_clear(&end_ga);
3859 return FAIL;
3860 }
3861 p = skipwhite(*arg);
3862 }
3863
3864 // Fill in the end label in all jumps.
3865 while (end_ga.ga_len > 0)
3866 {
3867 isn_T *isn;
3868
3869 --end_ga.ga_len;
3870 isn = ((isn_T *)instr->ga_data)
3871 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3872 isn->isn_arg.jump.jump_where = instr->ga_len;
3873 }
3874 ga_clear(&end_ga);
3875 }
3876
3877 return OK;
3878}
3879
3880/*
3881 * expr4a && expr4a && expr4a logical AND
3882 *
3883 * Produces instructions:
3884 * EVAL expr4a Push result of "expr4a"
3885 * JUMP_AND_KEEP_IF_FALSE end
3886 * EVAL expr4b Push result of "expr4b"
3887 * JUMP_AND_KEEP_IF_FALSE end
3888 * EVAL expr4c Push result of "expr4c"
3889 * end:
3890 */
3891 static int
3892compile_expr3(char_u **arg, cctx_T *cctx)
3893{
3894 // get the first variable
3895 if (compile_expr4(arg, cctx) == FAIL)
3896 return FAIL;
3897
3898 // || and && work almost the same
3899 return compile_and_or(arg, cctx, "&&");
3900}
3901
3902/*
3903 * expr3a || expr3b || expr3c logical OR
3904 *
3905 * Produces instructions:
3906 * EVAL expr3a Push result of "expr3a"
3907 * JUMP_AND_KEEP_IF_TRUE end
3908 * EVAL expr3b Push result of "expr3b"
3909 * JUMP_AND_KEEP_IF_TRUE end
3910 * EVAL expr3c Push result of "expr3c"
3911 * end:
3912 */
3913 static int
3914compile_expr2(char_u **arg, cctx_T *cctx)
3915{
3916 // eval the first expression
3917 if (compile_expr3(arg, cctx) == FAIL)
3918 return FAIL;
3919
3920 // || and && work almost the same
3921 return compile_and_or(arg, cctx, "||");
3922}
3923
3924/*
3925 * Toplevel expression: expr2 ? expr1a : expr1b
3926 *
3927 * Produces instructions:
3928 * EVAL expr2 Push result of "expr"
3929 * JUMP_IF_FALSE alt jump if false
3930 * EVAL expr1a
3931 * JUMP_ALWAYS end
3932 * alt: EVAL expr1b
3933 * end:
3934 */
3935 static int
3936compile_expr1(char_u **arg, cctx_T *cctx)
3937{
3938 char_u *p;
3939
Bram Moolenaar3df02f52020-05-03 15:47:33 +02003940 // TODO: Try parsing as a constant. If that works just one PUSH
3941 // instruction needs to be generated.
3942
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 // evaluate the first expression
3944 if (compile_expr2(arg, cctx) == FAIL)
3945 return FAIL;
3946
3947 p = skipwhite(*arg);
3948 if (*p == '?')
3949 {
3950 garray_T *instr = &cctx->ctx_instr;
3951 garray_T *stack = &cctx->ctx_type_stack;
3952 int alt_idx = instr->ga_len;
3953 int end_idx;
3954 isn_T *isn;
3955 type_T *type1;
3956 type_T *type2;
3957
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003958 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3959 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003961 return FAIL;
3962 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963
3964 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
3965
3966 // evaluate the second expression; any type is accepted
3967 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003968 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003969 return FAIL;
3970
Bram Moolenaara6d53682020-01-28 23:04:06 +01003971 if (compile_expr1(arg, cctx) == FAIL)
3972 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973
3974 // remember the type and drop it
3975 --stack->ga_len;
3976 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
3977
3978 end_idx = instr->ga_len;
3979 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3980
3981 // jump here from JUMP_IF_FALSE
3982 isn = ((isn_T *)instr->ga_data) + alt_idx;
3983 isn->isn_arg.jump.jump_where = instr->ga_len;
3984
3985 // Check for the ":".
3986 p = skipwhite(*arg);
3987 if (*p != ':')
3988 {
3989 emsg(_(e_missing_colon));
3990 return FAIL;
3991 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003992 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3993 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003995 return FAIL;
3996 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997
3998 // evaluate the third expression
3999 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004000 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004001 return FAIL;
4002
Bram Moolenaara6d53682020-01-28 23:04:06 +01004003 if (compile_expr1(arg, cctx) == FAIL)
4004 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005
4006 // If the types differ, the result has a more generic type.
4007 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004008 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009
4010 // jump here from JUMP_ALWAYS
4011 isn = ((isn_T *)instr->ga_data) + end_idx;
4012 isn->isn_arg.jump.jump_where = instr->ga_len;
4013 }
4014 return OK;
4015}
4016
4017/*
4018 * compile "return [expr]"
4019 */
4020 static char_u *
4021compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4022{
4023 char_u *p = arg;
4024 garray_T *stack = &cctx->ctx_type_stack;
4025 type_T *stack_type;
4026
4027 if (*p != NUL && *p != '|' && *p != '\n')
4028 {
4029 // compile return argument into instructions
4030 if (compile_expr1(&p, cctx) == FAIL)
4031 return NULL;
4032
4033 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4034 if (set_return_type)
4035 cctx->ctx_ufunc->uf_ret_type = stack_type;
4036 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4037 == FAIL)
4038 return NULL;
4039 }
4040 else
4041 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004042 // "set_return_type" cannot be TRUE, only used for a lambda which
4043 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004044 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4045 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 {
4047 emsg(_("E1003: Missing return value"));
4048 return NULL;
4049 }
4050
4051 // No argument, return zero.
4052 generate_PUSHNR(cctx, 0);
4053 }
4054
4055 if (generate_instr(cctx, ISN_RETURN) == NULL)
4056 return NULL;
4057
4058 // "return val | endif" is possible
4059 return skipwhite(p);
4060}
4061
4062/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004063 * Get a line from the compilation context, compatible with exarg_T getline().
4064 * Return a pointer to the line in allocated memory.
4065 * Return NULL for end-of-file or some error.
4066 */
4067 static char_u *
4068exarg_getline(
4069 int c UNUSED,
4070 void *cookie,
4071 int indent UNUSED,
4072 int do_concat UNUSED)
4073{
4074 cctx_T *cctx = (cctx_T *)cookie;
4075
4076 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4077 {
4078 iemsg("Heredoc got to end");
4079 return NULL;
4080 }
4081 ++cctx->ctx_lnum;
4082 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4083 [cctx->ctx_lnum]);
4084}
4085
4086/*
4087 * Compile a nested :def command.
4088 */
4089 static char_u *
4090compile_nested_function(exarg_T *eap, cctx_T *cctx)
4091{
4092 char_u *name_start = eap->arg;
4093 char_u *name_end = to_name_end(eap->arg, FALSE);
4094 char_u *name = get_lambda_name();
4095 lvar_T *lvar;
4096 ufunc_T *ufunc;
4097
4098 eap->arg = name_end;
4099 eap->getline = exarg_getline;
4100 eap->cookie = cctx;
4101 eap->skip = cctx->ctx_skip == TRUE;
4102 eap->forceit = FALSE;
4103 ufunc = def_function(eap, name, cctx);
4104
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004105 if (ufunc == NULL || ufunc->uf_dfunc_idx < 0)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004106 return NULL;
4107
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004108 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004109 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004110 TRUE, ufunc->uf_func_type);
4111
4112 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4113 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4114 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004115
4116 // TODO: warning for trailing?
4117 return (char_u *)"";
4118}
4119
4120/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 * Return the length of an assignment operator, or zero if there isn't one.
4122 */
4123 int
4124assignment_len(char_u *p, int *heredoc)
4125{
4126 if (*p == '=')
4127 {
4128 if (p[1] == '<' && p[2] == '<')
4129 {
4130 *heredoc = TRUE;
4131 return 3;
4132 }
4133 return 1;
4134 }
4135 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4136 return 2;
4137 if (STRNCMP(p, "..=", 3) == 0)
4138 return 3;
4139 return 0;
4140}
4141
4142// words that cannot be used as a variable
4143static char *reserved[] = {
4144 "true",
4145 "false",
4146 NULL
4147};
4148
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004149typedef enum {
4150 dest_local,
4151 dest_option,
4152 dest_env,
4153 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004154 dest_buffer,
4155 dest_window,
4156 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004157 dest_vimvar,
4158 dest_script,
4159 dest_reg,
4160} assign_dest_T;
4161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162/*
4163 * compile "let var [= expr]", "const var = expr" and "var = expr"
4164 * "arg" points to "var".
4165 */
4166 static char_u *
4167compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4168{
4169 char_u *p;
4170 char_u *ret = NULL;
4171 int var_count = 0;
4172 int semicolon = 0;
4173 size_t varlen;
4174 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004175 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004178 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004180 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 int oplen = 0;
4182 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004183 type_T *type = &t_any;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004184 lvar_T *lvar = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 char_u *name;
4186 char_u *sp;
4187 int has_type = FALSE;
4188 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
4189 int instr_count = -1;
4190
4191 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
4192 if (p == NULL)
4193 return NULL;
4194 if (var_count > 0)
4195 {
4196 // TODO: let [var, var] = list
4197 emsg("Cannot handle a list yet");
4198 return NULL;
4199 }
4200
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004201 // "a: type" is declaring variable "a" with a type, not "a:".
4202 if (is_decl && p == arg + 2 && p[-1] == ':')
4203 --p;
4204
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 varlen = p - arg;
4206 name = vim_strnsave(arg, (int)varlen);
4207 if (name == NULL)
4208 return NULL;
4209
Bram Moolenaar080457c2020-03-03 21:53:32 +01004210 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004212 if (*arg == '&')
4213 {
4214 int cc;
4215 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216
Bram Moolenaar080457c2020-03-03 21:53:32 +01004217 dest = dest_option;
4218 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004220 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004221 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 if (is_decl)
4224 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004225 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 goto theend;
4227 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004228 p = arg;
4229 p = find_option_end(&p, &opt_flags);
4230 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004232 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01004233 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004234 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004235 }
4236 cc = *p;
4237 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004238 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004239 *p = cc;
4240 if (opt_type == -3)
4241 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004242 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004243 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004244 }
4245 if (opt_type == -2 || opt_type == 0)
4246 type = &t_string;
4247 else
4248 type = &t_number; // both number and boolean option
4249 }
4250 else if (*arg == '$')
4251 {
4252 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004253 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004254 if (is_decl)
4255 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004256 semsg(_("E1065: Cannot declare an environment variable: %s"),
4257 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004258 goto theend;
4259 }
4260 }
4261 else if (*arg == '@')
4262 {
4263 if (!valid_yank_reg(arg[1], TRUE))
4264 {
4265 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004266 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004267 }
4268 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004269 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004270 if (is_decl)
4271 {
4272 semsg(_("E1066: Cannot declare a register: %s"), name);
4273 goto theend;
4274 }
4275 }
4276 else if (STRNCMP(arg, "g:", 2) == 0)
4277 {
4278 dest = dest_global;
4279 if (is_decl)
4280 {
4281 semsg(_("E1016: Cannot declare a global variable: %s"), name);
4282 goto theend;
4283 }
4284 }
Bram Moolenaard3aac292020-04-19 14:32:17 +02004285 else if (STRNCMP(arg, "b:", 2) == 0)
4286 {
4287 dest = dest_buffer;
4288 if (is_decl)
4289 {
4290 semsg(_("E1078: Cannot declare a buffer variable: %s"), name);
4291 goto theend;
4292 }
4293 }
4294 else if (STRNCMP(arg, "w:", 2) == 0)
4295 {
4296 dest = dest_window;
4297 if (is_decl)
4298 {
4299 semsg(_("E1079: Cannot declare a window variable: %s"), name);
4300 goto theend;
4301 }
4302 }
4303 else if (STRNCMP(arg, "t:", 2) == 0)
4304 {
4305 dest = dest_tab;
4306 if (is_decl)
4307 {
4308 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
4309 goto theend;
4310 }
4311 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004312 else if (STRNCMP(arg, "v:", 2) == 0)
4313 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004314 typval_T *vtv;
4315 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004316
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004317 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004318 if (vimvaridx < 0)
4319 {
4320 semsg(_(e_var_notfound), arg);
4321 goto theend;
4322 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004323 // We use the current value of "sandbox" here, is that OK?
4324 if (var_check_ro(di_flags, name, FALSE))
4325 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004326 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004327 vtv = get_vim_var_tv(vimvaridx);
4328 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004329 if (is_decl)
4330 {
4331 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
4332 goto theend;
4333 }
4334 }
4335 else
4336 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004337 int idx;
4338
Bram Moolenaar080457c2020-03-03 21:53:32 +01004339 for (idx = 0; reserved[idx] != NULL; ++idx)
4340 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004342 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 goto theend;
4344 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004345
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004346 lvar = lookup_local(arg, varlen, cctx);
4347 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004349 if (is_decl)
4350 {
4351 semsg(_("E1017: Variable already declared: %s"), name);
4352 goto theend;
4353 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004354 else if (lvar->lv_const)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004355 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004356 semsg(_("E1018: Cannot assign to a constant: %s"), name);
4357 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004358 }
4359 }
4360 else if (STRNCMP(arg, "s:", 2) == 0
4361 || lookup_script(arg, varlen) == OK
4362 || find_imported(arg, varlen, cctx) != NULL)
4363 {
4364 dest = dest_script;
4365 if (is_decl)
4366 {
4367 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004368 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004369 goto theend;
4370 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004372 else if (name[1] == ':' && name[2] != NUL)
4373 {
4374 semsg(_("E1082: Cannot use a namespaced variable: %s"), name);
4375 goto theend;
4376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 }
4378 }
4379
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004380 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 {
4382 if (is_decl && *p == ':')
4383 {
4384 // parse optional type: "let var: type = expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004385 if (!VIM_ISWHITE(p[1]))
4386 {
4387 semsg(_(e_white_after), ":");
4388 goto theend;
4389 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 p = skipwhite(p + 1);
4391 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 has_type = TRUE;
4393 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004394 else if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 type = lvar->lv_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 }
4397
4398 sp = p;
4399 p = skipwhite(p);
4400 op = p;
4401 oplen = assignment_len(p, &heredoc);
4402 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4403 {
4404 char_u buf[4];
4405
4406 vim_strncpy(buf, op, oplen);
4407 semsg(_(e_white_both), buf);
4408 }
4409
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004410 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02004411 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004413 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 goto theend;
4415 }
4416
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004417 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 {
4419 if (oplen > 1 && !heredoc)
4420 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004421 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4423 name);
4424 goto theend;
4425 }
4426
4427 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02004428 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004429 goto theend;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004430 lvar = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4431 if (lvar == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004433 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 }
4435
4436 if (heredoc)
4437 {
4438 list_T *l;
4439 listitem_T *li;
4440
4441 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004442 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004444 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445
4446 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004447 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 {
4449 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4450 li->li_tv.vval.v_string = NULL;
4451 }
4452 generate_NEWLIST(cctx, l->lv_len);
4453 type = &t_list_string;
4454 list_free(l);
4455 p += STRLEN(p);
4456 }
4457 else if (oplen > 0)
4458 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004459 int r;
4460 type_T *stacktype;
4461 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004462
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 // for "+=", "*=", "..=" etc. first load the current value
4464 if (*op != '=')
4465 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004466 switch (dest)
4467 {
4468 case dest_option:
4469 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004470 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004471 break;
4472 case dest_global:
4473 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4474 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004475 case dest_buffer:
4476 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4477 break;
4478 case dest_window:
4479 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4480 break;
4481 case dest_tab:
4482 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4483 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004484 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01004485 compile_load_scriptvar(cctx,
4486 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004487 break;
4488 case dest_env:
4489 // Include $ in the name here
4490 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4491 break;
4492 case dest_reg:
4493 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
4494 break;
4495 case dest_vimvar:
4496 generate_LOADV(cctx, name + 2, TRUE);
4497 break;
4498 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004499 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004500 break;
4501 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502 }
4503
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004504 // Compile the expression. Temporarily hide the new local variable
4505 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02004506 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004507 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 instr_count = instr->ga_len;
4509 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004510 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02004511 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004512 ++cctx->ctx_locals.ga_len;
4513 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514 goto theend;
4515
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004516 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004518 stack = &cctx->ctx_type_stack;
4519 stacktype = stack->ga_len == 0 ? &t_void
4520 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004521 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004523 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004525 if (stacktype->tt_type == VAR_VOID)
4526 {
4527 emsg(_("E1031: Cannot use void value"));
4528 goto theend;
4529 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004530 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004531 {
4532 // An empty list or dict has a &t_void member, for a
4533 // variable that implies &t_any.
4534 if (stacktype == &t_list_empty)
4535 lvar->lv_type = &t_list_any;
4536 else if (stacktype == &t_dict_empty)
4537 lvar->lv_type = &t_dict_any;
4538 else
4539 lvar->lv_type = stacktype;
4540 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004541 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004542 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
4543 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004544 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004545 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004546 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547 }
4548 }
4549 else if (cmdidx == CMD_const)
4550 {
4551 emsg(_("E1021: const requires a value"));
4552 goto theend;
4553 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004554 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 {
4556 emsg(_("E1022: type or initialization required"));
4557 goto theend;
4558 }
4559 else
4560 {
4561 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 if (ga_grow(instr, 1) == FAIL)
4563 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004564 switch (type->tt_type)
4565 {
4566 case VAR_BOOL:
4567 generate_PUSHBOOL(cctx, VVAL_FALSE);
4568 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004569 case VAR_FLOAT:
4570#ifdef FEAT_FLOAT
4571 generate_PUSHF(cctx, 0.0);
4572#endif
4573 break;
4574 case VAR_STRING:
4575 generate_PUSHS(cctx, NULL);
4576 break;
4577 case VAR_BLOB:
4578 generate_PUSHBLOB(cctx, NULL);
4579 break;
4580 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004581 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004582 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004583 case VAR_LIST:
4584 generate_NEWLIST(cctx, 0);
4585 break;
4586 case VAR_DICT:
4587 generate_NEWDICT(cctx, 0);
4588 break;
4589 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004590 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004591 break;
4592 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004593 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004594 break;
4595 case VAR_NUMBER:
4596 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004597 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02004598 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01004599 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02004600 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01004601 generate_PUSHNR(cctx, 0);
4602 break;
4603 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 }
4605
4606 if (oplen > 0 && *op != '=')
4607 {
4608 type_T *expected = &t_number;
4609 garray_T *stack = &cctx->ctx_type_stack;
4610 type_T *stacktype;
4611
4612 // TODO: if type is known use float or any operation
4613
4614 if (*op == '.')
4615 expected = &t_string;
4616 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4617 if (need_type(stacktype, expected, -1, cctx) == FAIL)
4618 goto theend;
4619
4620 if (*op == '.')
4621 generate_instr_drop(cctx, ISN_CONCAT, 1);
4622 else
4623 {
4624 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
4625
4626 if (isn == NULL)
4627 goto theend;
4628 switch (*op)
4629 {
4630 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
4631 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
4632 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
4633 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
4634 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
4635 }
4636 }
4637 }
4638
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004639 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004641 case dest_option:
4642 generate_STOREOPT(cctx, name + 1, opt_flags);
4643 break;
4644 case dest_global:
4645 // include g: with the name, easier to execute that way
4646 generate_STORE(cctx, ISN_STOREG, 0, name);
4647 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004648 case dest_buffer:
4649 // include b: with the name, easier to execute that way
4650 generate_STORE(cctx, ISN_STOREB, 0, name);
4651 break;
4652 case dest_window:
4653 // include w: with the name, easier to execute that way
4654 generate_STORE(cctx, ISN_STOREW, 0, name);
4655 break;
4656 case dest_tab:
4657 // include t: with the name, easier to execute that way
4658 generate_STORE(cctx, ISN_STORET, 0, name);
4659 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004660 case dest_env:
4661 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
4662 break;
4663 case dest_reg:
4664 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
4665 break;
4666 case dest_vimvar:
4667 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
4668 break;
4669 case dest_script:
4670 {
4671 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4672 imported_T *import = NULL;
4673 int sid = current_sctx.sc_sid;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004674 int idx;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004675
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004676 if (name[1] != ':')
4677 {
4678 import = find_imported(name, 0, cctx);
4679 if (import != NULL)
4680 sid = import->imp_sid;
4681 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004683 idx = get_script_item_idx(sid, rawname, TRUE);
4684 // TODO: specific type
4685 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004686 {
4687 char_u *name_s = name;
4688
4689 // Include s: in the name for store_var()
4690 if (name[1] != ':')
4691 {
4692 int len = (int)STRLEN(name) + 3;
4693
4694 name_s = alloc(len);
4695 if (name_s == NULL)
4696 name_s = name;
4697 else
4698 vim_snprintf((char *)name_s, len, "s:%s", name);
4699 }
4700 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
4701 if (name_s != name)
4702 vim_free(name_s);
4703 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004704 else
4705 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
4706 sid, idx, &t_any);
4707 }
4708 break;
4709 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004710 if (lvar != NULL)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004711 {
4712 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004714 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
4715 // into ISN_STORENR
4716 if (instr->ga_len == instr_count + 1
4717 && isn->isn_type == ISN_PUSHNR)
4718 {
4719 varnumber_T val = isn->isn_arg.number;
4720 garray_T *stack = &cctx->ctx_type_stack;
4721
4722 isn->isn_type = ISN_STORENR;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004723 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004724 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004725 if (stack->ga_len > 0)
4726 --stack->ga_len;
4727 }
4728 else
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004729 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004730 }
4731 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732 }
4733 ret = p;
4734
4735theend:
4736 vim_free(name);
4737 return ret;
4738}
4739
4740/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004741 * Check if "name" can be "unlet".
4742 */
4743 int
4744check_vim9_unlet(char_u *name)
4745{
4746 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
4747 {
4748 semsg(_("E1081: Cannot unlet %s"), name);
4749 return FAIL;
4750 }
4751 return OK;
4752}
4753
4754/*
4755 * Callback passed to ex_unletlock().
4756 */
4757 static int
4758compile_unlet(
4759 lval_T *lvp,
4760 char_u *name_end,
4761 exarg_T *eap,
4762 int deep UNUSED,
4763 void *coookie)
4764{
4765 cctx_T *cctx = coookie;
4766
4767 if (lvp->ll_tv == NULL)
4768 {
4769 char_u *p = lvp->ll_name;
4770 int cc = *name_end;
4771 int ret = OK;
4772
4773 // Normal name. Only supports g:, w:, t: and b: namespaces.
4774 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004775 if (*p == '$')
4776 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
4777 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004778 ret = FAIL;
4779 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004780 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004781
4782 *name_end = cc;
4783 return ret;
4784 }
4785
4786 // TODO: unlet {list}[idx]
4787 // TODO: unlet {dict}[key]
4788 emsg("Sorry, :unlet not fully implemented yet");
4789 return FAIL;
4790}
4791
4792/*
4793 * compile "unlet var", "lock var" and "unlock var"
4794 * "arg" points to "var".
4795 */
4796 static char_u *
4797compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
4798{
4799 char_u *p = arg;
4800
4801 if (eap->cmdidx != CMD_unlet)
4802 {
4803 emsg("Sorry, :lock and unlock not implemented yet");
4804 return NULL;
4805 }
4806
4807 if (*p == '!')
4808 {
4809 p = skipwhite(p + 1);
4810 eap->forceit = TRUE;
4811 }
4812
4813 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
4814 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4815}
4816
4817/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004818 * Compile an :import command.
4819 */
4820 static char_u *
4821compile_import(char_u *arg, cctx_T *cctx)
4822{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01004823 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824}
4825
4826/*
4827 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
4828 */
4829 static int
4830compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
4831{
4832 garray_T *instr = &cctx->ctx_instr;
4833 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
4834
4835 if (endlabel == NULL)
4836 return FAIL;
4837 endlabel->el_next = *el;
4838 *el = endlabel;
4839 endlabel->el_end_label = instr->ga_len;
4840
4841 generate_JUMP(cctx, when, 0);
4842 return OK;
4843}
4844
4845 static void
4846compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
4847{
4848 garray_T *instr = &cctx->ctx_instr;
4849
4850 while (*el != NULL)
4851 {
4852 endlabel_T *cur = (*el);
4853 isn_T *isn;
4854
4855 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
4856 isn->isn_arg.jump.jump_where = instr->ga_len;
4857 *el = cur->el_next;
4858 vim_free(cur);
4859 }
4860}
4861
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004862 static void
4863compile_free_jump_to_end(endlabel_T **el)
4864{
4865 while (*el != NULL)
4866 {
4867 endlabel_T *cur = (*el);
4868
4869 *el = cur->el_next;
4870 vim_free(cur);
4871 }
4872}
4873
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874/*
4875 * Create a new scope and set up the generic items.
4876 */
4877 static scope_T *
4878new_scope(cctx_T *cctx, scopetype_T type)
4879{
4880 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
4881
4882 if (scope == NULL)
4883 return NULL;
4884 scope->se_outer = cctx->ctx_scope;
4885 cctx->ctx_scope = scope;
4886 scope->se_type = type;
4887 scope->se_local_count = cctx->ctx_locals.ga_len;
4888 return scope;
4889}
4890
4891/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004892 * Free the current scope and go back to the outer scope.
4893 */
4894 static void
4895drop_scope(cctx_T *cctx)
4896{
4897 scope_T *scope = cctx->ctx_scope;
4898
4899 if (scope == NULL)
4900 {
4901 iemsg("calling drop_scope() without a scope");
4902 return;
4903 }
4904 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02004905 switch (scope->se_type)
4906 {
4907 case IF_SCOPE:
4908 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
4909 case FOR_SCOPE:
4910 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
4911 case WHILE_SCOPE:
4912 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
4913 case TRY_SCOPE:
4914 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
4915 case NO_SCOPE:
4916 case BLOCK_SCOPE:
4917 break;
4918 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004919 vim_free(scope);
4920}
4921
4922/*
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004923 * Evaluate an expression that is a constant:
4924 * has(arg)
4925 *
4926 * Also handle:
4927 * ! in front logical NOT
4928 *
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004929 * Return FAIL if the expression is not a constant.
4930 */
4931 static int
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004932evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004933{
4934 typval_T argvars[2];
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004935 char_u *start_leader, *end_leader;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004936 int has_call = FALSE;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004937
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004938 /*
4939 * Skip '!' characters. They are handled later.
4940 */
4941 start_leader = *arg;
4942 while (**arg == '!')
4943 *arg = skipwhite(*arg + 1);
4944 end_leader = *arg;
4945
4946 /*
Bram Moolenaar080457c2020-03-03 21:53:32 +01004947 * Recognize only a few types of constants for now.
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004948 */
Bram Moolenaar080457c2020-03-03 21:53:32 +01004949 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
4950 {
4951 tv->v_type = VAR_SPECIAL;
4952 tv->vval.v_number = VVAL_TRUE;
4953 *arg += 4;
4954 return OK;
4955 }
4956 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
4957 {
4958 tv->v_type = VAR_SPECIAL;
4959 tv->vval.v_number = VVAL_FALSE;
4960 *arg += 5;
4961 return OK;
4962 }
4963
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004964 if (STRNCMP("has(", *arg, 4) == 0)
4965 {
4966 has_call = TRUE;
4967 *arg = skipwhite(*arg + 4);
4968 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004969
4970 if (**arg == '"')
4971 {
4972 if (get_string_tv(arg, tv, TRUE) == FAIL)
4973 return FAIL;
4974 }
4975 else if (**arg == '\'')
4976 {
4977 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
4978 return FAIL;
4979 }
4980 else
4981 return FAIL;
4982
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004983 if (has_call)
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01004984 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004985 *arg = skipwhite(*arg);
4986 if (**arg != ')')
4987 return FAIL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004988 *arg = *arg + 1;
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02004989
4990 argvars[0] = *tv;
4991 argvars[1].v_type = VAR_UNKNOWN;
4992 tv->v_type = VAR_NUMBER;
4993 tv->vval.v_number = 0;
4994 f_has(argvars, tv);
4995 clear_tv(&argvars[0]);
4996
4997 while (start_leader < end_leader)
4998 {
4999 if (*start_leader == '!')
5000 tv->vval.v_number = !tv->vval.v_number;
5001 ++start_leader;
5002 }
Bram Moolenaar7f829ca2020-01-31 22:12:41 +01005003 }
5004
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005005 return OK;
5006}
5007
Bram Moolenaar080457c2020-03-03 21:53:32 +01005008 static int
5009evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
5010{
5011 exptype_T type = EXPR_UNKNOWN;
5012 char_u *p;
5013 int len = 2;
5014 int type_is = FALSE;
5015
5016 // get the first variable
5017 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
5018 return FAIL;
5019
5020 p = skipwhite(*arg);
5021 type = get_compare_type(p, &len, &type_is);
5022
5023 /*
5024 * If there is a comparative operator, use it.
5025 */
5026 if (type != EXPR_UNKNOWN)
5027 {
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02005028 typval_T tv2;
5029 char_u *s1, *s2;
5030 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
5031 int n;
5032
5033 // TODO: Only string == string is supported now
5034 if (tv->v_type != VAR_STRING)
5035 return FAIL;
5036 if (type != EXPR_EQUAL)
5037 return FAIL;
5038
5039 // get the second variable
Bram Moolenaar4227c782020-04-02 16:00:04 +02005040 init_tv(&tv2);
Bram Moolenaar80c34ca2020-04-01 23:05:18 +02005041 *arg = skipwhite(p + len);
5042 if (evaluate_const_expr7(arg, cctx, &tv2) == FAIL
5043 || tv2.v_type != VAR_STRING)
5044 {
5045 clear_tv(&tv2);
5046 return FAIL;
5047 }
5048 s1 = tv_get_string_buf(tv, buf1);
5049 s2 = tv_get_string_buf(&tv2, buf2);
5050 n = STRCMP(s1, s2);
5051 clear_tv(tv);
5052 clear_tv(&tv2);
5053 tv->v_type = VAR_BOOL;
5054 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar080457c2020-03-03 21:53:32 +01005055 }
5056
5057 return OK;
5058}
5059
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005060static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
5061
5062/*
5063 * Compile constant || or &&.
5064 */
5065 static int
5066evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
5067{
5068 char_u *p = skipwhite(*arg);
5069 int opchar = *op;
5070
5071 if (p[0] == opchar && p[1] == opchar)
5072 {
5073 int val = tv2bool(tv);
5074
5075 /*
5076 * Repeat until there is no following "||" or "&&"
5077 */
5078 while (p[0] == opchar && p[1] == opchar)
5079 {
5080 typval_T tv2;
5081
5082 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
5083 return FAIL;
5084
5085 // eval the next expression
5086 *arg = skipwhite(p + 2);
5087 tv2.v_type = VAR_UNKNOWN;
Bram Moolenaareed35712020-02-04 23:08:14 +01005088 tv2.v_lock = 0;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005089 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
Bram Moolenaar080457c2020-03-03 21:53:32 +01005090 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005091 {
5092 clear_tv(&tv2);
5093 return FAIL;
5094 }
5095 if ((opchar == '&') == val)
5096 {
5097 // false || tv2 or true && tv2: use tv2
5098 clear_tv(tv);
5099 *tv = tv2;
5100 val = tv2bool(tv);
5101 }
5102 else
5103 clear_tv(&tv2);
5104 p = skipwhite(*arg);
5105 }
5106 }
5107
5108 return OK;
5109}
5110
5111/*
5112 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
5113 * Return FAIL if the expression is not a constant.
5114 */
5115 static int
5116evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
5117{
5118 // evaluate the first expression
Bram Moolenaar080457c2020-03-03 21:53:32 +01005119 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005120 return FAIL;
5121
5122 // || and && work almost the same
5123 return evaluate_const_and_or(arg, cctx, "&&", tv);
5124}
5125
5126/*
5127 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
5128 * Return FAIL if the expression is not a constant.
5129 */
5130 static int
5131evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
5132{
5133 // evaluate the first expression
5134 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
5135 return FAIL;
5136
5137 // || and && work almost the same
5138 return evaluate_const_and_or(arg, cctx, "||", tv);
5139}
5140
5141/*
5142 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
5143 * E.g. for "has('feature')".
5144 * This does not produce error messages. "tv" should be cleared afterwards.
5145 * Return FAIL if the expression is not a constant.
5146 */
5147 static int
5148evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
5149{
5150 char_u *p;
5151
5152 // evaluate the first expression
5153 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
5154 return FAIL;
5155
5156 p = skipwhite(*arg);
5157 if (*p == '?')
5158 {
5159 int val = tv2bool(tv);
5160 typval_T tv2;
5161
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005162 // require space before and after the ?
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005163 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
5164 return FAIL;
5165
5166 // evaluate the second expression; any type is accepted
5167 clear_tv(tv);
5168 *arg = skipwhite(p + 1);
5169 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
5170 return FAIL;
5171
5172 // Check for the ":".
5173 p = skipwhite(*arg);
5174 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
5175 return FAIL;
5176
5177 // evaluate the third expression
5178 *arg = skipwhite(p + 1);
5179 tv2.v_type = VAR_UNKNOWN;
5180 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
5181 {
5182 clear_tv(&tv2);
5183 return FAIL;
5184 }
5185 if (val)
5186 {
5187 // use the expr after "?"
5188 clear_tv(&tv2);
5189 }
5190 else
5191 {
5192 // use the expr after ":"
5193 clear_tv(tv);
5194 *tv = tv2;
5195 }
5196 }
5197 return OK;
5198}
5199
5200/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201 * compile "if expr"
5202 *
5203 * "if expr" Produces instructions:
5204 * EVAL expr Push result of "expr"
5205 * JUMP_IF_FALSE end
5206 * ... body ...
5207 * end:
5208 *
5209 * "if expr | else" Produces instructions:
5210 * EVAL expr Push result of "expr"
5211 * JUMP_IF_FALSE else
5212 * ... body ...
5213 * JUMP_ALWAYS end
5214 * else:
5215 * ... body ...
5216 * end:
5217 *
5218 * "if expr1 | elseif expr2 | else" Produces instructions:
5219 * EVAL expr Push result of "expr"
5220 * JUMP_IF_FALSE elseif
5221 * ... body ...
5222 * JUMP_ALWAYS end
5223 * elseif:
5224 * EVAL expr Push result of "expr"
5225 * JUMP_IF_FALSE else
5226 * ... body ...
5227 * JUMP_ALWAYS end
5228 * else:
5229 * ... body ...
5230 * end:
5231 */
5232 static char_u *
5233compile_if(char_u *arg, cctx_T *cctx)
5234{
5235 char_u *p = arg;
5236 garray_T *instr = &cctx->ctx_instr;
5237 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005238 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005240 // compile "expr"; if we know it evaluates to FALSE skip the block
5241 tv.v_type = VAR_UNKNOWN;
5242 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5243 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5244 else
5245 cctx->ctx_skip = MAYBE;
5246 clear_tv(&tv);
5247 if (cctx->ctx_skip == MAYBE)
5248 {
5249 p = arg;
5250 if (compile_expr1(&p, cctx) == FAIL)
5251 return NULL;
5252 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005253
5254 scope = new_scope(cctx, IF_SCOPE);
5255 if (scope == NULL)
5256 return NULL;
5257
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005258 if (cctx->ctx_skip == MAYBE)
5259 {
5260 // "where" is set when ":elseif", "else" or ":endif" is found
5261 scope->se_u.se_if.is_if_label = instr->ga_len;
5262 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5263 }
5264 else
5265 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005266
5267 return p;
5268}
5269
5270 static char_u *
5271compile_elseif(char_u *arg, cctx_T *cctx)
5272{
5273 char_u *p = arg;
5274 garray_T *instr = &cctx->ctx_instr;
5275 isn_T *isn;
5276 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005277 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005278
5279 if (scope == NULL || scope->se_type != IF_SCOPE)
5280 {
5281 emsg(_(e_elseif_without_if));
5282 return NULL;
5283 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005284 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005285
Bram Moolenaar158906c2020-02-06 20:39:45 +01005286 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005287 {
5288 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005289 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005290 return NULL;
5291 // previous "if" or "elseif" jumps here
5292 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5293 isn->isn_arg.jump.jump_where = instr->ga_len;
5294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005295
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005296 // compile "expr"; if we know it evaluates to FALSE skip the block
5297 tv.v_type = VAR_UNKNOWN;
5298 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5299 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5300 else
5301 cctx->ctx_skip = MAYBE;
5302 clear_tv(&tv);
5303 if (cctx->ctx_skip == MAYBE)
5304 {
5305 p = arg;
5306 if (compile_expr1(&p, cctx) == FAIL)
5307 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005308
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005309 // "where" is set when ":elseif", "else" or ":endif" is found
5310 scope->se_u.se_if.is_if_label = instr->ga_len;
5311 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5312 }
5313 else
5314 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005315
5316 return p;
5317}
5318
5319 static char_u *
5320compile_else(char_u *arg, cctx_T *cctx)
5321{
5322 char_u *p = arg;
5323 garray_T *instr = &cctx->ctx_instr;
5324 isn_T *isn;
5325 scope_T *scope = cctx->ctx_scope;
5326
5327 if (scope == NULL || scope->se_type != IF_SCOPE)
5328 {
5329 emsg(_(e_else_without_if));
5330 return NULL;
5331 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005332 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005333
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005334 // jump from previous block to the end, unless the else block is empty
5335 if (cctx->ctx_skip == MAYBE)
5336 {
5337 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005339 return NULL;
5340 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341
Bram Moolenaar158906c2020-02-06 20:39:45 +01005342 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005343 {
5344 if (scope->se_u.se_if.is_if_label >= 0)
5345 {
5346 // previous "if" or "elseif" jumps here
5347 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5348 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005349 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005350 }
5351 }
5352
5353 if (cctx->ctx_skip != MAYBE)
5354 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005355
5356 return p;
5357}
5358
5359 static char_u *
5360compile_endif(char_u *arg, cctx_T *cctx)
5361{
5362 scope_T *scope = cctx->ctx_scope;
5363 ifscope_T *ifscope;
5364 garray_T *instr = &cctx->ctx_instr;
5365 isn_T *isn;
5366
5367 if (scope == NULL || scope->se_type != IF_SCOPE)
5368 {
5369 emsg(_(e_endif_without_if));
5370 return NULL;
5371 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005372 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005373 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005374
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005375 if (scope->se_u.se_if.is_if_label >= 0)
5376 {
5377 // previous "if" or "elseif" jumps here
5378 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5379 isn->isn_arg.jump.jump_where = instr->ga_len;
5380 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005381 // Fill in the "end" label in jumps at the end of the blocks.
5382 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005383 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005384
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005385 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005386 return arg;
5387}
5388
5389/*
5390 * compile "for var in expr"
5391 *
5392 * Produces instructions:
5393 * PUSHNR -1
5394 * STORE loop-idx Set index to -1
5395 * EVAL expr Push result of "expr"
5396 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5397 * - if beyond end, jump to "end"
5398 * - otherwise get item from list and push it
5399 * STORE var Store item in "var"
5400 * ... body ...
5401 * JUMP top Jump back to repeat
5402 * end: DROP Drop the result of "expr"
5403 *
5404 */
5405 static char_u *
5406compile_for(char_u *arg, cctx_T *cctx)
5407{
5408 char_u *p;
5409 size_t varlen;
5410 garray_T *instr = &cctx->ctx_instr;
5411 garray_T *stack = &cctx->ctx_type_stack;
5412 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005413 lvar_T *loop_lvar; // loop iteration variable
5414 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005415 type_T *vartype;
5416
5417 // TODO: list of variables: "for [key, value] in dict"
5418 // parse "var"
5419 for (p = arg; eval_isnamec1(*p); ++p)
5420 ;
5421 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005422 var_lvar = lookup_local(arg, varlen, cctx);
5423 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005424 {
5425 semsg(_("E1023: variable already defined: %s"), arg);
5426 return NULL;
5427 }
5428
5429 // consume "in"
5430 p = skipwhite(p);
5431 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5432 {
5433 emsg(_(e_missing_in));
5434 return NULL;
5435 }
5436 p = skipwhite(p + 2);
5437
5438
5439 scope = new_scope(cctx, FOR_SCOPE);
5440 if (scope == NULL)
5441 return NULL;
5442
5443 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005444 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5445 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005446 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005447 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005448 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005450 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005451
5452 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005453 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5454 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005455 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005456 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005457 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005458 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005459 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005460
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005461 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005462
5463 // compile "expr", it remains on the stack until "endfor"
5464 arg = p;
5465 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005466 {
5467 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005468 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005469 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005470
5471 // now we know the type of "var"
5472 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5473 if (vartype->tt_type != VAR_LIST)
5474 {
5475 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005476 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005477 return NULL;
5478 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005479 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005480 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005481
5482 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005483 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005484
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005485 generate_FOR(cctx, loop_lvar->lv_idx);
5486 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005487
5488 return arg;
5489}
5490
5491/*
5492 * compile "endfor"
5493 */
5494 static char_u *
5495compile_endfor(char_u *arg, cctx_T *cctx)
5496{
5497 garray_T *instr = &cctx->ctx_instr;
5498 scope_T *scope = cctx->ctx_scope;
5499 forscope_T *forscope;
5500 isn_T *isn;
5501
5502 if (scope == NULL || scope->se_type != FOR_SCOPE)
5503 {
5504 emsg(_(e_for));
5505 return NULL;
5506 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005507 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005508 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005509 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005510
5511 // At end of ":for" scope jump back to the FOR instruction.
5512 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5513
5514 // Fill in the "end" label in the FOR statement so it can jump here
5515 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5516 isn->isn_arg.forloop.for_end = instr->ga_len;
5517
5518 // Fill in the "end" label any BREAK statements
5519 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5520
5521 // Below the ":for" scope drop the "expr" list from the stack.
5522 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5523 return NULL;
5524
5525 vim_free(scope);
5526
5527 return arg;
5528}
5529
5530/*
5531 * compile "while expr"
5532 *
5533 * Produces instructions:
5534 * top: EVAL expr Push result of "expr"
5535 * JUMP_IF_FALSE end jump if false
5536 * ... body ...
5537 * JUMP top Jump back to repeat
5538 * end:
5539 *
5540 */
5541 static char_u *
5542compile_while(char_u *arg, cctx_T *cctx)
5543{
5544 char_u *p = arg;
5545 garray_T *instr = &cctx->ctx_instr;
5546 scope_T *scope;
5547
5548 scope = new_scope(cctx, WHILE_SCOPE);
5549 if (scope == NULL)
5550 return NULL;
5551
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005552 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005553
5554 // compile "expr"
5555 if (compile_expr1(&p, cctx) == FAIL)
5556 return NULL;
5557
5558 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005559 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005560 JUMP_IF_FALSE, cctx) == FAIL)
5561 return FAIL;
5562
5563 return p;
5564}
5565
5566/*
5567 * compile "endwhile"
5568 */
5569 static char_u *
5570compile_endwhile(char_u *arg, cctx_T *cctx)
5571{
5572 scope_T *scope = cctx->ctx_scope;
5573
5574 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5575 {
5576 emsg(_(e_while));
5577 return NULL;
5578 }
5579 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005580 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005581
5582 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005583 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584
5585 // Fill in the "end" label in the WHILE statement so it can jump here.
5586 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005587 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005588
5589 vim_free(scope);
5590
5591 return arg;
5592}
5593
5594/*
5595 * compile "continue"
5596 */
5597 static char_u *
5598compile_continue(char_u *arg, cctx_T *cctx)
5599{
5600 scope_T *scope = cctx->ctx_scope;
5601
5602 for (;;)
5603 {
5604 if (scope == NULL)
5605 {
5606 emsg(_(e_continue));
5607 return NULL;
5608 }
5609 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5610 break;
5611 scope = scope->se_outer;
5612 }
5613
5614 // Jump back to the FOR or WHILE instruction.
5615 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005616 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5617 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005618 return arg;
5619}
5620
5621/*
5622 * compile "break"
5623 */
5624 static char_u *
5625compile_break(char_u *arg, cctx_T *cctx)
5626{
5627 scope_T *scope = cctx->ctx_scope;
5628 endlabel_T **el;
5629
5630 for (;;)
5631 {
5632 if (scope == NULL)
5633 {
5634 emsg(_(e_break));
5635 return NULL;
5636 }
5637 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5638 break;
5639 scope = scope->se_outer;
5640 }
5641
5642 // Jump to the end of the FOR or WHILE loop.
5643 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005644 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005645 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005646 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005647 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5648 return FAIL;
5649
5650 return arg;
5651}
5652
5653/*
5654 * compile "{" start of block
5655 */
5656 static char_u *
5657compile_block(char_u *arg, cctx_T *cctx)
5658{
5659 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5660 return NULL;
5661 return skipwhite(arg + 1);
5662}
5663
5664/*
5665 * compile end of block: drop one scope
5666 */
5667 static void
5668compile_endblock(cctx_T *cctx)
5669{
5670 scope_T *scope = cctx->ctx_scope;
5671
5672 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005673 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005674 vim_free(scope);
5675}
5676
5677/*
5678 * compile "try"
5679 * Creates a new scope for the try-endtry, pointing to the first catch and
5680 * finally.
5681 * Creates another scope for the "try" block itself.
5682 * TRY instruction sets up exception handling at runtime.
5683 *
5684 * "try"
5685 * TRY -> catch1, -> finally push trystack entry
5686 * ... try block
5687 * "throw {exception}"
5688 * EVAL {exception}
5689 * THROW create exception
5690 * ... try block
5691 * " catch {expr}"
5692 * JUMP -> finally
5693 * catch1: PUSH exeception
5694 * EVAL {expr}
5695 * MATCH
5696 * JUMP nomatch -> catch2
5697 * CATCH remove exception
5698 * ... catch block
5699 * " catch"
5700 * JUMP -> finally
5701 * catch2: CATCH remove exception
5702 * ... catch block
5703 * " finally"
5704 * finally:
5705 * ... finally block
5706 * " endtry"
5707 * ENDTRY pop trystack entry, may rethrow
5708 */
5709 static char_u *
5710compile_try(char_u *arg, cctx_T *cctx)
5711{
5712 garray_T *instr = &cctx->ctx_instr;
5713 scope_T *try_scope;
5714 scope_T *scope;
5715
5716 // scope that holds the jumps that go to catch/finally/endtry
5717 try_scope = new_scope(cctx, TRY_SCOPE);
5718 if (try_scope == NULL)
5719 return NULL;
5720
5721 // "catch" is set when the first ":catch" is found.
5722 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005723 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005724 if (generate_instr(cctx, ISN_TRY) == NULL)
5725 return NULL;
5726
5727 // scope for the try block itself
5728 scope = new_scope(cctx, BLOCK_SCOPE);
5729 if (scope == NULL)
5730 return NULL;
5731
5732 return arg;
5733}
5734
5735/*
5736 * compile "catch {expr}"
5737 */
5738 static char_u *
5739compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5740{
5741 scope_T *scope = cctx->ctx_scope;
5742 garray_T *instr = &cctx->ctx_instr;
5743 char_u *p;
5744 isn_T *isn;
5745
5746 // end block scope from :try or :catch
5747 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5748 compile_endblock(cctx);
5749 scope = cctx->ctx_scope;
5750
5751 // Error if not in a :try scope
5752 if (scope == NULL || scope->se_type != TRY_SCOPE)
5753 {
5754 emsg(_(e_catch));
5755 return NULL;
5756 }
5757
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005758 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005759 {
5760 emsg(_("E1033: catch unreachable after catch-all"));
5761 return NULL;
5762 }
5763
5764 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005765 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005766 JUMP_ALWAYS, cctx) == FAIL)
5767 return NULL;
5768
5769 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005770 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005771 if (isn->isn_arg.try.try_catch == 0)
5772 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005773 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005774 {
5775 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005776 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005777 isn->isn_arg.jump.jump_where = instr->ga_len;
5778 }
5779
5780 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02005781 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005782 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005783 scope->se_u.se_try.ts_caught_all = TRUE;
5784 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005785 }
5786 else
5787 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005788 char_u *end;
5789 char_u *pat;
5790 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005791 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005792 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005794 // Push v:exception, push {expr} and MATCH
5795 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5796
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005797 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005798 if (*end != *p)
5799 {
5800 semsg(_("E1067: Separator mismatch: %s"), p);
5801 vim_free(tofree);
5802 return FAIL;
5803 }
5804 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005805 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005806 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005807 len = (int)(end - tofree);
5808 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005809 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005810 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005811 if (pat == NULL)
5812 return FAIL;
5813 if (generate_PUSHS(cctx, pat) == FAIL)
5814 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005815
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005816 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5817 return NULL;
5818
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005819 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005820 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5821 return NULL;
5822 }
5823
5824 if (generate_instr(cctx, ISN_CATCH) == NULL)
5825 return NULL;
5826
5827 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5828 return NULL;
5829 return p;
5830}
5831
5832 static char_u *
5833compile_finally(char_u *arg, cctx_T *cctx)
5834{
5835 scope_T *scope = cctx->ctx_scope;
5836 garray_T *instr = &cctx->ctx_instr;
5837 isn_T *isn;
5838
5839 // end block scope from :try or :catch
5840 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5841 compile_endblock(cctx);
5842 scope = cctx->ctx_scope;
5843
5844 // Error if not in a :try scope
5845 if (scope == NULL || scope->se_type != TRY_SCOPE)
5846 {
5847 emsg(_(e_finally));
5848 return NULL;
5849 }
5850
5851 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005852 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005853 if (isn->isn_arg.try.try_finally != 0)
5854 {
5855 emsg(_(e_finally_dup));
5856 return NULL;
5857 }
5858
5859 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005860 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005861
Bram Moolenaar585fea72020-04-02 22:33:21 +02005862 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005863 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005864 {
5865 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005866 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005867 isn->isn_arg.jump.jump_where = instr->ga_len;
5868 }
5869
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005870 // TODO: set index in ts_finally_label jumps
5871
5872 return arg;
5873}
5874
5875 static char_u *
5876compile_endtry(char_u *arg, cctx_T *cctx)
5877{
5878 scope_T *scope = cctx->ctx_scope;
5879 garray_T *instr = &cctx->ctx_instr;
5880 isn_T *isn;
5881
5882 // end block scope from :catch or :finally
5883 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5884 compile_endblock(cctx);
5885 scope = cctx->ctx_scope;
5886
5887 // Error if not in a :try scope
5888 if (scope == NULL || scope->se_type != TRY_SCOPE)
5889 {
5890 if (scope == NULL)
5891 emsg(_(e_no_endtry));
5892 else if (scope->se_type == WHILE_SCOPE)
5893 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01005894 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895 emsg(_(e_endfor));
5896 else
5897 emsg(_(e_endif));
5898 return NULL;
5899 }
5900
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005901 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005902 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
5903 {
5904 emsg(_("E1032: missing :catch or :finally"));
5905 return NULL;
5906 }
5907
5908 // Fill in the "end" label in jumps at the end of the blocks, if not done
5909 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005910 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005911
5912 // End :catch or :finally scope: set value in ISN_TRY instruction
5913 if (isn->isn_arg.try.try_finally == 0)
5914 isn->isn_arg.try.try_finally = instr->ga_len;
5915 compile_endblock(cctx);
5916
5917 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
5918 return NULL;
5919 return arg;
5920}
5921
5922/*
5923 * compile "throw {expr}"
5924 */
5925 static char_u *
5926compile_throw(char_u *arg, cctx_T *cctx UNUSED)
5927{
5928 char_u *p = skipwhite(arg);
5929
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005930 if (compile_expr1(&p, cctx) == FAIL)
5931 return NULL;
5932 if (may_generate_2STRING(-1, cctx) == FAIL)
5933 return NULL;
5934 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
5935 return NULL;
5936
5937 return p;
5938}
5939
5940/*
5941 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005942 * compile "echomsg expr"
5943 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01005944 * compile "execute expr"
5945 */
5946 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005947compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01005948{
5949 char_u *p = arg;
5950 int count = 0;
5951
5952 for (;;)
5953 {
5954 if (compile_expr1(&p, cctx) == FAIL)
5955 return NULL;
5956 ++count;
5957 p = skipwhite(p);
5958 if (ends_excmd(*p))
5959 break;
5960 }
5961
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005962 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
5963 generate_ECHO(cctx, cmdidx == CMD_echo, count);
5964 else if (cmdidx == CMD_execute)
5965 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
5966 else if (cmdidx == CMD_echomsg)
5967 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
5968 else
5969 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005970 return p;
5971}
5972
5973/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005974 * A command that is not compiled, execute with legacy code.
5975 */
5976 static char_u *
5977compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
5978{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005979 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02005980 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005981
5982 if (cctx->ctx_skip == TRUE)
5983 goto theend;
5984
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02005985 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
5986 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005987 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
5988 {
5989 // expand filename in "syntax include [@group] filename"
5990 has_expr = TRUE;
5991 eap->arg = skipwhite(eap->arg + 7);
5992 if (*eap->arg == '@')
5993 eap->arg = skiptowhite(eap->arg);
5994 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005995
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02005996 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02005997 {
5998 int count = 0;
5999 char_u *start = skipwhite(line);
6000
6001 // :cmd xxx`=expr1`yyy`=expr2`zzz
6002 // PUSHS ":cmd xxx"
6003 // eval expr1
6004 // PUSHS "yyy"
6005 // eval expr2
6006 // PUSHS "zzz"
6007 // EXECCONCAT 5
6008 for (;;)
6009 {
6010 if (p > start)
6011 {
6012 generate_PUSHS(cctx, vim_strnsave(start, (int)(p - start)));
6013 ++count;
6014 }
6015 p += 2;
6016 if (compile_expr1(&p, cctx) == FAIL)
6017 return NULL;
6018 may_generate_2STRING(-1, cctx);
6019 ++count;
6020 p = skipwhite(p);
6021 if (*p != '`')
6022 {
6023 emsg(_("E1083: missing backtick"));
6024 return NULL;
6025 }
6026 start = p + 1;
6027
6028 p = (char_u *)strstr((char *)start, "`=");
6029 if (p == NULL)
6030 {
6031 if (*skipwhite(start) != NUL)
6032 {
6033 generate_PUSHS(cctx, vim_strsave(start));
6034 ++count;
6035 }
6036 break;
6037 }
6038 }
6039 generate_EXECCONCAT(cctx, count);
6040 }
6041 else
6042 generate_EXEC(cctx, line);
6043
6044theend:
6045 return (char_u *)"";
6046}
6047
6048/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006049 * After ex_function() has collected all the function lines: parse and compile
6050 * the lines into instructions.
6051 * Adds the function to "def_functions".
6052 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6053 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006054 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006055 * This can be used recursively through compile_lambda(), which may reallocate
6056 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006057 */
6058 void
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006059compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006060{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061 char_u *line = NULL;
6062 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006063 char *errormsg = NULL; // error message
6064 int had_return = FALSE;
6065 cctx_T cctx;
6066 garray_T *instr;
6067 int called_emsg_before = called_emsg;
6068 int ret = FAIL;
6069 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006070 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006072 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006073 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01006074
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006075 if (ufunc->uf_dfunc_idx >= 0)
6076 {
6077 // Redefining a function that was compiled before.
6078 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
6079
6080 // Free old instructions.
6081 delete_def_function_contents(dfunc);
6082 }
6083 else
6084 {
6085 // Add the function to "def_functions".
6086 if (ga_grow(&def_functions, 1) == FAIL)
6087 return;
6088 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006089 CLEAR_POINTER(dfunc);
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006090 dfunc->df_idx = def_functions.ga_len;
6091 ufunc->uf_dfunc_idx = dfunc->df_idx;
6092 dfunc->df_ufunc = ufunc;
6093 ++def_functions.ga_len;
6094 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006095 }
6096
Bram Moolenaara80faa82020-04-12 19:37:17 +02006097 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006098 cctx.ctx_ufunc = ufunc;
6099 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006100 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006101 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6102 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6103 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6104 cctx.ctx_type_list = &ufunc->uf_type_list;
6105 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6106 instr = &cctx.ctx_instr;
6107
6108 // Most modern script version.
6109 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6110
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006111 if (ufunc->uf_def_args.ga_len > 0)
6112 {
6113 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006114 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006115 int i;
6116 char_u *arg;
6117 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6118
6119 // Produce instructions for the default values of optional arguments.
6120 // Store the instruction index in uf_def_arg_idx[] so that we know
6121 // where to start when the function is called, depending on the number
6122 // of arguments.
6123 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6124 if (ufunc->uf_def_arg_idx == NULL)
6125 goto erret;
6126 for (i = 0; i < count; ++i)
6127 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006128 garray_T *stack = &cctx.ctx_type_stack;
6129 type_T *val_type;
6130 int arg_idx = first_def_arg + i;
6131
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006132 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6133 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006134 if (compile_expr1(&arg, &cctx) == FAIL)
6135 goto erret;
6136
6137 // If no type specified use the type of the default value.
6138 // Otherwise check that the default value type matches the
6139 // specified type.
6140 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6141 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6142 ufunc->uf_arg_types[arg_idx] = val_type;
6143 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6144 == FAIL)
6145 {
6146 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6147 arg_idx + 1);
6148 goto erret;
6149 }
6150
6151 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006152 goto erret;
6153 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006154 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6155 }
6156
6157 /*
6158 * Loop over all the lines of the function and generate instructions.
6159 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006160 for (;;)
6161 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006162 exarg_T ea;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006163 int is_ex_command = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006164
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006165 // Bail out on the first error to avoid a flood of errors and report
6166 // the right line number when inside try/catch.
6167 if (emsg_before != called_emsg)
6168 goto erret;
6169
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006170 if (line != NULL && *line == '|')
6171 // the line continues after a '|'
6172 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006173 else if (line != NULL && *line != NUL
6174 && !(*line == '#' && (line == cctx.ctx_line_start
6175 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006176 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006177 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006178 goto erret;
6179 }
6180 else
6181 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006182 line = next_line_from_context(&cctx);
6183 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006184 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006185 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006186 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006187 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006188
6189 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006190 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006191 ea.cmdlinep = &line;
6192 ea.cmd = skipwhite(line);
6193
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006194 // Some things can be recognized by the first character.
6195 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006196 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006197 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006198 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006199 if (ea.cmd[1] != '{')
6200 {
6201 line = (char_u *)"";
6202 continue;
6203 }
6204 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006205
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006206 case '}':
6207 {
6208 // "}" ends a block scope
6209 scopetype_T stype = cctx.ctx_scope == NULL
6210 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006211
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006212 if (stype == BLOCK_SCOPE)
6213 {
6214 compile_endblock(&cctx);
6215 line = ea.cmd;
6216 }
6217 else
6218 {
6219 emsg(_("E1025: using } outside of a block scope"));
6220 goto erret;
6221 }
6222 if (line != NULL)
6223 line = skipwhite(ea.cmd + 1);
6224 continue;
6225 }
6226
6227 case '{':
6228 // "{" starts a block scope
6229 // "{'a': 1}->func() is something else
6230 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6231 {
6232 line = compile_block(ea.cmd, &cctx);
6233 continue;
6234 }
6235 break;
6236
6237 case ':':
6238 is_ex_command = TRUE;
6239 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006240 }
6241
6242 /*
6243 * COMMAND MODIFIERS
6244 */
6245 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6246 {
6247 if (errormsg != NULL)
6248 goto erret;
6249 // empty line or comment
6250 line = (char_u *)"";
6251 continue;
6252 }
6253
6254 // Skip ":call" to get to the function name.
6255 if (checkforcmd(&ea.cmd, "call", 3))
6256 ea.cmd = skipwhite(ea.cmd);
6257
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006258 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006260 // Assuming the command starts with a variable or function name,
6261 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
6262 // val".
6263 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
6264 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006265 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006266 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006268 int oplen;
6269 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006270
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006271 oplen = assignment_len(skipwhite(p), &heredoc);
6272 if (oplen > 0)
6273 {
6274 // Recognize an assignment if we recognize the variable
6275 // name:
6276 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006277 // "local = expr" where "local" is a local var.
6278 // "script = expr" where "script" is a script-local var.
6279 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006280 // "&opt = expr"
6281 // "$ENV = expr"
6282 // "@r = expr"
6283 if (*ea.cmd == '&'
6284 || *ea.cmd == '$'
6285 || *ea.cmd == '@'
6286 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006287 || lookup_local(ea.cmd, p - ea.cmd, &cctx) != NULL
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006288 || lookup_script(ea.cmd, p - ea.cmd) == OK
6289 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
6290 {
6291 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6292 if (line == NULL)
6293 goto erret;
6294 continue;
6295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296 }
6297 }
6298 }
6299
6300 /*
6301 * COMMAND after range
6302 */
6303 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006304 p = find_ex_command(&ea, NULL, is_ex_command ? NULL
6305 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006306 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307
6308 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6309 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006310 if (cctx.ctx_skip == TRUE)
6311 {
6312 line += STRLEN(line);
6313 continue;
6314 }
6315
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316 // Expression or function call.
6317 if (ea.cmdidx == CMD_eval)
6318 {
6319 p = ea.cmd;
6320 if (compile_expr1(&p, &cctx) == FAIL)
6321 goto erret;
6322
6323 // drop the return value
6324 generate_instr_drop(&cctx, ISN_DROP, 1);
6325 line = p;
6326 continue;
6327 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006328 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329 iemsg("Command from find_ex_command() not handled");
6330 goto erret;
6331 }
6332
6333 p = skipwhite(p);
6334
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006335 if (cctx.ctx_skip == TRUE
6336 && ea.cmdidx != CMD_elseif
6337 && ea.cmdidx != CMD_else
6338 && ea.cmdidx != CMD_endif)
6339 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006340 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006341 continue;
6342 }
6343
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006344 switch (ea.cmdidx)
6345 {
6346 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006347 ea.arg = p;
6348 line = compile_nested_function(&ea, &cctx);
6349 break;
6350
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006351 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006352 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 goto erret;
6354
6355 case CMD_return:
6356 line = compile_return(p, set_return_type, &cctx);
6357 had_return = TRUE;
6358 break;
6359
6360 case CMD_let:
6361 case CMD_const:
6362 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
6363 break;
6364
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006365 case CMD_unlet:
6366 case CMD_unlockvar:
6367 case CMD_lockvar:
6368 line = compile_unletlock(p, &ea, &cctx);
6369 break;
6370
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006371 case CMD_import:
6372 line = compile_import(p, &cctx);
6373 break;
6374
6375 case CMD_if:
6376 line = compile_if(p, &cctx);
6377 break;
6378 case CMD_elseif:
6379 line = compile_elseif(p, &cctx);
6380 break;
6381 case CMD_else:
6382 line = compile_else(p, &cctx);
6383 break;
6384 case CMD_endif:
6385 line = compile_endif(p, &cctx);
6386 break;
6387
6388 case CMD_while:
6389 line = compile_while(p, &cctx);
6390 break;
6391 case CMD_endwhile:
6392 line = compile_endwhile(p, &cctx);
6393 break;
6394
6395 case CMD_for:
6396 line = compile_for(p, &cctx);
6397 break;
6398 case CMD_endfor:
6399 line = compile_endfor(p, &cctx);
6400 break;
6401 case CMD_continue:
6402 line = compile_continue(p, &cctx);
6403 break;
6404 case CMD_break:
6405 line = compile_break(p, &cctx);
6406 break;
6407
6408 case CMD_try:
6409 line = compile_try(p, &cctx);
6410 break;
6411 case CMD_catch:
6412 line = compile_catch(p, &cctx);
6413 break;
6414 case CMD_finally:
6415 line = compile_finally(p, &cctx);
6416 break;
6417 case CMD_endtry:
6418 line = compile_endtry(p, &cctx);
6419 break;
6420 case CMD_throw:
6421 line = compile_throw(p, &cctx);
6422 break;
6423
6424 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006425 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006426 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006427 case CMD_echomsg:
6428 case CMD_echoerr:
6429 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006430 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006431
6432 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006433 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006434 // Not recognized, execute with do_cmdline_cmd().
6435 ea.arg = p;
6436 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006437 break;
6438 }
6439 if (line == NULL)
6440 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006441 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006442
6443 if (cctx.ctx_type_stack.ga_len < 0)
6444 {
6445 iemsg("Type stack underflow");
6446 goto erret;
6447 }
6448 }
6449
6450 if (cctx.ctx_scope != NULL)
6451 {
6452 if (cctx.ctx_scope->se_type == IF_SCOPE)
6453 emsg(_(e_endif));
6454 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6455 emsg(_(e_endwhile));
6456 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6457 emsg(_(e_endfor));
6458 else
6459 emsg(_("E1026: Missing }"));
6460 goto erret;
6461 }
6462
6463 if (!had_return)
6464 {
6465 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6466 {
6467 emsg(_("E1027: Missing return statement"));
6468 goto erret;
6469 }
6470
6471 // Return zero if there is no return at the end.
6472 generate_PUSHNR(&cctx, 0);
6473 generate_instr(&cctx, ISN_RETURN);
6474 }
6475
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006476 {
6477 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6478 + ufunc->uf_dfunc_idx;
6479 dfunc->df_deleted = FALSE;
6480 dfunc->df_instr = instr->ga_data;
6481 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006482 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006483 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006484 if (cctx.ctx_outer_used)
6485 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006486 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006487
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006488 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006489 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006490 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006491
6492 // Create a type for the function, with the return type and any
6493 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006494 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6495 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006496 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006497 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006498 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6499 argcount, &ufunc->uf_type_list);
6500 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006501 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006502 argcount + varargs,
6503 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006504 {
6505 ret = FAIL;
6506 goto erret;
6507 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006508 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6509 ufunc->uf_func_type->tt_min_argcount =
6510 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006511 if (ufunc->uf_arg_types == NULL)
6512 {
6513 int i;
6514
6515 // lambda does not have argument types.
6516 for (i = 0; i < argcount; ++i)
6517 ufunc->uf_func_type->tt_args[i] = &t_any;
6518 }
6519 else
6520 mch_memmove(ufunc->uf_func_type->tt_args,
6521 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006522 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006523 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006524 ufunc->uf_func_type->tt_args[argcount] =
6525 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006526 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6527 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006528 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006529 else
6530 // No arguments, can use a predefined type.
6531 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6532 argcount, &ufunc->uf_type_list);
6533
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006534 }
6535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006536 ret = OK;
6537
6538erret:
6539 if (ret == FAIL)
6540 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006541 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006542 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6543 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006544
6545 for (idx = 0; idx < instr->ga_len; ++idx)
6546 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006549 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006550 if (!dfunc->df_deleted)
6551 --def_functions.ga_len;
6552
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006553 while (cctx.ctx_scope != NULL)
6554 drop_scope(&cctx);
6555
Bram Moolenaar20431c92020-03-20 18:39:46 +01006556 // Don't execute this function body.
6557 ga_clear_strings(&ufunc->uf_lines);
6558
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006559 if (errormsg != NULL)
6560 emsg(errormsg);
6561 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006562 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006563 }
6564
6565 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006566 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006567 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006568 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006569}
6570
6571/*
6572 * Delete an instruction, free what it contains.
6573 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006574 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006575delete_instr(isn_T *isn)
6576{
6577 switch (isn->isn_type)
6578 {
6579 case ISN_EXEC:
6580 case ISN_LOADENV:
6581 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006582 case ISN_LOADB:
6583 case ISN_LOADW:
6584 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006585 case ISN_LOADOPT:
6586 case ISN_MEMBER:
6587 case ISN_PUSHEXC:
6588 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006589 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006590 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006591 case ISN_STOREB:
6592 case ISN_STOREW:
6593 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006594 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006595 vim_free(isn->isn_arg.string);
6596 break;
6597
6598 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006599 case ISN_STORES:
6600 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601 break;
6602
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006603 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006604 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006605 vim_free(isn->isn_arg.unlet.ul_name);
6606 break;
6607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608 case ISN_STOREOPT:
6609 vim_free(isn->isn_arg.storeopt.so_name);
6610 break;
6611
6612 case ISN_PUSHBLOB: // push blob isn_arg.blob
6613 blob_unref(isn->isn_arg.blob);
6614 break;
6615
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006616 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006617#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006618 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006619#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006620 break;
6621
6622 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006623#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006624 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006625#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006626 break;
6627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628 case ISN_UCALL:
6629 vim_free(isn->isn_arg.ufunc.cuf_name);
6630 break;
6631
6632 case ISN_2BOOL:
6633 case ISN_2STRING:
6634 case ISN_ADDBLOB:
6635 case ISN_ADDLIST:
6636 case ISN_BCALL:
6637 case ISN_CATCH:
6638 case ISN_CHECKNR:
6639 case ISN_CHECKTYPE:
6640 case ISN_COMPAREANY:
6641 case ISN_COMPAREBLOB:
6642 case ISN_COMPAREBOOL:
6643 case ISN_COMPAREDICT:
6644 case ISN_COMPAREFLOAT:
6645 case ISN_COMPAREFUNC:
6646 case ISN_COMPARELIST:
6647 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006648 case ISN_COMPARESPECIAL:
6649 case ISN_COMPARESTRING:
6650 case ISN_CONCAT:
6651 case ISN_DCALL:
6652 case ISN_DROP:
6653 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006654 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006655 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006656 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006657 case ISN_EXECCONCAT:
6658 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006659 case ISN_FOR:
6660 case ISN_FUNCREF:
6661 case ISN_INDEX:
6662 case ISN_JUMP:
6663 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006664 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665 case ISN_LOADSCRIPT:
6666 case ISN_LOADREG:
6667 case ISN_LOADV:
6668 case ISN_NEGATENR:
6669 case ISN_NEWDICT:
6670 case ISN_NEWLIST:
6671 case ISN_OPNR:
6672 case ISN_OPFLOAT:
6673 case ISN_OPANY:
6674 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006675 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006676 case ISN_PUSHF:
6677 case ISN_PUSHNR:
6678 case ISN_PUSHBOOL:
6679 case ISN_PUSHSPEC:
6680 case ISN_RETURN:
6681 case ISN_STORE:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006682 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006684 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685 case ISN_STORESCRIPT:
6686 case ISN_THROW:
6687 case ISN_TRY:
6688 // nothing allocated
6689 break;
6690 }
6691}
6692
6693/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006694 * Free all instructions for "dfunc".
6695 */
6696 static void
6697delete_def_function_contents(dfunc_T *dfunc)
6698{
6699 int idx;
6700
6701 ga_clear(&dfunc->df_def_args_isn);
6702
6703 if (dfunc->df_instr != NULL)
6704 {
6705 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6706 delete_instr(dfunc->df_instr + idx);
6707 VIM_CLEAR(dfunc->df_instr);
6708 }
6709
6710 dfunc->df_deleted = TRUE;
6711}
6712
6713/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006714 * When a user function is deleted, delete any associated def function.
6715 */
6716 void
6717delete_def_function(ufunc_T *ufunc)
6718{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006719 if (ufunc->uf_dfunc_idx >= 0)
6720 {
6721 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6722 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723
Bram Moolenaar20431c92020-03-20 18:39:46 +01006724 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006725 }
6726}
6727
6728#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006729/*
6730 * Free all functions defined with ":def".
6731 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006732 void
6733free_def_functions(void)
6734{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006735 int idx;
6736
6737 for (idx = 0; idx < def_functions.ga_len; ++idx)
6738 {
6739 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6740
6741 delete_def_function_contents(dfunc);
6742 }
6743
6744 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006745}
6746#endif
6747
6748
6749#endif // FEAT_EVAL