blob: 5f6ce7492be2db8be30ef276826cb23759019748 [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 Moolenaar61a89812020-05-07 16:58:17 +02001044 * Generate a PUSH instruction for "tv".
1045 */
1046 static int
1047generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1048{
1049 switch (tv->v_type)
1050 {
1051 case VAR_BOOL:
1052 generate_PUSHBOOL(cctx, tv->vval.v_number);
1053 break;
1054 case VAR_SPECIAL:
1055 generate_PUSHSPEC(cctx, tv->vval.v_number);
1056 break;
1057 case VAR_NUMBER:
1058 generate_PUSHNR(cctx, tv->vval.v_number);
1059 break;
1060#ifdef FEAT_FLOAT
1061 case VAR_FLOAT:
1062 generate_PUSHF(cctx, tv->vval.v_float);
1063 break;
1064#endif
1065 case VAR_BLOB:
1066 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1067 tv->vval.v_blob = NULL;
1068 break;
1069 case VAR_STRING:
1070 generate_PUSHS(cctx, tv->vval.v_string);
1071 tv->vval.v_string = NULL;
1072 break;
1073 default:
1074 iemsg("constant type not supported");
1075 return FAIL;
1076 }
1077 return OK;
1078}
1079
1080/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 * Generate an ISN_STORE instruction.
1082 */
1083 static int
1084generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1085{
1086 isn_T *isn;
1087
Bram Moolenaar080457c2020-03-03 21:53:32 +01001088 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001089 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1090 return FAIL;
1091 if (name != NULL)
1092 isn->isn_arg.string = vim_strsave(name);
1093 else
1094 isn->isn_arg.number = idx;
1095
1096 return OK;
1097}
1098
1099/*
1100 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1101 */
1102 static int
1103generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1104{
1105 isn_T *isn;
1106
Bram Moolenaar080457c2020-03-03 21:53:32 +01001107 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1109 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001110 isn->isn_arg.storenr.stnr_idx = idx;
1111 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112
1113 return OK;
1114}
1115
1116/*
1117 * Generate an ISN_STOREOPT instruction
1118 */
1119 static int
1120generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1121{
1122 isn_T *isn;
1123
Bram Moolenaar080457c2020-03-03 21:53:32 +01001124 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1126 return FAIL;
1127 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1128 isn->isn_arg.storeopt.so_flags = opt_flags;
1129
1130 return OK;
1131}
1132
1133/*
1134 * Generate an ISN_LOAD or similar instruction.
1135 */
1136 static int
1137generate_LOAD(
1138 cctx_T *cctx,
1139 isntype_T isn_type,
1140 int idx,
1141 char_u *name,
1142 type_T *type)
1143{
1144 isn_T *isn;
1145
Bram Moolenaar080457c2020-03-03 21:53:32 +01001146 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001147 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1148 return FAIL;
1149 if (name != NULL)
1150 isn->isn_arg.string = vim_strsave(name);
1151 else
1152 isn->isn_arg.number = idx;
1153
1154 return OK;
1155}
1156
1157/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001158 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001159 */
1160 static int
1161generate_LOADV(
1162 cctx_T *cctx,
1163 char_u *name,
1164 int error)
1165{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001166 int di_flags;
1167 int vidx = find_vim_var(name, &di_flags);
1168 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001169
Bram Moolenaar080457c2020-03-03 21:53:32 +01001170 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001171 if (vidx < 0)
1172 {
1173 if (error)
1174 semsg(_(e_var_notfound), name);
1175 return FAIL;
1176 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001177 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001178
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001179 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001180}
1181
1182/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001183 * Generate an ISN_UNLET instruction.
1184 */
1185 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001186generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001187{
1188 isn_T *isn;
1189
1190 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001191 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001192 return FAIL;
1193 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1194 isn->isn_arg.unlet.ul_forceit = forceit;
1195
1196 return OK;
1197}
1198
1199/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200 * Generate an ISN_LOADS instruction.
1201 */
1202 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001203generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001205 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001207 int sid,
1208 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209{
1210 isn_T *isn;
1211
Bram Moolenaar080457c2020-03-03 21:53:32 +01001212 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001213 if (isn_type == ISN_LOADS)
1214 isn = generate_instr_type(cctx, isn_type, type);
1215 else
1216 isn = generate_instr_drop(cctx, isn_type, 1);
1217 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001219 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1220 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221
1222 return OK;
1223}
1224
1225/*
1226 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1227 */
1228 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001229generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230 cctx_T *cctx,
1231 isntype_T isn_type,
1232 int sid,
1233 int idx,
1234 type_T *type)
1235{
1236 isn_T *isn;
1237
Bram Moolenaar080457c2020-03-03 21:53:32 +01001238 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 if (isn_type == ISN_LOADSCRIPT)
1240 isn = generate_instr_type(cctx, isn_type, type);
1241 else
1242 isn = generate_instr_drop(cctx, isn_type, 1);
1243 if (isn == NULL)
1244 return FAIL;
1245 isn->isn_arg.script.script_sid = sid;
1246 isn->isn_arg.script.script_idx = idx;
1247 return OK;
1248}
1249
1250/*
1251 * Generate an ISN_NEWLIST instruction.
1252 */
1253 static int
1254generate_NEWLIST(cctx_T *cctx, int count)
1255{
1256 isn_T *isn;
1257 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 type_T *type;
1259 type_T *member;
1260
Bram Moolenaar080457c2020-03-03 21:53:32 +01001261 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1263 return FAIL;
1264 isn->isn_arg.number = count;
1265
1266 // drop the value types
1267 stack->ga_len -= count;
1268
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001269 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001270 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 if (count > 0)
1272 member = ((type_T **)stack->ga_data)[stack->ga_len];
1273 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001274 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001275 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276
1277 // add the list type to the type stack
1278 if (ga_grow(stack, 1) == FAIL)
1279 return FAIL;
1280 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1281 ++stack->ga_len;
1282
1283 return OK;
1284}
1285
1286/*
1287 * Generate an ISN_NEWDICT instruction.
1288 */
1289 static int
1290generate_NEWDICT(cctx_T *cctx, int count)
1291{
1292 isn_T *isn;
1293 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001294 type_T *type;
1295 type_T *member;
1296
Bram Moolenaar080457c2020-03-03 21:53:32 +01001297 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1299 return FAIL;
1300 isn->isn_arg.number = count;
1301
1302 // drop the key and value types
1303 stack->ga_len -= 2 * count;
1304
Bram Moolenaar436472f2020-02-20 22:54:43 +01001305 // Use the first value type for the list member type. Use "void" for an
1306 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307 if (count > 0)
1308 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1309 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001310 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001311 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312
1313 // add the dict type to the type stack
1314 if (ga_grow(stack, 1) == FAIL)
1315 return FAIL;
1316 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1317 ++stack->ga_len;
1318
1319 return OK;
1320}
1321
1322/*
1323 * Generate an ISN_FUNCREF instruction.
1324 */
1325 static int
1326generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1327{
1328 isn_T *isn;
1329 garray_T *stack = &cctx->ctx_type_stack;
1330
Bram Moolenaar080457c2020-03-03 21:53:32 +01001331 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1333 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001334 isn->isn_arg.funcref.fr_func = dfunc_idx;
1335 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336
1337 if (ga_grow(stack, 1) == FAIL)
1338 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001339 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 // TODO: argument and return types
1341 ++stack->ga_len;
1342
1343 return OK;
1344}
1345
1346/*
1347 * Generate an ISN_JUMP instruction.
1348 */
1349 static int
1350generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1351{
1352 isn_T *isn;
1353 garray_T *stack = &cctx->ctx_type_stack;
1354
Bram Moolenaar080457c2020-03-03 21:53:32 +01001355 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1357 return FAIL;
1358 isn->isn_arg.jump.jump_when = when;
1359 isn->isn_arg.jump.jump_where = where;
1360
1361 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1362 --stack->ga_len;
1363
1364 return OK;
1365}
1366
1367 static int
1368generate_FOR(cctx_T *cctx, int loop_idx)
1369{
1370 isn_T *isn;
1371 garray_T *stack = &cctx->ctx_type_stack;
1372
Bram Moolenaar080457c2020-03-03 21:53:32 +01001373 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1375 return FAIL;
1376 isn->isn_arg.forloop.for_idx = loop_idx;
1377
1378 if (ga_grow(stack, 1) == FAIL)
1379 return FAIL;
1380 // type doesn't matter, will be stored next
1381 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1382 ++stack->ga_len;
1383
1384 return OK;
1385}
1386
1387/*
1388 * Generate an ISN_BCALL instruction.
1389 * Return FAIL if the number of arguments is wrong.
1390 */
1391 static int
1392generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1393{
1394 isn_T *isn;
1395 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001396 type_T *argtypes[MAX_FUNC_ARGS];
1397 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398
Bram Moolenaar080457c2020-03-03 21:53:32 +01001399 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 if (check_internal_func(func_idx, argcount) == FAIL)
1401 return FAIL;
1402
1403 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1404 return FAIL;
1405 isn->isn_arg.bfunc.cbf_idx = func_idx;
1406 isn->isn_arg.bfunc.cbf_argcount = argcount;
1407
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001408 for (i = 0; i < argcount; ++i)
1409 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 stack->ga_len -= argcount; // drop the arguments
1412 if (ga_grow(stack, 1) == FAIL)
1413 return FAIL;
1414 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001415 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 ++stack->ga_len; // add return value
1417
1418 return OK;
1419}
1420
1421/*
1422 * Generate an ISN_DCALL or ISN_UCALL instruction.
1423 * Return FAIL if the number of arguments is wrong.
1424 */
1425 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001426generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427{
1428 isn_T *isn;
1429 garray_T *stack = &cctx->ctx_type_stack;
1430 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001431 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432
Bram Moolenaar080457c2020-03-03 21:53:32 +01001433 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 if (argcount > regular_args && !has_varargs(ufunc))
1435 {
1436 semsg(_(e_toomanyarg), ufunc->uf_name);
1437 return FAIL;
1438 }
1439 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1440 {
1441 semsg(_(e_toofewarg), ufunc->uf_name);
1442 return FAIL;
1443 }
1444
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001445 if (ufunc->uf_dfunc_idx >= 0)
1446 {
1447 int i;
1448
1449 for (i = 0; i < argcount; ++i)
1450 {
1451 type_T *expected;
1452 type_T *actual;
1453
1454 if (i < regular_args)
1455 {
1456 if (ufunc->uf_arg_types == NULL)
1457 continue;
1458 expected = ufunc->uf_arg_types[i];
1459 }
1460 else
1461 expected = ufunc->uf_va_type->tt_member;
1462 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001463 if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001464 {
1465 arg_type_mismatch(expected, actual, i + 1);
1466 return FAIL;
1467 }
1468 }
1469 }
1470
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 if ((isn = generate_instr(cctx,
1472 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1473 return FAIL;
1474 if (ufunc->uf_dfunc_idx >= 0)
1475 {
1476 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1477 isn->isn_arg.dfunc.cdf_argcount = argcount;
1478 }
1479 else
1480 {
1481 // A user function may be deleted and redefined later, can't use the
1482 // ufunc pointer, need to look it up again at runtime.
1483 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1484 isn->isn_arg.ufunc.cuf_argcount = argcount;
1485 }
1486
1487 stack->ga_len -= argcount; // drop the arguments
1488 if (ga_grow(stack, 1) == FAIL)
1489 return FAIL;
1490 // add return value
1491 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1492 ++stack->ga_len;
1493
1494 return OK;
1495}
1496
1497/*
1498 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1499 */
1500 static int
1501generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1502{
1503 isn_T *isn;
1504 garray_T *stack = &cctx->ctx_type_stack;
1505
Bram Moolenaar080457c2020-03-03 21:53:32 +01001506 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1508 return FAIL;
1509 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1510 isn->isn_arg.ufunc.cuf_argcount = argcount;
1511
1512 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001513 if (ga_grow(stack, 1) == FAIL)
1514 return FAIL;
1515 // add return value
1516 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1517 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518
1519 return OK;
1520}
1521
1522/*
1523 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001524 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525 */
1526 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001527generate_PCALL(
1528 cctx_T *cctx,
1529 int argcount,
1530 char_u *name,
1531 type_T *type,
1532 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533{
1534 isn_T *isn;
1535 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001536 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537
Bram Moolenaar080457c2020-03-03 21:53:32 +01001538 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001539
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001540 if (type->tt_type == VAR_ANY)
1541 ret_type = &t_any;
1542 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001543 {
1544 if (type->tt_argcount != -1)
1545 {
1546 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1547
1548 if (argcount < type->tt_min_argcount - varargs)
1549 {
1550 semsg(_(e_toofewarg), "[reference]");
1551 return FAIL;
1552 }
1553 if (!varargs && argcount > type->tt_argcount)
1554 {
1555 semsg(_(e_toomanyarg), "[reference]");
1556 return FAIL;
1557 }
1558 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001559 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001560 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001561 else
1562 {
1563 semsg(_("E1085: Not a callable type: %s"), name);
1564 return FAIL;
1565 }
1566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001567 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1568 return FAIL;
1569 isn->isn_arg.pfunc.cpf_top = at_top;
1570 isn->isn_arg.pfunc.cpf_argcount = argcount;
1571
1572 stack->ga_len -= argcount; // drop the arguments
1573
1574 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001575 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001577 // If partial is above the arguments it must be cleared and replaced with
1578 // the return value.
1579 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1580 return FAIL;
1581
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582 return OK;
1583}
1584
1585/*
1586 * Generate an ISN_MEMBER instruction.
1587 */
1588 static int
1589generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1590{
1591 isn_T *isn;
1592 garray_T *stack = &cctx->ctx_type_stack;
1593 type_T *type;
1594
Bram Moolenaar080457c2020-03-03 21:53:32 +01001595 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1597 return FAIL;
1598 isn->isn_arg.string = vim_strnsave(name, (int)len);
1599
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001600 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001602 if (type->tt_type != VAR_DICT && type != &t_any)
1603 {
1604 emsg(_(e_dictreq));
1605 return FAIL;
1606 }
1607 // change dict type to dict member type
1608 if (type->tt_type == VAR_DICT)
1609 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610
1611 return OK;
1612}
1613
1614/*
1615 * Generate an ISN_ECHO instruction.
1616 */
1617 static int
1618generate_ECHO(cctx_T *cctx, int with_white, int count)
1619{
1620 isn_T *isn;
1621
Bram Moolenaar080457c2020-03-03 21:53:32 +01001622 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001623 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1624 return FAIL;
1625 isn->isn_arg.echo.echo_with_white = with_white;
1626 isn->isn_arg.echo.echo_count = count;
1627
1628 return OK;
1629}
1630
Bram Moolenaarad39c092020-02-26 18:23:43 +01001631/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001632 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001633 */
1634 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001635generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001636{
1637 isn_T *isn;
1638
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001639 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001640 return FAIL;
1641 isn->isn_arg.number = count;
1642
1643 return OK;
1644}
1645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 static int
1647generate_EXEC(cctx_T *cctx, char_u *line)
1648{
1649 isn_T *isn;
1650
Bram Moolenaar080457c2020-03-03 21:53:32 +01001651 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1653 return FAIL;
1654 isn->isn_arg.string = vim_strsave(line);
1655 return OK;
1656}
1657
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001658 static int
1659generate_EXECCONCAT(cctx_T *cctx, int count)
1660{
1661 isn_T *isn;
1662
1663 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1664 return FAIL;
1665 isn->isn_arg.number = count;
1666 return OK;
1667}
1668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669/*
1670 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001671 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001673 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1675{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 lvar_T *lvar;
1677
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001678 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 {
1680 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001681 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 }
1683
1684 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001685 return NULL;
1686 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001688 // Every local variable uses the next entry on the stack. We could re-use
1689 // the last ones when leaving a scope, but then variables used in a closure
1690 // might get overwritten. To keep things simple do not re-use stack
1691 // entries. This is less efficient, but memory is cheap these days.
1692 lvar->lv_idx = cctx->ctx_locals_count++;
1693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1695 lvar->lv_const = isConst;
1696 lvar->lv_type = type;
1697
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001698 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699}
1700
1701/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001702 * Remove local variables above "new_top".
1703 */
1704 static void
1705unwind_locals(cctx_T *cctx, int new_top)
1706{
1707 if (cctx->ctx_locals.ga_len > new_top)
1708 {
1709 int idx;
1710 lvar_T *lvar;
1711
1712 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1713 {
1714 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1715 vim_free(lvar->lv_name);
1716 }
1717 }
1718 cctx->ctx_locals.ga_len = new_top;
1719}
1720
1721/*
1722 * Free all local variables.
1723 */
1724 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001725free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001726{
1727 unwind_locals(cctx, 0);
1728 ga_clear(&cctx->ctx_locals);
1729}
1730
1731/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732 * Skip over a type definition and return a pointer to just after it.
1733 */
1734 char_u *
1735skip_type(char_u *start)
1736{
1737 char_u *p = start;
1738
1739 while (ASCII_ISALNUM(*p) || *p == '_')
1740 ++p;
1741
1742 // Skip over "<type>"; this is permissive about white space.
1743 if (*skipwhite(p) == '<')
1744 {
1745 p = skipwhite(p);
1746 p = skip_type(skipwhite(p + 1));
1747 p = skipwhite(p);
1748 if (*p == '>')
1749 ++p;
1750 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001751 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1752 {
1753 // handle func(args): type
1754 ++p;
1755 while (*p != ')' && *p != NUL)
1756 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001757 char_u *sp = p;
1758
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001759 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001760 if (p == sp)
1761 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001762 if (*p == ',')
1763 p = skipwhite(p + 1);
1764 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001765 if (*p == ')')
1766 {
1767 if (p[1] == ':')
1768 p = skip_type(skipwhite(p + 2));
1769 else
1770 p = skipwhite(p + 1);
1771 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001772 }
1773
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774 return p;
1775}
1776
1777/*
1778 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001779 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780 * Returns NULL in case of failure.
1781 */
1782 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001783parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784{
1785 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001786 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787
1788 if (**arg != '<')
1789 {
1790 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001791 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 else
1793 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001794 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 }
1796 *arg = skipwhite(*arg + 1);
1797
Bram Moolenaard77a8522020-04-03 21:59:57 +02001798 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799
1800 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001801 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 {
1803 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001804 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 }
1806 ++*arg;
1807
1808 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001809 return get_list_type(member_type, type_gap);
1810 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811}
1812
1813/*
1814 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001815 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816 */
1817 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001818parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819{
1820 char_u *p = *arg;
1821 size_t len;
1822
1823 // skip over the first word
1824 while (ASCII_ISALNUM(*p) || *p == '_')
1825 ++p;
1826 len = p - *arg;
1827
1828 switch (**arg)
1829 {
1830 case 'a':
1831 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1832 {
1833 *arg += len;
1834 return &t_any;
1835 }
1836 break;
1837 case 'b':
1838 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1839 {
1840 *arg += len;
1841 return &t_bool;
1842 }
1843 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1844 {
1845 *arg += len;
1846 return &t_blob;
1847 }
1848 break;
1849 case 'c':
1850 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1851 {
1852 *arg += len;
1853 return &t_channel;
1854 }
1855 break;
1856 case 'd':
1857 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1858 {
1859 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001860 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861 }
1862 break;
1863 case 'f':
1864 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1865 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001866#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001867 *arg += len;
1868 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001869#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001870 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001871 return &t_any;
1872#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873 }
1874 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1875 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001876 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001877 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001878 int argcount = -1;
1879 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001880 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001881 type_T *arg_type[MAX_FUNC_ARGS + 1];
1882
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001883 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001885 if (**arg == '(')
1886 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001887 // "func" may or may not return a value, "func()" does
1888 // not return a value.
1889 ret_type = &t_void;
1890
Bram Moolenaard77a8522020-04-03 21:59:57 +02001891 p = ++*arg;
1892 argcount = 0;
1893 while (*p != NUL && *p != ')')
1894 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001895 if (*p == '?')
1896 {
1897 if (first_optional == -1)
1898 first_optional = argcount;
1899 ++p;
1900 }
1901 else if (first_optional != -1)
1902 {
1903 emsg(_("E1007: mandatory argument after optional argument"));
1904 return &t_any;
1905 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001906 else if (STRNCMP(p, "...", 3) == 0)
1907 {
1908 flags |= TTFLAG_VARARGS;
1909 p += 3;
1910 }
1911
1912 arg_type[argcount++] = parse_type(&p, type_gap);
1913
1914 // Nothing comes after "...{type}".
1915 if (flags & TTFLAG_VARARGS)
1916 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001917
Bram Moolenaard77a8522020-04-03 21:59:57 +02001918 if (*p != ',' && *skipwhite(p) == ',')
1919 {
1920 semsg(_(e_no_white_before), ",");
1921 return &t_any;
1922 }
1923 if (*p == ',')
1924 {
1925 ++p;
1926 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001927 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001928 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001929 return &t_any;
1930 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001931 }
1932 p = skipwhite(p);
1933 if (argcount == MAX_FUNC_ARGS)
1934 {
1935 emsg(_("E740: Too many argument types"));
1936 return &t_any;
1937 }
1938 }
1939
1940 p = skipwhite(p);
1941 if (*p != ')')
1942 {
1943 emsg(_(e_missing_close));
1944 return &t_any;
1945 }
1946 *arg = p + 1;
1947 }
1948 if (**arg == ':')
1949 {
1950 // parse return type
1951 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001952 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001953 semsg(_(e_white_after), ":");
1954 *arg = skipwhite(*arg);
1955 ret_type = parse_type(arg, type_gap);
1956 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001957 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001958 type = get_func_type(ret_type, argcount, type_gap);
1959 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001960 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001961 type = alloc_func_type(ret_type, argcount, type_gap);
1962 type->tt_flags = flags;
1963 if (argcount > 0)
1964 {
1965 type->tt_argcount = argcount;
1966 type->tt_min_argcount = first_optional == -1
1967 ? argcount : first_optional;
1968 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001969 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001970 return &t_any;
1971 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001972 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001973 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001974 }
1975 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 }
1977 break;
1978 case 'j':
1979 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1980 {
1981 *arg += len;
1982 return &t_job;
1983 }
1984 break;
1985 case 'l':
1986 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1987 {
1988 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001989 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 }
1991 break;
1992 case 'n':
1993 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
1994 {
1995 *arg += len;
1996 return &t_number;
1997 }
1998 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 case 's':
2000 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2001 {
2002 *arg += len;
2003 return &t_string;
2004 }
2005 break;
2006 case 'v':
2007 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2008 {
2009 *arg += len;
2010 return &t_void;
2011 }
2012 break;
2013 }
2014
2015 semsg(_("E1010: Type not recognized: %s"), *arg);
2016 return &t_any;
2017}
2018
2019/*
2020 * Check if "type1" and "type2" are exactly the same.
2021 */
2022 static int
2023equal_type(type_T *type1, type_T *type2)
2024{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002025 int i;
2026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 if (type1->tt_type != type2->tt_type)
2028 return FALSE;
2029 switch (type1->tt_type)
2030 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002032 case VAR_ANY:
2033 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034 case VAR_SPECIAL:
2035 case VAR_BOOL:
2036 case VAR_NUMBER:
2037 case VAR_FLOAT:
2038 case VAR_STRING:
2039 case VAR_BLOB:
2040 case VAR_JOB:
2041 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002042 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043 case VAR_LIST:
2044 case VAR_DICT:
2045 return equal_type(type1->tt_member, type2->tt_member);
2046 case VAR_FUNC:
2047 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002048 if (!equal_type(type1->tt_member, type2->tt_member)
2049 || type1->tt_argcount != type2->tt_argcount)
2050 return FALSE;
2051 if (type1->tt_argcount < 0
2052 || type1->tt_args == NULL || type2->tt_args == NULL)
2053 return TRUE;
2054 for (i = 0; i < type1->tt_argcount; ++i)
2055 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2056 return FALSE;
2057 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 }
2059 return TRUE;
2060}
2061
2062/*
2063 * Find the common type of "type1" and "type2" and put it in "dest".
2064 * "type2" and "dest" may be the same.
2065 */
2066 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002067common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068{
2069 if (equal_type(type1, type2))
2070 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002071 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 return;
2073 }
2074
2075 if (type1->tt_type == type2->tt_type)
2076 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002077 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2078 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002079 type_T *common;
2080
Bram Moolenaard77a8522020-04-03 21:59:57 +02002081 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002082 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002083 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002084 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002085 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 return;
2087 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002088 if (type1->tt_type == VAR_FUNC)
2089 {
2090 type_T *common;
2091
2092 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2093 if (type1->tt_argcount == type2->tt_argcount
2094 && type1->tt_argcount >= 0)
2095 {
2096 int argcount = type1->tt_argcount;
2097 int i;
2098
2099 *dest = alloc_func_type(common, argcount, type_gap);
2100 if (type1->tt_args != NULL && type2->tt_args != NULL)
2101 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002102 if (func_type_add_arg_types(*dest, argcount,
2103 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002104 for (i = 0; i < argcount; ++i)
2105 common_type(type1->tt_args[i], type2->tt_args[i],
2106 &(*dest)->tt_args[i], type_gap);
2107 }
2108 }
2109 else
2110 *dest = alloc_func_type(common, -1, type_gap);
2111 return;
2112 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 }
2114
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002115 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116}
2117
2118 char *
2119vartype_name(vartype_T type)
2120{
2121 switch (type)
2122 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002123 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002124 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126 case VAR_SPECIAL: return "special";
2127 case VAR_BOOL: return "bool";
2128 case VAR_NUMBER: return "number";
2129 case VAR_FLOAT: return "float";
2130 case VAR_STRING: return "string";
2131 case VAR_BLOB: return "blob";
2132 case VAR_JOB: return "job";
2133 case VAR_CHANNEL: return "channel";
2134 case VAR_LIST: return "list";
2135 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002136
2137 case VAR_FUNC:
2138 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002139 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002140 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141}
2142
2143/*
2144 * Return the name of a type.
2145 * The result may be in allocated memory, in which case "tofree" is set.
2146 */
2147 char *
2148type_name(type_T *type, char **tofree)
2149{
2150 char *name = vartype_name(type->tt_type);
2151
2152 *tofree = NULL;
2153 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2154 {
2155 char *member_free;
2156 char *member_name = type_name(type->tt_member, &member_free);
2157 size_t len;
2158
2159 len = STRLEN(name) + STRLEN(member_name) + 3;
2160 *tofree = alloc(len);
2161 if (*tofree != NULL)
2162 {
2163 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2164 vim_free(member_free);
2165 return *tofree;
2166 }
2167 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002168 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002169 {
2170 garray_T ga;
2171 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002172 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002173
2174 ga_init2(&ga, 1, 100);
2175 if (ga_grow(&ga, 20) == FAIL)
2176 return "[unknown]";
2177 *tofree = ga.ga_data;
2178 STRCPY(ga.ga_data, "func(");
2179 ga.ga_len += 5;
2180
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002181 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002182 {
2183 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002184 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002185 int len;
2186
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002187 if (type->tt_args == NULL)
2188 arg_type = "[unknown]";
2189 else
2190 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002191 if (i > 0)
2192 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002193 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002194 ga.ga_len += 2;
2195 }
2196 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002197 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002198 {
2199 vim_free(arg_free);
2200 return "[unknown]";
2201 }
2202 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002203 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002204 {
2205 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2206 ga.ga_len += 3;
2207 }
2208 else if (i >= type->tt_min_argcount)
2209 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002210 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002211 ga.ga_len += len;
2212 vim_free(arg_free);
2213 }
2214
2215 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002216 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002217 else
2218 {
2219 char *ret_free;
2220 char *ret_name = type_name(type->tt_member, &ret_free);
2221 int len;
2222
2223 len = (int)STRLEN(ret_name) + 4;
2224 if (ga_grow(&ga, len) == FAIL)
2225 {
2226 vim_free(ret_free);
2227 return "[unknown]";
2228 }
2229 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002230 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2231 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002232 vim_free(ret_free);
2233 }
2234 return ga.ga_data;
2235 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236
2237 return name;
2238}
2239
2240/*
2241 * Find "name" in script-local items of script "sid".
2242 * Returns the index in "sn_var_vals" if found.
2243 * If found but not in "sn_var_vals" returns -1.
2244 * If not found returns -2.
2245 */
2246 int
2247get_script_item_idx(int sid, char_u *name, int check_writable)
2248{
2249 hashtab_T *ht;
2250 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002251 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252 int idx;
2253
2254 // First look the name up in the hashtable.
2255 if (sid <= 0 || sid > script_items.ga_len)
2256 return -1;
2257 ht = &SCRIPT_VARS(sid);
2258 di = find_var_in_ht(ht, 0, name, TRUE);
2259 if (di == NULL)
2260 return -2;
2261
2262 // Now find the svar_T index in sn_var_vals.
2263 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2264 {
2265 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2266
2267 if (sv->sv_tv == &di->di_tv)
2268 {
2269 if (check_writable && sv->sv_const)
2270 semsg(_(e_readonlyvar), name);
2271 return idx;
2272 }
2273 }
2274 return -1;
2275}
2276
2277/*
2278 * Find "name" in imported items of the current script/
2279 */
2280 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002281find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002282{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002283 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 int idx;
2285
2286 if (cctx != NULL)
2287 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2288 {
2289 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2290 + idx;
2291
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002292 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2293 : STRLEN(import->imp_name) == len
2294 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 return import;
2296 }
2297
2298 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2299 {
2300 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2301
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002302 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2303 : STRLEN(import->imp_name) == len
2304 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 return import;
2306 }
2307 return NULL;
2308}
2309
2310/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002311 * Free all imported variables.
2312 */
2313 static void
2314free_imported(cctx_T *cctx)
2315{
2316 int idx;
2317
2318 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2319 {
2320 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2321
2322 vim_free(import->imp_name);
2323 }
2324 ga_clear(&cctx->ctx_imports);
2325}
2326
2327/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002328 * Get the next line of the function from "cctx".
2329 * Returns NULL when at the end.
2330 */
2331 static char_u *
2332next_line_from_context(cctx_T *cctx)
2333{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002334 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002335
2336 do
2337 {
2338 ++cctx->ctx_lnum;
2339 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002340 {
2341 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002342 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002343 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002344 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002345 cctx->ctx_line_start = line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002346 SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum
2347 + cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002348 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002349 return line;
2350}
2351
2352/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002353 * Return TRUE if "p" points at a "#" but not at "#{".
2354 */
2355 static int
2356comment_start(char_u *p)
2357{
2358 return p[0] == '#' && p[1] != '{';
2359}
2360
2361/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002362 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002363 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002364 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2365 */
2366 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002367may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002368{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002369 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002370 {
2371 char_u *next = next_line_from_context(cctx);
2372
2373 if (next == NULL)
2374 return FAIL;
2375 *arg = skipwhite(next);
2376 }
2377 return OK;
2378}
2379
2380/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002381 * Generate an instruction to load script-local variable "name", without the
2382 * leading "s:".
2383 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 */
2385 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002386compile_load_scriptvar(
2387 cctx_T *cctx,
2388 char_u *name, // variable NUL terminated
2389 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002390 char_u **end, // end of variable
2391 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002393 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2395 imported_T *import;
2396
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002397 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002399 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002400 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2401 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 }
2403 if (idx >= 0)
2404 {
2405 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2406
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002407 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408 current_sctx.sc_sid, idx, sv->sv_type);
2409 return OK;
2410 }
2411
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002412 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 if (import != NULL)
2414 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002415 if (import->imp_all)
2416 {
2417 char_u *p = skipwhite(*end);
2418 int name_len;
2419 ufunc_T *ufunc;
2420 type_T *type;
2421
2422 // Used "import * as Name", need to lookup the member.
2423 if (*p != '.')
2424 {
2425 semsg(_("E1060: expected dot after name: %s"), start);
2426 return FAIL;
2427 }
2428 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002429 if (VIM_ISWHITE(*p))
2430 {
2431 emsg(_("E1074: no white space allowed after dot"));
2432 return FAIL;
2433 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002434
2435 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2436 // TODO: what if it is a function?
2437 if (idx < 0)
2438 return FAIL;
2439 *end = p;
2440
2441 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2442 import->imp_sid,
2443 idx,
2444 type);
2445 }
2446 else
2447 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002448 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002449 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2450 import->imp_sid,
2451 import->imp_var_vals_idx,
2452 import->imp_type);
2453 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 return OK;
2455 }
2456
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002457 if (error)
2458 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459 return FAIL;
2460}
2461
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002462 static int
2463generate_funcref(cctx_T *cctx, char_u *name)
2464{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002465 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002466
2467 if (ufunc == NULL)
2468 return FAIL;
2469
2470 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2471}
2472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473/*
2474 * Compile a variable name into a load instruction.
2475 * "end" points to just after the name.
2476 * When "error" is FALSE do not give an error when not found.
2477 */
2478 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002479compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480{
2481 type_T *type;
2482 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002483 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002485 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486
2487 if (*(*arg + 1) == ':')
2488 {
2489 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002490 if (end <= *arg + 2)
2491 name = vim_strsave((char_u *)"[empty]");
2492 else
2493 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494 if (name == NULL)
2495 return FAIL;
2496
2497 if (**arg == 'v')
2498 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002499 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 }
2501 else if (**arg == 'g')
2502 {
2503 // Global variables can be defined later, thus we don't check if it
2504 // exists, give error at runtime.
2505 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2506 }
2507 else if (**arg == 's')
2508 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002509 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002511 else if (**arg == 'b')
2512 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002513 // Buffer-local variables can be defined later, thus we don't check
2514 // if it exists, give error at runtime.
2515 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002516 }
2517 else if (**arg == 'w')
2518 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002519 // Window-local variables can be defined later, thus we don't check
2520 // if it exists, give error at runtime.
2521 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002522 }
2523 else if (**arg == 't')
2524 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002525 // Tabpage-local variables can be defined later, thus we don't
2526 // check if it exists, give error at runtime.
2527 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002528 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 else
2530 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002531 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 goto theend;
2533 }
2534 }
2535 else
2536 {
2537 size_t len = end - *arg;
2538 int idx;
2539 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002540 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541
2542 name = vim_strnsave(*arg, end - *arg);
2543 if (name == NULL)
2544 return FAIL;
2545
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002546 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002548 if (!gen_load_outer)
2549 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 }
2551 else
2552 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002553 lvar_T *lvar = lookup_local(*arg, len, cctx);
2554
2555 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002557 type = lvar->lv_type;
2558 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002559 if (lvar->lv_from_outer)
2560 gen_load_outer = TRUE;
2561 else
2562 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 }
2564 else
2565 {
2566 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2567 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2568 res = generate_PUSHBOOL(cctx, **arg == 't'
2569 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002570 else
2571 {
2572 // "var" can be script-local even without using "s:" if it
2573 // already exists.
2574 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2575 == SCRIPT_VERSION_VIM9
2576 || lookup_script(*arg, len) == OK)
2577 res = compile_load_scriptvar(cctx, name, *arg, &end,
2578 FALSE);
2579
2580 // When the name starts with an uppercase letter or "x:" it
2581 // can be a user defined function.
2582 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2583 res = generate_funcref(cctx, name);
2584 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 }
2586 }
2587 if (gen_load)
2588 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002589 if (gen_load_outer)
2590 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 }
2592
2593 *arg = end;
2594
2595theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002596 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 semsg(_(e_var_notfound), name);
2598 vim_free(name);
2599 return res;
2600}
2601
2602/*
2603 * Compile the argument expressions.
2604 * "arg" points to just after the "(" and is advanced to after the ")"
2605 */
2606 static int
2607compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2608{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002609 char_u *p = *arg;
2610 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611
Bram Moolenaare6085c52020-04-12 20:19:16 +02002612 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002614 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002615 {
2616 p = next_line_from_context(cctx);
2617 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002618 goto failret;
2619 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002620 p = skipwhite(p);
2621 }
2622 if (*p == ')')
2623 {
2624 *arg = p + 1;
2625 return OK;
2626 }
2627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 if (compile_expr1(&p, cctx) == FAIL)
2629 return FAIL;
2630 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002631
2632 if (*p != ',' && *skipwhite(p) == ',')
2633 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002634 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002635 p = skipwhite(p);
2636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002638 {
2639 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002640 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002641 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002642 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002643 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002644 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002646failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002647 emsg(_(e_missing_close));
2648 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649}
2650
2651/*
2652 * Compile a function call: name(arg1, arg2)
2653 * "arg" points to "name", "arg + varlen" to the "(".
2654 * "argcount_init" is 1 for "value->method()"
2655 * Instructions:
2656 * EVAL arg1
2657 * EVAL arg2
2658 * BCALL / DCALL / UCALL
2659 */
2660 static int
2661compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2662{
2663 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002664 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 int argcount = argcount_init;
2666 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002667 char_u fname_buf[FLEN_FIXED + 1];
2668 char_u *tofree = NULL;
2669 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002671 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672
2673 if (varlen >= sizeof(namebuf))
2674 {
2675 semsg(_("E1011: name too long: %s"), name);
2676 return FAIL;
2677 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002678 vim_strncpy(namebuf, *arg, varlen);
2679 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680
2681 *arg = skipwhite(*arg + varlen + 1);
2682 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002683 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002685 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 {
2687 int idx;
2688
2689 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002690 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002692 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002693 else
2694 semsg(_(e_unknownfunc), namebuf);
2695 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 }
2697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002699 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002701 {
2702 res = generate_CALL(cctx, ufunc, argcount);
2703 goto theend;
2704 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705
2706 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002707 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002709 if (STRNCMP(namebuf, "g:", 2) != 0
2710 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002711 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002712 garray_T *stack = &cctx->ctx_type_stack;
2713 type_T *type;
2714
2715 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2716 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002717 goto theend;
2718 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002720 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002721 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002722 if (STRNCMP(namebuf, "g:", 2) == 0)
2723 res = generate_UCALL(cctx, name, argcount);
2724 else
2725 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002726
2727theend:
2728 vim_free(tofree);
2729 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730}
2731
2732// like NAMESPACE_CHAR but with 'a' and 'l'.
2733#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2734
2735/*
2736 * Find the end of a variable or function name. Unlike find_name_end() this
2737 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002738 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 * Return a pointer to just after the name. Equal to "arg" if there is no
2740 * valid name.
2741 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002742 static char_u *
2743to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744{
2745 char_u *p;
2746
2747 // Quick check for valid starting character.
2748 if (!eval_isnamec1(*arg))
2749 return arg;
2750
2751 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2752 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2753 // and can be used in slice "[n:]".
2754 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002755 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2757 break;
2758 return p;
2759}
2760
2761/*
2762 * Like to_name_end() but also skip over a list or dict constant.
2763 */
2764 char_u *
2765to_name_const_end(char_u *arg)
2766{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002767 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 typval_T rettv;
2769
2770 if (p == arg && *arg == '[')
2771 {
2772
2773 // Can be "[1, 2, 3]->Func()".
2774 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2775 p = arg;
2776 }
2777 else if (p == arg && *arg == '#' && arg[1] == '{')
2778 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002779 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 ++p;
2781 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2782 p = arg;
2783 }
2784 else if (p == arg && *arg == '{')
2785 {
2786 int ret = get_lambda_tv(&p, &rettv, FALSE);
2787
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002788 // Can be "{x -> ret}()".
2789 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 if (ret == NOTDONE)
2791 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2792 if (ret != OK)
2793 p = arg;
2794 }
2795
2796 return p;
2797}
2798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799/*
2800 * parse a list: [expr, expr]
2801 * "*arg" points to the '['.
2802 */
2803 static int
2804compile_list(char_u **arg, cctx_T *cctx)
2805{
2806 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002807 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 int count = 0;
2809
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002810 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002812 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002813 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002814 p = next_line_from_context(cctx);
2815 if (p == NULL)
2816 {
2817 semsg(_(e_list_end), *arg);
2818 return FAIL;
2819 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002820 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002821 p = skipwhite(p);
2822 }
2823 if (*p == ']')
2824 {
2825 ++p;
2826 // Allow for following comment, after at least one space.
2827 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
2828 p += STRLEN(p);
2829 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002830 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 if (compile_expr1(&p, cctx) == FAIL)
2832 break;
2833 ++count;
2834 if (*p == ',')
2835 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002836 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 p = skipwhite(p);
2838 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002839 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840
2841 generate_NEWLIST(cctx, count);
2842 return OK;
2843}
2844
2845/*
2846 * parse a lambda: {arg, arg -> expr}
2847 * "*arg" points to the '{'.
2848 */
2849 static int
2850compile_lambda(char_u **arg, cctx_T *cctx)
2851{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 typval_T rettv;
2853 ufunc_T *ufunc;
2854
2855 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002856 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002858
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002860 ++ufunc->uf_refcount;
2861 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002862 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863
2864 // The function will have one line: "return {expr}".
2865 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002866 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867
2868 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002869 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 return FAIL;
2871}
2872
2873/*
2874 * Compile a lamda call: expr->{lambda}(args)
2875 * "arg" points to the "{".
2876 */
2877 static int
2878compile_lambda_call(char_u **arg, cctx_T *cctx)
2879{
2880 ufunc_T *ufunc;
2881 typval_T rettv;
2882 int argcount = 1;
2883 int ret = FAIL;
2884
2885 // Get the funcref in "rettv".
2886 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2887 return FAIL;
2888
2889 if (**arg != '(')
2890 {
2891 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002892 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 else
2894 semsg(_(e_missing_paren), "lambda");
2895 clear_tv(&rettv);
2896 return FAIL;
2897 }
2898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 ufunc = rettv.vval.v_partial->pt_func;
2900 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002901 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002902 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002903
2904 // The function will have one line: "return {expr}".
2905 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002906 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907
2908 // compile the arguments
2909 *arg = skipwhite(*arg + 1);
2910 if (compile_arguments(arg, cctx, &argcount) == OK)
2911 // call the compiled function
2912 ret = generate_CALL(cctx, ufunc, argcount);
2913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 return ret;
2915}
2916
2917/*
2918 * parse a dict: {'key': val} or #{key: val}
2919 * "*arg" points to the '{'.
2920 */
2921 static int
2922compile_dict(char_u **arg, cctx_T *cctx, int literal)
2923{
2924 garray_T *instr = &cctx->ctx_instr;
2925 int count = 0;
2926 dict_T *d = dict_alloc();
2927 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002928 char_u *whitep = *arg;
2929 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930
2931 if (d == NULL)
2932 return FAIL;
2933 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002934 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 {
2936 char_u *key = NULL;
2937
Bram Moolenaar2c330432020-04-13 14:41:35 +02002938 while (**arg == NUL || (literal && **arg == '"')
2939 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002940 {
2941 *arg = next_line_from_context(cctx);
2942 if (*arg == NULL)
2943 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002944 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002945 *arg = skipwhite(*arg);
2946 }
2947
2948 if (**arg == '}')
2949 break;
2950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 if (literal)
2952 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002953 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954
Bram Moolenaar2c330432020-04-13 14:41:35 +02002955 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 {
2957 semsg(_("E1014: Invalid key: %s"), *arg);
2958 return FAIL;
2959 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002960 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 if (generate_PUSHS(cctx, key) == FAIL)
2962 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002963 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 }
2965 else
2966 {
2967 isn_T *isn;
2968
2969 if (compile_expr1(arg, cctx) == FAIL)
2970 return FAIL;
2971 // TODO: check type is string
2972 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2973 if (isn->isn_type == ISN_PUSHS)
2974 key = isn->isn_arg.string;
2975 }
2976
2977 // Check for duplicate keys, if using string keys.
2978 if (key != NULL)
2979 {
2980 item = dict_find(d, key, -1);
2981 if (item != NULL)
2982 {
2983 semsg(_(e_duplicate_key), key);
2984 goto failret;
2985 }
2986 item = dictitem_alloc(key);
2987 if (item != NULL)
2988 {
2989 item->di_tv.v_type = VAR_UNKNOWN;
2990 item->di_tv.v_lock = 0;
2991 if (dict_add(d, item) == FAIL)
2992 dictitem_free(item);
2993 }
2994 }
2995
2996 *arg = skipwhite(*arg);
2997 if (**arg != ':')
2998 {
2999 semsg(_(e_missing_dict_colon), *arg);
3000 return FAIL;
3001 }
3002
Bram Moolenaar2c330432020-04-13 14:41:35 +02003003 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003005 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003006 {
3007 *arg = next_line_from_context(cctx);
3008 if (*arg == NULL)
3009 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003010 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003011 *arg = skipwhite(*arg);
3012 }
3013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 if (compile_expr1(arg, cctx) == FAIL)
3015 return FAIL;
3016 ++count;
3017
Bram Moolenaar2c330432020-04-13 14:41:35 +02003018 whitep = *arg;
3019 p = skipwhite(*arg);
3020 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003021 {
3022 *arg = next_line_from_context(cctx);
3023 if (*arg == NULL)
3024 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003025 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003026 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003027 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003028 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 if (**arg == '}')
3030 break;
3031 if (**arg != ',')
3032 {
3033 semsg(_(e_missing_dict_comma), *arg);
3034 goto failret;
3035 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003036 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 *arg = skipwhite(*arg + 1);
3038 }
3039
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 *arg = *arg + 1;
3041
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003042 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003043 p = skipwhite(*arg);
3044 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003045 *arg += STRLEN(*arg);
3046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 dict_unref(d);
3048 return generate_NEWDICT(cctx, count);
3049
3050failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003051 if (*arg == NULL)
3052 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 dict_unref(d);
3054 return FAIL;
3055}
3056
3057/*
3058 * Compile "&option".
3059 */
3060 static int
3061compile_get_option(char_u **arg, cctx_T *cctx)
3062{
3063 typval_T rettv;
3064 char_u *start = *arg;
3065 int ret;
3066
3067 // parse the option and get the current value to get the type.
3068 rettv.v_type = VAR_UNKNOWN;
3069 ret = get_option_tv(arg, &rettv, TRUE);
3070 if (ret == OK)
3071 {
3072 // include the '&' in the name, get_option_tv() expects it.
3073 char_u *name = vim_strnsave(start, *arg - start);
3074 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3075
3076 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3077 vim_free(name);
3078 }
3079 clear_tv(&rettv);
3080
3081 return ret;
3082}
3083
3084/*
3085 * Compile "$VAR".
3086 */
3087 static int
3088compile_get_env(char_u **arg, cctx_T *cctx)
3089{
3090 char_u *start = *arg;
3091 int len;
3092 int ret;
3093 char_u *name;
3094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 ++*arg;
3096 len = get_env_len(arg);
3097 if (len == 0)
3098 {
3099 semsg(_(e_syntax_at), start - 1);
3100 return FAIL;
3101 }
3102
3103 // include the '$' in the name, get_env_tv() expects it.
3104 name = vim_strnsave(start, len + 1);
3105 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3106 vim_free(name);
3107 return ret;
3108}
3109
3110/*
3111 * Compile "@r".
3112 */
3113 static int
3114compile_get_register(char_u **arg, cctx_T *cctx)
3115{
3116 int ret;
3117
3118 ++*arg;
3119 if (**arg == NUL)
3120 {
3121 semsg(_(e_syntax_at), *arg - 1);
3122 return FAIL;
3123 }
3124 if (!valid_yank_reg(**arg, TRUE))
3125 {
3126 emsg_invreg(**arg);
3127 return FAIL;
3128 }
3129 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3130 ++*arg;
3131 return ret;
3132}
3133
3134/*
3135 * Apply leading '!', '-' and '+' to constant "rettv".
3136 */
3137 static int
3138apply_leader(typval_T *rettv, char_u *start, char_u *end)
3139{
3140 char_u *p = end;
3141
3142 // this works from end to start
3143 while (p > start)
3144 {
3145 --p;
3146 if (*p == '-' || *p == '+')
3147 {
3148 // only '-' has an effect, for '+' we only check the type
3149#ifdef FEAT_FLOAT
3150 if (rettv->v_type == VAR_FLOAT)
3151 {
3152 if (*p == '-')
3153 rettv->vval.v_float = -rettv->vval.v_float;
3154 }
3155 else
3156#endif
3157 {
3158 varnumber_T val;
3159 int error = FALSE;
3160
3161 // tv_get_number_chk() accepts a string, but we don't want that
3162 // here
3163 if (check_not_string(rettv) == FAIL)
3164 return FAIL;
3165 val = tv_get_number_chk(rettv, &error);
3166 clear_tv(rettv);
3167 if (error)
3168 return FAIL;
3169 if (*p == '-')
3170 val = -val;
3171 rettv->v_type = VAR_NUMBER;
3172 rettv->vval.v_number = val;
3173 }
3174 }
3175 else
3176 {
3177 int v = tv2bool(rettv);
3178
3179 // '!' is permissive in the type.
3180 clear_tv(rettv);
3181 rettv->v_type = VAR_BOOL;
3182 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3183 }
3184 }
3185 return OK;
3186}
3187
3188/*
3189 * Recognize v: variables that are constants and set "rettv".
3190 */
3191 static void
3192get_vim_constant(char_u **arg, typval_T *rettv)
3193{
3194 if (STRNCMP(*arg, "v:true", 6) == 0)
3195 {
3196 rettv->v_type = VAR_BOOL;
3197 rettv->vval.v_number = VVAL_TRUE;
3198 *arg += 6;
3199 }
3200 else if (STRNCMP(*arg, "v:false", 7) == 0)
3201 {
3202 rettv->v_type = VAR_BOOL;
3203 rettv->vval.v_number = VVAL_FALSE;
3204 *arg += 7;
3205 }
3206 else if (STRNCMP(*arg, "v:null", 6) == 0)
3207 {
3208 rettv->v_type = VAR_SPECIAL;
3209 rettv->vval.v_number = VVAL_NULL;
3210 *arg += 6;
3211 }
3212 else if (STRNCMP(*arg, "v:none", 6) == 0)
3213 {
3214 rettv->v_type = VAR_SPECIAL;
3215 rettv->vval.v_number = VVAL_NONE;
3216 *arg += 6;
3217 }
3218}
3219
3220/*
Bram Moolenaar61a89812020-05-07 16:58:17 +02003221 * Evaluate an expression that is a constant:
3222 * has(arg)
3223 *
3224 * Also handle:
3225 * ! in front logical NOT
3226 *
3227 * Return FAIL if the expression is not a constant.
3228 */
3229 static int
3230evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
3231{
3232 typval_T argvars[2];
3233 char_u *start_leader, *end_leader;
3234 int has_call = FALSE;
3235
3236 /*
3237 * Skip '!' characters. They are handled later.
3238 * TODO: '-' and '+' characters
3239 */
3240 start_leader = *arg;
3241 while (**arg == '!')
3242 *arg = skipwhite(*arg + 1);
3243 end_leader = *arg;
3244
3245 /*
3246 * Recognize only a few types of constants for now.
3247 */
3248 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
3249 {
3250 tv->v_type = VAR_BOOL;
3251 tv->vval.v_number = VVAL_TRUE;
3252 *arg += 4;
3253 return OK;
3254 }
3255 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
3256 {
3257 tv->v_type = VAR_BOOL;
3258 tv->vval.v_number = VVAL_FALSE;
3259 *arg += 5;
3260 return OK;
3261 }
3262
3263 if (STRNCMP("has(", *arg, 4) == 0)
3264 {
3265 has_call = TRUE;
3266 *arg = skipwhite(*arg + 4);
3267 }
3268
3269 if (**arg == '"')
3270 {
3271 if (get_string_tv(arg, tv, TRUE) == FAIL)
3272 return FAIL;
3273 }
3274 else if (**arg == '\'')
3275 {
3276 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
3277 return FAIL;
3278 }
3279 else
3280 return FAIL;
3281
3282 if (has_call)
3283 {
3284 *arg = skipwhite(*arg);
3285 if (**arg != ')')
3286 return FAIL;
3287 *arg = *arg + 1;
3288
3289 argvars[0] = *tv;
3290 argvars[1].v_type = VAR_UNKNOWN;
3291 tv->v_type = VAR_NUMBER;
3292 tv->vval.v_number = 0;
3293 f_has(argvars, tv);
3294 clear_tv(&argvars[0]);
3295
3296 while (start_leader < end_leader)
3297 {
3298 if (*start_leader == '!')
3299 tv->vval.v_number = !tv->vval.v_number;
3300 ++start_leader;
3301 }
3302 }
3303 else if (end_leader > start_leader)
3304 {
3305 clear_tv(tv);
3306 return FAIL;
3307 }
3308
3309 return OK;
3310}
3311
3312/*
3313 * * number multiplication
3314 * / number division
3315 * % number modulo
3316 */
3317 static int
3318evaluate_const_expr6(char_u **arg, cctx_T *cctx, typval_T *tv)
3319{
3320 char_u *op;
3321
3322 // get the first variable
3323 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
3324 return FAIL;
3325
3326 /*
3327 * Repeat computing, until no "*", "/" or "%" is following.
3328 */
3329 for (;;)
3330 {
3331 op = skipwhite(*arg);
3332 if (*op != '*' && *op != '/' && *op != '%')
3333 break;
3334 // TODO: not implemented yet.
3335 clear_tv(tv);
3336 return FAIL;
3337 }
3338 return OK;
3339}
3340
3341/*
3342 * + number addition
3343 * - number subtraction
3344 * .. string concatenation
3345 */
3346 static int
3347evaluate_const_expr5(char_u **arg, cctx_T *cctx, typval_T *tv)
3348{
3349 char_u *op;
3350 int oplen;
3351
3352 // get the first variable
3353 if (evaluate_const_expr6(arg, cctx, tv) == FAIL)
3354 return FAIL;
3355
3356 /*
3357 * Repeat computing, until no "+", "-" or ".." is following.
3358 */
3359 for (;;)
3360 {
3361 op = skipwhite(*arg);
3362 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3363 break;
3364 oplen = (*op == '.' ? 2 : 1);
3365
3366 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
3367 {
3368 clear_tv(tv);
3369 return FAIL;
3370 }
3371
3372 if (*op == '.' && tv->v_type == VAR_STRING)
3373 {
3374 typval_T tv2;
3375 size_t len1;
3376 char_u *s1, *s2;
3377
3378 tv2.v_type = VAR_UNKNOWN;
3379 *arg = skipwhite(op + oplen);
3380
3381 // TODO: what if we fail???
3382 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
3383 return FAIL;
3384
3385 // get the second variable
3386 if (evaluate_const_expr6(arg, cctx, &tv2) == FAIL)
3387 {
3388 clear_tv(tv);
3389 return FAIL;
3390 }
3391 if (tv2.v_type != VAR_STRING)
3392 {
3393 clear_tv(tv);
3394 clear_tv(&tv2);
3395 return FAIL;
3396 }
3397 s1 = tv->vval.v_string;
3398 len1 = STRLEN(s1);
3399 s2 = tv2.vval.v_string;
3400 tv->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3401 if (tv->vval.v_string == NULL)
3402 {
3403 vim_free(s1);
3404 vim_free(s2);
3405 return FAIL;
3406 }
3407 mch_memmove(tv->vval.v_string, s1, len1);
3408 STRCPY(tv->vval.v_string + len1, s2);
3409 continue;
3410 }
3411
3412 // TODO: Not implemented yet.
3413 clear_tv(tv);
3414 return FAIL;
3415 }
3416 return OK;
3417}
3418
3419 static exptype_T
3420get_compare_type(char_u *p, int *len, int *type_is)
3421{
3422 exptype_T type = EXPR_UNKNOWN;
3423 int i;
3424
3425 switch (p[0])
3426 {
3427 case '=': if (p[1] == '=')
3428 type = EXPR_EQUAL;
3429 else if (p[1] == '~')
3430 type = EXPR_MATCH;
3431 break;
3432 case '!': if (p[1] == '=')
3433 type = EXPR_NEQUAL;
3434 else if (p[1] == '~')
3435 type = EXPR_NOMATCH;
3436 break;
3437 case '>': if (p[1] != '=')
3438 {
3439 type = EXPR_GREATER;
3440 *len = 1;
3441 }
3442 else
3443 type = EXPR_GEQUAL;
3444 break;
3445 case '<': if (p[1] != '=')
3446 {
3447 type = EXPR_SMALLER;
3448 *len = 1;
3449 }
3450 else
3451 type = EXPR_SEQUAL;
3452 break;
3453 case 'i': if (p[1] == 's')
3454 {
3455 // "is" and "isnot"; but not a prefix of a name
3456 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3457 *len = 5;
3458 i = p[*len];
3459 if (!isalnum(i) && i != '_')
3460 {
3461 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3462 *type_is = TRUE;
3463 }
3464 }
3465 break;
3466 }
3467 return type;
3468}
3469
3470/*
3471 * Only comparing strings is supported right now.
3472 * expr5a == expr5b
3473 */
3474 static int
3475evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
3476{
3477 exptype_T type = EXPR_UNKNOWN;
3478 char_u *p;
3479 int len = 2;
3480 int type_is = FALSE;
3481
3482 // get the first variable
3483 if (evaluate_const_expr5(arg, cctx, tv) == FAIL)
3484 return FAIL;
3485
3486 p = skipwhite(*arg);
3487 type = get_compare_type(p, &len, &type_is);
3488
3489 /*
3490 * If there is a comparative operator, use it.
3491 */
3492 if (type != EXPR_UNKNOWN)
3493 {
3494 typval_T tv2;
3495 char_u *s1, *s2;
3496 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3497 int n;
3498
3499 // TODO: Only string == string is supported now
3500 if (tv->v_type != VAR_STRING)
3501 return FAIL;
3502 if (type != EXPR_EQUAL)
3503 return FAIL;
3504
3505 // get the second variable
3506 init_tv(&tv2);
3507 *arg = skipwhite(p + len);
3508 if (evaluate_const_expr5(arg, cctx, &tv2) == FAIL
3509 || tv2.v_type != VAR_STRING)
3510 {
3511 clear_tv(&tv2);
3512 return FAIL;
3513 }
3514 s1 = tv_get_string_buf(tv, buf1);
3515 s2 = tv_get_string_buf(&tv2, buf2);
3516 n = STRCMP(s1, s2);
3517 clear_tv(tv);
3518 clear_tv(&tv2);
3519 tv->v_type = VAR_BOOL;
3520 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
3521 }
3522
3523 return OK;
3524}
3525
3526static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
3527
3528/*
3529 * Compile constant || or &&.
3530 */
3531 static int
3532evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
3533{
3534 char_u *p = skipwhite(*arg);
3535 int opchar = *op;
3536
3537 if (p[0] == opchar && p[1] == opchar)
3538 {
3539 int val = tv2bool(tv);
3540
3541 /*
3542 * Repeat until there is no following "||" or "&&"
3543 */
3544 while (p[0] == opchar && p[1] == opchar)
3545 {
3546 typval_T tv2;
3547
3548 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3549 return FAIL;
3550
3551 // eval the next expression
3552 *arg = skipwhite(p + 2);
3553 tv2.v_type = VAR_UNKNOWN;
3554 tv2.v_lock = 0;
3555 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
3556 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
3557 {
3558 clear_tv(&tv2);
3559 return FAIL;
3560 }
3561 if ((opchar == '&') == val)
3562 {
3563 // false || tv2 or true && tv2: use tv2
3564 clear_tv(tv);
3565 *tv = tv2;
3566 val = tv2bool(tv);
3567 }
3568 else
3569 clear_tv(&tv2);
3570 p = skipwhite(*arg);
3571 }
3572 }
3573
3574 return OK;
3575}
3576
3577/*
3578 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
3579 * Return FAIL if the expression is not a constant.
3580 */
3581 static int
3582evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
3583{
3584 // evaluate the first expression
3585 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
3586 return FAIL;
3587
3588 // || and && work almost the same
3589 return evaluate_const_and_or(arg, cctx, "&&", tv);
3590}
3591
3592/*
3593 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
3594 * Return FAIL if the expression is not a constant.
3595 */
3596 static int
3597evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
3598{
3599 // evaluate the first expression
3600 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
3601 return FAIL;
3602
3603 // || and && work almost the same
3604 return evaluate_const_and_or(arg, cctx, "||", tv);
3605}
3606
3607/*
3608 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
3609 * E.g. for "has('feature')".
3610 * This does not produce error messages. "tv" should be cleared afterwards.
3611 * Return FAIL if the expression is not a constant.
3612 */
3613 static int
3614evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
3615{
3616 char_u *p;
3617
3618 // evaluate the first expression
3619 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
3620 return FAIL;
3621
3622 p = skipwhite(*arg);
3623 if (*p == '?')
3624 {
3625 int val = tv2bool(tv);
3626 typval_T tv2;
3627
3628 // require space before and after the ?
3629 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3630 return FAIL;
3631
3632 // evaluate the second expression; any type is accepted
3633 clear_tv(tv);
3634 *arg = skipwhite(p + 1);
3635 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
3636 return FAIL;
3637
3638 // Check for the ":".
3639 p = skipwhite(*arg);
3640 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3641 return FAIL;
3642
3643 // evaluate the third expression
3644 *arg = skipwhite(p + 1);
3645 tv2.v_type = VAR_UNKNOWN;
3646 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
3647 {
3648 clear_tv(&tv2);
3649 return FAIL;
3650 }
3651 if (val)
3652 {
3653 // use the expr after "?"
3654 clear_tv(&tv2);
3655 }
3656 else
3657 {
3658 // use the expr after ":"
3659 clear_tv(tv);
3660 *tv = tv2;
3661 }
3662 }
3663 return OK;
3664}
3665
3666/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 * Compile code to apply '-', '+' and '!'.
3668 */
3669 static int
3670compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3671{
3672 char_u *p = end;
3673
3674 // this works from end to start
3675 while (p > start)
3676 {
3677 --p;
3678 if (*p == '-' || *p == '+')
3679 {
3680 int negate = *p == '-';
3681 isn_T *isn;
3682
3683 // TODO: check type
3684 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3685 {
3686 --p;
3687 if (*p == '-')
3688 negate = !negate;
3689 }
3690 // only '-' has an effect, for '+' we only check the type
3691 if (negate)
3692 isn = generate_instr(cctx, ISN_NEGATENR);
3693 else
3694 isn = generate_instr(cctx, ISN_CHECKNR);
3695 if (isn == NULL)
3696 return FAIL;
3697 }
3698 else
3699 {
3700 int invert = TRUE;
3701
3702 while (p > start && p[-1] == '!')
3703 {
3704 --p;
3705 invert = !invert;
3706 }
3707 if (generate_2BOOL(cctx, invert) == FAIL)
3708 return FAIL;
3709 }
3710 }
3711 return OK;
3712}
3713
3714/*
3715 * Compile whatever comes after "name" or "name()".
3716 */
3717 static int
3718compile_subscript(
3719 char_u **arg,
3720 cctx_T *cctx,
3721 char_u **start_leader,
3722 char_u *end_leader)
3723{
3724 for (;;)
3725 {
3726 if (**arg == '(')
3727 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003728 garray_T *stack = &cctx->ctx_type_stack;
3729 type_T *type;
3730 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731
3732 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003733 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 *arg = skipwhite(*arg + 1);
3736 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3737 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003738 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 return FAIL;
3740 }
3741 else if (**arg == '-' && (*arg)[1] == '>')
3742 {
3743 char_u *p;
3744
3745 // something->method()
3746 // Apply the '!', '-' and '+' first:
3747 // -1.0->func() works like (-1.0)->func()
3748 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3749 return FAIL;
3750 *start_leader = end_leader; // don't apply again later
3751
3752 *arg = skipwhite(*arg + 2);
3753 if (**arg == '{')
3754 {
3755 // lambda call: list->{lambda}
3756 if (compile_lambda_call(arg, cctx) == FAIL)
3757 return FAIL;
3758 }
3759 else
3760 {
3761 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003762 p = *arg;
3763 if (ASCII_ISALPHA(*p) && p[1] == ':')
3764 p += 2;
3765 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 ;
3767 if (*p != '(')
3768 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003769 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 return FAIL;
3771 }
3772 // TODO: base value may not be the first argument
3773 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
3774 return FAIL;
3775 }
3776 }
3777 else if (**arg == '[')
3778 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01003779 garray_T *stack;
3780 type_T **typep;
3781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 // list index: list[123]
3783 // TODO: more arguments
3784 // TODO: dict member dict['name']
3785 *arg = skipwhite(*arg + 1);
3786 if (compile_expr1(arg, cctx) == FAIL)
3787 return FAIL;
3788
3789 if (**arg != ']')
3790 {
3791 emsg(_(e_missbrac));
3792 return FAIL;
3793 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003794 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795
3796 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3797 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003798 stack = &cctx->ctx_type_stack;
3799 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
3800 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
3801 {
3802 emsg(_(e_listreq));
3803 return FAIL;
3804 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01003805 if ((*typep)->tt_type == VAR_LIST)
3806 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 }
3808 else if (**arg == '.' && (*arg)[1] != '.')
3809 {
3810 char_u *p;
3811
3812 ++*arg;
3813 p = *arg;
3814 // dictionary member: dict.name
3815 if (eval_isnamec1(*p))
3816 while (eval_isnamec(*p))
3817 MB_PTR_ADV(p);
3818 if (p == *arg)
3819 {
3820 semsg(_(e_syntax_at), *arg);
3821 return FAIL;
3822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
3824 return FAIL;
3825 *arg = p;
3826 }
3827 else
3828 break;
3829 }
3830
3831 // TODO - see handle_subscript():
3832 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3833 // Don't do this when "Func" is already a partial that was bound
3834 // explicitly (pt_auto is FALSE).
3835
3836 return OK;
3837}
3838
3839/*
3840 * Compile an expression at "*p" and add instructions to "instr".
3841 * "p" is advanced until after the expression, skipping white space.
3842 *
3843 * This is the equivalent of eval1(), eval2(), etc.
3844 */
3845
3846/*
3847 * number number constant
3848 * 0zFFFFFFFF Blob constant
3849 * "string" string constant
3850 * 'string' literal string constant
3851 * &option-name option value
3852 * @r register contents
3853 * identifier variable value
3854 * function() function call
3855 * $VAR environment variable
3856 * (expression) nested expression
3857 * [expr, expr] List
3858 * {key: val, key: val} Dictionary
3859 * #{key: val, key: val} Dictionary with literal keys
3860 *
3861 * Also handle:
3862 * ! in front logical NOT
3863 * - in front unary minus
3864 * + in front unary plus (ignored)
3865 * trailing (arg) funcref/partial call
3866 * trailing [] subscript in String or List
3867 * trailing .name entry in Dictionary
3868 * trailing ->name() method call
3869 */
3870 static int
3871compile_expr7(char_u **arg, cctx_T *cctx)
3872{
3873 typval_T rettv;
3874 char_u *start_leader, *end_leader;
3875 int ret = OK;
3876
3877 /*
3878 * Skip '!', '-' and '+' characters. They are handled later.
3879 */
3880 start_leader = *arg;
3881 while (**arg == '!' || **arg == '-' || **arg == '+')
3882 *arg = skipwhite(*arg + 1);
3883 end_leader = *arg;
3884
3885 rettv.v_type = VAR_UNKNOWN;
3886 switch (**arg)
3887 {
3888 /*
3889 * Number constant.
3890 */
3891 case '0': // also for blob starting with 0z
3892 case '1':
3893 case '2':
3894 case '3':
3895 case '4':
3896 case '5':
3897 case '6':
3898 case '7':
3899 case '8':
3900 case '9':
3901 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
3902 return FAIL;
3903 break;
3904
3905 /*
3906 * String constant: "string".
3907 */
3908 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
3909 return FAIL;
3910 break;
3911
3912 /*
3913 * Literal string constant: 'str''ing'.
3914 */
3915 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
3916 return FAIL;
3917 break;
3918
3919 /*
3920 * Constant Vim variable.
3921 */
3922 case 'v': get_vim_constant(arg, &rettv);
3923 ret = NOTDONE;
3924 break;
3925
3926 /*
3927 * List: [expr, expr]
3928 */
3929 case '[': ret = compile_list(arg, cctx);
3930 break;
3931
3932 /*
3933 * Dictionary: #{key: val, key: val}
3934 */
3935 case '#': if ((*arg)[1] == '{')
3936 {
3937 ++*arg;
3938 ret = compile_dict(arg, cctx, TRUE);
3939 }
3940 else
3941 ret = NOTDONE;
3942 break;
3943
3944 /*
3945 * Lambda: {arg, arg -> expr}
3946 * Dictionary: {'key': val, 'key': val}
3947 */
3948 case '{': {
3949 char_u *start = skipwhite(*arg + 1);
3950
3951 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003952 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003954 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 if (ret != FAIL && *start == '>')
3956 ret = compile_lambda(arg, cctx);
3957 else
3958 ret = compile_dict(arg, cctx, FALSE);
3959 }
3960 break;
3961
3962 /*
3963 * Option value: &name
3964 */
3965 case '&': ret = compile_get_option(arg, cctx);
3966 break;
3967
3968 /*
3969 * Environment variable: $VAR.
3970 */
3971 case '$': ret = compile_get_env(arg, cctx);
3972 break;
3973
3974 /*
3975 * Register contents: @r.
3976 */
3977 case '@': ret = compile_get_register(arg, cctx);
3978 break;
3979 /*
3980 * nested expression: (expression).
3981 */
3982 case '(': *arg = skipwhite(*arg + 1);
3983 ret = compile_expr1(arg, cctx); // recursive!
3984 *arg = skipwhite(*arg);
3985 if (**arg == ')')
3986 ++*arg;
3987 else if (ret == OK)
3988 {
3989 emsg(_(e_missing_close));
3990 ret = FAIL;
3991 }
3992 break;
3993
3994 default: ret = NOTDONE;
3995 break;
3996 }
3997 if (ret == FAIL)
3998 return FAIL;
3999
4000 if (rettv.v_type != VAR_UNKNOWN)
4001 {
4002 // apply the '!', '-' and '+' before the constant
4003 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
4004 {
4005 clear_tv(&rettv);
4006 return FAIL;
4007 }
4008 start_leader = end_leader; // don't apply again below
4009
4010 // push constant
Bram Moolenaar61a89812020-05-07 16:58:17 +02004011 if (generate_tv_PUSH(cctx, &rettv) == FAIL)
4012 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 }
4014 else if (ret == NOTDONE)
4015 {
4016 char_u *p;
4017 int r;
4018
4019 if (!eval_isnamec1(**arg))
4020 {
4021 semsg(_("E1015: Name expected: %s"), *arg);
4022 return FAIL;
4023 }
4024
4025 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004026 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 if (*p == '(')
4028 r = compile_call(arg, p - *arg, cctx, 0);
4029 else
4030 r = compile_load(arg, p, cctx, TRUE);
4031 if (r == FAIL)
4032 return FAIL;
4033 }
4034
4035 if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL)
4036 return FAIL;
4037
4038 // Now deal with prefixed '-', '+' and '!', if not done already.
4039 return compile_leader(cctx, start_leader, end_leader);
4040}
4041
4042/*
4043 * * number multiplication
4044 * / number division
4045 * % number modulo
4046 */
4047 static int
4048compile_expr6(char_u **arg, cctx_T *cctx)
4049{
4050 char_u *op;
4051
4052 // get the first variable
4053 if (compile_expr7(arg, cctx) == FAIL)
4054 return FAIL;
4055
4056 /*
4057 * Repeat computing, until no "*", "/" or "%" is following.
4058 */
4059 for (;;)
4060 {
4061 op = skipwhite(*arg);
4062 if (*op != '*' && *op != '/' && *op != '%')
4063 break;
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004064 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 {
4066 char_u buf[3];
4067
4068 vim_strncpy(buf, op, 1);
4069 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004070 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 }
4072 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004073 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004074 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075
4076 // get the second variable
4077 if (compile_expr7(arg, cctx) == FAIL)
4078 return FAIL;
4079
4080 generate_two_op(cctx, op);
4081 }
4082
4083 return OK;
4084}
4085
4086/*
4087 * + number addition
4088 * - number subtraction
4089 * .. string concatenation
4090 */
4091 static int
4092compile_expr5(char_u **arg, cctx_T *cctx)
4093{
4094 char_u *op;
4095 int oplen;
4096
4097 // get the first variable
4098 if (compile_expr6(arg, cctx) == FAIL)
4099 return FAIL;
4100
4101 /*
4102 * Repeat computing, until no "+", "-" or ".." is following.
4103 */
4104 for (;;)
4105 {
4106 op = skipwhite(*arg);
4107 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
4108 break;
4109 oplen = (*op == '.' ? 2 : 1);
4110
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004111 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 {
4113 char_u buf[3];
4114
4115 vim_strncpy(buf, op, oplen);
4116 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004117 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 }
4119
4120 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004121 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004122 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123
4124 // get the second variable
4125 if (compile_expr6(arg, cctx) == FAIL)
4126 return FAIL;
4127
4128 if (*op == '.')
4129 {
4130 if (may_generate_2STRING(-2, cctx) == FAIL
4131 || may_generate_2STRING(-1, cctx) == FAIL)
4132 return FAIL;
4133 generate_instr_drop(cctx, ISN_CONCAT, 1);
4134 }
4135 else
4136 generate_two_op(cctx, op);
4137 }
4138
4139 return OK;
4140}
4141
4142/*
4143 * expr5a == expr5b
4144 * expr5a =~ expr5b
4145 * expr5a != expr5b
4146 * expr5a !~ expr5b
4147 * expr5a > expr5b
4148 * expr5a >= expr5b
4149 * expr5a < expr5b
4150 * expr5a <= expr5b
4151 * expr5a is expr5b
4152 * expr5a isnot expr5b
4153 *
4154 * Produces instructions:
4155 * EVAL expr5a Push result of "expr5a"
4156 * EVAL expr5b Push result of "expr5b"
4157 * COMPARE one of the compare instructions
4158 */
4159 static int
4160compile_expr4(char_u **arg, cctx_T *cctx)
4161{
4162 exptype_T type = EXPR_UNKNOWN;
4163 char_u *p;
4164 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 int type_is = FALSE;
4166
4167 // get the first variable
4168 if (compile_expr5(arg, cctx) == FAIL)
4169 return FAIL;
4170
4171 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004172 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173
4174 /*
4175 * If there is a comparative operator, use it.
4176 */
4177 if (type != EXPR_UNKNOWN)
4178 {
4179 int ic = FALSE; // Default: do not ignore case
4180
4181 if (type_is && (p[len] == '?' || p[len] == '#'))
4182 {
4183 semsg(_(e_invexpr2), *arg);
4184 return FAIL;
4185 }
4186 // extra question mark appended: ignore case
4187 if (p[len] == '?')
4188 {
4189 ic = TRUE;
4190 ++len;
4191 }
4192 // extra '#' appended: match case (ignored)
4193 else if (p[len] == '#')
4194 ++len;
4195 // nothing appended: match case
4196
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004197 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 {
4199 char_u buf[7];
4200
4201 vim_strncpy(buf, p, len);
4202 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004203 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 }
4205
4206 // get the second variable
4207 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004208 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004209 return FAIL;
4210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 if (compile_expr5(arg, cctx) == FAIL)
4212 return FAIL;
4213
4214 generate_COMPARE(cctx, type, ic);
4215 }
4216
4217 return OK;
4218}
4219
4220/*
4221 * Compile || or &&.
4222 */
4223 static int
4224compile_and_or(char_u **arg, cctx_T *cctx, char *op)
4225{
4226 char_u *p = skipwhite(*arg);
4227 int opchar = *op;
4228
4229 if (p[0] == opchar && p[1] == opchar)
4230 {
4231 garray_T *instr = &cctx->ctx_instr;
4232 garray_T end_ga;
4233
4234 /*
4235 * Repeat until there is no following "||" or "&&"
4236 */
4237 ga_init2(&end_ga, sizeof(int), 10);
4238 while (p[0] == opchar && p[1] == opchar)
4239 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004240 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4241 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004243 return FAIL;
4244 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245
4246 if (ga_grow(&end_ga, 1) == FAIL)
4247 {
4248 ga_clear(&end_ga);
4249 return FAIL;
4250 }
4251 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4252 ++end_ga.ga_len;
4253 generate_JUMP(cctx, opchar == '|'
4254 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4255
4256 // eval the next expression
4257 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004258 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004259 return FAIL;
4260
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 if ((opchar == '|' ? compile_expr3(arg, cctx)
4262 : compile_expr4(arg, cctx)) == FAIL)
4263 {
4264 ga_clear(&end_ga);
4265 return FAIL;
4266 }
4267 p = skipwhite(*arg);
4268 }
4269
4270 // Fill in the end label in all jumps.
4271 while (end_ga.ga_len > 0)
4272 {
4273 isn_T *isn;
4274
4275 --end_ga.ga_len;
4276 isn = ((isn_T *)instr->ga_data)
4277 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4278 isn->isn_arg.jump.jump_where = instr->ga_len;
4279 }
4280 ga_clear(&end_ga);
4281 }
4282
4283 return OK;
4284}
4285
4286/*
4287 * expr4a && expr4a && expr4a logical AND
4288 *
4289 * Produces instructions:
4290 * EVAL expr4a Push result of "expr4a"
4291 * JUMP_AND_KEEP_IF_FALSE end
4292 * EVAL expr4b Push result of "expr4b"
4293 * JUMP_AND_KEEP_IF_FALSE end
4294 * EVAL expr4c Push result of "expr4c"
4295 * end:
4296 */
4297 static int
4298compile_expr3(char_u **arg, cctx_T *cctx)
4299{
4300 // get the first variable
4301 if (compile_expr4(arg, cctx) == FAIL)
4302 return FAIL;
4303
4304 // || and && work almost the same
4305 return compile_and_or(arg, cctx, "&&");
4306}
4307
4308/*
4309 * expr3a || expr3b || expr3c logical OR
4310 *
4311 * Produces instructions:
4312 * EVAL expr3a Push result of "expr3a"
4313 * JUMP_AND_KEEP_IF_TRUE end
4314 * EVAL expr3b Push result of "expr3b"
4315 * JUMP_AND_KEEP_IF_TRUE end
4316 * EVAL expr3c Push result of "expr3c"
4317 * end:
4318 */
4319 static int
4320compile_expr2(char_u **arg, cctx_T *cctx)
4321{
4322 // eval the first expression
4323 if (compile_expr3(arg, cctx) == FAIL)
4324 return FAIL;
4325
4326 // || and && work almost the same
4327 return compile_and_or(arg, cctx, "||");
4328}
4329
4330/*
4331 * Toplevel expression: expr2 ? expr1a : expr1b
4332 *
4333 * Produces instructions:
4334 * EVAL expr2 Push result of "expr"
4335 * JUMP_IF_FALSE alt jump if false
4336 * EVAL expr1a
4337 * JUMP_ALWAYS end
4338 * alt: EVAL expr1b
4339 * end:
4340 */
4341 static int
4342compile_expr1(char_u **arg, cctx_T *cctx)
4343{
4344 char_u *p;
Bram Moolenaar61a89812020-05-07 16:58:17 +02004345 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346
Bram Moolenaar61a89812020-05-07 16:58:17 +02004347 // Evaluate the first expression.
4348 // First try parsing as a constant. If that works just one PUSH
Bram Moolenaar3df02f52020-05-03 15:47:33 +02004349 // instruction needs to be generated.
Bram Moolenaar61a89812020-05-07 16:58:17 +02004350 tv.v_type = VAR_UNKNOWN;
4351 p = *arg;
4352 if (evaluate_const_expr2(&p, cctx, &tv) == OK)
4353 {
4354 *arg = p;
4355 generate_tv_PUSH(cctx, &tv);
4356 }
4357 else if (compile_expr2(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 return FAIL;
4359
4360 p = skipwhite(*arg);
4361 if (*p == '?')
4362 {
4363 garray_T *instr = &cctx->ctx_instr;
4364 garray_T *stack = &cctx->ctx_type_stack;
4365 int alt_idx = instr->ga_len;
4366 int end_idx;
4367 isn_T *isn;
4368 type_T *type1;
4369 type_T *type2;
4370
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004371 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4372 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004374 return FAIL;
4375 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376
4377 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4378
4379 // evaluate the second expression; any type is accepted
4380 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004381 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004382 return FAIL;
4383
Bram Moolenaara6d53682020-01-28 23:04:06 +01004384 if (compile_expr1(arg, cctx) == FAIL)
4385 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386
4387 // remember the type and drop it
4388 --stack->ga_len;
4389 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
4390
4391 end_idx = instr->ga_len;
4392 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4393
4394 // jump here from JUMP_IF_FALSE
4395 isn = ((isn_T *)instr->ga_data) + alt_idx;
4396 isn->isn_arg.jump.jump_where = instr->ga_len;
4397
4398 // Check for the ":".
4399 p = skipwhite(*arg);
4400 if (*p != ':')
4401 {
4402 emsg(_(e_missing_colon));
4403 return FAIL;
4404 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004405 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4406 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004408 return FAIL;
4409 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410
4411 // evaluate the third expression
4412 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004413 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004414 return FAIL;
4415
Bram Moolenaara6d53682020-01-28 23:04:06 +01004416 if (compile_expr1(arg, cctx) == FAIL)
4417 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418
4419 // If the types differ, the result has a more generic type.
4420 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004421 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422
4423 // jump here from JUMP_ALWAYS
4424 isn = ((isn_T *)instr->ga_data) + end_idx;
4425 isn->isn_arg.jump.jump_where = instr->ga_len;
4426 }
4427 return OK;
4428}
4429
4430/*
4431 * compile "return [expr]"
4432 */
4433 static char_u *
4434compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4435{
4436 char_u *p = arg;
4437 garray_T *stack = &cctx->ctx_type_stack;
4438 type_T *stack_type;
4439
4440 if (*p != NUL && *p != '|' && *p != '\n')
4441 {
4442 // compile return argument into instructions
4443 if (compile_expr1(&p, cctx) == FAIL)
4444 return NULL;
4445
4446 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4447 if (set_return_type)
4448 cctx->ctx_ufunc->uf_ret_type = stack_type;
4449 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4450 == FAIL)
4451 return NULL;
4452 }
4453 else
4454 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004455 // "set_return_type" cannot be TRUE, only used for a lambda which
4456 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004457 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4458 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 {
4460 emsg(_("E1003: Missing return value"));
4461 return NULL;
4462 }
4463
4464 // No argument, return zero.
4465 generate_PUSHNR(cctx, 0);
4466 }
4467
4468 if (generate_instr(cctx, ISN_RETURN) == NULL)
4469 return NULL;
4470
4471 // "return val | endif" is possible
4472 return skipwhite(p);
4473}
4474
4475/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004476 * Get a line from the compilation context, compatible with exarg_T getline().
4477 * Return a pointer to the line in allocated memory.
4478 * Return NULL for end-of-file or some error.
4479 */
4480 static char_u *
4481exarg_getline(
4482 int c UNUSED,
4483 void *cookie,
4484 int indent UNUSED,
4485 int do_concat UNUSED)
4486{
4487 cctx_T *cctx = (cctx_T *)cookie;
4488
4489 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4490 {
4491 iemsg("Heredoc got to end");
4492 return NULL;
4493 }
4494 ++cctx->ctx_lnum;
4495 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4496 [cctx->ctx_lnum]);
4497}
4498
4499/*
4500 * Compile a nested :def command.
4501 */
4502 static char_u *
4503compile_nested_function(exarg_T *eap, cctx_T *cctx)
4504{
4505 char_u *name_start = eap->arg;
4506 char_u *name_end = to_name_end(eap->arg, FALSE);
4507 char_u *name = get_lambda_name();
4508 lvar_T *lvar;
4509 ufunc_T *ufunc;
4510
4511 eap->arg = name_end;
4512 eap->getline = exarg_getline;
4513 eap->cookie = cctx;
4514 eap->skip = cctx->ctx_skip == TRUE;
4515 eap->forceit = FALSE;
4516 ufunc = def_function(eap, name, cctx);
4517
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004518 if (ufunc == NULL || ufunc->uf_dfunc_idx < 0)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004519 return NULL;
4520
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004521 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004522 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004523 TRUE, ufunc->uf_func_type);
4524
4525 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4526 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4527 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004528
Bram Moolenaar61a89812020-05-07 16:58:17 +02004529 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004530 return (char_u *)"";
4531}
4532
4533/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004534 * Return the length of an assignment operator, or zero if there isn't one.
4535 */
4536 int
4537assignment_len(char_u *p, int *heredoc)
4538{
4539 if (*p == '=')
4540 {
4541 if (p[1] == '<' && p[2] == '<')
4542 {
4543 *heredoc = TRUE;
4544 return 3;
4545 }
4546 return 1;
4547 }
4548 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4549 return 2;
4550 if (STRNCMP(p, "..=", 3) == 0)
4551 return 3;
4552 return 0;
4553}
4554
4555// words that cannot be used as a variable
4556static char *reserved[] = {
4557 "true",
4558 "false",
4559 NULL
4560};
4561
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004562typedef enum {
4563 dest_local,
4564 dest_option,
4565 dest_env,
4566 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004567 dest_buffer,
4568 dest_window,
4569 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004570 dest_vimvar,
4571 dest_script,
4572 dest_reg,
4573} assign_dest_T;
4574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575/*
4576 * compile "let var [= expr]", "const var = expr" and "var = expr"
4577 * "arg" points to "var".
4578 */
4579 static char_u *
4580compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4581{
4582 char_u *p;
4583 char_u *ret = NULL;
4584 int var_count = 0;
4585 int semicolon = 0;
4586 size_t varlen;
4587 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004588 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004591 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004593 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 int oplen = 0;
4595 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004596 type_T *type = &t_any;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004597 lvar_T *lvar = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 char_u *name;
4599 char_u *sp;
4600 int has_type = FALSE;
4601 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
4602 int instr_count = -1;
4603
4604 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
4605 if (p == NULL)
4606 return NULL;
4607 if (var_count > 0)
4608 {
4609 // TODO: let [var, var] = list
4610 emsg("Cannot handle a list yet");
4611 return NULL;
4612 }
4613
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004614 // "a: type" is declaring variable "a" with a type, not "a:".
4615 if (is_decl && p == arg + 2 && p[-1] == ':')
4616 --p;
4617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 varlen = p - arg;
4619 name = vim_strnsave(arg, (int)varlen);
4620 if (name == NULL)
4621 return NULL;
4622
Bram Moolenaar080457c2020-03-03 21:53:32 +01004623 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004625 if (*arg == '&')
4626 {
4627 int cc;
4628 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629
Bram Moolenaar080457c2020-03-03 21:53:32 +01004630 dest = dest_option;
4631 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004633 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004634 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 if (is_decl)
4637 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004638 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 goto theend;
4640 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004641 p = arg;
4642 p = find_option_end(&p, &opt_flags);
4643 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004645 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01004646 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004647 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004648 }
4649 cc = *p;
4650 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004651 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004652 *p = cc;
4653 if (opt_type == -3)
4654 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004655 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004656 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004657 }
4658 if (opt_type == -2 || opt_type == 0)
4659 type = &t_string;
4660 else
4661 type = &t_number; // both number and boolean option
4662 }
4663 else if (*arg == '$')
4664 {
4665 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004666 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004667 if (is_decl)
4668 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004669 semsg(_("E1065: Cannot declare an environment variable: %s"),
4670 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004671 goto theend;
4672 }
4673 }
4674 else if (*arg == '@')
4675 {
4676 if (!valid_yank_reg(arg[1], TRUE))
4677 {
4678 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004679 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004680 }
4681 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004682 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004683 if (is_decl)
4684 {
4685 semsg(_("E1066: Cannot declare a register: %s"), name);
4686 goto theend;
4687 }
4688 }
4689 else if (STRNCMP(arg, "g:", 2) == 0)
4690 {
4691 dest = dest_global;
4692 if (is_decl)
4693 {
4694 semsg(_("E1016: Cannot declare a global variable: %s"), name);
4695 goto theend;
4696 }
4697 }
Bram Moolenaard3aac292020-04-19 14:32:17 +02004698 else if (STRNCMP(arg, "b:", 2) == 0)
4699 {
4700 dest = dest_buffer;
4701 if (is_decl)
4702 {
4703 semsg(_("E1078: Cannot declare a buffer variable: %s"), name);
4704 goto theend;
4705 }
4706 }
4707 else if (STRNCMP(arg, "w:", 2) == 0)
4708 {
4709 dest = dest_window;
4710 if (is_decl)
4711 {
4712 semsg(_("E1079: Cannot declare a window variable: %s"), name);
4713 goto theend;
4714 }
4715 }
4716 else if (STRNCMP(arg, "t:", 2) == 0)
4717 {
4718 dest = dest_tab;
4719 if (is_decl)
4720 {
4721 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
4722 goto theend;
4723 }
4724 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004725 else if (STRNCMP(arg, "v:", 2) == 0)
4726 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004727 typval_T *vtv;
4728 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004729
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004730 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004731 if (vimvaridx < 0)
4732 {
4733 semsg(_(e_var_notfound), arg);
4734 goto theend;
4735 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004736 // We use the current value of "sandbox" here, is that OK?
4737 if (var_check_ro(di_flags, name, FALSE))
4738 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004739 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004740 vtv = get_vim_var_tv(vimvaridx);
4741 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004742 if (is_decl)
4743 {
4744 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
4745 goto theend;
4746 }
4747 }
4748 else
4749 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004750 int idx;
4751
Bram Moolenaar080457c2020-03-03 21:53:32 +01004752 for (idx = 0; reserved[idx] != NULL; ++idx)
4753 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004754 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004755 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004756 goto theend;
4757 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004758
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004759 lvar = lookup_local(arg, varlen, cctx);
4760 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004762 if (is_decl)
4763 {
4764 semsg(_("E1017: Variable already declared: %s"), name);
4765 goto theend;
4766 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004767 else if (lvar->lv_const)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004768 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004769 semsg(_("E1018: Cannot assign to a constant: %s"), name);
4770 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004771 }
4772 }
4773 else if (STRNCMP(arg, "s:", 2) == 0
4774 || lookup_script(arg, varlen) == OK
4775 || find_imported(arg, varlen, cctx) != NULL)
4776 {
4777 dest = dest_script;
4778 if (is_decl)
4779 {
4780 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004781 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004782 goto theend;
4783 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004785 else if (name[1] == ':' && name[2] != NUL)
4786 {
4787 semsg(_("E1082: Cannot use a namespaced variable: %s"), name);
4788 goto theend;
4789 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790 }
4791 }
4792
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004793 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794 {
4795 if (is_decl && *p == ':')
4796 {
4797 // parse optional type: "let var: type = expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004798 if (!VIM_ISWHITE(p[1]))
4799 {
4800 semsg(_(e_white_after), ":");
4801 goto theend;
4802 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004803 p = skipwhite(p + 1);
4804 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805 has_type = TRUE;
4806 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004807 else if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 type = lvar->lv_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 }
4810
4811 sp = p;
4812 p = skipwhite(p);
4813 op = p;
4814 oplen = assignment_len(p, &heredoc);
4815 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4816 {
4817 char_u buf[4];
4818
4819 vim_strncpy(buf, op, oplen);
4820 semsg(_(e_white_both), buf);
4821 }
4822
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004823 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02004824 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004826 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004827 goto theend;
4828 }
4829
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004830 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004831 {
4832 if (oplen > 1 && !heredoc)
4833 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004834 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4836 name);
4837 goto theend;
4838 }
4839
4840 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02004841 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004842 goto theend;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004843 lvar = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4844 if (lvar == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004846 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847 }
4848
4849 if (heredoc)
4850 {
4851 list_T *l;
4852 listitem_T *li;
4853
4854 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004855 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004857 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858
4859 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004860 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 {
4862 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4863 li->li_tv.vval.v_string = NULL;
4864 }
4865 generate_NEWLIST(cctx, l->lv_len);
4866 type = &t_list_string;
4867 list_free(l);
4868 p += STRLEN(p);
4869 }
4870 else if (oplen > 0)
4871 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004872 int r;
4873 type_T *stacktype;
4874 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 // for "+=", "*=", "..=" etc. first load the current value
4877 if (*op != '=')
4878 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004879 switch (dest)
4880 {
4881 case dest_option:
4882 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004883 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004884 break;
4885 case dest_global:
4886 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4887 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004888 case dest_buffer:
4889 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4890 break;
4891 case dest_window:
4892 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4893 break;
4894 case dest_tab:
4895 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4896 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004897 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01004898 compile_load_scriptvar(cctx,
4899 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004900 break;
4901 case dest_env:
4902 // Include $ in the name here
4903 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4904 break;
4905 case dest_reg:
4906 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
4907 break;
4908 case dest_vimvar:
4909 generate_LOADV(cctx, name + 2, TRUE);
4910 break;
4911 case dest_local:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004912 if (lvar->lv_from_outer)
4913 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4914 NULL, type);
4915 else
4916 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004917 break;
4918 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004919 }
4920
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004921 // Compile the expression. Temporarily hide the new local variable
4922 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02004923 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004924 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 instr_count = instr->ga_len;
4926 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004927 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02004928 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004929 ++cctx->ctx_locals.ga_len;
4930 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 goto theend;
4932
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004933 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004935 stack = &cctx->ctx_type_stack;
4936 stacktype = stack->ga_len == 0 ? &t_void
4937 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004938 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004939 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004940 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004942 if (stacktype->tt_type == VAR_VOID)
4943 {
4944 emsg(_("E1031: Cannot use void value"));
4945 goto theend;
4946 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004947 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004948 {
4949 // An empty list or dict has a &t_void member, for a
4950 // variable that implies &t_any.
4951 if (stacktype == &t_list_empty)
4952 lvar->lv_type = &t_list_any;
4953 else if (stacktype == &t_dict_empty)
4954 lvar->lv_type = &t_dict_any;
4955 else
4956 lvar->lv_type = stacktype;
4957 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004958 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004959 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
4960 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004962 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004963 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964 }
4965 }
4966 else if (cmdidx == CMD_const)
4967 {
4968 emsg(_("E1021: const requires a value"));
4969 goto theend;
4970 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004971 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972 {
4973 emsg(_("E1022: type or initialization required"));
4974 goto theend;
4975 }
4976 else
4977 {
4978 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979 if (ga_grow(instr, 1) == FAIL)
4980 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004981 switch (type->tt_type)
4982 {
4983 case VAR_BOOL:
4984 generate_PUSHBOOL(cctx, VVAL_FALSE);
4985 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01004986 case VAR_FLOAT:
4987#ifdef FEAT_FLOAT
4988 generate_PUSHF(cctx, 0.0);
4989#endif
4990 break;
4991 case VAR_STRING:
4992 generate_PUSHS(cctx, NULL);
4993 break;
4994 case VAR_BLOB:
4995 generate_PUSHBLOB(cctx, NULL);
4996 break;
4997 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004998 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01004999 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005000 case VAR_LIST:
5001 generate_NEWLIST(cctx, 0);
5002 break;
5003 case VAR_DICT:
5004 generate_NEWDICT(cctx, 0);
5005 break;
5006 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005007 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005008 break;
5009 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005010 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005011 break;
5012 case VAR_NUMBER:
5013 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005014 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02005015 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01005016 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02005017 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01005018 generate_PUSHNR(cctx, 0);
5019 break;
5020 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005021 }
5022
5023 if (oplen > 0 && *op != '=')
5024 {
5025 type_T *expected = &t_number;
5026 garray_T *stack = &cctx->ctx_type_stack;
5027 type_T *stacktype;
5028
5029 // TODO: if type is known use float or any operation
5030
5031 if (*op == '.')
5032 expected = &t_string;
5033 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5034 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5035 goto theend;
5036
5037 if (*op == '.')
5038 generate_instr_drop(cctx, ISN_CONCAT, 1);
5039 else
5040 {
5041 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5042
5043 if (isn == NULL)
5044 goto theend;
5045 switch (*op)
5046 {
5047 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5048 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5049 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5050 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5051 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5052 }
5053 }
5054 }
5055
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005056 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005057 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005058 case dest_option:
5059 generate_STOREOPT(cctx, name + 1, opt_flags);
5060 break;
5061 case dest_global:
5062 // include g: with the name, easier to execute that way
5063 generate_STORE(cctx, ISN_STOREG, 0, name);
5064 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02005065 case dest_buffer:
5066 // include b: with the name, easier to execute that way
5067 generate_STORE(cctx, ISN_STOREB, 0, name);
5068 break;
5069 case dest_window:
5070 // include w: with the name, easier to execute that way
5071 generate_STORE(cctx, ISN_STOREW, 0, name);
5072 break;
5073 case dest_tab:
5074 // include t: with the name, easier to execute that way
5075 generate_STORE(cctx, ISN_STORET, 0, name);
5076 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005077 case dest_env:
5078 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5079 break;
5080 case dest_reg:
5081 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5082 break;
5083 case dest_vimvar:
5084 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5085 break;
5086 case dest_script:
5087 {
5088 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5089 imported_T *import = NULL;
5090 int sid = current_sctx.sc_sid;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005091 int idx;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005092
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005093 if (name[1] != ':')
5094 {
5095 import = find_imported(name, 0, cctx);
5096 if (import != NULL)
5097 sid = import->imp_sid;
5098 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005099
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005100 idx = get_script_item_idx(sid, rawname, TRUE);
5101 // TODO: specific type
5102 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005103 {
5104 char_u *name_s = name;
5105
5106 // Include s: in the name for store_var()
5107 if (name[1] != ':')
5108 {
5109 int len = (int)STRLEN(name) + 3;
5110
5111 name_s = alloc(len);
5112 if (name_s == NULL)
5113 name_s = name;
5114 else
5115 vim_snprintf((char *)name_s, len, "s:%s", name);
5116 }
5117 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
5118 if (name_s != name)
5119 vim_free(name_s);
5120 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005121 else
5122 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5123 sid, idx, &t_any);
5124 }
5125 break;
5126 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005127 if (lvar != NULL)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005128 {
5129 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005131 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
5132 // into ISN_STORENR
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005133 if (!lvar->lv_from_outer && instr->ga_len == instr_count + 1
5134 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005135 {
5136 varnumber_T val = isn->isn_arg.number;
5137 garray_T *stack = &cctx->ctx_type_stack;
5138
5139 isn->isn_type = ISN_STORENR;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005140 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
Bram Moolenaara471eea2020-03-04 22:20:26 +01005141 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005142 if (stack->ga_len > 0)
5143 --stack->ga_len;
5144 }
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005145 else if (lvar->lv_from_outer)
5146 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005147 else
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005148 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005149 }
5150 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151 }
5152 ret = p;
5153
5154theend:
5155 vim_free(name);
5156 return ret;
5157}
5158
5159/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005160 * Check if "name" can be "unlet".
5161 */
5162 int
5163check_vim9_unlet(char_u *name)
5164{
5165 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5166 {
5167 semsg(_("E1081: Cannot unlet %s"), name);
5168 return FAIL;
5169 }
5170 return OK;
5171}
5172
5173/*
5174 * Callback passed to ex_unletlock().
5175 */
5176 static int
5177compile_unlet(
5178 lval_T *lvp,
5179 char_u *name_end,
5180 exarg_T *eap,
5181 int deep UNUSED,
5182 void *coookie)
5183{
5184 cctx_T *cctx = coookie;
5185
5186 if (lvp->ll_tv == NULL)
5187 {
5188 char_u *p = lvp->ll_name;
5189 int cc = *name_end;
5190 int ret = OK;
5191
5192 // Normal name. Only supports g:, w:, t: and b: namespaces.
5193 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005194 if (*p == '$')
5195 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5196 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005197 ret = FAIL;
5198 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005199 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005200
5201 *name_end = cc;
5202 return ret;
5203 }
5204
5205 // TODO: unlet {list}[idx]
5206 // TODO: unlet {dict}[key]
5207 emsg("Sorry, :unlet not fully implemented yet");
5208 return FAIL;
5209}
5210
5211/*
5212 * compile "unlet var", "lock var" and "unlock var"
5213 * "arg" points to "var".
5214 */
5215 static char_u *
5216compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5217{
5218 char_u *p = arg;
5219
5220 if (eap->cmdidx != CMD_unlet)
5221 {
5222 emsg("Sorry, :lock and unlock not implemented yet");
5223 return NULL;
5224 }
5225
5226 if (*p == '!')
5227 {
5228 p = skipwhite(p + 1);
5229 eap->forceit = TRUE;
5230 }
5231
5232 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5233 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5234}
5235
5236/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237 * Compile an :import command.
5238 */
5239 static char_u *
5240compile_import(char_u *arg, cctx_T *cctx)
5241{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01005242 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243}
5244
5245/*
5246 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5247 */
5248 static int
5249compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5250{
5251 garray_T *instr = &cctx->ctx_instr;
5252 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5253
5254 if (endlabel == NULL)
5255 return FAIL;
5256 endlabel->el_next = *el;
5257 *el = endlabel;
5258 endlabel->el_end_label = instr->ga_len;
5259
5260 generate_JUMP(cctx, when, 0);
5261 return OK;
5262}
5263
5264 static void
5265compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5266{
5267 garray_T *instr = &cctx->ctx_instr;
5268
5269 while (*el != NULL)
5270 {
5271 endlabel_T *cur = (*el);
5272 isn_T *isn;
5273
5274 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5275 isn->isn_arg.jump.jump_where = instr->ga_len;
5276 *el = cur->el_next;
5277 vim_free(cur);
5278 }
5279}
5280
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005281 static void
5282compile_free_jump_to_end(endlabel_T **el)
5283{
5284 while (*el != NULL)
5285 {
5286 endlabel_T *cur = (*el);
5287
5288 *el = cur->el_next;
5289 vim_free(cur);
5290 }
5291}
5292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005293/*
5294 * Create a new scope and set up the generic items.
5295 */
5296 static scope_T *
5297new_scope(cctx_T *cctx, scopetype_T type)
5298{
5299 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5300
5301 if (scope == NULL)
5302 return NULL;
5303 scope->se_outer = cctx->ctx_scope;
5304 cctx->ctx_scope = scope;
5305 scope->se_type = type;
5306 scope->se_local_count = cctx->ctx_locals.ga_len;
5307 return scope;
5308}
5309
5310/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005311 * Free the current scope and go back to the outer scope.
5312 */
5313 static void
5314drop_scope(cctx_T *cctx)
5315{
5316 scope_T *scope = cctx->ctx_scope;
5317
5318 if (scope == NULL)
5319 {
5320 iemsg("calling drop_scope() without a scope");
5321 return;
5322 }
5323 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005324 switch (scope->se_type)
5325 {
5326 case IF_SCOPE:
5327 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5328 case FOR_SCOPE:
5329 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5330 case WHILE_SCOPE:
5331 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5332 case TRY_SCOPE:
5333 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5334 case NO_SCOPE:
5335 case BLOCK_SCOPE:
5336 break;
5337 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005338 vim_free(scope);
5339}
5340
5341/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342 * compile "if expr"
5343 *
5344 * "if expr" Produces instructions:
5345 * EVAL expr Push result of "expr"
5346 * JUMP_IF_FALSE end
5347 * ... body ...
5348 * end:
5349 *
5350 * "if expr | else" Produces instructions:
5351 * EVAL expr Push result of "expr"
5352 * JUMP_IF_FALSE else
5353 * ... body ...
5354 * JUMP_ALWAYS end
5355 * else:
5356 * ... body ...
5357 * end:
5358 *
5359 * "if expr1 | elseif expr2 | else" Produces instructions:
5360 * EVAL expr Push result of "expr"
5361 * JUMP_IF_FALSE elseif
5362 * ... body ...
5363 * JUMP_ALWAYS end
5364 * elseif:
5365 * EVAL expr Push result of "expr"
5366 * JUMP_IF_FALSE else
5367 * ... body ...
5368 * JUMP_ALWAYS end
5369 * else:
5370 * ... body ...
5371 * end:
5372 */
5373 static char_u *
5374compile_if(char_u *arg, cctx_T *cctx)
5375{
5376 char_u *p = arg;
5377 garray_T *instr = &cctx->ctx_instr;
5378 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005379 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005380
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005381 // compile "expr"; if we know it evaluates to FALSE skip the block
5382 tv.v_type = VAR_UNKNOWN;
5383 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5384 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5385 else
5386 cctx->ctx_skip = MAYBE;
5387 clear_tv(&tv);
5388 if (cctx->ctx_skip == MAYBE)
5389 {
5390 p = arg;
5391 if (compile_expr1(&p, cctx) == FAIL)
5392 return NULL;
5393 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005394
5395 scope = new_scope(cctx, IF_SCOPE);
5396 if (scope == NULL)
5397 return NULL;
5398
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005399 if (cctx->ctx_skip == MAYBE)
5400 {
5401 // "where" is set when ":elseif", "else" or ":endif" is found
5402 scope->se_u.se_if.is_if_label = instr->ga_len;
5403 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5404 }
5405 else
5406 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005407
5408 return p;
5409}
5410
5411 static char_u *
5412compile_elseif(char_u *arg, cctx_T *cctx)
5413{
5414 char_u *p = arg;
5415 garray_T *instr = &cctx->ctx_instr;
5416 isn_T *isn;
5417 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005418 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005419
5420 if (scope == NULL || scope->se_type != IF_SCOPE)
5421 {
5422 emsg(_(e_elseif_without_if));
5423 return NULL;
5424 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005425 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005426
Bram Moolenaar158906c2020-02-06 20:39:45 +01005427 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005428 {
5429 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005430 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005431 return NULL;
5432 // previous "if" or "elseif" jumps here
5433 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5434 isn->isn_arg.jump.jump_where = instr->ga_len;
5435 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005436
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005437 // compile "expr"; if we know it evaluates to FALSE skip the block
5438 tv.v_type = VAR_UNKNOWN;
5439 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5440 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5441 else
5442 cctx->ctx_skip = MAYBE;
5443 clear_tv(&tv);
5444 if (cctx->ctx_skip == MAYBE)
5445 {
5446 p = arg;
5447 if (compile_expr1(&p, cctx) == FAIL)
5448 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005450 // "where" is set when ":elseif", "else" or ":endif" is found
5451 scope->se_u.se_if.is_if_label = instr->ga_len;
5452 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5453 }
5454 else
5455 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005456
5457 return p;
5458}
5459
5460 static char_u *
5461compile_else(char_u *arg, cctx_T *cctx)
5462{
5463 char_u *p = arg;
5464 garray_T *instr = &cctx->ctx_instr;
5465 isn_T *isn;
5466 scope_T *scope = cctx->ctx_scope;
5467
5468 if (scope == NULL || scope->se_type != IF_SCOPE)
5469 {
5470 emsg(_(e_else_without_if));
5471 return NULL;
5472 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005473 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005474
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005475 // jump from previous block to the end, unless the else block is empty
5476 if (cctx->ctx_skip == MAYBE)
5477 {
5478 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005479 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005480 return NULL;
5481 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005482
Bram Moolenaar158906c2020-02-06 20:39:45 +01005483 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005484 {
5485 if (scope->se_u.se_if.is_if_label >= 0)
5486 {
5487 // previous "if" or "elseif" jumps here
5488 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5489 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005490 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005491 }
5492 }
5493
5494 if (cctx->ctx_skip != MAYBE)
5495 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005496
5497 return p;
5498}
5499
5500 static char_u *
5501compile_endif(char_u *arg, cctx_T *cctx)
5502{
5503 scope_T *scope = cctx->ctx_scope;
5504 ifscope_T *ifscope;
5505 garray_T *instr = &cctx->ctx_instr;
5506 isn_T *isn;
5507
5508 if (scope == NULL || scope->se_type != IF_SCOPE)
5509 {
5510 emsg(_(e_endif_without_if));
5511 return NULL;
5512 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005513 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005514 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005515
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005516 if (scope->se_u.se_if.is_if_label >= 0)
5517 {
5518 // previous "if" or "elseif" jumps here
5519 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5520 isn->isn_arg.jump.jump_where = instr->ga_len;
5521 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005522 // Fill in the "end" label in jumps at the end of the blocks.
5523 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005524 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005525
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005526 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005527 return arg;
5528}
5529
5530/*
5531 * compile "for var in expr"
5532 *
5533 * Produces instructions:
5534 * PUSHNR -1
5535 * STORE loop-idx Set index to -1
5536 * EVAL expr Push result of "expr"
5537 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5538 * - if beyond end, jump to "end"
5539 * - otherwise get item from list and push it
5540 * STORE var Store item in "var"
5541 * ... body ...
5542 * JUMP top Jump back to repeat
5543 * end: DROP Drop the result of "expr"
5544 *
5545 */
5546 static char_u *
5547compile_for(char_u *arg, cctx_T *cctx)
5548{
5549 char_u *p;
5550 size_t varlen;
5551 garray_T *instr = &cctx->ctx_instr;
5552 garray_T *stack = &cctx->ctx_type_stack;
5553 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005554 lvar_T *loop_lvar; // loop iteration variable
5555 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005556 type_T *vartype;
5557
5558 // TODO: list of variables: "for [key, value] in dict"
5559 // parse "var"
5560 for (p = arg; eval_isnamec1(*p); ++p)
5561 ;
5562 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005563 var_lvar = lookup_local(arg, varlen, cctx);
5564 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005565 {
5566 semsg(_("E1023: variable already defined: %s"), arg);
5567 return NULL;
5568 }
5569
5570 // consume "in"
5571 p = skipwhite(p);
5572 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5573 {
5574 emsg(_(e_missing_in));
5575 return NULL;
5576 }
5577 p = skipwhite(p + 2);
5578
5579
5580 scope = new_scope(cctx, FOR_SCOPE);
5581 if (scope == NULL)
5582 return NULL;
5583
5584 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005585 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5586 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005587 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005588 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005589 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005591 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005592
5593 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005594 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5595 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005596 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005597 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005598 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005599 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005600 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005601
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005602 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005603
5604 // compile "expr", it remains on the stack until "endfor"
5605 arg = p;
5606 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005607 {
5608 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005609 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005610 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005611
5612 // now we know the type of "var"
5613 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5614 if (vartype->tt_type != VAR_LIST)
5615 {
5616 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005617 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005618 return NULL;
5619 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005620 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005621 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005622
5623 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005624 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005625
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005626 generate_FOR(cctx, loop_lvar->lv_idx);
5627 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005628
5629 return arg;
5630}
5631
5632/*
5633 * compile "endfor"
5634 */
5635 static char_u *
5636compile_endfor(char_u *arg, cctx_T *cctx)
5637{
5638 garray_T *instr = &cctx->ctx_instr;
5639 scope_T *scope = cctx->ctx_scope;
5640 forscope_T *forscope;
5641 isn_T *isn;
5642
5643 if (scope == NULL || scope->se_type != FOR_SCOPE)
5644 {
5645 emsg(_(e_for));
5646 return NULL;
5647 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005648 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005649 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005650 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005651
5652 // At end of ":for" scope jump back to the FOR instruction.
5653 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5654
5655 // Fill in the "end" label in the FOR statement so it can jump here
5656 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5657 isn->isn_arg.forloop.for_end = instr->ga_len;
5658
5659 // Fill in the "end" label any BREAK statements
5660 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5661
5662 // Below the ":for" scope drop the "expr" list from the stack.
5663 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5664 return NULL;
5665
5666 vim_free(scope);
5667
5668 return arg;
5669}
5670
5671/*
5672 * compile "while expr"
5673 *
5674 * Produces instructions:
5675 * top: EVAL expr Push result of "expr"
5676 * JUMP_IF_FALSE end jump if false
5677 * ... body ...
5678 * JUMP top Jump back to repeat
5679 * end:
5680 *
5681 */
5682 static char_u *
5683compile_while(char_u *arg, cctx_T *cctx)
5684{
5685 char_u *p = arg;
5686 garray_T *instr = &cctx->ctx_instr;
5687 scope_T *scope;
5688
5689 scope = new_scope(cctx, WHILE_SCOPE);
5690 if (scope == NULL)
5691 return NULL;
5692
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005693 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005694
5695 // compile "expr"
5696 if (compile_expr1(&p, cctx) == FAIL)
5697 return NULL;
5698
5699 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005700 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005701 JUMP_IF_FALSE, cctx) == FAIL)
5702 return FAIL;
5703
5704 return p;
5705}
5706
5707/*
5708 * compile "endwhile"
5709 */
5710 static char_u *
5711compile_endwhile(char_u *arg, cctx_T *cctx)
5712{
5713 scope_T *scope = cctx->ctx_scope;
5714
5715 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5716 {
5717 emsg(_(e_while));
5718 return NULL;
5719 }
5720 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005721 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005722
5723 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005724 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005725
5726 // Fill in the "end" label in the WHILE statement so it can jump here.
5727 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005728 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005729
5730 vim_free(scope);
5731
5732 return arg;
5733}
5734
5735/*
5736 * compile "continue"
5737 */
5738 static char_u *
5739compile_continue(char_u *arg, cctx_T *cctx)
5740{
5741 scope_T *scope = cctx->ctx_scope;
5742
5743 for (;;)
5744 {
5745 if (scope == NULL)
5746 {
5747 emsg(_(e_continue));
5748 return NULL;
5749 }
5750 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5751 break;
5752 scope = scope->se_outer;
5753 }
5754
5755 // Jump back to the FOR or WHILE instruction.
5756 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005757 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5758 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005759 return arg;
5760}
5761
5762/*
5763 * compile "break"
5764 */
5765 static char_u *
5766compile_break(char_u *arg, cctx_T *cctx)
5767{
5768 scope_T *scope = cctx->ctx_scope;
5769 endlabel_T **el;
5770
5771 for (;;)
5772 {
5773 if (scope == NULL)
5774 {
5775 emsg(_(e_break));
5776 return NULL;
5777 }
5778 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5779 break;
5780 scope = scope->se_outer;
5781 }
5782
5783 // Jump to the end of the FOR or WHILE loop.
5784 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005785 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005786 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005787 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005788 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5789 return FAIL;
5790
5791 return arg;
5792}
5793
5794/*
5795 * compile "{" start of block
5796 */
5797 static char_u *
5798compile_block(char_u *arg, cctx_T *cctx)
5799{
5800 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5801 return NULL;
5802 return skipwhite(arg + 1);
5803}
5804
5805/*
5806 * compile end of block: drop one scope
5807 */
5808 static void
5809compile_endblock(cctx_T *cctx)
5810{
5811 scope_T *scope = cctx->ctx_scope;
5812
5813 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005814 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005815 vim_free(scope);
5816}
5817
5818/*
5819 * compile "try"
5820 * Creates a new scope for the try-endtry, pointing to the first catch and
5821 * finally.
5822 * Creates another scope for the "try" block itself.
5823 * TRY instruction sets up exception handling at runtime.
5824 *
5825 * "try"
5826 * TRY -> catch1, -> finally push trystack entry
5827 * ... try block
5828 * "throw {exception}"
5829 * EVAL {exception}
5830 * THROW create exception
5831 * ... try block
5832 * " catch {expr}"
5833 * JUMP -> finally
5834 * catch1: PUSH exeception
5835 * EVAL {expr}
5836 * MATCH
5837 * JUMP nomatch -> catch2
5838 * CATCH remove exception
5839 * ... catch block
5840 * " catch"
5841 * JUMP -> finally
5842 * catch2: CATCH remove exception
5843 * ... catch block
5844 * " finally"
5845 * finally:
5846 * ... finally block
5847 * " endtry"
5848 * ENDTRY pop trystack entry, may rethrow
5849 */
5850 static char_u *
5851compile_try(char_u *arg, cctx_T *cctx)
5852{
5853 garray_T *instr = &cctx->ctx_instr;
5854 scope_T *try_scope;
5855 scope_T *scope;
5856
5857 // scope that holds the jumps that go to catch/finally/endtry
5858 try_scope = new_scope(cctx, TRY_SCOPE);
5859 if (try_scope == NULL)
5860 return NULL;
5861
5862 // "catch" is set when the first ":catch" is found.
5863 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005864 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005865 if (generate_instr(cctx, ISN_TRY) == NULL)
5866 return NULL;
5867
5868 // scope for the try block itself
5869 scope = new_scope(cctx, BLOCK_SCOPE);
5870 if (scope == NULL)
5871 return NULL;
5872
5873 return arg;
5874}
5875
5876/*
5877 * compile "catch {expr}"
5878 */
5879 static char_u *
5880compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5881{
5882 scope_T *scope = cctx->ctx_scope;
5883 garray_T *instr = &cctx->ctx_instr;
5884 char_u *p;
5885 isn_T *isn;
5886
5887 // end block scope from :try or :catch
5888 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5889 compile_endblock(cctx);
5890 scope = cctx->ctx_scope;
5891
5892 // Error if not in a :try scope
5893 if (scope == NULL || scope->se_type != TRY_SCOPE)
5894 {
5895 emsg(_(e_catch));
5896 return NULL;
5897 }
5898
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005899 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005900 {
5901 emsg(_("E1033: catch unreachable after catch-all"));
5902 return NULL;
5903 }
5904
5905 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005906 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005907 JUMP_ALWAYS, cctx) == FAIL)
5908 return NULL;
5909
5910 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005911 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005912 if (isn->isn_arg.try.try_catch == 0)
5913 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005914 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005915 {
5916 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005917 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005918 isn->isn_arg.jump.jump_where = instr->ga_len;
5919 }
5920
5921 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02005922 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005923 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005924 scope->se_u.se_try.ts_caught_all = TRUE;
5925 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005926 }
5927 else
5928 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005929 char_u *end;
5930 char_u *pat;
5931 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005932 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005933 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005934
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005935 // Push v:exception, push {expr} and MATCH
5936 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
5937
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005938 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005939 if (*end != *p)
5940 {
5941 semsg(_("E1067: Separator mismatch: %s"), p);
5942 vim_free(tofree);
5943 return FAIL;
5944 }
5945 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01005946 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005947 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005948 len = (int)(end - tofree);
5949 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005950 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02005951 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01005952 if (pat == NULL)
5953 return FAIL;
5954 if (generate_PUSHS(cctx, pat) == FAIL)
5955 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005957 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
5958 return NULL;
5959
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005960 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005961 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
5962 return NULL;
5963 }
5964
5965 if (generate_instr(cctx, ISN_CATCH) == NULL)
5966 return NULL;
5967
5968 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5969 return NULL;
5970 return p;
5971}
5972
5973 static char_u *
5974compile_finally(char_u *arg, cctx_T *cctx)
5975{
5976 scope_T *scope = cctx->ctx_scope;
5977 garray_T *instr = &cctx->ctx_instr;
5978 isn_T *isn;
5979
5980 // end block scope from :try or :catch
5981 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5982 compile_endblock(cctx);
5983 scope = cctx->ctx_scope;
5984
5985 // Error if not in a :try scope
5986 if (scope == NULL || scope->se_type != TRY_SCOPE)
5987 {
5988 emsg(_(e_finally));
5989 return NULL;
5990 }
5991
5992 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005993 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005994 if (isn->isn_arg.try.try_finally != 0)
5995 {
5996 emsg(_(e_finally_dup));
5997 return NULL;
5998 }
5999
6000 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006001 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006002
Bram Moolenaar585fea72020-04-02 22:33:21 +02006003 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006004 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006005 {
6006 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006007 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006008 isn->isn_arg.jump.jump_where = instr->ga_len;
6009 }
6010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006011 // TODO: set index in ts_finally_label jumps
6012
6013 return arg;
6014}
6015
6016 static char_u *
6017compile_endtry(char_u *arg, cctx_T *cctx)
6018{
6019 scope_T *scope = cctx->ctx_scope;
6020 garray_T *instr = &cctx->ctx_instr;
6021 isn_T *isn;
6022
6023 // end block scope from :catch or :finally
6024 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6025 compile_endblock(cctx);
6026 scope = cctx->ctx_scope;
6027
6028 // Error if not in a :try scope
6029 if (scope == NULL || scope->se_type != TRY_SCOPE)
6030 {
6031 if (scope == NULL)
6032 emsg(_(e_no_endtry));
6033 else if (scope->se_type == WHILE_SCOPE)
6034 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006035 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006036 emsg(_(e_endfor));
6037 else
6038 emsg(_(e_endif));
6039 return NULL;
6040 }
6041
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006042 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006043 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6044 {
6045 emsg(_("E1032: missing :catch or :finally"));
6046 return NULL;
6047 }
6048
6049 // Fill in the "end" label in jumps at the end of the blocks, if not done
6050 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006051 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006052
6053 // End :catch or :finally scope: set value in ISN_TRY instruction
6054 if (isn->isn_arg.try.try_finally == 0)
6055 isn->isn_arg.try.try_finally = instr->ga_len;
6056 compile_endblock(cctx);
6057
6058 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6059 return NULL;
6060 return arg;
6061}
6062
6063/*
6064 * compile "throw {expr}"
6065 */
6066 static char_u *
6067compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6068{
6069 char_u *p = skipwhite(arg);
6070
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006071 if (compile_expr1(&p, cctx) == FAIL)
6072 return NULL;
6073 if (may_generate_2STRING(-1, cctx) == FAIL)
6074 return NULL;
6075 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6076 return NULL;
6077
6078 return p;
6079}
6080
6081/*
6082 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006083 * compile "echomsg expr"
6084 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006085 * compile "execute expr"
6086 */
6087 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006088compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006089{
6090 char_u *p = arg;
6091 int count = 0;
6092
6093 for (;;)
6094 {
6095 if (compile_expr1(&p, cctx) == FAIL)
6096 return NULL;
6097 ++count;
6098 p = skipwhite(p);
6099 if (ends_excmd(*p))
6100 break;
6101 }
6102
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006103 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6104 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6105 else if (cmdidx == CMD_execute)
6106 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6107 else if (cmdidx == CMD_echomsg)
6108 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6109 else
6110 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006111 return p;
6112}
6113
6114/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006115 * A command that is not compiled, execute with legacy code.
6116 */
6117 static char_u *
6118compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6119{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006120 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006121 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006122
6123 if (cctx->ctx_skip == TRUE)
6124 goto theend;
6125
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006126 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
6127 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006128 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6129 {
6130 // expand filename in "syntax include [@group] filename"
6131 has_expr = TRUE;
6132 eap->arg = skipwhite(eap->arg + 7);
6133 if (*eap->arg == '@')
6134 eap->arg = skiptowhite(eap->arg);
6135 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006136
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006137 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006138 {
6139 int count = 0;
6140 char_u *start = skipwhite(line);
6141
6142 // :cmd xxx`=expr1`yyy`=expr2`zzz
6143 // PUSHS ":cmd xxx"
6144 // eval expr1
6145 // PUSHS "yyy"
6146 // eval expr2
6147 // PUSHS "zzz"
6148 // EXECCONCAT 5
6149 for (;;)
6150 {
6151 if (p > start)
6152 {
6153 generate_PUSHS(cctx, vim_strnsave(start, (int)(p - start)));
6154 ++count;
6155 }
6156 p += 2;
6157 if (compile_expr1(&p, cctx) == FAIL)
6158 return NULL;
6159 may_generate_2STRING(-1, cctx);
6160 ++count;
6161 p = skipwhite(p);
6162 if (*p != '`')
6163 {
6164 emsg(_("E1083: missing backtick"));
6165 return NULL;
6166 }
6167 start = p + 1;
6168
6169 p = (char_u *)strstr((char *)start, "`=");
6170 if (p == NULL)
6171 {
6172 if (*skipwhite(start) != NUL)
6173 {
6174 generate_PUSHS(cctx, vim_strsave(start));
6175 ++count;
6176 }
6177 break;
6178 }
6179 }
6180 generate_EXECCONCAT(cctx, count);
6181 }
6182 else
6183 generate_EXEC(cctx, line);
6184
6185theend:
6186 return (char_u *)"";
6187}
6188
6189/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006190 * After ex_function() has collected all the function lines: parse and compile
6191 * the lines into instructions.
6192 * Adds the function to "def_functions".
6193 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6194 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006195 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006196 * This can be used recursively through compile_lambda(), which may reallocate
6197 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006198 */
6199 void
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006200compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006201{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006202 char_u *line = NULL;
6203 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006204 char *errormsg = NULL; // error message
6205 int had_return = FALSE;
6206 cctx_T cctx;
6207 garray_T *instr;
6208 int called_emsg_before = called_emsg;
6209 int ret = FAIL;
6210 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006211 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006212
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006213 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006214 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01006215
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006216 if (ufunc->uf_dfunc_idx >= 0)
6217 {
6218 // Redefining a function that was compiled before.
6219 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
6220
6221 // Free old instructions.
6222 delete_def_function_contents(dfunc);
6223 }
6224 else
6225 {
6226 // Add the function to "def_functions".
6227 if (ga_grow(&def_functions, 1) == FAIL)
6228 return;
6229 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006230 CLEAR_POINTER(dfunc);
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006231 dfunc->df_idx = def_functions.ga_len;
6232 ufunc->uf_dfunc_idx = dfunc->df_idx;
6233 dfunc->df_ufunc = ufunc;
6234 ++def_functions.ga_len;
6235 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006236 }
6237
Bram Moolenaara80faa82020-04-12 19:37:17 +02006238 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006239 cctx.ctx_ufunc = ufunc;
6240 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006241 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6243 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6244 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6245 cctx.ctx_type_list = &ufunc->uf_type_list;
6246 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6247 instr = &cctx.ctx_instr;
6248
6249 // Most modern script version.
6250 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6251
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006252 if (ufunc->uf_def_args.ga_len > 0)
6253 {
6254 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006255 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006256 int i;
6257 char_u *arg;
6258 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6259
6260 // Produce instructions for the default values of optional arguments.
6261 // Store the instruction index in uf_def_arg_idx[] so that we know
6262 // where to start when the function is called, depending on the number
6263 // of arguments.
6264 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6265 if (ufunc->uf_def_arg_idx == NULL)
6266 goto erret;
6267 for (i = 0; i < count; ++i)
6268 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006269 garray_T *stack = &cctx.ctx_type_stack;
6270 type_T *val_type;
6271 int arg_idx = first_def_arg + i;
6272
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006273 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6274 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006275 if (compile_expr1(&arg, &cctx) == FAIL)
6276 goto erret;
6277
6278 // If no type specified use the type of the default value.
6279 // Otherwise check that the default value type matches the
6280 // specified type.
6281 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6282 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6283 ufunc->uf_arg_types[arg_idx] = val_type;
6284 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6285 == FAIL)
6286 {
6287 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6288 arg_idx + 1);
6289 goto erret;
6290 }
6291
6292 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006293 goto erret;
6294 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006295 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6296 }
6297
6298 /*
6299 * Loop over all the lines of the function and generate instructions.
6300 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301 for (;;)
6302 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006303 exarg_T ea;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006304 int is_ex_command = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006305
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006306 // Bail out on the first error to avoid a flood of errors and report
6307 // the right line number when inside try/catch.
6308 if (emsg_before != called_emsg)
6309 goto erret;
6310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006311 if (line != NULL && *line == '|')
6312 // the line continues after a '|'
6313 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006314 else if (line != NULL && *line != NUL
6315 && !(*line == '#' && (line == cctx.ctx_line_start
6316 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006317 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006318 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319 goto erret;
6320 }
6321 else
6322 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006323 line = next_line_from_context(&cctx);
6324 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006325 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006326 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006327 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006328 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329
6330 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006331 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006332 ea.cmdlinep = &line;
6333 ea.cmd = skipwhite(line);
6334
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006335 // Some things can be recognized by the first character.
6336 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006337 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006338 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006339 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006340 if (ea.cmd[1] != '{')
6341 {
6342 line = (char_u *)"";
6343 continue;
6344 }
6345 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006346
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006347 case '}':
6348 {
6349 // "}" ends a block scope
6350 scopetype_T stype = cctx.ctx_scope == NULL
6351 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006352
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006353 if (stype == BLOCK_SCOPE)
6354 {
6355 compile_endblock(&cctx);
6356 line = ea.cmd;
6357 }
6358 else
6359 {
6360 emsg(_("E1025: using } outside of a block scope"));
6361 goto erret;
6362 }
6363 if (line != NULL)
6364 line = skipwhite(ea.cmd + 1);
6365 continue;
6366 }
6367
6368 case '{':
6369 // "{" starts a block scope
6370 // "{'a': 1}->func() is something else
6371 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6372 {
6373 line = compile_block(ea.cmd, &cctx);
6374 continue;
6375 }
6376 break;
6377
6378 case ':':
6379 is_ex_command = TRUE;
6380 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006381 }
6382
6383 /*
6384 * COMMAND MODIFIERS
6385 */
6386 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6387 {
6388 if (errormsg != NULL)
6389 goto erret;
6390 // empty line or comment
6391 line = (char_u *)"";
6392 continue;
6393 }
6394
6395 // Skip ":call" to get to the function name.
6396 if (checkforcmd(&ea.cmd, "call", 3))
6397 ea.cmd = skipwhite(ea.cmd);
6398
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006399 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006401 // Assuming the command starts with a variable or function name,
6402 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
6403 // val".
6404 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
6405 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006406 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006407 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006409 int oplen;
6410 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006411
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006412 oplen = assignment_len(skipwhite(p), &heredoc);
6413 if (oplen > 0)
6414 {
6415 // Recognize an assignment if we recognize the variable
6416 // name:
6417 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006418 // "local = expr" where "local" is a local var.
6419 // "script = expr" where "script" is a script-local var.
6420 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006421 // "&opt = expr"
6422 // "$ENV = expr"
6423 // "@r = expr"
6424 if (*ea.cmd == '&'
6425 || *ea.cmd == '$'
6426 || *ea.cmd == '@'
6427 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006428 || lookup_local(ea.cmd, p - ea.cmd, &cctx) != NULL
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006429 || lookup_script(ea.cmd, p - ea.cmd) == OK
6430 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
6431 {
6432 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6433 if (line == NULL)
6434 goto erret;
6435 continue;
6436 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006437 }
6438 }
6439 }
6440
6441 /*
6442 * COMMAND after range
6443 */
6444 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006445 p = find_ex_command(&ea, NULL, is_ex_command ? NULL
6446 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006447 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006448
6449 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6450 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006451 if (cctx.ctx_skip == TRUE)
6452 {
6453 line += STRLEN(line);
6454 continue;
6455 }
6456
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006457 // Expression or function call.
6458 if (ea.cmdidx == CMD_eval)
6459 {
6460 p = ea.cmd;
6461 if (compile_expr1(&p, &cctx) == FAIL)
6462 goto erret;
6463
6464 // drop the return value
6465 generate_instr_drop(&cctx, ISN_DROP, 1);
6466 line = p;
6467 continue;
6468 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006469 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006470 iemsg("Command from find_ex_command() not handled");
6471 goto erret;
6472 }
6473
6474 p = skipwhite(p);
6475
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006476 if (cctx.ctx_skip == TRUE
6477 && ea.cmdidx != CMD_elseif
6478 && ea.cmdidx != CMD_else
6479 && ea.cmdidx != CMD_endif)
6480 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006481 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006482 continue;
6483 }
6484
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006485 switch (ea.cmdidx)
6486 {
6487 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006488 ea.arg = p;
6489 line = compile_nested_function(&ea, &cctx);
6490 break;
6491
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006492 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006493 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006494 goto erret;
6495
6496 case CMD_return:
6497 line = compile_return(p, set_return_type, &cctx);
6498 had_return = TRUE;
6499 break;
6500
6501 case CMD_let:
6502 case CMD_const:
6503 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
6504 break;
6505
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006506 case CMD_unlet:
6507 case CMD_unlockvar:
6508 case CMD_lockvar:
6509 line = compile_unletlock(p, &ea, &cctx);
6510 break;
6511
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006512 case CMD_import:
6513 line = compile_import(p, &cctx);
6514 break;
6515
6516 case CMD_if:
6517 line = compile_if(p, &cctx);
6518 break;
6519 case CMD_elseif:
6520 line = compile_elseif(p, &cctx);
6521 break;
6522 case CMD_else:
6523 line = compile_else(p, &cctx);
6524 break;
6525 case CMD_endif:
6526 line = compile_endif(p, &cctx);
6527 break;
6528
6529 case CMD_while:
6530 line = compile_while(p, &cctx);
6531 break;
6532 case CMD_endwhile:
6533 line = compile_endwhile(p, &cctx);
6534 break;
6535
6536 case CMD_for:
6537 line = compile_for(p, &cctx);
6538 break;
6539 case CMD_endfor:
6540 line = compile_endfor(p, &cctx);
6541 break;
6542 case CMD_continue:
6543 line = compile_continue(p, &cctx);
6544 break;
6545 case CMD_break:
6546 line = compile_break(p, &cctx);
6547 break;
6548
6549 case CMD_try:
6550 line = compile_try(p, &cctx);
6551 break;
6552 case CMD_catch:
6553 line = compile_catch(p, &cctx);
6554 break;
6555 case CMD_finally:
6556 line = compile_finally(p, &cctx);
6557 break;
6558 case CMD_endtry:
6559 line = compile_endtry(p, &cctx);
6560 break;
6561 case CMD_throw:
6562 line = compile_throw(p, &cctx);
6563 break;
6564
6565 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006567 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006568 case CMD_echomsg:
6569 case CMD_echoerr:
6570 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006571 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006572
6573 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006574 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006575 // Not recognized, execute with do_cmdline_cmd().
6576 ea.arg = p;
6577 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006578 break;
6579 }
6580 if (line == NULL)
6581 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006582 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006583
6584 if (cctx.ctx_type_stack.ga_len < 0)
6585 {
6586 iemsg("Type stack underflow");
6587 goto erret;
6588 }
6589 }
6590
6591 if (cctx.ctx_scope != NULL)
6592 {
6593 if (cctx.ctx_scope->se_type == IF_SCOPE)
6594 emsg(_(e_endif));
6595 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6596 emsg(_(e_endwhile));
6597 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6598 emsg(_(e_endfor));
6599 else
6600 emsg(_("E1026: Missing }"));
6601 goto erret;
6602 }
6603
6604 if (!had_return)
6605 {
6606 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6607 {
6608 emsg(_("E1027: Missing return statement"));
6609 goto erret;
6610 }
6611
6612 // Return zero if there is no return at the end.
6613 generate_PUSHNR(&cctx, 0);
6614 generate_instr(&cctx, ISN_RETURN);
6615 }
6616
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006617 {
6618 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6619 + ufunc->uf_dfunc_idx;
6620 dfunc->df_deleted = FALSE;
6621 dfunc->df_instr = instr->ga_data;
6622 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006623 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006624 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006625 if (cctx.ctx_outer_used)
6626 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006629 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006630 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006631 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006632
6633 // Create a type for the function, with the return type and any
6634 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006635 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6636 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006637 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006638 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006639 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6640 argcount, &ufunc->uf_type_list);
6641 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006642 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006643 argcount + varargs,
6644 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006645 {
6646 ret = FAIL;
6647 goto erret;
6648 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006649 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6650 ufunc->uf_func_type->tt_min_argcount =
6651 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006652 if (ufunc->uf_arg_types == NULL)
6653 {
6654 int i;
6655
6656 // lambda does not have argument types.
6657 for (i = 0; i < argcount; ++i)
6658 ufunc->uf_func_type->tt_args[i] = &t_any;
6659 }
6660 else
6661 mch_memmove(ufunc->uf_func_type->tt_args,
6662 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006663 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006664 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006665 ufunc->uf_func_type->tt_args[argcount] =
6666 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006667 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6668 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006669 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006670 else
6671 // No arguments, can use a predefined type.
6672 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6673 argcount, &ufunc->uf_type_list);
6674
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006675 }
6676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006677 ret = OK;
6678
6679erret:
6680 if (ret == FAIL)
6681 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006682 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006683 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6684 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006685
6686 for (idx = 0; idx < instr->ga_len; ++idx)
6687 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006690 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006691 if (!dfunc->df_deleted)
6692 --def_functions.ga_len;
6693
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006694 while (cctx.ctx_scope != NULL)
6695 drop_scope(&cctx);
6696
Bram Moolenaar20431c92020-03-20 18:39:46 +01006697 // Don't execute this function body.
6698 ga_clear_strings(&ufunc->uf_lines);
6699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700 if (errormsg != NULL)
6701 emsg(errormsg);
6702 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006703 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006704 }
6705
6706 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006707 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006708 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006709 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006710}
6711
6712/*
6713 * Delete an instruction, free what it contains.
6714 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006715 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006716delete_instr(isn_T *isn)
6717{
6718 switch (isn->isn_type)
6719 {
6720 case ISN_EXEC:
6721 case ISN_LOADENV:
6722 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006723 case ISN_LOADB:
6724 case ISN_LOADW:
6725 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006726 case ISN_LOADOPT:
6727 case ISN_MEMBER:
6728 case ISN_PUSHEXC:
6729 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006730 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006731 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006732 case ISN_STOREB:
6733 case ISN_STOREW:
6734 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006735 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006736 vim_free(isn->isn_arg.string);
6737 break;
6738
6739 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006740 case ISN_STORES:
6741 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006742 break;
6743
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006744 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006745 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006746 vim_free(isn->isn_arg.unlet.ul_name);
6747 break;
6748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006749 case ISN_STOREOPT:
6750 vim_free(isn->isn_arg.storeopt.so_name);
6751 break;
6752
6753 case ISN_PUSHBLOB: // push blob isn_arg.blob
6754 blob_unref(isn->isn_arg.blob);
6755 break;
6756
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006757 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006758#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006759 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006760#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006761 break;
6762
6763 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006764#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006765 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006766#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006767 break;
6768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006769 case ISN_UCALL:
6770 vim_free(isn->isn_arg.ufunc.cuf_name);
6771 break;
6772
Bram Moolenaar221fcc72020-05-05 19:46:20 +02006773 case ISN_FUNCREF:
6774 {
6775 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6776 + isn->isn_arg.funcref.fr_func;
6777 func_ptr_unref(dfunc->df_ufunc);
6778 }
6779 break;
6780
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006781 case ISN_2BOOL:
6782 case ISN_2STRING:
6783 case ISN_ADDBLOB:
6784 case ISN_ADDLIST:
6785 case ISN_BCALL:
6786 case ISN_CATCH:
6787 case ISN_CHECKNR:
6788 case ISN_CHECKTYPE:
6789 case ISN_COMPAREANY:
6790 case ISN_COMPAREBLOB:
6791 case ISN_COMPAREBOOL:
6792 case ISN_COMPAREDICT:
6793 case ISN_COMPAREFLOAT:
6794 case ISN_COMPAREFUNC:
6795 case ISN_COMPARELIST:
6796 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006797 case ISN_COMPARESPECIAL:
6798 case ISN_COMPARESTRING:
6799 case ISN_CONCAT:
6800 case ISN_DCALL:
6801 case ISN_DROP:
6802 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006803 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006804 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006806 case ISN_EXECCONCAT:
6807 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006809 case ISN_INDEX:
6810 case ISN_JUMP:
6811 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006812 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006813 case ISN_LOADSCRIPT:
6814 case ISN_LOADREG:
6815 case ISN_LOADV:
6816 case ISN_NEGATENR:
6817 case ISN_NEWDICT:
6818 case ISN_NEWLIST:
6819 case ISN_OPNR:
6820 case ISN_OPFLOAT:
6821 case ISN_OPANY:
6822 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006823 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006824 case ISN_PUSHF:
6825 case ISN_PUSHNR:
6826 case ISN_PUSHBOOL:
6827 case ISN_PUSHSPEC:
6828 case ISN_RETURN:
6829 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02006830 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006831 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006832 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006833 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006834 case ISN_STORESCRIPT:
6835 case ISN_THROW:
6836 case ISN_TRY:
6837 // nothing allocated
6838 break;
6839 }
6840}
6841
6842/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006843 * Free all instructions for "dfunc".
6844 */
6845 static void
6846delete_def_function_contents(dfunc_T *dfunc)
6847{
6848 int idx;
6849
6850 ga_clear(&dfunc->df_def_args_isn);
6851
6852 if (dfunc->df_instr != NULL)
6853 {
6854 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6855 delete_instr(dfunc->df_instr + idx);
6856 VIM_CLEAR(dfunc->df_instr);
6857 }
6858
6859 dfunc->df_deleted = TRUE;
6860}
6861
6862/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006863 * When a user function is deleted, delete any associated def function.
6864 */
6865 void
6866delete_def_function(ufunc_T *ufunc)
6867{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868 if (ufunc->uf_dfunc_idx >= 0)
6869 {
6870 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6871 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006872
Bram Moolenaar20431c92020-03-20 18:39:46 +01006873 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006874 }
6875}
6876
6877#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006878/*
6879 * Free all functions defined with ":def".
6880 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881 void
6882free_def_functions(void)
6883{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006884 int idx;
6885
6886 for (idx = 0; idx < def_functions.ga_len; ++idx)
6887 {
6888 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6889
6890 delete_def_function_contents(dfunc);
6891 }
6892
6893 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006894}
6895#endif
6896
6897
6898#endif // FEAT_EVAL