blob: 4ebb88062cb61ba606d5b2970fb04747400213e4 [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 Moolenaarf0eefce2020-05-07 22:19:01 +02004077 if (compile_subscript(arg, cctx, &start_leader, end_leader,
4078 bef1_tv, bef2_tv, new_tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 return FAIL;
4080
4081 // Now deal with prefixed '-', '+' and '!', if not done already.
4082 return compile_leader(cctx, start_leader, end_leader);
4083}
4084
4085/*
4086 * * number multiplication
4087 * / number division
4088 * % number modulo
4089 */
4090 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004091compile_expr6(
4092 char_u **arg,
4093 cctx_T *cctx,
4094 typval_T *bef_tv,
4095 typval_T *new_tv)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096{
4097 char_u *op;
4098
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004099 // get the first expression
4100 if (compile_expr7(arg, cctx, NULL, bef_tv, new_tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 return FAIL;
4102
4103 /*
4104 * Repeat computing, until no "*", "/" or "%" is following.
4105 */
4106 for (;;)
4107 {
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004108 typval_T tv2;
4109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 op = skipwhite(*arg);
4111 if (*op != '*' && *op != '/' && *op != '%')
4112 break;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004113
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004114 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 {
4116 char_u buf[3];
4117
4118 vim_strncpy(buf, op, 1);
4119 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004120 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 }
4122 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004123 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004124 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004126 // get the second expression
4127 tv2.v_type = VAR_UNKNOWN;
4128 if (compile_expr7(arg, cctx, bef_tv, new_tv, &tv2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 return FAIL;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004130 if (new_tv->v_type == VAR_NUMBER && tv2.v_type == VAR_NUMBER)
4131 {
4132 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004134 // both are numbers: compute the result
4135 switch (*op)
4136 {
4137 case '*': res = new_tv->vval.v_number * tv2.vval.v_number;
4138 break;
4139 case '/': res = new_tv->vval.v_number / tv2.vval.v_number;
4140 break;
4141 case '%': res = new_tv->vval.v_number % tv2.vval.v_number;
4142 break;
4143 }
4144 new_tv->vval.v_number = res;
4145 }
4146 else
4147 {
4148 generate_tv_PUSH(cctx, new_tv);
4149 generate_tv_PUSH(cctx, &tv2);
4150 generate_two_op(cctx, op);
4151 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 }
4153
4154 return OK;
4155}
4156
4157/*
4158 * + number addition
4159 * - number subtraction
4160 * .. string concatenation
4161 */
4162 static int
4163compile_expr5(char_u **arg, cctx_T *cctx)
4164{
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004165 typval_T tv1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 char_u *op;
4167 int oplen;
4168
4169 // get the first variable
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004170 tv1.v_type = VAR_UNKNOWN;
4171 if (compile_expr6(arg, cctx, NULL, &tv1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 return FAIL;
4173
4174 /*
4175 * Repeat computing, until no "+", "-" or ".." is following.
4176 */
4177 for (;;)
4178 {
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004179 typval_T tv2;
4180
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 op = skipwhite(*arg);
4182 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
4183 break;
4184 oplen = (*op == '.' ? 2 : 1);
4185
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004186 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 {
4188 char_u buf[3];
4189
4190 vim_strncpy(buf, op, oplen);
4191 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004192 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 }
4194
4195 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004196 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004197 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004199 // get the second expression
4200 tv2.v_type = VAR_UNKNOWN;
4201 if (compile_expr6(arg, cctx, &tv1, &tv2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 return FAIL;
4203
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004204 if (*op == '+' && tv1.v_type == VAR_NUMBER && tv2.v_type == VAR_NUMBER)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 {
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004206 // add constant numbers
4207 tv1.vval.v_number = tv1.vval.v_number + tv2.vval.v_number;
4208 }
4209 else if (*op == '-' && tv1.v_type == VAR_NUMBER
4210 && tv2.v_type == VAR_NUMBER)
4211 {
4212 // subtract constant numbers
4213 tv1.vval.v_number = tv1.vval.v_number - tv2.vval.v_number;
4214 }
4215 else if (*op == '.' && tv1.v_type == VAR_STRING
4216 && tv2.v_type == VAR_STRING)
4217 {
4218 // concatenate constant strings
4219 char_u *s1 = tv1.vval.v_string;
4220 char_u *s2 = tv2.vval.v_string;
4221 size_t len1 = STRLEN(s1);
4222
4223 tv1.vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4224 if (tv1.vval.v_string == NULL)
4225 {
4226 vim_free(s1);
4227 vim_free(s2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 return FAIL;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004229 }
4230 mch_memmove(tv1.vval.v_string, s1, len1);
4231 STRCPY(tv1.vval.v_string + len1, s2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 }
4233 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004234 {
4235 generate_tv_PUSH(cctx, &tv1);
4236 generate_tv_PUSH(cctx, &tv2);
4237 if (*op == '.')
4238 {
4239 if (may_generate_2STRING(-2, cctx) == FAIL
4240 || may_generate_2STRING(-1, cctx) == FAIL)
4241 return FAIL;
4242 generate_instr_drop(cctx, ISN_CONCAT, 1);
4243 }
4244 else
4245 generate_two_op(cctx, op);
4246 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 }
4248
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004249 // TODO: move to caller
4250 generate_tv_PUSH(cctx, &tv1);
4251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 return OK;
4253}
4254
4255/*
4256 * expr5a == expr5b
4257 * expr5a =~ expr5b
4258 * expr5a != expr5b
4259 * expr5a !~ expr5b
4260 * expr5a > expr5b
4261 * expr5a >= expr5b
4262 * expr5a < expr5b
4263 * expr5a <= expr5b
4264 * expr5a is expr5b
4265 * expr5a isnot expr5b
4266 *
4267 * Produces instructions:
4268 * EVAL expr5a Push result of "expr5a"
4269 * EVAL expr5b Push result of "expr5b"
4270 * COMPARE one of the compare instructions
4271 */
4272 static int
4273compile_expr4(char_u **arg, cctx_T *cctx)
4274{
4275 exptype_T type = EXPR_UNKNOWN;
4276 char_u *p;
4277 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 int type_is = FALSE;
4279
4280 // get the first variable
4281 if (compile_expr5(arg, cctx) == FAIL)
4282 return FAIL;
4283
4284 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004285 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286
4287 /*
4288 * If there is a comparative operator, use it.
4289 */
4290 if (type != EXPR_UNKNOWN)
4291 {
4292 int ic = FALSE; // Default: do not ignore case
4293
4294 if (type_is && (p[len] == '?' || p[len] == '#'))
4295 {
4296 semsg(_(e_invexpr2), *arg);
4297 return FAIL;
4298 }
4299 // extra question mark appended: ignore case
4300 if (p[len] == '?')
4301 {
4302 ic = TRUE;
4303 ++len;
4304 }
4305 // extra '#' appended: match case (ignored)
4306 else if (p[len] == '#')
4307 ++len;
4308 // nothing appended: match case
4309
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004310 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 {
4312 char_u buf[7];
4313
4314 vim_strncpy(buf, p, len);
4315 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004316 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 }
4318
4319 // get the second variable
4320 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004321 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004322 return FAIL;
4323
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 if (compile_expr5(arg, cctx) == FAIL)
4325 return FAIL;
4326
4327 generate_COMPARE(cctx, type, ic);
4328 }
4329
4330 return OK;
4331}
4332
4333/*
4334 * Compile || or &&.
4335 */
4336 static int
4337compile_and_or(char_u **arg, cctx_T *cctx, char *op)
4338{
4339 char_u *p = skipwhite(*arg);
4340 int opchar = *op;
4341
4342 if (p[0] == opchar && p[1] == opchar)
4343 {
4344 garray_T *instr = &cctx->ctx_instr;
4345 garray_T end_ga;
4346
4347 /*
4348 * Repeat until there is no following "||" or "&&"
4349 */
4350 ga_init2(&end_ga, sizeof(int), 10);
4351 while (p[0] == opchar && p[1] == opchar)
4352 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004353 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4354 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004356 return FAIL;
4357 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358
4359 if (ga_grow(&end_ga, 1) == FAIL)
4360 {
4361 ga_clear(&end_ga);
4362 return FAIL;
4363 }
4364 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4365 ++end_ga.ga_len;
4366 generate_JUMP(cctx, opchar == '|'
4367 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4368
4369 // eval the next expression
4370 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004371 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004372 return FAIL;
4373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374 if ((opchar == '|' ? compile_expr3(arg, cctx)
4375 : compile_expr4(arg, cctx)) == FAIL)
4376 {
4377 ga_clear(&end_ga);
4378 return FAIL;
4379 }
4380 p = skipwhite(*arg);
4381 }
4382
4383 // Fill in the end label in all jumps.
4384 while (end_ga.ga_len > 0)
4385 {
4386 isn_T *isn;
4387
4388 --end_ga.ga_len;
4389 isn = ((isn_T *)instr->ga_data)
4390 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4391 isn->isn_arg.jump.jump_where = instr->ga_len;
4392 }
4393 ga_clear(&end_ga);
4394 }
4395
4396 return OK;
4397}
4398
4399/*
4400 * expr4a && expr4a && expr4a logical AND
4401 *
4402 * Produces instructions:
4403 * EVAL expr4a Push result of "expr4a"
4404 * JUMP_AND_KEEP_IF_FALSE end
4405 * EVAL expr4b Push result of "expr4b"
4406 * JUMP_AND_KEEP_IF_FALSE end
4407 * EVAL expr4c Push result of "expr4c"
4408 * end:
4409 */
4410 static int
4411compile_expr3(char_u **arg, cctx_T *cctx)
4412{
4413 // get the first variable
4414 if (compile_expr4(arg, cctx) == FAIL)
4415 return FAIL;
4416
4417 // || and && work almost the same
4418 return compile_and_or(arg, cctx, "&&");
4419}
4420
4421/*
4422 * expr3a || expr3b || expr3c logical OR
4423 *
4424 * Produces instructions:
4425 * EVAL expr3a Push result of "expr3a"
4426 * JUMP_AND_KEEP_IF_TRUE end
4427 * EVAL expr3b Push result of "expr3b"
4428 * JUMP_AND_KEEP_IF_TRUE end
4429 * EVAL expr3c Push result of "expr3c"
4430 * end:
4431 */
4432 static int
4433compile_expr2(char_u **arg, cctx_T *cctx)
4434{
4435 // eval the first expression
4436 if (compile_expr3(arg, cctx) == FAIL)
4437 return FAIL;
4438
4439 // || and && work almost the same
4440 return compile_and_or(arg, cctx, "||");
4441}
4442
4443/*
4444 * Toplevel expression: expr2 ? expr1a : expr1b
4445 *
4446 * Produces instructions:
4447 * EVAL expr2 Push result of "expr"
4448 * JUMP_IF_FALSE alt jump if false
4449 * EVAL expr1a
4450 * JUMP_ALWAYS end
4451 * alt: EVAL expr1b
4452 * end:
4453 */
4454 static int
4455compile_expr1(char_u **arg, cctx_T *cctx)
4456{
4457 char_u *p;
4458
Bram Moolenaar61a89812020-05-07 16:58:17 +02004459 // Evaluate the first expression.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004460 if (compile_expr2(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461 return FAIL;
4462
4463 p = skipwhite(*arg);
4464 if (*p == '?')
4465 {
4466 garray_T *instr = &cctx->ctx_instr;
4467 garray_T *stack = &cctx->ctx_type_stack;
4468 int alt_idx = instr->ga_len;
4469 int end_idx;
4470 isn_T *isn;
4471 type_T *type1;
4472 type_T *type2;
4473
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004474 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4475 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004477 return FAIL;
4478 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479
4480 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4481
4482 // evaluate the second expression; any type is accepted
4483 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004484 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004485 return FAIL;
4486
Bram Moolenaara6d53682020-01-28 23:04:06 +01004487 if (compile_expr1(arg, cctx) == FAIL)
4488 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489
4490 // remember the type and drop it
4491 --stack->ga_len;
4492 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
4493
4494 end_idx = instr->ga_len;
4495 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4496
4497 // jump here from JUMP_IF_FALSE
4498 isn = ((isn_T *)instr->ga_data) + alt_idx;
4499 isn->isn_arg.jump.jump_where = instr->ga_len;
4500
4501 // Check for the ":".
4502 p = skipwhite(*arg);
4503 if (*p != ':')
4504 {
4505 emsg(_(e_missing_colon));
4506 return FAIL;
4507 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004508 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4509 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004511 return FAIL;
4512 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513
4514 // evaluate the third expression
4515 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004516 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004517 return FAIL;
4518
Bram Moolenaara6d53682020-01-28 23:04:06 +01004519 if (compile_expr1(arg, cctx) == FAIL)
4520 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521
4522 // If the types differ, the result has a more generic type.
4523 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004524 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525
4526 // jump here from JUMP_ALWAYS
4527 isn = ((isn_T *)instr->ga_data) + end_idx;
4528 isn->isn_arg.jump.jump_where = instr->ga_len;
4529 }
4530 return OK;
4531}
4532
4533/*
4534 * compile "return [expr]"
4535 */
4536 static char_u *
4537compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4538{
4539 char_u *p = arg;
4540 garray_T *stack = &cctx->ctx_type_stack;
4541 type_T *stack_type;
4542
4543 if (*p != NUL && *p != '|' && *p != '\n')
4544 {
4545 // compile return argument into instructions
4546 if (compile_expr1(&p, cctx) == FAIL)
4547 return NULL;
4548
4549 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4550 if (set_return_type)
4551 cctx->ctx_ufunc->uf_ret_type = stack_type;
4552 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4553 == FAIL)
4554 return NULL;
4555 }
4556 else
4557 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004558 // "set_return_type" cannot be TRUE, only used for a lambda which
4559 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004560 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4561 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 {
4563 emsg(_("E1003: Missing return value"));
4564 return NULL;
4565 }
4566
4567 // No argument, return zero.
4568 generate_PUSHNR(cctx, 0);
4569 }
4570
4571 if (generate_instr(cctx, ISN_RETURN) == NULL)
4572 return NULL;
4573
4574 // "return val | endif" is possible
4575 return skipwhite(p);
4576}
4577
4578/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004579 * Get a line from the compilation context, compatible with exarg_T getline().
4580 * Return a pointer to the line in allocated memory.
4581 * Return NULL for end-of-file or some error.
4582 */
4583 static char_u *
4584exarg_getline(
4585 int c UNUSED,
4586 void *cookie,
4587 int indent UNUSED,
4588 int do_concat UNUSED)
4589{
4590 cctx_T *cctx = (cctx_T *)cookie;
4591
4592 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4593 {
4594 iemsg("Heredoc got to end");
4595 return NULL;
4596 }
4597 ++cctx->ctx_lnum;
4598 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4599 [cctx->ctx_lnum]);
4600}
4601
4602/*
4603 * Compile a nested :def command.
4604 */
4605 static char_u *
4606compile_nested_function(exarg_T *eap, cctx_T *cctx)
4607{
4608 char_u *name_start = eap->arg;
4609 char_u *name_end = to_name_end(eap->arg, FALSE);
4610 char_u *name = get_lambda_name();
4611 lvar_T *lvar;
4612 ufunc_T *ufunc;
4613
4614 eap->arg = name_end;
4615 eap->getline = exarg_getline;
4616 eap->cookie = cctx;
4617 eap->skip = cctx->ctx_skip == TRUE;
4618 eap->forceit = FALSE;
4619 ufunc = def_function(eap, name, cctx);
4620
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004621 if (ufunc == NULL || ufunc->uf_dfunc_idx < 0)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004622 return NULL;
4623
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004624 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004625 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004626 TRUE, ufunc->uf_func_type);
4627
4628 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4629 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4630 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004631
Bram Moolenaar61a89812020-05-07 16:58:17 +02004632 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004633 return (char_u *)"";
4634}
4635
4636/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637 * Return the length of an assignment operator, or zero if there isn't one.
4638 */
4639 int
4640assignment_len(char_u *p, int *heredoc)
4641{
4642 if (*p == '=')
4643 {
4644 if (p[1] == '<' && p[2] == '<')
4645 {
4646 *heredoc = TRUE;
4647 return 3;
4648 }
4649 return 1;
4650 }
4651 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4652 return 2;
4653 if (STRNCMP(p, "..=", 3) == 0)
4654 return 3;
4655 return 0;
4656}
4657
4658// words that cannot be used as a variable
4659static char *reserved[] = {
4660 "true",
4661 "false",
4662 NULL
4663};
4664
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004665typedef enum {
4666 dest_local,
4667 dest_option,
4668 dest_env,
4669 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004670 dest_buffer,
4671 dest_window,
4672 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004673 dest_vimvar,
4674 dest_script,
4675 dest_reg,
4676} assign_dest_T;
4677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678/*
4679 * compile "let var [= expr]", "const var = expr" and "var = expr"
4680 * "arg" points to "var".
4681 */
4682 static char_u *
4683compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4684{
4685 char_u *p;
4686 char_u *ret = NULL;
4687 int var_count = 0;
4688 int semicolon = 0;
4689 size_t varlen;
4690 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004691 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004694 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004696 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 int oplen = 0;
4698 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004699 type_T *type = &t_any;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004700 lvar_T *lvar = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701 char_u *name;
4702 char_u *sp;
4703 int has_type = FALSE;
4704 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
4705 int instr_count = -1;
4706
4707 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
4708 if (p == NULL)
4709 return NULL;
4710 if (var_count > 0)
4711 {
4712 // TODO: let [var, var] = list
4713 emsg("Cannot handle a list yet");
4714 return NULL;
4715 }
4716
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004717 // "a: type" is declaring variable "a" with a type, not "a:".
4718 if (is_decl && p == arg + 2 && p[-1] == ':')
4719 --p;
4720
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 varlen = p - arg;
4722 name = vim_strnsave(arg, (int)varlen);
4723 if (name == NULL)
4724 return NULL;
4725
Bram Moolenaar080457c2020-03-03 21:53:32 +01004726 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004728 if (*arg == '&')
4729 {
4730 int cc;
4731 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732
Bram Moolenaar080457c2020-03-03 21:53:32 +01004733 dest = dest_option;
4734 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004736 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004737 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739 if (is_decl)
4740 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004741 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 goto theend;
4743 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004744 p = arg;
4745 p = find_option_end(&p, &opt_flags);
4746 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004747 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004748 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01004749 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004750 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004751 }
4752 cc = *p;
4753 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004754 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004755 *p = cc;
4756 if (opt_type == -3)
4757 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004758 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004759 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004760 }
4761 if (opt_type == -2 || opt_type == 0)
4762 type = &t_string;
4763 else
4764 type = &t_number; // both number and boolean option
4765 }
4766 else if (*arg == '$')
4767 {
4768 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004769 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004770 if (is_decl)
4771 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004772 semsg(_("E1065: Cannot declare an environment variable: %s"),
4773 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004774 goto theend;
4775 }
4776 }
4777 else if (*arg == '@')
4778 {
4779 if (!valid_yank_reg(arg[1], TRUE))
4780 {
4781 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004782 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004783 }
4784 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004785 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004786 if (is_decl)
4787 {
4788 semsg(_("E1066: Cannot declare a register: %s"), name);
4789 goto theend;
4790 }
4791 }
4792 else if (STRNCMP(arg, "g:", 2) == 0)
4793 {
4794 dest = dest_global;
4795 if (is_decl)
4796 {
4797 semsg(_("E1016: Cannot declare a global variable: %s"), name);
4798 goto theend;
4799 }
4800 }
Bram Moolenaard3aac292020-04-19 14:32:17 +02004801 else if (STRNCMP(arg, "b:", 2) == 0)
4802 {
4803 dest = dest_buffer;
4804 if (is_decl)
4805 {
4806 semsg(_("E1078: Cannot declare a buffer variable: %s"), name);
4807 goto theend;
4808 }
4809 }
4810 else if (STRNCMP(arg, "w:", 2) == 0)
4811 {
4812 dest = dest_window;
4813 if (is_decl)
4814 {
4815 semsg(_("E1079: Cannot declare a window variable: %s"), name);
4816 goto theend;
4817 }
4818 }
4819 else if (STRNCMP(arg, "t:", 2) == 0)
4820 {
4821 dest = dest_tab;
4822 if (is_decl)
4823 {
4824 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
4825 goto theend;
4826 }
4827 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004828 else if (STRNCMP(arg, "v:", 2) == 0)
4829 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004830 typval_T *vtv;
4831 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004832
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004833 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004834 if (vimvaridx < 0)
4835 {
4836 semsg(_(e_var_notfound), arg);
4837 goto theend;
4838 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004839 // We use the current value of "sandbox" here, is that OK?
4840 if (var_check_ro(di_flags, name, FALSE))
4841 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004842 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004843 vtv = get_vim_var_tv(vimvaridx);
4844 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004845 if (is_decl)
4846 {
4847 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
4848 goto theend;
4849 }
4850 }
4851 else
4852 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004853 int idx;
4854
Bram Moolenaar080457c2020-03-03 21:53:32 +01004855 for (idx = 0; reserved[idx] != NULL; ++idx)
4856 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004858 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004859 goto theend;
4860 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004861
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004862 lvar = lookup_local(arg, varlen, cctx);
4863 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004865 if (is_decl)
4866 {
4867 semsg(_("E1017: Variable already declared: %s"), name);
4868 goto theend;
4869 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004870 else if (lvar->lv_const)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004871 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004872 semsg(_("E1018: Cannot assign to a constant: %s"), name);
4873 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004874 }
4875 }
4876 else if (STRNCMP(arg, "s:", 2) == 0
4877 || lookup_script(arg, varlen) == OK
4878 || find_imported(arg, varlen, cctx) != NULL)
4879 {
4880 dest = dest_script;
4881 if (is_decl)
4882 {
4883 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004884 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004885 goto theend;
4886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004888 else if (name[1] == ':' && name[2] != NUL)
4889 {
4890 semsg(_("E1082: Cannot use a namespaced variable: %s"), name);
4891 goto theend;
4892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 }
4894 }
4895
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004896 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 {
4898 if (is_decl && *p == ':')
4899 {
4900 // parse optional type: "let var: type = expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004901 if (!VIM_ISWHITE(p[1]))
4902 {
4903 semsg(_(e_white_after), ":");
4904 goto theend;
4905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906 p = skipwhite(p + 1);
4907 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908 has_type = TRUE;
4909 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004910 else if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911 type = lvar->lv_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 }
4913
4914 sp = p;
4915 p = skipwhite(p);
4916 op = p;
4917 oplen = assignment_len(p, &heredoc);
4918 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4919 {
4920 char_u buf[4];
4921
4922 vim_strncpy(buf, op, oplen);
4923 semsg(_(e_white_both), buf);
4924 }
4925
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004926 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02004927 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004929 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930 goto theend;
4931 }
4932
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004933 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934 {
4935 if (oplen > 1 && !heredoc)
4936 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004937 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4939 name);
4940 goto theend;
4941 }
4942
4943 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02004944 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004945 goto theend;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004946 lvar = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4947 if (lvar == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004949 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950 }
4951
4952 if (heredoc)
4953 {
4954 list_T *l;
4955 listitem_T *li;
4956
4957 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004958 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004960 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961
4962 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004963 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964 {
4965 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4966 li->li_tv.vval.v_string = NULL;
4967 }
4968 generate_NEWLIST(cctx, l->lv_len);
4969 type = &t_list_string;
4970 list_free(l);
4971 p += STRLEN(p);
4972 }
4973 else if (oplen > 0)
4974 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004975 int r;
4976 type_T *stacktype;
4977 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004978
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979 // for "+=", "*=", "..=" etc. first load the current value
4980 if (*op != '=')
4981 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004982 switch (dest)
4983 {
4984 case dest_option:
4985 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004986 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004987 break;
4988 case dest_global:
4989 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4990 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004991 case dest_buffer:
4992 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4993 break;
4994 case dest_window:
4995 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4996 break;
4997 case dest_tab:
4998 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4999 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005000 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01005001 compile_load_scriptvar(cctx,
5002 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005003 break;
5004 case dest_env:
5005 // Include $ in the name here
5006 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5007 break;
5008 case dest_reg:
5009 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
5010 break;
5011 case dest_vimvar:
5012 generate_LOADV(cctx, name + 2, TRUE);
5013 break;
5014 case dest_local:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005015 if (lvar->lv_from_outer)
5016 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5017 NULL, type);
5018 else
5019 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005020 break;
5021 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005022 }
5023
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005024 // Compile the expression. Temporarily hide the new local variable
5025 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02005026 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005027 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028 instr_count = instr->ga_len;
5029 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005030 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02005031 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005032 ++cctx->ctx_locals.ga_len;
5033 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005034 goto theend;
5035
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005036 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005037 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005038 stack = &cctx->ctx_type_stack;
5039 stacktype = stack->ga_len == 0 ? &t_void
5040 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005041 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005043 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005045 if (stacktype->tt_type == VAR_VOID)
5046 {
5047 emsg(_("E1031: Cannot use void value"));
5048 goto theend;
5049 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005050 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005051 {
5052 // An empty list or dict has a &t_void member, for a
5053 // variable that implies &t_any.
5054 if (stacktype == &t_list_empty)
5055 lvar->lv_type = &t_list_any;
5056 else if (stacktype == &t_dict_empty)
5057 lvar->lv_type = &t_dict_any;
5058 else
5059 lvar->lv_type = stacktype;
5060 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005061 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005062 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
5063 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005064 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005065 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005066 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067 }
5068 }
5069 else if (cmdidx == CMD_const)
5070 {
5071 emsg(_("E1021: const requires a value"));
5072 goto theend;
5073 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005074 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075 {
5076 emsg(_("E1022: type or initialization required"));
5077 goto theend;
5078 }
5079 else
5080 {
5081 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082 if (ga_grow(instr, 1) == FAIL)
5083 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005084 switch (type->tt_type)
5085 {
5086 case VAR_BOOL:
5087 generate_PUSHBOOL(cctx, VVAL_FALSE);
5088 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005089 case VAR_FLOAT:
5090#ifdef FEAT_FLOAT
5091 generate_PUSHF(cctx, 0.0);
5092#endif
5093 break;
5094 case VAR_STRING:
5095 generate_PUSHS(cctx, NULL);
5096 break;
5097 case VAR_BLOB:
5098 generate_PUSHBLOB(cctx, NULL);
5099 break;
5100 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005101 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005102 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005103 case VAR_LIST:
5104 generate_NEWLIST(cctx, 0);
5105 break;
5106 case VAR_DICT:
5107 generate_NEWDICT(cctx, 0);
5108 break;
5109 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005110 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005111 break;
5112 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005113 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005114 break;
5115 case VAR_NUMBER:
5116 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005117 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02005118 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01005119 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02005120 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01005121 generate_PUSHNR(cctx, 0);
5122 break;
5123 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005124 }
5125
5126 if (oplen > 0 && *op != '=')
5127 {
5128 type_T *expected = &t_number;
5129 garray_T *stack = &cctx->ctx_type_stack;
5130 type_T *stacktype;
5131
5132 // TODO: if type is known use float or any operation
5133
5134 if (*op == '.')
5135 expected = &t_string;
5136 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5137 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5138 goto theend;
5139
5140 if (*op == '.')
5141 generate_instr_drop(cctx, ISN_CONCAT, 1);
5142 else
5143 {
5144 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5145
5146 if (isn == NULL)
5147 goto theend;
5148 switch (*op)
5149 {
5150 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5151 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5152 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5153 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5154 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5155 }
5156 }
5157 }
5158
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005159 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005160 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005161 case dest_option:
5162 generate_STOREOPT(cctx, name + 1, opt_flags);
5163 break;
5164 case dest_global:
5165 // include g: with the name, easier to execute that way
5166 generate_STORE(cctx, ISN_STOREG, 0, name);
5167 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02005168 case dest_buffer:
5169 // include b: with the name, easier to execute that way
5170 generate_STORE(cctx, ISN_STOREB, 0, name);
5171 break;
5172 case dest_window:
5173 // include w: with the name, easier to execute that way
5174 generate_STORE(cctx, ISN_STOREW, 0, name);
5175 break;
5176 case dest_tab:
5177 // include t: with the name, easier to execute that way
5178 generate_STORE(cctx, ISN_STORET, 0, name);
5179 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005180 case dest_env:
5181 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5182 break;
5183 case dest_reg:
5184 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5185 break;
5186 case dest_vimvar:
5187 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5188 break;
5189 case dest_script:
5190 {
5191 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5192 imported_T *import = NULL;
5193 int sid = current_sctx.sc_sid;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005194 int idx;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005195
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005196 if (name[1] != ':')
5197 {
5198 import = find_imported(name, 0, cctx);
5199 if (import != NULL)
5200 sid = import->imp_sid;
5201 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005203 idx = get_script_item_idx(sid, rawname, TRUE);
5204 // TODO: specific type
5205 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005206 {
5207 char_u *name_s = name;
5208
5209 // Include s: in the name for store_var()
5210 if (name[1] != ':')
5211 {
5212 int len = (int)STRLEN(name) + 3;
5213
5214 name_s = alloc(len);
5215 if (name_s == NULL)
5216 name_s = name;
5217 else
5218 vim_snprintf((char *)name_s, len, "s:%s", name);
5219 }
5220 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
5221 if (name_s != name)
5222 vim_free(name_s);
5223 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005224 else
5225 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5226 sid, idx, &t_any);
5227 }
5228 break;
5229 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005230 if (lvar != NULL)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005231 {
5232 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005234 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
5235 // into ISN_STORENR
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005236 if (!lvar->lv_from_outer && instr->ga_len == instr_count + 1
5237 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005238 {
5239 varnumber_T val = isn->isn_arg.number;
5240 garray_T *stack = &cctx->ctx_type_stack;
5241
5242 isn->isn_type = ISN_STORENR;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005243 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
Bram Moolenaara471eea2020-03-04 22:20:26 +01005244 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005245 if (stack->ga_len > 0)
5246 --stack->ga_len;
5247 }
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005248 else if (lvar->lv_from_outer)
5249 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005250 else
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005251 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005252 }
5253 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005254 }
5255 ret = p;
5256
5257theend:
5258 vim_free(name);
5259 return ret;
5260}
5261
5262/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005263 * Check if "name" can be "unlet".
5264 */
5265 int
5266check_vim9_unlet(char_u *name)
5267{
5268 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5269 {
5270 semsg(_("E1081: Cannot unlet %s"), name);
5271 return FAIL;
5272 }
5273 return OK;
5274}
5275
5276/*
5277 * Callback passed to ex_unletlock().
5278 */
5279 static int
5280compile_unlet(
5281 lval_T *lvp,
5282 char_u *name_end,
5283 exarg_T *eap,
5284 int deep UNUSED,
5285 void *coookie)
5286{
5287 cctx_T *cctx = coookie;
5288
5289 if (lvp->ll_tv == NULL)
5290 {
5291 char_u *p = lvp->ll_name;
5292 int cc = *name_end;
5293 int ret = OK;
5294
5295 // Normal name. Only supports g:, w:, t: and b: namespaces.
5296 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005297 if (*p == '$')
5298 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5299 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005300 ret = FAIL;
5301 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005302 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005303
5304 *name_end = cc;
5305 return ret;
5306 }
5307
5308 // TODO: unlet {list}[idx]
5309 // TODO: unlet {dict}[key]
5310 emsg("Sorry, :unlet not fully implemented yet");
5311 return FAIL;
5312}
5313
5314/*
5315 * compile "unlet var", "lock var" and "unlock var"
5316 * "arg" points to "var".
5317 */
5318 static char_u *
5319compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5320{
5321 char_u *p = arg;
5322
5323 if (eap->cmdidx != CMD_unlet)
5324 {
5325 emsg("Sorry, :lock and unlock not implemented yet");
5326 return NULL;
5327 }
5328
5329 if (*p == '!')
5330 {
5331 p = skipwhite(p + 1);
5332 eap->forceit = TRUE;
5333 }
5334
5335 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5336 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5337}
5338
5339/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005340 * Compile an :import command.
5341 */
5342 static char_u *
5343compile_import(char_u *arg, cctx_T *cctx)
5344{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01005345 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005346}
5347
5348/*
5349 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5350 */
5351 static int
5352compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5353{
5354 garray_T *instr = &cctx->ctx_instr;
5355 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5356
5357 if (endlabel == NULL)
5358 return FAIL;
5359 endlabel->el_next = *el;
5360 *el = endlabel;
5361 endlabel->el_end_label = instr->ga_len;
5362
5363 generate_JUMP(cctx, when, 0);
5364 return OK;
5365}
5366
5367 static void
5368compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5369{
5370 garray_T *instr = &cctx->ctx_instr;
5371
5372 while (*el != NULL)
5373 {
5374 endlabel_T *cur = (*el);
5375 isn_T *isn;
5376
5377 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5378 isn->isn_arg.jump.jump_where = instr->ga_len;
5379 *el = cur->el_next;
5380 vim_free(cur);
5381 }
5382}
5383
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005384 static void
5385compile_free_jump_to_end(endlabel_T **el)
5386{
5387 while (*el != NULL)
5388 {
5389 endlabel_T *cur = (*el);
5390
5391 *el = cur->el_next;
5392 vim_free(cur);
5393 }
5394}
5395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005396/*
5397 * Create a new scope and set up the generic items.
5398 */
5399 static scope_T *
5400new_scope(cctx_T *cctx, scopetype_T type)
5401{
5402 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5403
5404 if (scope == NULL)
5405 return NULL;
5406 scope->se_outer = cctx->ctx_scope;
5407 cctx->ctx_scope = scope;
5408 scope->se_type = type;
5409 scope->se_local_count = cctx->ctx_locals.ga_len;
5410 return scope;
5411}
5412
5413/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005414 * Free the current scope and go back to the outer scope.
5415 */
5416 static void
5417drop_scope(cctx_T *cctx)
5418{
5419 scope_T *scope = cctx->ctx_scope;
5420
5421 if (scope == NULL)
5422 {
5423 iemsg("calling drop_scope() without a scope");
5424 return;
5425 }
5426 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005427 switch (scope->se_type)
5428 {
5429 case IF_SCOPE:
5430 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5431 case FOR_SCOPE:
5432 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5433 case WHILE_SCOPE:
5434 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5435 case TRY_SCOPE:
5436 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5437 case NO_SCOPE:
5438 case BLOCK_SCOPE:
5439 break;
5440 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005441 vim_free(scope);
5442}
5443
5444/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005445 * compile "if expr"
5446 *
5447 * "if expr" Produces instructions:
5448 * EVAL expr Push result of "expr"
5449 * JUMP_IF_FALSE end
5450 * ... body ...
5451 * end:
5452 *
5453 * "if expr | else" Produces instructions:
5454 * EVAL expr Push result of "expr"
5455 * JUMP_IF_FALSE else
5456 * ... body ...
5457 * JUMP_ALWAYS end
5458 * else:
5459 * ... body ...
5460 * end:
5461 *
5462 * "if expr1 | elseif expr2 | else" Produces instructions:
5463 * EVAL expr Push result of "expr"
5464 * JUMP_IF_FALSE elseif
5465 * ... body ...
5466 * JUMP_ALWAYS end
5467 * elseif:
5468 * EVAL expr Push result of "expr"
5469 * JUMP_IF_FALSE else
5470 * ... body ...
5471 * JUMP_ALWAYS end
5472 * else:
5473 * ... body ...
5474 * end:
5475 */
5476 static char_u *
5477compile_if(char_u *arg, cctx_T *cctx)
5478{
5479 char_u *p = arg;
5480 garray_T *instr = &cctx->ctx_instr;
5481 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005482 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005483
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005484 // compile "expr"; if we know it evaluates to FALSE skip the block
5485 tv.v_type = VAR_UNKNOWN;
5486 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5487 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5488 else
5489 cctx->ctx_skip = MAYBE;
5490 clear_tv(&tv);
5491 if (cctx->ctx_skip == MAYBE)
5492 {
5493 p = arg;
5494 if (compile_expr1(&p, cctx) == FAIL)
5495 return NULL;
5496 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497
5498 scope = new_scope(cctx, IF_SCOPE);
5499 if (scope == NULL)
5500 return NULL;
5501
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005502 if (cctx->ctx_skip == MAYBE)
5503 {
5504 // "where" is set when ":elseif", "else" or ":endif" is found
5505 scope->se_u.se_if.is_if_label = instr->ga_len;
5506 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5507 }
5508 else
5509 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005510
5511 return p;
5512}
5513
5514 static char_u *
5515compile_elseif(char_u *arg, cctx_T *cctx)
5516{
5517 char_u *p = arg;
5518 garray_T *instr = &cctx->ctx_instr;
5519 isn_T *isn;
5520 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005521 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005522
5523 if (scope == NULL || scope->se_type != IF_SCOPE)
5524 {
5525 emsg(_(e_elseif_without_if));
5526 return NULL;
5527 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005528 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005529
Bram Moolenaar158906c2020-02-06 20:39:45 +01005530 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005531 {
5532 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005533 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005534 return NULL;
5535 // previous "if" or "elseif" jumps here
5536 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5537 isn->isn_arg.jump.jump_where = instr->ga_len;
5538 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005539
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005540 // compile "expr"; if we know it evaluates to FALSE skip the block
5541 tv.v_type = VAR_UNKNOWN;
5542 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5543 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5544 else
5545 cctx->ctx_skip = MAYBE;
5546 clear_tv(&tv);
5547 if (cctx->ctx_skip == MAYBE)
5548 {
5549 p = arg;
5550 if (compile_expr1(&p, cctx) == FAIL)
5551 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005552
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005553 // "where" is set when ":elseif", "else" or ":endif" is found
5554 scope->se_u.se_if.is_if_label = instr->ga_len;
5555 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5556 }
5557 else
5558 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005559
5560 return p;
5561}
5562
5563 static char_u *
5564compile_else(char_u *arg, cctx_T *cctx)
5565{
5566 char_u *p = arg;
5567 garray_T *instr = &cctx->ctx_instr;
5568 isn_T *isn;
5569 scope_T *scope = cctx->ctx_scope;
5570
5571 if (scope == NULL || scope->se_type != IF_SCOPE)
5572 {
5573 emsg(_(e_else_without_if));
5574 return NULL;
5575 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005576 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005577
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005578 // jump from previous block to the end, unless the else block is empty
5579 if (cctx->ctx_skip == MAYBE)
5580 {
5581 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005582 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005583 return NULL;
5584 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585
Bram Moolenaar158906c2020-02-06 20:39:45 +01005586 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005587 {
5588 if (scope->se_u.se_if.is_if_label >= 0)
5589 {
5590 // previous "if" or "elseif" jumps here
5591 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5592 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005593 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005594 }
5595 }
5596
5597 if (cctx->ctx_skip != MAYBE)
5598 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005599
5600 return p;
5601}
5602
5603 static char_u *
5604compile_endif(char_u *arg, cctx_T *cctx)
5605{
5606 scope_T *scope = cctx->ctx_scope;
5607 ifscope_T *ifscope;
5608 garray_T *instr = &cctx->ctx_instr;
5609 isn_T *isn;
5610
5611 if (scope == NULL || scope->se_type != IF_SCOPE)
5612 {
5613 emsg(_(e_endif_without_if));
5614 return NULL;
5615 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005616 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005617 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005618
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005619 if (scope->se_u.se_if.is_if_label >= 0)
5620 {
5621 // previous "if" or "elseif" jumps here
5622 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5623 isn->isn_arg.jump.jump_where = instr->ga_len;
5624 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005625 // Fill in the "end" label in jumps at the end of the blocks.
5626 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005627 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005628
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005629 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005630 return arg;
5631}
5632
5633/*
5634 * compile "for var in expr"
5635 *
5636 * Produces instructions:
5637 * PUSHNR -1
5638 * STORE loop-idx Set index to -1
5639 * EVAL expr Push result of "expr"
5640 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5641 * - if beyond end, jump to "end"
5642 * - otherwise get item from list and push it
5643 * STORE var Store item in "var"
5644 * ... body ...
5645 * JUMP top Jump back to repeat
5646 * end: DROP Drop the result of "expr"
5647 *
5648 */
5649 static char_u *
5650compile_for(char_u *arg, cctx_T *cctx)
5651{
5652 char_u *p;
5653 size_t varlen;
5654 garray_T *instr = &cctx->ctx_instr;
5655 garray_T *stack = &cctx->ctx_type_stack;
5656 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005657 lvar_T *loop_lvar; // loop iteration variable
5658 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005659 type_T *vartype;
5660
5661 // TODO: list of variables: "for [key, value] in dict"
5662 // parse "var"
5663 for (p = arg; eval_isnamec1(*p); ++p)
5664 ;
5665 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005666 var_lvar = lookup_local(arg, varlen, cctx);
5667 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005668 {
5669 semsg(_("E1023: variable already defined: %s"), arg);
5670 return NULL;
5671 }
5672
5673 // consume "in"
5674 p = skipwhite(p);
5675 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5676 {
5677 emsg(_(e_missing_in));
5678 return NULL;
5679 }
5680 p = skipwhite(p + 2);
5681
5682
5683 scope = new_scope(cctx, FOR_SCOPE);
5684 if (scope == NULL)
5685 return NULL;
5686
5687 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005688 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5689 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005690 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005691 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005692 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005693 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005694 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005695
5696 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005697 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5698 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005699 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005700 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005701 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005702 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005703 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005704
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005705 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005706
5707 // compile "expr", it remains on the stack until "endfor"
5708 arg = p;
5709 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005710 {
5711 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005712 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005714
5715 // now we know the type of "var"
5716 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5717 if (vartype->tt_type != VAR_LIST)
5718 {
5719 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005720 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005721 return NULL;
5722 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005723 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005724 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005725
5726 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005727 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005728
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005729 generate_FOR(cctx, loop_lvar->lv_idx);
5730 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005731
5732 return arg;
5733}
5734
5735/*
5736 * compile "endfor"
5737 */
5738 static char_u *
5739compile_endfor(char_u *arg, cctx_T *cctx)
5740{
5741 garray_T *instr = &cctx->ctx_instr;
5742 scope_T *scope = cctx->ctx_scope;
5743 forscope_T *forscope;
5744 isn_T *isn;
5745
5746 if (scope == NULL || scope->se_type != FOR_SCOPE)
5747 {
5748 emsg(_(e_for));
5749 return NULL;
5750 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005751 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005752 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005753 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005754
5755 // At end of ":for" scope jump back to the FOR instruction.
5756 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5757
5758 // Fill in the "end" label in the FOR statement so it can jump here
5759 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5760 isn->isn_arg.forloop.for_end = instr->ga_len;
5761
5762 // Fill in the "end" label any BREAK statements
5763 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5764
5765 // Below the ":for" scope drop the "expr" list from the stack.
5766 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5767 return NULL;
5768
5769 vim_free(scope);
5770
5771 return arg;
5772}
5773
5774/*
5775 * compile "while expr"
5776 *
5777 * Produces instructions:
5778 * top: EVAL expr Push result of "expr"
5779 * JUMP_IF_FALSE end jump if false
5780 * ... body ...
5781 * JUMP top Jump back to repeat
5782 * end:
5783 *
5784 */
5785 static char_u *
5786compile_while(char_u *arg, cctx_T *cctx)
5787{
5788 char_u *p = arg;
5789 garray_T *instr = &cctx->ctx_instr;
5790 scope_T *scope;
5791
5792 scope = new_scope(cctx, WHILE_SCOPE);
5793 if (scope == NULL)
5794 return NULL;
5795
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005796 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005797
5798 // compile "expr"
5799 if (compile_expr1(&p, cctx) == FAIL)
5800 return NULL;
5801
5802 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005803 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005804 JUMP_IF_FALSE, cctx) == FAIL)
5805 return FAIL;
5806
5807 return p;
5808}
5809
5810/*
5811 * compile "endwhile"
5812 */
5813 static char_u *
5814compile_endwhile(char_u *arg, cctx_T *cctx)
5815{
5816 scope_T *scope = cctx->ctx_scope;
5817
5818 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5819 {
5820 emsg(_(e_while));
5821 return NULL;
5822 }
5823 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005824 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005825
5826 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005827 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005828
5829 // Fill in the "end" label in the WHILE statement so it can jump here.
5830 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005831 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005832
5833 vim_free(scope);
5834
5835 return arg;
5836}
5837
5838/*
5839 * compile "continue"
5840 */
5841 static char_u *
5842compile_continue(char_u *arg, cctx_T *cctx)
5843{
5844 scope_T *scope = cctx->ctx_scope;
5845
5846 for (;;)
5847 {
5848 if (scope == NULL)
5849 {
5850 emsg(_(e_continue));
5851 return NULL;
5852 }
5853 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5854 break;
5855 scope = scope->se_outer;
5856 }
5857
5858 // Jump back to the FOR or WHILE instruction.
5859 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005860 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5861 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005862 return arg;
5863}
5864
5865/*
5866 * compile "break"
5867 */
5868 static char_u *
5869compile_break(char_u *arg, cctx_T *cctx)
5870{
5871 scope_T *scope = cctx->ctx_scope;
5872 endlabel_T **el;
5873
5874 for (;;)
5875 {
5876 if (scope == NULL)
5877 {
5878 emsg(_(e_break));
5879 return NULL;
5880 }
5881 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5882 break;
5883 scope = scope->se_outer;
5884 }
5885
5886 // Jump to the end of the FOR or WHILE loop.
5887 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005888 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005889 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005890 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005891 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5892 return FAIL;
5893
5894 return arg;
5895}
5896
5897/*
5898 * compile "{" start of block
5899 */
5900 static char_u *
5901compile_block(char_u *arg, cctx_T *cctx)
5902{
5903 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5904 return NULL;
5905 return skipwhite(arg + 1);
5906}
5907
5908/*
5909 * compile end of block: drop one scope
5910 */
5911 static void
5912compile_endblock(cctx_T *cctx)
5913{
5914 scope_T *scope = cctx->ctx_scope;
5915
5916 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005917 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005918 vim_free(scope);
5919}
5920
5921/*
5922 * compile "try"
5923 * Creates a new scope for the try-endtry, pointing to the first catch and
5924 * finally.
5925 * Creates another scope for the "try" block itself.
5926 * TRY instruction sets up exception handling at runtime.
5927 *
5928 * "try"
5929 * TRY -> catch1, -> finally push trystack entry
5930 * ... try block
5931 * "throw {exception}"
5932 * EVAL {exception}
5933 * THROW create exception
5934 * ... try block
5935 * " catch {expr}"
5936 * JUMP -> finally
5937 * catch1: PUSH exeception
5938 * EVAL {expr}
5939 * MATCH
5940 * JUMP nomatch -> catch2
5941 * CATCH remove exception
5942 * ... catch block
5943 * " catch"
5944 * JUMP -> finally
5945 * catch2: CATCH remove exception
5946 * ... catch block
5947 * " finally"
5948 * finally:
5949 * ... finally block
5950 * " endtry"
5951 * ENDTRY pop trystack entry, may rethrow
5952 */
5953 static char_u *
5954compile_try(char_u *arg, cctx_T *cctx)
5955{
5956 garray_T *instr = &cctx->ctx_instr;
5957 scope_T *try_scope;
5958 scope_T *scope;
5959
5960 // scope that holds the jumps that go to catch/finally/endtry
5961 try_scope = new_scope(cctx, TRY_SCOPE);
5962 if (try_scope == NULL)
5963 return NULL;
5964
5965 // "catch" is set when the first ":catch" is found.
5966 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005967 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005968 if (generate_instr(cctx, ISN_TRY) == NULL)
5969 return NULL;
5970
5971 // scope for the try block itself
5972 scope = new_scope(cctx, BLOCK_SCOPE);
5973 if (scope == NULL)
5974 return NULL;
5975
5976 return arg;
5977}
5978
5979/*
5980 * compile "catch {expr}"
5981 */
5982 static char_u *
5983compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5984{
5985 scope_T *scope = cctx->ctx_scope;
5986 garray_T *instr = &cctx->ctx_instr;
5987 char_u *p;
5988 isn_T *isn;
5989
5990 // end block scope from :try or :catch
5991 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5992 compile_endblock(cctx);
5993 scope = cctx->ctx_scope;
5994
5995 // Error if not in a :try scope
5996 if (scope == NULL || scope->se_type != TRY_SCOPE)
5997 {
5998 emsg(_(e_catch));
5999 return NULL;
6000 }
6001
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006002 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006003 {
6004 emsg(_("E1033: catch unreachable after catch-all"));
6005 return NULL;
6006 }
6007
6008 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006009 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006010 JUMP_ALWAYS, cctx) == FAIL)
6011 return NULL;
6012
6013 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006014 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015 if (isn->isn_arg.try.try_catch == 0)
6016 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006017 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006018 {
6019 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006020 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021 isn->isn_arg.jump.jump_where = instr->ga_len;
6022 }
6023
6024 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006025 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006027 scope->se_u.se_try.ts_caught_all = TRUE;
6028 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006029 }
6030 else
6031 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006032 char_u *end;
6033 char_u *pat;
6034 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006035 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006036 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006037
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006038 // Push v:exception, push {expr} and MATCH
6039 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6040
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006041 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006042 if (*end != *p)
6043 {
6044 semsg(_("E1067: Separator mismatch: %s"), p);
6045 vim_free(tofree);
6046 return FAIL;
6047 }
6048 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006049 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006050 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006051 len = (int)(end - tofree);
6052 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006053 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006054 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006055 if (pat == NULL)
6056 return FAIL;
6057 if (generate_PUSHS(cctx, pat) == FAIL)
6058 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006060 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6061 return NULL;
6062
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006063 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006064 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6065 return NULL;
6066 }
6067
6068 if (generate_instr(cctx, ISN_CATCH) == NULL)
6069 return NULL;
6070
6071 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6072 return NULL;
6073 return p;
6074}
6075
6076 static char_u *
6077compile_finally(char_u *arg, cctx_T *cctx)
6078{
6079 scope_T *scope = cctx->ctx_scope;
6080 garray_T *instr = &cctx->ctx_instr;
6081 isn_T *isn;
6082
6083 // end block scope from :try or :catch
6084 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6085 compile_endblock(cctx);
6086 scope = cctx->ctx_scope;
6087
6088 // Error if not in a :try scope
6089 if (scope == NULL || scope->se_type != TRY_SCOPE)
6090 {
6091 emsg(_(e_finally));
6092 return NULL;
6093 }
6094
6095 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006096 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006097 if (isn->isn_arg.try.try_finally != 0)
6098 {
6099 emsg(_(e_finally_dup));
6100 return NULL;
6101 }
6102
6103 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006104 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006105
Bram Moolenaar585fea72020-04-02 22:33:21 +02006106 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006107 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006108 {
6109 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006110 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006111 isn->isn_arg.jump.jump_where = instr->ga_len;
6112 }
6113
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006114 // TODO: set index in ts_finally_label jumps
6115
6116 return arg;
6117}
6118
6119 static char_u *
6120compile_endtry(char_u *arg, cctx_T *cctx)
6121{
6122 scope_T *scope = cctx->ctx_scope;
6123 garray_T *instr = &cctx->ctx_instr;
6124 isn_T *isn;
6125
6126 // end block scope from :catch or :finally
6127 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6128 compile_endblock(cctx);
6129 scope = cctx->ctx_scope;
6130
6131 // Error if not in a :try scope
6132 if (scope == NULL || scope->se_type != TRY_SCOPE)
6133 {
6134 if (scope == NULL)
6135 emsg(_(e_no_endtry));
6136 else if (scope->se_type == WHILE_SCOPE)
6137 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006138 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006139 emsg(_(e_endfor));
6140 else
6141 emsg(_(e_endif));
6142 return NULL;
6143 }
6144
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006145 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006146 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6147 {
6148 emsg(_("E1032: missing :catch or :finally"));
6149 return NULL;
6150 }
6151
6152 // Fill in the "end" label in jumps at the end of the blocks, if not done
6153 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006154 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006155
6156 // End :catch or :finally scope: set value in ISN_TRY instruction
6157 if (isn->isn_arg.try.try_finally == 0)
6158 isn->isn_arg.try.try_finally = instr->ga_len;
6159 compile_endblock(cctx);
6160
6161 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6162 return NULL;
6163 return arg;
6164}
6165
6166/*
6167 * compile "throw {expr}"
6168 */
6169 static char_u *
6170compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6171{
6172 char_u *p = skipwhite(arg);
6173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006174 if (compile_expr1(&p, cctx) == FAIL)
6175 return NULL;
6176 if (may_generate_2STRING(-1, cctx) == FAIL)
6177 return NULL;
6178 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6179 return NULL;
6180
6181 return p;
6182}
6183
6184/*
6185 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006186 * compile "echomsg expr"
6187 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006188 * compile "execute expr"
6189 */
6190 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006191compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006192{
6193 char_u *p = arg;
6194 int count = 0;
6195
6196 for (;;)
6197 {
6198 if (compile_expr1(&p, cctx) == FAIL)
6199 return NULL;
6200 ++count;
6201 p = skipwhite(p);
6202 if (ends_excmd(*p))
6203 break;
6204 }
6205
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006206 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6207 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6208 else if (cmdidx == CMD_execute)
6209 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6210 else if (cmdidx == CMD_echomsg)
6211 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6212 else
6213 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006214 return p;
6215}
6216
6217/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006218 * A command that is not compiled, execute with legacy code.
6219 */
6220 static char_u *
6221compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6222{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006223 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006224 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006225
6226 if (cctx->ctx_skip == TRUE)
6227 goto theend;
6228
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006229 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
6230 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006231 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6232 {
6233 // expand filename in "syntax include [@group] filename"
6234 has_expr = TRUE;
6235 eap->arg = skipwhite(eap->arg + 7);
6236 if (*eap->arg == '@')
6237 eap->arg = skiptowhite(eap->arg);
6238 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006239
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006240 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006241 {
6242 int count = 0;
6243 char_u *start = skipwhite(line);
6244
6245 // :cmd xxx`=expr1`yyy`=expr2`zzz
6246 // PUSHS ":cmd xxx"
6247 // eval expr1
6248 // PUSHS "yyy"
6249 // eval expr2
6250 // PUSHS "zzz"
6251 // EXECCONCAT 5
6252 for (;;)
6253 {
6254 if (p > start)
6255 {
6256 generate_PUSHS(cctx, vim_strnsave(start, (int)(p - start)));
6257 ++count;
6258 }
6259 p += 2;
6260 if (compile_expr1(&p, cctx) == FAIL)
6261 return NULL;
6262 may_generate_2STRING(-1, cctx);
6263 ++count;
6264 p = skipwhite(p);
6265 if (*p != '`')
6266 {
6267 emsg(_("E1083: missing backtick"));
6268 return NULL;
6269 }
6270 start = p + 1;
6271
6272 p = (char_u *)strstr((char *)start, "`=");
6273 if (p == NULL)
6274 {
6275 if (*skipwhite(start) != NUL)
6276 {
6277 generate_PUSHS(cctx, vim_strsave(start));
6278 ++count;
6279 }
6280 break;
6281 }
6282 }
6283 generate_EXECCONCAT(cctx, count);
6284 }
6285 else
6286 generate_EXEC(cctx, line);
6287
6288theend:
6289 return (char_u *)"";
6290}
6291
6292/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293 * After ex_function() has collected all the function lines: parse and compile
6294 * the lines into instructions.
6295 * Adds the function to "def_functions".
6296 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6297 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006298 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006299 * This can be used recursively through compile_lambda(), which may reallocate
6300 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301 */
6302 void
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006303compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006305 char_u *line = NULL;
6306 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307 char *errormsg = NULL; // error message
6308 int had_return = FALSE;
6309 cctx_T cctx;
6310 garray_T *instr;
6311 int called_emsg_before = called_emsg;
6312 int ret = FAIL;
6313 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006314 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006317 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01006318
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006319 if (ufunc->uf_dfunc_idx >= 0)
6320 {
6321 // Redefining a function that was compiled before.
6322 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
6323
6324 // Free old instructions.
6325 delete_def_function_contents(dfunc);
6326 }
6327 else
6328 {
6329 // Add the function to "def_functions".
6330 if (ga_grow(&def_functions, 1) == FAIL)
6331 return;
6332 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006333 CLEAR_POINTER(dfunc);
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006334 dfunc->df_idx = def_functions.ga_len;
6335 ufunc->uf_dfunc_idx = dfunc->df_idx;
6336 dfunc->df_ufunc = ufunc;
6337 ++def_functions.ga_len;
6338 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006339 }
6340
Bram Moolenaara80faa82020-04-12 19:37:17 +02006341 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006342 cctx.ctx_ufunc = ufunc;
6343 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006344 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006345 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6346 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6347 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6348 cctx.ctx_type_list = &ufunc->uf_type_list;
6349 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6350 instr = &cctx.ctx_instr;
6351
6352 // Most modern script version.
6353 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6354
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006355 if (ufunc->uf_def_args.ga_len > 0)
6356 {
6357 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006358 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006359 int i;
6360 char_u *arg;
6361 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6362
6363 // Produce instructions for the default values of optional arguments.
6364 // Store the instruction index in uf_def_arg_idx[] so that we know
6365 // where to start when the function is called, depending on the number
6366 // of arguments.
6367 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6368 if (ufunc->uf_def_arg_idx == NULL)
6369 goto erret;
6370 for (i = 0; i < count; ++i)
6371 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006372 garray_T *stack = &cctx.ctx_type_stack;
6373 type_T *val_type;
6374 int arg_idx = first_def_arg + i;
6375
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006376 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6377 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006378 if (compile_expr1(&arg, &cctx) == FAIL)
6379 goto erret;
6380
6381 // If no type specified use the type of the default value.
6382 // Otherwise check that the default value type matches the
6383 // specified type.
6384 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6385 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6386 ufunc->uf_arg_types[arg_idx] = val_type;
6387 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6388 == FAIL)
6389 {
6390 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6391 arg_idx + 1);
6392 goto erret;
6393 }
6394
6395 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006396 goto erret;
6397 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006398 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6399 }
6400
6401 /*
6402 * Loop over all the lines of the function and generate instructions.
6403 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006404 for (;;)
6405 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006406 exarg_T ea;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006407 int is_ex_command = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006408
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006409 // Bail out on the first error to avoid a flood of errors and report
6410 // the right line number when inside try/catch.
6411 if (emsg_before != called_emsg)
6412 goto erret;
6413
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006414 if (line != NULL && *line == '|')
6415 // the line continues after a '|'
6416 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006417 else if (line != NULL && *line != NUL
6418 && !(*line == '#' && (line == cctx.ctx_line_start
6419 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006420 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006421 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006422 goto erret;
6423 }
6424 else
6425 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006426 line = next_line_from_context(&cctx);
6427 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006428 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006429 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006430 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006431 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006432
6433 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006434 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006435 ea.cmdlinep = &line;
6436 ea.cmd = skipwhite(line);
6437
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006438 // Some things can be recognized by the first character.
6439 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006440 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006441 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006442 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006443 if (ea.cmd[1] != '{')
6444 {
6445 line = (char_u *)"";
6446 continue;
6447 }
6448 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006449
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006450 case '}':
6451 {
6452 // "}" ends a block scope
6453 scopetype_T stype = cctx.ctx_scope == NULL
6454 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006455
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006456 if (stype == BLOCK_SCOPE)
6457 {
6458 compile_endblock(&cctx);
6459 line = ea.cmd;
6460 }
6461 else
6462 {
6463 emsg(_("E1025: using } outside of a block scope"));
6464 goto erret;
6465 }
6466 if (line != NULL)
6467 line = skipwhite(ea.cmd + 1);
6468 continue;
6469 }
6470
6471 case '{':
6472 // "{" starts a block scope
6473 // "{'a': 1}->func() is something else
6474 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6475 {
6476 line = compile_block(ea.cmd, &cctx);
6477 continue;
6478 }
6479 break;
6480
6481 case ':':
6482 is_ex_command = TRUE;
6483 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006484 }
6485
6486 /*
6487 * COMMAND MODIFIERS
6488 */
6489 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6490 {
6491 if (errormsg != NULL)
6492 goto erret;
6493 // empty line or comment
6494 line = (char_u *)"";
6495 continue;
6496 }
6497
6498 // Skip ":call" to get to the function name.
6499 if (checkforcmd(&ea.cmd, "call", 3))
6500 ea.cmd = skipwhite(ea.cmd);
6501
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006502 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006503 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006504 // Assuming the command starts with a variable or function name,
6505 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
6506 // val".
6507 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
6508 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006509 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006510 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006512 int oplen;
6513 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006514
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006515 oplen = assignment_len(skipwhite(p), &heredoc);
6516 if (oplen > 0)
6517 {
6518 // Recognize an assignment if we recognize the variable
6519 // name:
6520 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006521 // "local = expr" where "local" is a local var.
6522 // "script = expr" where "script" is a script-local var.
6523 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006524 // "&opt = expr"
6525 // "$ENV = expr"
6526 // "@r = expr"
6527 if (*ea.cmd == '&'
6528 || *ea.cmd == '$'
6529 || *ea.cmd == '@'
6530 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006531 || lookup_local(ea.cmd, p - ea.cmd, &cctx) != NULL
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006532 || lookup_script(ea.cmd, p - ea.cmd) == OK
6533 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
6534 {
6535 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6536 if (line == NULL)
6537 goto erret;
6538 continue;
6539 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006540 }
6541 }
6542 }
6543
6544 /*
6545 * COMMAND after range
6546 */
6547 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006548 p = find_ex_command(&ea, NULL, is_ex_command ? NULL
6549 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006550 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006551
6552 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6553 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006554 if (cctx.ctx_skip == TRUE)
6555 {
6556 line += STRLEN(line);
6557 continue;
6558 }
6559
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006560 // Expression or function call.
6561 if (ea.cmdidx == CMD_eval)
6562 {
6563 p = ea.cmd;
6564 if (compile_expr1(&p, &cctx) == FAIL)
6565 goto erret;
6566
6567 // drop the return value
6568 generate_instr_drop(&cctx, ISN_DROP, 1);
6569 line = p;
6570 continue;
6571 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006572 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573 iemsg("Command from find_ex_command() not handled");
6574 goto erret;
6575 }
6576
6577 p = skipwhite(p);
6578
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006579 if (cctx.ctx_skip == TRUE
6580 && ea.cmdidx != CMD_elseif
6581 && ea.cmdidx != CMD_else
6582 && ea.cmdidx != CMD_endif)
6583 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006584 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006585 continue;
6586 }
6587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006588 switch (ea.cmdidx)
6589 {
6590 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006591 ea.arg = p;
6592 line = compile_nested_function(&ea, &cctx);
6593 break;
6594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006595 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006596 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597 goto erret;
6598
6599 case CMD_return:
6600 line = compile_return(p, set_return_type, &cctx);
6601 had_return = TRUE;
6602 break;
6603
6604 case CMD_let:
6605 case CMD_const:
6606 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
6607 break;
6608
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006609 case CMD_unlet:
6610 case CMD_unlockvar:
6611 case CMD_lockvar:
6612 line = compile_unletlock(p, &ea, &cctx);
6613 break;
6614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 case CMD_import:
6616 line = compile_import(p, &cctx);
6617 break;
6618
6619 case CMD_if:
6620 line = compile_if(p, &cctx);
6621 break;
6622 case CMD_elseif:
6623 line = compile_elseif(p, &cctx);
6624 break;
6625 case CMD_else:
6626 line = compile_else(p, &cctx);
6627 break;
6628 case CMD_endif:
6629 line = compile_endif(p, &cctx);
6630 break;
6631
6632 case CMD_while:
6633 line = compile_while(p, &cctx);
6634 break;
6635 case CMD_endwhile:
6636 line = compile_endwhile(p, &cctx);
6637 break;
6638
6639 case CMD_for:
6640 line = compile_for(p, &cctx);
6641 break;
6642 case CMD_endfor:
6643 line = compile_endfor(p, &cctx);
6644 break;
6645 case CMD_continue:
6646 line = compile_continue(p, &cctx);
6647 break;
6648 case CMD_break:
6649 line = compile_break(p, &cctx);
6650 break;
6651
6652 case CMD_try:
6653 line = compile_try(p, &cctx);
6654 break;
6655 case CMD_catch:
6656 line = compile_catch(p, &cctx);
6657 break;
6658 case CMD_finally:
6659 line = compile_finally(p, &cctx);
6660 break;
6661 case CMD_endtry:
6662 line = compile_endtry(p, &cctx);
6663 break;
6664 case CMD_throw:
6665 line = compile_throw(p, &cctx);
6666 break;
6667
6668 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006669 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006670 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006671 case CMD_echomsg:
6672 case CMD_echoerr:
6673 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006674 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006675
6676 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006677 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006678 // Not recognized, execute with do_cmdline_cmd().
6679 ea.arg = p;
6680 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006681 break;
6682 }
6683 if (line == NULL)
6684 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006685 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006686
6687 if (cctx.ctx_type_stack.ga_len < 0)
6688 {
6689 iemsg("Type stack underflow");
6690 goto erret;
6691 }
6692 }
6693
6694 if (cctx.ctx_scope != NULL)
6695 {
6696 if (cctx.ctx_scope->se_type == IF_SCOPE)
6697 emsg(_(e_endif));
6698 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6699 emsg(_(e_endwhile));
6700 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6701 emsg(_(e_endfor));
6702 else
6703 emsg(_("E1026: Missing }"));
6704 goto erret;
6705 }
6706
6707 if (!had_return)
6708 {
6709 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6710 {
6711 emsg(_("E1027: Missing return statement"));
6712 goto erret;
6713 }
6714
6715 // Return zero if there is no return at the end.
6716 generate_PUSHNR(&cctx, 0);
6717 generate_instr(&cctx, ISN_RETURN);
6718 }
6719
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006720 {
6721 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6722 + ufunc->uf_dfunc_idx;
6723 dfunc->df_deleted = FALSE;
6724 dfunc->df_instr = instr->ga_data;
6725 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006726 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006727 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006728 if (cctx.ctx_outer_used)
6729 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006730 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006731
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006732 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006733 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006734 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006735
6736 // Create a type for the function, with the return type and any
6737 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006738 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6739 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006740 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006741 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006742 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6743 argcount, &ufunc->uf_type_list);
6744 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006745 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006746 argcount + varargs,
6747 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006748 {
6749 ret = FAIL;
6750 goto erret;
6751 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006752 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6753 ufunc->uf_func_type->tt_min_argcount =
6754 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006755 if (ufunc->uf_arg_types == NULL)
6756 {
6757 int i;
6758
6759 // lambda does not have argument types.
6760 for (i = 0; i < argcount; ++i)
6761 ufunc->uf_func_type->tt_args[i] = &t_any;
6762 }
6763 else
6764 mch_memmove(ufunc->uf_func_type->tt_args,
6765 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006766 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006767 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006768 ufunc->uf_func_type->tt_args[argcount] =
6769 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006770 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6771 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006772 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006773 else
6774 // No arguments, can use a predefined type.
6775 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6776 argcount, &ufunc->uf_type_list);
6777
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006778 }
6779
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006780 ret = OK;
6781
6782erret:
6783 if (ret == FAIL)
6784 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006785 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006786 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6787 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006788
6789 for (idx = 0; idx < instr->ga_len; ++idx)
6790 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006791 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006793 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006794 if (!dfunc->df_deleted)
6795 --def_functions.ga_len;
6796
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006797 while (cctx.ctx_scope != NULL)
6798 drop_scope(&cctx);
6799
Bram Moolenaar20431c92020-03-20 18:39:46 +01006800 // Don't execute this function body.
6801 ga_clear_strings(&ufunc->uf_lines);
6802
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006803 if (errormsg != NULL)
6804 emsg(errormsg);
6805 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006806 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006807 }
6808
6809 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006810 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006811 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006812 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006813}
6814
6815/*
6816 * Delete an instruction, free what it contains.
6817 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006818 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006819delete_instr(isn_T *isn)
6820{
6821 switch (isn->isn_type)
6822 {
6823 case ISN_EXEC:
6824 case ISN_LOADENV:
6825 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006826 case ISN_LOADB:
6827 case ISN_LOADW:
6828 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006829 case ISN_LOADOPT:
6830 case ISN_MEMBER:
6831 case ISN_PUSHEXC:
6832 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006833 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006834 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006835 case ISN_STOREB:
6836 case ISN_STOREW:
6837 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006838 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006839 vim_free(isn->isn_arg.string);
6840 break;
6841
6842 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006843 case ISN_STORES:
6844 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006845 break;
6846
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006847 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006848 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006849 vim_free(isn->isn_arg.unlet.ul_name);
6850 break;
6851
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006852 case ISN_STOREOPT:
6853 vim_free(isn->isn_arg.storeopt.so_name);
6854 break;
6855
6856 case ISN_PUSHBLOB: // push blob isn_arg.blob
6857 blob_unref(isn->isn_arg.blob);
6858 break;
6859
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006860 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006861#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006862 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006863#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006864 break;
6865
6866 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006867#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006868 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006869#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006870 break;
6871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006872 case ISN_UCALL:
6873 vim_free(isn->isn_arg.ufunc.cuf_name);
6874 break;
6875
Bram Moolenaar221fcc72020-05-05 19:46:20 +02006876 case ISN_FUNCREF:
6877 {
6878 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6879 + isn->isn_arg.funcref.fr_func;
6880 func_ptr_unref(dfunc->df_ufunc);
6881 }
6882 break;
6883
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006884 case ISN_2BOOL:
6885 case ISN_2STRING:
6886 case ISN_ADDBLOB:
6887 case ISN_ADDLIST:
6888 case ISN_BCALL:
6889 case ISN_CATCH:
6890 case ISN_CHECKNR:
6891 case ISN_CHECKTYPE:
6892 case ISN_COMPAREANY:
6893 case ISN_COMPAREBLOB:
6894 case ISN_COMPAREBOOL:
6895 case ISN_COMPAREDICT:
6896 case ISN_COMPAREFLOAT:
6897 case ISN_COMPAREFUNC:
6898 case ISN_COMPARELIST:
6899 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006900 case ISN_COMPARESPECIAL:
6901 case ISN_COMPARESTRING:
6902 case ISN_CONCAT:
6903 case ISN_DCALL:
6904 case ISN_DROP:
6905 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006906 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006907 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006908 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006909 case ISN_EXECCONCAT:
6910 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006911 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006912 case ISN_INDEX:
6913 case ISN_JUMP:
6914 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006915 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006916 case ISN_LOADSCRIPT:
6917 case ISN_LOADREG:
6918 case ISN_LOADV:
6919 case ISN_NEGATENR:
6920 case ISN_NEWDICT:
6921 case ISN_NEWLIST:
6922 case ISN_OPNR:
6923 case ISN_OPFLOAT:
6924 case ISN_OPANY:
6925 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006926 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927 case ISN_PUSHF:
6928 case ISN_PUSHNR:
6929 case ISN_PUSHBOOL:
6930 case ISN_PUSHSPEC:
6931 case ISN_RETURN:
6932 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02006933 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006934 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006935 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006936 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006937 case ISN_STORESCRIPT:
6938 case ISN_THROW:
6939 case ISN_TRY:
6940 // nothing allocated
6941 break;
6942 }
6943}
6944
6945/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006946 * Free all instructions for "dfunc".
6947 */
6948 static void
6949delete_def_function_contents(dfunc_T *dfunc)
6950{
6951 int idx;
6952
6953 ga_clear(&dfunc->df_def_args_isn);
6954
6955 if (dfunc->df_instr != NULL)
6956 {
6957 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6958 delete_instr(dfunc->df_instr + idx);
6959 VIM_CLEAR(dfunc->df_instr);
6960 }
6961
6962 dfunc->df_deleted = TRUE;
6963}
6964
6965/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006966 * When a user function is deleted, delete any associated def function.
6967 */
6968 void
6969delete_def_function(ufunc_T *ufunc)
6970{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971 if (ufunc->uf_dfunc_idx >= 0)
6972 {
6973 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6974 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975
Bram Moolenaar20431c92020-03-20 18:39:46 +01006976 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977 }
6978}
6979
6980#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006981/*
6982 * Free all functions defined with ":def".
6983 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006984 void
6985free_def_functions(void)
6986{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006987 int idx;
6988
6989 for (idx = 0; idx < def_functions.ga_len; ++idx)
6990 {
6991 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6992
6993 delete_def_function_contents(dfunc);
6994 }
6995
6996 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997}
6998#endif
6999
7000
7001#endif // FEAT_EVAL