blob: 67ca3de66710ee2e0c8251e8fad93e72cc557198 [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".
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02001045 * "tv" will be consumed or cleared. "tv" may be NULL;
Bram Moolenaar61a89812020-05-07 16:58:17 +02001046 */
1047 static int
1048generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1049{
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02001050 if (tv != NULL)
Bram Moolenaar61a89812020-05-07 16:58:17 +02001051 {
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02001052 switch (tv->v_type)
1053 {
1054 case VAR_UNKNOWN:
1055 break;
1056 case VAR_BOOL:
1057 generate_PUSHBOOL(cctx, tv->vval.v_number);
1058 break;
1059 case VAR_SPECIAL:
1060 generate_PUSHSPEC(cctx, tv->vval.v_number);
1061 break;
1062 case VAR_NUMBER:
1063 generate_PUSHNR(cctx, tv->vval.v_number);
1064 break;
Bram Moolenaar61a89812020-05-07 16:58:17 +02001065#ifdef FEAT_FLOAT
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02001066 case VAR_FLOAT:
1067 generate_PUSHF(cctx, tv->vval.v_float);
1068 break;
Bram Moolenaar61a89812020-05-07 16:58:17 +02001069#endif
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02001070 case VAR_BLOB:
1071 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1072 tv->vval.v_blob = NULL;
1073 break;
1074 case VAR_STRING:
1075 generate_PUSHS(cctx, tv->vval.v_string);
1076 tv->vval.v_string = NULL;
1077 break;
1078 default:
1079 iemsg("constant type not supported");
1080 clear_tv(tv);
1081 return FAIL;
1082 }
1083 tv->v_type = VAR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02001084 }
1085 return OK;
1086}
1087
1088/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001089 * Generate an ISN_STORE instruction.
1090 */
1091 static int
1092generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1093{
1094 isn_T *isn;
1095
Bram Moolenaar080457c2020-03-03 21:53:32 +01001096 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1098 return FAIL;
1099 if (name != NULL)
1100 isn->isn_arg.string = vim_strsave(name);
1101 else
1102 isn->isn_arg.number = idx;
1103
1104 return OK;
1105}
1106
1107/*
1108 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1109 */
1110 static int
1111generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1112{
1113 isn_T *isn;
1114
Bram Moolenaar080457c2020-03-03 21:53:32 +01001115 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1117 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001118 isn->isn_arg.storenr.stnr_idx = idx;
1119 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120
1121 return OK;
1122}
1123
1124/*
1125 * Generate an ISN_STOREOPT instruction
1126 */
1127 static int
1128generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1129{
1130 isn_T *isn;
1131
Bram Moolenaar080457c2020-03-03 21:53:32 +01001132 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1134 return FAIL;
1135 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1136 isn->isn_arg.storeopt.so_flags = opt_flags;
1137
1138 return OK;
1139}
1140
1141/*
1142 * Generate an ISN_LOAD or similar instruction.
1143 */
1144 static int
1145generate_LOAD(
1146 cctx_T *cctx,
1147 isntype_T isn_type,
1148 int idx,
1149 char_u *name,
1150 type_T *type)
1151{
1152 isn_T *isn;
1153
Bram Moolenaar080457c2020-03-03 21:53:32 +01001154 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1156 return FAIL;
1157 if (name != NULL)
1158 isn->isn_arg.string = vim_strsave(name);
1159 else
1160 isn->isn_arg.number = idx;
1161
1162 return OK;
1163}
1164
1165/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001166 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001167 */
1168 static int
1169generate_LOADV(
1170 cctx_T *cctx,
1171 char_u *name,
1172 int error)
1173{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001174 int di_flags;
1175 int vidx = find_vim_var(name, &di_flags);
1176 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001177
Bram Moolenaar080457c2020-03-03 21:53:32 +01001178 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001179 if (vidx < 0)
1180 {
1181 if (error)
1182 semsg(_(e_var_notfound), name);
1183 return FAIL;
1184 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001185 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001186
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001187 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001188}
1189
1190/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001191 * Generate an ISN_UNLET instruction.
1192 */
1193 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001194generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001195{
1196 isn_T *isn;
1197
1198 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001199 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001200 return FAIL;
1201 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1202 isn->isn_arg.unlet.ul_forceit = forceit;
1203
1204 return OK;
1205}
1206
1207/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208 * Generate an ISN_LOADS instruction.
1209 */
1210 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001211generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001213 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001215 int sid,
1216 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217{
1218 isn_T *isn;
1219
Bram Moolenaar080457c2020-03-03 21:53:32 +01001220 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001221 if (isn_type == ISN_LOADS)
1222 isn = generate_instr_type(cctx, isn_type, type);
1223 else
1224 isn = generate_instr_drop(cctx, isn_type, 1);
1225 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001226 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001227 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1228 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229
1230 return OK;
1231}
1232
1233/*
1234 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1235 */
1236 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001237generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 cctx_T *cctx,
1239 isntype_T isn_type,
1240 int sid,
1241 int idx,
1242 type_T *type)
1243{
1244 isn_T *isn;
1245
Bram Moolenaar080457c2020-03-03 21:53:32 +01001246 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 if (isn_type == ISN_LOADSCRIPT)
1248 isn = generate_instr_type(cctx, isn_type, type);
1249 else
1250 isn = generate_instr_drop(cctx, isn_type, 1);
1251 if (isn == NULL)
1252 return FAIL;
1253 isn->isn_arg.script.script_sid = sid;
1254 isn->isn_arg.script.script_idx = idx;
1255 return OK;
1256}
1257
1258/*
1259 * Generate an ISN_NEWLIST instruction.
1260 */
1261 static int
1262generate_NEWLIST(cctx_T *cctx, int count)
1263{
1264 isn_T *isn;
1265 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266 type_T *type;
1267 type_T *member;
1268
Bram Moolenaar080457c2020-03-03 21:53:32 +01001269 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1271 return FAIL;
1272 isn->isn_arg.number = count;
1273
1274 // drop the value types
1275 stack->ga_len -= count;
1276
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001277 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001278 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 if (count > 0)
1280 member = ((type_T **)stack->ga_data)[stack->ga_len];
1281 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001282 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001283 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284
1285 // add the list type to the type stack
1286 if (ga_grow(stack, 1) == FAIL)
1287 return FAIL;
1288 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1289 ++stack->ga_len;
1290
1291 return OK;
1292}
1293
1294/*
1295 * Generate an ISN_NEWDICT instruction.
1296 */
1297 static int
1298generate_NEWDICT(cctx_T *cctx, int count)
1299{
1300 isn_T *isn;
1301 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001302 type_T *type;
1303 type_T *member;
1304
Bram Moolenaar080457c2020-03-03 21:53:32 +01001305 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1307 return FAIL;
1308 isn->isn_arg.number = count;
1309
1310 // drop the key and value types
1311 stack->ga_len -= 2 * count;
1312
Bram Moolenaar436472f2020-02-20 22:54:43 +01001313 // Use the first value type for the list member type. Use "void" for an
1314 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 if (count > 0)
1316 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1317 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001318 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001319 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320
1321 // add the dict type to the type stack
1322 if (ga_grow(stack, 1) == FAIL)
1323 return FAIL;
1324 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1325 ++stack->ga_len;
1326
1327 return OK;
1328}
1329
1330/*
1331 * Generate an ISN_FUNCREF instruction.
1332 */
1333 static int
1334generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1335{
1336 isn_T *isn;
1337 garray_T *stack = &cctx->ctx_type_stack;
1338
Bram Moolenaar080457c2020-03-03 21:53:32 +01001339 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1341 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001342 isn->isn_arg.funcref.fr_func = dfunc_idx;
1343 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344
1345 if (ga_grow(stack, 1) == FAIL)
1346 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001347 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 // TODO: argument and return types
1349 ++stack->ga_len;
1350
1351 return OK;
1352}
1353
1354/*
1355 * Generate an ISN_JUMP instruction.
1356 */
1357 static int
1358generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1359{
1360 isn_T *isn;
1361 garray_T *stack = &cctx->ctx_type_stack;
1362
Bram Moolenaar080457c2020-03-03 21:53:32 +01001363 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1365 return FAIL;
1366 isn->isn_arg.jump.jump_when = when;
1367 isn->isn_arg.jump.jump_where = where;
1368
1369 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1370 --stack->ga_len;
1371
1372 return OK;
1373}
1374
1375 static int
1376generate_FOR(cctx_T *cctx, int loop_idx)
1377{
1378 isn_T *isn;
1379 garray_T *stack = &cctx->ctx_type_stack;
1380
Bram Moolenaar080457c2020-03-03 21:53:32 +01001381 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1383 return FAIL;
1384 isn->isn_arg.forloop.for_idx = loop_idx;
1385
1386 if (ga_grow(stack, 1) == FAIL)
1387 return FAIL;
1388 // type doesn't matter, will be stored next
1389 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1390 ++stack->ga_len;
1391
1392 return OK;
1393}
1394
1395/*
1396 * Generate an ISN_BCALL instruction.
1397 * Return FAIL if the number of arguments is wrong.
1398 */
1399 static int
1400generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1401{
1402 isn_T *isn;
1403 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001404 type_T *argtypes[MAX_FUNC_ARGS];
1405 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406
Bram Moolenaar080457c2020-03-03 21:53:32 +01001407 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408 if (check_internal_func(func_idx, argcount) == FAIL)
1409 return FAIL;
1410
1411 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1412 return FAIL;
1413 isn->isn_arg.bfunc.cbf_idx = func_idx;
1414 isn->isn_arg.bfunc.cbf_argcount = argcount;
1415
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001416 for (i = 0; i < argcount; ++i)
1417 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 stack->ga_len -= argcount; // drop the arguments
1420 if (ga_grow(stack, 1) == FAIL)
1421 return FAIL;
1422 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001423 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 ++stack->ga_len; // add return value
1425
1426 return OK;
1427}
1428
1429/*
1430 * Generate an ISN_DCALL or ISN_UCALL instruction.
1431 * Return FAIL if the number of arguments is wrong.
1432 */
1433 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001434generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435{
1436 isn_T *isn;
1437 garray_T *stack = &cctx->ctx_type_stack;
1438 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001439 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001440
Bram Moolenaar080457c2020-03-03 21:53:32 +01001441 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 if (argcount > regular_args && !has_varargs(ufunc))
1443 {
1444 semsg(_(e_toomanyarg), ufunc->uf_name);
1445 return FAIL;
1446 }
1447 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1448 {
1449 semsg(_(e_toofewarg), ufunc->uf_name);
1450 return FAIL;
1451 }
1452
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001453 if (ufunc->uf_dfunc_idx >= 0)
1454 {
1455 int i;
1456
1457 for (i = 0; i < argcount; ++i)
1458 {
1459 type_T *expected;
1460 type_T *actual;
1461
1462 if (i < regular_args)
1463 {
1464 if (ufunc->uf_arg_types == NULL)
1465 continue;
1466 expected = ufunc->uf_arg_types[i];
1467 }
1468 else
1469 expected = ufunc->uf_va_type->tt_member;
1470 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001471 if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001472 {
1473 arg_type_mismatch(expected, actual, i + 1);
1474 return FAIL;
1475 }
1476 }
1477 }
1478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 if ((isn = generate_instr(cctx,
1480 ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL)
1481 return FAIL;
1482 if (ufunc->uf_dfunc_idx >= 0)
1483 {
1484 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1485 isn->isn_arg.dfunc.cdf_argcount = argcount;
1486 }
1487 else
1488 {
1489 // A user function may be deleted and redefined later, can't use the
1490 // ufunc pointer, need to look it up again at runtime.
1491 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1492 isn->isn_arg.ufunc.cuf_argcount = argcount;
1493 }
1494
1495 stack->ga_len -= argcount; // drop the arguments
1496 if (ga_grow(stack, 1) == FAIL)
1497 return FAIL;
1498 // add return value
1499 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1500 ++stack->ga_len;
1501
1502 return OK;
1503}
1504
1505/*
1506 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1507 */
1508 static int
1509generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1510{
1511 isn_T *isn;
1512 garray_T *stack = &cctx->ctx_type_stack;
1513
Bram Moolenaar080457c2020-03-03 21:53:32 +01001514 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1516 return FAIL;
1517 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1518 isn->isn_arg.ufunc.cuf_argcount = argcount;
1519
1520 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001521 if (ga_grow(stack, 1) == FAIL)
1522 return FAIL;
1523 // add return value
1524 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1525 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526
1527 return OK;
1528}
1529
1530/*
1531 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001532 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 */
1534 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001535generate_PCALL(
1536 cctx_T *cctx,
1537 int argcount,
1538 char_u *name,
1539 type_T *type,
1540 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541{
1542 isn_T *isn;
1543 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001544 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545
Bram Moolenaar080457c2020-03-03 21:53:32 +01001546 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001547
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001548 if (type->tt_type == VAR_ANY)
1549 ret_type = &t_any;
1550 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001551 {
1552 if (type->tt_argcount != -1)
1553 {
1554 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1555
1556 if (argcount < type->tt_min_argcount - varargs)
1557 {
1558 semsg(_(e_toofewarg), "[reference]");
1559 return FAIL;
1560 }
1561 if (!varargs && argcount > type->tt_argcount)
1562 {
1563 semsg(_(e_toomanyarg), "[reference]");
1564 return FAIL;
1565 }
1566 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001567 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001568 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001569 else
1570 {
1571 semsg(_("E1085: Not a callable type: %s"), name);
1572 return FAIL;
1573 }
1574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1576 return FAIL;
1577 isn->isn_arg.pfunc.cpf_top = at_top;
1578 isn->isn_arg.pfunc.cpf_argcount = argcount;
1579
1580 stack->ga_len -= argcount; // drop the arguments
1581
1582 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001583 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001585 // If partial is above the arguments it must be cleared and replaced with
1586 // the return value.
1587 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1588 return FAIL;
1589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 return OK;
1591}
1592
1593/*
1594 * Generate an ISN_MEMBER instruction.
1595 */
1596 static int
1597generate_MEMBER(cctx_T *cctx, char_u *name, size_t len)
1598{
1599 isn_T *isn;
1600 garray_T *stack = &cctx->ctx_type_stack;
1601 type_T *type;
1602
Bram Moolenaar080457c2020-03-03 21:53:32 +01001603 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL)
1605 return FAIL;
1606 isn->isn_arg.string = vim_strnsave(name, (int)len);
1607
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001608 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001610 if (type->tt_type != VAR_DICT && type != &t_any)
1611 {
1612 emsg(_(e_dictreq));
1613 return FAIL;
1614 }
1615 // change dict type to dict member type
1616 if (type->tt_type == VAR_DICT)
1617 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618
1619 return OK;
1620}
1621
1622/*
1623 * Generate an ISN_ECHO instruction.
1624 */
1625 static int
1626generate_ECHO(cctx_T *cctx, int with_white, int count)
1627{
1628 isn_T *isn;
1629
Bram Moolenaar080457c2020-03-03 21:53:32 +01001630 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1632 return FAIL;
1633 isn->isn_arg.echo.echo_with_white = with_white;
1634 isn->isn_arg.echo.echo_count = count;
1635
1636 return OK;
1637}
1638
Bram Moolenaarad39c092020-02-26 18:23:43 +01001639/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001640 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001641 */
1642 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001643generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001644{
1645 isn_T *isn;
1646
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001647 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001648 return FAIL;
1649 isn->isn_arg.number = count;
1650
1651 return OK;
1652}
1653
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 static int
1655generate_EXEC(cctx_T *cctx, char_u *line)
1656{
1657 isn_T *isn;
1658
Bram Moolenaar080457c2020-03-03 21:53:32 +01001659 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001660 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1661 return FAIL;
1662 isn->isn_arg.string = vim_strsave(line);
1663 return OK;
1664}
1665
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001666 static int
1667generate_EXECCONCAT(cctx_T *cctx, int count)
1668{
1669 isn_T *isn;
1670
1671 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1672 return FAIL;
1673 isn->isn_arg.number = count;
1674 return OK;
1675}
1676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677/*
1678 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001679 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001681 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1683{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 lvar_T *lvar;
1685
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001686 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 {
1688 emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001689 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 }
1691
1692 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001693 return NULL;
1694 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001696 // Every local variable uses the next entry on the stack. We could re-use
1697 // the last ones when leaving a scope, but then variables used in a closure
1698 // might get overwritten. To keep things simple do not re-use stack
1699 // entries. This is less efficient, but memory is cheap these days.
1700 lvar->lv_idx = cctx->ctx_locals_count++;
1701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len));
1703 lvar->lv_const = isConst;
1704 lvar->lv_type = type;
1705
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001706 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707}
1708
1709/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001710 * Remove local variables above "new_top".
1711 */
1712 static void
1713unwind_locals(cctx_T *cctx, int new_top)
1714{
1715 if (cctx->ctx_locals.ga_len > new_top)
1716 {
1717 int idx;
1718 lvar_T *lvar;
1719
1720 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1721 {
1722 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1723 vim_free(lvar->lv_name);
1724 }
1725 }
1726 cctx->ctx_locals.ga_len = new_top;
1727}
1728
1729/*
1730 * Free all local variables.
1731 */
1732 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001733free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001734{
1735 unwind_locals(cctx, 0);
1736 ga_clear(&cctx->ctx_locals);
1737}
1738
1739/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 * Skip over a type definition and return a pointer to just after it.
1741 */
1742 char_u *
1743skip_type(char_u *start)
1744{
1745 char_u *p = start;
1746
1747 while (ASCII_ISALNUM(*p) || *p == '_')
1748 ++p;
1749
1750 // Skip over "<type>"; this is permissive about white space.
1751 if (*skipwhite(p) == '<')
1752 {
1753 p = skipwhite(p);
1754 p = skip_type(skipwhite(p + 1));
1755 p = skipwhite(p);
1756 if (*p == '>')
1757 ++p;
1758 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001759 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1760 {
1761 // handle func(args): type
1762 ++p;
1763 while (*p != ')' && *p != NUL)
1764 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001765 char_u *sp = p;
1766
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001767 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001768 if (p == sp)
1769 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001770 if (*p == ',')
1771 p = skipwhite(p + 1);
1772 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001773 if (*p == ')')
1774 {
1775 if (p[1] == ':')
1776 p = skip_type(skipwhite(p + 2));
1777 else
1778 p = skipwhite(p + 1);
1779 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001780 }
1781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 return p;
1783}
1784
1785/*
1786 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001787 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 * Returns NULL in case of failure.
1789 */
1790 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001791parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792{
1793 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001794 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795
1796 if (**arg != '<')
1797 {
1798 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001799 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001800 else
1801 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001802 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 }
1804 *arg = skipwhite(*arg + 1);
1805
Bram Moolenaard77a8522020-04-03 21:59:57 +02001806 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807
1808 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001809 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 {
1811 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001812 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813 }
1814 ++*arg;
1815
1816 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001817 return get_list_type(member_type, type_gap);
1818 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819}
1820
1821/*
1822 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001823 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 */
1825 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001826parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827{
1828 char_u *p = *arg;
1829 size_t len;
1830
1831 // skip over the first word
1832 while (ASCII_ISALNUM(*p) || *p == '_')
1833 ++p;
1834 len = p - *arg;
1835
1836 switch (**arg)
1837 {
1838 case 'a':
1839 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1840 {
1841 *arg += len;
1842 return &t_any;
1843 }
1844 break;
1845 case 'b':
1846 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1847 {
1848 *arg += len;
1849 return &t_bool;
1850 }
1851 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1852 {
1853 *arg += len;
1854 return &t_blob;
1855 }
1856 break;
1857 case 'c':
1858 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1859 {
1860 *arg += len;
1861 return &t_channel;
1862 }
1863 break;
1864 case 'd':
1865 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1866 {
1867 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001868 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869 }
1870 break;
1871 case 'f':
1872 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1873 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001874#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 *arg += len;
1876 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001877#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001878 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001879 return &t_any;
1880#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881 }
1882 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1883 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001884 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001885 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001886 int argcount = -1;
1887 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001888 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001889 type_T *arg_type[MAX_FUNC_ARGS + 1];
1890
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001891 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001893 if (**arg == '(')
1894 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001895 // "func" may or may not return a value, "func()" does
1896 // not return a value.
1897 ret_type = &t_void;
1898
Bram Moolenaard77a8522020-04-03 21:59:57 +02001899 p = ++*arg;
1900 argcount = 0;
1901 while (*p != NUL && *p != ')')
1902 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001903 if (*p == '?')
1904 {
1905 if (first_optional == -1)
1906 first_optional = argcount;
1907 ++p;
1908 }
1909 else if (first_optional != -1)
1910 {
1911 emsg(_("E1007: mandatory argument after optional argument"));
1912 return &t_any;
1913 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001914 else if (STRNCMP(p, "...", 3) == 0)
1915 {
1916 flags |= TTFLAG_VARARGS;
1917 p += 3;
1918 }
1919
1920 arg_type[argcount++] = parse_type(&p, type_gap);
1921
1922 // Nothing comes after "...{type}".
1923 if (flags & TTFLAG_VARARGS)
1924 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001925
Bram Moolenaard77a8522020-04-03 21:59:57 +02001926 if (*p != ',' && *skipwhite(p) == ',')
1927 {
1928 semsg(_(e_no_white_before), ",");
1929 return &t_any;
1930 }
1931 if (*p == ',')
1932 {
1933 ++p;
1934 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001935 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001936 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001937 return &t_any;
1938 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001939 }
1940 p = skipwhite(p);
1941 if (argcount == MAX_FUNC_ARGS)
1942 {
1943 emsg(_("E740: Too many argument types"));
1944 return &t_any;
1945 }
1946 }
1947
1948 p = skipwhite(p);
1949 if (*p != ')')
1950 {
1951 emsg(_(e_missing_close));
1952 return &t_any;
1953 }
1954 *arg = p + 1;
1955 }
1956 if (**arg == ':')
1957 {
1958 // parse return type
1959 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001960 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02001961 semsg(_(e_white_after), ":");
1962 *arg = skipwhite(*arg);
1963 ret_type = parse_type(arg, type_gap);
1964 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001965 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001966 type = get_func_type(ret_type, argcount, type_gap);
1967 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02001968 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001969 type = alloc_func_type(ret_type, argcount, type_gap);
1970 type->tt_flags = flags;
1971 if (argcount > 0)
1972 {
1973 type->tt_argcount = argcount;
1974 type->tt_min_argcount = first_optional == -1
1975 ? argcount : first_optional;
1976 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001977 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001978 return &t_any;
1979 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02001980 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001981 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001982 }
1983 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 }
1985 break;
1986 case 'j':
1987 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
1988 {
1989 *arg += len;
1990 return &t_job;
1991 }
1992 break;
1993 case 'l':
1994 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
1995 {
1996 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001997 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 }
1999 break;
2000 case 'n':
2001 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2002 {
2003 *arg += len;
2004 return &t_number;
2005 }
2006 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 case 's':
2008 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2009 {
2010 *arg += len;
2011 return &t_string;
2012 }
2013 break;
2014 case 'v':
2015 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2016 {
2017 *arg += len;
2018 return &t_void;
2019 }
2020 break;
2021 }
2022
2023 semsg(_("E1010: Type not recognized: %s"), *arg);
2024 return &t_any;
2025}
2026
2027/*
2028 * Check if "type1" and "type2" are exactly the same.
2029 */
2030 static int
2031equal_type(type_T *type1, type_T *type2)
2032{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002033 int i;
2034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 if (type1->tt_type != type2->tt_type)
2036 return FALSE;
2037 switch (type1->tt_type)
2038 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002040 case VAR_ANY:
2041 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 case VAR_SPECIAL:
2043 case VAR_BOOL:
2044 case VAR_NUMBER:
2045 case VAR_FLOAT:
2046 case VAR_STRING:
2047 case VAR_BLOB:
2048 case VAR_JOB:
2049 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002050 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051 case VAR_LIST:
2052 case VAR_DICT:
2053 return equal_type(type1->tt_member, type2->tt_member);
2054 case VAR_FUNC:
2055 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002056 if (!equal_type(type1->tt_member, type2->tt_member)
2057 || type1->tt_argcount != type2->tt_argcount)
2058 return FALSE;
2059 if (type1->tt_argcount < 0
2060 || type1->tt_args == NULL || type2->tt_args == NULL)
2061 return TRUE;
2062 for (i = 0; i < type1->tt_argcount; ++i)
2063 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2064 return FALSE;
2065 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 }
2067 return TRUE;
2068}
2069
2070/*
2071 * Find the common type of "type1" and "type2" and put it in "dest".
2072 * "type2" and "dest" may be the same.
2073 */
2074 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002075common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076{
2077 if (equal_type(type1, type2))
2078 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002079 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080 return;
2081 }
2082
2083 if (type1->tt_type == type2->tt_type)
2084 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2086 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002087 type_T *common;
2088
Bram Moolenaard77a8522020-04-03 21:59:57 +02002089 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002090 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002091 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002092 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002093 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002094 return;
2095 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002096 if (type1->tt_type == VAR_FUNC)
2097 {
2098 type_T *common;
2099
2100 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2101 if (type1->tt_argcount == type2->tt_argcount
2102 && type1->tt_argcount >= 0)
2103 {
2104 int argcount = type1->tt_argcount;
2105 int i;
2106
2107 *dest = alloc_func_type(common, argcount, type_gap);
2108 if (type1->tt_args != NULL && type2->tt_args != NULL)
2109 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002110 if (func_type_add_arg_types(*dest, argcount,
2111 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002112 for (i = 0; i < argcount; ++i)
2113 common_type(type1->tt_args[i], type2->tt_args[i],
2114 &(*dest)->tt_args[i], type_gap);
2115 }
2116 }
2117 else
2118 *dest = alloc_func_type(common, -1, type_gap);
2119 return;
2120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121 }
2122
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002123 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124}
2125
2126 char *
2127vartype_name(vartype_T type)
2128{
2129 switch (type)
2130 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002131 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002132 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 case VAR_SPECIAL: return "special";
2135 case VAR_BOOL: return "bool";
2136 case VAR_NUMBER: return "number";
2137 case VAR_FLOAT: return "float";
2138 case VAR_STRING: return "string";
2139 case VAR_BLOB: return "blob";
2140 case VAR_JOB: return "job";
2141 case VAR_CHANNEL: return "channel";
2142 case VAR_LIST: return "list";
2143 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002144
2145 case VAR_FUNC:
2146 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002147 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002148 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149}
2150
2151/*
2152 * Return the name of a type.
2153 * The result may be in allocated memory, in which case "tofree" is set.
2154 */
2155 char *
2156type_name(type_T *type, char **tofree)
2157{
2158 char *name = vartype_name(type->tt_type);
2159
2160 *tofree = NULL;
2161 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2162 {
2163 char *member_free;
2164 char *member_name = type_name(type->tt_member, &member_free);
2165 size_t len;
2166
2167 len = STRLEN(name) + STRLEN(member_name) + 3;
2168 *tofree = alloc(len);
2169 if (*tofree != NULL)
2170 {
2171 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2172 vim_free(member_free);
2173 return *tofree;
2174 }
2175 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002176 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002177 {
2178 garray_T ga;
2179 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002180 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002181
2182 ga_init2(&ga, 1, 100);
2183 if (ga_grow(&ga, 20) == FAIL)
2184 return "[unknown]";
2185 *tofree = ga.ga_data;
2186 STRCPY(ga.ga_data, "func(");
2187 ga.ga_len += 5;
2188
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002189 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002190 {
2191 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002192 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002193 int len;
2194
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002195 if (type->tt_args == NULL)
2196 arg_type = "[unknown]";
2197 else
2198 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002199 if (i > 0)
2200 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002201 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002202 ga.ga_len += 2;
2203 }
2204 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002205 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002206 {
2207 vim_free(arg_free);
2208 return "[unknown]";
2209 }
2210 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002211 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002212 {
2213 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2214 ga.ga_len += 3;
2215 }
2216 else if (i >= type->tt_min_argcount)
2217 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002218 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002219 ga.ga_len += len;
2220 vim_free(arg_free);
2221 }
2222
2223 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002224 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002225 else
2226 {
2227 char *ret_free;
2228 char *ret_name = type_name(type->tt_member, &ret_free);
2229 int len;
2230
2231 len = (int)STRLEN(ret_name) + 4;
2232 if (ga_grow(&ga, len) == FAIL)
2233 {
2234 vim_free(ret_free);
2235 return "[unknown]";
2236 }
2237 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002238 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2239 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002240 vim_free(ret_free);
2241 }
2242 return ga.ga_data;
2243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002244
2245 return name;
2246}
2247
2248/*
2249 * Find "name" in script-local items of script "sid".
2250 * Returns the index in "sn_var_vals" if found.
2251 * If found but not in "sn_var_vals" returns -1.
2252 * If not found returns -2.
2253 */
2254 int
2255get_script_item_idx(int sid, char_u *name, int check_writable)
2256{
2257 hashtab_T *ht;
2258 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002259 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 int idx;
2261
2262 // First look the name up in the hashtable.
2263 if (sid <= 0 || sid > script_items.ga_len)
2264 return -1;
2265 ht = &SCRIPT_VARS(sid);
2266 di = find_var_in_ht(ht, 0, name, TRUE);
2267 if (di == NULL)
2268 return -2;
2269
2270 // Now find the svar_T index in sn_var_vals.
2271 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2272 {
2273 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2274
2275 if (sv->sv_tv == &di->di_tv)
2276 {
2277 if (check_writable && sv->sv_const)
2278 semsg(_(e_readonlyvar), name);
2279 return idx;
2280 }
2281 }
2282 return -1;
2283}
2284
2285/*
2286 * Find "name" in imported items of the current script/
2287 */
2288 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002289find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002291 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 int idx;
2293
2294 if (cctx != NULL)
2295 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2296 {
2297 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2298 + idx;
2299
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002300 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2301 : STRLEN(import->imp_name) == len
2302 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 return import;
2304 }
2305
2306 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2307 {
2308 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2309
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002310 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2311 : STRLEN(import->imp_name) == len
2312 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 return import;
2314 }
2315 return NULL;
2316}
2317
2318/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002319 * Free all imported variables.
2320 */
2321 static void
2322free_imported(cctx_T *cctx)
2323{
2324 int idx;
2325
2326 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2327 {
2328 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2329
2330 vim_free(import->imp_name);
2331 }
2332 ga_clear(&cctx->ctx_imports);
2333}
2334
2335/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002336 * Get the next line of the function from "cctx".
2337 * Returns NULL when at the end.
2338 */
2339 static char_u *
2340next_line_from_context(cctx_T *cctx)
2341{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002342 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002343
2344 do
2345 {
2346 ++cctx->ctx_lnum;
2347 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002348 {
2349 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002350 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002351 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002352 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002353 cctx->ctx_line_start = line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002354 SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum
2355 + cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002356 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002357 return line;
2358}
2359
2360/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002361 * Return TRUE if "p" points at a "#" but not at "#{".
2362 */
2363 static int
2364comment_start(char_u *p)
2365{
2366 return p[0] == '#' && p[1] != '{';
2367}
2368
2369/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002370 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002371 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002372 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2373 */
2374 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002375may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002376{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002377 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002378 {
2379 char_u *next = next_line_from_context(cctx);
2380
2381 if (next == NULL)
2382 return FAIL;
2383 *arg = skipwhite(next);
2384 }
2385 return OK;
2386}
2387
2388/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002389 * Generate an instruction to load script-local variable "name", without the
2390 * leading "s:".
2391 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392 */
2393 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002394compile_load_scriptvar(
2395 cctx_T *cctx,
2396 char_u *name, // variable NUL terminated
2397 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002398 char_u **end, // end of variable
2399 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002401 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2403 imported_T *import;
2404
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002405 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002407 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002408 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2409 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 }
2411 if (idx >= 0)
2412 {
2413 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2414
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002415 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 current_sctx.sc_sid, idx, sv->sv_type);
2417 return OK;
2418 }
2419
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002420 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421 if (import != NULL)
2422 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002423 if (import->imp_all)
2424 {
2425 char_u *p = skipwhite(*end);
2426 int name_len;
2427 ufunc_T *ufunc;
2428 type_T *type;
2429
2430 // Used "import * as Name", need to lookup the member.
2431 if (*p != '.')
2432 {
2433 semsg(_("E1060: expected dot after name: %s"), start);
2434 return FAIL;
2435 }
2436 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002437 if (VIM_ISWHITE(*p))
2438 {
2439 emsg(_("E1074: no white space allowed after dot"));
2440 return FAIL;
2441 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002442
2443 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2444 // TODO: what if it is a function?
2445 if (idx < 0)
2446 return FAIL;
2447 *end = p;
2448
2449 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2450 import->imp_sid,
2451 idx,
2452 type);
2453 }
2454 else
2455 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002456 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002457 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2458 import->imp_sid,
2459 import->imp_var_vals_idx,
2460 import->imp_type);
2461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 return OK;
2463 }
2464
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002465 if (error)
2466 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 return FAIL;
2468}
2469
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002470 static int
2471generate_funcref(cctx_T *cctx, char_u *name)
2472{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002473 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002474
2475 if (ufunc == NULL)
2476 return FAIL;
2477
2478 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2479}
2480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481/*
2482 * Compile a variable name into a load instruction.
2483 * "end" points to just after the name.
2484 * When "error" is FALSE do not give an error when not found.
2485 */
2486 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002487compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488{
2489 type_T *type;
2490 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002491 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002493 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494
2495 if (*(*arg + 1) == ':')
2496 {
2497 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002498 if (end <= *arg + 2)
2499 name = vim_strsave((char_u *)"[empty]");
2500 else
2501 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 if (name == NULL)
2503 return FAIL;
2504
2505 if (**arg == 'v')
2506 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002507 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 }
2509 else if (**arg == 'g')
2510 {
2511 // Global variables can be defined later, thus we don't check if it
2512 // exists, give error at runtime.
2513 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2514 }
2515 else if (**arg == 's')
2516 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002517 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002519 else if (**arg == 'b')
2520 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002521 // Buffer-local variables can be defined later, thus we don't check
2522 // if it exists, give error at runtime.
2523 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002524 }
2525 else if (**arg == 'w')
2526 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002527 // Window-local variables can be defined later, thus we don't check
2528 // if it exists, give error at runtime.
2529 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002530 }
2531 else if (**arg == 't')
2532 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002533 // Tabpage-local variables can be defined later, thus we don't
2534 // check if it exists, give error at runtime.
2535 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002536 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 else
2538 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002539 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 goto theend;
2541 }
2542 }
2543 else
2544 {
2545 size_t len = end - *arg;
2546 int idx;
2547 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002548 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549
2550 name = vim_strnsave(*arg, end - *arg);
2551 if (name == NULL)
2552 return FAIL;
2553
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002554 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002556 if (!gen_load_outer)
2557 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 }
2559 else
2560 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002561 lvar_T *lvar = lookup_local(*arg, len, cctx);
2562
2563 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002565 type = lvar->lv_type;
2566 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002567 if (lvar->lv_from_outer)
2568 gen_load_outer = TRUE;
2569 else
2570 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 }
2572 else
2573 {
2574 if ((len == 4 && STRNCMP("true", *arg, 4) == 0)
2575 || (len == 5 && STRNCMP("false", *arg, 5) == 0))
2576 res = generate_PUSHBOOL(cctx, **arg == 't'
2577 ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002578 else
2579 {
2580 // "var" can be script-local even without using "s:" if it
2581 // already exists.
2582 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2583 == SCRIPT_VERSION_VIM9
2584 || lookup_script(*arg, len) == OK)
2585 res = compile_load_scriptvar(cctx, name, *arg, &end,
2586 FALSE);
2587
2588 // When the name starts with an uppercase letter or "x:" it
2589 // can be a user defined function.
2590 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2591 res = generate_funcref(cctx, name);
2592 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 }
2594 }
2595 if (gen_load)
2596 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002597 if (gen_load_outer)
2598 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 }
2600
2601 *arg = end;
2602
2603theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002604 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 semsg(_(e_var_notfound), name);
2606 vim_free(name);
2607 return res;
2608}
2609
2610/*
2611 * Compile the argument expressions.
2612 * "arg" points to just after the "(" and is advanced to after the ")"
2613 */
2614 static int
2615compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2616{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002617 char_u *p = *arg;
2618 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619
Bram Moolenaare6085c52020-04-12 20:19:16 +02002620 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002622 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002623 {
2624 p = next_line_from_context(cctx);
2625 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002626 goto failret;
2627 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002628 p = skipwhite(p);
2629 }
2630 if (*p == ')')
2631 {
2632 *arg = p + 1;
2633 return OK;
2634 }
2635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 if (compile_expr1(&p, cctx) == FAIL)
2637 return FAIL;
2638 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002639
2640 if (*p != ',' && *skipwhite(p) == ',')
2641 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002642 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002643 p = skipwhite(p);
2644 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002646 {
2647 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002648 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002649 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002650 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002651 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002652 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002654failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002655 emsg(_(e_missing_close));
2656 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657}
2658
2659/*
2660 * Compile a function call: name(arg1, arg2)
2661 * "arg" points to "name", "arg + varlen" to the "(".
2662 * "argcount_init" is 1 for "value->method()"
2663 * Instructions:
2664 * EVAL arg1
2665 * EVAL arg2
2666 * BCALL / DCALL / UCALL
2667 */
2668 static int
2669compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init)
2670{
2671 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002672 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 int argcount = argcount_init;
2674 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002675 char_u fname_buf[FLEN_FIXED + 1];
2676 char_u *tofree = NULL;
2677 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002678 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002679 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680
2681 if (varlen >= sizeof(namebuf))
2682 {
2683 semsg(_("E1011: name too long: %s"), name);
2684 return FAIL;
2685 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002686 vim_strncpy(namebuf, *arg, varlen);
2687 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688
2689 *arg = skipwhite(*arg + varlen + 1);
2690 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002691 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002693 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 {
2695 int idx;
2696
2697 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002698 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002700 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002701 else
2702 semsg(_(e_unknownfunc), namebuf);
2703 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 }
2705
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002707 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002709 {
2710 res = generate_CALL(cctx, ufunc, argcount);
2711 goto theend;
2712 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713
2714 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002715 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002717 if (STRNCMP(namebuf, "g:", 2) != 0
2718 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002719 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002720 garray_T *stack = &cctx->ctx_type_stack;
2721 type_T *type;
2722
2723 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2724 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002725 goto theend;
2726 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002728 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002729 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002730 if (STRNCMP(namebuf, "g:", 2) == 0)
2731 res = generate_UCALL(cctx, name, argcount);
2732 else
2733 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002734
2735theend:
2736 vim_free(tofree);
2737 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738}
2739
2740// like NAMESPACE_CHAR but with 'a' and 'l'.
2741#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2742
2743/*
2744 * Find the end of a variable or function name. Unlike find_name_end() this
2745 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002746 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747 * Return a pointer to just after the name. Equal to "arg" if there is no
2748 * valid name.
2749 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002750 static char_u *
2751to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752{
2753 char_u *p;
2754
2755 // Quick check for valid starting character.
2756 if (!eval_isnamec1(*arg))
2757 return arg;
2758
2759 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2760 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2761 // and can be used in slice "[n:]".
2762 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002763 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2765 break;
2766 return p;
2767}
2768
2769/*
2770 * Like to_name_end() but also skip over a list or dict constant.
2771 */
2772 char_u *
2773to_name_const_end(char_u *arg)
2774{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002775 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 typval_T rettv;
2777
2778 if (p == arg && *arg == '[')
2779 {
2780
2781 // Can be "[1, 2, 3]->Func()".
2782 if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL)
2783 p = arg;
2784 }
2785 else if (p == arg && *arg == '#' && arg[1] == '{')
2786 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002787 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 ++p;
2789 if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL)
2790 p = arg;
2791 }
2792 else if (p == arg && *arg == '{')
2793 {
2794 int ret = get_lambda_tv(&p, &rettv, FALSE);
2795
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002796 // Can be "{x -> ret}()".
2797 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 if (ret == NOTDONE)
2799 ret = eval_dict(&p, &rettv, FALSE, FALSE);
2800 if (ret != OK)
2801 p = arg;
2802 }
2803
2804 return p;
2805}
2806
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807/*
2808 * parse a list: [expr, expr]
2809 * "*arg" points to the '['.
2810 */
2811 static int
2812compile_list(char_u **arg, cctx_T *cctx)
2813{
2814 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002815 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 int count = 0;
2817
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002818 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002820 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002821 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002822 p = next_line_from_context(cctx);
2823 if (p == NULL)
2824 {
2825 semsg(_(e_list_end), *arg);
2826 return FAIL;
2827 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002828 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002829 p = skipwhite(p);
2830 }
2831 if (*p == ']')
2832 {
2833 ++p;
2834 // Allow for following comment, after at least one space.
2835 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
2836 p += STRLEN(p);
2837 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002838 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 if (compile_expr1(&p, cctx) == FAIL)
2840 break;
2841 ++count;
2842 if (*p == ',')
2843 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002844 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 p = skipwhite(p);
2846 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002847 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848
2849 generate_NEWLIST(cctx, count);
2850 return OK;
2851}
2852
2853/*
2854 * parse a lambda: {arg, arg -> expr}
2855 * "*arg" points to the '{'.
2856 */
2857 static int
2858compile_lambda(char_u **arg, cctx_T *cctx)
2859{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 typval_T rettv;
2861 ufunc_T *ufunc;
2862
2863 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01002864 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002866
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002868 ++ufunc->uf_refcount;
2869 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002870 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871
2872 // The function will have one line: "return {expr}".
2873 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002874 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875
2876 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002877 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 return FAIL;
2879}
2880
2881/*
2882 * Compile a lamda call: expr->{lambda}(args)
2883 * "arg" points to the "{".
2884 */
2885 static int
2886compile_lambda_call(char_u **arg, cctx_T *cctx)
2887{
2888 ufunc_T *ufunc;
2889 typval_T rettv;
2890 int argcount = 1;
2891 int ret = FAIL;
2892
2893 // Get the funcref in "rettv".
2894 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
2895 return FAIL;
2896
2897 if (**arg != '(')
2898 {
2899 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002900 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 else
2902 semsg(_(e_missing_paren), "lambda");
2903 clear_tv(&rettv);
2904 return FAIL;
2905 }
2906
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 ufunc = rettv.vval.v_partial->pt_func;
2908 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002909 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002910 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002911
2912 // The function will have one line: "return {expr}".
2913 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002914 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915
2916 // compile the arguments
2917 *arg = skipwhite(*arg + 1);
2918 if (compile_arguments(arg, cctx, &argcount) == OK)
2919 // call the compiled function
2920 ret = generate_CALL(cctx, ufunc, argcount);
2921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 return ret;
2923}
2924
2925/*
2926 * parse a dict: {'key': val} or #{key: val}
2927 * "*arg" points to the '{'.
2928 */
2929 static int
2930compile_dict(char_u **arg, cctx_T *cctx, int literal)
2931{
2932 garray_T *instr = &cctx->ctx_instr;
2933 int count = 0;
2934 dict_T *d = dict_alloc();
2935 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002936 char_u *whitep = *arg;
2937 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938
2939 if (d == NULL)
2940 return FAIL;
2941 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002942 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 {
2944 char_u *key = NULL;
2945
Bram Moolenaar2c330432020-04-13 14:41:35 +02002946 while (**arg == NUL || (literal && **arg == '"')
2947 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002948 {
2949 *arg = next_line_from_context(cctx);
2950 if (*arg == NULL)
2951 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002952 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002953 *arg = skipwhite(*arg);
2954 }
2955
2956 if (**arg == '}')
2957 break;
2958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 if (literal)
2960 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002961 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962
Bram Moolenaar2c330432020-04-13 14:41:35 +02002963 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 {
2965 semsg(_("E1014: Invalid key: %s"), *arg);
2966 return FAIL;
2967 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002968 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 if (generate_PUSHS(cctx, key) == FAIL)
2970 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002971 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 }
2973 else
2974 {
2975 isn_T *isn;
2976
2977 if (compile_expr1(arg, cctx) == FAIL)
2978 return FAIL;
2979 // TODO: check type is string
2980 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2981 if (isn->isn_type == ISN_PUSHS)
2982 key = isn->isn_arg.string;
2983 }
2984
2985 // Check for duplicate keys, if using string keys.
2986 if (key != NULL)
2987 {
2988 item = dict_find(d, key, -1);
2989 if (item != NULL)
2990 {
2991 semsg(_(e_duplicate_key), key);
2992 goto failret;
2993 }
2994 item = dictitem_alloc(key);
2995 if (item != NULL)
2996 {
2997 item->di_tv.v_type = VAR_UNKNOWN;
2998 item->di_tv.v_lock = 0;
2999 if (dict_add(d, item) == FAIL)
3000 dictitem_free(item);
3001 }
3002 }
3003
3004 *arg = skipwhite(*arg);
3005 if (**arg != ':')
3006 {
3007 semsg(_(e_missing_dict_colon), *arg);
3008 return FAIL;
3009 }
3010
Bram Moolenaar2c330432020-04-13 14:41:35 +02003011 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003013 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003014 {
3015 *arg = next_line_from_context(cctx);
3016 if (*arg == NULL)
3017 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003018 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003019 *arg = skipwhite(*arg);
3020 }
3021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 if (compile_expr1(arg, cctx) == FAIL)
3023 return FAIL;
3024 ++count;
3025
Bram Moolenaar2c330432020-04-13 14:41:35 +02003026 whitep = *arg;
3027 p = skipwhite(*arg);
3028 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003029 {
3030 *arg = next_line_from_context(cctx);
3031 if (*arg == NULL)
3032 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003033 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003034 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003035 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003036 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 if (**arg == '}')
3038 break;
3039 if (**arg != ',')
3040 {
3041 semsg(_(e_missing_dict_comma), *arg);
3042 goto failret;
3043 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003044 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 *arg = skipwhite(*arg + 1);
3046 }
3047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 *arg = *arg + 1;
3049
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003050 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003051 p = skipwhite(*arg);
3052 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003053 *arg += STRLEN(*arg);
3054
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 dict_unref(d);
3056 return generate_NEWDICT(cctx, count);
3057
3058failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003059 if (*arg == NULL)
3060 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 dict_unref(d);
3062 return FAIL;
3063}
3064
3065/*
3066 * Compile "&option".
3067 */
3068 static int
3069compile_get_option(char_u **arg, cctx_T *cctx)
3070{
3071 typval_T rettv;
3072 char_u *start = *arg;
3073 int ret;
3074
3075 // parse the option and get the current value to get the type.
3076 rettv.v_type = VAR_UNKNOWN;
3077 ret = get_option_tv(arg, &rettv, TRUE);
3078 if (ret == OK)
3079 {
3080 // include the '&' in the name, get_option_tv() expects it.
3081 char_u *name = vim_strnsave(start, *arg - start);
3082 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3083
3084 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3085 vim_free(name);
3086 }
3087 clear_tv(&rettv);
3088
3089 return ret;
3090}
3091
3092/*
3093 * Compile "$VAR".
3094 */
3095 static int
3096compile_get_env(char_u **arg, cctx_T *cctx)
3097{
3098 char_u *start = *arg;
3099 int len;
3100 int ret;
3101 char_u *name;
3102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 ++*arg;
3104 len = get_env_len(arg);
3105 if (len == 0)
3106 {
3107 semsg(_(e_syntax_at), start - 1);
3108 return FAIL;
3109 }
3110
3111 // include the '$' in the name, get_env_tv() expects it.
3112 name = vim_strnsave(start, len + 1);
3113 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3114 vim_free(name);
3115 return ret;
3116}
3117
3118/*
3119 * Compile "@r".
3120 */
3121 static int
3122compile_get_register(char_u **arg, cctx_T *cctx)
3123{
3124 int ret;
3125
3126 ++*arg;
3127 if (**arg == NUL)
3128 {
3129 semsg(_(e_syntax_at), *arg - 1);
3130 return FAIL;
3131 }
3132 if (!valid_yank_reg(**arg, TRUE))
3133 {
3134 emsg_invreg(**arg);
3135 return FAIL;
3136 }
3137 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3138 ++*arg;
3139 return ret;
3140}
3141
3142/*
3143 * Apply leading '!', '-' and '+' to constant "rettv".
3144 */
3145 static int
3146apply_leader(typval_T *rettv, char_u *start, char_u *end)
3147{
3148 char_u *p = end;
3149
3150 // this works from end to start
3151 while (p > start)
3152 {
3153 --p;
3154 if (*p == '-' || *p == '+')
3155 {
3156 // only '-' has an effect, for '+' we only check the type
3157#ifdef FEAT_FLOAT
3158 if (rettv->v_type == VAR_FLOAT)
3159 {
3160 if (*p == '-')
3161 rettv->vval.v_float = -rettv->vval.v_float;
3162 }
3163 else
3164#endif
3165 {
3166 varnumber_T val;
3167 int error = FALSE;
3168
3169 // tv_get_number_chk() accepts a string, but we don't want that
3170 // here
3171 if (check_not_string(rettv) == FAIL)
3172 return FAIL;
3173 val = tv_get_number_chk(rettv, &error);
3174 clear_tv(rettv);
3175 if (error)
3176 return FAIL;
3177 if (*p == '-')
3178 val = -val;
3179 rettv->v_type = VAR_NUMBER;
3180 rettv->vval.v_number = val;
3181 }
3182 }
3183 else
3184 {
3185 int v = tv2bool(rettv);
3186
3187 // '!' is permissive in the type.
3188 clear_tv(rettv);
3189 rettv->v_type = VAR_BOOL;
3190 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3191 }
3192 }
3193 return OK;
3194}
3195
3196/*
3197 * Recognize v: variables that are constants and set "rettv".
3198 */
3199 static void
3200get_vim_constant(char_u **arg, typval_T *rettv)
3201{
3202 if (STRNCMP(*arg, "v:true", 6) == 0)
3203 {
3204 rettv->v_type = VAR_BOOL;
3205 rettv->vval.v_number = VVAL_TRUE;
3206 *arg += 6;
3207 }
3208 else if (STRNCMP(*arg, "v:false", 7) == 0)
3209 {
3210 rettv->v_type = VAR_BOOL;
3211 rettv->vval.v_number = VVAL_FALSE;
3212 *arg += 7;
3213 }
3214 else if (STRNCMP(*arg, "v:null", 6) == 0)
3215 {
3216 rettv->v_type = VAR_SPECIAL;
3217 rettv->vval.v_number = VVAL_NULL;
3218 *arg += 6;
3219 }
3220 else if (STRNCMP(*arg, "v:none", 6) == 0)
3221 {
3222 rettv->v_type = VAR_SPECIAL;
3223 rettv->vval.v_number = VVAL_NONE;
3224 *arg += 6;
3225 }
3226}
3227
3228/*
Bram Moolenaar61a89812020-05-07 16:58:17 +02003229 * Evaluate an expression that is a constant:
3230 * has(arg)
3231 *
3232 * Also handle:
3233 * ! in front logical NOT
3234 *
3235 * Return FAIL if the expression is not a constant.
3236 */
3237 static int
3238evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
3239{
3240 typval_T argvars[2];
3241 char_u *start_leader, *end_leader;
3242 int has_call = FALSE;
3243
3244 /*
3245 * Skip '!' characters. They are handled later.
3246 * TODO: '-' and '+' characters
3247 */
3248 start_leader = *arg;
3249 while (**arg == '!')
3250 *arg = skipwhite(*arg + 1);
3251 end_leader = *arg;
3252
3253 /*
3254 * Recognize only a few types of constants for now.
3255 */
3256 if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4]))
3257 {
3258 tv->v_type = VAR_BOOL;
3259 tv->vval.v_number = VVAL_TRUE;
3260 *arg += 4;
3261 return OK;
3262 }
3263 if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5]))
3264 {
3265 tv->v_type = VAR_BOOL;
3266 tv->vval.v_number = VVAL_FALSE;
3267 *arg += 5;
3268 return OK;
3269 }
3270
3271 if (STRNCMP("has(", *arg, 4) == 0)
3272 {
3273 has_call = TRUE;
3274 *arg = skipwhite(*arg + 4);
3275 }
3276
3277 if (**arg == '"')
3278 {
3279 if (get_string_tv(arg, tv, TRUE) == FAIL)
3280 return FAIL;
3281 }
3282 else if (**arg == '\'')
3283 {
3284 if (get_lit_string_tv(arg, tv, TRUE) == FAIL)
3285 return FAIL;
3286 }
3287 else
3288 return FAIL;
3289
3290 if (has_call)
3291 {
3292 *arg = skipwhite(*arg);
3293 if (**arg != ')')
3294 return FAIL;
3295 *arg = *arg + 1;
3296
3297 argvars[0] = *tv;
3298 argvars[1].v_type = VAR_UNKNOWN;
3299 tv->v_type = VAR_NUMBER;
3300 tv->vval.v_number = 0;
3301 f_has(argvars, tv);
3302 clear_tv(&argvars[0]);
3303
3304 while (start_leader < end_leader)
3305 {
3306 if (*start_leader == '!')
3307 tv->vval.v_number = !tv->vval.v_number;
3308 ++start_leader;
3309 }
3310 }
3311 else if (end_leader > start_leader)
3312 {
3313 clear_tv(tv);
3314 return FAIL;
3315 }
3316
3317 return OK;
3318}
3319
3320/*
3321 * * number multiplication
3322 * / number division
3323 * % number modulo
3324 */
3325 static int
3326evaluate_const_expr6(char_u **arg, cctx_T *cctx, typval_T *tv)
3327{
3328 char_u *op;
3329
3330 // get the first variable
3331 if (evaluate_const_expr7(arg, cctx, tv) == FAIL)
3332 return FAIL;
3333
3334 /*
3335 * Repeat computing, until no "*", "/" or "%" is following.
3336 */
3337 for (;;)
3338 {
3339 op = skipwhite(*arg);
3340 if (*op != '*' && *op != '/' && *op != '%')
3341 break;
3342 // TODO: not implemented yet.
3343 clear_tv(tv);
3344 return FAIL;
3345 }
3346 return OK;
3347}
3348
3349/*
3350 * + number addition
3351 * - number subtraction
3352 * .. string concatenation
3353 */
3354 static int
3355evaluate_const_expr5(char_u **arg, cctx_T *cctx, typval_T *tv)
3356{
3357 char_u *op;
3358 int oplen;
3359
3360 // get the first variable
3361 if (evaluate_const_expr6(arg, cctx, tv) == FAIL)
3362 return FAIL;
3363
3364 /*
3365 * Repeat computing, until no "+", "-" or ".." is following.
3366 */
3367 for (;;)
3368 {
3369 op = skipwhite(*arg);
3370 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
3371 break;
3372 oplen = (*op == '.' ? 2 : 1);
3373
3374 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
3375 {
3376 clear_tv(tv);
3377 return FAIL;
3378 }
3379
3380 if (*op == '.' && tv->v_type == VAR_STRING)
3381 {
3382 typval_T tv2;
3383 size_t len1;
3384 char_u *s1, *s2;
3385
3386 tv2.v_type = VAR_UNKNOWN;
3387 *arg = skipwhite(op + oplen);
3388
3389 // TODO: what if we fail???
3390 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
3391 return FAIL;
3392
3393 // get the second variable
3394 if (evaluate_const_expr6(arg, cctx, &tv2) == FAIL)
3395 {
3396 clear_tv(tv);
3397 return FAIL;
3398 }
3399 if (tv2.v_type != VAR_STRING)
3400 {
3401 clear_tv(tv);
3402 clear_tv(&tv2);
3403 return FAIL;
3404 }
3405 s1 = tv->vval.v_string;
3406 len1 = STRLEN(s1);
3407 s2 = tv2.vval.v_string;
3408 tv->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3409 if (tv->vval.v_string == NULL)
3410 {
3411 vim_free(s1);
3412 vim_free(s2);
3413 return FAIL;
3414 }
3415 mch_memmove(tv->vval.v_string, s1, len1);
3416 STRCPY(tv->vval.v_string + len1, s2);
3417 continue;
3418 }
3419
3420 // TODO: Not implemented yet.
3421 clear_tv(tv);
3422 return FAIL;
3423 }
3424 return OK;
3425}
3426
3427 static exptype_T
3428get_compare_type(char_u *p, int *len, int *type_is)
3429{
3430 exptype_T type = EXPR_UNKNOWN;
3431 int i;
3432
3433 switch (p[0])
3434 {
3435 case '=': if (p[1] == '=')
3436 type = EXPR_EQUAL;
3437 else if (p[1] == '~')
3438 type = EXPR_MATCH;
3439 break;
3440 case '!': if (p[1] == '=')
3441 type = EXPR_NEQUAL;
3442 else if (p[1] == '~')
3443 type = EXPR_NOMATCH;
3444 break;
3445 case '>': if (p[1] != '=')
3446 {
3447 type = EXPR_GREATER;
3448 *len = 1;
3449 }
3450 else
3451 type = EXPR_GEQUAL;
3452 break;
3453 case '<': if (p[1] != '=')
3454 {
3455 type = EXPR_SMALLER;
3456 *len = 1;
3457 }
3458 else
3459 type = EXPR_SEQUAL;
3460 break;
3461 case 'i': if (p[1] == 's')
3462 {
3463 // "is" and "isnot"; but not a prefix of a name
3464 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3465 *len = 5;
3466 i = p[*len];
3467 if (!isalnum(i) && i != '_')
3468 {
3469 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3470 *type_is = TRUE;
3471 }
3472 }
3473 break;
3474 }
3475 return type;
3476}
3477
3478/*
3479 * Only comparing strings is supported right now.
3480 * expr5a == expr5b
3481 */
3482 static int
3483evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv)
3484{
3485 exptype_T type = EXPR_UNKNOWN;
3486 char_u *p;
3487 int len = 2;
3488 int type_is = FALSE;
3489
3490 // get the first variable
3491 if (evaluate_const_expr5(arg, cctx, tv) == FAIL)
3492 return FAIL;
3493
3494 p = skipwhite(*arg);
3495 type = get_compare_type(p, &len, &type_is);
3496
3497 /*
3498 * If there is a comparative operator, use it.
3499 */
3500 if (type != EXPR_UNKNOWN)
3501 {
3502 typval_T tv2;
3503 char_u *s1, *s2;
3504 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3505 int n;
3506
3507 // TODO: Only string == string is supported now
3508 if (tv->v_type != VAR_STRING)
3509 return FAIL;
3510 if (type != EXPR_EQUAL)
3511 return FAIL;
3512
3513 // get the second variable
3514 init_tv(&tv2);
3515 *arg = skipwhite(p + len);
3516 if (evaluate_const_expr5(arg, cctx, &tv2) == FAIL
3517 || tv2.v_type != VAR_STRING)
3518 {
3519 clear_tv(&tv2);
3520 return FAIL;
3521 }
3522 s1 = tv_get_string_buf(tv, buf1);
3523 s2 = tv_get_string_buf(&tv2, buf2);
3524 n = STRCMP(s1, s2);
3525 clear_tv(tv);
3526 clear_tv(&tv2);
3527 tv->v_type = VAR_BOOL;
3528 tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE;
3529 }
3530
3531 return OK;
3532}
3533
3534static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv);
3535
3536/*
3537 * Compile constant || or &&.
3538 */
3539 static int
3540evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv)
3541{
3542 char_u *p = skipwhite(*arg);
3543 int opchar = *op;
3544
3545 if (p[0] == opchar && p[1] == opchar)
3546 {
3547 int val = tv2bool(tv);
3548
3549 /*
3550 * Repeat until there is no following "||" or "&&"
3551 */
3552 while (p[0] == opchar && p[1] == opchar)
3553 {
3554 typval_T tv2;
3555
3556 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2]))
3557 return FAIL;
3558
3559 // eval the next expression
3560 *arg = skipwhite(p + 2);
3561 tv2.v_type = VAR_UNKNOWN;
3562 tv2.v_lock = 0;
3563 if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2)
3564 : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL)
3565 {
3566 clear_tv(&tv2);
3567 return FAIL;
3568 }
3569 if ((opchar == '&') == val)
3570 {
3571 // false || tv2 or true && tv2: use tv2
3572 clear_tv(tv);
3573 *tv = tv2;
3574 val = tv2bool(tv);
3575 }
3576 else
3577 clear_tv(&tv2);
3578 p = skipwhite(*arg);
3579 }
3580 }
3581
3582 return OK;
3583}
3584
3585/*
3586 * Evaluate an expression that is a constant: expr4 && expr4 && expr4
3587 * Return FAIL if the expression is not a constant.
3588 */
3589 static int
3590evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv)
3591{
3592 // evaluate the first expression
3593 if (evaluate_const_expr4(arg, cctx, tv) == FAIL)
3594 return FAIL;
3595
3596 // || and && work almost the same
3597 return evaluate_const_and_or(arg, cctx, "&&", tv);
3598}
3599
3600/*
3601 * Evaluate an expression that is a constant: expr3 || expr3 || expr3
3602 * Return FAIL if the expression is not a constant.
3603 */
3604 static int
3605evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv)
3606{
3607 // evaluate the first expression
3608 if (evaluate_const_expr3(arg, cctx, tv) == FAIL)
3609 return FAIL;
3610
3611 // || and && work almost the same
3612 return evaluate_const_and_or(arg, cctx, "||", tv);
3613}
3614
3615/*
3616 * Evaluate an expression that is a constant: expr2 ? expr1 : expr1
3617 * E.g. for "has('feature')".
3618 * This does not produce error messages. "tv" should be cleared afterwards.
3619 * Return FAIL if the expression is not a constant.
3620 */
3621 static int
3622evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv)
3623{
3624 char_u *p;
3625
3626 // evaluate the first expression
3627 if (evaluate_const_expr2(arg, cctx, tv) == FAIL)
3628 return FAIL;
3629
3630 p = skipwhite(*arg);
3631 if (*p == '?')
3632 {
3633 int val = tv2bool(tv);
3634 typval_T tv2;
3635
3636 // require space before and after the ?
3637 if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3638 return FAIL;
3639
3640 // evaluate the second expression; any type is accepted
3641 clear_tv(tv);
3642 *arg = skipwhite(p + 1);
3643 if (evaluate_const_expr1(arg, cctx, tv) == FAIL)
3644 return FAIL;
3645
3646 // Check for the ":".
3647 p = skipwhite(*arg);
3648 if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1]))
3649 return FAIL;
3650
3651 // evaluate the third expression
3652 *arg = skipwhite(p + 1);
3653 tv2.v_type = VAR_UNKNOWN;
3654 if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL)
3655 {
3656 clear_tv(&tv2);
3657 return FAIL;
3658 }
3659 if (val)
3660 {
3661 // use the expr after "?"
3662 clear_tv(&tv2);
3663 }
3664 else
3665 {
3666 // use the expr after ":"
3667 clear_tv(tv);
3668 *tv = tv2;
3669 }
3670 }
3671 return OK;
3672}
3673
3674/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 * Compile code to apply '-', '+' and '!'.
3676 */
3677 static int
3678compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3679{
3680 char_u *p = end;
3681
3682 // this works from end to start
3683 while (p > start)
3684 {
3685 --p;
3686 if (*p == '-' || *p == '+')
3687 {
3688 int negate = *p == '-';
3689 isn_T *isn;
3690
3691 // TODO: check type
3692 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3693 {
3694 --p;
3695 if (*p == '-')
3696 negate = !negate;
3697 }
3698 // only '-' has an effect, for '+' we only check the type
3699 if (negate)
3700 isn = generate_instr(cctx, ISN_NEGATENR);
3701 else
3702 isn = generate_instr(cctx, ISN_CHECKNR);
3703 if (isn == NULL)
3704 return FAIL;
3705 }
3706 else
3707 {
3708 int invert = TRUE;
3709
3710 while (p > start && p[-1] == '!')
3711 {
3712 --p;
3713 invert = !invert;
3714 }
3715 if (generate_2BOOL(cctx, invert) == FAIL)
3716 return FAIL;
3717 }
3718 }
3719 return OK;
3720}
3721
3722/*
3723 * Compile whatever comes after "name" or "name()".
3724 */
3725 static int
3726compile_subscript(
3727 char_u **arg,
3728 cctx_T *cctx,
3729 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003730 char_u *end_leader,
3731 typval_T *bef1_tv,
3732 typval_T *bef2_tv,
3733 typval_T *new_tv)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734{
3735 for (;;)
3736 {
3737 if (**arg == '(')
3738 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003739 garray_T *stack = &cctx->ctx_type_stack;
3740 type_T *type;
3741 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003743 if (generate_tv_PUSH(cctx, bef1_tv) == FAIL
3744 || generate_tv_PUSH(cctx, bef2_tv) == FAIL
3745 || generate_tv_PUSH(cctx, new_tv) == FAIL)
3746 return FAIL;
3747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003749 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3750
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 *arg = skipwhite(*arg + 1);
3752 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3753 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003754 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 return FAIL;
3756 }
3757 else if (**arg == '-' && (*arg)[1] == '>')
3758 {
3759 char_u *p;
3760
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003761 if (generate_tv_PUSH(cctx, bef1_tv) == FAIL
3762 || generate_tv_PUSH(cctx, bef2_tv) == FAIL
3763 || generate_tv_PUSH(cctx, new_tv) == FAIL)
3764 return FAIL;
3765
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 // something->method()
3767 // Apply the '!', '-' and '+' first:
3768 // -1.0->func() works like (-1.0)->func()
3769 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3770 return FAIL;
3771 *start_leader = end_leader; // don't apply again later
3772
3773 *arg = skipwhite(*arg + 2);
3774 if (**arg == '{')
3775 {
3776 // lambda call: list->{lambda}
3777 if (compile_lambda_call(arg, cctx) == FAIL)
3778 return FAIL;
3779 }
3780 else
3781 {
3782 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003783 p = *arg;
3784 if (ASCII_ISALPHA(*p) && p[1] == ':')
3785 p += 2;
3786 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 ;
3788 if (*p != '(')
3789 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003790 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 return FAIL;
3792 }
3793 // TODO: base value may not be the first argument
3794 if (compile_call(arg, p - *arg, cctx, 1) == FAIL)
3795 return FAIL;
3796 }
3797 }
3798 else if (**arg == '[')
3799 {
Bram Moolenaarb13af502020-02-17 21:12:08 +01003800 garray_T *stack;
3801 type_T **typep;
3802
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003803 if (generate_tv_PUSH(cctx, bef1_tv) == FAIL
3804 || generate_tv_PUSH(cctx, bef2_tv) == FAIL
3805 || generate_tv_PUSH(cctx, new_tv) == FAIL)
3806 return FAIL;
3807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 // list index: list[123]
3809 // TODO: more arguments
3810 // TODO: dict member dict['name']
3811 *arg = skipwhite(*arg + 1);
3812 if (compile_expr1(arg, cctx) == FAIL)
3813 return FAIL;
3814
3815 if (**arg != ']')
3816 {
3817 emsg(_(e_missbrac));
3818 return FAIL;
3819 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003820 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821
3822 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3823 return FAIL;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003824 stack = &cctx->ctx_type_stack;
3825 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
3826 if ((*typep)->tt_type != VAR_LIST && *typep != &t_any)
3827 {
3828 emsg(_(e_listreq));
3829 return FAIL;
3830 }
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01003831 if ((*typep)->tt_type == VAR_LIST)
3832 *typep = (*typep)->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 }
3834 else if (**arg == '.' && (*arg)[1] != '.')
3835 {
3836 char_u *p;
3837
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003838 if (generate_tv_PUSH(cctx, bef1_tv) == FAIL
3839 || generate_tv_PUSH(cctx, bef2_tv) == FAIL
3840 || generate_tv_PUSH(cctx, new_tv) == FAIL)
3841 return FAIL;
3842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 ++*arg;
3844 p = *arg;
3845 // dictionary member: dict.name
3846 if (eval_isnamec1(*p))
3847 while (eval_isnamec(*p))
3848 MB_PTR_ADV(p);
3849 if (p == *arg)
3850 {
3851 semsg(_(e_syntax_at), *arg);
3852 return FAIL;
3853 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL)
3855 return FAIL;
3856 *arg = p;
3857 }
3858 else
3859 break;
3860 }
3861
3862 // TODO - see handle_subscript():
3863 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3864 // Don't do this when "Func" is already a partial that was bound
3865 // explicitly (pt_auto is FALSE).
3866
3867 return OK;
3868}
3869
3870/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003871 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3872 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 *
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003874 * If the value is a constant "new_tv" will be set.
3875 * Before instructions are generated, any "bef_tv" will generated.
3876 *
3877 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 */
3879
3880/*
3881 * number number constant
3882 * 0zFFFFFFFF Blob constant
3883 * "string" string constant
3884 * 'string' literal string constant
3885 * &option-name option value
3886 * @r register contents
3887 * identifier variable value
3888 * function() function call
3889 * $VAR environment variable
3890 * (expression) nested expression
3891 * [expr, expr] List
3892 * {key: val, key: val} Dictionary
3893 * #{key: val, key: val} Dictionary with literal keys
3894 *
3895 * Also handle:
3896 * ! in front logical NOT
3897 * - in front unary minus
3898 * + in front unary plus (ignored)
3899 * trailing (arg) funcref/partial call
3900 * trailing [] subscript in String or List
3901 * trailing .name entry in Dictionary
3902 * trailing ->name() method call
3903 */
3904 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003905compile_expr7(
3906 char_u **arg,
3907 cctx_T *cctx,
3908 typval_T *bef1_tv,
3909 typval_T *bef2_tv,
3910 typval_T *new_tv)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911{
3912 typval_T rettv;
3913 char_u *start_leader, *end_leader;
3914 int ret = OK;
3915
3916 /*
3917 * Skip '!', '-' and '+' characters. They are handled later.
3918 */
3919 start_leader = *arg;
3920 while (**arg == '!' || **arg == '-' || **arg == '+')
3921 *arg = skipwhite(*arg + 1);
3922 end_leader = *arg;
3923
3924 rettv.v_type = VAR_UNKNOWN;
3925 switch (**arg)
3926 {
3927 /*
3928 * Number constant.
3929 */
3930 case '0': // also for blob starting with 0z
3931 case '1':
3932 case '2':
3933 case '3':
3934 case '4':
3935 case '5':
3936 case '6':
3937 case '7':
3938 case '8':
3939 case '9':
3940 case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL)
3941 return FAIL;
3942 break;
3943
3944 /*
3945 * String constant: "string".
3946 */
3947 case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL)
3948 return FAIL;
3949 break;
3950
3951 /*
3952 * Literal string constant: 'str''ing'.
3953 */
3954 case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL)
3955 return FAIL;
3956 break;
3957
3958 /*
3959 * Constant Vim variable.
3960 */
3961 case 'v': get_vim_constant(arg, &rettv);
3962 ret = NOTDONE;
3963 break;
3964
3965 /*
3966 * List: [expr, expr]
3967 */
3968 case '[': ret = compile_list(arg, cctx);
3969 break;
3970
3971 /*
3972 * Dictionary: #{key: val, key: val}
3973 */
3974 case '#': if ((*arg)[1] == '{')
3975 {
3976 ++*arg;
3977 ret = compile_dict(arg, cctx, TRUE);
3978 }
3979 else
3980 ret = NOTDONE;
3981 break;
3982
3983 /*
3984 * Lambda: {arg, arg -> expr}
3985 * Dictionary: {'key': val, 'key': val}
3986 */
3987 case '{': {
3988 char_u *start = skipwhite(*arg + 1);
3989
3990 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003991 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003993 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 if (ret != FAIL && *start == '>')
3995 ret = compile_lambda(arg, cctx);
3996 else
3997 ret = compile_dict(arg, cctx, FALSE);
3998 }
3999 break;
4000
4001 /*
4002 * Option value: &name
4003 */
4004 case '&': ret = compile_get_option(arg, cctx);
4005 break;
4006
4007 /*
4008 * Environment variable: $VAR.
4009 */
4010 case '$': ret = compile_get_env(arg, cctx);
4011 break;
4012
4013 /*
4014 * Register contents: @r.
4015 */
4016 case '@': ret = compile_get_register(arg, cctx);
4017 break;
4018 /*
4019 * nested expression: (expression).
4020 */
4021 case '(': *arg = skipwhite(*arg + 1);
4022 ret = compile_expr1(arg, cctx); // recursive!
4023 *arg = skipwhite(*arg);
4024 if (**arg == ')')
4025 ++*arg;
4026 else if (ret == OK)
4027 {
4028 emsg(_(e_missing_close));
4029 ret = FAIL;
4030 }
4031 break;
4032
4033 default: ret = NOTDONE;
4034 break;
4035 }
4036 if (ret == FAIL)
4037 return FAIL;
4038
4039 if (rettv.v_type != VAR_UNKNOWN)
4040 {
4041 // apply the '!', '-' and '+' before the constant
4042 if (apply_leader(&rettv, start_leader, end_leader) == FAIL)
4043 {
4044 clear_tv(&rettv);
4045 return FAIL;
4046 }
4047 start_leader = end_leader; // don't apply again below
4048
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004049 // A constant expression can possibly be handled compile time.
4050 *new_tv = rettv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 }
4052 else if (ret == NOTDONE)
4053 {
4054 char_u *p;
4055 int r;
4056
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004057 if (generate_tv_PUSH(cctx, bef1_tv) == FAIL
4058 || generate_tv_PUSH(cctx, bef2_tv) == FAIL)
4059 return FAIL;
4060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 if (!eval_isnamec1(**arg))
4062 {
4063 semsg(_("E1015: Name expected: %s"), *arg);
4064 return FAIL;
4065 }
4066
4067 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004068 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 if (*p == '(')
4070 r = compile_call(arg, p - *arg, cctx, 0);
4071 else
4072 r = compile_load(arg, p, cctx, TRUE);
4073 if (r == FAIL)
4074 return FAIL;
4075 }
4076
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004077 // Handle following "[]", ".member", etc.
4078 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004079 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004080 bef1_tv, bef2_tv, new_tv) == FAIL
4081 || compile_leader(cctx, start_leader, end_leader) == FAIL)
4082 {
4083 clear_tv(new_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004085 }
4086 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087}
4088
4089/*
4090 * * number multiplication
4091 * / number division
4092 * % number modulo
4093 */
4094 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004095compile_expr6(
4096 char_u **arg,
4097 cctx_T *cctx,
4098 typval_T *bef_tv,
4099 typval_T *new_tv)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100{
4101 char_u *op;
4102
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004103 // get the first expression
4104 if (compile_expr7(arg, cctx, NULL, bef_tv, new_tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 return FAIL;
4106
4107 /*
4108 * Repeat computing, until no "*", "/" or "%" is following.
4109 */
4110 for (;;)
4111 {
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004112 typval_T tv2;
4113
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 op = skipwhite(*arg);
4115 if (*op != '*' && *op != '/' && *op != '%')
4116 break;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004117
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004118 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 {
4120 char_u buf[3];
4121
4122 vim_strncpy(buf, op, 1);
4123 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004124 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 }
4126 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004127 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004128 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004130 // get the second expression
4131 tv2.v_type = VAR_UNKNOWN;
4132 if (compile_expr7(arg, cctx, bef_tv, new_tv, &tv2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 return FAIL;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004134 if (new_tv->v_type == VAR_NUMBER && tv2.v_type == VAR_NUMBER)
4135 {
4136 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004138 // both are numbers: compute the result
4139 switch (*op)
4140 {
4141 case '*': res = new_tv->vval.v_number * tv2.vval.v_number;
4142 break;
4143 case '/': res = new_tv->vval.v_number / tv2.vval.v_number;
4144 break;
4145 case '%': res = new_tv->vval.v_number % tv2.vval.v_number;
4146 break;
4147 }
4148 new_tv->vval.v_number = res;
4149 }
4150 else
4151 {
4152 generate_tv_PUSH(cctx, new_tv);
4153 generate_tv_PUSH(cctx, &tv2);
4154 generate_two_op(cctx, op);
4155 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 }
4157
4158 return OK;
4159}
4160
4161/*
4162 * + number addition
4163 * - number subtraction
4164 * .. string concatenation
4165 */
4166 static int
4167compile_expr5(char_u **arg, cctx_T *cctx)
4168{
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004169 typval_T tv1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 char_u *op;
4171 int oplen;
4172
4173 // get the first variable
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004174 tv1.v_type = VAR_UNKNOWN;
4175 if (compile_expr6(arg, cctx, NULL, &tv1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 return FAIL;
4177
4178 /*
4179 * Repeat computing, until no "+", "-" or ".." is following.
4180 */
4181 for (;;)
4182 {
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004183 typval_T tv2;
4184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 op = skipwhite(*arg);
4186 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
4187 break;
4188 oplen = (*op == '.' ? 2 : 1);
4189
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004190 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 {
4192 char_u buf[3];
4193
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004194 clear_tv(&tv1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195 vim_strncpy(buf, op, oplen);
4196 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004197 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 }
4199
4200 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004201 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004202 {
4203 clear_tv(&tv1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004204 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004205 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004207 // get the second expression
4208 tv2.v_type = VAR_UNKNOWN;
4209 if (compile_expr6(arg, cctx, &tv1, &tv2) == FAIL)
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004210 {
4211 clear_tv(&tv1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004215 if (*op == '+' && tv1.v_type == VAR_NUMBER && tv2.v_type == VAR_NUMBER)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 {
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004217 // add constant numbers
4218 tv1.vval.v_number = tv1.vval.v_number + tv2.vval.v_number;
4219 }
4220 else if (*op == '-' && tv1.v_type == VAR_NUMBER
4221 && tv2.v_type == VAR_NUMBER)
4222 {
4223 // subtract constant numbers
4224 tv1.vval.v_number = tv1.vval.v_number - tv2.vval.v_number;
4225 }
4226 else if (*op == '.' && tv1.v_type == VAR_STRING
4227 && tv2.v_type == VAR_STRING)
4228 {
4229 // concatenate constant strings
4230 char_u *s1 = tv1.vval.v_string;
4231 char_u *s2 = tv2.vval.v_string;
4232 size_t len1 = STRLEN(s1);
4233
4234 tv1.vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4235 if (tv1.vval.v_string == NULL)
4236 {
4237 vim_free(s1);
4238 vim_free(s2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239 return FAIL;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004240 }
4241 mch_memmove(tv1.vval.v_string, s1, len1);
4242 STRCPY(tv1.vval.v_string + len1, s2);
Bram Moolenaarcca34aa2020-05-07 22:23:58 +02004243 vim_free(s1);
4244 vim_free(s2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 }
4246 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004247 {
4248 generate_tv_PUSH(cctx, &tv1);
4249 generate_tv_PUSH(cctx, &tv2);
4250 if (*op == '.')
4251 {
4252 if (may_generate_2STRING(-2, cctx) == FAIL
4253 || may_generate_2STRING(-1, cctx) == FAIL)
4254 return FAIL;
4255 generate_instr_drop(cctx, ISN_CONCAT, 1);
4256 }
4257 else
4258 generate_two_op(cctx, op);
4259 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 }
4261
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004262 // TODO: move to caller
4263 generate_tv_PUSH(cctx, &tv1);
4264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 return OK;
4266}
4267
4268/*
4269 * expr5a == expr5b
4270 * expr5a =~ expr5b
4271 * expr5a != expr5b
4272 * expr5a !~ expr5b
4273 * expr5a > expr5b
4274 * expr5a >= expr5b
4275 * expr5a < expr5b
4276 * expr5a <= expr5b
4277 * expr5a is expr5b
4278 * expr5a isnot expr5b
4279 *
4280 * Produces instructions:
4281 * EVAL expr5a Push result of "expr5a"
4282 * EVAL expr5b Push result of "expr5b"
4283 * COMPARE one of the compare instructions
4284 */
4285 static int
4286compile_expr4(char_u **arg, cctx_T *cctx)
4287{
4288 exptype_T type = EXPR_UNKNOWN;
4289 char_u *p;
4290 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291 int type_is = FALSE;
4292
4293 // get the first variable
4294 if (compile_expr5(arg, cctx) == FAIL)
4295 return FAIL;
4296
4297 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004298 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299
4300 /*
4301 * If there is a comparative operator, use it.
4302 */
4303 if (type != EXPR_UNKNOWN)
4304 {
4305 int ic = FALSE; // Default: do not ignore case
4306
4307 if (type_is && (p[len] == '?' || p[len] == '#'))
4308 {
4309 semsg(_(e_invexpr2), *arg);
4310 return FAIL;
4311 }
4312 // extra question mark appended: ignore case
4313 if (p[len] == '?')
4314 {
4315 ic = TRUE;
4316 ++len;
4317 }
4318 // extra '#' appended: match case (ignored)
4319 else if (p[len] == '#')
4320 ++len;
4321 // nothing appended: match case
4322
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004323 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 {
4325 char_u buf[7];
4326
4327 vim_strncpy(buf, p, len);
4328 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004329 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 }
4331
4332 // get the second variable
4333 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004334 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004335 return FAIL;
4336
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337 if (compile_expr5(arg, cctx) == FAIL)
4338 return FAIL;
4339
4340 generate_COMPARE(cctx, type, ic);
4341 }
4342
4343 return OK;
4344}
4345
4346/*
4347 * Compile || or &&.
4348 */
4349 static int
4350compile_and_or(char_u **arg, cctx_T *cctx, char *op)
4351{
4352 char_u *p = skipwhite(*arg);
4353 int opchar = *op;
4354
4355 if (p[0] == opchar && p[1] == opchar)
4356 {
4357 garray_T *instr = &cctx->ctx_instr;
4358 garray_T end_ga;
4359
4360 /*
4361 * Repeat until there is no following "||" or "&&"
4362 */
4363 ga_init2(&end_ga, sizeof(int), 10);
4364 while (p[0] == opchar && p[1] == opchar)
4365 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004366 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4367 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004369 return FAIL;
4370 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371
4372 if (ga_grow(&end_ga, 1) == FAIL)
4373 {
4374 ga_clear(&end_ga);
4375 return FAIL;
4376 }
4377 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4378 ++end_ga.ga_len;
4379 generate_JUMP(cctx, opchar == '|'
4380 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4381
4382 // eval the next expression
4383 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004384 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004385 return FAIL;
4386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 if ((opchar == '|' ? compile_expr3(arg, cctx)
4388 : compile_expr4(arg, cctx)) == FAIL)
4389 {
4390 ga_clear(&end_ga);
4391 return FAIL;
4392 }
4393 p = skipwhite(*arg);
4394 }
4395
4396 // Fill in the end label in all jumps.
4397 while (end_ga.ga_len > 0)
4398 {
4399 isn_T *isn;
4400
4401 --end_ga.ga_len;
4402 isn = ((isn_T *)instr->ga_data)
4403 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4404 isn->isn_arg.jump.jump_where = instr->ga_len;
4405 }
4406 ga_clear(&end_ga);
4407 }
4408
4409 return OK;
4410}
4411
4412/*
4413 * expr4a && expr4a && expr4a logical AND
4414 *
4415 * Produces instructions:
4416 * EVAL expr4a Push result of "expr4a"
4417 * JUMP_AND_KEEP_IF_FALSE end
4418 * EVAL expr4b Push result of "expr4b"
4419 * JUMP_AND_KEEP_IF_FALSE end
4420 * EVAL expr4c Push result of "expr4c"
4421 * end:
4422 */
4423 static int
4424compile_expr3(char_u **arg, cctx_T *cctx)
4425{
4426 // get the first variable
4427 if (compile_expr4(arg, cctx) == FAIL)
4428 return FAIL;
4429
4430 // || and && work almost the same
4431 return compile_and_or(arg, cctx, "&&");
4432}
4433
4434/*
4435 * expr3a || expr3b || expr3c logical OR
4436 *
4437 * Produces instructions:
4438 * EVAL expr3a Push result of "expr3a"
4439 * JUMP_AND_KEEP_IF_TRUE end
4440 * EVAL expr3b Push result of "expr3b"
4441 * JUMP_AND_KEEP_IF_TRUE end
4442 * EVAL expr3c Push result of "expr3c"
4443 * end:
4444 */
4445 static int
4446compile_expr2(char_u **arg, cctx_T *cctx)
4447{
4448 // eval the first expression
4449 if (compile_expr3(arg, cctx) == FAIL)
4450 return FAIL;
4451
4452 // || and && work almost the same
4453 return compile_and_or(arg, cctx, "||");
4454}
4455
4456/*
4457 * Toplevel expression: expr2 ? expr1a : expr1b
4458 *
4459 * Produces instructions:
4460 * EVAL expr2 Push result of "expr"
4461 * JUMP_IF_FALSE alt jump if false
4462 * EVAL expr1a
4463 * JUMP_ALWAYS end
4464 * alt: EVAL expr1b
4465 * end:
4466 */
4467 static int
4468compile_expr1(char_u **arg, cctx_T *cctx)
4469{
4470 char_u *p;
4471
Bram Moolenaar61a89812020-05-07 16:58:17 +02004472 // Evaluate the first expression.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004473 if (compile_expr2(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474 return FAIL;
4475
4476 p = skipwhite(*arg);
4477 if (*p == '?')
4478 {
4479 garray_T *instr = &cctx->ctx_instr;
4480 garray_T *stack = &cctx->ctx_type_stack;
4481 int alt_idx = instr->ga_len;
4482 int end_idx;
4483 isn_T *isn;
4484 type_T *type1;
4485 type_T *type2;
4486
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004487 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4488 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004490 return FAIL;
4491 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492
4493 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4494
4495 // evaluate the second expression; any type is accepted
4496 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004497 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004498 return FAIL;
4499
Bram Moolenaara6d53682020-01-28 23:04:06 +01004500 if (compile_expr1(arg, cctx) == FAIL)
4501 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502
4503 // remember the type and drop it
4504 --stack->ga_len;
4505 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
4506
4507 end_idx = instr->ga_len;
4508 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4509
4510 // jump here from JUMP_IF_FALSE
4511 isn = ((isn_T *)instr->ga_data) + alt_idx;
4512 isn->isn_arg.jump.jump_where = instr->ga_len;
4513
4514 // Check for the ":".
4515 p = skipwhite(*arg);
4516 if (*p != ':')
4517 {
4518 emsg(_(e_missing_colon));
4519 return FAIL;
4520 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004521 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4522 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004524 return FAIL;
4525 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004526
4527 // evaluate the third expression
4528 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004529 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004530 return FAIL;
4531
Bram Moolenaara6d53682020-01-28 23:04:06 +01004532 if (compile_expr1(arg, cctx) == FAIL)
4533 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004534
4535 // If the types differ, the result has a more generic type.
4536 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004537 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538
4539 // jump here from JUMP_ALWAYS
4540 isn = ((isn_T *)instr->ga_data) + end_idx;
4541 isn->isn_arg.jump.jump_where = instr->ga_len;
4542 }
4543 return OK;
4544}
4545
4546/*
4547 * compile "return [expr]"
4548 */
4549 static char_u *
4550compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4551{
4552 char_u *p = arg;
4553 garray_T *stack = &cctx->ctx_type_stack;
4554 type_T *stack_type;
4555
4556 if (*p != NUL && *p != '|' && *p != '\n')
4557 {
4558 // compile return argument into instructions
4559 if (compile_expr1(&p, cctx) == FAIL)
4560 return NULL;
4561
4562 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4563 if (set_return_type)
4564 cctx->ctx_ufunc->uf_ret_type = stack_type;
4565 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4566 == FAIL)
4567 return NULL;
4568 }
4569 else
4570 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004571 // "set_return_type" cannot be TRUE, only used for a lambda which
4572 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004573 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4574 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 {
4576 emsg(_("E1003: Missing return value"));
4577 return NULL;
4578 }
4579
4580 // No argument, return zero.
4581 generate_PUSHNR(cctx, 0);
4582 }
4583
4584 if (generate_instr(cctx, ISN_RETURN) == NULL)
4585 return NULL;
4586
4587 // "return val | endif" is possible
4588 return skipwhite(p);
4589}
4590
4591/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004592 * Get a line from the compilation context, compatible with exarg_T getline().
4593 * Return a pointer to the line in allocated memory.
4594 * Return NULL for end-of-file or some error.
4595 */
4596 static char_u *
4597exarg_getline(
4598 int c UNUSED,
4599 void *cookie,
4600 int indent UNUSED,
4601 int do_concat UNUSED)
4602{
4603 cctx_T *cctx = (cctx_T *)cookie;
4604
4605 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4606 {
4607 iemsg("Heredoc got to end");
4608 return NULL;
4609 }
4610 ++cctx->ctx_lnum;
4611 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4612 [cctx->ctx_lnum]);
4613}
4614
4615/*
4616 * Compile a nested :def command.
4617 */
4618 static char_u *
4619compile_nested_function(exarg_T *eap, cctx_T *cctx)
4620{
4621 char_u *name_start = eap->arg;
4622 char_u *name_end = to_name_end(eap->arg, FALSE);
4623 char_u *name = get_lambda_name();
4624 lvar_T *lvar;
4625 ufunc_T *ufunc;
4626
4627 eap->arg = name_end;
4628 eap->getline = exarg_getline;
4629 eap->cookie = cctx;
4630 eap->skip = cctx->ctx_skip == TRUE;
4631 eap->forceit = FALSE;
4632 ufunc = def_function(eap, name, cctx);
4633
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004634 if (ufunc == NULL || ufunc->uf_dfunc_idx < 0)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004635 return NULL;
4636
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004637 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004638 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004639 TRUE, ufunc->uf_func_type);
4640
4641 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4642 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4643 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004644
Bram Moolenaar61a89812020-05-07 16:58:17 +02004645 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004646 return (char_u *)"";
4647}
4648
4649/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650 * Return the length of an assignment operator, or zero if there isn't one.
4651 */
4652 int
4653assignment_len(char_u *p, int *heredoc)
4654{
4655 if (*p == '=')
4656 {
4657 if (p[1] == '<' && p[2] == '<')
4658 {
4659 *heredoc = TRUE;
4660 return 3;
4661 }
4662 return 1;
4663 }
4664 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4665 return 2;
4666 if (STRNCMP(p, "..=", 3) == 0)
4667 return 3;
4668 return 0;
4669}
4670
4671// words that cannot be used as a variable
4672static char *reserved[] = {
4673 "true",
4674 "false",
4675 NULL
4676};
4677
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004678typedef enum {
4679 dest_local,
4680 dest_option,
4681 dest_env,
4682 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004683 dest_buffer,
4684 dest_window,
4685 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004686 dest_vimvar,
4687 dest_script,
4688 dest_reg,
4689} assign_dest_T;
4690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691/*
4692 * compile "let var [= expr]", "const var = expr" and "var = expr"
4693 * "arg" points to "var".
4694 */
4695 static char_u *
4696compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4697{
4698 char_u *p;
4699 char_u *ret = NULL;
4700 int var_count = 0;
4701 int semicolon = 0;
4702 size_t varlen;
4703 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004704 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004707 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004709 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 int oplen = 0;
4711 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004712 type_T *type = &t_any;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004713 lvar_T *lvar = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714 char_u *name;
4715 char_u *sp;
4716 int has_type = FALSE;
4717 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
4718 int instr_count = -1;
4719
4720 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
4721 if (p == NULL)
4722 return NULL;
4723 if (var_count > 0)
4724 {
4725 // TODO: let [var, var] = list
4726 emsg("Cannot handle a list yet");
4727 return NULL;
4728 }
4729
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004730 // "a: type" is declaring variable "a" with a type, not "a:".
4731 if (is_decl && p == arg + 2 && p[-1] == ':')
4732 --p;
4733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734 varlen = p - arg;
4735 name = vim_strnsave(arg, (int)varlen);
4736 if (name == NULL)
4737 return NULL;
4738
Bram Moolenaar080457c2020-03-03 21:53:32 +01004739 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004741 if (*arg == '&')
4742 {
4743 int cc;
4744 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745
Bram Moolenaar080457c2020-03-03 21:53:32 +01004746 dest = dest_option;
4747 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004749 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004750 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004751 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004752 if (is_decl)
4753 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004754 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755 goto theend;
4756 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004757 p = arg;
4758 p = find_option_end(&p, &opt_flags);
4759 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004760 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004761 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01004762 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004763 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004764 }
4765 cc = *p;
4766 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004767 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004768 *p = cc;
4769 if (opt_type == -3)
4770 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004771 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004772 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004773 }
4774 if (opt_type == -2 || opt_type == 0)
4775 type = &t_string;
4776 else
4777 type = &t_number; // both number and boolean option
4778 }
4779 else if (*arg == '$')
4780 {
4781 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004782 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004783 if (is_decl)
4784 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004785 semsg(_("E1065: Cannot declare an environment variable: %s"),
4786 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004787 goto theend;
4788 }
4789 }
4790 else if (*arg == '@')
4791 {
4792 if (!valid_yank_reg(arg[1], TRUE))
4793 {
4794 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004795 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004796 }
4797 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004798 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004799 if (is_decl)
4800 {
4801 semsg(_("E1066: Cannot declare a register: %s"), name);
4802 goto theend;
4803 }
4804 }
4805 else if (STRNCMP(arg, "g:", 2) == 0)
4806 {
4807 dest = dest_global;
4808 if (is_decl)
4809 {
4810 semsg(_("E1016: Cannot declare a global variable: %s"), name);
4811 goto theend;
4812 }
4813 }
Bram Moolenaard3aac292020-04-19 14:32:17 +02004814 else if (STRNCMP(arg, "b:", 2) == 0)
4815 {
4816 dest = dest_buffer;
4817 if (is_decl)
4818 {
4819 semsg(_("E1078: Cannot declare a buffer variable: %s"), name);
4820 goto theend;
4821 }
4822 }
4823 else if (STRNCMP(arg, "w:", 2) == 0)
4824 {
4825 dest = dest_window;
4826 if (is_decl)
4827 {
4828 semsg(_("E1079: Cannot declare a window variable: %s"), name);
4829 goto theend;
4830 }
4831 }
4832 else if (STRNCMP(arg, "t:", 2) == 0)
4833 {
4834 dest = dest_tab;
4835 if (is_decl)
4836 {
4837 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
4838 goto theend;
4839 }
4840 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004841 else if (STRNCMP(arg, "v:", 2) == 0)
4842 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004843 typval_T *vtv;
4844 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004845
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004846 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004847 if (vimvaridx < 0)
4848 {
4849 semsg(_(e_var_notfound), arg);
4850 goto theend;
4851 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004852 // We use the current value of "sandbox" here, is that OK?
4853 if (var_check_ro(di_flags, name, FALSE))
4854 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004855 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004856 vtv = get_vim_var_tv(vimvaridx);
4857 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004858 if (is_decl)
4859 {
4860 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
4861 goto theend;
4862 }
4863 }
4864 else
4865 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004866 int idx;
4867
Bram Moolenaar080457c2020-03-03 21:53:32 +01004868 for (idx = 0; reserved[idx] != NULL; ++idx)
4869 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004871 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 goto theend;
4873 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004874
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004875 lvar = lookup_local(arg, varlen, cctx);
4876 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004878 if (is_decl)
4879 {
4880 semsg(_("E1017: Variable already declared: %s"), name);
4881 goto theend;
4882 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004883 else if (lvar->lv_const)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004884 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004885 semsg(_("E1018: Cannot assign to a constant: %s"), name);
4886 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004887 }
4888 }
4889 else if (STRNCMP(arg, "s:", 2) == 0
4890 || lookup_script(arg, varlen) == OK
4891 || find_imported(arg, varlen, cctx) != NULL)
4892 {
4893 dest = dest_script;
4894 if (is_decl)
4895 {
4896 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004897 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004898 goto theend;
4899 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004901 else if (name[1] == ':' && name[2] != NUL)
4902 {
4903 semsg(_("E1082: Cannot use a namespaced variable: %s"), name);
4904 goto theend;
4905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906 }
4907 }
4908
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004909 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910 {
4911 if (is_decl && *p == ':')
4912 {
4913 // parse optional type: "let var: type = expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004914 if (!VIM_ISWHITE(p[1]))
4915 {
4916 semsg(_(e_white_after), ":");
4917 goto theend;
4918 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004919 p = skipwhite(p + 1);
4920 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004921 has_type = TRUE;
4922 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004923 else if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004924 type = lvar->lv_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 }
4926
4927 sp = p;
4928 p = skipwhite(p);
4929 op = p;
4930 oplen = assignment_len(p, &heredoc);
4931 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4932 {
4933 char_u buf[4];
4934
4935 vim_strncpy(buf, op, oplen);
4936 semsg(_(e_white_both), buf);
4937 }
4938
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004939 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02004940 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004942 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 goto theend;
4944 }
4945
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004946 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947 {
4948 if (oplen > 1 && !heredoc)
4949 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004950 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4952 name);
4953 goto theend;
4954 }
4955
4956 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02004957 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004958 goto theend;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004959 lvar = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4960 if (lvar == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004962 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 }
4964
4965 if (heredoc)
4966 {
4967 list_T *l;
4968 listitem_T *li;
4969
4970 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004971 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004973 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974
4975 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004976 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977 {
4978 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4979 li->li_tv.vval.v_string = NULL;
4980 }
4981 generate_NEWLIST(cctx, l->lv_len);
4982 type = &t_list_string;
4983 list_free(l);
4984 p += STRLEN(p);
4985 }
4986 else if (oplen > 0)
4987 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004988 int r;
4989 type_T *stacktype;
4990 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992 // for "+=", "*=", "..=" etc. first load the current value
4993 if (*op != '=')
4994 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004995 switch (dest)
4996 {
4997 case dest_option:
4998 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004999 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005000 break;
5001 case dest_global:
5002 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5003 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02005004 case dest_buffer:
5005 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5006 break;
5007 case dest_window:
5008 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5009 break;
5010 case dest_tab:
5011 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5012 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005013 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01005014 compile_load_scriptvar(cctx,
5015 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005016 break;
5017 case dest_env:
5018 // Include $ in the name here
5019 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5020 break;
5021 case dest_reg:
5022 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
5023 break;
5024 case dest_vimvar:
5025 generate_LOADV(cctx, name + 2, TRUE);
5026 break;
5027 case dest_local:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005028 if (lvar->lv_from_outer)
5029 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5030 NULL, type);
5031 else
5032 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005033 break;
5034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035 }
5036
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005037 // Compile the expression. Temporarily hide the new local variable
5038 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02005039 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005040 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005041 instr_count = instr->ga_len;
5042 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005043 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02005044 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005045 ++cctx->ctx_locals.ga_len;
5046 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005047 goto theend;
5048
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005049 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005050 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005051 stack = &cctx->ctx_type_stack;
5052 stacktype = stack->ga_len == 0 ? &t_void
5053 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005054 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005055 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005056 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005057 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005058 if (stacktype->tt_type == VAR_VOID)
5059 {
5060 emsg(_("E1031: Cannot use void value"));
5061 goto theend;
5062 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005063 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005064 {
5065 // An empty list or dict has a &t_void member, for a
5066 // variable that implies &t_any.
5067 if (stacktype == &t_list_empty)
5068 lvar->lv_type = &t_list_any;
5069 else if (stacktype == &t_dict_empty)
5070 lvar->lv_type = &t_dict_any;
5071 else
5072 lvar->lv_type = stacktype;
5073 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005074 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005075 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
5076 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005078 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005079 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005080 }
5081 }
5082 else if (cmdidx == CMD_const)
5083 {
5084 emsg(_("E1021: const requires a value"));
5085 goto theend;
5086 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005087 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005088 {
5089 emsg(_("E1022: type or initialization required"));
5090 goto theend;
5091 }
5092 else
5093 {
5094 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095 if (ga_grow(instr, 1) == FAIL)
5096 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005097 switch (type->tt_type)
5098 {
5099 case VAR_BOOL:
5100 generate_PUSHBOOL(cctx, VVAL_FALSE);
5101 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005102 case VAR_FLOAT:
5103#ifdef FEAT_FLOAT
5104 generate_PUSHF(cctx, 0.0);
5105#endif
5106 break;
5107 case VAR_STRING:
5108 generate_PUSHS(cctx, NULL);
5109 break;
5110 case VAR_BLOB:
5111 generate_PUSHBLOB(cctx, NULL);
5112 break;
5113 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005114 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005115 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005116 case VAR_LIST:
5117 generate_NEWLIST(cctx, 0);
5118 break;
5119 case VAR_DICT:
5120 generate_NEWDICT(cctx, 0);
5121 break;
5122 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005123 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005124 break;
5125 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005126 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005127 break;
5128 case VAR_NUMBER:
5129 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005130 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02005131 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01005132 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02005133 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01005134 generate_PUSHNR(cctx, 0);
5135 break;
5136 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137 }
5138
5139 if (oplen > 0 && *op != '=')
5140 {
5141 type_T *expected = &t_number;
5142 garray_T *stack = &cctx->ctx_type_stack;
5143 type_T *stacktype;
5144
5145 // TODO: if type is known use float or any operation
5146
5147 if (*op == '.')
5148 expected = &t_string;
5149 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5150 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5151 goto theend;
5152
5153 if (*op == '.')
5154 generate_instr_drop(cctx, ISN_CONCAT, 1);
5155 else
5156 {
5157 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5158
5159 if (isn == NULL)
5160 goto theend;
5161 switch (*op)
5162 {
5163 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5164 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5165 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5166 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5167 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5168 }
5169 }
5170 }
5171
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005172 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005174 case dest_option:
5175 generate_STOREOPT(cctx, name + 1, opt_flags);
5176 break;
5177 case dest_global:
5178 // include g: with the name, easier to execute that way
5179 generate_STORE(cctx, ISN_STOREG, 0, name);
5180 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02005181 case dest_buffer:
5182 // include b: with the name, easier to execute that way
5183 generate_STORE(cctx, ISN_STOREB, 0, name);
5184 break;
5185 case dest_window:
5186 // include w: with the name, easier to execute that way
5187 generate_STORE(cctx, ISN_STOREW, 0, name);
5188 break;
5189 case dest_tab:
5190 // include t: with the name, easier to execute that way
5191 generate_STORE(cctx, ISN_STORET, 0, name);
5192 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005193 case dest_env:
5194 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5195 break;
5196 case dest_reg:
5197 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5198 break;
5199 case dest_vimvar:
5200 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5201 break;
5202 case dest_script:
5203 {
5204 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5205 imported_T *import = NULL;
5206 int sid = current_sctx.sc_sid;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005207 int idx;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005208
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005209 if (name[1] != ':')
5210 {
5211 import = find_imported(name, 0, cctx);
5212 if (import != NULL)
5213 sid = import->imp_sid;
5214 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005215
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005216 idx = get_script_item_idx(sid, rawname, TRUE);
5217 // TODO: specific type
5218 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005219 {
5220 char_u *name_s = name;
5221
5222 // Include s: in the name for store_var()
5223 if (name[1] != ':')
5224 {
5225 int len = (int)STRLEN(name) + 3;
5226
5227 name_s = alloc(len);
5228 if (name_s == NULL)
5229 name_s = name;
5230 else
5231 vim_snprintf((char *)name_s, len, "s:%s", name);
5232 }
5233 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
5234 if (name_s != name)
5235 vim_free(name_s);
5236 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005237 else
5238 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5239 sid, idx, &t_any);
5240 }
5241 break;
5242 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005243 if (lvar != NULL)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005244 {
5245 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005246
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005247 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
5248 // into ISN_STORENR
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005249 if (!lvar->lv_from_outer && instr->ga_len == instr_count + 1
5250 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005251 {
5252 varnumber_T val = isn->isn_arg.number;
5253 garray_T *stack = &cctx->ctx_type_stack;
5254
5255 isn->isn_type = ISN_STORENR;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005256 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
Bram Moolenaara471eea2020-03-04 22:20:26 +01005257 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005258 if (stack->ga_len > 0)
5259 --stack->ga_len;
5260 }
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005261 else if (lvar->lv_from_outer)
5262 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005263 else
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005264 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005265 }
5266 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005267 }
5268 ret = p;
5269
5270theend:
5271 vim_free(name);
5272 return ret;
5273}
5274
5275/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005276 * Check if "name" can be "unlet".
5277 */
5278 int
5279check_vim9_unlet(char_u *name)
5280{
5281 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5282 {
5283 semsg(_("E1081: Cannot unlet %s"), name);
5284 return FAIL;
5285 }
5286 return OK;
5287}
5288
5289/*
5290 * Callback passed to ex_unletlock().
5291 */
5292 static int
5293compile_unlet(
5294 lval_T *lvp,
5295 char_u *name_end,
5296 exarg_T *eap,
5297 int deep UNUSED,
5298 void *coookie)
5299{
5300 cctx_T *cctx = coookie;
5301
5302 if (lvp->ll_tv == NULL)
5303 {
5304 char_u *p = lvp->ll_name;
5305 int cc = *name_end;
5306 int ret = OK;
5307
5308 // Normal name. Only supports g:, w:, t: and b: namespaces.
5309 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005310 if (*p == '$')
5311 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5312 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005313 ret = FAIL;
5314 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005315 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005316
5317 *name_end = cc;
5318 return ret;
5319 }
5320
5321 // TODO: unlet {list}[idx]
5322 // TODO: unlet {dict}[key]
5323 emsg("Sorry, :unlet not fully implemented yet");
5324 return FAIL;
5325}
5326
5327/*
5328 * compile "unlet var", "lock var" and "unlock var"
5329 * "arg" points to "var".
5330 */
5331 static char_u *
5332compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5333{
5334 char_u *p = arg;
5335
5336 if (eap->cmdidx != CMD_unlet)
5337 {
5338 emsg("Sorry, :lock and unlock not implemented yet");
5339 return NULL;
5340 }
5341
5342 if (*p == '!')
5343 {
5344 p = skipwhite(p + 1);
5345 eap->forceit = TRUE;
5346 }
5347
5348 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5349 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5350}
5351
5352/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005353 * Compile an :import command.
5354 */
5355 static char_u *
5356compile_import(char_u *arg, cctx_T *cctx)
5357{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01005358 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005359}
5360
5361/*
5362 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5363 */
5364 static int
5365compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5366{
5367 garray_T *instr = &cctx->ctx_instr;
5368 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5369
5370 if (endlabel == NULL)
5371 return FAIL;
5372 endlabel->el_next = *el;
5373 *el = endlabel;
5374 endlabel->el_end_label = instr->ga_len;
5375
5376 generate_JUMP(cctx, when, 0);
5377 return OK;
5378}
5379
5380 static void
5381compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5382{
5383 garray_T *instr = &cctx->ctx_instr;
5384
5385 while (*el != NULL)
5386 {
5387 endlabel_T *cur = (*el);
5388 isn_T *isn;
5389
5390 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5391 isn->isn_arg.jump.jump_where = instr->ga_len;
5392 *el = cur->el_next;
5393 vim_free(cur);
5394 }
5395}
5396
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005397 static void
5398compile_free_jump_to_end(endlabel_T **el)
5399{
5400 while (*el != NULL)
5401 {
5402 endlabel_T *cur = (*el);
5403
5404 *el = cur->el_next;
5405 vim_free(cur);
5406 }
5407}
5408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005409/*
5410 * Create a new scope and set up the generic items.
5411 */
5412 static scope_T *
5413new_scope(cctx_T *cctx, scopetype_T type)
5414{
5415 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5416
5417 if (scope == NULL)
5418 return NULL;
5419 scope->se_outer = cctx->ctx_scope;
5420 cctx->ctx_scope = scope;
5421 scope->se_type = type;
5422 scope->se_local_count = cctx->ctx_locals.ga_len;
5423 return scope;
5424}
5425
5426/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005427 * Free the current scope and go back to the outer scope.
5428 */
5429 static void
5430drop_scope(cctx_T *cctx)
5431{
5432 scope_T *scope = cctx->ctx_scope;
5433
5434 if (scope == NULL)
5435 {
5436 iemsg("calling drop_scope() without a scope");
5437 return;
5438 }
5439 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005440 switch (scope->se_type)
5441 {
5442 case IF_SCOPE:
5443 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5444 case FOR_SCOPE:
5445 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5446 case WHILE_SCOPE:
5447 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5448 case TRY_SCOPE:
5449 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5450 case NO_SCOPE:
5451 case BLOCK_SCOPE:
5452 break;
5453 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005454 vim_free(scope);
5455}
5456
5457/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005458 * compile "if expr"
5459 *
5460 * "if expr" Produces instructions:
5461 * EVAL expr Push result of "expr"
5462 * JUMP_IF_FALSE end
5463 * ... body ...
5464 * end:
5465 *
5466 * "if expr | else" Produces instructions:
5467 * EVAL expr Push result of "expr"
5468 * JUMP_IF_FALSE else
5469 * ... body ...
5470 * JUMP_ALWAYS end
5471 * else:
5472 * ... body ...
5473 * end:
5474 *
5475 * "if expr1 | elseif expr2 | else" Produces instructions:
5476 * EVAL expr Push result of "expr"
5477 * JUMP_IF_FALSE elseif
5478 * ... body ...
5479 * JUMP_ALWAYS end
5480 * elseif:
5481 * EVAL expr Push result of "expr"
5482 * JUMP_IF_FALSE else
5483 * ... body ...
5484 * JUMP_ALWAYS end
5485 * else:
5486 * ... body ...
5487 * end:
5488 */
5489 static char_u *
5490compile_if(char_u *arg, cctx_T *cctx)
5491{
5492 char_u *p = arg;
5493 garray_T *instr = &cctx->ctx_instr;
5494 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005495 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005496
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005497 // compile "expr"; if we know it evaluates to FALSE skip the block
5498 tv.v_type = VAR_UNKNOWN;
5499 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5500 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5501 else
5502 cctx->ctx_skip = MAYBE;
5503 clear_tv(&tv);
5504 if (cctx->ctx_skip == MAYBE)
5505 {
5506 p = arg;
5507 if (compile_expr1(&p, cctx) == FAIL)
5508 return NULL;
5509 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005510
5511 scope = new_scope(cctx, IF_SCOPE);
5512 if (scope == NULL)
5513 return NULL;
5514
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005515 if (cctx->ctx_skip == MAYBE)
5516 {
5517 // "where" is set when ":elseif", "else" or ":endif" is found
5518 scope->se_u.se_if.is_if_label = instr->ga_len;
5519 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5520 }
5521 else
5522 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005523
5524 return p;
5525}
5526
5527 static char_u *
5528compile_elseif(char_u *arg, cctx_T *cctx)
5529{
5530 char_u *p = arg;
5531 garray_T *instr = &cctx->ctx_instr;
5532 isn_T *isn;
5533 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005534 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005535
5536 if (scope == NULL || scope->se_type != IF_SCOPE)
5537 {
5538 emsg(_(e_elseif_without_if));
5539 return NULL;
5540 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005541 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005542
Bram Moolenaar158906c2020-02-06 20:39:45 +01005543 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005544 {
5545 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005547 return NULL;
5548 // previous "if" or "elseif" jumps here
5549 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5550 isn->isn_arg.jump.jump_where = instr->ga_len;
5551 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005552
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005553 // compile "expr"; if we know it evaluates to FALSE skip the block
5554 tv.v_type = VAR_UNKNOWN;
5555 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5556 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5557 else
5558 cctx->ctx_skip = MAYBE;
5559 clear_tv(&tv);
5560 if (cctx->ctx_skip == MAYBE)
5561 {
5562 p = arg;
5563 if (compile_expr1(&p, cctx) == FAIL)
5564 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005565
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005566 // "where" is set when ":elseif", "else" or ":endif" is found
5567 scope->se_u.se_if.is_if_label = instr->ga_len;
5568 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5569 }
5570 else
5571 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005572
5573 return p;
5574}
5575
5576 static char_u *
5577compile_else(char_u *arg, cctx_T *cctx)
5578{
5579 char_u *p = arg;
5580 garray_T *instr = &cctx->ctx_instr;
5581 isn_T *isn;
5582 scope_T *scope = cctx->ctx_scope;
5583
5584 if (scope == NULL || scope->se_type != IF_SCOPE)
5585 {
5586 emsg(_(e_else_without_if));
5587 return NULL;
5588 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005589 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005591 // jump from previous block to the end, unless the else block is empty
5592 if (cctx->ctx_skip == MAYBE)
5593 {
5594 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005595 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005596 return NULL;
5597 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005598
Bram Moolenaar158906c2020-02-06 20:39:45 +01005599 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005600 {
5601 if (scope->se_u.se_if.is_if_label >= 0)
5602 {
5603 // previous "if" or "elseif" jumps here
5604 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5605 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005606 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005607 }
5608 }
5609
5610 if (cctx->ctx_skip != MAYBE)
5611 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005612
5613 return p;
5614}
5615
5616 static char_u *
5617compile_endif(char_u *arg, cctx_T *cctx)
5618{
5619 scope_T *scope = cctx->ctx_scope;
5620 ifscope_T *ifscope;
5621 garray_T *instr = &cctx->ctx_instr;
5622 isn_T *isn;
5623
5624 if (scope == NULL || scope->se_type != IF_SCOPE)
5625 {
5626 emsg(_(e_endif_without_if));
5627 return NULL;
5628 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005629 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005630 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005631
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005632 if (scope->se_u.se_if.is_if_label >= 0)
5633 {
5634 // previous "if" or "elseif" jumps here
5635 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5636 isn->isn_arg.jump.jump_where = instr->ga_len;
5637 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005638 // Fill in the "end" label in jumps at the end of the blocks.
5639 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005640 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005641
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005642 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005643 return arg;
5644}
5645
5646/*
5647 * compile "for var in expr"
5648 *
5649 * Produces instructions:
5650 * PUSHNR -1
5651 * STORE loop-idx Set index to -1
5652 * EVAL expr Push result of "expr"
5653 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5654 * - if beyond end, jump to "end"
5655 * - otherwise get item from list and push it
5656 * STORE var Store item in "var"
5657 * ... body ...
5658 * JUMP top Jump back to repeat
5659 * end: DROP Drop the result of "expr"
5660 *
5661 */
5662 static char_u *
5663compile_for(char_u *arg, cctx_T *cctx)
5664{
5665 char_u *p;
5666 size_t varlen;
5667 garray_T *instr = &cctx->ctx_instr;
5668 garray_T *stack = &cctx->ctx_type_stack;
5669 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005670 lvar_T *loop_lvar; // loop iteration variable
5671 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005672 type_T *vartype;
5673
5674 // TODO: list of variables: "for [key, value] in dict"
5675 // parse "var"
5676 for (p = arg; eval_isnamec1(*p); ++p)
5677 ;
5678 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005679 var_lvar = lookup_local(arg, varlen, cctx);
5680 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005681 {
5682 semsg(_("E1023: variable already defined: %s"), arg);
5683 return NULL;
5684 }
5685
5686 // consume "in"
5687 p = skipwhite(p);
5688 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5689 {
5690 emsg(_(e_missing_in));
5691 return NULL;
5692 }
5693 p = skipwhite(p + 2);
5694
5695
5696 scope = new_scope(cctx, FOR_SCOPE);
5697 if (scope == NULL)
5698 return NULL;
5699
5700 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005701 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5702 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005703 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005704 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005705 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005706 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005707 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005708
5709 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005710 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5711 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005712 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005713 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005714 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005715 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005716 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005717
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005718 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005719
5720 // compile "expr", it remains on the stack until "endfor"
5721 arg = p;
5722 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005723 {
5724 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005725 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005726 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005727
5728 // now we know the type of "var"
5729 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5730 if (vartype->tt_type != VAR_LIST)
5731 {
5732 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005733 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005734 return NULL;
5735 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005736 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005737 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005738
5739 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005740 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005741
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005742 generate_FOR(cctx, loop_lvar->lv_idx);
5743 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005744
5745 return arg;
5746}
5747
5748/*
5749 * compile "endfor"
5750 */
5751 static char_u *
5752compile_endfor(char_u *arg, cctx_T *cctx)
5753{
5754 garray_T *instr = &cctx->ctx_instr;
5755 scope_T *scope = cctx->ctx_scope;
5756 forscope_T *forscope;
5757 isn_T *isn;
5758
5759 if (scope == NULL || scope->se_type != FOR_SCOPE)
5760 {
5761 emsg(_(e_for));
5762 return NULL;
5763 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005764 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005765 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005766 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005767
5768 // At end of ":for" scope jump back to the FOR instruction.
5769 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5770
5771 // Fill in the "end" label in the FOR statement so it can jump here
5772 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5773 isn->isn_arg.forloop.for_end = instr->ga_len;
5774
5775 // Fill in the "end" label any BREAK statements
5776 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5777
5778 // Below the ":for" scope drop the "expr" list from the stack.
5779 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5780 return NULL;
5781
5782 vim_free(scope);
5783
5784 return arg;
5785}
5786
5787/*
5788 * compile "while expr"
5789 *
5790 * Produces instructions:
5791 * top: EVAL expr Push result of "expr"
5792 * JUMP_IF_FALSE end jump if false
5793 * ... body ...
5794 * JUMP top Jump back to repeat
5795 * end:
5796 *
5797 */
5798 static char_u *
5799compile_while(char_u *arg, cctx_T *cctx)
5800{
5801 char_u *p = arg;
5802 garray_T *instr = &cctx->ctx_instr;
5803 scope_T *scope;
5804
5805 scope = new_scope(cctx, WHILE_SCOPE);
5806 if (scope == NULL)
5807 return NULL;
5808
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005809 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005810
5811 // compile "expr"
5812 if (compile_expr1(&p, cctx) == FAIL)
5813 return NULL;
5814
5815 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005816 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005817 JUMP_IF_FALSE, cctx) == FAIL)
5818 return FAIL;
5819
5820 return p;
5821}
5822
5823/*
5824 * compile "endwhile"
5825 */
5826 static char_u *
5827compile_endwhile(char_u *arg, cctx_T *cctx)
5828{
5829 scope_T *scope = cctx->ctx_scope;
5830
5831 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5832 {
5833 emsg(_(e_while));
5834 return NULL;
5835 }
5836 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005837 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005838
5839 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005840 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005841
5842 // Fill in the "end" label in the WHILE statement so it can jump here.
5843 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005844 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005845
5846 vim_free(scope);
5847
5848 return arg;
5849}
5850
5851/*
5852 * compile "continue"
5853 */
5854 static char_u *
5855compile_continue(char_u *arg, cctx_T *cctx)
5856{
5857 scope_T *scope = cctx->ctx_scope;
5858
5859 for (;;)
5860 {
5861 if (scope == NULL)
5862 {
5863 emsg(_(e_continue));
5864 return NULL;
5865 }
5866 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5867 break;
5868 scope = scope->se_outer;
5869 }
5870
5871 // Jump back to the FOR or WHILE instruction.
5872 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005873 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5874 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005875 return arg;
5876}
5877
5878/*
5879 * compile "break"
5880 */
5881 static char_u *
5882compile_break(char_u *arg, cctx_T *cctx)
5883{
5884 scope_T *scope = cctx->ctx_scope;
5885 endlabel_T **el;
5886
5887 for (;;)
5888 {
5889 if (scope == NULL)
5890 {
5891 emsg(_(e_break));
5892 return NULL;
5893 }
5894 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5895 break;
5896 scope = scope->se_outer;
5897 }
5898
5899 // Jump to the end of the FOR or WHILE loop.
5900 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005901 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005902 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005903 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005904 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5905 return FAIL;
5906
5907 return arg;
5908}
5909
5910/*
5911 * compile "{" start of block
5912 */
5913 static char_u *
5914compile_block(char_u *arg, cctx_T *cctx)
5915{
5916 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5917 return NULL;
5918 return skipwhite(arg + 1);
5919}
5920
5921/*
5922 * compile end of block: drop one scope
5923 */
5924 static void
5925compile_endblock(cctx_T *cctx)
5926{
5927 scope_T *scope = cctx->ctx_scope;
5928
5929 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005930 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005931 vim_free(scope);
5932}
5933
5934/*
5935 * compile "try"
5936 * Creates a new scope for the try-endtry, pointing to the first catch and
5937 * finally.
5938 * Creates another scope for the "try" block itself.
5939 * TRY instruction sets up exception handling at runtime.
5940 *
5941 * "try"
5942 * TRY -> catch1, -> finally push trystack entry
5943 * ... try block
5944 * "throw {exception}"
5945 * EVAL {exception}
5946 * THROW create exception
5947 * ... try block
5948 * " catch {expr}"
5949 * JUMP -> finally
5950 * catch1: PUSH exeception
5951 * EVAL {expr}
5952 * MATCH
5953 * JUMP nomatch -> catch2
5954 * CATCH remove exception
5955 * ... catch block
5956 * " catch"
5957 * JUMP -> finally
5958 * catch2: CATCH remove exception
5959 * ... catch block
5960 * " finally"
5961 * finally:
5962 * ... finally block
5963 * " endtry"
5964 * ENDTRY pop trystack entry, may rethrow
5965 */
5966 static char_u *
5967compile_try(char_u *arg, cctx_T *cctx)
5968{
5969 garray_T *instr = &cctx->ctx_instr;
5970 scope_T *try_scope;
5971 scope_T *scope;
5972
5973 // scope that holds the jumps that go to catch/finally/endtry
5974 try_scope = new_scope(cctx, TRY_SCOPE);
5975 if (try_scope == NULL)
5976 return NULL;
5977
5978 // "catch" is set when the first ":catch" is found.
5979 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005980 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005981 if (generate_instr(cctx, ISN_TRY) == NULL)
5982 return NULL;
5983
5984 // scope for the try block itself
5985 scope = new_scope(cctx, BLOCK_SCOPE);
5986 if (scope == NULL)
5987 return NULL;
5988
5989 return arg;
5990}
5991
5992/*
5993 * compile "catch {expr}"
5994 */
5995 static char_u *
5996compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5997{
5998 scope_T *scope = cctx->ctx_scope;
5999 garray_T *instr = &cctx->ctx_instr;
6000 char_u *p;
6001 isn_T *isn;
6002
6003 // end block scope from :try or :catch
6004 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6005 compile_endblock(cctx);
6006 scope = cctx->ctx_scope;
6007
6008 // Error if not in a :try scope
6009 if (scope == NULL || scope->se_type != TRY_SCOPE)
6010 {
6011 emsg(_(e_catch));
6012 return NULL;
6013 }
6014
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006015 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006016 {
6017 emsg(_("E1033: catch unreachable after catch-all"));
6018 return NULL;
6019 }
6020
6021 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006022 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006023 JUMP_ALWAYS, cctx) == FAIL)
6024 return NULL;
6025
6026 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006027 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006028 if (isn->isn_arg.try.try_catch == 0)
6029 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006030 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006031 {
6032 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006033 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006034 isn->isn_arg.jump.jump_where = instr->ga_len;
6035 }
6036
6037 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006038 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006039 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006040 scope->se_u.se_try.ts_caught_all = TRUE;
6041 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006042 }
6043 else
6044 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006045 char_u *end;
6046 char_u *pat;
6047 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006048 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006049 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006050
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006051 // Push v:exception, push {expr} and MATCH
6052 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6053
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006054 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006055 if (*end != *p)
6056 {
6057 semsg(_("E1067: Separator mismatch: %s"), p);
6058 vim_free(tofree);
6059 return FAIL;
6060 }
6061 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006062 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006063 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006064 len = (int)(end - tofree);
6065 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006066 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006067 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006068 if (pat == NULL)
6069 return FAIL;
6070 if (generate_PUSHS(cctx, pat) == FAIL)
6071 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006072
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006073 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6074 return NULL;
6075
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006076 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006077 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6078 return NULL;
6079 }
6080
6081 if (generate_instr(cctx, ISN_CATCH) == NULL)
6082 return NULL;
6083
6084 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6085 return NULL;
6086 return p;
6087}
6088
6089 static char_u *
6090compile_finally(char_u *arg, cctx_T *cctx)
6091{
6092 scope_T *scope = cctx->ctx_scope;
6093 garray_T *instr = &cctx->ctx_instr;
6094 isn_T *isn;
6095
6096 // end block scope from :try or :catch
6097 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6098 compile_endblock(cctx);
6099 scope = cctx->ctx_scope;
6100
6101 // Error if not in a :try scope
6102 if (scope == NULL || scope->se_type != TRY_SCOPE)
6103 {
6104 emsg(_(e_finally));
6105 return NULL;
6106 }
6107
6108 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006109 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006110 if (isn->isn_arg.try.try_finally != 0)
6111 {
6112 emsg(_(e_finally_dup));
6113 return NULL;
6114 }
6115
6116 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006117 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006118
Bram Moolenaar585fea72020-04-02 22:33:21 +02006119 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006120 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006121 {
6122 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006123 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006124 isn->isn_arg.jump.jump_where = instr->ga_len;
6125 }
6126
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006127 // TODO: set index in ts_finally_label jumps
6128
6129 return arg;
6130}
6131
6132 static char_u *
6133compile_endtry(char_u *arg, cctx_T *cctx)
6134{
6135 scope_T *scope = cctx->ctx_scope;
6136 garray_T *instr = &cctx->ctx_instr;
6137 isn_T *isn;
6138
6139 // end block scope from :catch or :finally
6140 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6141 compile_endblock(cctx);
6142 scope = cctx->ctx_scope;
6143
6144 // Error if not in a :try scope
6145 if (scope == NULL || scope->se_type != TRY_SCOPE)
6146 {
6147 if (scope == NULL)
6148 emsg(_(e_no_endtry));
6149 else if (scope->se_type == WHILE_SCOPE)
6150 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006151 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006152 emsg(_(e_endfor));
6153 else
6154 emsg(_(e_endif));
6155 return NULL;
6156 }
6157
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006158 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006159 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6160 {
6161 emsg(_("E1032: missing :catch or :finally"));
6162 return NULL;
6163 }
6164
6165 // Fill in the "end" label in jumps at the end of the blocks, if not done
6166 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006167 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006168
6169 // End :catch or :finally scope: set value in ISN_TRY instruction
6170 if (isn->isn_arg.try.try_finally == 0)
6171 isn->isn_arg.try.try_finally = instr->ga_len;
6172 compile_endblock(cctx);
6173
6174 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6175 return NULL;
6176 return arg;
6177}
6178
6179/*
6180 * compile "throw {expr}"
6181 */
6182 static char_u *
6183compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6184{
6185 char_u *p = skipwhite(arg);
6186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006187 if (compile_expr1(&p, cctx) == FAIL)
6188 return NULL;
6189 if (may_generate_2STRING(-1, cctx) == FAIL)
6190 return NULL;
6191 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6192 return NULL;
6193
6194 return p;
6195}
6196
6197/*
6198 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006199 * compile "echomsg expr"
6200 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006201 * compile "execute expr"
6202 */
6203 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006204compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006205{
6206 char_u *p = arg;
6207 int count = 0;
6208
6209 for (;;)
6210 {
6211 if (compile_expr1(&p, cctx) == FAIL)
6212 return NULL;
6213 ++count;
6214 p = skipwhite(p);
6215 if (ends_excmd(*p))
6216 break;
6217 }
6218
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006219 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6220 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6221 else if (cmdidx == CMD_execute)
6222 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6223 else if (cmdidx == CMD_echomsg)
6224 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6225 else
6226 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006227 return p;
6228}
6229
6230/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006231 * A command that is not compiled, execute with legacy code.
6232 */
6233 static char_u *
6234compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6235{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006236 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006237 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006238
6239 if (cctx->ctx_skip == TRUE)
6240 goto theend;
6241
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006242 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
6243 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006244 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6245 {
6246 // expand filename in "syntax include [@group] filename"
6247 has_expr = TRUE;
6248 eap->arg = skipwhite(eap->arg + 7);
6249 if (*eap->arg == '@')
6250 eap->arg = skiptowhite(eap->arg);
6251 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006252
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006253 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006254 {
6255 int count = 0;
6256 char_u *start = skipwhite(line);
6257
6258 // :cmd xxx`=expr1`yyy`=expr2`zzz
6259 // PUSHS ":cmd xxx"
6260 // eval expr1
6261 // PUSHS "yyy"
6262 // eval expr2
6263 // PUSHS "zzz"
6264 // EXECCONCAT 5
6265 for (;;)
6266 {
6267 if (p > start)
6268 {
6269 generate_PUSHS(cctx, vim_strnsave(start, (int)(p - start)));
6270 ++count;
6271 }
6272 p += 2;
6273 if (compile_expr1(&p, cctx) == FAIL)
6274 return NULL;
6275 may_generate_2STRING(-1, cctx);
6276 ++count;
6277 p = skipwhite(p);
6278 if (*p != '`')
6279 {
6280 emsg(_("E1083: missing backtick"));
6281 return NULL;
6282 }
6283 start = p + 1;
6284
6285 p = (char_u *)strstr((char *)start, "`=");
6286 if (p == NULL)
6287 {
6288 if (*skipwhite(start) != NUL)
6289 {
6290 generate_PUSHS(cctx, vim_strsave(start));
6291 ++count;
6292 }
6293 break;
6294 }
6295 }
6296 generate_EXECCONCAT(cctx, count);
6297 }
6298 else
6299 generate_EXEC(cctx, line);
6300
6301theend:
6302 return (char_u *)"";
6303}
6304
6305/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006306 * After ex_function() has collected all the function lines: parse and compile
6307 * the lines into instructions.
6308 * Adds the function to "def_functions".
6309 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6310 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006311 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006312 * This can be used recursively through compile_lambda(), which may reallocate
6313 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006314 */
6315 void
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006316compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006317{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318 char_u *line = NULL;
6319 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320 char *errormsg = NULL; // error message
6321 int had_return = FALSE;
6322 cctx_T cctx;
6323 garray_T *instr;
6324 int called_emsg_before = called_emsg;
6325 int ret = FAIL;
6326 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006327 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006330 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01006331
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006332 if (ufunc->uf_dfunc_idx >= 0)
6333 {
6334 // Redefining a function that was compiled before.
6335 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
6336
6337 // Free old instructions.
6338 delete_def_function_contents(dfunc);
6339 }
6340 else
6341 {
6342 // Add the function to "def_functions".
6343 if (ga_grow(&def_functions, 1) == FAIL)
6344 return;
6345 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006346 CLEAR_POINTER(dfunc);
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006347 dfunc->df_idx = def_functions.ga_len;
6348 ufunc->uf_dfunc_idx = dfunc->df_idx;
6349 dfunc->df_ufunc = ufunc;
6350 ++def_functions.ga_len;
6351 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006352 }
6353
Bram Moolenaara80faa82020-04-12 19:37:17 +02006354 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006355 cctx.ctx_ufunc = ufunc;
6356 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006357 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006358 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6359 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6360 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6361 cctx.ctx_type_list = &ufunc->uf_type_list;
6362 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6363 instr = &cctx.ctx_instr;
6364
6365 // Most modern script version.
6366 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6367
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006368 if (ufunc->uf_def_args.ga_len > 0)
6369 {
6370 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006371 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006372 int i;
6373 char_u *arg;
6374 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6375
6376 // Produce instructions for the default values of optional arguments.
6377 // Store the instruction index in uf_def_arg_idx[] so that we know
6378 // where to start when the function is called, depending on the number
6379 // of arguments.
6380 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6381 if (ufunc->uf_def_arg_idx == NULL)
6382 goto erret;
6383 for (i = 0; i < count; ++i)
6384 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006385 garray_T *stack = &cctx.ctx_type_stack;
6386 type_T *val_type;
6387 int arg_idx = first_def_arg + i;
6388
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006389 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6390 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006391 if (compile_expr1(&arg, &cctx) == FAIL)
6392 goto erret;
6393
6394 // If no type specified use the type of the default value.
6395 // Otherwise check that the default value type matches the
6396 // specified type.
6397 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6398 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6399 ufunc->uf_arg_types[arg_idx] = val_type;
6400 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6401 == FAIL)
6402 {
6403 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6404 arg_idx + 1);
6405 goto erret;
6406 }
6407
6408 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006409 goto erret;
6410 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006411 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6412 }
6413
6414 /*
6415 * Loop over all the lines of the function and generate instructions.
6416 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006417 for (;;)
6418 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006419 exarg_T ea;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006420 int is_ex_command = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006421
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006422 // Bail out on the first error to avoid a flood of errors and report
6423 // the right line number when inside try/catch.
6424 if (emsg_before != called_emsg)
6425 goto erret;
6426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006427 if (line != NULL && *line == '|')
6428 // the line continues after a '|'
6429 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006430 else if (line != NULL && *line != NUL
6431 && !(*line == '#' && (line == cctx.ctx_line_start
6432 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006433 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006434 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006435 goto erret;
6436 }
6437 else
6438 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006439 line = next_line_from_context(&cctx);
6440 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006441 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006442 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006443 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006444 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006445
6446 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006447 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006448 ea.cmdlinep = &line;
6449 ea.cmd = skipwhite(line);
6450
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006451 // Some things can be recognized by the first character.
6452 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006453 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006454 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006455 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006456 if (ea.cmd[1] != '{')
6457 {
6458 line = (char_u *)"";
6459 continue;
6460 }
6461 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006462
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006463 case '}':
6464 {
6465 // "}" ends a block scope
6466 scopetype_T stype = cctx.ctx_scope == NULL
6467 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006468
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006469 if (stype == BLOCK_SCOPE)
6470 {
6471 compile_endblock(&cctx);
6472 line = ea.cmd;
6473 }
6474 else
6475 {
6476 emsg(_("E1025: using } outside of a block scope"));
6477 goto erret;
6478 }
6479 if (line != NULL)
6480 line = skipwhite(ea.cmd + 1);
6481 continue;
6482 }
6483
6484 case '{':
6485 // "{" starts a block scope
6486 // "{'a': 1}->func() is something else
6487 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6488 {
6489 line = compile_block(ea.cmd, &cctx);
6490 continue;
6491 }
6492 break;
6493
6494 case ':':
6495 is_ex_command = TRUE;
6496 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006497 }
6498
6499 /*
6500 * COMMAND MODIFIERS
6501 */
6502 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6503 {
6504 if (errormsg != NULL)
6505 goto erret;
6506 // empty line or comment
6507 line = (char_u *)"";
6508 continue;
6509 }
6510
6511 // Skip ":call" to get to the function name.
6512 if (checkforcmd(&ea.cmd, "call", 3))
6513 ea.cmd = skipwhite(ea.cmd);
6514
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006515 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006516 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006517 // Assuming the command starts with a variable or function name,
6518 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
6519 // val".
6520 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
6521 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006522 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006523 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006524 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006525 int oplen;
6526 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006527
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006528 oplen = assignment_len(skipwhite(p), &heredoc);
6529 if (oplen > 0)
6530 {
6531 // Recognize an assignment if we recognize the variable
6532 // name:
6533 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006534 // "local = expr" where "local" is a local var.
6535 // "script = expr" where "script" is a script-local var.
6536 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006537 // "&opt = expr"
6538 // "$ENV = expr"
6539 // "@r = expr"
6540 if (*ea.cmd == '&'
6541 || *ea.cmd == '$'
6542 || *ea.cmd == '@'
6543 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006544 || lookup_local(ea.cmd, p - ea.cmd, &cctx) != NULL
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006545 || lookup_script(ea.cmd, p - ea.cmd) == OK
6546 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
6547 {
6548 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6549 if (line == NULL)
6550 goto erret;
6551 continue;
6552 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006553 }
6554 }
6555 }
6556
6557 /*
6558 * COMMAND after range
6559 */
6560 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006561 p = find_ex_command(&ea, NULL, is_ex_command ? NULL
6562 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006563 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006564
6565 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6566 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006567 if (cctx.ctx_skip == TRUE)
6568 {
6569 line += STRLEN(line);
6570 continue;
6571 }
6572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573 // Expression or function call.
6574 if (ea.cmdidx == CMD_eval)
6575 {
6576 p = ea.cmd;
6577 if (compile_expr1(&p, &cctx) == FAIL)
6578 goto erret;
6579
6580 // drop the return value
6581 generate_instr_drop(&cctx, ISN_DROP, 1);
6582 line = p;
6583 continue;
6584 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006585 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006586 iemsg("Command from find_ex_command() not handled");
6587 goto erret;
6588 }
6589
6590 p = skipwhite(p);
6591
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006592 if (cctx.ctx_skip == TRUE
6593 && ea.cmdidx != CMD_elseif
6594 && ea.cmdidx != CMD_else
6595 && ea.cmdidx != CMD_endif)
6596 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006597 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006598 continue;
6599 }
6600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601 switch (ea.cmdidx)
6602 {
6603 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006604 ea.arg = p;
6605 line = compile_nested_function(&ea, &cctx);
6606 break;
6607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006609 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006610 goto erret;
6611
6612 case CMD_return:
6613 line = compile_return(p, set_return_type, &cctx);
6614 had_return = TRUE;
6615 break;
6616
6617 case CMD_let:
6618 case CMD_const:
6619 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
6620 break;
6621
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006622 case CMD_unlet:
6623 case CMD_unlockvar:
6624 case CMD_lockvar:
6625 line = compile_unletlock(p, &ea, &cctx);
6626 break;
6627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628 case CMD_import:
6629 line = compile_import(p, &cctx);
6630 break;
6631
6632 case CMD_if:
6633 line = compile_if(p, &cctx);
6634 break;
6635 case CMD_elseif:
6636 line = compile_elseif(p, &cctx);
6637 break;
6638 case CMD_else:
6639 line = compile_else(p, &cctx);
6640 break;
6641 case CMD_endif:
6642 line = compile_endif(p, &cctx);
6643 break;
6644
6645 case CMD_while:
6646 line = compile_while(p, &cctx);
6647 break;
6648 case CMD_endwhile:
6649 line = compile_endwhile(p, &cctx);
6650 break;
6651
6652 case CMD_for:
6653 line = compile_for(p, &cctx);
6654 break;
6655 case CMD_endfor:
6656 line = compile_endfor(p, &cctx);
6657 break;
6658 case CMD_continue:
6659 line = compile_continue(p, &cctx);
6660 break;
6661 case CMD_break:
6662 line = compile_break(p, &cctx);
6663 break;
6664
6665 case CMD_try:
6666 line = compile_try(p, &cctx);
6667 break;
6668 case CMD_catch:
6669 line = compile_catch(p, &cctx);
6670 break;
6671 case CMD_finally:
6672 line = compile_finally(p, &cctx);
6673 break;
6674 case CMD_endtry:
6675 line = compile_endtry(p, &cctx);
6676 break;
6677 case CMD_throw:
6678 line = compile_throw(p, &cctx);
6679 break;
6680
6681 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006682 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006683 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006684 case CMD_echomsg:
6685 case CMD_echoerr:
6686 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006687 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688
6689 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006690 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006691 // Not recognized, execute with do_cmdline_cmd().
6692 ea.arg = p;
6693 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006694 break;
6695 }
6696 if (line == NULL)
6697 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006698 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006699
6700 if (cctx.ctx_type_stack.ga_len < 0)
6701 {
6702 iemsg("Type stack underflow");
6703 goto erret;
6704 }
6705 }
6706
6707 if (cctx.ctx_scope != NULL)
6708 {
6709 if (cctx.ctx_scope->se_type == IF_SCOPE)
6710 emsg(_(e_endif));
6711 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6712 emsg(_(e_endwhile));
6713 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6714 emsg(_(e_endfor));
6715 else
6716 emsg(_("E1026: Missing }"));
6717 goto erret;
6718 }
6719
6720 if (!had_return)
6721 {
6722 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6723 {
6724 emsg(_("E1027: Missing return statement"));
6725 goto erret;
6726 }
6727
6728 // Return zero if there is no return at the end.
6729 generate_PUSHNR(&cctx, 0);
6730 generate_instr(&cctx, ISN_RETURN);
6731 }
6732
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006733 {
6734 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6735 + ufunc->uf_dfunc_idx;
6736 dfunc->df_deleted = FALSE;
6737 dfunc->df_instr = instr->ga_data;
6738 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006739 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006740 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006741 if (cctx.ctx_outer_used)
6742 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006743 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006745 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006746 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006747 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006748
6749 // Create a type for the function, with the return type and any
6750 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006751 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6752 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006753 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006754 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006755 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6756 argcount, &ufunc->uf_type_list);
6757 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006758 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006759 argcount + varargs,
6760 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006761 {
6762 ret = FAIL;
6763 goto erret;
6764 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006765 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6766 ufunc->uf_func_type->tt_min_argcount =
6767 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006768 if (ufunc->uf_arg_types == NULL)
6769 {
6770 int i;
6771
6772 // lambda does not have argument types.
6773 for (i = 0; i < argcount; ++i)
6774 ufunc->uf_func_type->tt_args[i] = &t_any;
6775 }
6776 else
6777 mch_memmove(ufunc->uf_func_type->tt_args,
6778 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006779 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006780 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006781 ufunc->uf_func_type->tt_args[argcount] =
6782 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006783 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6784 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006785 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006786 else
6787 // No arguments, can use a predefined type.
6788 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6789 argcount, &ufunc->uf_type_list);
6790
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006791 }
6792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006793 ret = OK;
6794
6795erret:
6796 if (ret == FAIL)
6797 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006798 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006799 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6800 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006801
6802 for (idx = 0; idx < instr->ga_len; ++idx)
6803 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006806 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006807 if (!dfunc->df_deleted)
6808 --def_functions.ga_len;
6809
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006810 while (cctx.ctx_scope != NULL)
6811 drop_scope(&cctx);
6812
Bram Moolenaar20431c92020-03-20 18:39:46 +01006813 // Don't execute this function body.
6814 ga_clear_strings(&ufunc->uf_lines);
6815
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006816 if (errormsg != NULL)
6817 emsg(errormsg);
6818 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006819 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006820 }
6821
6822 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006823 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006824 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006825 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006826}
6827
6828/*
6829 * Delete an instruction, free what it contains.
6830 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006831 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006832delete_instr(isn_T *isn)
6833{
6834 switch (isn->isn_type)
6835 {
6836 case ISN_EXEC:
6837 case ISN_LOADENV:
6838 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006839 case ISN_LOADB:
6840 case ISN_LOADW:
6841 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006842 case ISN_LOADOPT:
6843 case ISN_MEMBER:
6844 case ISN_PUSHEXC:
6845 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006846 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006847 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006848 case ISN_STOREB:
6849 case ISN_STOREW:
6850 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006851 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006852 vim_free(isn->isn_arg.string);
6853 break;
6854
6855 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006856 case ISN_STORES:
6857 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006858 break;
6859
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006860 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006861 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006862 vim_free(isn->isn_arg.unlet.ul_name);
6863 break;
6864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006865 case ISN_STOREOPT:
6866 vim_free(isn->isn_arg.storeopt.so_name);
6867 break;
6868
6869 case ISN_PUSHBLOB: // push blob isn_arg.blob
6870 blob_unref(isn->isn_arg.blob);
6871 break;
6872
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006873 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006874#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006875 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006876#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006877 break;
6878
6879 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006880#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006881 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006882#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006883 break;
6884
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006885 case ISN_UCALL:
6886 vim_free(isn->isn_arg.ufunc.cuf_name);
6887 break;
6888
Bram Moolenaar221fcc72020-05-05 19:46:20 +02006889 case ISN_FUNCREF:
6890 {
6891 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6892 + isn->isn_arg.funcref.fr_func;
6893 func_ptr_unref(dfunc->df_ufunc);
6894 }
6895 break;
6896
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897 case ISN_2BOOL:
6898 case ISN_2STRING:
6899 case ISN_ADDBLOB:
6900 case ISN_ADDLIST:
6901 case ISN_BCALL:
6902 case ISN_CATCH:
6903 case ISN_CHECKNR:
6904 case ISN_CHECKTYPE:
6905 case ISN_COMPAREANY:
6906 case ISN_COMPAREBLOB:
6907 case ISN_COMPAREBOOL:
6908 case ISN_COMPAREDICT:
6909 case ISN_COMPAREFLOAT:
6910 case ISN_COMPAREFUNC:
6911 case ISN_COMPARELIST:
6912 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006913 case ISN_COMPARESPECIAL:
6914 case ISN_COMPARESTRING:
6915 case ISN_CONCAT:
6916 case ISN_DCALL:
6917 case ISN_DROP:
6918 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006919 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006920 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006921 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006922 case ISN_EXECCONCAT:
6923 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006924 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006925 case ISN_INDEX:
6926 case ISN_JUMP:
6927 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006928 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929 case ISN_LOADSCRIPT:
6930 case ISN_LOADREG:
6931 case ISN_LOADV:
6932 case ISN_NEGATENR:
6933 case ISN_NEWDICT:
6934 case ISN_NEWLIST:
6935 case ISN_OPNR:
6936 case ISN_OPFLOAT:
6937 case ISN_OPANY:
6938 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006939 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006940 case ISN_PUSHF:
6941 case ISN_PUSHNR:
6942 case ISN_PUSHBOOL:
6943 case ISN_PUSHSPEC:
6944 case ISN_RETURN:
6945 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02006946 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006947 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006949 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950 case ISN_STORESCRIPT:
6951 case ISN_THROW:
6952 case ISN_TRY:
6953 // nothing allocated
6954 break;
6955 }
6956}
6957
6958/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006959 * Free all instructions for "dfunc".
6960 */
6961 static void
6962delete_def_function_contents(dfunc_T *dfunc)
6963{
6964 int idx;
6965
6966 ga_clear(&dfunc->df_def_args_isn);
6967
6968 if (dfunc->df_instr != NULL)
6969 {
6970 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6971 delete_instr(dfunc->df_instr + idx);
6972 VIM_CLEAR(dfunc->df_instr);
6973 }
6974
6975 dfunc->df_deleted = TRUE;
6976}
6977
6978/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979 * When a user function is deleted, delete any associated def function.
6980 */
6981 void
6982delete_def_function(ufunc_T *ufunc)
6983{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006984 if (ufunc->uf_dfunc_idx >= 0)
6985 {
6986 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6987 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006988
Bram Moolenaar20431c92020-03-20 18:39:46 +01006989 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006990 }
6991}
6992
6993#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006994/*
6995 * Free all functions defined with ":def".
6996 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997 void
6998free_def_functions(void)
6999{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007000 int idx;
7001
7002 for (idx = 0; idx < def_functions.ga_len; ++idx)
7003 {
7004 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7005
7006 delete_def_function_contents(dfunc);
7007 }
7008
7009 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007010}
7011#endif
7012
7013
7014#endif // FEAT_EVAL