blob: 3d3b3c30fcca199c3a44dc1fe5f72aa5aef73950 [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 Moolenaarcca34aa2020-05-07 22:23:58 +02004232 vim_free(s1);
4233 vim_free(s2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 }
4235 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004236 {
4237 generate_tv_PUSH(cctx, &tv1);
4238 generate_tv_PUSH(cctx, &tv2);
4239 if (*op == '.')
4240 {
4241 if (may_generate_2STRING(-2, cctx) == FAIL
4242 || may_generate_2STRING(-1, cctx) == FAIL)
4243 return FAIL;
4244 generate_instr_drop(cctx, ISN_CONCAT, 1);
4245 }
4246 else
4247 generate_two_op(cctx, op);
4248 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 }
4250
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004251 // TODO: move to caller
4252 generate_tv_PUSH(cctx, &tv1);
4253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 return OK;
4255}
4256
4257/*
4258 * expr5a == expr5b
4259 * expr5a =~ expr5b
4260 * expr5a != expr5b
4261 * expr5a !~ expr5b
4262 * expr5a > expr5b
4263 * expr5a >= expr5b
4264 * expr5a < expr5b
4265 * expr5a <= expr5b
4266 * expr5a is expr5b
4267 * expr5a isnot expr5b
4268 *
4269 * Produces instructions:
4270 * EVAL expr5a Push result of "expr5a"
4271 * EVAL expr5b Push result of "expr5b"
4272 * COMPARE one of the compare instructions
4273 */
4274 static int
4275compile_expr4(char_u **arg, cctx_T *cctx)
4276{
4277 exptype_T type = EXPR_UNKNOWN;
4278 char_u *p;
4279 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 int type_is = FALSE;
4281
4282 // get the first variable
4283 if (compile_expr5(arg, cctx) == FAIL)
4284 return FAIL;
4285
4286 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004287 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288
4289 /*
4290 * If there is a comparative operator, use it.
4291 */
4292 if (type != EXPR_UNKNOWN)
4293 {
4294 int ic = FALSE; // Default: do not ignore case
4295
4296 if (type_is && (p[len] == '?' || p[len] == '#'))
4297 {
4298 semsg(_(e_invexpr2), *arg);
4299 return FAIL;
4300 }
4301 // extra question mark appended: ignore case
4302 if (p[len] == '?')
4303 {
4304 ic = TRUE;
4305 ++len;
4306 }
4307 // extra '#' appended: match case (ignored)
4308 else if (p[len] == '#')
4309 ++len;
4310 // nothing appended: match case
4311
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004312 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 {
4314 char_u buf[7];
4315
4316 vim_strncpy(buf, p, len);
4317 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004318 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 }
4320
4321 // get the second variable
4322 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004323 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004324 return FAIL;
4325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 if (compile_expr5(arg, cctx) == FAIL)
4327 return FAIL;
4328
4329 generate_COMPARE(cctx, type, ic);
4330 }
4331
4332 return OK;
4333}
4334
4335/*
4336 * Compile || or &&.
4337 */
4338 static int
4339compile_and_or(char_u **arg, cctx_T *cctx, char *op)
4340{
4341 char_u *p = skipwhite(*arg);
4342 int opchar = *op;
4343
4344 if (p[0] == opchar && p[1] == opchar)
4345 {
4346 garray_T *instr = &cctx->ctx_instr;
4347 garray_T end_ga;
4348
4349 /*
4350 * Repeat until there is no following "||" or "&&"
4351 */
4352 ga_init2(&end_ga, sizeof(int), 10);
4353 while (p[0] == opchar && p[1] == opchar)
4354 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004355 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4356 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004358 return FAIL;
4359 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360
4361 if (ga_grow(&end_ga, 1) == FAIL)
4362 {
4363 ga_clear(&end_ga);
4364 return FAIL;
4365 }
4366 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4367 ++end_ga.ga_len;
4368 generate_JUMP(cctx, opchar == '|'
4369 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4370
4371 // eval the next expression
4372 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004373 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004374 return FAIL;
4375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 if ((opchar == '|' ? compile_expr3(arg, cctx)
4377 : compile_expr4(arg, cctx)) == FAIL)
4378 {
4379 ga_clear(&end_ga);
4380 return FAIL;
4381 }
4382 p = skipwhite(*arg);
4383 }
4384
4385 // Fill in the end label in all jumps.
4386 while (end_ga.ga_len > 0)
4387 {
4388 isn_T *isn;
4389
4390 --end_ga.ga_len;
4391 isn = ((isn_T *)instr->ga_data)
4392 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4393 isn->isn_arg.jump.jump_where = instr->ga_len;
4394 }
4395 ga_clear(&end_ga);
4396 }
4397
4398 return OK;
4399}
4400
4401/*
4402 * expr4a && expr4a && expr4a logical AND
4403 *
4404 * Produces instructions:
4405 * EVAL expr4a Push result of "expr4a"
4406 * JUMP_AND_KEEP_IF_FALSE end
4407 * EVAL expr4b Push result of "expr4b"
4408 * JUMP_AND_KEEP_IF_FALSE end
4409 * EVAL expr4c Push result of "expr4c"
4410 * end:
4411 */
4412 static int
4413compile_expr3(char_u **arg, cctx_T *cctx)
4414{
4415 // get the first variable
4416 if (compile_expr4(arg, cctx) == FAIL)
4417 return FAIL;
4418
4419 // || and && work almost the same
4420 return compile_and_or(arg, cctx, "&&");
4421}
4422
4423/*
4424 * expr3a || expr3b || expr3c logical OR
4425 *
4426 * Produces instructions:
4427 * EVAL expr3a Push result of "expr3a"
4428 * JUMP_AND_KEEP_IF_TRUE end
4429 * EVAL expr3b Push result of "expr3b"
4430 * JUMP_AND_KEEP_IF_TRUE end
4431 * EVAL expr3c Push result of "expr3c"
4432 * end:
4433 */
4434 static int
4435compile_expr2(char_u **arg, cctx_T *cctx)
4436{
4437 // eval the first expression
4438 if (compile_expr3(arg, cctx) == FAIL)
4439 return FAIL;
4440
4441 // || and && work almost the same
4442 return compile_and_or(arg, cctx, "||");
4443}
4444
4445/*
4446 * Toplevel expression: expr2 ? expr1a : expr1b
4447 *
4448 * Produces instructions:
4449 * EVAL expr2 Push result of "expr"
4450 * JUMP_IF_FALSE alt jump if false
4451 * EVAL expr1a
4452 * JUMP_ALWAYS end
4453 * alt: EVAL expr1b
4454 * end:
4455 */
4456 static int
4457compile_expr1(char_u **arg, cctx_T *cctx)
4458{
4459 char_u *p;
4460
Bram Moolenaar61a89812020-05-07 16:58:17 +02004461 // Evaluate the first expression.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004462 if (compile_expr2(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 return FAIL;
4464
4465 p = skipwhite(*arg);
4466 if (*p == '?')
4467 {
4468 garray_T *instr = &cctx->ctx_instr;
4469 garray_T *stack = &cctx->ctx_type_stack;
4470 int alt_idx = instr->ga_len;
4471 int end_idx;
4472 isn_T *isn;
4473 type_T *type1;
4474 type_T *type2;
4475
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004476 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4477 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004479 return FAIL;
4480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481
4482 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4483
4484 // evaluate the second expression; any type is accepted
4485 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004486 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004487 return FAIL;
4488
Bram Moolenaara6d53682020-01-28 23:04:06 +01004489 if (compile_expr1(arg, cctx) == FAIL)
4490 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491
4492 // remember the type and drop it
4493 --stack->ga_len;
4494 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
4495
4496 end_idx = instr->ga_len;
4497 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4498
4499 // jump here from JUMP_IF_FALSE
4500 isn = ((isn_T *)instr->ga_data) + alt_idx;
4501 isn->isn_arg.jump.jump_where = instr->ga_len;
4502
4503 // Check for the ":".
4504 p = skipwhite(*arg);
4505 if (*p != ':')
4506 {
4507 emsg(_(e_missing_colon));
4508 return FAIL;
4509 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004510 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4511 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004513 return FAIL;
4514 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515
4516 // evaluate the third expression
4517 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004518 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004519 return FAIL;
4520
Bram Moolenaara6d53682020-01-28 23:04:06 +01004521 if (compile_expr1(arg, cctx) == FAIL)
4522 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523
4524 // If the types differ, the result has a more generic type.
4525 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004526 common_type(type1, type2, &type2, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527
4528 // jump here from JUMP_ALWAYS
4529 isn = ((isn_T *)instr->ga_data) + end_idx;
4530 isn->isn_arg.jump.jump_where = instr->ga_len;
4531 }
4532 return OK;
4533}
4534
4535/*
4536 * compile "return [expr]"
4537 */
4538 static char_u *
4539compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4540{
4541 char_u *p = arg;
4542 garray_T *stack = &cctx->ctx_type_stack;
4543 type_T *stack_type;
4544
4545 if (*p != NUL && *p != '|' && *p != '\n')
4546 {
4547 // compile return argument into instructions
4548 if (compile_expr1(&p, cctx) == FAIL)
4549 return NULL;
4550
4551 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4552 if (set_return_type)
4553 cctx->ctx_ufunc->uf_ret_type = stack_type;
4554 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4555 == FAIL)
4556 return NULL;
4557 }
4558 else
4559 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004560 // "set_return_type" cannot be TRUE, only used for a lambda which
4561 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004562 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4563 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 {
4565 emsg(_("E1003: Missing return value"));
4566 return NULL;
4567 }
4568
4569 // No argument, return zero.
4570 generate_PUSHNR(cctx, 0);
4571 }
4572
4573 if (generate_instr(cctx, ISN_RETURN) == NULL)
4574 return NULL;
4575
4576 // "return val | endif" is possible
4577 return skipwhite(p);
4578}
4579
4580/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004581 * Get a line from the compilation context, compatible with exarg_T getline().
4582 * Return a pointer to the line in allocated memory.
4583 * Return NULL for end-of-file or some error.
4584 */
4585 static char_u *
4586exarg_getline(
4587 int c UNUSED,
4588 void *cookie,
4589 int indent UNUSED,
4590 int do_concat UNUSED)
4591{
4592 cctx_T *cctx = (cctx_T *)cookie;
4593
4594 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4595 {
4596 iemsg("Heredoc got to end");
4597 return NULL;
4598 }
4599 ++cctx->ctx_lnum;
4600 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4601 [cctx->ctx_lnum]);
4602}
4603
4604/*
4605 * Compile a nested :def command.
4606 */
4607 static char_u *
4608compile_nested_function(exarg_T *eap, cctx_T *cctx)
4609{
4610 char_u *name_start = eap->arg;
4611 char_u *name_end = to_name_end(eap->arg, FALSE);
4612 char_u *name = get_lambda_name();
4613 lvar_T *lvar;
4614 ufunc_T *ufunc;
4615
4616 eap->arg = name_end;
4617 eap->getline = exarg_getline;
4618 eap->cookie = cctx;
4619 eap->skip = cctx->ctx_skip == TRUE;
4620 eap->forceit = FALSE;
4621 ufunc = def_function(eap, name, cctx);
4622
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004623 if (ufunc == NULL || ufunc->uf_dfunc_idx < 0)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004624 return NULL;
4625
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004626 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004627 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004628 TRUE, ufunc->uf_func_type);
4629
4630 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4631 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4632 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004633
Bram Moolenaar61a89812020-05-07 16:58:17 +02004634 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004635 return (char_u *)"";
4636}
4637
4638/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 * Return the length of an assignment operator, or zero if there isn't one.
4640 */
4641 int
4642assignment_len(char_u *p, int *heredoc)
4643{
4644 if (*p == '=')
4645 {
4646 if (p[1] == '<' && p[2] == '<')
4647 {
4648 *heredoc = TRUE;
4649 return 3;
4650 }
4651 return 1;
4652 }
4653 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4654 return 2;
4655 if (STRNCMP(p, "..=", 3) == 0)
4656 return 3;
4657 return 0;
4658}
4659
4660// words that cannot be used as a variable
4661static char *reserved[] = {
4662 "true",
4663 "false",
4664 NULL
4665};
4666
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004667typedef enum {
4668 dest_local,
4669 dest_option,
4670 dest_env,
4671 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004672 dest_buffer,
4673 dest_window,
4674 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004675 dest_vimvar,
4676 dest_script,
4677 dest_reg,
4678} assign_dest_T;
4679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680/*
4681 * compile "let var [= expr]", "const var = expr" and "var = expr"
4682 * "arg" points to "var".
4683 */
4684 static char_u *
4685compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4686{
4687 char_u *p;
4688 char_u *ret = NULL;
4689 int var_count = 0;
4690 int semicolon = 0;
4691 size_t varlen;
4692 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004693 int new_local = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695 int opt_type;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004696 assign_dest_T dest = dest_local;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 int opt_flags = 0;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004698 int vimvaridx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 int oplen = 0;
4700 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004701 type_T *type = &t_any;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004702 lvar_T *lvar = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703 char_u *name;
4704 char_u *sp;
4705 int has_type = FALSE;
4706 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
4707 int instr_count = -1;
4708
4709 p = skip_var_list(arg, FALSE, &var_count, &semicolon);
4710 if (p == NULL)
4711 return NULL;
4712 if (var_count > 0)
4713 {
4714 // TODO: let [var, var] = list
4715 emsg("Cannot handle a list yet");
4716 return NULL;
4717 }
4718
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004719 // "a: type" is declaring variable "a" with a type, not "a:".
4720 if (is_decl && p == arg + 2 && p[-1] == ':')
4721 --p;
4722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723 varlen = p - arg;
4724 name = vim_strnsave(arg, (int)varlen);
4725 if (name == NULL)
4726 return NULL;
4727
Bram Moolenaar080457c2020-03-03 21:53:32 +01004728 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004730 if (*arg == '&')
4731 {
4732 int cc;
4733 long numval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734
Bram Moolenaar080457c2020-03-03 21:53:32 +01004735 dest = dest_option;
4736 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004738 emsg(_(e_const_option));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004739 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004741 if (is_decl)
4742 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004743 semsg(_("E1052: Cannot declare an option: %s"), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 goto theend;
4745 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004746 p = arg;
4747 p = find_option_end(&p, &opt_flags);
4748 if (p == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004750 // cannot happen?
Bram Moolenaar080457c2020-03-03 21:53:32 +01004751 emsg(_(e_letunexp));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004752 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004753 }
4754 cc = *p;
4755 *p = NUL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004756 opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004757 *p = cc;
4758 if (opt_type == -3)
4759 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004760 semsg(_(e_unknown_option), arg);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004761 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004762 }
4763 if (opt_type == -2 || opt_type == 0)
4764 type = &t_string;
4765 else
4766 type = &t_number; // both number and boolean option
4767 }
4768 else if (*arg == '$')
4769 {
4770 dest = dest_env;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004771 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004772 if (is_decl)
4773 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004774 semsg(_("E1065: Cannot declare an environment variable: %s"),
4775 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004776 goto theend;
4777 }
4778 }
4779 else if (*arg == '@')
4780 {
4781 if (!valid_yank_reg(arg[1], TRUE))
4782 {
4783 emsg_invreg(arg[1]);
Bram Moolenaar25b70c72020-04-01 16:34:17 +02004784 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004785 }
4786 dest = dest_reg;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004787 type = &t_string;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004788 if (is_decl)
4789 {
4790 semsg(_("E1066: Cannot declare a register: %s"), name);
4791 goto theend;
4792 }
4793 }
4794 else if (STRNCMP(arg, "g:", 2) == 0)
4795 {
4796 dest = dest_global;
4797 if (is_decl)
4798 {
4799 semsg(_("E1016: Cannot declare a global variable: %s"), name);
4800 goto theend;
4801 }
4802 }
Bram Moolenaard3aac292020-04-19 14:32:17 +02004803 else if (STRNCMP(arg, "b:", 2) == 0)
4804 {
4805 dest = dest_buffer;
4806 if (is_decl)
4807 {
4808 semsg(_("E1078: Cannot declare a buffer variable: %s"), name);
4809 goto theend;
4810 }
4811 }
4812 else if (STRNCMP(arg, "w:", 2) == 0)
4813 {
4814 dest = dest_window;
4815 if (is_decl)
4816 {
4817 semsg(_("E1079: Cannot declare a window variable: %s"), name);
4818 goto theend;
4819 }
4820 }
4821 else if (STRNCMP(arg, "t:", 2) == 0)
4822 {
4823 dest = dest_tab;
4824 if (is_decl)
4825 {
4826 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
4827 goto theend;
4828 }
4829 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004830 else if (STRNCMP(arg, "v:", 2) == 0)
4831 {
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004832 typval_T *vtv;
4833 int di_flags;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004834
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004835 vimvaridx = find_vim_var(name + 2, &di_flags);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004836 if (vimvaridx < 0)
4837 {
4838 semsg(_(e_var_notfound), arg);
4839 goto theend;
4840 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02004841 // We use the current value of "sandbox" here, is that OK?
4842 if (var_check_ro(di_flags, name, FALSE))
4843 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004844 dest = dest_vimvar;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004845 vtv = get_vim_var_tv(vimvaridx);
4846 type = typval2type(vtv);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004847 if (is_decl)
4848 {
4849 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
4850 goto theend;
4851 }
4852 }
4853 else
4854 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004855 int idx;
4856
Bram Moolenaar080457c2020-03-03 21:53:32 +01004857 for (idx = 0; reserved[idx] != NULL; ++idx)
4858 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004859 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004860 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 goto theend;
4862 }
Bram Moolenaar080457c2020-03-03 21:53:32 +01004863
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004864 lvar = lookup_local(arg, varlen, cctx);
4865 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004866 {
Bram Moolenaar080457c2020-03-03 21:53:32 +01004867 if (is_decl)
4868 {
4869 semsg(_("E1017: Variable already declared: %s"), name);
4870 goto theend;
4871 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004872 else if (lvar->lv_const)
Bram Moolenaar080457c2020-03-03 21:53:32 +01004873 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004874 semsg(_("E1018: Cannot assign to a constant: %s"), name);
4875 goto theend;
Bram Moolenaar080457c2020-03-03 21:53:32 +01004876 }
4877 }
4878 else if (STRNCMP(arg, "s:", 2) == 0
4879 || lookup_script(arg, varlen) == OK
4880 || find_imported(arg, varlen, cctx) != NULL)
4881 {
4882 dest = dest_script;
4883 if (is_decl)
4884 {
4885 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004886 name);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004887 goto theend;
4888 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004890 else if (name[1] == ':' && name[2] != NUL)
4891 {
4892 semsg(_("E1082: Cannot use a namespaced variable: %s"), name);
4893 goto theend;
4894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 }
4896 }
4897
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004898 if (dest != dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899 {
4900 if (is_decl && *p == ':')
4901 {
4902 // parse optional type: "let var: type = expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004903 if (!VIM_ISWHITE(p[1]))
4904 {
4905 semsg(_(e_white_after), ":");
4906 goto theend;
4907 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908 p = skipwhite(p + 1);
4909 type = parse_type(&p, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910 has_type = TRUE;
4911 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004912 else if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004913 type = lvar->lv_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914 }
4915
4916 sp = p;
4917 p = skipwhite(p);
4918 op = p;
4919 oplen = assignment_len(p, &heredoc);
4920 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4921 {
4922 char_u buf[4];
4923
4924 vim_strncpy(buf, op, oplen);
4925 semsg(_(e_white_both), buf);
4926 }
4927
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004928 if (oplen == 3 && !heredoc && dest != dest_global
Bram Moolenaar4c683752020-04-05 21:38:23 +02004929 && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01004931 emsg(_("E1019: Can only concatenate to string"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932 goto theend;
4933 }
4934
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004935 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 {
4937 if (oplen > 1 && !heredoc)
4938 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004939 // +=, /=, etc. require an existing variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 semsg(_("E1020: cannot use an operator on a new variable: %s"),
4941 name);
4942 goto theend;
4943 }
4944
4945 // new local variable
Bram Moolenaar08938ee2020-04-11 23:17:17 +02004946 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004947 goto theend;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004948 lvar = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type);
4949 if (lvar == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950 goto theend;
Bram Moolenaar01b38622020-03-30 21:28:39 +02004951 new_local = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952 }
4953
4954 if (heredoc)
4955 {
4956 list_T *l;
4957 listitem_T *li;
4958
4959 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004960 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004962 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963
4964 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004965 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966 {
4967 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4968 li->li_tv.vval.v_string = NULL;
4969 }
4970 generate_NEWLIST(cctx, l->lv_len);
4971 type = &t_list_string;
4972 list_free(l);
4973 p += STRLEN(p);
4974 }
4975 else if (oplen > 0)
4976 {
Bram Moolenaara8c17702020-04-01 21:17:24 +02004977 int r;
4978 type_T *stacktype;
4979 garray_T *stack;
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004980
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 // for "+=", "*=", "..=" etc. first load the current value
4982 if (*op != '=')
4983 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004984 switch (dest)
4985 {
4986 case dest_option:
4987 // TODO: check the option exists
Bram Moolenaara8c17702020-04-01 21:17:24 +02004988 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004989 break;
4990 case dest_global:
4991 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4992 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004993 case dest_buffer:
4994 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4995 break;
4996 case dest_window:
4997 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4998 break;
4999 case dest_tab:
5000 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5001 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005002 case dest_script:
Bram Moolenaarb35efa52020-02-26 20:15:18 +01005003 compile_load_scriptvar(cctx,
5004 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005005 break;
5006 case dest_env:
5007 // Include $ in the name here
5008 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5009 break;
5010 case dest_reg:
5011 generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string);
5012 break;
5013 case dest_vimvar:
5014 generate_LOADV(cctx, name + 2, TRUE);
5015 break;
5016 case dest_local:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005017 if (lvar->lv_from_outer)
5018 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5019 NULL, type);
5020 else
5021 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005022 break;
5023 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005024 }
5025
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005026 // Compile the expression. Temporarily hide the new local variable
5027 // here, it is not available to this expression.
Bram Moolenaar01b38622020-03-30 21:28:39 +02005028 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005029 --cctx->ctx_locals.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005030 instr_count = instr->ga_len;
5031 p = skipwhite(p + oplen);
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005032 r = compile_expr1(&p, cctx);
Bram Moolenaar01b38622020-03-30 21:28:39 +02005033 if (new_local)
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005034 ++cctx->ctx_locals.ga_len;
5035 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036 goto theend;
5037
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005038 if (cctx->ctx_skip != TRUE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005040 stack = &cctx->ctx_type_stack;
5041 stacktype = stack->ga_len == 0 ? &t_void
5042 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005043 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005045 if (new_local && !has_type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005047 if (stacktype->tt_type == VAR_VOID)
5048 {
5049 emsg(_("E1031: Cannot use void value"));
5050 goto theend;
5051 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005052 else
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005053 {
5054 // An empty list or dict has a &t_void member, for a
5055 // variable that implies &t_any.
5056 if (stacktype == &t_list_empty)
5057 lvar->lv_type = &t_list_any;
5058 else if (stacktype == &t_dict_empty)
5059 lvar->lv_type = &t_dict_any;
5060 else
5061 lvar->lv_type = stacktype;
5062 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005063 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005064 else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
5065 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066 }
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005067 else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005068 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069 }
5070 }
5071 else if (cmdidx == CMD_const)
5072 {
5073 emsg(_("E1021: const requires a value"));
5074 goto theend;
5075 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005076 else if (!has_type || dest == dest_option)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077 {
5078 emsg(_("E1022: type or initialization required"));
5079 goto theend;
5080 }
5081 else
5082 {
5083 // variables are always initialized
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084 if (ga_grow(instr, 1) == FAIL)
5085 goto theend;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005086 switch (type->tt_type)
5087 {
5088 case VAR_BOOL:
5089 generate_PUSHBOOL(cctx, VVAL_FALSE);
5090 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005091 case VAR_FLOAT:
5092#ifdef FEAT_FLOAT
5093 generate_PUSHF(cctx, 0.0);
5094#endif
5095 break;
5096 case VAR_STRING:
5097 generate_PUSHS(cctx, NULL);
5098 break;
5099 case VAR_BLOB:
5100 generate_PUSHBLOB(cctx, NULL);
5101 break;
5102 case VAR_FUNC:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005103 generate_PUSHFUNC(cctx, NULL, &t_func_void);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005104 break;
Bram Moolenaar04d05222020-02-06 22:06:54 +01005105 case VAR_LIST:
5106 generate_NEWLIST(cctx, 0);
5107 break;
5108 case VAR_DICT:
5109 generate_NEWDICT(cctx, 0);
5110 break;
5111 case VAR_JOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005112 generate_PUSHJOB(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005113 break;
5114 case VAR_CHANNEL:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01005115 generate_PUSHCHANNEL(cctx, NULL);
Bram Moolenaar04d05222020-02-06 22:06:54 +01005116 break;
5117 case VAR_NUMBER:
5118 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005119 case VAR_ANY:
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +02005120 case VAR_PARTIAL:
Bram Moolenaar04d05222020-02-06 22:06:54 +01005121 case VAR_VOID:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02005122 case VAR_SPECIAL: // cannot happen
Bram Moolenaar04d05222020-02-06 22:06:54 +01005123 generate_PUSHNR(cctx, 0);
5124 break;
5125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126 }
5127
5128 if (oplen > 0 && *op != '=')
5129 {
5130 type_T *expected = &t_number;
5131 garray_T *stack = &cctx->ctx_type_stack;
5132 type_T *stacktype;
5133
5134 // TODO: if type is known use float or any operation
5135
5136 if (*op == '.')
5137 expected = &t_string;
5138 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5139 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5140 goto theend;
5141
5142 if (*op == '.')
5143 generate_instr_drop(cctx, ISN_CONCAT, 1);
5144 else
5145 {
5146 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5147
5148 if (isn == NULL)
5149 goto theend;
5150 switch (*op)
5151 {
5152 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5153 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5154 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5155 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5156 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5157 }
5158 }
5159 }
5160
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005161 switch (dest)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005162 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005163 case dest_option:
5164 generate_STOREOPT(cctx, name + 1, opt_flags);
5165 break;
5166 case dest_global:
5167 // include g: with the name, easier to execute that way
5168 generate_STORE(cctx, ISN_STOREG, 0, name);
5169 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02005170 case dest_buffer:
5171 // include b: with the name, easier to execute that way
5172 generate_STORE(cctx, ISN_STOREB, 0, name);
5173 break;
5174 case dest_window:
5175 // include w: with the name, easier to execute that way
5176 generate_STORE(cctx, ISN_STOREW, 0, name);
5177 break;
5178 case dest_tab:
5179 // include t: with the name, easier to execute that way
5180 generate_STORE(cctx, ISN_STORET, 0, name);
5181 break;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005182 case dest_env:
5183 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5184 break;
5185 case dest_reg:
5186 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5187 break;
5188 case dest_vimvar:
5189 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5190 break;
5191 case dest_script:
5192 {
5193 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5194 imported_T *import = NULL;
5195 int sid = current_sctx.sc_sid;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005196 int idx;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01005197
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005198 if (name[1] != ':')
5199 {
5200 import = find_imported(name, 0, cctx);
5201 if (import != NULL)
5202 sid = import->imp_sid;
5203 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005205 idx = get_script_item_idx(sid, rawname, TRUE);
5206 // TODO: specific type
5207 if (idx < 0)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005208 {
5209 char_u *name_s = name;
5210
5211 // Include s: in the name for store_var()
5212 if (name[1] != ':')
5213 {
5214 int len = (int)STRLEN(name) + 3;
5215
5216 name_s = alloc(len);
5217 if (name_s == NULL)
5218 name_s = name;
5219 else
5220 vim_snprintf((char *)name_s, len, "s:%s", name);
5221 }
5222 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any);
5223 if (name_s != name)
5224 vim_free(name_s);
5225 }
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005226 else
5227 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5228 sid, idx, &t_any);
5229 }
5230 break;
5231 case dest_local:
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005232 if (lvar != NULL)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005233 {
5234 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005236 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE
5237 // into ISN_STORENR
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005238 if (!lvar->lv_from_outer && instr->ga_len == instr_count + 1
5239 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005240 {
5241 varnumber_T val = isn->isn_arg.number;
5242 garray_T *stack = &cctx->ctx_type_stack;
5243
5244 isn->isn_type = ISN_STORENR;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005245 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
Bram Moolenaara471eea2020-03-04 22:20:26 +01005246 isn->isn_arg.storenr.stnr_val = val;
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005247 if (stack->ga_len > 0)
5248 --stack->ga_len;
5249 }
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005250 else if (lvar->lv_from_outer)
5251 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005252 else
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005253 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005254 }
5255 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256 }
5257 ret = p;
5258
5259theend:
5260 vim_free(name);
5261 return ret;
5262}
5263
5264/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005265 * Check if "name" can be "unlet".
5266 */
5267 int
5268check_vim9_unlet(char_u *name)
5269{
5270 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5271 {
5272 semsg(_("E1081: Cannot unlet %s"), name);
5273 return FAIL;
5274 }
5275 return OK;
5276}
5277
5278/*
5279 * Callback passed to ex_unletlock().
5280 */
5281 static int
5282compile_unlet(
5283 lval_T *lvp,
5284 char_u *name_end,
5285 exarg_T *eap,
5286 int deep UNUSED,
5287 void *coookie)
5288{
5289 cctx_T *cctx = coookie;
5290
5291 if (lvp->ll_tv == NULL)
5292 {
5293 char_u *p = lvp->ll_name;
5294 int cc = *name_end;
5295 int ret = OK;
5296
5297 // Normal name. Only supports g:, w:, t: and b: namespaces.
5298 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005299 if (*p == '$')
5300 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5301 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005302 ret = FAIL;
5303 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005304 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005305
5306 *name_end = cc;
5307 return ret;
5308 }
5309
5310 // TODO: unlet {list}[idx]
5311 // TODO: unlet {dict}[key]
5312 emsg("Sorry, :unlet not fully implemented yet");
5313 return FAIL;
5314}
5315
5316/*
5317 * compile "unlet var", "lock var" and "unlock var"
5318 * "arg" points to "var".
5319 */
5320 static char_u *
5321compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5322{
5323 char_u *p = arg;
5324
5325 if (eap->cmdidx != CMD_unlet)
5326 {
5327 emsg("Sorry, :lock and unlock not implemented yet");
5328 return NULL;
5329 }
5330
5331 if (*p == '!')
5332 {
5333 p = skipwhite(p + 1);
5334 eap->forceit = TRUE;
5335 }
5336
5337 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5338 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5339}
5340
5341/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342 * Compile an :import command.
5343 */
5344 static char_u *
5345compile_import(char_u *arg, cctx_T *cctx)
5346{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01005347 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005348}
5349
5350/*
5351 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5352 */
5353 static int
5354compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5355{
5356 garray_T *instr = &cctx->ctx_instr;
5357 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5358
5359 if (endlabel == NULL)
5360 return FAIL;
5361 endlabel->el_next = *el;
5362 *el = endlabel;
5363 endlabel->el_end_label = instr->ga_len;
5364
5365 generate_JUMP(cctx, when, 0);
5366 return OK;
5367}
5368
5369 static void
5370compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5371{
5372 garray_T *instr = &cctx->ctx_instr;
5373
5374 while (*el != NULL)
5375 {
5376 endlabel_T *cur = (*el);
5377 isn_T *isn;
5378
5379 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5380 isn->isn_arg.jump.jump_where = instr->ga_len;
5381 *el = cur->el_next;
5382 vim_free(cur);
5383 }
5384}
5385
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005386 static void
5387compile_free_jump_to_end(endlabel_T **el)
5388{
5389 while (*el != NULL)
5390 {
5391 endlabel_T *cur = (*el);
5392
5393 *el = cur->el_next;
5394 vim_free(cur);
5395 }
5396}
5397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005398/*
5399 * Create a new scope and set up the generic items.
5400 */
5401 static scope_T *
5402new_scope(cctx_T *cctx, scopetype_T type)
5403{
5404 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5405
5406 if (scope == NULL)
5407 return NULL;
5408 scope->se_outer = cctx->ctx_scope;
5409 cctx->ctx_scope = scope;
5410 scope->se_type = type;
5411 scope->se_local_count = cctx->ctx_locals.ga_len;
5412 return scope;
5413}
5414
5415/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005416 * Free the current scope and go back to the outer scope.
5417 */
5418 static void
5419drop_scope(cctx_T *cctx)
5420{
5421 scope_T *scope = cctx->ctx_scope;
5422
5423 if (scope == NULL)
5424 {
5425 iemsg("calling drop_scope() without a scope");
5426 return;
5427 }
5428 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005429 switch (scope->se_type)
5430 {
5431 case IF_SCOPE:
5432 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5433 case FOR_SCOPE:
5434 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5435 case WHILE_SCOPE:
5436 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5437 case TRY_SCOPE:
5438 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5439 case NO_SCOPE:
5440 case BLOCK_SCOPE:
5441 break;
5442 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005443 vim_free(scope);
5444}
5445
5446/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005447 * compile "if expr"
5448 *
5449 * "if expr" Produces instructions:
5450 * EVAL expr Push result of "expr"
5451 * JUMP_IF_FALSE end
5452 * ... body ...
5453 * end:
5454 *
5455 * "if expr | else" Produces instructions:
5456 * EVAL expr Push result of "expr"
5457 * JUMP_IF_FALSE else
5458 * ... body ...
5459 * JUMP_ALWAYS end
5460 * else:
5461 * ... body ...
5462 * end:
5463 *
5464 * "if expr1 | elseif expr2 | else" Produces instructions:
5465 * EVAL expr Push result of "expr"
5466 * JUMP_IF_FALSE elseif
5467 * ... body ...
5468 * JUMP_ALWAYS end
5469 * elseif:
5470 * EVAL expr Push result of "expr"
5471 * JUMP_IF_FALSE else
5472 * ... body ...
5473 * JUMP_ALWAYS end
5474 * else:
5475 * ... body ...
5476 * end:
5477 */
5478 static char_u *
5479compile_if(char_u *arg, cctx_T *cctx)
5480{
5481 char_u *p = arg;
5482 garray_T *instr = &cctx->ctx_instr;
5483 scope_T *scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005484 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005485
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005486 // compile "expr"; if we know it evaluates to FALSE skip the block
5487 tv.v_type = VAR_UNKNOWN;
5488 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5489 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5490 else
5491 cctx->ctx_skip = MAYBE;
5492 clear_tv(&tv);
5493 if (cctx->ctx_skip == MAYBE)
5494 {
5495 p = arg;
5496 if (compile_expr1(&p, cctx) == FAIL)
5497 return NULL;
5498 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005499
5500 scope = new_scope(cctx, IF_SCOPE);
5501 if (scope == NULL)
5502 return NULL;
5503
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005504 if (cctx->ctx_skip == MAYBE)
5505 {
5506 // "where" is set when ":elseif", "else" or ":endif" is found
5507 scope->se_u.se_if.is_if_label = instr->ga_len;
5508 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5509 }
5510 else
5511 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005512
5513 return p;
5514}
5515
5516 static char_u *
5517compile_elseif(char_u *arg, cctx_T *cctx)
5518{
5519 char_u *p = arg;
5520 garray_T *instr = &cctx->ctx_instr;
5521 isn_T *isn;
5522 scope_T *scope = cctx->ctx_scope;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005523 typval_T tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005524
5525 if (scope == NULL || scope->se_type != IF_SCOPE)
5526 {
5527 emsg(_(e_elseif_without_if));
5528 return NULL;
5529 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005530 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005531
Bram Moolenaar158906c2020-02-06 20:39:45 +01005532 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005533 {
5534 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005535 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005536 return NULL;
5537 // previous "if" or "elseif" jumps here
5538 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5539 isn->isn_arg.jump.jump_where = instr->ga_len;
5540 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005541
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005542 // compile "expr"; if we know it evaluates to FALSE skip the block
5543 tv.v_type = VAR_UNKNOWN;
5544 if (evaluate_const_expr1(&p, cctx, &tv) == OK)
5545 cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE;
5546 else
5547 cctx->ctx_skip = MAYBE;
5548 clear_tv(&tv);
5549 if (cctx->ctx_skip == MAYBE)
5550 {
5551 p = arg;
5552 if (compile_expr1(&p, cctx) == FAIL)
5553 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005554
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005555 // "where" is set when ":elseif", "else" or ":endif" is found
5556 scope->se_u.se_if.is_if_label = instr->ga_len;
5557 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5558 }
5559 else
5560 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005561
5562 return p;
5563}
5564
5565 static char_u *
5566compile_else(char_u *arg, cctx_T *cctx)
5567{
5568 char_u *p = arg;
5569 garray_T *instr = &cctx->ctx_instr;
5570 isn_T *isn;
5571 scope_T *scope = cctx->ctx_scope;
5572
5573 if (scope == NULL || scope->se_type != IF_SCOPE)
5574 {
5575 emsg(_(e_else_without_if));
5576 return NULL;
5577 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005578 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005579
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005580 // jump from previous block to the end, unless the else block is empty
5581 if (cctx->ctx_skip == MAYBE)
5582 {
5583 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005585 return NULL;
5586 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005587
Bram Moolenaar158906c2020-02-06 20:39:45 +01005588 if (cctx->ctx_skip == MAYBE)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005589 {
5590 if (scope->se_u.se_if.is_if_label >= 0)
5591 {
5592 // previous "if" or "elseif" jumps here
5593 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5594 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005595 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005596 }
5597 }
5598
5599 if (cctx->ctx_skip != MAYBE)
5600 cctx->ctx_skip = !cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005601
5602 return p;
5603}
5604
5605 static char_u *
5606compile_endif(char_u *arg, cctx_T *cctx)
5607{
5608 scope_T *scope = cctx->ctx_scope;
5609 ifscope_T *ifscope;
5610 garray_T *instr = &cctx->ctx_instr;
5611 isn_T *isn;
5612
5613 if (scope == NULL || scope->se_type != IF_SCOPE)
5614 {
5615 emsg(_(e_endif_without_if));
5616 return NULL;
5617 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005618 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005619 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005620
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005621 if (scope->se_u.se_if.is_if_label >= 0)
5622 {
5623 // previous "if" or "elseif" jumps here
5624 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5625 isn->isn_arg.jump.jump_where = instr->ga_len;
5626 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005627 // Fill in the "end" label in jumps at the end of the blocks.
5628 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005629 cctx->ctx_skip = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005630
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005631 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005632 return arg;
5633}
5634
5635/*
5636 * compile "for var in expr"
5637 *
5638 * Produces instructions:
5639 * PUSHNR -1
5640 * STORE loop-idx Set index to -1
5641 * EVAL expr Push result of "expr"
5642 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5643 * - if beyond end, jump to "end"
5644 * - otherwise get item from list and push it
5645 * STORE var Store item in "var"
5646 * ... body ...
5647 * JUMP top Jump back to repeat
5648 * end: DROP Drop the result of "expr"
5649 *
5650 */
5651 static char_u *
5652compile_for(char_u *arg, cctx_T *cctx)
5653{
5654 char_u *p;
5655 size_t varlen;
5656 garray_T *instr = &cctx->ctx_instr;
5657 garray_T *stack = &cctx->ctx_type_stack;
5658 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005659 lvar_T *loop_lvar; // loop iteration variable
5660 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005661 type_T *vartype;
5662
5663 // TODO: list of variables: "for [key, value] in dict"
5664 // parse "var"
5665 for (p = arg; eval_isnamec1(*p); ++p)
5666 ;
5667 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005668 var_lvar = lookup_local(arg, varlen, cctx);
5669 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005670 {
5671 semsg(_("E1023: variable already defined: %s"), arg);
5672 return NULL;
5673 }
5674
5675 // consume "in"
5676 p = skipwhite(p);
5677 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5678 {
5679 emsg(_(e_missing_in));
5680 return NULL;
5681 }
5682 p = skipwhite(p + 2);
5683
5684
5685 scope = new_scope(cctx, FOR_SCOPE);
5686 if (scope == NULL)
5687 return NULL;
5688
5689 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005690 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5691 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005692 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005693 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005694 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005695 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005697
5698 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005699 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5700 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005701 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005702 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005703 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005704 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005705 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005706
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005707 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005708
5709 // compile "expr", it remains on the stack until "endfor"
5710 arg = p;
5711 if (compile_expr1(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005712 {
5713 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005714 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005715 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005716
5717 // now we know the type of "var"
5718 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5719 if (vartype->tt_type != VAR_LIST)
5720 {
5721 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005722 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005723 return NULL;
5724 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005725 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005726 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005727
5728 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005729 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005730
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005731 generate_FOR(cctx, loop_lvar->lv_idx);
5732 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005733
5734 return arg;
5735}
5736
5737/*
5738 * compile "endfor"
5739 */
5740 static char_u *
5741compile_endfor(char_u *arg, cctx_T *cctx)
5742{
5743 garray_T *instr = &cctx->ctx_instr;
5744 scope_T *scope = cctx->ctx_scope;
5745 forscope_T *forscope;
5746 isn_T *isn;
5747
5748 if (scope == NULL || scope->se_type != FOR_SCOPE)
5749 {
5750 emsg(_(e_for));
5751 return NULL;
5752 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005753 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005754 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005755 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005756
5757 // At end of ":for" scope jump back to the FOR instruction.
5758 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5759
5760 // Fill in the "end" label in the FOR statement so it can jump here
5761 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5762 isn->isn_arg.forloop.for_end = instr->ga_len;
5763
5764 // Fill in the "end" label any BREAK statements
5765 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5766
5767 // Below the ":for" scope drop the "expr" list from the stack.
5768 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5769 return NULL;
5770
5771 vim_free(scope);
5772
5773 return arg;
5774}
5775
5776/*
5777 * compile "while expr"
5778 *
5779 * Produces instructions:
5780 * top: EVAL expr Push result of "expr"
5781 * JUMP_IF_FALSE end jump if false
5782 * ... body ...
5783 * JUMP top Jump back to repeat
5784 * end:
5785 *
5786 */
5787 static char_u *
5788compile_while(char_u *arg, cctx_T *cctx)
5789{
5790 char_u *p = arg;
5791 garray_T *instr = &cctx->ctx_instr;
5792 scope_T *scope;
5793
5794 scope = new_scope(cctx, WHILE_SCOPE);
5795 if (scope == NULL)
5796 return NULL;
5797
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005798 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005799
5800 // compile "expr"
5801 if (compile_expr1(&p, cctx) == FAIL)
5802 return NULL;
5803
5804 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005805 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005806 JUMP_IF_FALSE, cctx) == FAIL)
5807 return FAIL;
5808
5809 return p;
5810}
5811
5812/*
5813 * compile "endwhile"
5814 */
5815 static char_u *
5816compile_endwhile(char_u *arg, cctx_T *cctx)
5817{
5818 scope_T *scope = cctx->ctx_scope;
5819
5820 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5821 {
5822 emsg(_(e_while));
5823 return NULL;
5824 }
5825 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005826 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005827
5828 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005829 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005830
5831 // Fill in the "end" label in the WHILE statement so it can jump here.
5832 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005833 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005834
5835 vim_free(scope);
5836
5837 return arg;
5838}
5839
5840/*
5841 * compile "continue"
5842 */
5843 static char_u *
5844compile_continue(char_u *arg, cctx_T *cctx)
5845{
5846 scope_T *scope = cctx->ctx_scope;
5847
5848 for (;;)
5849 {
5850 if (scope == NULL)
5851 {
5852 emsg(_(e_continue));
5853 return NULL;
5854 }
5855 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5856 break;
5857 scope = scope->se_outer;
5858 }
5859
5860 // Jump back to the FOR or WHILE instruction.
5861 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005862 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5863 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005864 return arg;
5865}
5866
5867/*
5868 * compile "break"
5869 */
5870 static char_u *
5871compile_break(char_u *arg, cctx_T *cctx)
5872{
5873 scope_T *scope = cctx->ctx_scope;
5874 endlabel_T **el;
5875
5876 for (;;)
5877 {
5878 if (scope == NULL)
5879 {
5880 emsg(_(e_break));
5881 return NULL;
5882 }
5883 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5884 break;
5885 scope = scope->se_outer;
5886 }
5887
5888 // Jump to the end of the FOR or WHILE loop.
5889 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005890 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005891 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005892 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005893 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5894 return FAIL;
5895
5896 return arg;
5897}
5898
5899/*
5900 * compile "{" start of block
5901 */
5902 static char_u *
5903compile_block(char_u *arg, cctx_T *cctx)
5904{
5905 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5906 return NULL;
5907 return skipwhite(arg + 1);
5908}
5909
5910/*
5911 * compile end of block: drop one scope
5912 */
5913 static void
5914compile_endblock(cctx_T *cctx)
5915{
5916 scope_T *scope = cctx->ctx_scope;
5917
5918 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005919 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005920 vim_free(scope);
5921}
5922
5923/*
5924 * compile "try"
5925 * Creates a new scope for the try-endtry, pointing to the first catch and
5926 * finally.
5927 * Creates another scope for the "try" block itself.
5928 * TRY instruction sets up exception handling at runtime.
5929 *
5930 * "try"
5931 * TRY -> catch1, -> finally push trystack entry
5932 * ... try block
5933 * "throw {exception}"
5934 * EVAL {exception}
5935 * THROW create exception
5936 * ... try block
5937 * " catch {expr}"
5938 * JUMP -> finally
5939 * catch1: PUSH exeception
5940 * EVAL {expr}
5941 * MATCH
5942 * JUMP nomatch -> catch2
5943 * CATCH remove exception
5944 * ... catch block
5945 * " catch"
5946 * JUMP -> finally
5947 * catch2: CATCH remove exception
5948 * ... catch block
5949 * " finally"
5950 * finally:
5951 * ... finally block
5952 * " endtry"
5953 * ENDTRY pop trystack entry, may rethrow
5954 */
5955 static char_u *
5956compile_try(char_u *arg, cctx_T *cctx)
5957{
5958 garray_T *instr = &cctx->ctx_instr;
5959 scope_T *try_scope;
5960 scope_T *scope;
5961
5962 // scope that holds the jumps that go to catch/finally/endtry
5963 try_scope = new_scope(cctx, TRY_SCOPE);
5964 if (try_scope == NULL)
5965 return NULL;
5966
5967 // "catch" is set when the first ":catch" is found.
5968 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005969 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005970 if (generate_instr(cctx, ISN_TRY) == NULL)
5971 return NULL;
5972
5973 // scope for the try block itself
5974 scope = new_scope(cctx, BLOCK_SCOPE);
5975 if (scope == NULL)
5976 return NULL;
5977
5978 return arg;
5979}
5980
5981/*
5982 * compile "catch {expr}"
5983 */
5984 static char_u *
5985compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5986{
5987 scope_T *scope = cctx->ctx_scope;
5988 garray_T *instr = &cctx->ctx_instr;
5989 char_u *p;
5990 isn_T *isn;
5991
5992 // end block scope from :try or :catch
5993 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5994 compile_endblock(cctx);
5995 scope = cctx->ctx_scope;
5996
5997 // Error if not in a :try scope
5998 if (scope == NULL || scope->se_type != TRY_SCOPE)
5999 {
6000 emsg(_(e_catch));
6001 return NULL;
6002 }
6003
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006004 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006005 {
6006 emsg(_("E1033: catch unreachable after catch-all"));
6007 return NULL;
6008 }
6009
6010 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006011 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006012 JUMP_ALWAYS, cctx) == FAIL)
6013 return NULL;
6014
6015 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006016 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006017 if (isn->isn_arg.try.try_catch == 0)
6018 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006019 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006020 {
6021 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006022 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006023 isn->isn_arg.jump.jump_where = instr->ga_len;
6024 }
6025
6026 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006027 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006028 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006029 scope->se_u.se_try.ts_caught_all = TRUE;
6030 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006031 }
6032 else
6033 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006034 char_u *end;
6035 char_u *pat;
6036 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006037 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006038 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006039
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006040 // Push v:exception, push {expr} and MATCH
6041 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6042
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006043 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006044 if (*end != *p)
6045 {
6046 semsg(_("E1067: Separator mismatch: %s"), p);
6047 vim_free(tofree);
6048 return FAIL;
6049 }
6050 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006051 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006052 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006053 len = (int)(end - tofree);
6054 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006055 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006056 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006057 if (pat == NULL)
6058 return FAIL;
6059 if (generate_PUSHS(cctx, pat) == FAIL)
6060 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006062 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6063 return NULL;
6064
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006065 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6067 return NULL;
6068 }
6069
6070 if (generate_instr(cctx, ISN_CATCH) == NULL)
6071 return NULL;
6072
6073 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6074 return NULL;
6075 return p;
6076}
6077
6078 static char_u *
6079compile_finally(char_u *arg, cctx_T *cctx)
6080{
6081 scope_T *scope = cctx->ctx_scope;
6082 garray_T *instr = &cctx->ctx_instr;
6083 isn_T *isn;
6084
6085 // end block scope from :try or :catch
6086 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6087 compile_endblock(cctx);
6088 scope = cctx->ctx_scope;
6089
6090 // Error if not in a :try scope
6091 if (scope == NULL || scope->se_type != TRY_SCOPE)
6092 {
6093 emsg(_(e_finally));
6094 return NULL;
6095 }
6096
6097 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006098 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006099 if (isn->isn_arg.try.try_finally != 0)
6100 {
6101 emsg(_(e_finally_dup));
6102 return NULL;
6103 }
6104
6105 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006106 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006107
Bram Moolenaar585fea72020-04-02 22:33:21 +02006108 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006109 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006110 {
6111 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006112 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006113 isn->isn_arg.jump.jump_where = instr->ga_len;
6114 }
6115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006116 // TODO: set index in ts_finally_label jumps
6117
6118 return arg;
6119}
6120
6121 static char_u *
6122compile_endtry(char_u *arg, cctx_T *cctx)
6123{
6124 scope_T *scope = cctx->ctx_scope;
6125 garray_T *instr = &cctx->ctx_instr;
6126 isn_T *isn;
6127
6128 // end block scope from :catch or :finally
6129 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6130 compile_endblock(cctx);
6131 scope = cctx->ctx_scope;
6132
6133 // Error if not in a :try scope
6134 if (scope == NULL || scope->se_type != TRY_SCOPE)
6135 {
6136 if (scope == NULL)
6137 emsg(_(e_no_endtry));
6138 else if (scope->se_type == WHILE_SCOPE)
6139 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006140 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006141 emsg(_(e_endfor));
6142 else
6143 emsg(_(e_endif));
6144 return NULL;
6145 }
6146
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006147 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006148 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6149 {
6150 emsg(_("E1032: missing :catch or :finally"));
6151 return NULL;
6152 }
6153
6154 // Fill in the "end" label in jumps at the end of the blocks, if not done
6155 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006156 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006157
6158 // End :catch or :finally scope: set value in ISN_TRY instruction
6159 if (isn->isn_arg.try.try_finally == 0)
6160 isn->isn_arg.try.try_finally = instr->ga_len;
6161 compile_endblock(cctx);
6162
6163 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6164 return NULL;
6165 return arg;
6166}
6167
6168/*
6169 * compile "throw {expr}"
6170 */
6171 static char_u *
6172compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6173{
6174 char_u *p = skipwhite(arg);
6175
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006176 if (compile_expr1(&p, cctx) == FAIL)
6177 return NULL;
6178 if (may_generate_2STRING(-1, cctx) == FAIL)
6179 return NULL;
6180 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6181 return NULL;
6182
6183 return p;
6184}
6185
6186/*
6187 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006188 * compile "echomsg expr"
6189 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006190 * compile "execute expr"
6191 */
6192 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006193compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006194{
6195 char_u *p = arg;
6196 int count = 0;
6197
6198 for (;;)
6199 {
6200 if (compile_expr1(&p, cctx) == FAIL)
6201 return NULL;
6202 ++count;
6203 p = skipwhite(p);
6204 if (ends_excmd(*p))
6205 break;
6206 }
6207
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006208 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6209 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6210 else if (cmdidx == CMD_execute)
6211 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6212 else if (cmdidx == CMD_echomsg)
6213 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6214 else
6215 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006216 return p;
6217}
6218
6219/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006220 * A command that is not compiled, execute with legacy code.
6221 */
6222 static char_u *
6223compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6224{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006225 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006226 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006227
6228 if (cctx->ctx_skip == TRUE)
6229 goto theend;
6230
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006231 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
6232 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006233 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6234 {
6235 // expand filename in "syntax include [@group] filename"
6236 has_expr = TRUE;
6237 eap->arg = skipwhite(eap->arg + 7);
6238 if (*eap->arg == '@')
6239 eap->arg = skiptowhite(eap->arg);
6240 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006241
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006242 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006243 {
6244 int count = 0;
6245 char_u *start = skipwhite(line);
6246
6247 // :cmd xxx`=expr1`yyy`=expr2`zzz
6248 // PUSHS ":cmd xxx"
6249 // eval expr1
6250 // PUSHS "yyy"
6251 // eval expr2
6252 // PUSHS "zzz"
6253 // EXECCONCAT 5
6254 for (;;)
6255 {
6256 if (p > start)
6257 {
6258 generate_PUSHS(cctx, vim_strnsave(start, (int)(p - start)));
6259 ++count;
6260 }
6261 p += 2;
6262 if (compile_expr1(&p, cctx) == FAIL)
6263 return NULL;
6264 may_generate_2STRING(-1, cctx);
6265 ++count;
6266 p = skipwhite(p);
6267 if (*p != '`')
6268 {
6269 emsg(_("E1083: missing backtick"));
6270 return NULL;
6271 }
6272 start = p + 1;
6273
6274 p = (char_u *)strstr((char *)start, "`=");
6275 if (p == NULL)
6276 {
6277 if (*skipwhite(start) != NUL)
6278 {
6279 generate_PUSHS(cctx, vim_strsave(start));
6280 ++count;
6281 }
6282 break;
6283 }
6284 }
6285 generate_EXECCONCAT(cctx, count);
6286 }
6287 else
6288 generate_EXEC(cctx, line);
6289
6290theend:
6291 return (char_u *)"";
6292}
6293
6294/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006295 * After ex_function() has collected all the function lines: parse and compile
6296 * the lines into instructions.
6297 * Adds the function to "def_functions".
6298 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6299 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006300 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006301 * This can be used recursively through compile_lambda(), which may reallocate
6302 * "def_functions".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 */
6304 void
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006305compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006306{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307 char_u *line = NULL;
6308 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006309 char *errormsg = NULL; // error message
6310 int had_return = FALSE;
6311 cctx_T cctx;
6312 garray_T *instr;
6313 int called_emsg_before = called_emsg;
6314 int ret = FAIL;
6315 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006316 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006319 dfunc_T *dfunc; // may be invalidated by compile_lambda()
Bram Moolenaar20431c92020-03-20 18:39:46 +01006320
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006321 if (ufunc->uf_dfunc_idx >= 0)
6322 {
6323 // Redefining a function that was compiled before.
6324 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
6325
6326 // Free old instructions.
6327 delete_def_function_contents(dfunc);
6328 }
6329 else
6330 {
6331 // Add the function to "def_functions".
6332 if (ga_grow(&def_functions, 1) == FAIL)
6333 return;
6334 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006335 CLEAR_POINTER(dfunc);
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006336 dfunc->df_idx = def_functions.ga_len;
6337 ufunc->uf_dfunc_idx = dfunc->df_idx;
6338 dfunc->df_ufunc = ufunc;
6339 ++def_functions.ga_len;
6340 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006341 }
6342
Bram Moolenaara80faa82020-04-12 19:37:17 +02006343 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006344 cctx.ctx_ufunc = ufunc;
6345 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006346 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006347 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6348 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6349 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6350 cctx.ctx_type_list = &ufunc->uf_type_list;
6351 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6352 instr = &cctx.ctx_instr;
6353
6354 // Most modern script version.
6355 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6356
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006357 if (ufunc->uf_def_args.ga_len > 0)
6358 {
6359 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006360 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006361 int i;
6362 char_u *arg;
6363 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6364
6365 // Produce instructions for the default values of optional arguments.
6366 // Store the instruction index in uf_def_arg_idx[] so that we know
6367 // where to start when the function is called, depending on the number
6368 // of arguments.
6369 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6370 if (ufunc->uf_def_arg_idx == NULL)
6371 goto erret;
6372 for (i = 0; i < count; ++i)
6373 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006374 garray_T *stack = &cctx.ctx_type_stack;
6375 type_T *val_type;
6376 int arg_idx = first_def_arg + i;
6377
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006378 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6379 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006380 if (compile_expr1(&arg, &cctx) == FAIL)
6381 goto erret;
6382
6383 // If no type specified use the type of the default value.
6384 // Otherwise check that the default value type matches the
6385 // specified type.
6386 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6387 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6388 ufunc->uf_arg_types[arg_idx] = val_type;
6389 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6390 == FAIL)
6391 {
6392 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6393 arg_idx + 1);
6394 goto erret;
6395 }
6396
6397 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006398 goto erret;
6399 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006400 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6401 }
6402
6403 /*
6404 * Loop over all the lines of the function and generate instructions.
6405 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406 for (;;)
6407 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006408 exarg_T ea;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006409 int is_ex_command = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006410
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006411 // Bail out on the first error to avoid a flood of errors and report
6412 // the right line number when inside try/catch.
6413 if (emsg_before != called_emsg)
6414 goto erret;
6415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006416 if (line != NULL && *line == '|')
6417 // the line continues after a '|'
6418 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006419 else if (line != NULL && *line != NUL
6420 && !(*line == '#' && (line == cctx.ctx_line_start
6421 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006422 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006423 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006424 goto erret;
6425 }
6426 else
6427 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006428 line = next_line_from_context(&cctx);
6429 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006430 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006431 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006432 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006433 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006434
6435 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006436 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006437 ea.cmdlinep = &line;
6438 ea.cmd = skipwhite(line);
6439
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006440 // Some things can be recognized by the first character.
6441 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006442 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006443 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006444 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006445 if (ea.cmd[1] != '{')
6446 {
6447 line = (char_u *)"";
6448 continue;
6449 }
6450 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006451
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006452 case '}':
6453 {
6454 // "}" ends a block scope
6455 scopetype_T stype = cctx.ctx_scope == NULL
6456 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006457
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006458 if (stype == BLOCK_SCOPE)
6459 {
6460 compile_endblock(&cctx);
6461 line = ea.cmd;
6462 }
6463 else
6464 {
6465 emsg(_("E1025: using } outside of a block scope"));
6466 goto erret;
6467 }
6468 if (line != NULL)
6469 line = skipwhite(ea.cmd + 1);
6470 continue;
6471 }
6472
6473 case '{':
6474 // "{" starts a block scope
6475 // "{'a': 1}->func() is something else
6476 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6477 {
6478 line = compile_block(ea.cmd, &cctx);
6479 continue;
6480 }
6481 break;
6482
6483 case ':':
6484 is_ex_command = TRUE;
6485 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486 }
6487
6488 /*
6489 * COMMAND MODIFIERS
6490 */
6491 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6492 {
6493 if (errormsg != NULL)
6494 goto erret;
6495 // empty line or comment
6496 line = (char_u *)"";
6497 continue;
6498 }
6499
6500 // Skip ":call" to get to the function name.
6501 if (checkforcmd(&ea.cmd, "call", 3))
6502 ea.cmd = skipwhite(ea.cmd);
6503
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006504 if (!is_ex_command)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006505 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006506 // Assuming the command starts with a variable or function name,
6507 // find what follows. Also "&opt = val", "$ENV = val" and "@r =
6508 // val".
6509 p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
6510 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006511 p = to_name_end(p, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006512 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006513 {
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006514 int oplen;
6515 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006516
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006517 oplen = assignment_len(skipwhite(p), &heredoc);
6518 if (oplen > 0)
6519 {
6520 // Recognize an assignment if we recognize the variable
6521 // name:
6522 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006523 // "local = expr" where "local" is a local var.
6524 // "script = expr" where "script" is a script-local var.
6525 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006526 // "&opt = expr"
6527 // "$ENV = expr"
6528 // "@r = expr"
6529 if (*ea.cmd == '&'
6530 || *ea.cmd == '$'
6531 || *ea.cmd == '@'
6532 || ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006533 || lookup_local(ea.cmd, p - ea.cmd, &cctx) != NULL
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006534 || lookup_script(ea.cmd, p - ea.cmd) == OK
6535 || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL)
6536 {
6537 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6538 if (line == NULL)
6539 goto erret;
6540 continue;
6541 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 }
6543 }
6544 }
6545
6546 /*
6547 * COMMAND after range
6548 */
6549 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006550 p = find_ex_command(&ea, NULL, is_ex_command ? NULL
6551 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006552 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006553
6554 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6555 {
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006556 if (cctx.ctx_skip == TRUE)
6557 {
6558 line += STRLEN(line);
6559 continue;
6560 }
6561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006562 // Expression or function call.
6563 if (ea.cmdidx == CMD_eval)
6564 {
6565 p = ea.cmd;
6566 if (compile_expr1(&p, &cctx) == FAIL)
6567 goto erret;
6568
6569 // drop the return value
6570 generate_instr_drop(&cctx, ISN_DROP, 1);
6571 line = p;
6572 continue;
6573 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006574 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006575 iemsg("Command from find_ex_command() not handled");
6576 goto erret;
6577 }
6578
6579 p = skipwhite(p);
6580
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006581 if (cctx.ctx_skip == TRUE
6582 && ea.cmdidx != CMD_elseif
6583 && ea.cmdidx != CMD_else
6584 && ea.cmdidx != CMD_endif)
6585 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006586 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006587 continue;
6588 }
6589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006590 switch (ea.cmdidx)
6591 {
6592 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006593 ea.arg = p;
6594 line = compile_nested_function(&ea, &cctx);
6595 break;
6596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006598 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 goto erret;
6600
6601 case CMD_return:
6602 line = compile_return(p, set_return_type, &cctx);
6603 had_return = TRUE;
6604 break;
6605
6606 case CMD_let:
6607 case CMD_const:
6608 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
6609 break;
6610
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006611 case CMD_unlet:
6612 case CMD_unlockvar:
6613 case CMD_lockvar:
6614 line = compile_unletlock(p, &ea, &cctx);
6615 break;
6616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617 case CMD_import:
6618 line = compile_import(p, &cctx);
6619 break;
6620
6621 case CMD_if:
6622 line = compile_if(p, &cctx);
6623 break;
6624 case CMD_elseif:
6625 line = compile_elseif(p, &cctx);
6626 break;
6627 case CMD_else:
6628 line = compile_else(p, &cctx);
6629 break;
6630 case CMD_endif:
6631 line = compile_endif(p, &cctx);
6632 break;
6633
6634 case CMD_while:
6635 line = compile_while(p, &cctx);
6636 break;
6637 case CMD_endwhile:
6638 line = compile_endwhile(p, &cctx);
6639 break;
6640
6641 case CMD_for:
6642 line = compile_for(p, &cctx);
6643 break;
6644 case CMD_endfor:
6645 line = compile_endfor(p, &cctx);
6646 break;
6647 case CMD_continue:
6648 line = compile_continue(p, &cctx);
6649 break;
6650 case CMD_break:
6651 line = compile_break(p, &cctx);
6652 break;
6653
6654 case CMD_try:
6655 line = compile_try(p, &cctx);
6656 break;
6657 case CMD_catch:
6658 line = compile_catch(p, &cctx);
6659 break;
6660 case CMD_finally:
6661 line = compile_finally(p, &cctx);
6662 break;
6663 case CMD_endtry:
6664 line = compile_endtry(p, &cctx);
6665 break;
6666 case CMD_throw:
6667 line = compile_throw(p, &cctx);
6668 break;
6669
6670 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006671 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006672 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006673 case CMD_echomsg:
6674 case CMD_echoerr:
6675 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006676 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006677
6678 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006679 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006680 // Not recognized, execute with do_cmdline_cmd().
6681 ea.arg = p;
6682 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683 break;
6684 }
6685 if (line == NULL)
6686 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006687 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688
6689 if (cctx.ctx_type_stack.ga_len < 0)
6690 {
6691 iemsg("Type stack underflow");
6692 goto erret;
6693 }
6694 }
6695
6696 if (cctx.ctx_scope != NULL)
6697 {
6698 if (cctx.ctx_scope->se_type == IF_SCOPE)
6699 emsg(_(e_endif));
6700 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6701 emsg(_(e_endwhile));
6702 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6703 emsg(_(e_endfor));
6704 else
6705 emsg(_("E1026: Missing }"));
6706 goto erret;
6707 }
6708
6709 if (!had_return)
6710 {
6711 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6712 {
6713 emsg(_("E1027: Missing return statement"));
6714 goto erret;
6715 }
6716
6717 // Return zero if there is no return at the end.
6718 generate_PUSHNR(&cctx, 0);
6719 generate_instr(&cctx, ISN_RETURN);
6720 }
6721
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006722 {
6723 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6724 + ufunc->uf_dfunc_idx;
6725 dfunc->df_deleted = FALSE;
6726 dfunc->df_instr = instr->ga_data;
6727 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006728 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006729 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006730 if (cctx.ctx_outer_used)
6731 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006732 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006733
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006734 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006735 int varargs = ufunc->uf_va_name != NULL;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006736 int argcount = ufunc->uf_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006737
6738 // Create a type for the function, with the return type and any
6739 // argument types.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006740 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6741 // The type is included in "tt_args".
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006742 if (argcount > 0 || varargs)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006743 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006744 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6745 argcount, &ufunc->uf_type_list);
6746 // Add argument types to the function type.
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006747 if (func_type_add_arg_types(ufunc->uf_func_type,
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006748 argcount + varargs,
6749 &ufunc->uf_type_list) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006750 {
6751 ret = FAIL;
6752 goto erret;
6753 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006754 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6755 ufunc->uf_func_type->tt_min_argcount =
6756 argcount - ufunc->uf_def_args.ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006757 if (ufunc->uf_arg_types == NULL)
6758 {
6759 int i;
6760
6761 // lambda does not have argument types.
6762 for (i = 0; i < argcount; ++i)
6763 ufunc->uf_func_type->tt_args[i] = &t_any;
6764 }
6765 else
6766 mch_memmove(ufunc->uf_func_type->tt_args,
6767 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006768 if (varargs)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006769 {
Bram Moolenaar5d905c22020-04-05 18:20:45 +02006770 ufunc->uf_func_type->tt_args[argcount] =
6771 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006772 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
6773 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006774 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02006775 else
6776 // No arguments, can use a predefined type.
6777 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
6778 argcount, &ufunc->uf_type_list);
6779
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006780 }
6781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006782 ret = OK;
6783
6784erret:
6785 if (ret == FAIL)
6786 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006787 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006788 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6789 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006790
6791 for (idx = 0; idx < instr->ga_len; ++idx)
6792 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006793 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006794
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006795 ufunc->uf_dfunc_idx = -1;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006796 if (!dfunc->df_deleted)
6797 --def_functions.ga_len;
6798
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006799 while (cctx.ctx_scope != NULL)
6800 drop_scope(&cctx);
6801
Bram Moolenaar20431c92020-03-20 18:39:46 +01006802 // Don't execute this function body.
6803 ga_clear_strings(&ufunc->uf_lines);
6804
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805 if (errormsg != NULL)
6806 emsg(errormsg);
6807 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01006808 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006809 }
6810
6811 current_sctx = save_current_sctx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006812 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006813 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006814 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006815}
6816
6817/*
6818 * Delete an instruction, free what it contains.
6819 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01006820 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006821delete_instr(isn_T *isn)
6822{
6823 switch (isn->isn_type)
6824 {
6825 case ISN_EXEC:
6826 case ISN_LOADENV:
6827 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006828 case ISN_LOADB:
6829 case ISN_LOADW:
6830 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006831 case ISN_LOADOPT:
6832 case ISN_MEMBER:
6833 case ISN_PUSHEXC:
6834 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006835 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006836 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02006837 case ISN_STOREB:
6838 case ISN_STOREW:
6839 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006840 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006841 vim_free(isn->isn_arg.string);
6842 break;
6843
6844 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006845 case ISN_STORES:
6846 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006847 break;
6848
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006849 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006850 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006851 vim_free(isn->isn_arg.unlet.ul_name);
6852 break;
6853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006854 case ISN_STOREOPT:
6855 vim_free(isn->isn_arg.storeopt.so_name);
6856 break;
6857
6858 case ISN_PUSHBLOB: // push blob isn_arg.blob
6859 blob_unref(isn->isn_arg.blob);
6860 break;
6861
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006862 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006863#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006864 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006865#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006866 break;
6867
6868 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006869#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006870 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01006871#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006872 break;
6873
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006874 case ISN_UCALL:
6875 vim_free(isn->isn_arg.ufunc.cuf_name);
6876 break;
6877
Bram Moolenaar221fcc72020-05-05 19:46:20 +02006878 case ISN_FUNCREF:
6879 {
6880 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6881 + isn->isn_arg.funcref.fr_func;
6882 func_ptr_unref(dfunc->df_ufunc);
6883 }
6884 break;
6885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006886 case ISN_2BOOL:
6887 case ISN_2STRING:
6888 case ISN_ADDBLOB:
6889 case ISN_ADDLIST:
6890 case ISN_BCALL:
6891 case ISN_CATCH:
6892 case ISN_CHECKNR:
6893 case ISN_CHECKTYPE:
6894 case ISN_COMPAREANY:
6895 case ISN_COMPAREBLOB:
6896 case ISN_COMPAREBOOL:
6897 case ISN_COMPAREDICT:
6898 case ISN_COMPAREFLOAT:
6899 case ISN_COMPAREFUNC:
6900 case ISN_COMPARELIST:
6901 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006902 case ISN_COMPARESPECIAL:
6903 case ISN_COMPARESTRING:
6904 case ISN_CONCAT:
6905 case ISN_DCALL:
6906 case ISN_DROP:
6907 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006908 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006909 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006910 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006911 case ISN_EXECCONCAT:
6912 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006913 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914 case ISN_INDEX:
6915 case ISN_JUMP:
6916 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006917 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006918 case ISN_LOADSCRIPT:
6919 case ISN_LOADREG:
6920 case ISN_LOADV:
6921 case ISN_NEGATENR:
6922 case ISN_NEWDICT:
6923 case ISN_NEWLIST:
6924 case ISN_OPNR:
6925 case ISN_OPFLOAT:
6926 case ISN_OPANY:
6927 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006928 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929 case ISN_PUSHF:
6930 case ISN_PUSHNR:
6931 case ISN_PUSHBOOL:
6932 case ISN_PUSHSPEC:
6933 case ISN_RETURN:
6934 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02006935 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006936 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006937 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01006938 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006939 case ISN_STORESCRIPT:
6940 case ISN_THROW:
6941 case ISN_TRY:
6942 // nothing allocated
6943 break;
6944 }
6945}
6946
6947/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01006948 * Free all instructions for "dfunc".
6949 */
6950 static void
6951delete_def_function_contents(dfunc_T *dfunc)
6952{
6953 int idx;
6954
6955 ga_clear(&dfunc->df_def_args_isn);
6956
6957 if (dfunc->df_instr != NULL)
6958 {
6959 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
6960 delete_instr(dfunc->df_instr + idx);
6961 VIM_CLEAR(dfunc->df_instr);
6962 }
6963
6964 dfunc->df_deleted = TRUE;
6965}
6966
6967/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006968 * When a user function is deleted, delete any associated def function.
6969 */
6970 void
6971delete_def_function(ufunc_T *ufunc)
6972{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973 if (ufunc->uf_dfunc_idx >= 0)
6974 {
6975 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6976 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977
Bram Moolenaar20431c92020-03-20 18:39:46 +01006978 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979 }
6980}
6981
6982#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006983/*
6984 * Free all functions defined with ":def".
6985 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006986 void
6987free_def_functions(void)
6988{
Bram Moolenaar20431c92020-03-20 18:39:46 +01006989 int idx;
6990
6991 for (idx = 0; idx < def_functions.ga_len; ++idx)
6992 {
6993 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
6994
6995 delete_def_function_contents(dfunc);
6996 }
6997
6998 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999}
7000#endif
7001
7002
7003#endif // FEAT_EVAL