blob: c45f620cafe09df98afa7b85a16a2bb351a933a2 [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
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200106// values for ctx_skip
107typedef enum {
108 SKIP_NOT, // condition is a constant, produce code
109 SKIP_YES, // condition is a constant, do NOT produce code
110 SKIP_UNKNONW // condition is not a constant, produce code
111} skip_T;
112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113/*
114 * Context for compiling lines of Vim script.
115 * Stores info about the local variables and condition stack.
116 */
117struct cctx_S {
118 ufunc_T *ctx_ufunc; // current function
119 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200120 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 garray_T ctx_instr; // generated instructions
122
123 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200124 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100125
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200126 int ctx_closure_count; // number of closures created in the
127 // function
128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 garray_T ctx_imports; // imported items
130
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200131 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 scope_T *ctx_scope; // current scope, NULL at toplevel
133
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200134 cctx_T *ctx_outer; // outer scope for lambda or nested
135 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200136 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200137
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200139 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100140};
141
142static char e_var_notfound[] = N_("E1001: variable not found: %s");
143static char e_syntax_at[] = N_("E1002: Syntax error at %s");
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +0200144static char e_used_as_arg[] = N_("E1006: %s is used as an argument");
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200145static char e_cannot_use_void[] = N_("E1031: Cannot use void value");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146
Bram Moolenaar20431c92020-03-20 18:39:46 +0100147static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200148static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149
150/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200151 * Lookup variable "name" in the local scope and return it.
152 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200154 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155lookup_local(char_u *name, size_t len, cctx_T *cctx)
156{
157 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200158 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100160 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200161 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200162
163 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
165 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200166 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167 if (STRNCMP(name, lvar->lv_name, len) == 0
168 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200169 {
170 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200171 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174
175 // Find local in outer function scope.
176 if (cctx->ctx_outer != NULL)
177 {
178 lvar = lookup_local(name, len, cctx->ctx_outer);
179 if (lvar != NULL)
180 {
181 // TODO: are there situations we should not mark the outer scope as
182 // used?
183 cctx->ctx_outer_used = TRUE;
184 lvar->lv_from_outer = TRUE;
185 return lvar;
186 }
187 }
188
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200189 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190}
191
192/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200193 * Lookup an argument in the current function and an enclosing function.
194 * Returns the argument index in "idxp"
195 * Returns the argument type in "type"
196 * Sets "gen_load_outer" to TRUE if found in outer scope.
197 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100198 */
199 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200200lookup_arg(
201 char_u *name,
202 size_t len,
203 int *idxp,
204 type_T **type,
205 int *gen_load_outer,
206 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207{
208 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200209 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100210
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100211 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200212 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
214 {
215 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
216
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200217 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
218 {
219 if (idxp != NULL)
220 {
221 // Arguments are located above the frame pointer. One further
222 // if there is a vararg argument
223 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
224 + STACK_FRAME_SIZE)
225 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
226
227 if (cctx->ctx_ufunc->uf_arg_types != NULL)
228 *type = cctx->ctx_ufunc->uf_arg_types[idx];
229 else
230 *type = &t_any;
231 }
232 return OK;
233 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200236 va_name = cctx->ctx_ufunc->uf_va_name;
237 if (va_name != NULL
238 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
239 {
240 if (idxp != NULL)
241 {
242 // varargs is always the last argument
243 *idxp = -STACK_FRAME_SIZE - 1;
244 *type = cctx->ctx_ufunc->uf_va_type;
245 }
246 return OK;
247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200249 if (cctx->ctx_outer != NULL)
250 {
251 // Lookup the name for an argument of the outer function.
252 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
253 == OK)
254 {
255 *gen_load_outer = TRUE;
256 return OK;
257 }
258 }
259
260 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100261}
262
263/*
264 * Lookup a variable in the current script.
265 * Returns OK or FAIL.
266 */
267 static int
268lookup_script(char_u *name, size_t len)
269{
270 int cc;
271 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
272 dictitem_T *di;
273
274 cc = name[len];
275 name[len] = NUL;
276 di = find_var_in_ht(ht, 0, name, TRUE);
277 name[len] = cc;
278 return di == NULL ? FAIL: OK;
279}
280
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100281/*
282 * Check if "p[len]" is already defined, either in script "import_sid" or in
283 * compilation context "cctx".
284 * Return FAIL and give an error if it defined.
285 */
286 int
287check_defined(char_u *p, int len, cctx_T *cctx)
288{
289 if (lookup_script(p, len) == OK
290 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200291 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100292 || find_imported(p, len, cctx) != NULL)))
293 {
294 semsg("E1073: imported name already defined: %s", p);
295 return FAIL;
296 }
297 return OK;
298}
299
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200300/*
301 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
302 * be freed later.
303 */
304 static type_T *
305alloc_type(garray_T *type_gap)
306{
307 type_T *type;
308
309 if (ga_grow(type_gap, 1) == FAIL)
310 return NULL;
311 type = ALLOC_CLEAR_ONE(type_T);
312 if (type != NULL)
313 {
314 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
315 ++type_gap->ga_len;
316 }
317 return type;
318}
319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100320 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200321get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100322{
323 type_T *type;
324
325 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200326 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100327 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200328 if (member_type->tt_type == VAR_VOID
329 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100330 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100331 if (member_type->tt_type == VAR_BOOL)
332 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100333 if (member_type->tt_type == VAR_NUMBER)
334 return &t_list_number;
335 if (member_type->tt_type == VAR_STRING)
336 return &t_list_string;
337
338 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200339 type = alloc_type(type_gap);
340 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100341 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100342 type->tt_type = VAR_LIST;
343 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200344 type->tt_argcount = 0;
345 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 return type;
347}
348
349 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200350get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100351{
352 type_T *type;
353
354 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200355 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100356 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200357 if (member_type->tt_type == VAR_VOID
358 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100359 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100360 if (member_type->tt_type == VAR_BOOL)
361 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100362 if (member_type->tt_type == VAR_NUMBER)
363 return &t_dict_number;
364 if (member_type->tt_type == VAR_STRING)
365 return &t_dict_string;
366
367 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200368 type = alloc_type(type_gap);
369 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100370 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100371 type->tt_type = VAR_DICT;
372 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200373 type->tt_argcount = 0;
374 type->tt_args = NULL;
375 return type;
376}
377
378/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200379 * Allocate a new type for a function.
380 */
381 static type_T *
382alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
383{
384 type_T *type = alloc_type(type_gap);
385
386 if (type == NULL)
387 return &t_any;
388 type->tt_type = VAR_FUNC;
389 type->tt_member = ret_type;
390 type->tt_argcount = argcount;
391 type->tt_args = NULL;
392 return type;
393}
394
395/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200396 * Get a function type, based on the return type "ret_type".
397 * If "argcount" is -1 or 0 a predefined type can be used.
398 * If "argcount" > 0 always create a new type, so that arguments can be added.
399 */
400 static type_T *
401get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
402{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200403 // recognize commonly used types
404 if (argcount <= 0)
405 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200406 if (ret_type == &t_unknown)
407 {
408 // (argcount == 0) is not possible
409 return &t_func_unknown;
410 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200411 if (ret_type == &t_void)
412 {
413 if (argcount == 0)
414 return &t_func_0_void;
415 else
416 return &t_func_void;
417 }
418 if (ret_type == &t_any)
419 {
420 if (argcount == 0)
421 return &t_func_0_any;
422 else
423 return &t_func_any;
424 }
425 if (ret_type == &t_number)
426 {
427 if (argcount == 0)
428 return &t_func_0_number;
429 else
430 return &t_func_number;
431 }
432 if (ret_type == &t_string)
433 {
434 if (argcount == 0)
435 return &t_func_0_string;
436 else
437 return &t_func_string;
438 }
439 }
440
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200441 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100442}
443
Bram Moolenaara8c17702020-04-01 21:17:24 +0200444/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200445 * For a function type, reserve space for "argcount" argument types (including
446 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200447 */
448 static int
449func_type_add_arg_types(
450 type_T *functype,
451 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200452 garray_T *type_gap)
453{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200454 // To make it easy to free the space needed for the argument types, add the
455 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200456 if (ga_grow(type_gap, 1) == FAIL)
457 return FAIL;
458 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
459 if (functype->tt_args == NULL)
460 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200461 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
462 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200463 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200464 return OK;
465}
466
467/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200468 * Return the type_T for a typval. Only for primitive types.
469 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200470 type_T *
Bram Moolenaara8c17702020-04-01 21:17:24 +0200471typval2type(typval_T *tv)
472{
473 if (tv->v_type == VAR_NUMBER)
474 return &t_number;
475 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200476 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200477 if (tv->v_type == VAR_STRING)
478 return &t_string;
479 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
480 return &t_list_string;
481 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
482 return &t_dict_any;
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200483 return &t_any; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200484}
485
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200486 static void
487type_mismatch(type_T *expected, type_T *actual)
488{
489 char *tofree1, *tofree2;
490
491 semsg(_("E1013: type mismatch, expected %s but got %s"),
492 type_name(expected, &tofree1), type_name(actual, &tofree2));
493 vim_free(tofree1);
494 vim_free(tofree2);
495}
496
497 static void
498arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
499{
500 char *tofree1, *tofree2;
501
502 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
503 argidx,
504 type_name(expected, &tofree1), type_name(actual, &tofree2));
505 vim_free(tofree1);
506 vim_free(tofree2);
507}
508
509/*
510 * Check if the expected and actual types match.
511 * Does not allow for assigning "any" to a specific type.
512 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200513 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200514check_type(type_T *expected, type_T *actual, int give_msg)
515{
516 int ret = OK;
517
518 // When expected is "unknown" we accept any actual type.
519 // When expected is "any" we accept any actual type except "void".
520 if (expected->tt_type != VAR_UNKNOWN
521 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
522
523 {
524 if (expected->tt_type != actual->tt_type)
525 {
526 if (give_msg)
527 type_mismatch(expected, actual);
528 return FAIL;
529 }
530 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
531 {
532 // "unknown" is used for an empty list or dict
533 if (actual->tt_member != &t_unknown)
534 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
535 }
536 else if (expected->tt_type == VAR_FUNC)
537 {
538 if (expected->tt_member != &t_unknown)
539 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
540 if (ret == OK && expected->tt_argcount != -1
541 && (actual->tt_argcount < expected->tt_min_argcount
542 || actual->tt_argcount > expected->tt_argcount))
543 ret = FAIL;
544 }
545 if (ret == FAIL && give_msg)
546 type_mismatch(expected, actual);
547 }
548 return ret;
549}
550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551/////////////////////////////////////////////////////////////////////
552// Following generate_ functions expect the caller to call ga_grow().
553
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200554#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
555#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100557/*
558 * Generate an instruction without arguments.
559 * Returns a pointer to the new instruction, NULL if failed.
560 */
561 static isn_T *
562generate_instr(cctx_T *cctx, isntype_T isn_type)
563{
564 garray_T *instr = &cctx->ctx_instr;
565 isn_T *isn;
566
Bram Moolenaar080457c2020-03-03 21:53:32 +0100567 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100568 if (ga_grow(instr, 1) == FAIL)
569 return NULL;
570 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
571 isn->isn_type = isn_type;
572 isn->isn_lnum = cctx->ctx_lnum + 1;
573 ++instr->ga_len;
574
575 return isn;
576}
577
578/*
579 * Generate an instruction without arguments.
580 * "drop" will be removed from the stack.
581 * Returns a pointer to the new instruction, NULL if failed.
582 */
583 static isn_T *
584generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
585{
586 garray_T *stack = &cctx->ctx_type_stack;
587
Bram Moolenaar080457c2020-03-03 21:53:32 +0100588 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 stack->ga_len -= drop;
590 return generate_instr(cctx, isn_type);
591}
592
593/*
594 * Generate instruction "isn_type" and put "type" on the type stack.
595 */
596 static isn_T *
597generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
598{
599 isn_T *isn;
600 garray_T *stack = &cctx->ctx_type_stack;
601
602 if ((isn = generate_instr(cctx, isn_type)) == NULL)
603 return NULL;
604
605 if (ga_grow(stack, 1) == FAIL)
606 return NULL;
607 ((type_T **)stack->ga_data)[stack->ga_len] = type;
608 ++stack->ga_len;
609
610 return isn;
611}
612
613/*
614 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
615 */
616 static int
617may_generate_2STRING(int offset, cctx_T *cctx)
618{
619 isn_T *isn;
620 garray_T *stack = &cctx->ctx_type_stack;
621 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
622
623 if ((*type)->tt_type == VAR_STRING)
624 return OK;
625 *type = &t_string;
626
627 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
628 return FAIL;
629 isn->isn_arg.number = offset;
630
631 return OK;
632}
633
634 static int
635check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
636{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200637 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200639 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100640 {
641 if (*op == '+')
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100642 emsg(_("E1035: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 else
644 semsg(_("E1036: %c requires number or float arguments"), *op);
645 return FAIL;
646 }
647 return OK;
648}
649
650/*
651 * Generate an instruction with two arguments. The instruction depends on the
652 * type of the arguments.
653 */
654 static int
655generate_two_op(cctx_T *cctx, char_u *op)
656{
657 garray_T *stack = &cctx->ctx_type_stack;
658 type_T *type1;
659 type_T *type2;
660 vartype_T vartype;
661 isn_T *isn;
662
Bram Moolenaar080457c2020-03-03 21:53:32 +0100663 RETURN_OK_IF_SKIP(cctx);
664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665 // Get the known type of the two items on the stack. If they are matching
666 // use a type-specific instruction. Otherwise fall back to runtime type
667 // checking.
668 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
669 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200670 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671 if (type1->tt_type == type2->tt_type
672 && (type1->tt_type == VAR_NUMBER
673 || type1->tt_type == VAR_LIST
674#ifdef FEAT_FLOAT
675 || type1->tt_type == VAR_FLOAT
676#endif
677 || type1->tt_type == VAR_BLOB))
678 vartype = type1->tt_type;
679
680 switch (*op)
681 {
682 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200683 && type1->tt_type != VAR_ANY
684 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685 && check_number_or_float(
686 type1->tt_type, type2->tt_type, op) == FAIL)
687 return FAIL;
688 isn = generate_instr_drop(cctx,
689 vartype == VAR_NUMBER ? ISN_OPNR
690 : vartype == VAR_LIST ? ISN_ADDLIST
691 : vartype == VAR_BLOB ? ISN_ADDBLOB
692#ifdef FEAT_FLOAT
693 : vartype == VAR_FLOAT ? ISN_OPFLOAT
694#endif
695 : ISN_OPANY, 1);
696 if (isn != NULL)
697 isn->isn_arg.op.op_type = EXPR_ADD;
698 break;
699
700 case '-':
701 case '*':
702 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
703 op) == FAIL)
704 return FAIL;
705 if (vartype == VAR_NUMBER)
706 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
707#ifdef FEAT_FLOAT
708 else if (vartype == VAR_FLOAT)
709 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
710#endif
711 else
712 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
713 if (isn != NULL)
714 isn->isn_arg.op.op_type = *op == '*'
715 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
716 break;
717
Bram Moolenaar4c683752020-04-05 21:38:23 +0200718 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200720 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100721 && type2->tt_type != VAR_NUMBER))
722 {
723 emsg(_("E1035: % requires number arguments"));
724 return FAIL;
725 }
726 isn = generate_instr_drop(cctx,
727 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
728 if (isn != NULL)
729 isn->isn_arg.op.op_type = EXPR_REM;
730 break;
731 }
732
733 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200734 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100735 {
736 type_T *type = &t_any;
737
738#ifdef FEAT_FLOAT
739 // float+number and number+float results in float
740 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
741 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
742 type = &t_float;
743#endif
744 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
745 }
746
747 return OK;
748}
749
750/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200751 * Get the instruction to use for comparing "type1" with "type2"
752 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200754 static isntype_T
755get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756{
757 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758
Bram Moolenaar4c683752020-04-05 21:38:23 +0200759 if (type1 == VAR_UNKNOWN)
760 type1 = VAR_ANY;
761 if (type2 == VAR_UNKNOWN)
762 type2 = VAR_ANY;
763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 if (type1 == type2)
765 {
766 switch (type1)
767 {
768 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
769 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
770 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
771 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
772 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
773 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
774 case VAR_LIST: isntype = ISN_COMPARELIST; break;
775 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
776 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 default: isntype = ISN_COMPAREANY; break;
778 }
779 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200780 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
782 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
783 isntype = ISN_COMPAREANY;
784
785 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
786 && (isntype == ISN_COMPAREBOOL
787 || isntype == ISN_COMPARESPECIAL
788 || isntype == ISN_COMPARENR
789 || isntype == ISN_COMPAREFLOAT))
790 {
791 semsg(_("E1037: Cannot use \"%s\" with %s"),
792 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200793 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100794 }
795 if (isntype == ISN_DROP
796 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
797 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
798 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
799 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
800 && exptype != EXPR_IS && exptype != EXPR_ISNOT
801 && (type1 == VAR_BLOB || type2 == VAR_BLOB
802 || type1 == VAR_LIST || type2 == VAR_LIST))))
803 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100804 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200806 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100807 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200808 return isntype;
809}
810
811/*
812 * Generate an ISN_COMPARE* instruction with a boolean result.
813 */
814 static int
815generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
816{
817 isntype_T isntype;
818 isn_T *isn;
819 garray_T *stack = &cctx->ctx_type_stack;
820 vartype_T type1;
821 vartype_T type2;
822
823 RETURN_OK_IF_SKIP(cctx);
824
825 // Get the known type of the two items on the stack. If they are matching
826 // use a type-specific instruction. Otherwise fall back to runtime type
827 // checking.
828 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
829 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
830 isntype = get_compare_isn(exptype, type1, type2);
831 if (isntype == ISN_DROP)
832 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833
834 if ((isn = generate_instr(cctx, isntype)) == NULL)
835 return FAIL;
836 isn->isn_arg.op.op_type = exptype;
837 isn->isn_arg.op.op_ic = ic;
838
839 // takes two arguments, puts one bool back
840 if (stack->ga_len >= 2)
841 {
842 --stack->ga_len;
843 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
844 }
845
846 return OK;
847}
848
849/*
850 * Generate an ISN_2BOOL instruction.
851 */
852 static int
853generate_2BOOL(cctx_T *cctx, int invert)
854{
855 isn_T *isn;
856 garray_T *stack = &cctx->ctx_type_stack;
857
Bram Moolenaar080457c2020-03-03 21:53:32 +0100858 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
860 return FAIL;
861 isn->isn_arg.number = invert;
862
863 // type becomes bool
864 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
865
866 return OK;
867}
868
869 static int
870generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
871{
872 isn_T *isn;
873 garray_T *stack = &cctx->ctx_type_stack;
874
Bram Moolenaar080457c2020-03-03 21:53:32 +0100875 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
877 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200878 // TODO: whole type, e.g. for a function also arg and return types
879 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880 isn->isn_arg.type.ct_off = offset;
881
882 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200883 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884
885 return OK;
886}
887
888/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200889 * Check that
890 * - "actual" is "expected" type or
891 * - "actual" is a type that can be "expected" type: add a runtime check; or
892 * - return FAIL.
893 */
894 static int
895need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
896{
897 if (check_type(expected, actual, FALSE) == OK)
898 return OK;
899 if (actual->tt_type != VAR_ANY
900 && actual->tt_type != VAR_UNKNOWN
901 && !(actual->tt_type == VAR_FUNC
902 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
903 {
904 type_mismatch(expected, actual);
905 return FAIL;
906 }
907 generate_TYPECHECK(cctx, expected, offset);
908 return OK;
909}
910
911/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912 * Generate an ISN_PUSHNR instruction.
913 */
914 static int
915generate_PUSHNR(cctx_T *cctx, varnumber_T number)
916{
917 isn_T *isn;
918
Bram Moolenaar080457c2020-03-03 21:53:32 +0100919 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
921 return FAIL;
922 isn->isn_arg.number = number;
923
924 return OK;
925}
926
927/*
928 * Generate an ISN_PUSHBOOL instruction.
929 */
930 static int
931generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
932{
933 isn_T *isn;
934
Bram Moolenaar080457c2020-03-03 21:53:32 +0100935 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
937 return FAIL;
938 isn->isn_arg.number = number;
939
940 return OK;
941}
942
943/*
944 * Generate an ISN_PUSHSPEC instruction.
945 */
946 static int
947generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
948{
949 isn_T *isn;
950
Bram Moolenaar080457c2020-03-03 21:53:32 +0100951 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100952 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
953 return FAIL;
954 isn->isn_arg.number = number;
955
956 return OK;
957}
958
959#ifdef FEAT_FLOAT
960/*
961 * Generate an ISN_PUSHF instruction.
962 */
963 static int
964generate_PUSHF(cctx_T *cctx, float_T fnumber)
965{
966 isn_T *isn;
967
Bram Moolenaar080457c2020-03-03 21:53:32 +0100968 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
970 return FAIL;
971 isn->isn_arg.fnumber = fnumber;
972
973 return OK;
974}
975#endif
976
977/*
978 * Generate an ISN_PUSHS instruction.
979 * Consumes "str".
980 */
981 static int
982generate_PUSHS(cctx_T *cctx, char_u *str)
983{
984 isn_T *isn;
985
Bram Moolenaar080457c2020-03-03 21:53:32 +0100986 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
988 return FAIL;
989 isn->isn_arg.string = str;
990
991 return OK;
992}
993
994/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100995 * Generate an ISN_PUSHCHANNEL instruction.
996 * Consumes "channel".
997 */
998 static int
999generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1000{
1001 isn_T *isn;
1002
Bram Moolenaar080457c2020-03-03 21:53:32 +01001003 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001004 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1005 return FAIL;
1006 isn->isn_arg.channel = channel;
1007
1008 return OK;
1009}
1010
1011/*
1012 * Generate an ISN_PUSHJOB instruction.
1013 * Consumes "job".
1014 */
1015 static int
1016generate_PUSHJOB(cctx_T *cctx, job_T *job)
1017{
1018 isn_T *isn;
1019
Bram Moolenaar080457c2020-03-03 21:53:32 +01001020 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001021 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001022 return FAIL;
1023 isn->isn_arg.job = job;
1024
1025 return OK;
1026}
1027
1028/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 * Generate an ISN_PUSHBLOB instruction.
1030 * Consumes "blob".
1031 */
1032 static int
1033generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1034{
1035 isn_T *isn;
1036
Bram Moolenaar080457c2020-03-03 21:53:32 +01001037 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1039 return FAIL;
1040 isn->isn_arg.blob = blob;
1041
1042 return OK;
1043}
1044
1045/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001046 * Generate an ISN_PUSHFUNC instruction with name "name".
1047 * Consumes "name".
1048 */
1049 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001050generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001051{
1052 isn_T *isn;
1053
Bram Moolenaar080457c2020-03-03 21:53:32 +01001054 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001055 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001056 return FAIL;
1057 isn->isn_arg.string = name;
1058
1059 return OK;
1060}
1061
1062/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001063 * Generate an ISN_GETITEM instruction with "index".
1064 */
1065 static int
1066generate_GETITEM(cctx_T *cctx, int index)
1067{
1068 isn_T *isn;
1069 garray_T *stack = &cctx->ctx_type_stack;
1070 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1071 type_T *item_type = &t_any;
1072
1073 RETURN_OK_IF_SKIP(cctx);
1074
1075 if (type->tt_type == VAR_LIST)
1076 item_type = type->tt_member;
1077 else if (type->tt_type != VAR_ANY)
1078 {
1079 emsg(_(e_listreq));
1080 return FAIL;
1081 }
1082 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1083 return FAIL;
1084 isn->isn_arg.number = index;
1085
1086 // add the item type to the type stack
1087 if (ga_grow(stack, 1) == FAIL)
1088 return FAIL;
1089 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1090 ++stack->ga_len;
1091 return OK;
1092}
1093
1094/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001095 * Generate an ISN_SLICE instruction with "count".
1096 */
1097 static int
1098generate_SLICE(cctx_T *cctx, int count)
1099{
1100 isn_T *isn;
1101
1102 RETURN_OK_IF_SKIP(cctx);
1103 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1104 return FAIL;
1105 isn->isn_arg.number = count;
1106 return OK;
1107}
1108
1109/*
1110 * Generate an ISN_CHECKLEN instruction with "min_len".
1111 */
1112 static int
1113generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1114{
1115 isn_T *isn;
1116
1117 RETURN_OK_IF_SKIP(cctx);
1118
1119 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1120 return FAIL;
1121 isn->isn_arg.checklen.cl_min_len = min_len;
1122 isn->isn_arg.checklen.cl_more_OK = more_OK;
1123
1124 return OK;
1125}
1126
1127/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 * Generate an ISN_STORE instruction.
1129 */
1130 static int
1131generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1132{
1133 isn_T *isn;
1134
Bram Moolenaar080457c2020-03-03 21:53:32 +01001135 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001136 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1137 return FAIL;
1138 if (name != NULL)
1139 isn->isn_arg.string = vim_strsave(name);
1140 else
1141 isn->isn_arg.number = idx;
1142
1143 return OK;
1144}
1145
1146/*
1147 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1148 */
1149 static int
1150generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
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(cctx, ISN_STORENR)) == NULL)
1156 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001157 isn->isn_arg.storenr.stnr_idx = idx;
1158 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001159
1160 return OK;
1161}
1162
1163/*
1164 * Generate an ISN_STOREOPT instruction
1165 */
1166 static int
1167generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1168{
1169 isn_T *isn;
1170
Bram Moolenaar080457c2020-03-03 21:53:32 +01001171 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1173 return FAIL;
1174 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1175 isn->isn_arg.storeopt.so_flags = opt_flags;
1176
1177 return OK;
1178}
1179
1180/*
1181 * Generate an ISN_LOAD or similar instruction.
1182 */
1183 static int
1184generate_LOAD(
1185 cctx_T *cctx,
1186 isntype_T isn_type,
1187 int idx,
1188 char_u *name,
1189 type_T *type)
1190{
1191 isn_T *isn;
1192
Bram Moolenaar080457c2020-03-03 21:53:32 +01001193 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1195 return FAIL;
1196 if (name != NULL)
1197 isn->isn_arg.string = vim_strsave(name);
1198 else
1199 isn->isn_arg.number = idx;
1200
1201 return OK;
1202}
1203
1204/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001205 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001206 */
1207 static int
1208generate_LOADV(
1209 cctx_T *cctx,
1210 char_u *name,
1211 int error)
1212{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001213 int di_flags;
1214 int vidx = find_vim_var(name, &di_flags);
1215 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001216
Bram Moolenaar080457c2020-03-03 21:53:32 +01001217 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001218 if (vidx < 0)
1219 {
1220 if (error)
1221 semsg(_(e_var_notfound), name);
1222 return FAIL;
1223 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001224 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001225
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001226 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001227}
1228
1229/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001230 * Generate an ISN_UNLET instruction.
1231 */
1232 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001233generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001234{
1235 isn_T *isn;
1236
1237 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001238 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001239 return FAIL;
1240 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1241 isn->isn_arg.unlet.ul_forceit = forceit;
1242
1243 return OK;
1244}
1245
1246/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 * Generate an ISN_LOADS instruction.
1248 */
1249 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001250generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001252 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001253 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001254 int sid,
1255 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001256{
1257 isn_T *isn;
1258
Bram Moolenaar080457c2020-03-03 21:53:32 +01001259 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001260 if (isn_type == ISN_LOADS)
1261 isn = generate_instr_type(cctx, isn_type, type);
1262 else
1263 isn = generate_instr_drop(cctx, isn_type, 1);
1264 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001266 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1267 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268
1269 return OK;
1270}
1271
1272/*
1273 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1274 */
1275 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001276generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 cctx_T *cctx,
1278 isntype_T isn_type,
1279 int sid,
1280 int idx,
1281 type_T *type)
1282{
1283 isn_T *isn;
1284
Bram Moolenaar080457c2020-03-03 21:53:32 +01001285 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001286 if (isn_type == ISN_LOADSCRIPT)
1287 isn = generate_instr_type(cctx, isn_type, type);
1288 else
1289 isn = generate_instr_drop(cctx, isn_type, 1);
1290 if (isn == NULL)
1291 return FAIL;
1292 isn->isn_arg.script.script_sid = sid;
1293 isn->isn_arg.script.script_idx = idx;
1294 return OK;
1295}
1296
1297/*
1298 * Generate an ISN_NEWLIST instruction.
1299 */
1300 static int
1301generate_NEWLIST(cctx_T *cctx, int count)
1302{
1303 isn_T *isn;
1304 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 type_T *type;
1306 type_T *member;
1307
Bram Moolenaar080457c2020-03-03 21:53:32 +01001308 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1310 return FAIL;
1311 isn->isn_arg.number = count;
1312
1313 // drop the value types
1314 stack->ga_len -= count;
1315
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001316 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001317 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 if (count > 0)
1319 member = ((type_T **)stack->ga_data)[stack->ga_len];
1320 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001321 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001322 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323
1324 // add the list type to the type stack
1325 if (ga_grow(stack, 1) == FAIL)
1326 return FAIL;
1327 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1328 ++stack->ga_len;
1329
1330 return OK;
1331}
1332
1333/*
1334 * Generate an ISN_NEWDICT instruction.
1335 */
1336 static int
1337generate_NEWDICT(cctx_T *cctx, int count)
1338{
1339 isn_T *isn;
1340 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 type_T *type;
1342 type_T *member;
1343
Bram Moolenaar080457c2020-03-03 21:53:32 +01001344 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1346 return FAIL;
1347 isn->isn_arg.number = count;
1348
1349 // drop the key and value types
1350 stack->ga_len -= 2 * count;
1351
Bram Moolenaar436472f2020-02-20 22:54:43 +01001352 // Use the first value type for the list member type. Use "void" for an
1353 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 if (count > 0)
1355 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1356 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001357 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001358 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359
1360 // add the dict type to the type stack
1361 if (ga_grow(stack, 1) == FAIL)
1362 return FAIL;
1363 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1364 ++stack->ga_len;
1365
1366 return OK;
1367}
1368
1369/*
1370 * Generate an ISN_FUNCREF instruction.
1371 */
1372 static int
1373generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1374{
1375 isn_T *isn;
1376 garray_T *stack = &cctx->ctx_type_stack;
1377
Bram Moolenaar080457c2020-03-03 21:53:32 +01001378 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1380 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001381 isn->isn_arg.funcref.fr_func = dfunc_idx;
1382 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383
1384 if (ga_grow(stack, 1) == FAIL)
1385 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001386 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 // TODO: argument and return types
1388 ++stack->ga_len;
1389
1390 return OK;
1391}
1392
1393/*
1394 * Generate an ISN_JUMP instruction.
1395 */
1396 static int
1397generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1398{
1399 isn_T *isn;
1400 garray_T *stack = &cctx->ctx_type_stack;
1401
Bram Moolenaar080457c2020-03-03 21:53:32 +01001402 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1404 return FAIL;
1405 isn->isn_arg.jump.jump_when = when;
1406 isn->isn_arg.jump.jump_where = where;
1407
1408 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1409 --stack->ga_len;
1410
1411 return OK;
1412}
1413
1414 static int
1415generate_FOR(cctx_T *cctx, int loop_idx)
1416{
1417 isn_T *isn;
1418 garray_T *stack = &cctx->ctx_type_stack;
1419
Bram Moolenaar080457c2020-03-03 21:53:32 +01001420 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1422 return FAIL;
1423 isn->isn_arg.forloop.for_idx = loop_idx;
1424
1425 if (ga_grow(stack, 1) == FAIL)
1426 return FAIL;
1427 // type doesn't matter, will be stored next
1428 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1429 ++stack->ga_len;
1430
1431 return OK;
1432}
1433
1434/*
1435 * Generate an ISN_BCALL instruction.
1436 * Return FAIL if the number of arguments is wrong.
1437 */
1438 static int
1439generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1440{
1441 isn_T *isn;
1442 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001443 type_T *argtypes[MAX_FUNC_ARGS];
1444 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445
Bram Moolenaar080457c2020-03-03 21:53:32 +01001446 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 if (check_internal_func(func_idx, argcount) == FAIL)
1448 return FAIL;
1449
1450 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1451 return FAIL;
1452 isn->isn_arg.bfunc.cbf_idx = func_idx;
1453 isn->isn_arg.bfunc.cbf_argcount = argcount;
1454
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001455 for (i = 0; i < argcount; ++i)
1456 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 stack->ga_len -= argcount; // drop the arguments
1459 if (ga_grow(stack, 1) == FAIL)
1460 return FAIL;
1461 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001462 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 ++stack->ga_len; // add return value
1464
1465 return OK;
1466}
1467
1468/*
1469 * Generate an ISN_DCALL or ISN_UCALL instruction.
1470 * Return FAIL if the number of arguments is wrong.
1471 */
1472 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001473generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474{
1475 isn_T *isn;
1476 garray_T *stack = &cctx->ctx_type_stack;
1477 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001478 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479
Bram Moolenaar080457c2020-03-03 21:53:32 +01001480 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481 if (argcount > regular_args && !has_varargs(ufunc))
1482 {
1483 semsg(_(e_toomanyarg), ufunc->uf_name);
1484 return FAIL;
1485 }
1486 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1487 {
1488 semsg(_(e_toofewarg), ufunc->uf_name);
1489 return FAIL;
1490 }
1491
Bram Moolenaar822ba242020-05-24 23:00:18 +02001492 if (ufunc->uf_dfunc_idx != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001493 {
1494 int i;
1495
1496 for (i = 0; i < argcount; ++i)
1497 {
1498 type_T *expected;
1499 type_T *actual;
1500
1501 if (i < regular_args)
1502 {
1503 if (ufunc->uf_arg_types == NULL)
1504 continue;
1505 expected = ufunc->uf_arg_types[i];
1506 }
1507 else
1508 expected = ufunc->uf_va_type->tt_member;
1509 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001510 if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001511 {
1512 arg_type_mismatch(expected, actual, i + 1);
1513 return FAIL;
1514 }
1515 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001516 if (ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001517 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001518 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001519 }
1520
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 if ((isn = generate_instr(cctx,
Bram Moolenaar822ba242020-05-24 23:00:18 +02001522 ufunc->uf_dfunc_idx != UF_NOT_COMPILED ? ISN_DCALL
1523 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001524 return FAIL;
Bram Moolenaar822ba242020-05-24 23:00:18 +02001525 if (ufunc->uf_dfunc_idx != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526 {
1527 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1528 isn->isn_arg.dfunc.cdf_argcount = argcount;
1529 }
1530 else
1531 {
1532 // A user function may be deleted and redefined later, can't use the
1533 // ufunc pointer, need to look it up again at runtime.
1534 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1535 isn->isn_arg.ufunc.cuf_argcount = argcount;
1536 }
1537
1538 stack->ga_len -= argcount; // drop the arguments
1539 if (ga_grow(stack, 1) == FAIL)
1540 return FAIL;
1541 // add return value
1542 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1543 ++stack->ga_len;
1544
1545 return OK;
1546}
1547
1548/*
1549 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1550 */
1551 static int
1552generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1553{
1554 isn_T *isn;
1555 garray_T *stack = &cctx->ctx_type_stack;
1556
Bram Moolenaar080457c2020-03-03 21:53:32 +01001557 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1559 return FAIL;
1560 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1561 isn->isn_arg.ufunc.cuf_argcount = argcount;
1562
1563 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001564 if (ga_grow(stack, 1) == FAIL)
1565 return FAIL;
1566 // add return value
1567 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1568 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569
1570 return OK;
1571}
1572
1573/*
1574 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001575 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 */
1577 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001578generate_PCALL(
1579 cctx_T *cctx,
1580 int argcount,
1581 char_u *name,
1582 type_T *type,
1583 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584{
1585 isn_T *isn;
1586 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001587 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588
Bram Moolenaar080457c2020-03-03 21:53:32 +01001589 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001590
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001591 if (type->tt_type == VAR_ANY)
1592 ret_type = &t_any;
1593 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001594 {
1595 if (type->tt_argcount != -1)
1596 {
1597 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1598
1599 if (argcount < type->tt_min_argcount - varargs)
1600 {
1601 semsg(_(e_toofewarg), "[reference]");
1602 return FAIL;
1603 }
1604 if (!varargs && argcount > type->tt_argcount)
1605 {
1606 semsg(_(e_toomanyarg), "[reference]");
1607 return FAIL;
1608 }
1609 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001610 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001611 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001612 else
1613 {
1614 semsg(_("E1085: Not a callable type: %s"), name);
1615 return FAIL;
1616 }
1617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1619 return FAIL;
1620 isn->isn_arg.pfunc.cpf_top = at_top;
1621 isn->isn_arg.pfunc.cpf_argcount = argcount;
1622
1623 stack->ga_len -= argcount; // drop the arguments
1624
1625 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001626 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001628 // If partial is above the arguments it must be cleared and replaced with
1629 // the return value.
1630 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1631 return FAIL;
1632
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633 return OK;
1634}
1635
1636/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001637 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638 */
1639 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001640generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641{
1642 isn_T *isn;
1643 garray_T *stack = &cctx->ctx_type_stack;
1644 type_T *type;
1645
Bram Moolenaar080457c2020-03-03 21:53:32 +01001646 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001647 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001649 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001651 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001653 if (type->tt_type != VAR_DICT && type != &t_any)
1654 {
1655 emsg(_(e_dictreq));
1656 return FAIL;
1657 }
1658 // change dict type to dict member type
1659 if (type->tt_type == VAR_DICT)
1660 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661
1662 return OK;
1663}
1664
1665/*
1666 * Generate an ISN_ECHO instruction.
1667 */
1668 static int
1669generate_ECHO(cctx_T *cctx, int with_white, int count)
1670{
1671 isn_T *isn;
1672
Bram Moolenaar080457c2020-03-03 21:53:32 +01001673 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1675 return FAIL;
1676 isn->isn_arg.echo.echo_with_white = with_white;
1677 isn->isn_arg.echo.echo_count = count;
1678
1679 return OK;
1680}
1681
Bram Moolenaarad39c092020-02-26 18:23:43 +01001682/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001683 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001684 */
1685 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001686generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001687{
1688 isn_T *isn;
1689
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001690 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001691 return FAIL;
1692 isn->isn_arg.number = count;
1693
1694 return OK;
1695}
1696
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 static int
1698generate_EXEC(cctx_T *cctx, char_u *line)
1699{
1700 isn_T *isn;
1701
Bram Moolenaar080457c2020-03-03 21:53:32 +01001702 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1704 return FAIL;
1705 isn->isn_arg.string = vim_strsave(line);
1706 return OK;
1707}
1708
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001709 static int
1710generate_EXECCONCAT(cctx_T *cctx, int count)
1711{
1712 isn_T *isn;
1713
1714 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1715 return FAIL;
1716 isn->isn_arg.number = count;
1717 return OK;
1718}
1719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720/*
1721 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001722 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001724 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1726{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 lvar_T *lvar;
1728
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001729 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001731 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001732 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 }
1734
1735 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001736 return NULL;
1737 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001739 // Every local variable uses the next entry on the stack. We could re-use
1740 // the last ones when leaving a scope, but then variables used in a closure
1741 // might get overwritten. To keep things simple do not re-use stack
1742 // entries. This is less efficient, but memory is cheap these days.
1743 lvar->lv_idx = cctx->ctx_locals_count++;
1744
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001745 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001746 lvar->lv_const = isConst;
1747 lvar->lv_type = type;
1748
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001749 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750}
1751
1752/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001753 * Remove local variables above "new_top".
1754 */
1755 static void
1756unwind_locals(cctx_T *cctx, int new_top)
1757{
1758 if (cctx->ctx_locals.ga_len > new_top)
1759 {
1760 int idx;
1761 lvar_T *lvar;
1762
1763 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1764 {
1765 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1766 vim_free(lvar->lv_name);
1767 }
1768 }
1769 cctx->ctx_locals.ga_len = new_top;
1770}
1771
1772/*
1773 * Free all local variables.
1774 */
1775 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001776free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001777{
1778 unwind_locals(cctx, 0);
1779 ga_clear(&cctx->ctx_locals);
1780}
1781
1782/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 * Skip over a type definition and return a pointer to just after it.
1784 */
1785 char_u *
1786skip_type(char_u *start)
1787{
1788 char_u *p = start;
1789
1790 while (ASCII_ISALNUM(*p) || *p == '_')
1791 ++p;
1792
1793 // Skip over "<type>"; this is permissive about white space.
1794 if (*skipwhite(p) == '<')
1795 {
1796 p = skipwhite(p);
1797 p = skip_type(skipwhite(p + 1));
1798 p = skipwhite(p);
1799 if (*p == '>')
1800 ++p;
1801 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001802 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1803 {
1804 // handle func(args): type
1805 ++p;
1806 while (*p != ')' && *p != NUL)
1807 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001808 char_u *sp = p;
1809
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001810 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001811 if (p == sp)
1812 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001813 if (*p == ',')
1814 p = skipwhite(p + 1);
1815 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001816 if (*p == ')')
1817 {
1818 if (p[1] == ':')
1819 p = skip_type(skipwhite(p + 2));
1820 else
1821 p = skipwhite(p + 1);
1822 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001823 }
1824
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 return p;
1826}
1827
1828/*
1829 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001830 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 * Returns NULL in case of failure.
1832 */
1833 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001834parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835{
1836 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001837 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838
1839 if (**arg != '<')
1840 {
1841 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001842 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 else
1844 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001845 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 }
1847 *arg = skipwhite(*arg + 1);
1848
Bram Moolenaard77a8522020-04-03 21:59:57 +02001849 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850
1851 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001852 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001853 {
1854 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001855 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856 }
1857 ++*arg;
1858
1859 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001860 return get_list_type(member_type, type_gap);
1861 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862}
1863
1864/*
1865 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001866 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001867 */
1868 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001869parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870{
1871 char_u *p = *arg;
1872 size_t len;
1873
1874 // skip over the first word
1875 while (ASCII_ISALNUM(*p) || *p == '_')
1876 ++p;
1877 len = p - *arg;
1878
1879 switch (**arg)
1880 {
1881 case 'a':
1882 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1883 {
1884 *arg += len;
1885 return &t_any;
1886 }
1887 break;
1888 case 'b':
1889 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1890 {
1891 *arg += len;
1892 return &t_bool;
1893 }
1894 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1895 {
1896 *arg += len;
1897 return &t_blob;
1898 }
1899 break;
1900 case 'c':
1901 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1902 {
1903 *arg += len;
1904 return &t_channel;
1905 }
1906 break;
1907 case 'd':
1908 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1909 {
1910 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001911 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 }
1913 break;
1914 case 'f':
1915 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1916 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001917#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918 *arg += len;
1919 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001920#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001921 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001922 return &t_any;
1923#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 }
1925 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1926 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001927 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001928 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001929 int argcount = -1;
1930 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001931 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001932 type_T *arg_type[MAX_FUNC_ARGS + 1];
1933
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001934 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001936 if (**arg == '(')
1937 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001938 // "func" may or may not return a value, "func()" does
1939 // not return a value.
1940 ret_type = &t_void;
1941
Bram Moolenaard77a8522020-04-03 21:59:57 +02001942 p = ++*arg;
1943 argcount = 0;
1944 while (*p != NUL && *p != ')')
1945 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001946 if (*p == '?')
1947 {
1948 if (first_optional == -1)
1949 first_optional = argcount;
1950 ++p;
1951 }
1952 else if (first_optional != -1)
1953 {
1954 emsg(_("E1007: mandatory argument after optional argument"));
1955 return &t_any;
1956 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001957 else if (STRNCMP(p, "...", 3) == 0)
1958 {
1959 flags |= TTFLAG_VARARGS;
1960 p += 3;
1961 }
1962
1963 arg_type[argcount++] = parse_type(&p, type_gap);
1964
1965 // Nothing comes after "...{type}".
1966 if (flags & TTFLAG_VARARGS)
1967 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001968
Bram Moolenaard77a8522020-04-03 21:59:57 +02001969 if (*p != ',' && *skipwhite(p) == ',')
1970 {
1971 semsg(_(e_no_white_before), ",");
1972 return &t_any;
1973 }
1974 if (*p == ',')
1975 {
1976 ++p;
1977 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001978 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001979 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001980 return &t_any;
1981 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001982 }
1983 p = skipwhite(p);
1984 if (argcount == MAX_FUNC_ARGS)
1985 {
1986 emsg(_("E740: Too many argument types"));
1987 return &t_any;
1988 }
1989 }
1990
1991 p = skipwhite(p);
1992 if (*p != ')')
1993 {
1994 emsg(_(e_missing_close));
1995 return &t_any;
1996 }
1997 *arg = p + 1;
1998 }
1999 if (**arg == ':')
2000 {
2001 // parse return type
2002 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002003 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002004 semsg(_(e_white_after), ":");
2005 *arg = skipwhite(*arg);
2006 ret_type = parse_type(arg, type_gap);
2007 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002008 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002009 type = get_func_type(ret_type, argcount, type_gap);
2010 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002011 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002012 type = alloc_func_type(ret_type, argcount, type_gap);
2013 type->tt_flags = flags;
2014 if (argcount > 0)
2015 {
2016 type->tt_argcount = argcount;
2017 type->tt_min_argcount = first_optional == -1
2018 ? argcount : first_optional;
2019 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002020 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002021 return &t_any;
2022 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002023 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002024 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002025 }
2026 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 }
2028 break;
2029 case 'j':
2030 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2031 {
2032 *arg += len;
2033 return &t_job;
2034 }
2035 break;
2036 case 'l':
2037 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2038 {
2039 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002040 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002041 }
2042 break;
2043 case 'n':
2044 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2045 {
2046 *arg += len;
2047 return &t_number;
2048 }
2049 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 case 's':
2051 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2052 {
2053 *arg += len;
2054 return &t_string;
2055 }
2056 break;
2057 case 'v':
2058 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2059 {
2060 *arg += len;
2061 return &t_void;
2062 }
2063 break;
2064 }
2065
2066 semsg(_("E1010: Type not recognized: %s"), *arg);
2067 return &t_any;
2068}
2069
2070/*
2071 * Check if "type1" and "type2" are exactly the same.
2072 */
2073 static int
2074equal_type(type_T *type1, type_T *type2)
2075{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002076 int i;
2077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078 if (type1->tt_type != type2->tt_type)
2079 return FALSE;
2080 switch (type1->tt_type)
2081 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002082 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002083 case VAR_ANY:
2084 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085 case VAR_SPECIAL:
2086 case VAR_BOOL:
2087 case VAR_NUMBER:
2088 case VAR_FLOAT:
2089 case VAR_STRING:
2090 case VAR_BLOB:
2091 case VAR_JOB:
2092 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002093 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002094 case VAR_LIST:
2095 case VAR_DICT:
2096 return equal_type(type1->tt_member, type2->tt_member);
2097 case VAR_FUNC:
2098 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002099 if (!equal_type(type1->tt_member, type2->tt_member)
2100 || type1->tt_argcount != type2->tt_argcount)
2101 return FALSE;
2102 if (type1->tt_argcount < 0
2103 || type1->tt_args == NULL || type2->tt_args == NULL)
2104 return TRUE;
2105 for (i = 0; i < type1->tt_argcount; ++i)
2106 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2107 return FALSE;
2108 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 }
2110 return TRUE;
2111}
2112
2113/*
2114 * Find the common type of "type1" and "type2" and put it in "dest".
2115 * "type2" and "dest" may be the same.
2116 */
2117 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002118common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119{
2120 if (equal_type(type1, type2))
2121 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002122 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 return;
2124 }
2125
2126 if (type1->tt_type == type2->tt_type)
2127 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2129 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002130 type_T *common;
2131
Bram Moolenaard77a8522020-04-03 21:59:57 +02002132 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002133 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002134 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002135 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002136 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 return;
2138 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002139 if (type1->tt_type == VAR_FUNC)
2140 {
2141 type_T *common;
2142
2143 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2144 if (type1->tt_argcount == type2->tt_argcount
2145 && type1->tt_argcount >= 0)
2146 {
2147 int argcount = type1->tt_argcount;
2148 int i;
2149
2150 *dest = alloc_func_type(common, argcount, type_gap);
2151 if (type1->tt_args != NULL && type2->tt_args != NULL)
2152 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002153 if (func_type_add_arg_types(*dest, argcount,
2154 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002155 for (i = 0; i < argcount; ++i)
2156 common_type(type1->tt_args[i], type2->tt_args[i],
2157 &(*dest)->tt_args[i], type_gap);
2158 }
2159 }
2160 else
2161 *dest = alloc_func_type(common, -1, type_gap);
2162 return;
2163 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 }
2165
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002166 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002167}
2168
2169 char *
2170vartype_name(vartype_T type)
2171{
2172 switch (type)
2173 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002174 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002175 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 case VAR_SPECIAL: return "special";
2178 case VAR_BOOL: return "bool";
2179 case VAR_NUMBER: return "number";
2180 case VAR_FLOAT: return "float";
2181 case VAR_STRING: return "string";
2182 case VAR_BLOB: return "blob";
2183 case VAR_JOB: return "job";
2184 case VAR_CHANNEL: return "channel";
2185 case VAR_LIST: return "list";
2186 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002187
2188 case VAR_FUNC:
2189 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002191 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192}
2193
2194/*
2195 * Return the name of a type.
2196 * The result may be in allocated memory, in which case "tofree" is set.
2197 */
2198 char *
2199type_name(type_T *type, char **tofree)
2200{
2201 char *name = vartype_name(type->tt_type);
2202
2203 *tofree = NULL;
2204 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2205 {
2206 char *member_free;
2207 char *member_name = type_name(type->tt_member, &member_free);
2208 size_t len;
2209
2210 len = STRLEN(name) + STRLEN(member_name) + 3;
2211 *tofree = alloc(len);
2212 if (*tofree != NULL)
2213 {
2214 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2215 vim_free(member_free);
2216 return *tofree;
2217 }
2218 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002219 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002220 {
2221 garray_T ga;
2222 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002223 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002224
2225 ga_init2(&ga, 1, 100);
2226 if (ga_grow(&ga, 20) == FAIL)
2227 return "[unknown]";
2228 *tofree = ga.ga_data;
2229 STRCPY(ga.ga_data, "func(");
2230 ga.ga_len += 5;
2231
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002232 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002233 {
2234 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002235 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002236 int len;
2237
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002238 if (type->tt_args == NULL)
2239 arg_type = "[unknown]";
2240 else
2241 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002242 if (i > 0)
2243 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002244 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002245 ga.ga_len += 2;
2246 }
2247 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002248 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002249 {
2250 vim_free(arg_free);
2251 return "[unknown]";
2252 }
2253 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002254 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002255 {
2256 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2257 ga.ga_len += 3;
2258 }
2259 else if (i >= type->tt_min_argcount)
2260 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002261 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002262 ga.ga_len += len;
2263 vim_free(arg_free);
2264 }
2265
2266 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002267 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002268 else
2269 {
2270 char *ret_free;
2271 char *ret_name = type_name(type->tt_member, &ret_free);
2272 int len;
2273
2274 len = (int)STRLEN(ret_name) + 4;
2275 if (ga_grow(&ga, len) == FAIL)
2276 {
2277 vim_free(ret_free);
2278 return "[unknown]";
2279 }
2280 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002281 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2282 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002283 vim_free(ret_free);
2284 }
2285 return ga.ga_data;
2286 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287
2288 return name;
2289}
2290
2291/*
2292 * Find "name" in script-local items of script "sid".
2293 * Returns the index in "sn_var_vals" if found.
2294 * If found but not in "sn_var_vals" returns -1.
2295 * If not found returns -2.
2296 */
2297 int
2298get_script_item_idx(int sid, char_u *name, int check_writable)
2299{
2300 hashtab_T *ht;
2301 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002302 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 int idx;
2304
2305 // First look the name up in the hashtable.
2306 if (sid <= 0 || sid > script_items.ga_len)
2307 return -1;
2308 ht = &SCRIPT_VARS(sid);
2309 di = find_var_in_ht(ht, 0, name, TRUE);
2310 if (di == NULL)
2311 return -2;
2312
2313 // Now find the svar_T index in sn_var_vals.
2314 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2315 {
2316 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2317
2318 if (sv->sv_tv == &di->di_tv)
2319 {
2320 if (check_writable && sv->sv_const)
2321 semsg(_(e_readonlyvar), name);
2322 return idx;
2323 }
2324 }
2325 return -1;
2326}
2327
2328/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002329 * Find "name" in imported items of the current script or in "cctx" if not
2330 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002331 */
2332 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002333find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002335 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336 int idx;
2337
2338 if (cctx != NULL)
2339 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2340 {
2341 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2342 + idx;
2343
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002344 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2345 : STRLEN(import->imp_name) == len
2346 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 return import;
2348 }
2349
2350 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2351 {
2352 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2353
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002354 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2355 : STRLEN(import->imp_name) == len
2356 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002357 return import;
2358 }
2359 return NULL;
2360}
2361
2362/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002363 * Free all imported variables.
2364 */
2365 static void
2366free_imported(cctx_T *cctx)
2367{
2368 int idx;
2369
2370 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2371 {
2372 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2373
2374 vim_free(import->imp_name);
2375 }
2376 ga_clear(&cctx->ctx_imports);
2377}
2378
2379/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002380 * Get the next line of the function from "cctx".
2381 * Returns NULL when at the end.
2382 */
2383 static char_u *
2384next_line_from_context(cctx_T *cctx)
2385{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002386 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002387
2388 do
2389 {
2390 ++cctx->ctx_lnum;
2391 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002392 {
2393 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002394 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002395 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002396 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002397 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002398 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar675f7162020-04-12 22:53:54 +02002399 } while (line == NULL || *skipwhite(line) == NUL);
Bram Moolenaare6085c52020-04-12 20:19:16 +02002400 return line;
2401}
2402
2403/*
Bram Moolenaar2c330432020-04-13 14:41:35 +02002404 * Return TRUE if "p" points at a "#" but not at "#{".
2405 */
2406 static int
2407comment_start(char_u *p)
2408{
2409 return p[0] == '#' && p[1] != '{';
2410}
2411
2412/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002413 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002414 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002415 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2416 */
2417 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002418may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002419{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002420 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002421 {
2422 char_u *next = next_line_from_context(cctx);
2423
2424 if (next == NULL)
2425 return FAIL;
2426 *arg = skipwhite(next);
2427 }
2428 return OK;
2429}
2430
Bram Moolenaara5565e42020-05-09 15:44:01 +02002431// Structure passed between the compile_expr* functions to keep track of
2432// constants that have been parsed but for which no code was produced yet. If
2433// possible expressions on these constants are applied at compile time. If
2434// that is not possible, the code to push the constants needs to be generated
2435// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002436// Using 50 should be more than enough of 5 levels of ().
2437#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002438typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002439 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002440 int pp_used; // active entries in pp_tv[]
2441} ppconst_T;
2442
Bram Moolenaar1c747212020-05-09 18:28:34 +02002443static int compile_expr0(char_u **arg, cctx_T *cctx);
2444static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2445
Bram Moolenaara5565e42020-05-09 15:44:01 +02002446/*
2447 * Generate a PUSH instruction for "tv".
2448 * "tv" will be consumed or cleared.
2449 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2450 */
2451 static int
2452generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2453{
2454 if (tv != NULL)
2455 {
2456 switch (tv->v_type)
2457 {
2458 case VAR_UNKNOWN:
2459 break;
2460 case VAR_BOOL:
2461 generate_PUSHBOOL(cctx, tv->vval.v_number);
2462 break;
2463 case VAR_SPECIAL:
2464 generate_PUSHSPEC(cctx, tv->vval.v_number);
2465 break;
2466 case VAR_NUMBER:
2467 generate_PUSHNR(cctx, tv->vval.v_number);
2468 break;
2469#ifdef FEAT_FLOAT
2470 case VAR_FLOAT:
2471 generate_PUSHF(cctx, tv->vval.v_float);
2472 break;
2473#endif
2474 case VAR_BLOB:
2475 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2476 tv->vval.v_blob = NULL;
2477 break;
2478 case VAR_STRING:
2479 generate_PUSHS(cctx, tv->vval.v_string);
2480 tv->vval.v_string = NULL;
2481 break;
2482 default:
2483 iemsg("constant type not supported");
2484 clear_tv(tv);
2485 return FAIL;
2486 }
2487 tv->v_type = VAR_UNKNOWN;
2488 }
2489 return OK;
2490}
2491
2492/*
2493 * Generate code for any ppconst entries.
2494 */
2495 static int
2496generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2497{
2498 int i;
2499 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002500 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002501
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002502 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002503 for (i = 0; i < ppconst->pp_used; ++i)
2504 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2505 ret = FAIL;
2506 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002507 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002508 return ret;
2509}
2510
2511/*
2512 * Clear ppconst constants. Used when failing.
2513 */
2514 static void
2515clear_ppconst(ppconst_T *ppconst)
2516{
2517 int i;
2518
2519 for (i = 0; i < ppconst->pp_used; ++i)
2520 clear_tv(&ppconst->pp_tv[i]);
2521 ppconst->pp_used = 0;
2522}
2523
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002524/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002525 * Generate an instruction to load script-local variable "name", without the
2526 * leading "s:".
2527 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 */
2529 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002530compile_load_scriptvar(
2531 cctx_T *cctx,
2532 char_u *name, // variable NUL terminated
2533 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002534 char_u **end, // end of variable
2535 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002537 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2539 imported_T *import;
2540
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002541 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002543 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002544 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2545 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 }
2547 if (idx >= 0)
2548 {
2549 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2550
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002551 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 current_sctx.sc_sid, idx, sv->sv_type);
2553 return OK;
2554 }
2555
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002556 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 if (import != NULL)
2558 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002559 if (import->imp_all)
2560 {
2561 char_u *p = skipwhite(*end);
2562 int name_len;
2563 ufunc_T *ufunc;
2564 type_T *type;
2565
2566 // Used "import * as Name", need to lookup the member.
2567 if (*p != '.')
2568 {
2569 semsg(_("E1060: expected dot after name: %s"), start);
2570 return FAIL;
2571 }
2572 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002573 if (VIM_ISWHITE(*p))
2574 {
2575 emsg(_("E1074: no white space allowed after dot"));
2576 return FAIL;
2577 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002578
2579 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2580 // TODO: what if it is a function?
2581 if (idx < 0)
2582 return FAIL;
2583 *end = p;
2584
2585 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2586 import->imp_sid,
2587 idx,
2588 type);
2589 }
2590 else
2591 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002592 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002593 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2594 import->imp_sid,
2595 import->imp_var_vals_idx,
2596 import->imp_type);
2597 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598 return OK;
2599 }
2600
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002601 if (error)
2602 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 return FAIL;
2604}
2605
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002606 static int
2607generate_funcref(cctx_T *cctx, char_u *name)
2608{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002609 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002610
2611 if (ufunc == NULL)
2612 return FAIL;
2613
2614 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2615}
2616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617/*
2618 * Compile a variable name into a load instruction.
2619 * "end" points to just after the name.
2620 * When "error" is FALSE do not give an error when not found.
2621 */
2622 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002623compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624{
2625 type_T *type;
2626 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002627 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002629 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630
2631 if (*(*arg + 1) == ':')
2632 {
2633 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002634 if (end <= *arg + 2)
2635 name = vim_strsave((char_u *)"[empty]");
2636 else
2637 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638 if (name == NULL)
2639 return FAIL;
2640
2641 if (**arg == 'v')
2642 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002643 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 }
2645 else if (**arg == 'g')
2646 {
2647 // Global variables can be defined later, thus we don't check if it
2648 // exists, give error at runtime.
2649 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2650 }
2651 else if (**arg == 's')
2652 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002653 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002655 else if (**arg == 'b')
2656 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002657 // Buffer-local variables can be defined later, thus we don't check
2658 // if it exists, give error at runtime.
2659 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002660 }
2661 else if (**arg == 'w')
2662 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002663 // Window-local variables can be defined later, thus we don't check
2664 // if it exists, give error at runtime.
2665 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002666 }
2667 else if (**arg == 't')
2668 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002669 // Tabpage-local variables can be defined later, thus we don't
2670 // check if it exists, give error at runtime.
2671 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002672 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 else
2674 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002675 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 goto theend;
2677 }
2678 }
2679 else
2680 {
2681 size_t len = end - *arg;
2682 int idx;
2683 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002684 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685
2686 name = vim_strnsave(*arg, end - *arg);
2687 if (name == NULL)
2688 return FAIL;
2689
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002690 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002692 if (!gen_load_outer)
2693 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 }
2695 else
2696 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002697 lvar_T *lvar = lookup_local(*arg, len, cctx);
2698
2699 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002701 type = lvar->lv_type;
2702 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002703 if (lvar->lv_from_outer)
2704 gen_load_outer = TRUE;
2705 else
2706 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 }
2708 else
2709 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002710 // "var" can be script-local even without using "s:" if it
2711 // already exists.
2712 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2713 == SCRIPT_VERSION_VIM9
2714 || lookup_script(*arg, len) == OK)
2715 res = compile_load_scriptvar(cctx, name, *arg, &end,
2716 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002717
Bram Moolenaara5565e42020-05-09 15:44:01 +02002718 // When the name starts with an uppercase letter or "x:" it
2719 // can be a user defined function.
2720 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2721 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722 }
2723 }
2724 if (gen_load)
2725 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002726 if (gen_load_outer)
2727 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 }
2729
2730 *arg = end;
2731
2732theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002733 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 semsg(_(e_var_notfound), name);
2735 vim_free(name);
2736 return res;
2737}
2738
2739/*
2740 * Compile the argument expressions.
2741 * "arg" points to just after the "(" and is advanced to after the ")"
2742 */
2743 static int
2744compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2745{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002746 char_u *p = *arg;
2747 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748
Bram Moolenaare6085c52020-04-12 20:19:16 +02002749 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002751 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaare6085c52020-04-12 20:19:16 +02002752 {
2753 p = next_line_from_context(cctx);
2754 if (p == NULL)
Bram Moolenaar2c330432020-04-13 14:41:35 +02002755 goto failret;
2756 whitep = (char_u *)" ";
Bram Moolenaare6085c52020-04-12 20:19:16 +02002757 p = skipwhite(p);
2758 }
2759 if (*p == ')')
2760 {
2761 *arg = p + 1;
2762 return OK;
2763 }
2764
Bram Moolenaara5565e42020-05-09 15:44:01 +02002765 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 return FAIL;
2767 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002768
2769 if (*p != ',' && *skipwhite(p) == ',')
2770 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002771 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002772 p = skipwhite(p);
2773 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002775 {
2776 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002777 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002778 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002779 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002780 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002781 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002783failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002784 emsg(_(e_missing_close));
2785 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786}
2787
2788/*
2789 * Compile a function call: name(arg1, arg2)
2790 * "arg" points to "name", "arg + varlen" to the "(".
2791 * "argcount_init" is 1 for "value->method()"
2792 * Instructions:
2793 * EVAL arg1
2794 * EVAL arg2
2795 * BCALL / DCALL / UCALL
2796 */
2797 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002798compile_call(
2799 char_u **arg,
2800 size_t varlen,
2801 cctx_T *cctx,
2802 ppconst_T *ppconst,
2803 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804{
2805 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002806 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 int argcount = argcount_init;
2808 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002809 char_u fname_buf[FLEN_FIXED + 1];
2810 char_u *tofree = NULL;
2811 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002813 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814
Bram Moolenaara5565e42020-05-09 15:44:01 +02002815 // we can evaluate "has('name')" at compile time
2816 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2817 {
2818 char_u *s = skipwhite(*arg + varlen + 1);
2819 typval_T argvars[2];
2820
2821 argvars[0].v_type = VAR_UNKNOWN;
2822 if (*s == '"')
2823 (void)get_string_tv(&s, &argvars[0], TRUE);
2824 else if (*s == '\'')
2825 (void)get_lit_string_tv(&s, &argvars[0], TRUE);
2826 s = skipwhite(s);
2827 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2828 {
2829 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2830
2831 *arg = s + 1;
2832 argvars[1].v_type = VAR_UNKNOWN;
2833 tv->v_type = VAR_NUMBER;
2834 tv->vval.v_number = 0;
2835 f_has(argvars, tv);
2836 clear_tv(&argvars[0]);
2837 ++ppconst->pp_used;
2838 return OK;
2839 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002840 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002841 }
2842
2843 if (generate_ppconst(cctx, ppconst) == FAIL)
2844 return FAIL;
2845
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 if (varlen >= sizeof(namebuf))
2847 {
2848 semsg(_("E1011: name too long: %s"), name);
2849 return FAIL;
2850 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002851 vim_strncpy(namebuf, *arg, varlen);
2852 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853
2854 *arg = skipwhite(*arg + varlen + 1);
2855 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002856 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002858 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 {
2860 int idx;
2861
2862 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002863 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002865 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002866 else
2867 semsg(_(e_unknownfunc), namebuf);
2868 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 }
2870
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002872 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002874 {
2875 res = generate_CALL(cctx, ufunc, argcount);
2876 goto theend;
2877 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878
2879 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002880 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002882 if (STRNCMP(namebuf, "g:", 2) != 0
2883 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002884 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002885 garray_T *stack = &cctx->ctx_type_stack;
2886 type_T *type;
2887
2888 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2889 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002890 goto theend;
2891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002893 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002894 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002895 if (STRNCMP(namebuf, "g:", 2) == 0)
2896 res = generate_UCALL(cctx, name, argcount);
2897 else
2898 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002899
2900theend:
2901 vim_free(tofree);
2902 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903}
2904
2905// like NAMESPACE_CHAR but with 'a' and 'l'.
2906#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2907
2908/*
2909 * Find the end of a variable or function name. Unlike find_name_end() this
2910 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002911 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 * Return a pointer to just after the name. Equal to "arg" if there is no
2913 * valid name.
2914 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002915 static char_u *
2916to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917{
2918 char_u *p;
2919
2920 // Quick check for valid starting character.
2921 if (!eval_isnamec1(*arg))
2922 return arg;
2923
2924 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2925 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2926 // and can be used in slice "[n:]".
2927 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002928 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2930 break;
2931 return p;
2932}
2933
2934/*
2935 * Like to_name_end() but also skip over a list or dict constant.
2936 */
2937 char_u *
2938to_name_const_end(char_u *arg)
2939{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002940 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 typval_T rettv;
2942
2943 if (p == arg && *arg == '[')
2944 {
2945
2946 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar32e35112020-05-14 22:41:15 +02002947 if (get_list_tv(&p, &rettv, 0, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 p = arg;
2949 }
2950 else if (p == arg && *arg == '#' && arg[1] == '{')
2951 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002952 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 ++p;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002954 if (eval_dict(&p, &rettv, 0, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 p = arg;
2956 }
2957 else if (p == arg && *arg == '{')
2958 {
2959 int ret = get_lambda_tv(&p, &rettv, FALSE);
2960
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002961 // Can be "{x -> ret}()".
2962 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 if (ret == NOTDONE)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002964 ret = eval_dict(&p, &rettv, 0, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 if (ret != OK)
2966 p = arg;
2967 }
2968
2969 return p;
2970}
2971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972/*
2973 * parse a list: [expr, expr]
2974 * "*arg" points to the '['.
2975 */
2976 static int
2977compile_list(char_u **arg, cctx_T *cctx)
2978{
2979 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002980 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 int count = 0;
2982
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002983 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002985 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaara30590d2020-03-28 22:06:23 +01002986 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002987 p = next_line_from_context(cctx);
2988 if (p == NULL)
2989 {
2990 semsg(_(e_list_end), *arg);
2991 return FAIL;
2992 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002993 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002994 p = skipwhite(p);
2995 }
2996 if (*p == ']')
2997 {
2998 ++p;
2999 // Allow for following comment, after at least one space.
3000 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
3001 p += STRLEN(p);
3002 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003003 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003004 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 break;
3006 ++count;
3007 if (*p == ',')
3008 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003009 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 p = skipwhite(p);
3011 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003012 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013
3014 generate_NEWLIST(cctx, count);
3015 return OK;
3016}
3017
3018/*
3019 * parse a lambda: {arg, arg -> expr}
3020 * "*arg" points to the '{'.
3021 */
3022 static int
3023compile_lambda(char_u **arg, cctx_T *cctx)
3024{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 typval_T rettv;
3026 ufunc_T *ufunc;
3027
3028 // Get the funcref in "rettv".
Bram Moolenaara30590d2020-03-28 22:06:23 +01003029 if (get_lambda_tv(arg, &rettv, TRUE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003031
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003033 ++ufunc->uf_refcount;
3034 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003035 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036
3037 // The function will have one line: "return {expr}".
3038 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003039 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040
3041 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003042 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 return FAIL;
3044}
3045
3046/*
3047 * Compile a lamda call: expr->{lambda}(args)
3048 * "arg" points to the "{".
3049 */
3050 static int
3051compile_lambda_call(char_u **arg, cctx_T *cctx)
3052{
3053 ufunc_T *ufunc;
3054 typval_T rettv;
3055 int argcount = 1;
3056 int ret = FAIL;
3057
3058 // Get the funcref in "rettv".
3059 if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
3060 return FAIL;
3061
3062 if (**arg != '(')
3063 {
3064 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003065 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 else
3067 semsg(_(e_missing_paren), "lambda");
3068 clear_tv(&rettv);
3069 return FAIL;
3070 }
3071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 ufunc = rettv.vval.v_partial->pt_func;
3073 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003074 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003075 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003076
3077 // The function will have one line: "return {expr}".
3078 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003079 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080
3081 // compile the arguments
3082 *arg = skipwhite(*arg + 1);
3083 if (compile_arguments(arg, cctx, &argcount) == OK)
3084 // call the compiled function
3085 ret = generate_CALL(cctx, ufunc, argcount);
3086
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 return ret;
3088}
3089
3090/*
3091 * parse a dict: {'key': val} or #{key: val}
3092 * "*arg" points to the '{'.
3093 */
3094 static int
3095compile_dict(char_u **arg, cctx_T *cctx, int literal)
3096{
3097 garray_T *instr = &cctx->ctx_instr;
3098 int count = 0;
3099 dict_T *d = dict_alloc();
3100 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003101 char_u *whitep = *arg;
3102 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103
3104 if (d == NULL)
3105 return FAIL;
3106 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003107 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 {
3109 char_u *key = NULL;
3110
Bram Moolenaar2c330432020-04-13 14:41:35 +02003111 while (**arg == NUL || (literal && **arg == '"')
3112 || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003113 {
3114 *arg = next_line_from_context(cctx);
3115 if (*arg == NULL)
3116 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003117 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003118 *arg = skipwhite(*arg);
3119 }
3120
3121 if (**arg == '}')
3122 break;
3123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 if (literal)
3125 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003126 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127
Bram Moolenaar2c330432020-04-13 14:41:35 +02003128 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 {
3130 semsg(_("E1014: Invalid key: %s"), *arg);
3131 return FAIL;
3132 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003133 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 if (generate_PUSHS(cctx, key) == FAIL)
3135 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003136 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 }
3138 else
3139 {
3140 isn_T *isn;
3141
Bram Moolenaara5565e42020-05-09 15:44:01 +02003142 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 return FAIL;
3144 // TODO: check type is string
3145 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3146 if (isn->isn_type == ISN_PUSHS)
3147 key = isn->isn_arg.string;
3148 }
3149
3150 // Check for duplicate keys, if using string keys.
3151 if (key != NULL)
3152 {
3153 item = dict_find(d, key, -1);
3154 if (item != NULL)
3155 {
3156 semsg(_(e_duplicate_key), key);
3157 goto failret;
3158 }
3159 item = dictitem_alloc(key);
3160 if (item != NULL)
3161 {
3162 item->di_tv.v_type = VAR_UNKNOWN;
3163 item->di_tv.v_lock = 0;
3164 if (dict_add(d, item) == FAIL)
3165 dictitem_free(item);
3166 }
3167 }
3168
3169 *arg = skipwhite(*arg);
3170 if (**arg != ':')
3171 {
3172 semsg(_(e_missing_dict_colon), *arg);
3173 return FAIL;
3174 }
3175
Bram Moolenaar2c330432020-04-13 14:41:35 +02003176 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 *arg = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003178 while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003179 {
3180 *arg = next_line_from_context(cctx);
3181 if (*arg == NULL)
3182 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003183 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003184 *arg = skipwhite(*arg);
3185 }
3186
Bram Moolenaara5565e42020-05-09 15:44:01 +02003187 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 return FAIL;
3189 ++count;
3190
Bram Moolenaar2c330432020-04-13 14:41:35 +02003191 whitep = *arg;
3192 p = skipwhite(*arg);
3193 while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003194 {
3195 *arg = next_line_from_context(cctx);
3196 if (*arg == NULL)
3197 goto failret;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003198 whitep = (char_u *)" ";
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003199 *arg = skipwhite(*arg);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003200 p = *arg;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003201 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 if (**arg == '}')
3203 break;
3204 if (**arg != ',')
3205 {
3206 semsg(_(e_missing_dict_comma), *arg);
3207 goto failret;
3208 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003209 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 *arg = skipwhite(*arg + 1);
3211 }
3212
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 *arg = *arg + 1;
3214
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003215 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003216 p = skipwhite(*arg);
3217 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003218 *arg += STRLEN(*arg);
3219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 dict_unref(d);
3221 return generate_NEWDICT(cctx, count);
3222
3223failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003224 if (*arg == NULL)
3225 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 dict_unref(d);
3227 return FAIL;
3228}
3229
3230/*
3231 * Compile "&option".
3232 */
3233 static int
3234compile_get_option(char_u **arg, cctx_T *cctx)
3235{
3236 typval_T rettv;
3237 char_u *start = *arg;
3238 int ret;
3239
3240 // parse the option and get the current value to get the type.
3241 rettv.v_type = VAR_UNKNOWN;
3242 ret = get_option_tv(arg, &rettv, TRUE);
3243 if (ret == OK)
3244 {
3245 // include the '&' in the name, get_option_tv() expects it.
3246 char_u *name = vim_strnsave(start, *arg - start);
3247 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3248
3249 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3250 vim_free(name);
3251 }
3252 clear_tv(&rettv);
3253
3254 return ret;
3255}
3256
3257/*
3258 * Compile "$VAR".
3259 */
3260 static int
3261compile_get_env(char_u **arg, cctx_T *cctx)
3262{
3263 char_u *start = *arg;
3264 int len;
3265 int ret;
3266 char_u *name;
3267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 ++*arg;
3269 len = get_env_len(arg);
3270 if (len == 0)
3271 {
3272 semsg(_(e_syntax_at), start - 1);
3273 return FAIL;
3274 }
3275
3276 // include the '$' in the name, get_env_tv() expects it.
3277 name = vim_strnsave(start, len + 1);
3278 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3279 vim_free(name);
3280 return ret;
3281}
3282
3283/*
3284 * Compile "@r".
3285 */
3286 static int
3287compile_get_register(char_u **arg, cctx_T *cctx)
3288{
3289 int ret;
3290
3291 ++*arg;
3292 if (**arg == NUL)
3293 {
3294 semsg(_(e_syntax_at), *arg - 1);
3295 return FAIL;
3296 }
3297 if (!valid_yank_reg(**arg, TRUE))
3298 {
3299 emsg_invreg(**arg);
3300 return FAIL;
3301 }
3302 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3303 ++*arg;
3304 return ret;
3305}
3306
3307/*
3308 * Apply leading '!', '-' and '+' to constant "rettv".
3309 */
3310 static int
3311apply_leader(typval_T *rettv, char_u *start, char_u *end)
3312{
3313 char_u *p = end;
3314
3315 // this works from end to start
3316 while (p > start)
3317 {
3318 --p;
3319 if (*p == '-' || *p == '+')
3320 {
3321 // only '-' has an effect, for '+' we only check the type
3322#ifdef FEAT_FLOAT
3323 if (rettv->v_type == VAR_FLOAT)
3324 {
3325 if (*p == '-')
3326 rettv->vval.v_float = -rettv->vval.v_float;
3327 }
3328 else
3329#endif
3330 {
3331 varnumber_T val;
3332 int error = FALSE;
3333
3334 // tv_get_number_chk() accepts a string, but we don't want that
3335 // here
3336 if (check_not_string(rettv) == FAIL)
3337 return FAIL;
3338 val = tv_get_number_chk(rettv, &error);
3339 clear_tv(rettv);
3340 if (error)
3341 return FAIL;
3342 if (*p == '-')
3343 val = -val;
3344 rettv->v_type = VAR_NUMBER;
3345 rettv->vval.v_number = val;
3346 }
3347 }
3348 else
3349 {
3350 int v = tv2bool(rettv);
3351
3352 // '!' is permissive in the type.
3353 clear_tv(rettv);
3354 rettv->v_type = VAR_BOOL;
3355 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3356 }
3357 }
3358 return OK;
3359}
3360
3361/*
3362 * Recognize v: variables that are constants and set "rettv".
3363 */
3364 static void
3365get_vim_constant(char_u **arg, typval_T *rettv)
3366{
3367 if (STRNCMP(*arg, "v:true", 6) == 0)
3368 {
3369 rettv->v_type = VAR_BOOL;
3370 rettv->vval.v_number = VVAL_TRUE;
3371 *arg += 6;
3372 }
3373 else if (STRNCMP(*arg, "v:false", 7) == 0)
3374 {
3375 rettv->v_type = VAR_BOOL;
3376 rettv->vval.v_number = VVAL_FALSE;
3377 *arg += 7;
3378 }
3379 else if (STRNCMP(*arg, "v:null", 6) == 0)
3380 {
3381 rettv->v_type = VAR_SPECIAL;
3382 rettv->vval.v_number = VVAL_NULL;
3383 *arg += 6;
3384 }
3385 else if (STRNCMP(*arg, "v:none", 6) == 0)
3386 {
3387 rettv->v_type = VAR_SPECIAL;
3388 rettv->vval.v_number = VVAL_NONE;
3389 *arg += 6;
3390 }
3391}
3392
Bram Moolenaar61a89812020-05-07 16:58:17 +02003393 static exptype_T
3394get_compare_type(char_u *p, int *len, int *type_is)
3395{
3396 exptype_T type = EXPR_UNKNOWN;
3397 int i;
3398
3399 switch (p[0])
3400 {
3401 case '=': if (p[1] == '=')
3402 type = EXPR_EQUAL;
3403 else if (p[1] == '~')
3404 type = EXPR_MATCH;
3405 break;
3406 case '!': if (p[1] == '=')
3407 type = EXPR_NEQUAL;
3408 else if (p[1] == '~')
3409 type = EXPR_NOMATCH;
3410 break;
3411 case '>': if (p[1] != '=')
3412 {
3413 type = EXPR_GREATER;
3414 *len = 1;
3415 }
3416 else
3417 type = EXPR_GEQUAL;
3418 break;
3419 case '<': if (p[1] != '=')
3420 {
3421 type = EXPR_SMALLER;
3422 *len = 1;
3423 }
3424 else
3425 type = EXPR_SEQUAL;
3426 break;
3427 case 'i': if (p[1] == 's')
3428 {
3429 // "is" and "isnot"; but not a prefix of a name
3430 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3431 *len = 5;
3432 i = p[*len];
3433 if (!isalnum(i) && i != '_')
3434 {
3435 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3436 *type_is = TRUE;
3437 }
3438 }
3439 break;
3440 }
3441 return type;
3442}
3443
3444/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 * Compile code to apply '-', '+' and '!'.
3446 */
3447 static int
3448compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3449{
3450 char_u *p = end;
3451
3452 // this works from end to start
3453 while (p > start)
3454 {
3455 --p;
3456 if (*p == '-' || *p == '+')
3457 {
3458 int negate = *p == '-';
3459 isn_T *isn;
3460
3461 // TODO: check type
3462 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3463 {
3464 --p;
3465 if (*p == '-')
3466 negate = !negate;
3467 }
3468 // only '-' has an effect, for '+' we only check the type
3469 if (negate)
3470 isn = generate_instr(cctx, ISN_NEGATENR);
3471 else
3472 isn = generate_instr(cctx, ISN_CHECKNR);
3473 if (isn == NULL)
3474 return FAIL;
3475 }
3476 else
3477 {
3478 int invert = TRUE;
3479
3480 while (p > start && p[-1] == '!')
3481 {
3482 --p;
3483 invert = !invert;
3484 }
3485 if (generate_2BOOL(cctx, invert) == FAIL)
3486 return FAIL;
3487 }
3488 }
3489 return OK;
3490}
3491
3492/*
3493 * Compile whatever comes after "name" or "name()".
3494 */
3495 static int
3496compile_subscript(
3497 char_u **arg,
3498 cctx_T *cctx,
3499 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003500 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003501 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502{
3503 for (;;)
3504 {
3505 if (**arg == '(')
3506 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003507 garray_T *stack = &cctx->ctx_type_stack;
3508 type_T *type;
3509 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003511 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003512 return FAIL;
3513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003515 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3516
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 *arg = skipwhite(*arg + 1);
3518 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3519 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003520 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 return FAIL;
3522 }
3523 else if (**arg == '-' && (*arg)[1] == '>')
3524 {
3525 char_u *p;
3526
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003527 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003528 return FAIL;
3529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 // something->method()
3531 // Apply the '!', '-' and '+' first:
3532 // -1.0->func() works like (-1.0)->func()
3533 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3534 return FAIL;
3535 *start_leader = end_leader; // don't apply again later
3536
3537 *arg = skipwhite(*arg + 2);
3538 if (**arg == '{')
3539 {
3540 // lambda call: list->{lambda}
3541 if (compile_lambda_call(arg, cctx) == FAIL)
3542 return FAIL;
3543 }
3544 else
3545 {
3546 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003547 p = *arg;
3548 if (ASCII_ISALPHA(*p) && p[1] == ':')
3549 p += 2;
3550 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 ;
3552 if (*p != '(')
3553 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003554 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 return FAIL;
3556 }
3557 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003558 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 return FAIL;
3560 }
3561 }
3562 else if (**arg == '[')
3563 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003564 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003565 type_T **typep;
3566
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003567 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003568 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003569 // TODO: blob index
3570 // TODO: more arguments
3571 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003572 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003573 return FAIL;
3574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003576 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 return FAIL;
3578
3579 if (**arg != ']')
3580 {
3581 emsg(_(e_missbrac));
3582 return FAIL;
3583 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003584 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003586 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3587 if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003588 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003589 if ((*typep)->tt_type == VAR_LIST)
3590 *typep = (*typep)->tt_member;
3591 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3592 return FAIL;
3593 }
3594 else if ((*typep)->tt_type == VAR_DICT)
3595 {
3596 *typep = (*typep)->tt_member;
3597 if (may_generate_2STRING(-1, cctx) == FAIL)
3598 return FAIL;
3599 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3600 return FAIL;
3601 }
3602 else
3603 {
3604 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003605 return FAIL;
3606 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 }
3608 else if (**arg == '.' && (*arg)[1] != '.')
3609 {
3610 char_u *p;
3611
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003612 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003613 return FAIL;
3614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 ++*arg;
3616 p = *arg;
3617 // dictionary member: dict.name
3618 if (eval_isnamec1(*p))
3619 while (eval_isnamec(*p))
3620 MB_PTR_ADV(p);
3621 if (p == *arg)
3622 {
3623 semsg(_(e_syntax_at), *arg);
3624 return FAIL;
3625 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003626 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 return FAIL;
3628 *arg = p;
3629 }
3630 else
3631 break;
3632 }
3633
3634 // TODO - see handle_subscript():
3635 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3636 // Don't do this when "Func" is already a partial that was bound
3637 // explicitly (pt_auto is FALSE).
3638
3639 return OK;
3640}
3641
3642/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003643 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3644 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003646 * If the value is a constant "ppconst->pp_ret" will be set.
3647 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003648 *
3649 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 */
3651
3652/*
3653 * number number constant
3654 * 0zFFFFFFFF Blob constant
3655 * "string" string constant
3656 * 'string' literal string constant
3657 * &option-name option value
3658 * @r register contents
3659 * identifier variable value
3660 * function() function call
3661 * $VAR environment variable
3662 * (expression) nested expression
3663 * [expr, expr] List
3664 * {key: val, key: val} Dictionary
3665 * #{key: val, key: val} Dictionary with literal keys
3666 *
3667 * Also handle:
3668 * ! in front logical NOT
3669 * - in front unary minus
3670 * + in front unary plus (ignored)
3671 * trailing (arg) funcref/partial call
3672 * trailing [] subscript in String or List
3673 * trailing .name entry in Dictionary
3674 * trailing ->name() method call
3675 */
3676 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003677compile_expr7(
3678 char_u **arg,
3679 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003680 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 char_u *start_leader, *end_leader;
3683 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003684 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003685 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686
3687 /*
3688 * Skip '!', '-' and '+' characters. They are handled later.
3689 */
3690 start_leader = *arg;
3691 while (**arg == '!' || **arg == '-' || **arg == '+')
3692 *arg = skipwhite(*arg + 1);
3693 end_leader = *arg;
3694
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003695 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 switch (**arg)
3697 {
3698 /*
3699 * Number constant.
3700 */
3701 case '0': // also for blob starting with 0z
3702 case '1':
3703 case '2':
3704 case '3':
3705 case '4':
3706 case '5':
3707 case '6':
3708 case '7':
3709 case '8':
3710 case '9':
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003711 case '.': if (get_number_tv(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 return FAIL;
3713 break;
3714
3715 /*
3716 * String constant: "string".
3717 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003718 case '"': if (get_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 return FAIL;
3720 break;
3721
3722 /*
3723 * Literal string constant: 'str''ing'.
3724 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003725 case '\'': if (get_lit_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 return FAIL;
3727 break;
3728
3729 /*
3730 * Constant Vim variable.
3731 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003732 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 ret = NOTDONE;
3734 break;
3735
3736 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003737 * "true" constant
3738 */
3739 case 't': if (STRNCMP(*arg, "true", 4) == 0
3740 && !eval_isnamec((*arg)[4]))
3741 {
3742 *arg += 4;
3743 rettv->v_type = VAR_BOOL;
3744 rettv->vval.v_number = VVAL_TRUE;
3745 }
3746 else
3747 ret = NOTDONE;
3748 break;
3749
3750 /*
3751 * "false" constant
3752 */
3753 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3754 && !eval_isnamec((*arg)[5]))
3755 {
3756 *arg += 5;
3757 rettv->v_type = VAR_BOOL;
3758 rettv->vval.v_number = VVAL_FALSE;
3759 }
3760 else
3761 ret = NOTDONE;
3762 break;
3763
3764 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 * List: [expr, expr]
3766 */
3767 case '[': ret = compile_list(arg, cctx);
3768 break;
3769
3770 /*
3771 * Dictionary: #{key: val, key: val}
3772 */
3773 case '#': if ((*arg)[1] == '{')
3774 {
3775 ++*arg;
3776 ret = compile_dict(arg, cctx, TRUE);
3777 }
3778 else
3779 ret = NOTDONE;
3780 break;
3781
3782 /*
3783 * Lambda: {arg, arg -> expr}
3784 * Dictionary: {'key': val, 'key': val}
3785 */
3786 case '{': {
3787 char_u *start = skipwhite(*arg + 1);
3788
3789 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003790 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003792 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 if (ret != FAIL && *start == '>')
3794 ret = compile_lambda(arg, cctx);
3795 else
3796 ret = compile_dict(arg, cctx, FALSE);
3797 }
3798 break;
3799
3800 /*
3801 * Option value: &name
3802 */
3803 case '&': ret = compile_get_option(arg, cctx);
3804 break;
3805
3806 /*
3807 * Environment variable: $VAR.
3808 */
3809 case '$': ret = compile_get_env(arg, cctx);
3810 break;
3811
3812 /*
3813 * Register contents: @r.
3814 */
3815 case '@': ret = compile_get_register(arg, cctx);
3816 break;
3817 /*
3818 * nested expression: (expression).
3819 */
3820 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003821
3822 // recursive!
3823 if (ppconst->pp_used <= PPSIZE - 10)
3824 {
3825 ret = compile_expr1(arg, cctx, ppconst);
3826 }
3827 else
3828 {
3829 // Not enough space in ppconst, flush constants.
3830 if (generate_ppconst(cctx, ppconst) == FAIL)
3831 return FAIL;
3832 ret = compile_expr0(arg, cctx);
3833 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 *arg = skipwhite(*arg);
3835 if (**arg == ')')
3836 ++*arg;
3837 else if (ret == OK)
3838 {
3839 emsg(_(e_missing_close));
3840 ret = FAIL;
3841 }
3842 break;
3843
3844 default: ret = NOTDONE;
3845 break;
3846 }
3847 if (ret == FAIL)
3848 return FAIL;
3849
Bram Moolenaar1c747212020-05-09 18:28:34 +02003850 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 {
3852 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003853 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003855 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 return FAIL;
3857 }
3858 start_leader = end_leader; // don't apply again below
3859
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003860 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003861 clear_tv(rettv);
3862 else
3863 // A constant expression can possibly be handled compile time,
3864 // return the value instead of generating code.
3865 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 }
3867 else if (ret == NOTDONE)
3868 {
3869 char_u *p;
3870 int r;
3871
3872 if (!eval_isnamec1(**arg))
3873 {
3874 semsg(_("E1015: Name expected: %s"), *arg);
3875 return FAIL;
3876 }
3877
3878 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003879 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003881 {
3882 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3883 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003885 {
3886 if (generate_ppconst(cctx, ppconst) == FAIL)
3887 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003889 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 if (r == FAIL)
3891 return FAIL;
3892 }
3893
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003894 // Handle following "[]", ".member", etc.
3895 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003896 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003897 ppconst) == FAIL)
3898 return FAIL;
3899 if (ppconst->pp_used > 0)
3900 {
3901 // apply the '!', '-' and '+' before the constant
3902 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3903 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
3904 return FAIL;
3905 return OK;
3906 }
3907 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003909 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910}
3911
3912/*
3913 * * number multiplication
3914 * / number division
3915 * % number modulo
3916 */
3917 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003918compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919{
3920 char_u *op;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003921 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003923 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003924 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 return FAIL;
3926
3927 /*
3928 * Repeat computing, until no "*", "/" or "%" is following.
3929 */
3930 for (;;)
3931 {
3932 op = skipwhite(*arg);
3933 if (*op != '*' && *op != '/' && *op != '%')
3934 break;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003935
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003936 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 {
3938 char_u buf[3];
3939
3940 vim_strncpy(buf, op, 1);
3941 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003942 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 }
3944 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003945 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003946 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003948 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003949 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003951
3952 if (ppconst->pp_used == ppconst_used + 2
3953 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3954 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003955 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003956 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3957 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003958 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003960 // both are numbers: compute the result
3961 switch (*op)
3962 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003963 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003964 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003965 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003966 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003967 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003968 break;
3969 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003970 tv1->vval.v_number = res;
3971 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003972 }
3973 else
3974 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003975 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003976 generate_two_op(cctx, op);
3977 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 }
3979
3980 return OK;
3981}
3982
3983/*
3984 * + number addition
3985 * - number subtraction
3986 * .. string concatenation
3987 */
3988 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003989compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990{
3991 char_u *op;
3992 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003993 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994
3995 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003996 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997 return FAIL;
3998
3999 /*
4000 * Repeat computing, until no "+", "-" or ".." is following.
4001 */
4002 for (;;)
4003 {
4004 op = skipwhite(*arg);
4005 if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.')))
4006 break;
4007 oplen = (*op == '.' ? 2 : 1);
4008
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004009 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 {
4011 char_u buf[3];
4012
4013 vim_strncpy(buf, op, oplen);
4014 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004015 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 }
4017
4018 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004019 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004020 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004022 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004023 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 return FAIL;
4025
Bram Moolenaara5565e42020-05-09 15:44:01 +02004026 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004027 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004028 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4029 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4030 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4031 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004033 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4034 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004035
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004036 // concat/subtract/add constant numbers
4037 if (*op == '+')
4038 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4039 else if (*op == '-')
4040 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4041 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004042 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004043 // concatenate constant strings
4044 char_u *s1 = tv1->vval.v_string;
4045 char_u *s2 = tv2->vval.v_string;
4046 size_t len1 = STRLEN(s1);
4047
4048 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4049 if (tv1->vval.v_string == NULL)
4050 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004051 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004052 return FAIL;
4053 }
4054 mch_memmove(tv1->vval.v_string, s1, len1);
4055 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004056 vim_free(s1);
4057 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004058 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004059 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 }
4061 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004062 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004063 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004064 if (*op == '.')
4065 {
4066 if (may_generate_2STRING(-2, cctx) == FAIL
4067 || may_generate_2STRING(-1, cctx) == FAIL)
4068 return FAIL;
4069 generate_instr_drop(cctx, ISN_CONCAT, 1);
4070 }
4071 else
4072 generate_two_op(cctx, op);
4073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 }
4075
4076 return OK;
4077}
4078
4079/*
4080 * expr5a == expr5b
4081 * expr5a =~ expr5b
4082 * expr5a != expr5b
4083 * expr5a !~ expr5b
4084 * expr5a > expr5b
4085 * expr5a >= expr5b
4086 * expr5a < expr5b
4087 * expr5a <= expr5b
4088 * expr5a is expr5b
4089 * expr5a isnot expr5b
4090 *
4091 * Produces instructions:
4092 * EVAL expr5a Push result of "expr5a"
4093 * EVAL expr5b Push result of "expr5b"
4094 * COMPARE one of the compare instructions
4095 */
4096 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004097compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098{
4099 exptype_T type = EXPR_UNKNOWN;
4100 char_u *p;
4101 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004103 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104
4105 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004106 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 return FAIL;
4108
4109 p = skipwhite(*arg);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004110 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111
4112 /*
4113 * If there is a comparative operator, use it.
4114 */
4115 if (type != EXPR_UNKNOWN)
4116 {
4117 int ic = FALSE; // Default: do not ignore case
4118
4119 if (type_is && (p[len] == '?' || p[len] == '#'))
4120 {
4121 semsg(_(e_invexpr2), *arg);
4122 return FAIL;
4123 }
4124 // extra question mark appended: ignore case
4125 if (p[len] == '?')
4126 {
4127 ic = TRUE;
4128 ++len;
4129 }
4130 // extra '#' appended: match case (ignored)
4131 else if (p[len] == '#')
4132 ++len;
4133 // nothing appended: match case
4134
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004135 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 {
4137 char_u buf[7];
4138
4139 vim_strncpy(buf, p, len);
4140 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004141 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 }
4143
4144 // get the second variable
4145 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004146 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004147 return FAIL;
4148
Bram Moolenaara5565e42020-05-09 15:44:01 +02004149 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 return FAIL;
4151
Bram Moolenaara5565e42020-05-09 15:44:01 +02004152 if (ppconst->pp_used == ppconst_used + 2)
4153 {
4154 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4155 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4156 int ret;
4157
4158 // Both sides are a constant, compute the result now.
4159 // First check for a valid combination of types, this is more
4160 // strict than typval_compare().
4161 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
4162 ret = FAIL;
4163 else
4164 {
4165 ret = typval_compare(tv1, tv2, type, ic);
4166 tv1->v_type = VAR_BOOL;
4167 tv1->vval.v_number = tv1->vval.v_number
4168 ? VVAL_TRUE : VVAL_FALSE;
4169 clear_tv(tv2);
4170 --ppconst->pp_used;
4171 }
4172 return ret;
4173 }
4174
4175 generate_ppconst(cctx, ppconst);
4176 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 }
4178
4179 return OK;
4180}
4181
Bram Moolenaar7f141552020-05-09 17:35:53 +02004182static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4183
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184/*
4185 * Compile || or &&.
4186 */
4187 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004188compile_and_or(
4189 char_u **arg,
4190 cctx_T *cctx,
4191 char *op,
4192 ppconst_T *ppconst,
4193 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194{
4195 char_u *p = skipwhite(*arg);
4196 int opchar = *op;
4197
4198 if (p[0] == opchar && p[1] == opchar)
4199 {
4200 garray_T *instr = &cctx->ctx_instr;
4201 garray_T end_ga;
4202
4203 /*
4204 * Repeat until there is no following "||" or "&&"
4205 */
4206 ga_init2(&end_ga, sizeof(int), 10);
4207 while (p[0] == opchar && p[1] == opchar)
4208 {
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004209 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4210 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004212 return FAIL;
4213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214
Bram Moolenaara5565e42020-05-09 15:44:01 +02004215 // TODO: use ppconst if the value is a constant
4216 generate_ppconst(cctx, ppconst);
4217
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 if (ga_grow(&end_ga, 1) == FAIL)
4219 {
4220 ga_clear(&end_ga);
4221 return FAIL;
4222 }
4223 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4224 ++end_ga.ga_len;
4225 generate_JUMP(cctx, opchar == '|'
4226 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4227
4228 // eval the next expression
4229 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004230 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004231 return FAIL;
4232
Bram Moolenaara5565e42020-05-09 15:44:01 +02004233 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4234 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 {
4236 ga_clear(&end_ga);
4237 return FAIL;
4238 }
4239 p = skipwhite(*arg);
4240 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004241 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242
4243 // Fill in the end label in all jumps.
4244 while (end_ga.ga_len > 0)
4245 {
4246 isn_T *isn;
4247
4248 --end_ga.ga_len;
4249 isn = ((isn_T *)instr->ga_data)
4250 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4251 isn->isn_arg.jump.jump_where = instr->ga_len;
4252 }
4253 ga_clear(&end_ga);
4254 }
4255
4256 return OK;
4257}
4258
4259/*
4260 * expr4a && expr4a && expr4a logical AND
4261 *
4262 * Produces instructions:
4263 * EVAL expr4a Push result of "expr4a"
4264 * JUMP_AND_KEEP_IF_FALSE end
4265 * EVAL expr4b Push result of "expr4b"
4266 * JUMP_AND_KEEP_IF_FALSE end
4267 * EVAL expr4c Push result of "expr4c"
4268 * end:
4269 */
4270 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004271compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004273 int ppconst_used = ppconst->pp_used;
4274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004276 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 return FAIL;
4278
4279 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004280 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281}
4282
4283/*
4284 * expr3a || expr3b || expr3c logical OR
4285 *
4286 * Produces instructions:
4287 * EVAL expr3a Push result of "expr3a"
4288 * JUMP_AND_KEEP_IF_TRUE end
4289 * EVAL expr3b Push result of "expr3b"
4290 * JUMP_AND_KEEP_IF_TRUE end
4291 * EVAL expr3c Push result of "expr3c"
4292 * end:
4293 */
4294 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004295compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004297 int ppconst_used = ppconst->pp_used;
4298
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004300 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 return FAIL;
4302
4303 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004304 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305}
4306
4307/*
4308 * Toplevel expression: expr2 ? expr1a : expr1b
4309 *
4310 * Produces instructions:
4311 * EVAL expr2 Push result of "expr"
4312 * JUMP_IF_FALSE alt jump if false
4313 * EVAL expr1a
4314 * JUMP_ALWAYS end
4315 * alt: EVAL expr1b
4316 * end:
4317 */
4318 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004319compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320{
4321 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004322 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323
Bram Moolenaar61a89812020-05-07 16:58:17 +02004324 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004325 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 return FAIL;
4327
4328 p = skipwhite(*arg);
4329 if (*p == '?')
4330 {
4331 garray_T *instr = &cctx->ctx_instr;
4332 garray_T *stack = &cctx->ctx_type_stack;
4333 int alt_idx = instr->ga_len;
4334 int end_idx;
4335 isn_T *isn;
4336 type_T *type1;
4337 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004338 int has_const_expr = FALSE;
4339 int const_value = FALSE;
4340 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004342 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4343 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004345 return FAIL;
4346 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347
Bram Moolenaara5565e42020-05-09 15:44:01 +02004348 if (ppconst->pp_used == ppconst_used + 1)
4349 {
4350 // the condition is a constant, we know whether the ? or the :
4351 // expression is to be evaluated.
4352 has_const_expr = TRUE;
4353 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4354 clear_tv(&ppconst->pp_tv[ppconst_used]);
4355 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004356 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4357 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004358 }
4359 else
4360 {
4361 generate_ppconst(cctx, ppconst);
4362 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4363 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364
4365 // evaluate the second expression; any type is accepted
4366 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004367 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004368 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004369 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004370 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371
Bram Moolenaara5565e42020-05-09 15:44:01 +02004372 if (!has_const_expr)
4373 {
4374 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375
Bram Moolenaara5565e42020-05-09 15:44:01 +02004376 // remember the type and drop it
4377 --stack->ga_len;
4378 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379
Bram Moolenaara5565e42020-05-09 15:44:01 +02004380 end_idx = instr->ga_len;
4381 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4382
4383 // jump here from JUMP_IF_FALSE
4384 isn = ((isn_T *)instr->ga_data) + alt_idx;
4385 isn->isn_arg.jump.jump_where = instr->ga_len;
4386 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387
4388 // Check for the ":".
4389 p = skipwhite(*arg);
4390 if (*p != ':')
4391 {
4392 emsg(_(e_missing_colon));
4393 return FAIL;
4394 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004395 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4396 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004398 return FAIL;
4399 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400
4401 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004402 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004403 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4404 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004406 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004407 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004408 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004409 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410
Bram Moolenaara5565e42020-05-09 15:44:01 +02004411 if (!has_const_expr)
4412 {
4413 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414
Bram Moolenaara5565e42020-05-09 15:44:01 +02004415 // If the types differ, the result has a more generic type.
4416 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4417 common_type(type1, type2, &type2, cctx->ctx_type_list);
4418
4419 // jump here from JUMP_ALWAYS
4420 isn = ((isn_T *)instr->ga_data) + end_idx;
4421 isn->isn_arg.jump.jump_where = instr->ga_len;
4422 }
4423
4424 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 }
4426 return OK;
4427}
4428
4429/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004430 * Toplevel expression.
4431 */
4432 static int
4433compile_expr0(char_u **arg, cctx_T *cctx)
4434{
4435 ppconst_T ppconst;
4436
4437 CLEAR_FIELD(ppconst);
4438 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4439 {
4440 clear_ppconst(&ppconst);
4441 return FAIL;
4442 }
4443 if (generate_ppconst(cctx, &ppconst) == FAIL)
4444 return FAIL;
4445 return OK;
4446}
4447
4448/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 * compile "return [expr]"
4450 */
4451 static char_u *
4452compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4453{
4454 char_u *p = arg;
4455 garray_T *stack = &cctx->ctx_type_stack;
4456 type_T *stack_type;
4457
4458 if (*p != NUL && *p != '|' && *p != '\n')
4459 {
4460 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004461 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 return NULL;
4463
4464 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4465 if (set_return_type)
4466 cctx->ctx_ufunc->uf_ret_type = stack_type;
4467 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4468 == FAIL)
4469 return NULL;
4470 }
4471 else
4472 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004473 // "set_return_type" cannot be TRUE, only used for a lambda which
4474 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004475 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4476 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477 {
4478 emsg(_("E1003: Missing return value"));
4479 return NULL;
4480 }
4481
4482 // No argument, return zero.
4483 generate_PUSHNR(cctx, 0);
4484 }
4485
4486 if (generate_instr(cctx, ISN_RETURN) == NULL)
4487 return NULL;
4488
4489 // "return val | endif" is possible
4490 return skipwhite(p);
4491}
4492
4493/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004494 * Get a line from the compilation context, compatible with exarg_T getline().
4495 * Return a pointer to the line in allocated memory.
4496 * Return NULL for end-of-file or some error.
4497 */
4498 static char_u *
4499exarg_getline(
4500 int c UNUSED,
4501 void *cookie,
4502 int indent UNUSED,
4503 int do_concat UNUSED)
4504{
4505 cctx_T *cctx = (cctx_T *)cookie;
4506
4507 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4508 {
4509 iemsg("Heredoc got to end");
4510 return NULL;
4511 }
4512 ++cctx->ctx_lnum;
4513 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4514 [cctx->ctx_lnum]);
4515}
4516
4517/*
4518 * Compile a nested :def command.
4519 */
4520 static char_u *
4521compile_nested_function(exarg_T *eap, cctx_T *cctx)
4522{
4523 char_u *name_start = eap->arg;
4524 char_u *name_end = to_name_end(eap->arg, FALSE);
4525 char_u *name = get_lambda_name();
4526 lvar_T *lvar;
4527 ufunc_T *ufunc;
4528
4529 eap->arg = name_end;
4530 eap->getline = exarg_getline;
4531 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004532 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004533 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004534 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004535
Bram Moolenaar822ba242020-05-24 23:00:18 +02004536 if (ufunc == NULL)
4537 return NULL;
4538 if (ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED
4539 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004540 return NULL;
4541
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004542 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004543 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004544 TRUE, ufunc->uf_func_type);
4545
4546 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4547 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4548 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004549
Bram Moolenaar61a89812020-05-07 16:58:17 +02004550 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004551 return (char_u *)"";
4552}
4553
4554/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 * Return the length of an assignment operator, or zero if there isn't one.
4556 */
4557 int
4558assignment_len(char_u *p, int *heredoc)
4559{
4560 if (*p == '=')
4561 {
4562 if (p[1] == '<' && p[2] == '<')
4563 {
4564 *heredoc = TRUE;
4565 return 3;
4566 }
4567 return 1;
4568 }
4569 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4570 return 2;
4571 if (STRNCMP(p, "..=", 3) == 0)
4572 return 3;
4573 return 0;
4574}
4575
4576// words that cannot be used as a variable
4577static char *reserved[] = {
4578 "true",
4579 "false",
4580 NULL
4581};
4582
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004583typedef enum {
4584 dest_local,
4585 dest_option,
4586 dest_env,
4587 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004588 dest_buffer,
4589 dest_window,
4590 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004591 dest_vimvar,
4592 dest_script,
4593 dest_reg,
4594} assign_dest_T;
4595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004597 * Generate the load instruction for "name".
4598 */
4599 static void
4600generate_loadvar(
4601 cctx_T *cctx,
4602 assign_dest_T dest,
4603 char_u *name,
4604 lvar_T *lvar,
4605 type_T *type)
4606{
4607 switch (dest)
4608 {
4609 case dest_option:
4610 // TODO: check the option exists
4611 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4612 break;
4613 case dest_global:
4614 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4615 break;
4616 case dest_buffer:
4617 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4618 break;
4619 case dest_window:
4620 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4621 break;
4622 case dest_tab:
4623 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4624 break;
4625 case dest_script:
4626 compile_load_scriptvar(cctx,
4627 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4628 break;
4629 case dest_env:
4630 // Include $ in the name here
4631 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4632 break;
4633 case dest_reg:
4634 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4635 break;
4636 case dest_vimvar:
4637 generate_LOADV(cctx, name + 2, TRUE);
4638 break;
4639 case dest_local:
4640 if (lvar->lv_from_outer)
4641 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4642 NULL, type);
4643 else
4644 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4645 break;
4646 }
4647}
4648
4649/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004650 * Compile declaration and assignment:
4651 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004653 * Return NULL for an error.
4654 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 */
4656 static char_u *
4657compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4658{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004659 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004661 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 char_u *ret = NULL;
4663 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004664 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004667 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669 int oplen = 0;
4670 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004671 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004672 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004673 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004677 // Skip over the "var" or "[var, var]" to get to any "=".
4678 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4679 if (p == NULL)
4680 return *arg == '[' ? arg : NULL;
4681
4682 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004684 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685 return NULL;
4686 }
4687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688 sp = p;
4689 p = skipwhite(p);
4690 op = p;
4691 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004692
4693 if (var_count > 0 && oplen == 0)
4694 // can be something like "[1, 2]->func()"
4695 return arg;
4696
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4698 {
4699 char_u buf[4];
4700
4701 vim_strncpy(buf, op, oplen);
4702 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004703 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004704 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004705
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 if (heredoc)
4707 {
4708 list_T *l;
4709 listitem_T *li;
4710
4711 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004712 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004714 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715
4716 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004717 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718 {
4719 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4720 li->li_tv.vval.v_string = NULL;
4721 }
4722 generate_NEWLIST(cctx, l->lv_len);
4723 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004724 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725 list_free(l);
4726 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004727 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004729 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004731 // for "[var, var] = expr" evaluate the expression here, loop over the
4732 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004733
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004734 p = skipwhite(op + oplen);
4735 if (compile_expr0(&p, cctx) == FAIL)
4736 return NULL;
4737 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004739 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004741 type_T *stacktype;
4742
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004743 stacktype = stack->ga_len == 0 ? &t_void
4744 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004745 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004747 emsg(_(e_cannot_use_void));
4748 goto theend;
4749 }
4750 if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL)
4751 goto theend;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004752 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4753 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004754 }
4755 }
4756
4757 /*
4758 * Loop over variables in "[var, var] = expr".
4759 * For "var = expr" and "let var: type" this is done only once.
4760 */
4761 if (var_count > 0)
4762 var_start = skipwhite(arg + 1); // skip over the "["
4763 else
4764 var_start = arg;
4765 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4766 {
4767 char_u *var_end = skip_var_one(var_start, FALSE);
4768 size_t varlen;
4769 int new_local = FALSE;
4770 int opt_type;
4771 int opt_flags = 0;
4772 assign_dest_T dest = dest_local;
4773 int vimvaridx = -1;
4774 lvar_T *lvar = NULL;
4775 lvar_T arg_lvar;
4776 int has_type = FALSE;
4777 int has_index = FALSE;
4778 int instr_count = -1;
4779
4780 p = (*var_start == '&' || *var_start == '$'
4781 || *var_start == '@') ? var_start + 1 : var_start;
4782 p = to_name_end(p, TRUE);
4783
4784 // "a: type" is declaring variable "a" with a type, not "a:".
4785 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4786 --var_end;
4787 if (is_decl && p == var_start + 2 && p[-1] == ':')
4788 --p;
4789
4790 varlen = p - var_start;
4791 vim_free(name);
4792 name = vim_strnsave(var_start, varlen);
4793 if (name == NULL)
4794 return NULL;
4795 if (!heredoc)
4796 type = &t_any;
4797
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004798 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004799 {
4800 if (*var_start == '&')
4801 {
4802 int cc;
4803 long numval;
4804
4805 dest = dest_option;
4806 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004807 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004808 emsg(_(e_const_option));
4809 goto theend;
4810 }
4811 if (is_decl)
4812 {
4813 semsg(_("E1052: Cannot declare an option: %s"), var_start);
4814 goto theend;
4815 }
4816 p = var_start;
4817 p = find_option_end(&p, &opt_flags);
4818 if (p == NULL)
4819 {
4820 // cannot happen?
4821 emsg(_(e_letunexp));
4822 goto theend;
4823 }
4824 cc = *p;
4825 *p = NUL;
4826 opt_type = get_option_value(var_start + 1, &numval,
4827 NULL, opt_flags);
4828 *p = cc;
4829 if (opt_type == -3)
4830 {
4831 semsg(_(e_unknown_option), var_start);
4832 goto theend;
4833 }
4834 if (opt_type == -2 || opt_type == 0)
4835 type = &t_string;
4836 else
4837 type = &t_number; // both number and boolean option
4838 }
4839 else if (*var_start == '$')
4840 {
4841 dest = dest_env;
4842 type = &t_string;
4843 if (is_decl)
4844 {
4845 semsg(_("E1065: Cannot declare an environment variable: %s"),
4846 name);
4847 goto theend;
4848 }
4849 }
4850 else if (*var_start == '@')
4851 {
4852 if (!valid_yank_reg(var_start[1], TRUE))
4853 {
4854 emsg_invreg(var_start[1]);
4855 goto theend;
4856 }
4857 dest = dest_reg;
4858 type = &t_string;
4859 if (is_decl)
4860 {
4861 semsg(_("E1066: Cannot declare a register: %s"), name);
4862 goto theend;
4863 }
4864 }
4865 else if (STRNCMP(var_start, "g:", 2) == 0)
4866 {
4867 dest = dest_global;
4868 if (is_decl)
4869 {
4870 semsg(_("E1016: Cannot declare a global variable: %s"),
4871 name);
4872 goto theend;
4873 }
4874 }
4875 else if (STRNCMP(var_start, "b:", 2) == 0)
4876 {
4877 dest = dest_buffer;
4878 if (is_decl)
4879 {
4880 semsg(_("E1078: Cannot declare a buffer variable: %s"),
4881 name);
4882 goto theend;
4883 }
4884 }
4885 else if (STRNCMP(var_start, "w:", 2) == 0)
4886 {
4887 dest = dest_window;
4888 if (is_decl)
4889 {
4890 semsg(_("E1079: Cannot declare a window variable: %s"),
4891 name);
4892 goto theend;
4893 }
4894 }
4895 else if (STRNCMP(var_start, "t:", 2) == 0)
4896 {
4897 dest = dest_tab;
4898 if (is_decl)
4899 {
4900 semsg(_("E1080: Cannot declare a tab variable: %s"), name);
4901 goto theend;
4902 }
4903 }
4904 else if (STRNCMP(var_start, "v:", 2) == 0)
4905 {
4906 typval_T *vtv;
4907 int di_flags;
4908
4909 vimvaridx = find_vim_var(name + 2, &di_flags);
4910 if (vimvaridx < 0)
4911 {
4912 semsg(_(e_var_notfound), var_start);
4913 goto theend;
4914 }
4915 // We use the current value of "sandbox" here, is that OK?
4916 if (var_check_ro(di_flags, name, FALSE))
4917 goto theend;
4918 dest = dest_vimvar;
4919 vtv = get_vim_var_tv(vimvaridx);
4920 type = typval2type(vtv);
4921 if (is_decl)
4922 {
4923 semsg(_("E1064: Cannot declare a v: variable: %s"), name);
4924 goto theend;
4925 }
4926 }
4927 else
4928 {
4929 int idx;
4930
4931 for (idx = 0; reserved[idx] != NULL; ++idx)
4932 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004933 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004934 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004935 goto theend;
4936 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004937
4938 lvar = lookup_local(var_start, varlen, cctx);
4939 if (lvar == NULL)
4940 {
4941 CLEAR_FIELD(arg_lvar);
4942 if (lookup_arg(var_start, varlen,
4943 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4944 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004945 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004946 if (is_decl)
4947 {
4948 semsg(_(e_used_as_arg), name);
4949 goto theend;
4950 }
4951 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004952 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004953 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004954 if (lvar != NULL)
4955 {
4956 if (is_decl)
4957 {
4958 semsg(_("E1017: Variable already declared: %s"), name);
4959 goto theend;
4960 }
4961 else if (lvar->lv_const)
4962 {
4963 semsg(_("E1018: Cannot assign to a constant: %s"),
4964 name);
4965 goto theend;
4966 }
4967 }
4968 else if (STRNCMP(var_start, "s:", 2) == 0
4969 || lookup_script(var_start, varlen) == OK
4970 || find_imported(var_start, varlen, cctx) != NULL)
4971 {
4972 dest = dest_script;
4973 if (is_decl)
4974 {
4975 semsg(_("E1054: Variable already declared in the script: %s"),
4976 name);
4977 goto theend;
4978 }
4979 }
4980 else if (name[1] == ':' && name[2] != NUL)
4981 {
4982 semsg(_("E1082: Cannot use a namespaced variable: %s"),
4983 name);
4984 goto theend;
4985 }
4986 else if (!is_decl)
4987 {
4988 semsg(_("E1089: unknown variable: %s"), name);
4989 goto theend;
4990 }
4991 }
4992 }
4993
4994 // handle "a:name" as a name, not index "name" on "a"
4995 if (varlen > 1 || var_start[varlen] != ':')
4996 p = var_end;
4997
4998 if (dest != dest_option)
4999 {
5000 if (is_decl && *p == ':')
5001 {
5002 // parse optional type: "let var: type = expr"
5003 if (!VIM_ISWHITE(p[1]))
5004 {
5005 semsg(_(e_white_after), ":");
5006 goto theend;
5007 }
5008 p = skipwhite(p + 1);
5009 type = parse_type(&p, cctx->ctx_type_list);
5010 has_type = TRUE;
5011 }
5012 else if (lvar != NULL)
5013 type = lvar->lv_type;
5014 }
5015
5016 if (oplen == 3 && !heredoc && dest != dest_global
5017 && type->tt_type != VAR_STRING
5018 && type->tt_type != VAR_ANY)
5019 {
5020 emsg(_("E1019: Can only concatenate to string"));
5021 goto theend;
5022 }
5023
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005024 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005025 {
5026 if (oplen > 1 && !heredoc)
5027 {
5028 // +=, /=, etc. require an existing variable
5029 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5030 name);
5031 goto theend;
5032 }
5033
5034 // new local variable
5035 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5036 goto theend;
5037 lvar = reserve_local(cctx, var_start, varlen,
5038 cmdidx == CMD_const, type);
5039 if (lvar == NULL)
5040 goto theend;
5041 new_local = TRUE;
5042 }
5043
5044 member_type = type;
5045 if (var_end > var_start + varlen)
5046 {
5047 // Something follows after the variable: "var[idx]".
5048 if (is_decl)
5049 {
5050 emsg(_("E1087: cannot use an index when declaring a variable"));
5051 goto theend;
5052 }
5053
5054 if (var_start[varlen] == '[')
5055 {
5056 has_index = TRUE;
5057 if (type->tt_member == NULL)
5058 {
5059 semsg(_("E1088: cannot use an index on %s"), name);
5060 goto theend;
5061 }
5062 member_type = type->tt_member;
5063 }
5064 else
5065 {
5066 semsg("Not supported yet: %s", var_start);
5067 goto theend;
5068 }
5069 }
5070 else if (lvar == &arg_lvar)
5071 {
5072 semsg(_("E1090: Cannot assign to argument %s"), name);
5073 goto theend;
5074 }
5075
5076 if (!heredoc)
5077 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005078 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005079 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005080 if (oplen > 0 && var_count == 0)
5081 {
5082 // skip over the "=" and the expression
5083 p = skipwhite(op + oplen);
5084 compile_expr0(&p, cctx);
5085 }
5086 }
5087 else if (oplen > 0)
5088 {
5089 type_T *stacktype;
5090
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005091 // For "var = expr" evaluate the expression.
5092 if (var_count == 0)
5093 {
5094 int r;
5095
5096 // for "+=", "*=", "..=" etc. first load the current value
5097 if (*op != '=')
5098 {
5099 generate_loadvar(cctx, dest, name, lvar, type);
5100
5101 if (has_index)
5102 {
5103 // TODO: get member from list or dict
5104 emsg("Index with operation not supported yet");
5105 goto theend;
5106 }
5107 }
5108
5109 // Compile the expression. Temporarily hide the new local
5110 // variable here, it is not available to this expression.
5111 if (new_local)
5112 --cctx->ctx_locals.ga_len;
5113 instr_count = instr->ga_len;
5114 p = skipwhite(op + oplen);
5115 r = compile_expr0(&p, cctx);
5116 if (new_local)
5117 ++cctx->ctx_locals.ga_len;
5118 if (r == FAIL)
5119 goto theend;
5120 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005121 else if (semicolon && var_idx == var_count - 1)
5122 {
5123 // For "[var; var] = expr" get the rest of the list
5124 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5125 goto theend;
5126 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005127 else
5128 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005129 // For "[var, var] = expr" get the "var_idx" item from the
5130 // list.
5131 if (generate_GETITEM(cctx, var_idx) == FAIL)
5132 return FAIL;
5133 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005134
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005135 stacktype = stack->ga_len == 0 ? &t_void
5136 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5137 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005138 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005139 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005140 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005141 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005142 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005143 emsg(_(e_cannot_use_void));
5144 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005145 }
5146 else
5147 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005148 // An empty list or dict has a &t_void member,
5149 // for a variable that implies &t_any.
5150 if (stacktype == &t_list_empty)
5151 lvar->lv_type = &t_list_any;
5152 else if (stacktype == &t_dict_empty)
5153 lvar->lv_type = &t_dict_any;
5154 else
5155 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005156 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005157 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005158 else
5159 {
5160 type_T *use_type = lvar->lv_type;
5161
5162 if (has_index)
5163 {
5164 use_type = use_type->tt_member;
5165 if (use_type == NULL)
5166 use_type = &t_void;
5167 }
5168 if (need_type(stacktype, use_type, -1, cctx)
5169 == FAIL)
5170 goto theend;
5171 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005172 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005173 else if (*p != '=' && need_type(stacktype, member_type, -1,
5174 cctx) == FAIL)
5175 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005177 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005179 emsg(_(e_const_req_value));
5180 goto theend;
5181 }
5182 else if (!has_type || dest == dest_option)
5183 {
5184 emsg(_(e_type_req));
5185 goto theend;
5186 }
5187 else
5188 {
5189 // variables are always initialized
5190 if (ga_grow(instr, 1) == FAIL)
5191 goto theend;
5192 switch (member_type->tt_type)
5193 {
5194 case VAR_BOOL:
5195 generate_PUSHBOOL(cctx, VVAL_FALSE);
5196 break;
5197 case VAR_FLOAT:
5198#ifdef FEAT_FLOAT
5199 generate_PUSHF(cctx, 0.0);
5200#endif
5201 break;
5202 case VAR_STRING:
5203 generate_PUSHS(cctx, NULL);
5204 break;
5205 case VAR_BLOB:
5206 generate_PUSHBLOB(cctx, NULL);
5207 break;
5208 case VAR_FUNC:
5209 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5210 break;
5211 case VAR_LIST:
5212 generate_NEWLIST(cctx, 0);
5213 break;
5214 case VAR_DICT:
5215 generate_NEWDICT(cctx, 0);
5216 break;
5217 case VAR_JOB:
5218 generate_PUSHJOB(cctx, NULL);
5219 break;
5220 case VAR_CHANNEL:
5221 generate_PUSHCHANNEL(cctx, NULL);
5222 break;
5223 case VAR_NUMBER:
5224 case VAR_UNKNOWN:
5225 case VAR_ANY:
5226 case VAR_PARTIAL:
5227 case VAR_VOID:
5228 case VAR_SPECIAL: // cannot happen
5229 generate_PUSHNR(cctx, 0);
5230 break;
5231 }
5232 }
5233 if (var_count == 0)
5234 end = p;
5235 }
5236
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005237 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005238 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005239 break;
5240
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005241 if (oplen > 0 && *op != '=')
5242 {
5243 type_T *expected = &t_number;
5244 type_T *stacktype;
5245
5246 // TODO: if type is known use float or any operation
5247
5248 if (*op == '.')
5249 expected = &t_string;
5250 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5251 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5252 goto theend;
5253
5254 if (*op == '.')
5255 generate_instr_drop(cctx, ISN_CONCAT, 1);
5256 else
5257 {
5258 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5259
5260 if (isn == NULL)
5261 goto theend;
5262 switch (*op)
5263 {
5264 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5265 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5266 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5267 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5268 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5269 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005270 }
5271 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005272
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005273 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005274 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005275 int r;
5276
5277 // Compile the "idx" in "var[idx]".
5278 if (new_local)
5279 --cctx->ctx_locals.ga_len;
5280 p = skipwhite(var_start + varlen + 1);
5281 r = compile_expr0(&p, cctx);
5282 if (new_local)
5283 ++cctx->ctx_locals.ga_len;
5284 if (r == FAIL)
5285 goto theend;
5286 if (*skipwhite(p) != ']')
5287 {
5288 emsg(_(e_missbrac));
5289 goto theend;
5290 }
5291 if (type->tt_type == VAR_DICT
5292 && may_generate_2STRING(-1, cctx) == FAIL)
5293 goto theend;
5294 if (type->tt_type == VAR_LIST
5295 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005296 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005297 {
5298 emsg(_(e_number_exp));
5299 goto theend;
5300 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005301
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005302 // Load the dict or list. On the stack we then have:
5303 // - value
5304 // - index
5305 // - variable
5306 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005307
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005308 if (type->tt_type == VAR_LIST)
5309 {
5310 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5311 return FAIL;
5312 }
5313 else if (type->tt_type == VAR_DICT)
5314 {
5315 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5316 return FAIL;
5317 }
5318 else
5319 {
5320 emsg(_(e_listreq));
5321 goto theend;
5322 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005323 }
5324 else
5325 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005326 switch (dest)
5327 {
5328 case dest_option:
5329 generate_STOREOPT(cctx, name + 1, opt_flags);
5330 break;
5331 case dest_global:
5332 // include g: with the name, easier to execute that way
5333 generate_STORE(cctx, ISN_STOREG, 0, name);
5334 break;
5335 case dest_buffer:
5336 // include b: with the name, easier to execute that way
5337 generate_STORE(cctx, ISN_STOREB, 0, name);
5338 break;
5339 case dest_window:
5340 // include w: with the name, easier to execute that way
5341 generate_STORE(cctx, ISN_STOREW, 0, name);
5342 break;
5343 case dest_tab:
5344 // include t: with the name, easier to execute that way
5345 generate_STORE(cctx, ISN_STORET, 0, name);
5346 break;
5347 case dest_env:
5348 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5349 break;
5350 case dest_reg:
5351 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5352 break;
5353 case dest_vimvar:
5354 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5355 break;
5356 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005357 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005358 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5359 imported_T *import = NULL;
5360 int sid = current_sctx.sc_sid;
5361 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005362
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005363 if (name[1] != ':')
5364 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005365 import = find_imported(name, 0, cctx);
5366 if (import != NULL)
5367 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005368 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005369
5370 idx = get_script_item_idx(sid, rawname, TRUE);
5371 // TODO: specific type
5372 if (idx < 0)
5373 {
5374 char_u *name_s = name;
5375
5376 // Include s: in the name for store_var()
5377 if (name[1] != ':')
5378 {
5379 int len = (int)STRLEN(name) + 3;
5380
5381 name_s = alloc(len);
5382 if (name_s == NULL)
5383 name_s = name;
5384 else
5385 vim_snprintf((char *)name_s, len,
5386 "s:%s", name);
5387 }
5388 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005389 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005390 if (name_s != name)
5391 vim_free(name_s);
5392 }
5393 else
5394 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005395 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005396 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005397 break;
5398 case dest_local:
5399 if (lvar != NULL)
5400 {
5401 isn_T *isn = ((isn_T *)instr->ga_data)
5402 + instr->ga_len - 1;
5403
5404 // optimization: turn "var = 123" from ISN_PUSHNR +
5405 // ISN_STORE into ISN_STORENR
5406 if (!lvar->lv_from_outer
5407 && instr->ga_len == instr_count + 1
5408 && isn->isn_type == ISN_PUSHNR)
5409 {
5410 varnumber_T val = isn->isn_arg.number;
5411
5412 isn->isn_type = ISN_STORENR;
5413 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5414 isn->isn_arg.storenr.stnr_val = val;
5415 if (stack->ga_len > 0)
5416 --stack->ga_len;
5417 }
5418 else if (lvar->lv_from_outer)
5419 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005420 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005421 else
5422 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5423 }
5424 break;
5425 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005426 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005427
5428 if (var_idx + 1 < var_count)
5429 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005430 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005431
5432 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005433 if (var_count > 0 && !semicolon)
5434 {
5435 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5436 goto theend;
5437 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005438
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005439 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005440
5441theend:
5442 vim_free(name);
5443 return ret;
5444}
5445
5446/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005447 * Check if "name" can be "unlet".
5448 */
5449 int
5450check_vim9_unlet(char_u *name)
5451{
5452 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5453 {
5454 semsg(_("E1081: Cannot unlet %s"), name);
5455 return FAIL;
5456 }
5457 return OK;
5458}
5459
5460/*
5461 * Callback passed to ex_unletlock().
5462 */
5463 static int
5464compile_unlet(
5465 lval_T *lvp,
5466 char_u *name_end,
5467 exarg_T *eap,
5468 int deep UNUSED,
5469 void *coookie)
5470{
5471 cctx_T *cctx = coookie;
5472
5473 if (lvp->ll_tv == NULL)
5474 {
5475 char_u *p = lvp->ll_name;
5476 int cc = *name_end;
5477 int ret = OK;
5478
5479 // Normal name. Only supports g:, w:, t: and b: namespaces.
5480 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005481 if (*p == '$')
5482 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5483 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005484 ret = FAIL;
5485 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005486 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005487
5488 *name_end = cc;
5489 return ret;
5490 }
5491
5492 // TODO: unlet {list}[idx]
5493 // TODO: unlet {dict}[key]
5494 emsg("Sorry, :unlet not fully implemented yet");
5495 return FAIL;
5496}
5497
5498/*
5499 * compile "unlet var", "lock var" and "unlock var"
5500 * "arg" points to "var".
5501 */
5502 static char_u *
5503compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5504{
5505 char_u *p = arg;
5506
5507 if (eap->cmdidx != CMD_unlet)
5508 {
5509 emsg("Sorry, :lock and unlock not implemented yet");
5510 return NULL;
5511 }
5512
5513 if (*p == '!')
5514 {
5515 p = skipwhite(p + 1);
5516 eap->forceit = TRUE;
5517 }
5518
5519 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5520 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5521}
5522
5523/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005524 * Compile an :import command.
5525 */
5526 static char_u *
5527compile_import(char_u *arg, cctx_T *cctx)
5528{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01005529 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005530}
5531
5532/*
5533 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5534 */
5535 static int
5536compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5537{
5538 garray_T *instr = &cctx->ctx_instr;
5539 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5540
5541 if (endlabel == NULL)
5542 return FAIL;
5543 endlabel->el_next = *el;
5544 *el = endlabel;
5545 endlabel->el_end_label = instr->ga_len;
5546
5547 generate_JUMP(cctx, when, 0);
5548 return OK;
5549}
5550
5551 static void
5552compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5553{
5554 garray_T *instr = &cctx->ctx_instr;
5555
5556 while (*el != NULL)
5557 {
5558 endlabel_T *cur = (*el);
5559 isn_T *isn;
5560
5561 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5562 isn->isn_arg.jump.jump_where = instr->ga_len;
5563 *el = cur->el_next;
5564 vim_free(cur);
5565 }
5566}
5567
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005568 static void
5569compile_free_jump_to_end(endlabel_T **el)
5570{
5571 while (*el != NULL)
5572 {
5573 endlabel_T *cur = (*el);
5574
5575 *el = cur->el_next;
5576 vim_free(cur);
5577 }
5578}
5579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005580/*
5581 * Create a new scope and set up the generic items.
5582 */
5583 static scope_T *
5584new_scope(cctx_T *cctx, scopetype_T type)
5585{
5586 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5587
5588 if (scope == NULL)
5589 return NULL;
5590 scope->se_outer = cctx->ctx_scope;
5591 cctx->ctx_scope = scope;
5592 scope->se_type = type;
5593 scope->se_local_count = cctx->ctx_locals.ga_len;
5594 return scope;
5595}
5596
5597/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005598 * Free the current scope and go back to the outer scope.
5599 */
5600 static void
5601drop_scope(cctx_T *cctx)
5602{
5603 scope_T *scope = cctx->ctx_scope;
5604
5605 if (scope == NULL)
5606 {
5607 iemsg("calling drop_scope() without a scope");
5608 return;
5609 }
5610 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005611 switch (scope->se_type)
5612 {
5613 case IF_SCOPE:
5614 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5615 case FOR_SCOPE:
5616 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5617 case WHILE_SCOPE:
5618 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5619 case TRY_SCOPE:
5620 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5621 case NO_SCOPE:
5622 case BLOCK_SCOPE:
5623 break;
5624 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005625 vim_free(scope);
5626}
5627
5628/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005629 * compile "if expr"
5630 *
5631 * "if expr" Produces instructions:
5632 * EVAL expr Push result of "expr"
5633 * JUMP_IF_FALSE end
5634 * ... body ...
5635 * end:
5636 *
5637 * "if expr | else" Produces instructions:
5638 * EVAL expr Push result of "expr"
5639 * JUMP_IF_FALSE else
5640 * ... body ...
5641 * JUMP_ALWAYS end
5642 * else:
5643 * ... body ...
5644 * end:
5645 *
5646 * "if expr1 | elseif expr2 | else" Produces instructions:
5647 * EVAL expr Push result of "expr"
5648 * JUMP_IF_FALSE elseif
5649 * ... body ...
5650 * JUMP_ALWAYS end
5651 * elseif:
5652 * EVAL expr Push result of "expr"
5653 * JUMP_IF_FALSE else
5654 * ... body ...
5655 * JUMP_ALWAYS end
5656 * else:
5657 * ... body ...
5658 * end:
5659 */
5660 static char_u *
5661compile_if(char_u *arg, cctx_T *cctx)
5662{
5663 char_u *p = arg;
5664 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005665 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005666 scope_T *scope;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005667 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005668
Bram Moolenaara5565e42020-05-09 15:44:01 +02005669 CLEAR_FIELD(ppconst);
5670 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005671 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005672 clear_ppconst(&ppconst);
5673 return NULL;
5674 }
5675 if (instr->ga_len == instr_count && ppconst.pp_used == 1)
5676 {
5677 // The expression results in a constant.
5678 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005679 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005680 clear_ppconst(&ppconst);
5681 }
5682 else
5683 {
5684 // Not a constant, generate instructions for the expression.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005685 cctx->ctx_skip = SKIP_UNKNONW;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005686 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005687 return NULL;
5688 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005689
5690 scope = new_scope(cctx, IF_SCOPE);
5691 if (scope == NULL)
5692 return NULL;
5693
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005694 if (cctx->ctx_skip == SKIP_UNKNONW)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005695 {
5696 // "where" is set when ":elseif", "else" or ":endif" is found
5697 scope->se_u.se_if.is_if_label = instr->ga_len;
5698 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5699 }
5700 else
5701 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005702
5703 return p;
5704}
5705
5706 static char_u *
5707compile_elseif(char_u *arg, cctx_T *cctx)
5708{
5709 char_u *p = arg;
5710 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005711 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005712 isn_T *isn;
5713 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005714 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005715
5716 if (scope == NULL || scope->se_type != IF_SCOPE)
5717 {
5718 emsg(_(e_elseif_without_if));
5719 return NULL;
5720 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005721 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005722
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005723 if (cctx->ctx_skip == SKIP_UNKNONW)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005724 {
5725 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005726 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005727 return NULL;
5728 // previous "if" or "elseif" jumps here
5729 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5730 isn->isn_arg.jump.jump_where = instr->ga_len;
5731 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005732
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005733 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005734 CLEAR_FIELD(ppconst);
5735 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005736 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005737 clear_ppconst(&ppconst);
5738 return NULL;
5739 }
5740 if (instr->ga_len == instr_count && ppconst.pp_used == 1)
5741 {
5742 // The expression results in a constant.
5743 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005744 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005745 clear_ppconst(&ppconst);
5746 scope->se_u.se_if.is_if_label = -1;
5747 }
5748 else
5749 {
5750 // Not a constant, generate instructions for the expression.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005751 cctx->ctx_skip = SKIP_UNKNONW;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005752 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005753 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005754
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005755 // "where" is set when ":elseif", "else" or ":endif" is found
5756 scope->se_u.se_if.is_if_label = instr->ga_len;
5757 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5758 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005759
5760 return p;
5761}
5762
5763 static char_u *
5764compile_else(char_u *arg, cctx_T *cctx)
5765{
5766 char_u *p = arg;
5767 garray_T *instr = &cctx->ctx_instr;
5768 isn_T *isn;
5769 scope_T *scope = cctx->ctx_scope;
5770
5771 if (scope == NULL || scope->se_type != IF_SCOPE)
5772 {
5773 emsg(_(e_else_without_if));
5774 return NULL;
5775 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005776 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005777
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005778 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005779 if (cctx->ctx_skip == SKIP_UNKNONW)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005780 {
5781 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005782 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005783 return NULL;
5784 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005785
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005786 if (cctx->ctx_skip == SKIP_UNKNONW)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005787 {
5788 if (scope->se_u.se_if.is_if_label >= 0)
5789 {
5790 // previous "if" or "elseif" jumps here
5791 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5792 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaar158906c2020-02-06 20:39:45 +01005793 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005794 }
5795 }
5796
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005797 if (cctx->ctx_skip != SKIP_UNKNONW)
5798 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005799
5800 return p;
5801}
5802
5803 static char_u *
5804compile_endif(char_u *arg, cctx_T *cctx)
5805{
5806 scope_T *scope = cctx->ctx_scope;
5807 ifscope_T *ifscope;
5808 garray_T *instr = &cctx->ctx_instr;
5809 isn_T *isn;
5810
5811 if (scope == NULL || scope->se_type != IF_SCOPE)
5812 {
5813 emsg(_(e_endif_without_if));
5814 return NULL;
5815 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005816 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005817 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005818
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005819 if (scope->se_u.se_if.is_if_label >= 0)
5820 {
5821 // previous "if" or "elseif" jumps here
5822 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5823 isn->isn_arg.jump.jump_where = instr->ga_len;
5824 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005825 // Fill in the "end" label in jumps at the end of the blocks.
5826 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005827 // TODO: this should restore the value from before the :if
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005828 cctx->ctx_skip = SKIP_UNKNONW;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005829
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005830 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005831 return arg;
5832}
5833
5834/*
5835 * compile "for var in expr"
5836 *
5837 * Produces instructions:
5838 * PUSHNR -1
5839 * STORE loop-idx Set index to -1
5840 * EVAL expr Push result of "expr"
5841 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5842 * - if beyond end, jump to "end"
5843 * - otherwise get item from list and push it
5844 * STORE var Store item in "var"
5845 * ... body ...
5846 * JUMP top Jump back to repeat
5847 * end: DROP Drop the result of "expr"
5848 *
5849 */
5850 static char_u *
5851compile_for(char_u *arg, cctx_T *cctx)
5852{
5853 char_u *p;
5854 size_t varlen;
5855 garray_T *instr = &cctx->ctx_instr;
5856 garray_T *stack = &cctx->ctx_type_stack;
5857 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005858 lvar_T *loop_lvar; // loop iteration variable
5859 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005860 type_T *vartype;
5861
5862 // TODO: list of variables: "for [key, value] in dict"
5863 // parse "var"
5864 for (p = arg; eval_isnamec1(*p); ++p)
5865 ;
5866 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005867 var_lvar = lookup_local(arg, varlen, cctx);
5868 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005869 {
5870 semsg(_("E1023: variable already defined: %s"), arg);
5871 return NULL;
5872 }
5873
5874 // consume "in"
5875 p = skipwhite(p);
5876 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5877 {
5878 emsg(_(e_missing_in));
5879 return NULL;
5880 }
5881 p = skipwhite(p + 2);
5882
5883
5884 scope = new_scope(cctx, FOR_SCOPE);
5885 if (scope == NULL)
5886 return NULL;
5887
5888 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005889 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5890 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005891 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005892 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005893 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005894 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005895 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005896
5897 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005898 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5899 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005900 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005901 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005902 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005903 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005904 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005905
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005906 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005907
5908 // compile "expr", it remains on the stack until "endfor"
5909 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005910 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005911 {
5912 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005913 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005914 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005915
5916 // now we know the type of "var"
5917 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5918 if (vartype->tt_type != VAR_LIST)
5919 {
5920 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005921 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005922 return NULL;
5923 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02005924 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005925 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005926
5927 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005928 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005929
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005930 generate_FOR(cctx, loop_lvar->lv_idx);
5931 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005932
5933 return arg;
5934}
5935
5936/*
5937 * compile "endfor"
5938 */
5939 static char_u *
5940compile_endfor(char_u *arg, cctx_T *cctx)
5941{
5942 garray_T *instr = &cctx->ctx_instr;
5943 scope_T *scope = cctx->ctx_scope;
5944 forscope_T *forscope;
5945 isn_T *isn;
5946
5947 if (scope == NULL || scope->se_type != FOR_SCOPE)
5948 {
5949 emsg(_(e_for));
5950 return NULL;
5951 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005952 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005953 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005954 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005955
5956 // At end of ":for" scope jump back to the FOR instruction.
5957 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5958
5959 // Fill in the "end" label in the FOR statement so it can jump here
5960 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5961 isn->isn_arg.forloop.for_end = instr->ga_len;
5962
5963 // Fill in the "end" label any BREAK statements
5964 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5965
5966 // Below the ":for" scope drop the "expr" list from the stack.
5967 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5968 return NULL;
5969
5970 vim_free(scope);
5971
5972 return arg;
5973}
5974
5975/*
5976 * compile "while expr"
5977 *
5978 * Produces instructions:
5979 * top: EVAL expr Push result of "expr"
5980 * JUMP_IF_FALSE end jump if false
5981 * ... body ...
5982 * JUMP top Jump back to repeat
5983 * end:
5984 *
5985 */
5986 static char_u *
5987compile_while(char_u *arg, cctx_T *cctx)
5988{
5989 char_u *p = arg;
5990 garray_T *instr = &cctx->ctx_instr;
5991 scope_T *scope;
5992
5993 scope = new_scope(cctx, WHILE_SCOPE);
5994 if (scope == NULL)
5995 return NULL;
5996
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005997 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005998
5999 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006000 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006001 return NULL;
6002
6003 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006004 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006005 JUMP_IF_FALSE, cctx) == FAIL)
6006 return FAIL;
6007
6008 return p;
6009}
6010
6011/*
6012 * compile "endwhile"
6013 */
6014 static char_u *
6015compile_endwhile(char_u *arg, cctx_T *cctx)
6016{
6017 scope_T *scope = cctx->ctx_scope;
6018
6019 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6020 {
6021 emsg(_(e_while));
6022 return NULL;
6023 }
6024 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006025 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026
6027 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006028 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006029
6030 // Fill in the "end" label in the WHILE statement so it can jump here.
6031 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006032 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006033
6034 vim_free(scope);
6035
6036 return arg;
6037}
6038
6039/*
6040 * compile "continue"
6041 */
6042 static char_u *
6043compile_continue(char_u *arg, cctx_T *cctx)
6044{
6045 scope_T *scope = cctx->ctx_scope;
6046
6047 for (;;)
6048 {
6049 if (scope == NULL)
6050 {
6051 emsg(_(e_continue));
6052 return NULL;
6053 }
6054 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6055 break;
6056 scope = scope->se_outer;
6057 }
6058
6059 // Jump back to the FOR or WHILE instruction.
6060 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006061 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6062 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006063 return arg;
6064}
6065
6066/*
6067 * compile "break"
6068 */
6069 static char_u *
6070compile_break(char_u *arg, cctx_T *cctx)
6071{
6072 scope_T *scope = cctx->ctx_scope;
6073 endlabel_T **el;
6074
6075 for (;;)
6076 {
6077 if (scope == NULL)
6078 {
6079 emsg(_(e_break));
6080 return NULL;
6081 }
6082 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6083 break;
6084 scope = scope->se_outer;
6085 }
6086
6087 // Jump to the end of the FOR or WHILE loop.
6088 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006089 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006090 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006091 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006092 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6093 return FAIL;
6094
6095 return arg;
6096}
6097
6098/*
6099 * compile "{" start of block
6100 */
6101 static char_u *
6102compile_block(char_u *arg, cctx_T *cctx)
6103{
6104 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6105 return NULL;
6106 return skipwhite(arg + 1);
6107}
6108
6109/*
6110 * compile end of block: drop one scope
6111 */
6112 static void
6113compile_endblock(cctx_T *cctx)
6114{
6115 scope_T *scope = cctx->ctx_scope;
6116
6117 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006118 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006119 vim_free(scope);
6120}
6121
6122/*
6123 * compile "try"
6124 * Creates a new scope for the try-endtry, pointing to the first catch and
6125 * finally.
6126 * Creates another scope for the "try" block itself.
6127 * TRY instruction sets up exception handling at runtime.
6128 *
6129 * "try"
6130 * TRY -> catch1, -> finally push trystack entry
6131 * ... try block
6132 * "throw {exception}"
6133 * EVAL {exception}
6134 * THROW create exception
6135 * ... try block
6136 * " catch {expr}"
6137 * JUMP -> finally
6138 * catch1: PUSH exeception
6139 * EVAL {expr}
6140 * MATCH
6141 * JUMP nomatch -> catch2
6142 * CATCH remove exception
6143 * ... catch block
6144 * " catch"
6145 * JUMP -> finally
6146 * catch2: CATCH remove exception
6147 * ... catch block
6148 * " finally"
6149 * finally:
6150 * ... finally block
6151 * " endtry"
6152 * ENDTRY pop trystack entry, may rethrow
6153 */
6154 static char_u *
6155compile_try(char_u *arg, cctx_T *cctx)
6156{
6157 garray_T *instr = &cctx->ctx_instr;
6158 scope_T *try_scope;
6159 scope_T *scope;
6160
6161 // scope that holds the jumps that go to catch/finally/endtry
6162 try_scope = new_scope(cctx, TRY_SCOPE);
6163 if (try_scope == NULL)
6164 return NULL;
6165
6166 // "catch" is set when the first ":catch" is found.
6167 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006168 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006169 if (generate_instr(cctx, ISN_TRY) == NULL)
6170 return NULL;
6171
6172 // scope for the try block itself
6173 scope = new_scope(cctx, BLOCK_SCOPE);
6174 if (scope == NULL)
6175 return NULL;
6176
6177 return arg;
6178}
6179
6180/*
6181 * compile "catch {expr}"
6182 */
6183 static char_u *
6184compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6185{
6186 scope_T *scope = cctx->ctx_scope;
6187 garray_T *instr = &cctx->ctx_instr;
6188 char_u *p;
6189 isn_T *isn;
6190
6191 // end block scope from :try or :catch
6192 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6193 compile_endblock(cctx);
6194 scope = cctx->ctx_scope;
6195
6196 // Error if not in a :try scope
6197 if (scope == NULL || scope->se_type != TRY_SCOPE)
6198 {
6199 emsg(_(e_catch));
6200 return NULL;
6201 }
6202
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006203 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006204 {
6205 emsg(_("E1033: catch unreachable after catch-all"));
6206 return NULL;
6207 }
6208
6209 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006210 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006211 JUMP_ALWAYS, cctx) == FAIL)
6212 return NULL;
6213
6214 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006215 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006216 if (isn->isn_arg.try.try_catch == 0)
6217 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006218 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006219 {
6220 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006221 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 isn->isn_arg.jump.jump_where = instr->ga_len;
6223 }
6224
6225 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006226 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006227 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006228 scope->se_u.se_try.ts_caught_all = TRUE;
6229 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006230 }
6231 else
6232 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006233 char_u *end;
6234 char_u *pat;
6235 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006236 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006237 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006239 // Push v:exception, push {expr} and MATCH
6240 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6241
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006242 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006243 if (*end != *p)
6244 {
6245 semsg(_("E1067: Separator mismatch: %s"), p);
6246 vim_free(tofree);
6247 return FAIL;
6248 }
6249 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006250 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006251 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006252 len = (int)(end - tofree);
6253 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006254 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006255 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006256 if (pat == NULL)
6257 return FAIL;
6258 if (generate_PUSHS(cctx, pat) == FAIL)
6259 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006260
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006261 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6262 return NULL;
6263
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006264 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006265 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6266 return NULL;
6267 }
6268
6269 if (generate_instr(cctx, ISN_CATCH) == NULL)
6270 return NULL;
6271
6272 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6273 return NULL;
6274 return p;
6275}
6276
6277 static char_u *
6278compile_finally(char_u *arg, cctx_T *cctx)
6279{
6280 scope_T *scope = cctx->ctx_scope;
6281 garray_T *instr = &cctx->ctx_instr;
6282 isn_T *isn;
6283
6284 // end block scope from :try or :catch
6285 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6286 compile_endblock(cctx);
6287 scope = cctx->ctx_scope;
6288
6289 // Error if not in a :try scope
6290 if (scope == NULL || scope->se_type != TRY_SCOPE)
6291 {
6292 emsg(_(e_finally));
6293 return NULL;
6294 }
6295
6296 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006297 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006298 if (isn->isn_arg.try.try_finally != 0)
6299 {
6300 emsg(_(e_finally_dup));
6301 return NULL;
6302 }
6303
6304 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006305 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006306
Bram Moolenaar585fea72020-04-02 22:33:21 +02006307 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006308 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006309 {
6310 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006311 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006312 isn->isn_arg.jump.jump_where = instr->ga_len;
6313 }
6314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315 // TODO: set index in ts_finally_label jumps
6316
6317 return arg;
6318}
6319
6320 static char_u *
6321compile_endtry(char_u *arg, cctx_T *cctx)
6322{
6323 scope_T *scope = cctx->ctx_scope;
6324 garray_T *instr = &cctx->ctx_instr;
6325 isn_T *isn;
6326
6327 // end block scope from :catch or :finally
6328 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6329 compile_endblock(cctx);
6330 scope = cctx->ctx_scope;
6331
6332 // Error if not in a :try scope
6333 if (scope == NULL || scope->se_type != TRY_SCOPE)
6334 {
6335 if (scope == NULL)
6336 emsg(_(e_no_endtry));
6337 else if (scope->se_type == WHILE_SCOPE)
6338 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006339 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006340 emsg(_(e_endfor));
6341 else
6342 emsg(_(e_endif));
6343 return NULL;
6344 }
6345
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006346 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006347 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6348 {
6349 emsg(_("E1032: missing :catch or :finally"));
6350 return NULL;
6351 }
6352
6353 // Fill in the "end" label in jumps at the end of the blocks, if not done
6354 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006355 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006356
6357 // End :catch or :finally scope: set value in ISN_TRY instruction
6358 if (isn->isn_arg.try.try_finally == 0)
6359 isn->isn_arg.try.try_finally = instr->ga_len;
6360 compile_endblock(cctx);
6361
6362 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6363 return NULL;
6364 return arg;
6365}
6366
6367/*
6368 * compile "throw {expr}"
6369 */
6370 static char_u *
6371compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6372{
6373 char_u *p = skipwhite(arg);
6374
Bram Moolenaara5565e42020-05-09 15:44:01 +02006375 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006376 return NULL;
6377 if (may_generate_2STRING(-1, cctx) == FAIL)
6378 return NULL;
6379 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6380 return NULL;
6381
6382 return p;
6383}
6384
6385/*
6386 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006387 * compile "echomsg expr"
6388 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006389 * compile "execute expr"
6390 */
6391 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006392compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006393{
6394 char_u *p = arg;
6395 int count = 0;
6396
6397 for (;;)
6398 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006399 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006400 return NULL;
6401 ++count;
6402 p = skipwhite(p);
6403 if (ends_excmd(*p))
6404 break;
6405 }
6406
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006407 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6408 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6409 else if (cmdidx == CMD_execute)
6410 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6411 else if (cmdidx == CMD_echomsg)
6412 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6413 else
6414 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006415 return p;
6416}
6417
6418/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006419 * A command that is not compiled, execute with legacy code.
6420 */
6421 static char_u *
6422compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6423{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006424 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006425 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006426
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006427 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006428 goto theend;
6429
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006430 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
6431 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006432 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6433 {
6434 // expand filename in "syntax include [@group] filename"
6435 has_expr = TRUE;
6436 eap->arg = skipwhite(eap->arg + 7);
6437 if (*eap->arg == '@')
6438 eap->arg = skiptowhite(eap->arg);
6439 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006440
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006441 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006442 {
6443 int count = 0;
6444 char_u *start = skipwhite(line);
6445
6446 // :cmd xxx`=expr1`yyy`=expr2`zzz
6447 // PUSHS ":cmd xxx"
6448 // eval expr1
6449 // PUSHS "yyy"
6450 // eval expr2
6451 // PUSHS "zzz"
6452 // EXECCONCAT 5
6453 for (;;)
6454 {
6455 if (p > start)
6456 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006457 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006458 ++count;
6459 }
6460 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006461 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006462 return NULL;
6463 may_generate_2STRING(-1, cctx);
6464 ++count;
6465 p = skipwhite(p);
6466 if (*p != '`')
6467 {
6468 emsg(_("E1083: missing backtick"));
6469 return NULL;
6470 }
6471 start = p + 1;
6472
6473 p = (char_u *)strstr((char *)start, "`=");
6474 if (p == NULL)
6475 {
6476 if (*skipwhite(start) != NUL)
6477 {
6478 generate_PUSHS(cctx, vim_strsave(start));
6479 ++count;
6480 }
6481 break;
6482 }
6483 }
6484 generate_EXECCONCAT(cctx, count);
6485 }
6486 else
6487 generate_EXEC(cctx, line);
6488
6489theend:
6490 return (char_u *)"";
6491}
6492
6493/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006494 * Add a function to the list of :def functions.
6495 * This "sets ufunc->uf_dfunc_idx" but the function isn't compiled yet.
6496 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006497 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006498add_def_function(ufunc_T *ufunc)
6499{
6500 dfunc_T *dfunc;
6501
6502 // Add the function to "def_functions".
6503 if (ga_grow(&def_functions, 1) == FAIL)
6504 return FAIL;
6505 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6506 CLEAR_POINTER(dfunc);
6507 dfunc->df_idx = def_functions.ga_len;
6508 ufunc->uf_dfunc_idx = dfunc->df_idx;
6509 dfunc->df_ufunc = ufunc;
6510 ++def_functions.ga_len;
6511 return OK;
6512}
6513
6514/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006515 * After ex_function() has collected all the function lines: parse and compile
6516 * the lines into instructions.
6517 * Adds the function to "def_functions".
6518 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6519 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006520 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006521 * This can be used recursively through compile_lambda(), which may reallocate
6522 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006523 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006524 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006525 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006526compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006527{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006528 char_u *line = NULL;
6529 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006530 char *errormsg = NULL; // error message
6531 int had_return = FALSE;
6532 cctx_T cctx;
6533 garray_T *instr;
6534 int called_emsg_before = called_emsg;
6535 int ret = FAIL;
6536 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006537 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006538 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006539
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006540 // When using a function that was compiled before: Free old instructions.
6541 // Otherwise add a new entry in "def_functions".
Bram Moolenaar09689a02020-05-09 22:50:08 +02006542 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006543 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006544 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6545 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006546 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006548 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006549 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006550
Bram Moolenaara80faa82020-04-12 19:37:17 +02006551 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006552 cctx.ctx_ufunc = ufunc;
6553 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006554 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006555 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6556 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6557 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6558 cctx.ctx_type_list = &ufunc->uf_type_list;
6559 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6560 instr = &cctx.ctx_instr;
6561
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006562 // Set the context to the function, it may be compiled when called from
6563 // another script. Set the script version to the most modern one.
6564 // The line number will be set in next_line_from_context().
6565 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6567
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006568 // Make sure error messages are OK.
6569 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6570 if (do_estack_push)
6571 estack_push_ufunc(ufunc, 1);
6572
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006573 if (ufunc->uf_def_args.ga_len > 0)
6574 {
6575 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006576 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006577 int i;
6578 char_u *arg;
6579 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6580
6581 // Produce instructions for the default values of optional arguments.
6582 // Store the instruction index in uf_def_arg_idx[] so that we know
6583 // where to start when the function is called, depending on the number
6584 // of arguments.
6585 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6586 if (ufunc->uf_def_arg_idx == NULL)
6587 goto erret;
6588 for (i = 0; i < count; ++i)
6589 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006590 garray_T *stack = &cctx.ctx_type_stack;
6591 type_T *val_type;
6592 int arg_idx = first_def_arg + i;
6593
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006594 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6595 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006596 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006597 goto erret;
6598
6599 // If no type specified use the type of the default value.
6600 // Otherwise check that the default value type matches the
6601 // specified type.
6602 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6603 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6604 ufunc->uf_arg_types[arg_idx] = val_type;
6605 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6606 == FAIL)
6607 {
6608 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6609 arg_idx + 1);
6610 goto erret;
6611 }
6612
6613 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006614 goto erret;
6615 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006616 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6617 }
6618
6619 /*
6620 * Loop over all the lines of the function and generate instructions.
6621 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006622 for (;;)
6623 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006624 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006625 int starts_with_colon = FALSE;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006626
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006627 // Bail out on the first error to avoid a flood of errors and report
6628 // the right line number when inside try/catch.
6629 if (emsg_before != called_emsg)
6630 goto erret;
6631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006632 if (line != NULL && *line == '|')
6633 // the line continues after a '|'
6634 ++line;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006635 else if (line != NULL && *line != NUL
6636 && !(*line == '#' && (line == cctx.ctx_line_start
6637 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006638 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006639 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006640 goto erret;
6641 }
6642 else
6643 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006644 line = next_line_from_context(&cctx);
6645 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006646 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006647 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006648 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006649 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006650
6651 had_return = FALSE;
Bram Moolenaara80faa82020-04-12 19:37:17 +02006652 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006653 ea.cmdlinep = &line;
6654 ea.cmd = skipwhite(line);
6655
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006656 // Some things can be recognized by the first character.
6657 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006658 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006659 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006660 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006661 if (ea.cmd[1] != '{')
6662 {
6663 line = (char_u *)"";
6664 continue;
6665 }
6666 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006667
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006668 case '}':
6669 {
6670 // "}" ends a block scope
6671 scopetype_T stype = cctx.ctx_scope == NULL
6672 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006673
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006674 if (stype == BLOCK_SCOPE)
6675 {
6676 compile_endblock(&cctx);
6677 line = ea.cmd;
6678 }
6679 else
6680 {
6681 emsg(_("E1025: using } outside of a block scope"));
6682 goto erret;
6683 }
6684 if (line != NULL)
6685 line = skipwhite(ea.cmd + 1);
6686 continue;
6687 }
6688
6689 case '{':
6690 // "{" starts a block scope
6691 // "{'a': 1}->func() is something else
6692 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6693 {
6694 line = compile_block(ea.cmd, &cctx);
6695 continue;
6696 }
6697 break;
6698
6699 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006700 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006701 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006702 }
6703
6704 /*
6705 * COMMAND MODIFIERS
6706 */
6707 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6708 {
6709 if (errormsg != NULL)
6710 goto erret;
6711 // empty line or comment
6712 line = (char_u *)"";
6713 continue;
6714 }
6715
6716 // Skip ":call" to get to the function name.
6717 if (checkforcmd(&ea.cmd, "call", 3))
6718 ea.cmd = skipwhite(ea.cmd);
6719
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006720 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006721 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006722 char_u *pskip;
6723
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006724 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006725 // find what follows.
6726 // Skip over "var.member", "var[idx]" and the like.
6727 // Also "&opt = val", "$ENV = val" and "@r = val".
6728 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006729 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006730 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006731 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006732 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006733 char_u *var_end;
6734 int oplen;
6735 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006736
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006737 var_end = find_name_end(pskip, NULL, NULL,
6738 FNE_CHECK_START | FNE_INCL_BR);
6739 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006740 if (oplen > 0)
6741 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006742 size_t len = p - ea.cmd;
6743
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006744 // Recognize an assignment if we recognize the variable
6745 // name:
6746 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006747 // "local = expr" where "local" is a local var.
6748 // "script = expr" where "script" is a script-local var.
6749 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006750 // "&opt = expr"
6751 // "$ENV = expr"
6752 // "@r = expr"
6753 if (*ea.cmd == '&'
6754 || *ea.cmd == '$'
6755 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006756 || ((len) > 2 && ea.cmd[1] == ':')
6757 || lookup_local(ea.cmd, len, &cctx) != NULL
6758 || lookup_arg(ea.cmd, len, NULL, NULL,
6759 NULL, &cctx) == OK
6760 || lookup_script(ea.cmd, len) == OK
6761 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006762 {
6763 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006764 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006765 goto erret;
6766 continue;
6767 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006768 }
6769 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006770
6771 if (*ea.cmd == '[')
6772 {
6773 // [var, var] = expr
6774 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6775 if (line == NULL)
6776 goto erret;
6777 if (line != ea.cmd)
6778 continue;
6779 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006780 }
6781
6782 /*
6783 * COMMAND after range
6784 */
6785 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006786 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006787 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006788 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006789
6790 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6791 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006792 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006793 {
6794 line += STRLEN(line);
6795 continue;
6796 }
6797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006798 // Expression or function call.
6799 if (ea.cmdidx == CMD_eval)
6800 {
6801 p = ea.cmd;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006802 if (compile_expr0(&p, &cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006803 goto erret;
6804
6805 // drop the return value
6806 generate_instr_drop(&cctx, ISN_DROP, 1);
6807 line = p;
6808 continue;
6809 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006810 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006811 iemsg("Command from find_ex_command() not handled");
6812 goto erret;
6813 }
6814
6815 p = skipwhite(p);
6816
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006817 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006818 && ea.cmdidx != CMD_elseif
6819 && ea.cmdidx != CMD_else
6820 && ea.cmdidx != CMD_endif)
6821 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006822 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006823 continue;
6824 }
6825
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006826 switch (ea.cmdidx)
6827 {
6828 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006829 ea.arg = p;
6830 line = compile_nested_function(&ea, &cctx);
6831 break;
6832
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006833 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006834 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006835 goto erret;
6836
6837 case CMD_return:
6838 line = compile_return(p, set_return_type, &cctx);
6839 had_return = TRUE;
6840 break;
6841
6842 case CMD_let:
6843 case CMD_const:
6844 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006845 if (line == p)
6846 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006847 break;
6848
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006849 case CMD_unlet:
6850 case CMD_unlockvar:
6851 case CMD_lockvar:
6852 line = compile_unletlock(p, &ea, &cctx);
6853 break;
6854
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006855 case CMD_import:
6856 line = compile_import(p, &cctx);
6857 break;
6858
6859 case CMD_if:
6860 line = compile_if(p, &cctx);
6861 break;
6862 case CMD_elseif:
6863 line = compile_elseif(p, &cctx);
6864 break;
6865 case CMD_else:
6866 line = compile_else(p, &cctx);
6867 break;
6868 case CMD_endif:
6869 line = compile_endif(p, &cctx);
6870 break;
6871
6872 case CMD_while:
6873 line = compile_while(p, &cctx);
6874 break;
6875 case CMD_endwhile:
6876 line = compile_endwhile(p, &cctx);
6877 break;
6878
6879 case CMD_for:
6880 line = compile_for(p, &cctx);
6881 break;
6882 case CMD_endfor:
6883 line = compile_endfor(p, &cctx);
6884 break;
6885 case CMD_continue:
6886 line = compile_continue(p, &cctx);
6887 break;
6888 case CMD_break:
6889 line = compile_break(p, &cctx);
6890 break;
6891
6892 case CMD_try:
6893 line = compile_try(p, &cctx);
6894 break;
6895 case CMD_catch:
6896 line = compile_catch(p, &cctx);
6897 break;
6898 case CMD_finally:
6899 line = compile_finally(p, &cctx);
6900 break;
6901 case CMD_endtry:
6902 line = compile_endtry(p, &cctx);
6903 break;
6904 case CMD_throw:
6905 line = compile_throw(p, &cctx);
6906 break;
6907
6908 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006909 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006910 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006911 case CMD_echomsg:
6912 case CMD_echoerr:
6913 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006914 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006915
6916 default:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006917 // TODO: other commands with an expression argument
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006918 // Not recognized, execute with do_cmdline_cmd().
6919 ea.arg = p;
6920 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006921 break;
6922 }
6923 if (line == NULL)
6924 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006925 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006926
6927 if (cctx.ctx_type_stack.ga_len < 0)
6928 {
6929 iemsg("Type stack underflow");
6930 goto erret;
6931 }
6932 }
6933
6934 if (cctx.ctx_scope != NULL)
6935 {
6936 if (cctx.ctx_scope->se_type == IF_SCOPE)
6937 emsg(_(e_endif));
6938 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6939 emsg(_(e_endwhile));
6940 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6941 emsg(_(e_endfor));
6942 else
6943 emsg(_("E1026: Missing }"));
6944 goto erret;
6945 }
6946
6947 if (!had_return)
6948 {
6949 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6950 {
6951 emsg(_("E1027: Missing return statement"));
6952 goto erret;
6953 }
6954
6955 // Return zero if there is no return at the end.
6956 generate_PUSHNR(&cctx, 0);
6957 generate_instr(&cctx, ISN_RETURN);
6958 }
6959
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006960 {
6961 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6962 + ufunc->uf_dfunc_idx;
6963 dfunc->df_deleted = FALSE;
6964 dfunc->df_instr = instr->ga_data;
6965 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006966 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006967 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006968 if (cctx.ctx_outer_used)
6969 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006970 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971
6972 ret = OK;
6973
6974erret:
6975 if (ret == FAIL)
6976 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006977 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006978 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6979 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006980
6981 for (idx = 0; idx < instr->ga_len; ++idx)
6982 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006983 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006984
Bram Moolenaar45a15082020-05-25 00:28:33 +02006985 // if using the last entry in the table we might as well remove it
6986 if (!dfunc->df_deleted
6987 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01006988 --def_functions.ga_len;
Bram Moolenaar45a15082020-05-25 00:28:33 +02006989 ufunc->uf_dfunc_idx = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006990
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006991 while (cctx.ctx_scope != NULL)
6992 drop_scope(&cctx);
6993
Bram Moolenaar20431c92020-03-20 18:39:46 +01006994 // Don't execute this function body.
6995 ga_clear_strings(&ufunc->uf_lines);
6996
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997 if (errormsg != NULL)
6998 emsg(errormsg);
6999 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007000 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007001 }
7002
7003 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007004 if (do_estack_push)
7005 estack_pop();
7006
Bram Moolenaar20431c92020-03-20 18:39:46 +01007007 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007008 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007009 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007010 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011}
7012
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007013 void
7014set_function_type(ufunc_T *ufunc)
7015{
7016 int varargs = ufunc->uf_va_name != NULL;
7017 int argcount = ufunc->uf_args.ga_len;
7018
7019 // Create a type for the function, with the return type and any
7020 // argument types.
7021 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7022 // The type is included in "tt_args".
7023 if (argcount > 0 || varargs)
7024 {
7025 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7026 argcount, &ufunc->uf_type_list);
7027 // Add argument types to the function type.
7028 if (func_type_add_arg_types(ufunc->uf_func_type,
7029 argcount + varargs,
7030 &ufunc->uf_type_list) == FAIL)
7031 return;
7032 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7033 ufunc->uf_func_type->tt_min_argcount =
7034 argcount - ufunc->uf_def_args.ga_len;
7035 if (ufunc->uf_arg_types == NULL)
7036 {
7037 int i;
7038
7039 // lambda does not have argument types.
7040 for (i = 0; i < argcount; ++i)
7041 ufunc->uf_func_type->tt_args[i] = &t_any;
7042 }
7043 else
7044 mch_memmove(ufunc->uf_func_type->tt_args,
7045 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7046 if (varargs)
7047 {
7048 ufunc->uf_func_type->tt_args[argcount] =
7049 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7050 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7051 }
7052 }
7053 else
7054 // No arguments, can use a predefined type.
7055 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7056 argcount, &ufunc->uf_type_list);
7057}
7058
7059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060/*
7061 * Delete an instruction, free what it contains.
7062 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007063 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064delete_instr(isn_T *isn)
7065{
7066 switch (isn->isn_type)
7067 {
7068 case ISN_EXEC:
7069 case ISN_LOADENV:
7070 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007071 case ISN_LOADB:
7072 case ISN_LOADW:
7073 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007074 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007075 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007076 case ISN_PUSHEXC:
7077 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007078 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007079 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007080 case ISN_STOREB:
7081 case ISN_STOREW:
7082 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007083 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084 vim_free(isn->isn_arg.string);
7085 break;
7086
7087 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007088 case ISN_STORES:
7089 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007090 break;
7091
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007092 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007093 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007094 vim_free(isn->isn_arg.unlet.ul_name);
7095 break;
7096
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097 case ISN_STOREOPT:
7098 vim_free(isn->isn_arg.storeopt.so_name);
7099 break;
7100
7101 case ISN_PUSHBLOB: // push blob isn_arg.blob
7102 blob_unref(isn->isn_arg.blob);
7103 break;
7104
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007105 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007106#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007107 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007108#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007109 break;
7110
7111 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007112#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007113 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007114#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007115 break;
7116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007117 case ISN_UCALL:
7118 vim_free(isn->isn_arg.ufunc.cuf_name);
7119 break;
7120
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007121 case ISN_FUNCREF:
7122 {
7123 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7124 + isn->isn_arg.funcref.fr_func;
7125 func_ptr_unref(dfunc->df_ufunc);
7126 }
7127 break;
7128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129 case ISN_2BOOL:
7130 case ISN_2STRING:
7131 case ISN_ADDBLOB:
7132 case ISN_ADDLIST:
7133 case ISN_BCALL:
7134 case ISN_CATCH:
7135 case ISN_CHECKNR:
7136 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007137 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138 case ISN_COMPAREANY:
7139 case ISN_COMPAREBLOB:
7140 case ISN_COMPAREBOOL:
7141 case ISN_COMPAREDICT:
7142 case ISN_COMPAREFLOAT:
7143 case ISN_COMPAREFUNC:
7144 case ISN_COMPARELIST:
7145 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007146 case ISN_COMPARESPECIAL:
7147 case ISN_COMPARESTRING:
7148 case ISN_CONCAT:
7149 case ISN_DCALL:
7150 case ISN_DROP:
7151 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007152 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007153 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007154 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007155 case ISN_EXECCONCAT:
7156 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007157 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007159 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007160 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007161 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007162 case ISN_JUMP:
7163 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007164 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007165 case ISN_LOADSCRIPT:
7166 case ISN_LOADREG:
7167 case ISN_LOADV:
7168 case ISN_NEGATENR:
7169 case ISN_NEWDICT:
7170 case ISN_NEWLIST:
7171 case ISN_OPNR:
7172 case ISN_OPFLOAT:
7173 case ISN_OPANY:
7174 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007175 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007176 case ISN_PUSHF:
7177 case ISN_PUSHNR:
7178 case ISN_PUSHBOOL:
7179 case ISN_PUSHSPEC:
7180 case ISN_RETURN:
7181 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007182 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007183 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007185 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007186 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007187 case ISN_STOREDICT:
7188 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007189 case ISN_THROW:
7190 case ISN_TRY:
7191 // nothing allocated
7192 break;
7193 }
7194}
7195
7196/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007197 * Free all instructions for "dfunc".
7198 */
7199 static void
7200delete_def_function_contents(dfunc_T *dfunc)
7201{
7202 int idx;
7203
7204 ga_clear(&dfunc->df_def_args_isn);
7205
7206 if (dfunc->df_instr != NULL)
7207 {
7208 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7209 delete_instr(dfunc->df_instr + idx);
7210 VIM_CLEAR(dfunc->df_instr);
7211 }
7212
7213 dfunc->df_deleted = TRUE;
7214}
7215
7216/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007217 * When a user function is deleted, delete any associated def function.
7218 */
7219 void
7220delete_def_function(ufunc_T *ufunc)
7221{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007222 if (ufunc->uf_dfunc_idx >= 0)
7223 {
7224 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7225 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007226
Bram Moolenaar20431c92020-03-20 18:39:46 +01007227 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007228 }
7229}
7230
7231#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007232/*
7233 * Free all functions defined with ":def".
7234 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007235 void
7236free_def_functions(void)
7237{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007238 int idx;
7239
7240 for (idx = 0; idx < def_functions.ga_len; ++idx)
7241 {
7242 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7243
7244 delete_def_function_contents(dfunc);
7245 }
7246
7247 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007248}
7249#endif
7250
7251
7252#endif // FEAT_EVAL