blob: fdb422bf28e8c3d71dcc89ecca377aa43a1d4b8b [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
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200129 int ctx_closure_count; // number of closures created in the
130 // function
131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
146static char e_var_notfound[] = N_("E1001: variable not found: %s");
147static char e_syntax_at[] = N_("E1002: Syntax error at %s");
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +0200148static char e_used_as_arg[] = N_("E1006: %s is used as an argument");
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200149static char e_cannot_use_void[] = N_("E1031: Cannot use void value");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150
Bram Moolenaar20431c92020-03-20 18:39:46 +0100151static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200152static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153
154/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200155 * Lookup variable "name" in the local scope and return it.
156 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200158 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159lookup_local(char_u *name, size_t len, cctx_T *cctx)
160{
161 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200162 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100164 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200165 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200166
167 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
169 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 if (STRNCMP(name, lvar->lv_name, len) == 0
172 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200173 {
174 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200175 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200176 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100177 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200178
179 // Find local in outer function scope.
180 if (cctx->ctx_outer != NULL)
181 {
182 lvar = lookup_local(name, len, cctx->ctx_outer);
183 if (lvar != NULL)
184 {
185 // TODO: are there situations we should not mark the outer scope as
186 // used?
187 cctx->ctx_outer_used = TRUE;
188 lvar->lv_from_outer = TRUE;
189 return lvar;
190 }
191 }
192
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200193 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100194}
195
196/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200197 * Lookup an argument in the current function and an enclosing function.
198 * Returns the argument index in "idxp"
199 * Returns the argument type in "type"
200 * Sets "gen_load_outer" to TRUE if found in outer scope.
201 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100202 */
203 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200204lookup_arg(
205 char_u *name,
206 size_t len,
207 int *idxp,
208 type_T **type,
209 int *gen_load_outer,
210 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211{
212 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200213 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100214
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100215 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200216 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
218 {
219 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
220
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200221 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
222 {
223 if (idxp != NULL)
224 {
225 // Arguments are located above the frame pointer. One further
226 // if there is a vararg argument
227 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
228 + STACK_FRAME_SIZE)
229 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
230
231 if (cctx->ctx_ufunc->uf_arg_types != NULL)
232 *type = cctx->ctx_ufunc->uf_arg_types[idx];
233 else
234 *type = &t_any;
235 }
236 return OK;
237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200240 va_name = cctx->ctx_ufunc->uf_va_name;
241 if (va_name != NULL
242 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
243 {
244 if (idxp != NULL)
245 {
246 // varargs is always the last argument
247 *idxp = -STACK_FRAME_SIZE - 1;
248 *type = cctx->ctx_ufunc->uf_va_type;
249 }
250 return OK;
251 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200253 if (cctx->ctx_outer != NULL)
254 {
255 // Lookup the name for an argument of the outer function.
256 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
257 == OK)
258 {
259 *gen_load_outer = TRUE;
260 return OK;
261 }
262 }
263
264 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265}
266
267/*
268 * Lookup a variable in the current script.
269 * Returns OK or FAIL.
270 */
271 static int
272lookup_script(char_u *name, size_t len)
273{
274 int cc;
275 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
276 dictitem_T *di;
277
278 cc = name[len];
279 name[len] = NUL;
280 di = find_var_in_ht(ht, 0, name, TRUE);
281 name[len] = cc;
282 return di == NULL ? FAIL: OK;
283}
284
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100285/*
286 * Check if "p[len]" is already defined, either in script "import_sid" or in
287 * compilation context "cctx".
288 * Return FAIL and give an error if it defined.
289 */
290 int
291check_defined(char_u *p, int len, cctx_T *cctx)
292{
293 if (lookup_script(p, len) == OK
294 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200295 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100296 || find_imported(p, len, cctx) != NULL)))
297 {
298 semsg("E1073: imported name already defined: %s", p);
299 return FAIL;
300 }
301 return OK;
302}
303
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200304/*
305 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
306 * be freed later.
307 */
308 static type_T *
309alloc_type(garray_T *type_gap)
310{
311 type_T *type;
312
313 if (ga_grow(type_gap, 1) == FAIL)
314 return NULL;
315 type = ALLOC_CLEAR_ONE(type_T);
316 if (type != NULL)
317 {
318 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
319 ++type_gap->ga_len;
320 }
321 return type;
322}
323
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100324 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200325get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326{
327 type_T *type;
328
329 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200330 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100331 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200332 if (member_type->tt_type == VAR_VOID
333 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100334 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100335 if (member_type->tt_type == VAR_BOOL)
336 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100337 if (member_type->tt_type == VAR_NUMBER)
338 return &t_list_number;
339 if (member_type->tt_type == VAR_STRING)
340 return &t_list_string;
341
342 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200343 type = alloc_type(type_gap);
344 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100345 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 type->tt_type = VAR_LIST;
347 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200348 type->tt_argcount = 0;
349 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100350 return type;
351}
352
353 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200354get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100355{
356 type_T *type;
357
358 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200359 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100360 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200361 if (member_type->tt_type == VAR_VOID
362 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100363 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100364 if (member_type->tt_type == VAR_BOOL)
365 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366 if (member_type->tt_type == VAR_NUMBER)
367 return &t_dict_number;
368 if (member_type->tt_type == VAR_STRING)
369 return &t_dict_string;
370
371 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200372 type = alloc_type(type_gap);
373 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100374 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100375 type->tt_type = VAR_DICT;
376 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200377 type->tt_argcount = 0;
378 type->tt_args = NULL;
379 return type;
380}
381
382/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200383 * Allocate a new type for a function.
384 */
385 static type_T *
386alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
387{
388 type_T *type = alloc_type(type_gap);
389
390 if (type == NULL)
391 return &t_any;
392 type->tt_type = VAR_FUNC;
393 type->tt_member = ret_type;
394 type->tt_argcount = argcount;
395 type->tt_args = NULL;
396 return type;
397}
398
399/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200400 * Get a function type, based on the return type "ret_type".
401 * If "argcount" is -1 or 0 a predefined type can be used.
402 * If "argcount" > 0 always create a new type, so that arguments can be added.
403 */
404 static type_T *
405get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
406{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200407 // recognize commonly used types
408 if (argcount <= 0)
409 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200410 if (ret_type == &t_unknown)
411 {
412 // (argcount == 0) is not possible
413 return &t_func_unknown;
414 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200415 if (ret_type == &t_void)
416 {
417 if (argcount == 0)
418 return &t_func_0_void;
419 else
420 return &t_func_void;
421 }
422 if (ret_type == &t_any)
423 {
424 if (argcount == 0)
425 return &t_func_0_any;
426 else
427 return &t_func_any;
428 }
429 if (ret_type == &t_number)
430 {
431 if (argcount == 0)
432 return &t_func_0_number;
433 else
434 return &t_func_number;
435 }
436 if (ret_type == &t_string)
437 {
438 if (argcount == 0)
439 return &t_func_0_string;
440 else
441 return &t_func_string;
442 }
443 }
444
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200445 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100446}
447
Bram Moolenaara8c17702020-04-01 21:17:24 +0200448/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200449 * For a function type, reserve space for "argcount" argument types (including
450 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200451 */
452 static int
453func_type_add_arg_types(
454 type_T *functype,
455 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200456 garray_T *type_gap)
457{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200458 // To make it easy to free the space needed for the argument types, add the
459 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200460 if (ga_grow(type_gap, 1) == FAIL)
461 return FAIL;
462 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
463 if (functype->tt_args == NULL)
464 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200465 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
466 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200467 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200468 return OK;
469}
470
471/*
Bram Moolenaara8c17702020-04-01 21:17:24 +0200472 * Return the type_T for a typval. Only for primitive types.
473 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200474 type_T *
Bram Moolenaara8c17702020-04-01 21:17:24 +0200475typval2type(typval_T *tv)
476{
477 if (tv->v_type == VAR_NUMBER)
478 return &t_number;
479 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200480 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200481 if (tv->v_type == VAR_STRING)
482 return &t_string;
483 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
484 return &t_list_string;
485 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
486 return &t_dict_any;
Bram Moolenaar5da356e2020-04-09 19:34:43 +0200487 return &t_any; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200488}
489
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200490 static void
491type_mismatch(type_T *expected, type_T *actual)
492{
493 char *tofree1, *tofree2;
494
495 semsg(_("E1013: type mismatch, expected %s but got %s"),
496 type_name(expected, &tofree1), type_name(actual, &tofree2));
497 vim_free(tofree1);
498 vim_free(tofree2);
499}
500
501 static void
502arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
503{
504 char *tofree1, *tofree2;
505
506 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
507 argidx,
508 type_name(expected, &tofree1), type_name(actual, &tofree2));
509 vim_free(tofree1);
510 vim_free(tofree2);
511}
512
513/*
514 * Check if the expected and actual types match.
515 * Does not allow for assigning "any" to a specific type.
516 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200517 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200518check_type(type_T *expected, type_T *actual, int give_msg)
519{
520 int ret = OK;
521
522 // When expected is "unknown" we accept any actual type.
523 // When expected is "any" we accept any actual type except "void".
524 if (expected->tt_type != VAR_UNKNOWN
525 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
526
527 {
528 if (expected->tt_type != actual->tt_type)
529 {
530 if (give_msg)
531 type_mismatch(expected, actual);
532 return FAIL;
533 }
534 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
535 {
536 // "unknown" is used for an empty list or dict
537 if (actual->tt_member != &t_unknown)
538 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
539 }
540 else if (expected->tt_type == VAR_FUNC)
541 {
542 if (expected->tt_member != &t_unknown)
543 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
544 if (ret == OK && expected->tt_argcount != -1
545 && (actual->tt_argcount < expected->tt_min_argcount
546 || actual->tt_argcount > expected->tt_argcount))
547 ret = FAIL;
548 }
549 if (ret == FAIL && give_msg)
550 type_mismatch(expected, actual);
551 }
552 return ret;
553}
554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555/////////////////////////////////////////////////////////////////////
556// Following generate_ functions expect the caller to call ga_grow().
557
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200558#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
559#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100561/*
562 * Generate an instruction without arguments.
563 * Returns a pointer to the new instruction, NULL if failed.
564 */
565 static isn_T *
566generate_instr(cctx_T *cctx, isntype_T isn_type)
567{
568 garray_T *instr = &cctx->ctx_instr;
569 isn_T *isn;
570
Bram Moolenaar080457c2020-03-03 21:53:32 +0100571 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572 if (ga_grow(instr, 1) == FAIL)
573 return NULL;
574 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
575 isn->isn_type = isn_type;
576 isn->isn_lnum = cctx->ctx_lnum + 1;
577 ++instr->ga_len;
578
579 return isn;
580}
581
582/*
583 * Generate an instruction without arguments.
584 * "drop" will be removed from the stack.
585 * Returns a pointer to the new instruction, NULL if failed.
586 */
587 static isn_T *
588generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
589{
590 garray_T *stack = &cctx->ctx_type_stack;
591
Bram Moolenaar080457c2020-03-03 21:53:32 +0100592 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 stack->ga_len -= drop;
594 return generate_instr(cctx, isn_type);
595}
596
597/*
598 * Generate instruction "isn_type" and put "type" on the type stack.
599 */
600 static isn_T *
601generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
602{
603 isn_T *isn;
604 garray_T *stack = &cctx->ctx_type_stack;
605
606 if ((isn = generate_instr(cctx, isn_type)) == NULL)
607 return NULL;
608
609 if (ga_grow(stack, 1) == FAIL)
610 return NULL;
611 ((type_T **)stack->ga_data)[stack->ga_len] = type;
612 ++stack->ga_len;
613
614 return isn;
615}
616
617/*
618 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
619 */
620 static int
621may_generate_2STRING(int offset, cctx_T *cctx)
622{
623 isn_T *isn;
624 garray_T *stack = &cctx->ctx_type_stack;
625 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
626
627 if ((*type)->tt_type == VAR_STRING)
628 return OK;
629 *type = &t_string;
630
631 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
632 return FAIL;
633 isn->isn_arg.number = offset;
634
635 return OK;
636}
637
638 static int
639check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
640{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200641 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200643 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644 {
645 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200646 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100647 else
648 semsg(_("E1036: %c requires number or float arguments"), *op);
649 return FAIL;
650 }
651 return OK;
652}
653
654/*
655 * Generate an instruction with two arguments. The instruction depends on the
656 * type of the arguments.
657 */
658 static int
659generate_two_op(cctx_T *cctx, char_u *op)
660{
661 garray_T *stack = &cctx->ctx_type_stack;
662 type_T *type1;
663 type_T *type2;
664 vartype_T vartype;
665 isn_T *isn;
666
Bram Moolenaar080457c2020-03-03 21:53:32 +0100667 RETURN_OK_IF_SKIP(cctx);
668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669 // Get the known type of the two items on the stack. If they are matching
670 // use a type-specific instruction. Otherwise fall back to runtime type
671 // checking.
672 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
673 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200674 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675 if (type1->tt_type == type2->tt_type
676 && (type1->tt_type == VAR_NUMBER
677 || type1->tt_type == VAR_LIST
678#ifdef FEAT_FLOAT
679 || type1->tt_type == VAR_FLOAT
680#endif
681 || type1->tt_type == VAR_BLOB))
682 vartype = type1->tt_type;
683
684 switch (*op)
685 {
686 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200687 && type1->tt_type != VAR_ANY
688 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 && check_number_or_float(
690 type1->tt_type, type2->tt_type, op) == FAIL)
691 return FAIL;
692 isn = generate_instr_drop(cctx,
693 vartype == VAR_NUMBER ? ISN_OPNR
694 : vartype == VAR_LIST ? ISN_ADDLIST
695 : vartype == VAR_BLOB ? ISN_ADDBLOB
696#ifdef FEAT_FLOAT
697 : vartype == VAR_FLOAT ? ISN_OPFLOAT
698#endif
699 : ISN_OPANY, 1);
700 if (isn != NULL)
701 isn->isn_arg.op.op_type = EXPR_ADD;
702 break;
703
704 case '-':
705 case '*':
706 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
707 op) == FAIL)
708 return FAIL;
709 if (vartype == VAR_NUMBER)
710 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
711#ifdef FEAT_FLOAT
712 else if (vartype == VAR_FLOAT)
713 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
714#endif
715 else
716 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
717 if (isn != NULL)
718 isn->isn_arg.op.op_type = *op == '*'
719 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
720 break;
721
Bram Moolenaar4c683752020-04-05 21:38:23 +0200722 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200724 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725 && type2->tt_type != VAR_NUMBER))
726 {
727 emsg(_("E1035: % requires number arguments"));
728 return FAIL;
729 }
730 isn = generate_instr_drop(cctx,
731 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
732 if (isn != NULL)
733 isn->isn_arg.op.op_type = EXPR_REM;
734 break;
735 }
736
737 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200738 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100739 {
740 type_T *type = &t_any;
741
742#ifdef FEAT_FLOAT
743 // float+number and number+float results in float
744 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
745 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
746 type = &t_float;
747#endif
748 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
749 }
750
751 return OK;
752}
753
754/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200755 * Get the instruction to use for comparing "type1" with "type2"
756 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200758 static isntype_T
759get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760{
761 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762
Bram Moolenaar4c683752020-04-05 21:38:23 +0200763 if (type1 == VAR_UNKNOWN)
764 type1 = VAR_ANY;
765 if (type2 == VAR_UNKNOWN)
766 type2 = VAR_ANY;
767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768 if (type1 == type2)
769 {
770 switch (type1)
771 {
772 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
773 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
774 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
775 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
776 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
777 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
778 case VAR_LIST: isntype = ISN_COMPARELIST; break;
779 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
780 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 default: isntype = ISN_COMPAREANY; break;
782 }
783 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200784 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
786 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
787 isntype = ISN_COMPAREANY;
788
789 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
790 && (isntype == ISN_COMPAREBOOL
791 || isntype == ISN_COMPARESPECIAL
792 || isntype == ISN_COMPARENR
793 || isntype == ISN_COMPAREFLOAT))
794 {
795 semsg(_("E1037: Cannot use \"%s\" with %s"),
796 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200797 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798 }
799 if (isntype == ISN_DROP
800 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
801 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
802 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
803 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
804 && exptype != EXPR_IS && exptype != EXPR_ISNOT
805 && (type1 == VAR_BLOB || type2 == VAR_BLOB
806 || type1 == VAR_LIST || type2 == VAR_LIST))))
807 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100808 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200810 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200812 return isntype;
813}
814
815/*
816 * Generate an ISN_COMPARE* instruction with a boolean result.
817 */
818 static int
819generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
820{
821 isntype_T isntype;
822 isn_T *isn;
823 garray_T *stack = &cctx->ctx_type_stack;
824 vartype_T type1;
825 vartype_T type2;
826
827 RETURN_OK_IF_SKIP(cctx);
828
829 // Get the known type of the two items on the stack. If they are matching
830 // use a type-specific instruction. Otherwise fall back to runtime type
831 // checking.
832 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
833 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
834 isntype = get_compare_isn(exptype, type1, type2);
835 if (isntype == ISN_DROP)
836 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837
838 if ((isn = generate_instr(cctx, isntype)) == NULL)
839 return FAIL;
840 isn->isn_arg.op.op_type = exptype;
841 isn->isn_arg.op.op_ic = ic;
842
843 // takes two arguments, puts one bool back
844 if (stack->ga_len >= 2)
845 {
846 --stack->ga_len;
847 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
848 }
849
850 return OK;
851}
852
853/*
854 * Generate an ISN_2BOOL instruction.
855 */
856 static int
857generate_2BOOL(cctx_T *cctx, int invert)
858{
859 isn_T *isn;
860 garray_T *stack = &cctx->ctx_type_stack;
861
Bram Moolenaar080457c2020-03-03 21:53:32 +0100862 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100863 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
864 return FAIL;
865 isn->isn_arg.number = invert;
866
867 // type becomes bool
868 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
869
870 return OK;
871}
872
873 static int
874generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
875{
876 isn_T *isn;
877 garray_T *stack = &cctx->ctx_type_stack;
878
Bram Moolenaar080457c2020-03-03 21:53:32 +0100879 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
881 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200882 // TODO: whole type, e.g. for a function also arg and return types
883 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884 isn->isn_arg.type.ct_off = offset;
885
886 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200887 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888
889 return OK;
890}
891
892/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200893 * Check that
894 * - "actual" is "expected" type or
895 * - "actual" is a type that can be "expected" type: add a runtime check; or
896 * - return FAIL.
897 */
898 static int
899need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
900{
901 if (check_type(expected, actual, FALSE) == OK)
902 return OK;
903 if (actual->tt_type != VAR_ANY
904 && actual->tt_type != VAR_UNKNOWN
905 && !(actual->tt_type == VAR_FUNC
906 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
907 {
908 type_mismatch(expected, actual);
909 return FAIL;
910 }
911 generate_TYPECHECK(cctx, expected, offset);
912 return OK;
913}
914
915/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100916 * Generate an ISN_PUSHNR instruction.
917 */
918 static int
919generate_PUSHNR(cctx_T *cctx, varnumber_T number)
920{
921 isn_T *isn;
922
Bram Moolenaar080457c2020-03-03 21:53:32 +0100923 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100924 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
925 return FAIL;
926 isn->isn_arg.number = number;
927
928 return OK;
929}
930
931/*
932 * Generate an ISN_PUSHBOOL instruction.
933 */
934 static int
935generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
936{
937 isn_T *isn;
938
Bram Moolenaar080457c2020-03-03 21:53:32 +0100939 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
941 return FAIL;
942 isn->isn_arg.number = number;
943
944 return OK;
945}
946
947/*
948 * Generate an ISN_PUSHSPEC instruction.
949 */
950 static int
951generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
952{
953 isn_T *isn;
954
Bram Moolenaar080457c2020-03-03 21:53:32 +0100955 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
957 return FAIL;
958 isn->isn_arg.number = number;
959
960 return OK;
961}
962
963#ifdef FEAT_FLOAT
964/*
965 * Generate an ISN_PUSHF instruction.
966 */
967 static int
968generate_PUSHF(cctx_T *cctx, float_T fnumber)
969{
970 isn_T *isn;
971
Bram Moolenaar080457c2020-03-03 21:53:32 +0100972 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
974 return FAIL;
975 isn->isn_arg.fnumber = fnumber;
976
977 return OK;
978}
979#endif
980
981/*
982 * Generate an ISN_PUSHS instruction.
983 * Consumes "str".
984 */
985 static int
986generate_PUSHS(cctx_T *cctx, char_u *str)
987{
988 isn_T *isn;
989
Bram Moolenaar080457c2020-03-03 21:53:32 +0100990 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
992 return FAIL;
993 isn->isn_arg.string = str;
994
995 return OK;
996}
997
998/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100999 * Generate an ISN_PUSHCHANNEL instruction.
1000 * Consumes "channel".
1001 */
1002 static int
1003generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1004{
1005 isn_T *isn;
1006
Bram Moolenaar080457c2020-03-03 21:53:32 +01001007 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001008 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1009 return FAIL;
1010 isn->isn_arg.channel = channel;
1011
1012 return OK;
1013}
1014
1015/*
1016 * Generate an ISN_PUSHJOB instruction.
1017 * Consumes "job".
1018 */
1019 static int
1020generate_PUSHJOB(cctx_T *cctx, job_T *job)
1021{
1022 isn_T *isn;
1023
Bram Moolenaar080457c2020-03-03 21:53:32 +01001024 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001025 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001026 return FAIL;
1027 isn->isn_arg.job = job;
1028
1029 return OK;
1030}
1031
1032/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 * Generate an ISN_PUSHBLOB instruction.
1034 * Consumes "blob".
1035 */
1036 static int
1037generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1038{
1039 isn_T *isn;
1040
Bram Moolenaar080457c2020-03-03 21:53:32 +01001041 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1043 return FAIL;
1044 isn->isn_arg.blob = blob;
1045
1046 return OK;
1047}
1048
1049/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001050 * Generate an ISN_PUSHFUNC instruction with name "name".
1051 * Consumes "name".
1052 */
1053 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001054generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001055{
1056 isn_T *isn;
1057
Bram Moolenaar080457c2020-03-03 21:53:32 +01001058 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001059 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001060 return FAIL;
1061 isn->isn_arg.string = name;
1062
1063 return OK;
1064}
1065
1066/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001067 * Generate an ISN_GETITEM instruction with "index".
1068 */
1069 static int
1070generate_GETITEM(cctx_T *cctx, int index)
1071{
1072 isn_T *isn;
1073 garray_T *stack = &cctx->ctx_type_stack;
1074 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1075 type_T *item_type = &t_any;
1076
1077 RETURN_OK_IF_SKIP(cctx);
1078
1079 if (type->tt_type == VAR_LIST)
1080 item_type = type->tt_member;
1081 else if (type->tt_type != VAR_ANY)
1082 {
1083 emsg(_(e_listreq));
1084 return FAIL;
1085 }
1086 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1087 return FAIL;
1088 isn->isn_arg.number = index;
1089
1090 // add the item type to the type stack
1091 if (ga_grow(stack, 1) == FAIL)
1092 return FAIL;
1093 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1094 ++stack->ga_len;
1095 return OK;
1096}
1097
1098/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001099 * Generate an ISN_SLICE instruction with "count".
1100 */
1101 static int
1102generate_SLICE(cctx_T *cctx, int count)
1103{
1104 isn_T *isn;
1105
1106 RETURN_OK_IF_SKIP(cctx);
1107 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1108 return FAIL;
1109 isn->isn_arg.number = count;
1110 return OK;
1111}
1112
1113/*
1114 * Generate an ISN_CHECKLEN instruction with "min_len".
1115 */
1116 static int
1117generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1118{
1119 isn_T *isn;
1120
1121 RETURN_OK_IF_SKIP(cctx);
1122
1123 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1124 return FAIL;
1125 isn->isn_arg.checklen.cl_min_len = min_len;
1126 isn->isn_arg.checklen.cl_more_OK = more_OK;
1127
1128 return OK;
1129}
1130
1131/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 * Generate an ISN_STORE instruction.
1133 */
1134 static int
1135generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1136{
1137 isn_T *isn;
1138
Bram Moolenaar080457c2020-03-03 21:53:32 +01001139 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1141 return FAIL;
1142 if (name != NULL)
1143 isn->isn_arg.string = vim_strsave(name);
1144 else
1145 isn->isn_arg.number = idx;
1146
1147 return OK;
1148}
1149
1150/*
1151 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1152 */
1153 static int
1154generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1155{
1156 isn_T *isn;
1157
Bram Moolenaar080457c2020-03-03 21:53:32 +01001158 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001159 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1160 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001161 isn->isn_arg.storenr.stnr_idx = idx;
1162 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163
1164 return OK;
1165}
1166
1167/*
1168 * Generate an ISN_STOREOPT instruction
1169 */
1170 static int
1171generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1172{
1173 isn_T *isn;
1174
Bram Moolenaar080457c2020-03-03 21:53:32 +01001175 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1177 return FAIL;
1178 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1179 isn->isn_arg.storeopt.so_flags = opt_flags;
1180
1181 return OK;
1182}
1183
1184/*
1185 * Generate an ISN_LOAD or similar instruction.
1186 */
1187 static int
1188generate_LOAD(
1189 cctx_T *cctx,
1190 isntype_T isn_type,
1191 int idx,
1192 char_u *name,
1193 type_T *type)
1194{
1195 isn_T *isn;
1196
Bram Moolenaar080457c2020-03-03 21:53:32 +01001197 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1199 return FAIL;
1200 if (name != NULL)
1201 isn->isn_arg.string = vim_strsave(name);
1202 else
1203 isn->isn_arg.number = idx;
1204
1205 return OK;
1206}
1207
1208/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001209 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001210 */
1211 static int
1212generate_LOADV(
1213 cctx_T *cctx,
1214 char_u *name,
1215 int error)
1216{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001217 int di_flags;
1218 int vidx = find_vim_var(name, &di_flags);
1219 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001220
Bram Moolenaar080457c2020-03-03 21:53:32 +01001221 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001222 if (vidx < 0)
1223 {
1224 if (error)
1225 semsg(_(e_var_notfound), name);
1226 return FAIL;
1227 }
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001228 type = typval2type(get_vim_var_tv(vidx));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001229
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001230 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001231}
1232
1233/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001234 * Generate an ISN_UNLET instruction.
1235 */
1236 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001237generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001238{
1239 isn_T *isn;
1240
1241 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001242 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001243 return FAIL;
1244 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1245 isn->isn_arg.unlet.ul_forceit = forceit;
1246
1247 return OK;
1248}
1249
1250/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 * Generate an ISN_LOADS instruction.
1252 */
1253 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001254generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001256 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001258 int sid,
1259 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260{
1261 isn_T *isn;
1262
Bram Moolenaar080457c2020-03-03 21:53:32 +01001263 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001264 if (isn_type == ISN_LOADS)
1265 isn = generate_instr_type(cctx, isn_type, type);
1266 else
1267 isn = generate_instr_drop(cctx, isn_type, 1);
1268 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001270 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1271 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272
1273 return OK;
1274}
1275
1276/*
1277 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1278 */
1279 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001280generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 cctx_T *cctx,
1282 isntype_T isn_type,
1283 int sid,
1284 int idx,
1285 type_T *type)
1286{
1287 isn_T *isn;
1288
Bram Moolenaar080457c2020-03-03 21:53:32 +01001289 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 if (isn_type == ISN_LOADSCRIPT)
1291 isn = generate_instr_type(cctx, isn_type, type);
1292 else
1293 isn = generate_instr_drop(cctx, isn_type, 1);
1294 if (isn == NULL)
1295 return FAIL;
1296 isn->isn_arg.script.script_sid = sid;
1297 isn->isn_arg.script.script_idx = idx;
1298 return OK;
1299}
1300
1301/*
1302 * Generate an ISN_NEWLIST instruction.
1303 */
1304 static int
1305generate_NEWLIST(cctx_T *cctx, int count)
1306{
1307 isn_T *isn;
1308 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 type_T *type;
1310 type_T *member;
1311
Bram Moolenaar080457c2020-03-03 21:53:32 +01001312 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1314 return FAIL;
1315 isn->isn_arg.number = count;
1316
1317 // drop the value types
1318 stack->ga_len -= count;
1319
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001320 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001321 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 if (count > 0)
1323 member = ((type_T **)stack->ga_data)[stack->ga_len];
1324 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001325 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001326 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327
1328 // add the list type to the type stack
1329 if (ga_grow(stack, 1) == FAIL)
1330 return FAIL;
1331 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1332 ++stack->ga_len;
1333
1334 return OK;
1335}
1336
1337/*
1338 * Generate an ISN_NEWDICT instruction.
1339 */
1340 static int
1341generate_NEWDICT(cctx_T *cctx, int count)
1342{
1343 isn_T *isn;
1344 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 type_T *type;
1346 type_T *member;
1347
Bram Moolenaar080457c2020-03-03 21:53:32 +01001348 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1350 return FAIL;
1351 isn->isn_arg.number = count;
1352
1353 // drop the key and value types
1354 stack->ga_len -= 2 * count;
1355
Bram Moolenaar436472f2020-02-20 22:54:43 +01001356 // Use the first value type for the list member type. Use "void" for an
1357 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 if (count > 0)
1359 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1360 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001361 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001362 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363
1364 // add the dict type to the type stack
1365 if (ga_grow(stack, 1) == FAIL)
1366 return FAIL;
1367 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1368 ++stack->ga_len;
1369
1370 return OK;
1371}
1372
1373/*
1374 * Generate an ISN_FUNCREF instruction.
1375 */
1376 static int
1377generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1378{
1379 isn_T *isn;
1380 garray_T *stack = &cctx->ctx_type_stack;
1381
Bram Moolenaar080457c2020-03-03 21:53:32 +01001382 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1384 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001385 isn->isn_arg.funcref.fr_func = dfunc_idx;
1386 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387
1388 if (ga_grow(stack, 1) == FAIL)
1389 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001390 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 // TODO: argument and return types
1392 ++stack->ga_len;
1393
1394 return OK;
1395}
1396
1397/*
1398 * Generate an ISN_JUMP instruction.
1399 */
1400 static int
1401generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1402{
1403 isn_T *isn;
1404 garray_T *stack = &cctx->ctx_type_stack;
1405
Bram Moolenaar080457c2020-03-03 21:53:32 +01001406 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1408 return FAIL;
1409 isn->isn_arg.jump.jump_when = when;
1410 isn->isn_arg.jump.jump_where = where;
1411
1412 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1413 --stack->ga_len;
1414
1415 return OK;
1416}
1417
1418 static int
1419generate_FOR(cctx_T *cctx, int loop_idx)
1420{
1421 isn_T *isn;
1422 garray_T *stack = &cctx->ctx_type_stack;
1423
Bram Moolenaar080457c2020-03-03 21:53:32 +01001424 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1426 return FAIL;
1427 isn->isn_arg.forloop.for_idx = loop_idx;
1428
1429 if (ga_grow(stack, 1) == FAIL)
1430 return FAIL;
1431 // type doesn't matter, will be stored next
1432 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1433 ++stack->ga_len;
1434
1435 return OK;
1436}
1437
1438/*
1439 * Generate an ISN_BCALL instruction.
1440 * Return FAIL if the number of arguments is wrong.
1441 */
1442 static int
1443generate_BCALL(cctx_T *cctx, int func_idx, int argcount)
1444{
1445 isn_T *isn;
1446 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001447 type_T *argtypes[MAX_FUNC_ARGS];
1448 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001449
Bram Moolenaar080457c2020-03-03 21:53:32 +01001450 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 if (check_internal_func(func_idx, argcount) == FAIL)
1452 return FAIL;
1453
1454 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1455 return FAIL;
1456 isn->isn_arg.bfunc.cbf_idx = func_idx;
1457 isn->isn_arg.bfunc.cbf_argcount = argcount;
1458
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001459 for (i = 0; i < argcount; ++i)
1460 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1461
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 stack->ga_len -= argcount; // drop the arguments
1463 if (ga_grow(stack, 1) == FAIL)
1464 return FAIL;
1465 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001466 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 ++stack->ga_len; // add return value
1468
1469 return OK;
1470}
1471
1472/*
1473 * Generate an ISN_DCALL or ISN_UCALL instruction.
1474 * Return FAIL if the number of arguments is wrong.
1475 */
1476 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001477generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478{
1479 isn_T *isn;
1480 garray_T *stack = &cctx->ctx_type_stack;
1481 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001482 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483
Bram Moolenaar080457c2020-03-03 21:53:32 +01001484 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 if (argcount > regular_args && !has_varargs(ufunc))
1486 {
1487 semsg(_(e_toomanyarg), ufunc->uf_name);
1488 return FAIL;
1489 }
1490 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1491 {
1492 semsg(_(e_toofewarg), ufunc->uf_name);
1493 return FAIL;
1494 }
1495
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001496 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001497 {
1498 int i;
1499
1500 for (i = 0; i < argcount; ++i)
1501 {
1502 type_T *expected;
1503 type_T *actual;
1504
1505 if (i < regular_args)
1506 {
1507 if (ufunc->uf_arg_types == NULL)
1508 continue;
1509 expected = ufunc->uf_arg_types[i];
1510 }
1511 else
1512 expected = ufunc->uf_va_type->tt_member;
1513 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001514 if (need_type(actual, expected, -argcount + i, cctx) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001515 {
1516 arg_type_mismatch(expected, actual, i + 1);
1517 return FAIL;
1518 }
1519 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001520 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001521 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001522 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001523 }
1524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001526 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001527 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001529 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 {
1531 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1532 isn->isn_arg.dfunc.cdf_argcount = argcount;
1533 }
1534 else
1535 {
1536 // A user function may be deleted and redefined later, can't use the
1537 // ufunc pointer, need to look it up again at runtime.
1538 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1539 isn->isn_arg.ufunc.cuf_argcount = argcount;
1540 }
1541
1542 stack->ga_len -= argcount; // drop the arguments
1543 if (ga_grow(stack, 1) == FAIL)
1544 return FAIL;
1545 // add return value
1546 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1547 ++stack->ga_len;
1548
1549 return OK;
1550}
1551
1552/*
1553 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1554 */
1555 static int
1556generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1557{
1558 isn_T *isn;
1559 garray_T *stack = &cctx->ctx_type_stack;
1560
Bram Moolenaar080457c2020-03-03 21:53:32 +01001561 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1563 return FAIL;
1564 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1565 isn->isn_arg.ufunc.cuf_argcount = argcount;
1566
1567 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001568 if (ga_grow(stack, 1) == FAIL)
1569 return FAIL;
1570 // add return value
1571 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1572 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573
1574 return OK;
1575}
1576
1577/*
1578 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001579 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580 */
1581 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001582generate_PCALL(
1583 cctx_T *cctx,
1584 int argcount,
1585 char_u *name,
1586 type_T *type,
1587 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588{
1589 isn_T *isn;
1590 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001591 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592
Bram Moolenaar080457c2020-03-03 21:53:32 +01001593 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001594
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001595 if (type->tt_type == VAR_ANY)
1596 ret_type = &t_any;
1597 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001598 {
1599 if (type->tt_argcount != -1)
1600 {
1601 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1602
1603 if (argcount < type->tt_min_argcount - varargs)
1604 {
1605 semsg(_(e_toofewarg), "[reference]");
1606 return FAIL;
1607 }
1608 if (!varargs && argcount > type->tt_argcount)
1609 {
1610 semsg(_(e_toomanyarg), "[reference]");
1611 return FAIL;
1612 }
1613 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001614 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001615 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001616 else
1617 {
1618 semsg(_("E1085: Not a callable type: %s"), name);
1619 return FAIL;
1620 }
1621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1623 return FAIL;
1624 isn->isn_arg.pfunc.cpf_top = at_top;
1625 isn->isn_arg.pfunc.cpf_argcount = argcount;
1626
1627 stack->ga_len -= argcount; // drop the arguments
1628
1629 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001630 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001632 // If partial is above the arguments it must be cleared and replaced with
1633 // the return value.
1634 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1635 return FAIL;
1636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637 return OK;
1638}
1639
1640/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001641 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 */
1643 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001644generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645{
1646 isn_T *isn;
1647 garray_T *stack = &cctx->ctx_type_stack;
1648 type_T *type;
1649
Bram Moolenaar080457c2020-03-03 21:53:32 +01001650 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001651 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001653 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001655 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001657 if (type->tt_type != VAR_DICT && type != &t_any)
1658 {
1659 emsg(_(e_dictreq));
1660 return FAIL;
1661 }
1662 // change dict type to dict member type
1663 if (type->tt_type == VAR_DICT)
1664 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665
1666 return OK;
1667}
1668
1669/*
1670 * Generate an ISN_ECHO instruction.
1671 */
1672 static int
1673generate_ECHO(cctx_T *cctx, int with_white, int count)
1674{
1675 isn_T *isn;
1676
Bram Moolenaar080457c2020-03-03 21:53:32 +01001677 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1679 return FAIL;
1680 isn->isn_arg.echo.echo_with_white = with_white;
1681 isn->isn_arg.echo.echo_count = count;
1682
1683 return OK;
1684}
1685
Bram Moolenaarad39c092020-02-26 18:23:43 +01001686/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001687 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001688 */
1689 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001690generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001691{
1692 isn_T *isn;
1693
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001694 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001695 return FAIL;
1696 isn->isn_arg.number = count;
1697
1698 return OK;
1699}
1700
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 static int
1702generate_EXEC(cctx_T *cctx, char_u *line)
1703{
1704 isn_T *isn;
1705
Bram Moolenaar080457c2020-03-03 21:53:32 +01001706 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1708 return FAIL;
1709 isn->isn_arg.string = vim_strsave(line);
1710 return OK;
1711}
1712
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001713 static int
1714generate_EXECCONCAT(cctx_T *cctx, int count)
1715{
1716 isn_T *isn;
1717
1718 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1719 return FAIL;
1720 isn->isn_arg.number = count;
1721 return OK;
1722}
1723
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724/*
1725 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001726 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001728 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001729reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1730{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731 lvar_T *lvar;
1732
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001733 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001735 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001736 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 }
1738
1739 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001740 return NULL;
1741 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001743 // Every local variable uses the next entry on the stack. We could re-use
1744 // the last ones when leaving a scope, but then variables used in a closure
1745 // might get overwritten. To keep things simple do not re-use stack
1746 // entries. This is less efficient, but memory is cheap these days.
1747 lvar->lv_idx = cctx->ctx_locals_count++;
1748
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001749 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 lvar->lv_const = isConst;
1751 lvar->lv_type = type;
1752
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001753 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754}
1755
1756/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001757 * Remove local variables above "new_top".
1758 */
1759 static void
1760unwind_locals(cctx_T *cctx, int new_top)
1761{
1762 if (cctx->ctx_locals.ga_len > new_top)
1763 {
1764 int idx;
1765 lvar_T *lvar;
1766
1767 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1768 {
1769 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1770 vim_free(lvar->lv_name);
1771 }
1772 }
1773 cctx->ctx_locals.ga_len = new_top;
1774}
1775
1776/*
1777 * Free all local variables.
1778 */
1779 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001780free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001781{
1782 unwind_locals(cctx, 0);
1783 ga_clear(&cctx->ctx_locals);
1784}
1785
1786/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 * Skip over a type definition and return a pointer to just after it.
1788 */
1789 char_u *
1790skip_type(char_u *start)
1791{
1792 char_u *p = start;
1793
1794 while (ASCII_ISALNUM(*p) || *p == '_')
1795 ++p;
1796
1797 // Skip over "<type>"; this is permissive about white space.
1798 if (*skipwhite(p) == '<')
1799 {
1800 p = skipwhite(p);
1801 p = skip_type(skipwhite(p + 1));
1802 p = skipwhite(p);
1803 if (*p == '>')
1804 ++p;
1805 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001806 else if (*p == '(' && STRNCMP("func", start, 4) == 0)
1807 {
1808 // handle func(args): type
1809 ++p;
1810 while (*p != ')' && *p != NUL)
1811 {
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001812 char_u *sp = p;
1813
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001814 p = skip_type(p);
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001815 if (p == sp)
1816 return p; // syntax error
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001817 if (*p == ',')
1818 p = skipwhite(p + 1);
1819 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001820 if (*p == ')')
1821 {
1822 if (p[1] == ':')
1823 p = skip_type(skipwhite(p + 2));
1824 else
1825 p = skipwhite(p + 1);
1826 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001827 }
1828
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 return p;
1830}
1831
1832/*
1833 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02001834 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 * Returns NULL in case of failure.
1836 */
1837 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001838parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839{
1840 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001841 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842
1843 if (**arg != '<')
1844 {
1845 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02001846 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 else
1848 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001849 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 }
1851 *arg = skipwhite(*arg + 1);
1852
Bram Moolenaard77a8522020-04-03 21:59:57 +02001853 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854
1855 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001856 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 {
1858 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01001859 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 }
1861 ++*arg;
1862
1863 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02001864 return get_list_type(member_type, type_gap);
1865 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866}
1867
1868/*
1869 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02001870 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001871 */
1872 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02001873parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874{
1875 char_u *p = *arg;
1876 size_t len;
1877
1878 // skip over the first word
1879 while (ASCII_ISALNUM(*p) || *p == '_')
1880 ++p;
1881 len = p - *arg;
1882
1883 switch (**arg)
1884 {
1885 case 'a':
1886 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
1887 {
1888 *arg += len;
1889 return &t_any;
1890 }
1891 break;
1892 case 'b':
1893 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
1894 {
1895 *arg += len;
1896 return &t_bool;
1897 }
1898 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
1899 {
1900 *arg += len;
1901 return &t_blob;
1902 }
1903 break;
1904 case 'c':
1905 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
1906 {
1907 *arg += len;
1908 return &t_channel;
1909 }
1910 break;
1911 case 'd':
1912 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
1913 {
1914 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001915 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 }
1917 break;
1918 case 'f':
1919 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
1920 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01001921#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 *arg += len;
1923 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01001924#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001925 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01001926 return &t_any;
1927#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 }
1929 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
1930 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001931 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02001932 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001933 int argcount = -1;
1934 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001935 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001936 type_T *arg_type[MAX_FUNC_ARGS + 1];
1937
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001938 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001940 if (**arg == '(')
1941 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001942 // "func" may or may not return a value, "func()" does
1943 // not return a value.
1944 ret_type = &t_void;
1945
Bram Moolenaard77a8522020-04-03 21:59:57 +02001946 p = ++*arg;
1947 argcount = 0;
1948 while (*p != NUL && *p != ')')
1949 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001950 if (*p == '?')
1951 {
1952 if (first_optional == -1)
1953 first_optional = argcount;
1954 ++p;
1955 }
1956 else if (first_optional != -1)
1957 {
1958 emsg(_("E1007: mandatory argument after optional argument"));
1959 return &t_any;
1960 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001961 else if (STRNCMP(p, "...", 3) == 0)
1962 {
1963 flags |= TTFLAG_VARARGS;
1964 p += 3;
1965 }
1966
1967 arg_type[argcount++] = parse_type(&p, type_gap);
1968
1969 // Nothing comes after "...{type}".
1970 if (flags & TTFLAG_VARARGS)
1971 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001972
Bram Moolenaard77a8522020-04-03 21:59:57 +02001973 if (*p != ',' && *skipwhite(p) == ',')
1974 {
1975 semsg(_(e_no_white_before), ",");
1976 return &t_any;
1977 }
1978 if (*p == ',')
1979 {
1980 ++p;
1981 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001982 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02001983 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02001984 return &t_any;
1985 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02001986 }
1987 p = skipwhite(p);
1988 if (argcount == MAX_FUNC_ARGS)
1989 {
1990 emsg(_("E740: Too many argument types"));
1991 return &t_any;
1992 }
1993 }
1994
1995 p = skipwhite(p);
1996 if (*p != ')')
1997 {
1998 emsg(_(e_missing_close));
1999 return &t_any;
2000 }
2001 *arg = p + 1;
2002 }
2003 if (**arg == ':')
2004 {
2005 // parse return type
2006 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002007 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002008 semsg(_(e_white_after), ":");
2009 *arg = skipwhite(*arg);
2010 ret_type = parse_type(arg, type_gap);
2011 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002012 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002013 type = get_func_type(ret_type, argcount, type_gap);
2014 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002015 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002016 type = alloc_func_type(ret_type, argcount, type_gap);
2017 type->tt_flags = flags;
2018 if (argcount > 0)
2019 {
2020 type->tt_argcount = argcount;
2021 type->tt_min_argcount = first_optional == -1
2022 ? argcount : first_optional;
2023 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002024 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002025 return &t_any;
2026 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002027 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002028 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002029 }
2030 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 }
2032 break;
2033 case 'j':
2034 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2035 {
2036 *arg += len;
2037 return &t_job;
2038 }
2039 break;
2040 case 'l':
2041 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2042 {
2043 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002044 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 }
2046 break;
2047 case 'n':
2048 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2049 {
2050 *arg += len;
2051 return &t_number;
2052 }
2053 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054 case 's':
2055 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2056 {
2057 *arg += len;
2058 return &t_string;
2059 }
2060 break;
2061 case 'v':
2062 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2063 {
2064 *arg += len;
2065 return &t_void;
2066 }
2067 break;
2068 }
2069
2070 semsg(_("E1010: Type not recognized: %s"), *arg);
2071 return &t_any;
2072}
2073
2074/*
2075 * Check if "type1" and "type2" are exactly the same.
2076 */
2077 static int
2078equal_type(type_T *type1, type_T *type2)
2079{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002080 int i;
2081
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002082 if (type1->tt_type != type2->tt_type)
2083 return FALSE;
2084 switch (type1->tt_type)
2085 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002087 case VAR_ANY:
2088 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 case VAR_SPECIAL:
2090 case VAR_BOOL:
2091 case VAR_NUMBER:
2092 case VAR_FLOAT:
2093 case VAR_STRING:
2094 case VAR_BLOB:
2095 case VAR_JOB:
2096 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002097 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 case VAR_LIST:
2099 case VAR_DICT:
2100 return equal_type(type1->tt_member, type2->tt_member);
2101 case VAR_FUNC:
2102 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002103 if (!equal_type(type1->tt_member, type2->tt_member)
2104 || type1->tt_argcount != type2->tt_argcount)
2105 return FALSE;
2106 if (type1->tt_argcount < 0
2107 || type1->tt_args == NULL || type2->tt_args == NULL)
2108 return TRUE;
2109 for (i = 0; i < type1->tt_argcount; ++i)
2110 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2111 return FALSE;
2112 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 }
2114 return TRUE;
2115}
2116
2117/*
2118 * Find the common type of "type1" and "type2" and put it in "dest".
2119 * "type2" and "dest" may be the same.
2120 */
2121 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002122common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123{
2124 if (equal_type(type1, type2))
2125 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002126 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127 return;
2128 }
2129
2130 if (type1->tt_type == type2->tt_type)
2131 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2133 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002134 type_T *common;
2135
Bram Moolenaard77a8522020-04-03 21:59:57 +02002136 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002137 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002138 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002139 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002140 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 return;
2142 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002143 if (type1->tt_type == VAR_FUNC)
2144 {
2145 type_T *common;
2146
2147 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2148 if (type1->tt_argcount == type2->tt_argcount
2149 && type1->tt_argcount >= 0)
2150 {
2151 int argcount = type1->tt_argcount;
2152 int i;
2153
2154 *dest = alloc_func_type(common, argcount, type_gap);
2155 if (type1->tt_args != NULL && type2->tt_args != NULL)
2156 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002157 if (func_type_add_arg_types(*dest, argcount,
2158 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002159 for (i = 0; i < argcount; ++i)
2160 common_type(type1->tt_args[i], type2->tt_args[i],
2161 &(*dest)->tt_args[i], type_gap);
2162 }
2163 }
2164 else
2165 *dest = alloc_func_type(common, -1, type_gap);
2166 return;
2167 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 }
2169
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002170 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171}
2172
2173 char *
2174vartype_name(vartype_T type)
2175{
2176 switch (type)
2177 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002178 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002179 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 case VAR_SPECIAL: return "special";
2182 case VAR_BOOL: return "bool";
2183 case VAR_NUMBER: return "number";
2184 case VAR_FLOAT: return "float";
2185 case VAR_STRING: return "string";
2186 case VAR_BLOB: return "blob";
2187 case VAR_JOB: return "job";
2188 case VAR_CHANNEL: return "channel";
2189 case VAR_LIST: return "list";
2190 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002191
2192 case VAR_FUNC:
2193 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002195 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196}
2197
2198/*
2199 * Return the name of a type.
2200 * The result may be in allocated memory, in which case "tofree" is set.
2201 */
2202 char *
2203type_name(type_T *type, char **tofree)
2204{
2205 char *name = vartype_name(type->tt_type);
2206
2207 *tofree = NULL;
2208 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2209 {
2210 char *member_free;
2211 char *member_name = type_name(type->tt_member, &member_free);
2212 size_t len;
2213
2214 len = STRLEN(name) + STRLEN(member_name) + 3;
2215 *tofree = alloc(len);
2216 if (*tofree != NULL)
2217 {
2218 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2219 vim_free(member_free);
2220 return *tofree;
2221 }
2222 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002223 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002224 {
2225 garray_T ga;
2226 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002227 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002228
2229 ga_init2(&ga, 1, 100);
2230 if (ga_grow(&ga, 20) == FAIL)
2231 return "[unknown]";
2232 *tofree = ga.ga_data;
2233 STRCPY(ga.ga_data, "func(");
2234 ga.ga_len += 5;
2235
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002236 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002237 {
2238 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002239 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002240 int len;
2241
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002242 if (type->tt_args == NULL)
2243 arg_type = "[unknown]";
2244 else
2245 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002246 if (i > 0)
2247 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002248 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002249 ga.ga_len += 2;
2250 }
2251 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002252 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002253 {
2254 vim_free(arg_free);
2255 return "[unknown]";
2256 }
2257 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002258 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002259 {
2260 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2261 ga.ga_len += 3;
2262 }
2263 else if (i >= type->tt_min_argcount)
2264 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002265 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002266 ga.ga_len += len;
2267 vim_free(arg_free);
2268 }
2269
2270 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002271 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002272 else
2273 {
2274 char *ret_free;
2275 char *ret_name = type_name(type->tt_member, &ret_free);
2276 int len;
2277
2278 len = (int)STRLEN(ret_name) + 4;
2279 if (ga_grow(&ga, len) == FAIL)
2280 {
2281 vim_free(ret_free);
2282 return "[unknown]";
2283 }
2284 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002285 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2286 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002287 vim_free(ret_free);
2288 }
2289 return ga.ga_data;
2290 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002291
2292 return name;
2293}
2294
2295/*
2296 * Find "name" in script-local items of script "sid".
2297 * Returns the index in "sn_var_vals" if found.
2298 * If found but not in "sn_var_vals" returns -1.
2299 * If not found returns -2.
2300 */
2301 int
2302get_script_item_idx(int sid, char_u *name, int check_writable)
2303{
2304 hashtab_T *ht;
2305 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002306 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 int idx;
2308
2309 // First look the name up in the hashtable.
2310 if (sid <= 0 || sid > script_items.ga_len)
2311 return -1;
2312 ht = &SCRIPT_VARS(sid);
2313 di = find_var_in_ht(ht, 0, name, TRUE);
2314 if (di == NULL)
2315 return -2;
2316
2317 // Now find the svar_T index in sn_var_vals.
2318 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2319 {
2320 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2321
2322 if (sv->sv_tv == &di->di_tv)
2323 {
2324 if (check_writable && sv->sv_const)
2325 semsg(_(e_readonlyvar), name);
2326 return idx;
2327 }
2328 }
2329 return -1;
2330}
2331
2332/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002333 * Find "name" in imported items of the current script or in "cctx" if not
2334 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335 */
2336 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002337find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002338{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002339 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 int idx;
2341
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002342 if (current_sctx.sc_sid <= 0)
2343 return NULL;
2344 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 if (cctx != NULL)
2346 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2347 {
2348 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2349 + idx;
2350
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002351 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2352 : STRLEN(import->imp_name) == len
2353 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 return import;
2355 }
2356
2357 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2358 {
2359 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2360
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002361 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2362 : STRLEN(import->imp_name) == len
2363 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 return import;
2365 }
2366 return NULL;
2367}
2368
2369/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002370 * Free all imported variables.
2371 */
2372 static void
2373free_imported(cctx_T *cctx)
2374{
2375 int idx;
2376
2377 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2378 {
2379 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2380
2381 vim_free(import->imp_name);
2382 }
2383 ga_clear(&cctx->ctx_imports);
2384}
2385
2386/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002387 * Return TRUE if "p" points at a "#" but not at "#{".
2388 */
2389 static int
2390comment_start(char_u *p)
2391{
2392 return p[0] == '#' && p[1] != '{';
2393}
2394
2395/*
2396 * Return a pointer to the next line that isn't empty or only contains a
2397 * comment. Skips over white space.
2398 * Returns NULL if there is none.
2399 */
2400 static char_u *
2401peek_next_line(cctx_T *cctx)
2402{
2403 int lnum = cctx->ctx_lnum;
2404
2405 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2406 {
2407 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002408 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002409
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002410 if (line == NULL)
2411 break;
2412 p = skipwhite(line);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002413 if (*p != NUL && !comment_start(p))
2414 return p;
2415 }
2416 return NULL;
2417}
2418
2419/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002420 * Called when checking for a following operator at "arg". When the rest of
2421 * the line is empty or only a comment, peek the next line. If there is a next
2422 * line return a pointer to it and set "nextp".
2423 * Otherwise skip over white space.
2424 */
2425 static char_u *
2426may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2427{
2428 char_u *p = skipwhite(arg);
2429
2430 *nextp = NULL;
2431 if (*p == NUL || (VIM_ISWHITE(*arg) && comment_start(p)))
2432 {
2433 *nextp = peek_next_line(cctx);
2434 if (*nextp != NULL)
2435 return *nextp;
2436 }
2437 return p;
2438}
2439
2440/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002441 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002442 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002443 * Returns NULL when at the end.
2444 */
2445 static char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002446next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002447{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002448 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002449
2450 do
2451 {
2452 ++cctx->ctx_lnum;
2453 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002454 {
2455 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002456 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002457 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002458 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002459 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002460 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002461 } while (line == NULL || *skipwhite(line) == NUL
2462 || (skip_comment && comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002463 return line;
2464}
2465
2466/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002467 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002468 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002469 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2470 */
2471 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002472may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002473{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002474 if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002475 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002476 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002477
2478 if (next == NULL)
2479 return FAIL;
2480 *arg = skipwhite(next);
2481 }
2482 return OK;
2483}
2484
Bram Moolenaara5565e42020-05-09 15:44:01 +02002485// Structure passed between the compile_expr* functions to keep track of
2486// constants that have been parsed but for which no code was produced yet. If
2487// possible expressions on these constants are applied at compile time. If
2488// that is not possible, the code to push the constants needs to be generated
2489// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002490// Using 50 should be more than enough of 5 levels of ().
2491#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002492typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002493 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002494 int pp_used; // active entries in pp_tv[]
2495} ppconst_T;
2496
Bram Moolenaar1c747212020-05-09 18:28:34 +02002497static int compile_expr0(char_u **arg, cctx_T *cctx);
2498static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2499
Bram Moolenaara5565e42020-05-09 15:44:01 +02002500/*
2501 * Generate a PUSH instruction for "tv".
2502 * "tv" will be consumed or cleared.
2503 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2504 */
2505 static int
2506generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2507{
2508 if (tv != NULL)
2509 {
2510 switch (tv->v_type)
2511 {
2512 case VAR_UNKNOWN:
2513 break;
2514 case VAR_BOOL:
2515 generate_PUSHBOOL(cctx, tv->vval.v_number);
2516 break;
2517 case VAR_SPECIAL:
2518 generate_PUSHSPEC(cctx, tv->vval.v_number);
2519 break;
2520 case VAR_NUMBER:
2521 generate_PUSHNR(cctx, tv->vval.v_number);
2522 break;
2523#ifdef FEAT_FLOAT
2524 case VAR_FLOAT:
2525 generate_PUSHF(cctx, tv->vval.v_float);
2526 break;
2527#endif
2528 case VAR_BLOB:
2529 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2530 tv->vval.v_blob = NULL;
2531 break;
2532 case VAR_STRING:
2533 generate_PUSHS(cctx, tv->vval.v_string);
2534 tv->vval.v_string = NULL;
2535 break;
2536 default:
2537 iemsg("constant type not supported");
2538 clear_tv(tv);
2539 return FAIL;
2540 }
2541 tv->v_type = VAR_UNKNOWN;
2542 }
2543 return OK;
2544}
2545
2546/*
2547 * Generate code for any ppconst entries.
2548 */
2549 static int
2550generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2551{
2552 int i;
2553 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002554 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002555
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002556 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002557 for (i = 0; i < ppconst->pp_used; ++i)
2558 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2559 ret = FAIL;
2560 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002561 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002562 return ret;
2563}
2564
2565/*
2566 * Clear ppconst constants. Used when failing.
2567 */
2568 static void
2569clear_ppconst(ppconst_T *ppconst)
2570{
2571 int i;
2572
2573 for (i = 0; i < ppconst->pp_used; ++i)
2574 clear_tv(&ppconst->pp_tv[i]);
2575 ppconst->pp_used = 0;
2576}
2577
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002578/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002579 * Generate an instruction to load script-local variable "name", without the
2580 * leading "s:".
2581 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 */
2583 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002584compile_load_scriptvar(
2585 cctx_T *cctx,
2586 char_u *name, // variable NUL terminated
2587 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002588 char_u **end, // end of variable
2589 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002591 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2593 imported_T *import;
2594
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002595 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002597 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002598 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2599 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 }
2601 if (idx >= 0)
2602 {
2603 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2604
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002605 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 current_sctx.sc_sid, idx, sv->sv_type);
2607 return OK;
2608 }
2609
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002610 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 if (import != NULL)
2612 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002613 if (import->imp_all)
2614 {
2615 char_u *p = skipwhite(*end);
2616 int name_len;
2617 ufunc_T *ufunc;
2618 type_T *type;
2619
2620 // Used "import * as Name", need to lookup the member.
2621 if (*p != '.')
2622 {
2623 semsg(_("E1060: expected dot after name: %s"), start);
2624 return FAIL;
2625 }
2626 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002627 if (VIM_ISWHITE(*p))
2628 {
2629 emsg(_("E1074: no white space allowed after dot"));
2630 return FAIL;
2631 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002632
2633 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
2634 // TODO: what if it is a function?
2635 if (idx < 0)
2636 return FAIL;
2637 *end = p;
2638
2639 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2640 import->imp_sid,
2641 idx,
2642 type);
2643 }
2644 else
2645 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002646 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002647 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2648 import->imp_sid,
2649 import->imp_var_vals_idx,
2650 import->imp_type);
2651 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652 return OK;
2653 }
2654
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002655 if (error)
2656 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 return FAIL;
2658}
2659
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002660 static int
2661generate_funcref(cctx_T *cctx, char_u *name)
2662{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002663 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002664
2665 if (ufunc == NULL)
2666 return FAIL;
2667
2668 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2669}
2670
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671/*
2672 * Compile a variable name into a load instruction.
2673 * "end" points to just after the name.
2674 * When "error" is FALSE do not give an error when not found.
2675 */
2676 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002677compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002678{
2679 type_T *type;
2680 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002681 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002683 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684
2685 if (*(*arg + 1) == ':')
2686 {
2687 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002688 if (end <= *arg + 2)
2689 name = vim_strsave((char_u *)"[empty]");
2690 else
2691 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 if (name == NULL)
2693 return FAIL;
2694
2695 if (**arg == 'v')
2696 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002697 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698 }
2699 else if (**arg == 'g')
2700 {
2701 // Global variables can be defined later, thus we don't check if it
2702 // exists, give error at runtime.
2703 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2704 }
2705 else if (**arg == 's')
2706 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002707 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002709 else if (**arg == 'b')
2710 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002711 // Buffer-local variables can be defined later, thus we don't check
2712 // if it exists, give error at runtime.
2713 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002714 }
2715 else if (**arg == 'w')
2716 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002717 // Window-local variables can be defined later, thus we don't check
2718 // if it exists, give error at runtime.
2719 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002720 }
2721 else if (**arg == 't')
2722 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002723 // Tabpage-local variables can be defined later, thus we don't
2724 // check if it exists, give error at runtime.
2725 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002726 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 else
2728 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002729 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 goto theend;
2731 }
2732 }
2733 else
2734 {
2735 size_t len = end - *arg;
2736 int idx;
2737 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002738 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739
2740 name = vim_strnsave(*arg, end - *arg);
2741 if (name == NULL)
2742 return FAIL;
2743
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002744 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002746 if (!gen_load_outer)
2747 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 }
2749 else
2750 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002751 lvar_T *lvar = lookup_local(*arg, len, cctx);
2752
2753 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002755 type = lvar->lv_type;
2756 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002757 if (lvar->lv_from_outer)
2758 gen_load_outer = TRUE;
2759 else
2760 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 }
2762 else
2763 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002764 // "var" can be script-local even without using "s:" if it
2765 // already exists.
2766 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2767 == SCRIPT_VERSION_VIM9
2768 || lookup_script(*arg, len) == OK)
2769 res = compile_load_scriptvar(cctx, name, *arg, &end,
2770 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002771
Bram Moolenaara5565e42020-05-09 15:44:01 +02002772 // When the name starts with an uppercase letter or "x:" it
2773 // can be a user defined function.
2774 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2775 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 }
2777 }
2778 if (gen_load)
2779 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002780 if (gen_load_outer)
2781 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 }
2783
2784 *arg = end;
2785
2786theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002787 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 semsg(_(e_var_notfound), name);
2789 vim_free(name);
2790 return res;
2791}
2792
2793/*
2794 * Compile the argument expressions.
2795 * "arg" points to just after the "(" and is advanced to after the ")"
2796 */
2797 static int
2798compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2799{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002800 char_u *p = *arg;
2801 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802
Bram Moolenaare6085c52020-04-12 20:19:16 +02002803 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002805 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2806 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002807 if (*p == ')')
2808 {
2809 *arg = p + 1;
2810 return OK;
2811 }
2812
Bram Moolenaara5565e42020-05-09 15:44:01 +02002813 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814 return FAIL;
2815 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002816
2817 if (*p != ',' && *skipwhite(p) == ',')
2818 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002819 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002820 p = skipwhite(p);
2821 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002823 {
2824 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002825 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002826 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002827 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002828 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002829 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002831failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002832 emsg(_(e_missing_close));
2833 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834}
2835
2836/*
2837 * Compile a function call: name(arg1, arg2)
2838 * "arg" points to "name", "arg + varlen" to the "(".
2839 * "argcount_init" is 1 for "value->method()"
2840 * Instructions:
2841 * EVAL arg1
2842 * EVAL arg2
2843 * BCALL / DCALL / UCALL
2844 */
2845 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002846compile_call(
2847 char_u **arg,
2848 size_t varlen,
2849 cctx_T *cctx,
2850 ppconst_T *ppconst,
2851 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852{
2853 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002854 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 int argcount = argcount_init;
2856 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002857 char_u fname_buf[FLEN_FIXED + 1];
2858 char_u *tofree = NULL;
2859 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002861 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862
Bram Moolenaara5565e42020-05-09 15:44:01 +02002863 // we can evaluate "has('name')" at compile time
2864 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2865 {
2866 char_u *s = skipwhite(*arg + varlen + 1);
2867 typval_T argvars[2];
2868
2869 argvars[0].v_type = VAR_UNKNOWN;
2870 if (*s == '"')
2871 (void)get_string_tv(&s, &argvars[0], TRUE);
2872 else if (*s == '\'')
2873 (void)get_lit_string_tv(&s, &argvars[0], TRUE);
2874 s = skipwhite(s);
2875 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2876 {
2877 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2878
2879 *arg = s + 1;
2880 argvars[1].v_type = VAR_UNKNOWN;
2881 tv->v_type = VAR_NUMBER;
2882 tv->vval.v_number = 0;
2883 f_has(argvars, tv);
2884 clear_tv(&argvars[0]);
2885 ++ppconst->pp_used;
2886 return OK;
2887 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002888 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002889 }
2890
2891 if (generate_ppconst(cctx, ppconst) == FAIL)
2892 return FAIL;
2893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 if (varlen >= sizeof(namebuf))
2895 {
2896 semsg(_("E1011: name too long: %s"), name);
2897 return FAIL;
2898 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002899 vim_strncpy(namebuf, *arg, varlen);
2900 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901
2902 *arg = skipwhite(*arg + varlen + 1);
2903 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002904 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002906 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 {
2908 int idx;
2909
2910 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002911 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002913 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002914 else
2915 semsg(_(e_unknownfunc), namebuf);
2916 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 }
2918
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002920 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002922 {
2923 res = generate_CALL(cctx, ufunc, argcount);
2924 goto theend;
2925 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926
2927 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002928 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002930 if (STRNCMP(namebuf, "g:", 2) != 0
2931 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002932 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002933 garray_T *stack = &cctx->ctx_type_stack;
2934 type_T *type;
2935
2936 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2937 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002938 goto theend;
2939 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002941 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002942 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002943 if (STRNCMP(namebuf, "g:", 2) == 0)
2944 res = generate_UCALL(cctx, name, argcount);
2945 else
2946 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002947
2948theend:
2949 vim_free(tofree);
2950 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951}
2952
2953// like NAMESPACE_CHAR but with 'a' and 'l'.
2954#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2955
2956/*
2957 * Find the end of a variable or function name. Unlike find_name_end() this
2958 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002959 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 * Return a pointer to just after the name. Equal to "arg" if there is no
2961 * valid name.
2962 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002963 static char_u *
2964to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965{
2966 char_u *p;
2967
2968 // Quick check for valid starting character.
2969 if (!eval_isnamec1(*arg))
2970 return arg;
2971
2972 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2973 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2974 // and can be used in slice "[n:]".
2975 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002976 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2978 break;
2979 return p;
2980}
2981
2982/*
2983 * Like to_name_end() but also skip over a list or dict constant.
2984 */
2985 char_u *
2986to_name_const_end(char_u *arg)
2987{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002988 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 typval_T rettv;
2990
2991 if (p == arg && *arg == '[')
2992 {
2993
2994 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar71478202020-06-26 22:46:27 +02002995 if (get_list_tv(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 p = arg;
2997 }
2998 else if (p == arg && *arg == '#' && arg[1] == '{')
2999 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003000 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003002 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 p = arg;
3004 }
3005 else if (p == arg && *arg == '{')
3006 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003007 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003009 // Can be "{x -> ret}()".
3010 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003012 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 if (ret != OK)
3014 p = arg;
3015 }
3016
3017 return p;
3018}
3019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020/*
3021 * parse a list: [expr, expr]
3022 * "*arg" points to the '['.
3023 */
3024 static int
3025compile_list(char_u **arg, cctx_T *cctx)
3026{
3027 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003028 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 int count = 0;
3030
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003031 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003033 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003034 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003035 semsg(_(e_list_end), *arg);
3036 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003037 }
3038 if (*p == ']')
3039 {
3040 ++p;
3041 // Allow for following comment, after at least one space.
3042 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
3043 p += STRLEN(p);
3044 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003045 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003046 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 break;
3048 ++count;
3049 if (*p == ',')
3050 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003051 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 p = skipwhite(p);
3053 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003054 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055
3056 generate_NEWLIST(cctx, count);
3057 return OK;
3058}
3059
3060/*
3061 * parse a lambda: {arg, arg -> expr}
3062 * "*arg" points to the '{'.
3063 */
3064 static int
3065compile_lambda(char_u **arg, cctx_T *cctx)
3066{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 typval_T rettv;
3068 ufunc_T *ufunc;
3069
3070 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003071 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003073
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003075 ++ufunc->uf_refcount;
3076 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003077 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078
3079 // The function will have one line: "return {expr}".
3080 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003081 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003083 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003084 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 return FAIL;
3086}
3087
3088/*
3089 * Compile a lamda call: expr->{lambda}(args)
3090 * "arg" points to the "{".
3091 */
3092 static int
3093compile_lambda_call(char_u **arg, cctx_T *cctx)
3094{
3095 ufunc_T *ufunc;
3096 typval_T rettv;
3097 int argcount = 1;
3098 int ret = FAIL;
3099
3100 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003101 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 return FAIL;
3103
3104 if (**arg != '(')
3105 {
3106 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003107 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 else
3109 semsg(_(e_missing_paren), "lambda");
3110 clear_tv(&rettv);
3111 return FAIL;
3112 }
3113
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 ufunc = rettv.vval.v_partial->pt_func;
3115 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003116 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003117 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003118
3119 // The function will have one line: "return {expr}".
3120 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003121 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122
3123 // compile the arguments
3124 *arg = skipwhite(*arg + 1);
3125 if (compile_arguments(arg, cctx, &argcount) == OK)
3126 // call the compiled function
3127 ret = generate_CALL(cctx, ufunc, argcount);
3128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 return ret;
3130}
3131
3132/*
3133 * parse a dict: {'key': val} or #{key: val}
3134 * "*arg" points to the '{'.
3135 */
3136 static int
3137compile_dict(char_u **arg, cctx_T *cctx, int literal)
3138{
3139 garray_T *instr = &cctx->ctx_instr;
3140 int count = 0;
3141 dict_T *d = dict_alloc();
3142 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003143 char_u *whitep = *arg;
3144 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145
3146 if (d == NULL)
3147 return FAIL;
3148 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003149 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 {
3151 char_u *key = NULL;
3152
Bram Moolenaar23c55272020-06-21 16:58:13 +02003153 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003154 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003155 *arg = NULL;
3156 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003157 }
3158
3159 if (**arg == '}')
3160 break;
3161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 if (literal)
3163 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003164 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165
Bram Moolenaar2c330432020-04-13 14:41:35 +02003166 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 {
3168 semsg(_("E1014: Invalid key: %s"), *arg);
3169 return FAIL;
3170 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003171 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 if (generate_PUSHS(cctx, key) == FAIL)
3173 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003174 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 }
3176 else
3177 {
3178 isn_T *isn;
3179
Bram Moolenaara5565e42020-05-09 15:44:01 +02003180 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 return FAIL;
3182 // TODO: check type is string
3183 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3184 if (isn->isn_type == ISN_PUSHS)
3185 key = isn->isn_arg.string;
3186 }
3187
3188 // Check for duplicate keys, if using string keys.
3189 if (key != NULL)
3190 {
3191 item = dict_find(d, key, -1);
3192 if (item != NULL)
3193 {
3194 semsg(_(e_duplicate_key), key);
3195 goto failret;
3196 }
3197 item = dictitem_alloc(key);
3198 if (item != NULL)
3199 {
3200 item->di_tv.v_type = VAR_UNKNOWN;
3201 item->di_tv.v_lock = 0;
3202 if (dict_add(d, item) == FAIL)
3203 dictitem_free(item);
3204 }
3205 }
3206
3207 *arg = skipwhite(*arg);
3208 if (**arg != ':')
3209 {
3210 semsg(_(e_missing_dict_colon), *arg);
3211 return FAIL;
3212 }
3213
Bram Moolenaar2c330432020-04-13 14:41:35 +02003214 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003216 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003217 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003218 *arg = NULL;
3219 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003220 }
3221
Bram Moolenaara5565e42020-05-09 15:44:01 +02003222 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 return FAIL;
3224 ++count;
3225
Bram Moolenaar2c330432020-04-13 14:41:35 +02003226 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003227 *arg = skipwhite(*arg);
3228 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003229 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003230 *arg = NULL;
3231 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 if (**arg == '}')
3234 break;
3235 if (**arg != ',')
3236 {
3237 semsg(_(e_missing_dict_comma), *arg);
3238 goto failret;
3239 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003240 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 *arg = skipwhite(*arg + 1);
3242 }
3243
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 *arg = *arg + 1;
3245
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003246 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003247 p = skipwhite(*arg);
3248 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003249 *arg += STRLEN(*arg);
3250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 dict_unref(d);
3252 return generate_NEWDICT(cctx, count);
3253
3254failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003255 if (*arg == NULL)
3256 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 dict_unref(d);
3258 return FAIL;
3259}
3260
3261/*
3262 * Compile "&option".
3263 */
3264 static int
3265compile_get_option(char_u **arg, cctx_T *cctx)
3266{
3267 typval_T rettv;
3268 char_u *start = *arg;
3269 int ret;
3270
3271 // parse the option and get the current value to get the type.
3272 rettv.v_type = VAR_UNKNOWN;
3273 ret = get_option_tv(arg, &rettv, TRUE);
3274 if (ret == OK)
3275 {
3276 // include the '&' in the name, get_option_tv() expects it.
3277 char_u *name = vim_strnsave(start, *arg - start);
3278 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3279
3280 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3281 vim_free(name);
3282 }
3283 clear_tv(&rettv);
3284
3285 return ret;
3286}
3287
3288/*
3289 * Compile "$VAR".
3290 */
3291 static int
3292compile_get_env(char_u **arg, cctx_T *cctx)
3293{
3294 char_u *start = *arg;
3295 int len;
3296 int ret;
3297 char_u *name;
3298
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 ++*arg;
3300 len = get_env_len(arg);
3301 if (len == 0)
3302 {
3303 semsg(_(e_syntax_at), start - 1);
3304 return FAIL;
3305 }
3306
3307 // include the '$' in the name, get_env_tv() expects it.
3308 name = vim_strnsave(start, len + 1);
3309 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3310 vim_free(name);
3311 return ret;
3312}
3313
3314/*
3315 * Compile "@r".
3316 */
3317 static int
3318compile_get_register(char_u **arg, cctx_T *cctx)
3319{
3320 int ret;
3321
3322 ++*arg;
3323 if (**arg == NUL)
3324 {
3325 semsg(_(e_syntax_at), *arg - 1);
3326 return FAIL;
3327 }
3328 if (!valid_yank_reg(**arg, TRUE))
3329 {
3330 emsg_invreg(**arg);
3331 return FAIL;
3332 }
3333 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3334 ++*arg;
3335 return ret;
3336}
3337
3338/*
3339 * Apply leading '!', '-' and '+' to constant "rettv".
3340 */
3341 static int
3342apply_leader(typval_T *rettv, char_u *start, char_u *end)
3343{
3344 char_u *p = end;
3345
3346 // this works from end to start
3347 while (p > start)
3348 {
3349 --p;
3350 if (*p == '-' || *p == '+')
3351 {
3352 // only '-' has an effect, for '+' we only check the type
3353#ifdef FEAT_FLOAT
3354 if (rettv->v_type == VAR_FLOAT)
3355 {
3356 if (*p == '-')
3357 rettv->vval.v_float = -rettv->vval.v_float;
3358 }
3359 else
3360#endif
3361 {
3362 varnumber_T val;
3363 int error = FALSE;
3364
3365 // tv_get_number_chk() accepts a string, but we don't want that
3366 // here
3367 if (check_not_string(rettv) == FAIL)
3368 return FAIL;
3369 val = tv_get_number_chk(rettv, &error);
3370 clear_tv(rettv);
3371 if (error)
3372 return FAIL;
3373 if (*p == '-')
3374 val = -val;
3375 rettv->v_type = VAR_NUMBER;
3376 rettv->vval.v_number = val;
3377 }
3378 }
3379 else
3380 {
3381 int v = tv2bool(rettv);
3382
3383 // '!' is permissive in the type.
3384 clear_tv(rettv);
3385 rettv->v_type = VAR_BOOL;
3386 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3387 }
3388 }
3389 return OK;
3390}
3391
3392/*
3393 * Recognize v: variables that are constants and set "rettv".
3394 */
3395 static void
3396get_vim_constant(char_u **arg, typval_T *rettv)
3397{
3398 if (STRNCMP(*arg, "v:true", 6) == 0)
3399 {
3400 rettv->v_type = VAR_BOOL;
3401 rettv->vval.v_number = VVAL_TRUE;
3402 *arg += 6;
3403 }
3404 else if (STRNCMP(*arg, "v:false", 7) == 0)
3405 {
3406 rettv->v_type = VAR_BOOL;
3407 rettv->vval.v_number = VVAL_FALSE;
3408 *arg += 7;
3409 }
3410 else if (STRNCMP(*arg, "v:null", 6) == 0)
3411 {
3412 rettv->v_type = VAR_SPECIAL;
3413 rettv->vval.v_number = VVAL_NULL;
3414 *arg += 6;
3415 }
3416 else if (STRNCMP(*arg, "v:none", 6) == 0)
3417 {
3418 rettv->v_type = VAR_SPECIAL;
3419 rettv->vval.v_number = VVAL_NONE;
3420 *arg += 6;
3421 }
3422}
3423
Bram Moolenaar61a89812020-05-07 16:58:17 +02003424 static exptype_T
3425get_compare_type(char_u *p, int *len, int *type_is)
3426{
3427 exptype_T type = EXPR_UNKNOWN;
3428 int i;
3429
3430 switch (p[0])
3431 {
3432 case '=': if (p[1] == '=')
3433 type = EXPR_EQUAL;
3434 else if (p[1] == '~')
3435 type = EXPR_MATCH;
3436 break;
3437 case '!': if (p[1] == '=')
3438 type = EXPR_NEQUAL;
3439 else if (p[1] == '~')
3440 type = EXPR_NOMATCH;
3441 break;
3442 case '>': if (p[1] != '=')
3443 {
3444 type = EXPR_GREATER;
3445 *len = 1;
3446 }
3447 else
3448 type = EXPR_GEQUAL;
3449 break;
3450 case '<': if (p[1] != '=')
3451 {
3452 type = EXPR_SMALLER;
3453 *len = 1;
3454 }
3455 else
3456 type = EXPR_SEQUAL;
3457 break;
3458 case 'i': if (p[1] == 's')
3459 {
3460 // "is" and "isnot"; but not a prefix of a name
3461 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3462 *len = 5;
3463 i = p[*len];
3464 if (!isalnum(i) && i != '_')
3465 {
3466 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3467 *type_is = TRUE;
3468 }
3469 }
3470 break;
3471 }
3472 return type;
3473}
3474
3475/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 * Compile code to apply '-', '+' and '!'.
3477 */
3478 static int
3479compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3480{
3481 char_u *p = end;
3482
3483 // this works from end to start
3484 while (p > start)
3485 {
3486 --p;
3487 if (*p == '-' || *p == '+')
3488 {
3489 int negate = *p == '-';
3490 isn_T *isn;
3491
3492 // TODO: check type
3493 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3494 {
3495 --p;
3496 if (*p == '-')
3497 negate = !negate;
3498 }
3499 // only '-' has an effect, for '+' we only check the type
3500 if (negate)
3501 isn = generate_instr(cctx, ISN_NEGATENR);
3502 else
3503 isn = generate_instr(cctx, ISN_CHECKNR);
3504 if (isn == NULL)
3505 return FAIL;
3506 }
3507 else
3508 {
3509 int invert = TRUE;
3510
3511 while (p > start && p[-1] == '!')
3512 {
3513 --p;
3514 invert = !invert;
3515 }
3516 if (generate_2BOOL(cctx, invert) == FAIL)
3517 return FAIL;
3518 }
3519 }
3520 return OK;
3521}
3522
3523/*
3524 * Compile whatever comes after "name" or "name()".
3525 */
3526 static int
3527compile_subscript(
3528 char_u **arg,
3529 cctx_T *cctx,
3530 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003531 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003532 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533{
3534 for (;;)
3535 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003536 char_u *p = skipwhite(*arg);
3537
3538 if (*p == NUL || (VIM_ISWHITE(**arg) && comment_start(p)))
3539 {
3540 char_u *next = peek_next_line(cctx);
3541
3542 // If a following line starts with "->{" or "->X" advance to that
3543 // line, so that a line break before "->" is allowed.
3544 if (next != NULL && next[0] == '-' && next[1] == '>'
3545 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3546 {
3547 next = next_line_from_context(cctx, TRUE);
3548 if (next == NULL)
3549 return FAIL;
3550 *arg = skipwhite(next);
3551 }
3552 }
3553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 if (**arg == '(')
3555 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003556 garray_T *stack = &cctx->ctx_type_stack;
3557 type_T *type;
3558 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003560 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003561 return FAIL;
3562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003564 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 *arg = skipwhite(*arg + 1);
3567 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3568 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003569 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 return FAIL;
3571 }
3572 else if (**arg == '-' && (*arg)[1] == '>')
3573 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003574 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003575 return FAIL;
3576
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 // something->method()
3578 // Apply the '!', '-' and '+' first:
3579 // -1.0->func() works like (-1.0)->func()
3580 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3581 return FAIL;
3582 *start_leader = end_leader; // don't apply again later
3583
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003584 p = *arg + 2;
3585 *arg = skipwhite(p);
3586 if (may_get_next_line(p, arg, cctx) == FAIL)
3587 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588 if (**arg == '{')
3589 {
3590 // lambda call: list->{lambda}
3591 if (compile_lambda_call(arg, cctx) == FAIL)
3592 return FAIL;
3593 }
3594 else
3595 {
3596 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003597 p = *arg;
3598 if (ASCII_ISALPHA(*p) && p[1] == ':')
3599 p += 2;
3600 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 ;
3602 if (*p != '(')
3603 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003604 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 return FAIL;
3606 }
3607 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003608 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 return FAIL;
3610 }
3611 }
3612 else if (**arg == '[')
3613 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003614 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003615 type_T **typep;
3616
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003617 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003618 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003619 // TODO: blob index
3620 // TODO: more arguments
3621 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003622 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003623 return FAIL;
3624
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003625 p = *arg + 1;
3626 *arg = skipwhite(p);
3627 if (may_get_next_line(p, arg, cctx) == FAIL)
3628 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003629 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 return FAIL;
3631
3632 if (**arg != ']')
3633 {
3634 emsg(_(e_missbrac));
3635 return FAIL;
3636 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003637 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003639 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3640 if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003641 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003642 if ((*typep)->tt_type == VAR_LIST)
3643 *typep = (*typep)->tt_member;
3644 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3645 return FAIL;
3646 }
3647 else if ((*typep)->tt_type == VAR_DICT)
3648 {
3649 *typep = (*typep)->tt_member;
3650 if (may_generate_2STRING(-1, cctx) == FAIL)
3651 return FAIL;
3652 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3653 return FAIL;
3654 }
3655 else
3656 {
3657 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003658 return FAIL;
3659 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 }
3661 else if (**arg == '.' && (*arg)[1] != '.')
3662 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003663 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003664 return FAIL;
3665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 ++*arg;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003667 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3668 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003670 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 if (eval_isnamec1(*p))
3672 while (eval_isnamec(*p))
3673 MB_PTR_ADV(p);
3674 if (p == *arg)
3675 {
3676 semsg(_(e_syntax_at), *arg);
3677 return FAIL;
3678 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003679 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 return FAIL;
3681 *arg = p;
3682 }
3683 else
3684 break;
3685 }
3686
3687 // TODO - see handle_subscript():
3688 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3689 // Don't do this when "Func" is already a partial that was bound
3690 // explicitly (pt_auto is FALSE).
3691
3692 return OK;
3693}
3694
3695/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003696 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3697 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003699 * If the value is a constant "ppconst->pp_ret" will be set.
3700 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003701 *
3702 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 */
3704
3705/*
3706 * number number constant
3707 * 0zFFFFFFFF Blob constant
3708 * "string" string constant
3709 * 'string' literal string constant
3710 * &option-name option value
3711 * @r register contents
3712 * identifier variable value
3713 * function() function call
3714 * $VAR environment variable
3715 * (expression) nested expression
3716 * [expr, expr] List
3717 * {key: val, key: val} Dictionary
3718 * #{key: val, key: val} Dictionary with literal keys
3719 *
3720 * Also handle:
3721 * ! in front logical NOT
3722 * - in front unary minus
3723 * + in front unary plus (ignored)
3724 * trailing (arg) funcref/partial call
3725 * trailing [] subscript in String or List
3726 * trailing .name entry in Dictionary
3727 * trailing ->name() method call
3728 */
3729 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003730compile_expr7(
3731 char_u **arg,
3732 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003733 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 char_u *start_leader, *end_leader;
3736 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003737 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003738 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739
3740 /*
3741 * Skip '!', '-' and '+' characters. They are handled later.
3742 */
3743 start_leader = *arg;
3744 while (**arg == '!' || **arg == '-' || **arg == '+')
3745 *arg = skipwhite(*arg + 1);
3746 end_leader = *arg;
3747
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003748 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 switch (**arg)
3750 {
3751 /*
3752 * Number constant.
3753 */
3754 case '0': // also for blob starting with 0z
3755 case '1':
3756 case '2':
3757 case '3':
3758 case '4':
3759 case '5':
3760 case '6':
3761 case '7':
3762 case '8':
3763 case '9':
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003764 case '.': if (get_number_tv(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 return FAIL;
3766 break;
3767
3768 /*
3769 * String constant: "string".
3770 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003771 case '"': if (get_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 return FAIL;
3773 break;
3774
3775 /*
3776 * Literal string constant: 'str''ing'.
3777 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003778 case '\'': if (get_lit_string_tv(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 return FAIL;
3780 break;
3781
3782 /*
3783 * Constant Vim variable.
3784 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003785 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 ret = NOTDONE;
3787 break;
3788
3789 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003790 * "true" constant
3791 */
3792 case 't': if (STRNCMP(*arg, "true", 4) == 0
3793 && !eval_isnamec((*arg)[4]))
3794 {
3795 *arg += 4;
3796 rettv->v_type = VAR_BOOL;
3797 rettv->vval.v_number = VVAL_TRUE;
3798 }
3799 else
3800 ret = NOTDONE;
3801 break;
3802
3803 /*
3804 * "false" constant
3805 */
3806 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3807 && !eval_isnamec((*arg)[5]))
3808 {
3809 *arg += 5;
3810 rettv->v_type = VAR_BOOL;
3811 rettv->vval.v_number = VVAL_FALSE;
3812 }
3813 else
3814 ret = NOTDONE;
3815 break;
3816
3817 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818 * List: [expr, expr]
3819 */
3820 case '[': ret = compile_list(arg, cctx);
3821 break;
3822
3823 /*
3824 * Dictionary: #{key: val, key: val}
3825 */
3826 case '#': if ((*arg)[1] == '{')
3827 {
3828 ++*arg;
3829 ret = compile_dict(arg, cctx, TRUE);
3830 }
3831 else
3832 ret = NOTDONE;
3833 break;
3834
3835 /*
3836 * Lambda: {arg, arg -> expr}
3837 * Dictionary: {'key': val, 'key': val}
3838 */
3839 case '{': {
3840 char_u *start = skipwhite(*arg + 1);
3841
3842 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003843 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003845 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 if (ret != FAIL && *start == '>')
3847 ret = compile_lambda(arg, cctx);
3848 else
3849 ret = compile_dict(arg, cctx, FALSE);
3850 }
3851 break;
3852
3853 /*
3854 * Option value: &name
3855 */
3856 case '&': ret = compile_get_option(arg, cctx);
3857 break;
3858
3859 /*
3860 * Environment variable: $VAR.
3861 */
3862 case '$': ret = compile_get_env(arg, cctx);
3863 break;
3864
3865 /*
3866 * Register contents: @r.
3867 */
3868 case '@': ret = compile_get_register(arg, cctx);
3869 break;
3870 /*
3871 * nested expression: (expression).
3872 */
3873 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003874
3875 // recursive!
3876 if (ppconst->pp_used <= PPSIZE - 10)
3877 {
3878 ret = compile_expr1(arg, cctx, ppconst);
3879 }
3880 else
3881 {
3882 // Not enough space in ppconst, flush constants.
3883 if (generate_ppconst(cctx, ppconst) == FAIL)
3884 return FAIL;
3885 ret = compile_expr0(arg, cctx);
3886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 *arg = skipwhite(*arg);
3888 if (**arg == ')')
3889 ++*arg;
3890 else if (ret == OK)
3891 {
3892 emsg(_(e_missing_close));
3893 ret = FAIL;
3894 }
3895 break;
3896
3897 default: ret = NOTDONE;
3898 break;
3899 }
3900 if (ret == FAIL)
3901 return FAIL;
3902
Bram Moolenaar1c747212020-05-09 18:28:34 +02003903 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 {
3905 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003906 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003908 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 return FAIL;
3910 }
3911 start_leader = end_leader; // don't apply again below
3912
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003913 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003914 clear_tv(rettv);
3915 else
3916 // A constant expression can possibly be handled compile time,
3917 // return the value instead of generating code.
3918 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 }
3920 else if (ret == NOTDONE)
3921 {
3922 char_u *p;
3923 int r;
3924
3925 if (!eval_isnamec1(**arg))
3926 {
3927 semsg(_("E1015: Name expected: %s"), *arg);
3928 return FAIL;
3929 }
3930
3931 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003932 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003934 {
3935 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3936 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003938 {
3939 if (generate_ppconst(cctx, ppconst) == FAIL)
3940 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003942 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 if (r == FAIL)
3944 return FAIL;
3945 }
3946
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003947 // Handle following "[]", ".member", etc.
3948 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003949 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003950 ppconst) == FAIL)
3951 return FAIL;
3952 if (ppconst->pp_used > 0)
3953 {
3954 // apply the '!', '-' and '+' before the constant
3955 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3956 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
3957 return FAIL;
3958 return OK;
3959 }
3960 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003962 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963}
3964
3965/*
3966 * * number multiplication
3967 * / number division
3968 * % number modulo
3969 */
3970 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003971compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972{
3973 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003974 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003975 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003977 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003978 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 return FAIL;
3980
3981 /*
3982 * Repeat computing, until no "*", "/" or "%" is following.
3983 */
3984 for (;;)
3985 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003986 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 if (*op != '*' && *op != '/' && *op != '%')
3988 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003989 if (next != NULL)
3990 {
3991 *arg = next_line_from_context(cctx, TRUE);
3992 op = skipwhite(*arg);
3993 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003994
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003995 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 {
3997 char_u buf[3];
3998
3999 vim_strncpy(buf, op, 1);
4000 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004001 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 }
4003 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004004 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004005 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004007 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004008 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004010
4011 if (ppconst->pp_used == ppconst_used + 2
4012 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4013 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004014 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004015 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4016 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004017 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004019 // both are numbers: compute the result
4020 switch (*op)
4021 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004022 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004023 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004024 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004025 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004026 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004027 break;
4028 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004029 tv1->vval.v_number = res;
4030 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004031 }
4032 else
4033 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004034 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004035 generate_two_op(cctx, op);
4036 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 }
4038
4039 return OK;
4040}
4041
4042/*
4043 * + number addition
4044 * - number subtraction
4045 * .. string concatenation
4046 */
4047 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004048compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049{
4050 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004051 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004053 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054
4055 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004056 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 return FAIL;
4058
4059 /*
4060 * Repeat computing, until no "+", "-" or ".." is following.
4061 */
4062 for (;;)
4063 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004064 op = may_peek_next_line(cctx, *arg, &next);
4065 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066 break;
4067 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004068 if (next != NULL)
4069 {
4070 *arg = next_line_from_context(cctx, TRUE);
4071 op = skipwhite(*arg);
4072 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004074 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 {
4076 char_u buf[3];
4077
4078 vim_strncpy(buf, op, oplen);
4079 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004080 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 }
4082
4083 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004084 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004085 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004087 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004088 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 return FAIL;
4090
Bram Moolenaara5565e42020-05-09 15:44:01 +02004091 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004092 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004093 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4094 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4095 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4096 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004098 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4099 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004100
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004101 // concat/subtract/add constant numbers
4102 if (*op == '+')
4103 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4104 else if (*op == '-')
4105 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4106 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004107 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004108 // concatenate constant strings
4109 char_u *s1 = tv1->vval.v_string;
4110 char_u *s2 = tv2->vval.v_string;
4111 size_t len1 = STRLEN(s1);
4112
4113 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4114 if (tv1->vval.v_string == NULL)
4115 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004116 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004117 return FAIL;
4118 }
4119 mch_memmove(tv1->vval.v_string, s1, len1);
4120 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004121 vim_free(s1);
4122 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004123 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004124 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 }
4126 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004127 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004128 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004129 if (*op == '.')
4130 {
4131 if (may_generate_2STRING(-2, cctx) == FAIL
4132 || may_generate_2STRING(-1, cctx) == FAIL)
4133 return FAIL;
4134 generate_instr_drop(cctx, ISN_CONCAT, 1);
4135 }
4136 else
4137 generate_two_op(cctx, op);
4138 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 }
4140
4141 return OK;
4142}
4143
4144/*
4145 * expr5a == expr5b
4146 * expr5a =~ expr5b
4147 * expr5a != expr5b
4148 * expr5a !~ expr5b
4149 * expr5a > expr5b
4150 * expr5a >= expr5b
4151 * expr5a < expr5b
4152 * expr5a <= expr5b
4153 * expr5a is expr5b
4154 * expr5a isnot expr5b
4155 *
4156 * Produces instructions:
4157 * EVAL expr5a Push result of "expr5a"
4158 * EVAL expr5b Push result of "expr5b"
4159 * COMPARE one of the compare instructions
4160 */
4161 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004162compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163{
4164 exptype_T type = EXPR_UNKNOWN;
4165 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004166 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004169 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170
4171 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004172 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 return FAIL;
4174
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004175 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004176 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177
4178 /*
4179 * If there is a comparative operator, use it.
4180 */
4181 if (type != EXPR_UNKNOWN)
4182 {
4183 int ic = FALSE; // Default: do not ignore case
4184
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004185 if (next != NULL)
4186 {
4187 *arg = next_line_from_context(cctx, TRUE);
4188 p = skipwhite(*arg);
4189 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 if (type_is && (p[len] == '?' || p[len] == '#'))
4191 {
4192 semsg(_(e_invexpr2), *arg);
4193 return FAIL;
4194 }
4195 // extra question mark appended: ignore case
4196 if (p[len] == '?')
4197 {
4198 ic = TRUE;
4199 ++len;
4200 }
4201 // extra '#' appended: match case (ignored)
4202 else if (p[len] == '#')
4203 ++len;
4204 // nothing appended: match case
4205
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004206 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 {
4208 char_u buf[7];
4209
4210 vim_strncpy(buf, p, len);
4211 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004212 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 }
4214
4215 // get the second variable
4216 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004217 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004218 return FAIL;
4219
Bram Moolenaara5565e42020-05-09 15:44:01 +02004220 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221 return FAIL;
4222
Bram Moolenaara5565e42020-05-09 15:44:01 +02004223 if (ppconst->pp_used == ppconst_used + 2)
4224 {
4225 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4226 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4227 int ret;
4228
4229 // Both sides are a constant, compute the result now.
4230 // First check for a valid combination of types, this is more
4231 // strict than typval_compare().
4232 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
4233 ret = FAIL;
4234 else
4235 {
4236 ret = typval_compare(tv1, tv2, type, ic);
4237 tv1->v_type = VAR_BOOL;
4238 tv1->vval.v_number = tv1->vval.v_number
4239 ? VVAL_TRUE : VVAL_FALSE;
4240 clear_tv(tv2);
4241 --ppconst->pp_used;
4242 }
4243 return ret;
4244 }
4245
4246 generate_ppconst(cctx, ppconst);
4247 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 }
4249
4250 return OK;
4251}
4252
Bram Moolenaar7f141552020-05-09 17:35:53 +02004253static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4254
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255/*
4256 * Compile || or &&.
4257 */
4258 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004259compile_and_or(
4260 char_u **arg,
4261 cctx_T *cctx,
4262 char *op,
4263 ppconst_T *ppconst,
4264 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004266 char_u *next;
4267 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 int opchar = *op;
4269
4270 if (p[0] == opchar && p[1] == opchar)
4271 {
4272 garray_T *instr = &cctx->ctx_instr;
4273 garray_T end_ga;
4274
4275 /*
4276 * Repeat until there is no following "||" or "&&"
4277 */
4278 ga_init2(&end_ga, sizeof(int), 10);
4279 while (p[0] == opchar && p[1] == opchar)
4280 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004281 if (next != NULL)
4282 {
4283 *arg = next_line_from_context(cctx, TRUE);
4284 p = skipwhite(*arg);
4285 }
4286
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004287 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4288 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004290 return FAIL;
4291 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292
Bram Moolenaara5565e42020-05-09 15:44:01 +02004293 // TODO: use ppconst if the value is a constant
4294 generate_ppconst(cctx, ppconst);
4295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 if (ga_grow(&end_ga, 1) == FAIL)
4297 {
4298 ga_clear(&end_ga);
4299 return FAIL;
4300 }
4301 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4302 ++end_ga.ga_len;
4303 generate_JUMP(cctx, opchar == '|'
4304 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4305
4306 // eval the next expression
4307 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004308 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004309 return FAIL;
4310
Bram Moolenaara5565e42020-05-09 15:44:01 +02004311 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4312 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 {
4314 ga_clear(&end_ga);
4315 return FAIL;
4316 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004317
4318 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004320 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321
4322 // Fill in the end label in all jumps.
4323 while (end_ga.ga_len > 0)
4324 {
4325 isn_T *isn;
4326
4327 --end_ga.ga_len;
4328 isn = ((isn_T *)instr->ga_data)
4329 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4330 isn->isn_arg.jump.jump_where = instr->ga_len;
4331 }
4332 ga_clear(&end_ga);
4333 }
4334
4335 return OK;
4336}
4337
4338/*
4339 * expr4a && expr4a && expr4a logical AND
4340 *
4341 * Produces instructions:
4342 * EVAL expr4a Push result of "expr4a"
4343 * JUMP_AND_KEEP_IF_FALSE end
4344 * EVAL expr4b Push result of "expr4b"
4345 * JUMP_AND_KEEP_IF_FALSE end
4346 * EVAL expr4c Push result of "expr4c"
4347 * end:
4348 */
4349 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004350compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004352 int ppconst_used = ppconst->pp_used;
4353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004355 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 return FAIL;
4357
4358 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004359 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360}
4361
4362/*
4363 * expr3a || expr3b || expr3c logical OR
4364 *
4365 * Produces instructions:
4366 * EVAL expr3a Push result of "expr3a"
4367 * JUMP_AND_KEEP_IF_TRUE end
4368 * EVAL expr3b Push result of "expr3b"
4369 * JUMP_AND_KEEP_IF_TRUE end
4370 * EVAL expr3c Push result of "expr3c"
4371 * end:
4372 */
4373 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004374compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004376 int ppconst_used = ppconst->pp_used;
4377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004379 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 return FAIL;
4381
4382 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004383 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384}
4385
4386/*
4387 * Toplevel expression: expr2 ? expr1a : expr1b
4388 *
4389 * Produces instructions:
4390 * EVAL expr2 Push result of "expr"
4391 * JUMP_IF_FALSE alt jump if false
4392 * EVAL expr1a
4393 * JUMP_ALWAYS end
4394 * alt: EVAL expr1b
4395 * end:
4396 */
4397 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004398compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399{
4400 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004401 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004402 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403
Bram Moolenaar61a89812020-05-07 16:58:17 +02004404 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004405 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 return FAIL;
4407
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004408 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 if (*p == '?')
4410 {
4411 garray_T *instr = &cctx->ctx_instr;
4412 garray_T *stack = &cctx->ctx_type_stack;
4413 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004414 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004416 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004418 int has_const_expr = FALSE;
4419 int const_value = FALSE;
4420 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004422 if (next != NULL)
4423 {
4424 *arg = next_line_from_context(cctx, TRUE);
4425 p = skipwhite(*arg);
4426 }
4427
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004428 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4429 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004431 return FAIL;
4432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433
Bram Moolenaara5565e42020-05-09 15:44:01 +02004434 if (ppconst->pp_used == ppconst_used + 1)
4435 {
4436 // the condition is a constant, we know whether the ? or the :
4437 // expression is to be evaluated.
4438 has_const_expr = TRUE;
4439 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4440 clear_tv(&ppconst->pp_tv[ppconst_used]);
4441 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004442 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4443 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004444 }
4445 else
4446 {
4447 generate_ppconst(cctx, ppconst);
4448 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4449 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450
4451 // evaluate the second expression; any type is accepted
4452 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004453 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004454 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004455 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004456 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457
Bram Moolenaara5565e42020-05-09 15:44:01 +02004458 if (!has_const_expr)
4459 {
4460 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461
Bram Moolenaara5565e42020-05-09 15:44:01 +02004462 // remember the type and drop it
4463 --stack->ga_len;
4464 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465
Bram Moolenaara5565e42020-05-09 15:44:01 +02004466 end_idx = instr->ga_len;
4467 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4468
4469 // jump here from JUMP_IF_FALSE
4470 isn = ((isn_T *)instr->ga_data) + alt_idx;
4471 isn->isn_arg.jump.jump_where = instr->ga_len;
4472 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473
4474 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004475 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 if (*p != ':')
4477 {
4478 emsg(_(e_missing_colon));
4479 return FAIL;
4480 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004481 if (next != NULL)
4482 {
4483 *arg = next_line_from_context(cctx, TRUE);
4484 p = skipwhite(*arg);
4485 }
4486
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004487 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4488 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004490 return FAIL;
4491 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492
4493 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004494 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004495 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4496 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004498 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004499 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004500 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004501 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502
Bram Moolenaara5565e42020-05-09 15:44:01 +02004503 if (!has_const_expr)
4504 {
4505 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506
Bram Moolenaara5565e42020-05-09 15:44:01 +02004507 // If the types differ, the result has a more generic type.
4508 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4509 common_type(type1, type2, &type2, cctx->ctx_type_list);
4510
4511 // jump here from JUMP_ALWAYS
4512 isn = ((isn_T *)instr->ga_data) + end_idx;
4513 isn->isn_arg.jump.jump_where = instr->ga_len;
4514 }
4515
4516 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517 }
4518 return OK;
4519}
4520
4521/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004522 * Toplevel expression.
4523 */
4524 static int
4525compile_expr0(char_u **arg, cctx_T *cctx)
4526{
4527 ppconst_T ppconst;
4528
4529 CLEAR_FIELD(ppconst);
4530 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4531 {
4532 clear_ppconst(&ppconst);
4533 return FAIL;
4534 }
4535 if (generate_ppconst(cctx, &ppconst) == FAIL)
4536 return FAIL;
4537 return OK;
4538}
4539
4540/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 * compile "return [expr]"
4542 */
4543 static char_u *
4544compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4545{
4546 char_u *p = arg;
4547 garray_T *stack = &cctx->ctx_type_stack;
4548 type_T *stack_type;
4549
4550 if (*p != NUL && *p != '|' && *p != '\n')
4551 {
4552 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004553 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 return NULL;
4555
4556 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4557 if (set_return_type)
4558 cctx->ctx_ufunc->uf_ret_type = stack_type;
4559 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4560 == FAIL)
4561 return NULL;
4562 }
4563 else
4564 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004565 // "set_return_type" cannot be TRUE, only used for a lambda which
4566 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004567 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4568 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569 {
4570 emsg(_("E1003: Missing return value"));
4571 return NULL;
4572 }
4573
4574 // No argument, return zero.
4575 generate_PUSHNR(cctx, 0);
4576 }
4577
4578 if (generate_instr(cctx, ISN_RETURN) == NULL)
4579 return NULL;
4580
4581 // "return val | endif" is possible
4582 return skipwhite(p);
4583}
4584
4585/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004586 * Get a line from the compilation context, compatible with exarg_T getline().
4587 * Return a pointer to the line in allocated memory.
4588 * Return NULL for end-of-file or some error.
4589 */
4590 static char_u *
4591exarg_getline(
4592 int c UNUSED,
4593 void *cookie,
4594 int indent UNUSED,
4595 int do_concat UNUSED)
4596{
4597 cctx_T *cctx = (cctx_T *)cookie;
4598
4599 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4600 {
4601 iemsg("Heredoc got to end");
4602 return NULL;
4603 }
4604 ++cctx->ctx_lnum;
4605 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4606 [cctx->ctx_lnum]);
4607}
4608
4609/*
4610 * Compile a nested :def command.
4611 */
4612 static char_u *
4613compile_nested_function(exarg_T *eap, cctx_T *cctx)
4614{
4615 char_u *name_start = eap->arg;
4616 char_u *name_end = to_name_end(eap->arg, FALSE);
4617 char_u *name = get_lambda_name();
4618 lvar_T *lvar;
4619 ufunc_T *ufunc;
4620
4621 eap->arg = name_end;
4622 eap->getline = exarg_getline;
4623 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004624 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004625 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004626 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004627
Bram Moolenaar822ba242020-05-24 23:00:18 +02004628 if (ufunc == NULL)
4629 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004630 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004631 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004632 return NULL;
4633
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004634 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004635 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004636 TRUE, ufunc->uf_func_type);
4637
4638 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4639 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4640 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004641
Bram Moolenaar61a89812020-05-07 16:58:17 +02004642 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004643 return (char_u *)"";
4644}
4645
4646/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 * Return the length of an assignment operator, or zero if there isn't one.
4648 */
4649 int
4650assignment_len(char_u *p, int *heredoc)
4651{
4652 if (*p == '=')
4653 {
4654 if (p[1] == '<' && p[2] == '<')
4655 {
4656 *heredoc = TRUE;
4657 return 3;
4658 }
4659 return 1;
4660 }
4661 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4662 return 2;
4663 if (STRNCMP(p, "..=", 3) == 0)
4664 return 3;
4665 return 0;
4666}
4667
4668// words that cannot be used as a variable
4669static char *reserved[] = {
4670 "true",
4671 "false",
4672 NULL
4673};
4674
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004675typedef enum {
4676 dest_local,
4677 dest_option,
4678 dest_env,
4679 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004680 dest_buffer,
4681 dest_window,
4682 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004683 dest_vimvar,
4684 dest_script,
4685 dest_reg,
4686} assign_dest_T;
4687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004689 * Generate the load instruction for "name".
4690 */
4691 static void
4692generate_loadvar(
4693 cctx_T *cctx,
4694 assign_dest_T dest,
4695 char_u *name,
4696 lvar_T *lvar,
4697 type_T *type)
4698{
4699 switch (dest)
4700 {
4701 case dest_option:
4702 // TODO: check the option exists
4703 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4704 break;
4705 case dest_global:
4706 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4707 break;
4708 case dest_buffer:
4709 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4710 break;
4711 case dest_window:
4712 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4713 break;
4714 case dest_tab:
4715 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4716 break;
4717 case dest_script:
4718 compile_load_scriptvar(cctx,
4719 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4720 break;
4721 case dest_env:
4722 // Include $ in the name here
4723 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4724 break;
4725 case dest_reg:
4726 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4727 break;
4728 case dest_vimvar:
4729 generate_LOADV(cctx, name + 2, TRUE);
4730 break;
4731 case dest_local:
4732 if (lvar->lv_from_outer)
4733 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4734 NULL, type);
4735 else
4736 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4737 break;
4738 }
4739}
4740
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004741 void
4742vim9_declare_error(char_u *name)
4743{
4744 char *scope = "";
4745
4746 switch (*name)
4747 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004748 case 'g': scope = _("global"); break;
4749 case 'b': scope = _("buffer"); break;
4750 case 'w': scope = _("window"); break;
4751 case 't': scope = _("tab"); break;
4752 case 'v': scope = "v:"; break;
4753 case '$': semsg(_(e_declare_env_var), name); return;
4754 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004755 }
4756 semsg(_(e_declare_var), scope, name);
4757}
4758
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004759/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004760 * Compile declaration and assignment:
4761 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004763 * Return NULL for an error.
4764 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 */
4766 static char_u *
4767compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4768{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004769 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004771 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772 char_u *ret = NULL;
4773 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004774 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004777 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 int oplen = 0;
4780 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004781 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004782 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004783 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004787 // Skip over the "var" or "[var, var]" to get to any "=".
4788 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4789 if (p == NULL)
4790 return *arg == '[' ? arg : NULL;
4791
4792 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004794 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 return NULL;
4796 }
4797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 sp = p;
4799 p = skipwhite(p);
4800 op = p;
4801 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004802
4803 if (var_count > 0 && oplen == 0)
4804 // can be something like "[1, 2]->func()"
4805 return arg;
4806
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004807 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4808 {
4809 char_u buf[4];
4810
4811 vim_strncpy(buf, op, oplen);
4812 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004813 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004814 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004815
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 if (heredoc)
4817 {
4818 list_T *l;
4819 listitem_T *li;
4820
4821 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004822 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004824 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825
4826 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004827 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004828 {
4829 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4830 li->li_tv.vval.v_string = NULL;
4831 }
4832 generate_NEWLIST(cctx, l->lv_len);
4833 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004834 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835 list_free(l);
4836 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004837 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004839 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004841 // for "[var, var] = expr" evaluate the expression here, loop over the
4842 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004843
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004844 p = skipwhite(op + oplen);
4845 if (compile_expr0(&p, cctx) == FAIL)
4846 return NULL;
4847 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004849 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004851 type_T *stacktype;
4852
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004853 stacktype = stack->ga_len == 0 ? &t_void
4854 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004855 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004857 emsg(_(e_cannot_use_void));
4858 goto theend;
4859 }
4860 if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL)
4861 goto theend;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004862 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4863 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004864 }
4865 }
4866
4867 /*
4868 * Loop over variables in "[var, var] = expr".
4869 * For "var = expr" and "let var: type" this is done only once.
4870 */
4871 if (var_count > 0)
4872 var_start = skipwhite(arg + 1); // skip over the "["
4873 else
4874 var_start = arg;
4875 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4876 {
4877 char_u *var_end = skip_var_one(var_start, FALSE);
4878 size_t varlen;
4879 int new_local = FALSE;
4880 int opt_type;
4881 int opt_flags = 0;
4882 assign_dest_T dest = dest_local;
4883 int vimvaridx = -1;
4884 lvar_T *lvar = NULL;
4885 lvar_T arg_lvar;
4886 int has_type = FALSE;
4887 int has_index = FALSE;
4888 int instr_count = -1;
4889
4890 p = (*var_start == '&' || *var_start == '$'
4891 || *var_start == '@') ? var_start + 1 : var_start;
4892 p = to_name_end(p, TRUE);
4893
4894 // "a: type" is declaring variable "a" with a type, not "a:".
4895 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4896 --var_end;
4897 if (is_decl && p == var_start + 2 && p[-1] == ':')
4898 --p;
4899
4900 varlen = p - var_start;
4901 vim_free(name);
4902 name = vim_strnsave(var_start, varlen);
4903 if (name == NULL)
4904 return NULL;
4905 if (!heredoc)
4906 type = &t_any;
4907
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004908 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004909 {
4910 if (*var_start == '&')
4911 {
4912 int cc;
4913 long numval;
4914
4915 dest = dest_option;
4916 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004918 emsg(_(e_const_option));
4919 goto theend;
4920 }
4921 if (is_decl)
4922 {
4923 semsg(_("E1052: Cannot declare an option: %s"), var_start);
4924 goto theend;
4925 }
4926 p = var_start;
4927 p = find_option_end(&p, &opt_flags);
4928 if (p == NULL)
4929 {
4930 // cannot happen?
4931 emsg(_(e_letunexp));
4932 goto theend;
4933 }
4934 cc = *p;
4935 *p = NUL;
4936 opt_type = get_option_value(var_start + 1, &numval,
4937 NULL, opt_flags);
4938 *p = cc;
4939 if (opt_type == -3)
4940 {
4941 semsg(_(e_unknown_option), var_start);
4942 goto theend;
4943 }
4944 if (opt_type == -2 || opt_type == 0)
4945 type = &t_string;
4946 else
4947 type = &t_number; // both number and boolean option
4948 }
4949 else if (*var_start == '$')
4950 {
4951 dest = dest_env;
4952 type = &t_string;
4953 if (is_decl)
4954 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004955 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004956 goto theend;
4957 }
4958 }
4959 else if (*var_start == '@')
4960 {
4961 if (!valid_yank_reg(var_start[1], TRUE))
4962 {
4963 emsg_invreg(var_start[1]);
4964 goto theend;
4965 }
4966 dest = dest_reg;
4967 type = &t_string;
4968 if (is_decl)
4969 {
4970 semsg(_("E1066: Cannot declare a register: %s"), name);
4971 goto theend;
4972 }
4973 }
4974 else if (STRNCMP(var_start, "g:", 2) == 0)
4975 {
4976 dest = dest_global;
4977 if (is_decl)
4978 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004979 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004980 goto theend;
4981 }
4982 }
4983 else if (STRNCMP(var_start, "b:", 2) == 0)
4984 {
4985 dest = dest_buffer;
4986 if (is_decl)
4987 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004988 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004989 goto theend;
4990 }
4991 }
4992 else if (STRNCMP(var_start, "w:", 2) == 0)
4993 {
4994 dest = dest_window;
4995 if (is_decl)
4996 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004997 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004998 goto theend;
4999 }
5000 }
5001 else if (STRNCMP(var_start, "t:", 2) == 0)
5002 {
5003 dest = dest_tab;
5004 if (is_decl)
5005 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005006 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005007 goto theend;
5008 }
5009 }
5010 else if (STRNCMP(var_start, "v:", 2) == 0)
5011 {
5012 typval_T *vtv;
5013 int di_flags;
5014
5015 vimvaridx = find_vim_var(name + 2, &di_flags);
5016 if (vimvaridx < 0)
5017 {
5018 semsg(_(e_var_notfound), var_start);
5019 goto theend;
5020 }
5021 // We use the current value of "sandbox" here, is that OK?
5022 if (var_check_ro(di_flags, name, FALSE))
5023 goto theend;
5024 dest = dest_vimvar;
5025 vtv = get_vim_var_tv(vimvaridx);
5026 type = typval2type(vtv);
5027 if (is_decl)
5028 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005029 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005030 goto theend;
5031 }
5032 }
5033 else
5034 {
5035 int idx;
5036
5037 for (idx = 0; reserved[idx] != NULL; ++idx)
5038 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005039 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005040 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005041 goto theend;
5042 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005043
5044 lvar = lookup_local(var_start, varlen, cctx);
5045 if (lvar == NULL)
5046 {
5047 CLEAR_FIELD(arg_lvar);
5048 if (lookup_arg(var_start, varlen,
5049 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5050 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005051 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005052 if (is_decl)
5053 {
5054 semsg(_(e_used_as_arg), name);
5055 goto theend;
5056 }
5057 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005058 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005059 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005060 if (lvar != NULL)
5061 {
5062 if (is_decl)
5063 {
5064 semsg(_("E1017: Variable already declared: %s"), name);
5065 goto theend;
5066 }
5067 else if (lvar->lv_const)
5068 {
5069 semsg(_("E1018: Cannot assign to a constant: %s"),
5070 name);
5071 goto theend;
5072 }
5073 }
5074 else if (STRNCMP(var_start, "s:", 2) == 0
5075 || lookup_script(var_start, varlen) == OK
5076 || find_imported(var_start, varlen, cctx) != NULL)
5077 {
5078 dest = dest_script;
5079 if (is_decl)
5080 {
5081 semsg(_("E1054: Variable already declared in the script: %s"),
5082 name);
5083 goto theend;
5084 }
5085 }
5086 else if (name[1] == ':' && name[2] != NUL)
5087 {
5088 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5089 name);
5090 goto theend;
5091 }
5092 else if (!is_decl)
5093 {
5094 semsg(_("E1089: unknown variable: %s"), name);
5095 goto theend;
5096 }
5097 }
5098 }
5099
5100 // handle "a:name" as a name, not index "name" on "a"
5101 if (varlen > 1 || var_start[varlen] != ':')
5102 p = var_end;
5103
5104 if (dest != dest_option)
5105 {
5106 if (is_decl && *p == ':')
5107 {
5108 // parse optional type: "let var: type = expr"
5109 if (!VIM_ISWHITE(p[1]))
5110 {
5111 semsg(_(e_white_after), ":");
5112 goto theend;
5113 }
5114 p = skipwhite(p + 1);
5115 type = parse_type(&p, cctx->ctx_type_list);
5116 has_type = TRUE;
5117 }
5118 else if (lvar != NULL)
5119 type = lvar->lv_type;
5120 }
5121
5122 if (oplen == 3 && !heredoc && dest != dest_global
5123 && type->tt_type != VAR_STRING
5124 && type->tt_type != VAR_ANY)
5125 {
5126 emsg(_("E1019: Can only concatenate to string"));
5127 goto theend;
5128 }
5129
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005130 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005131 {
5132 if (oplen > 1 && !heredoc)
5133 {
5134 // +=, /=, etc. require an existing variable
5135 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5136 name);
5137 goto theend;
5138 }
5139
5140 // new local variable
5141 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5142 goto theend;
5143 lvar = reserve_local(cctx, var_start, varlen,
5144 cmdidx == CMD_const, type);
5145 if (lvar == NULL)
5146 goto theend;
5147 new_local = TRUE;
5148 }
5149
5150 member_type = type;
5151 if (var_end > var_start + varlen)
5152 {
5153 // Something follows after the variable: "var[idx]".
5154 if (is_decl)
5155 {
5156 emsg(_("E1087: cannot use an index when declaring a variable"));
5157 goto theend;
5158 }
5159
5160 if (var_start[varlen] == '[')
5161 {
5162 has_index = TRUE;
5163 if (type->tt_member == NULL)
5164 {
5165 semsg(_("E1088: cannot use an index on %s"), name);
5166 goto theend;
5167 }
5168 member_type = type->tt_member;
5169 }
5170 else
5171 {
5172 semsg("Not supported yet: %s", var_start);
5173 goto theend;
5174 }
5175 }
5176 else if (lvar == &arg_lvar)
5177 {
5178 semsg(_("E1090: Cannot assign to argument %s"), name);
5179 goto theend;
5180 }
5181
5182 if (!heredoc)
5183 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005184 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005185 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005186 if (oplen > 0 && var_count == 0)
5187 {
5188 // skip over the "=" and the expression
5189 p = skipwhite(op + oplen);
5190 compile_expr0(&p, cctx);
5191 }
5192 }
5193 else if (oplen > 0)
5194 {
5195 type_T *stacktype;
5196
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005197 // For "var = expr" evaluate the expression.
5198 if (var_count == 0)
5199 {
5200 int r;
5201
5202 // for "+=", "*=", "..=" etc. first load the current value
5203 if (*op != '=')
5204 {
5205 generate_loadvar(cctx, dest, name, lvar, type);
5206
5207 if (has_index)
5208 {
5209 // TODO: get member from list or dict
5210 emsg("Index with operation not supported yet");
5211 goto theend;
5212 }
5213 }
5214
5215 // Compile the expression. Temporarily hide the new local
5216 // variable here, it is not available to this expression.
5217 if (new_local)
5218 --cctx->ctx_locals.ga_len;
5219 instr_count = instr->ga_len;
5220 p = skipwhite(op + oplen);
5221 r = compile_expr0(&p, cctx);
5222 if (new_local)
5223 ++cctx->ctx_locals.ga_len;
5224 if (r == FAIL)
5225 goto theend;
5226 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005227 else if (semicolon && var_idx == var_count - 1)
5228 {
5229 // For "[var; var] = expr" get the rest of the list
5230 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5231 goto theend;
5232 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005233 else
5234 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005235 // For "[var, var] = expr" get the "var_idx" item from the
5236 // list.
5237 if (generate_GETITEM(cctx, var_idx) == FAIL)
5238 return FAIL;
5239 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005240
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005241 stacktype = stack->ga_len == 0 ? &t_void
5242 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5243 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005244 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005245 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005246 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005247 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005248 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005249 emsg(_(e_cannot_use_void));
5250 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005251 }
5252 else
5253 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005254 // An empty list or dict has a &t_void member,
5255 // for a variable that implies &t_any.
5256 if (stacktype == &t_list_empty)
5257 lvar->lv_type = &t_list_any;
5258 else if (stacktype == &t_dict_empty)
5259 lvar->lv_type = &t_dict_any;
5260 else
5261 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005262 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005263 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005264 else
5265 {
5266 type_T *use_type = lvar->lv_type;
5267
5268 if (has_index)
5269 {
5270 use_type = use_type->tt_member;
5271 if (use_type == NULL)
5272 use_type = &t_void;
5273 }
5274 if (need_type(stacktype, use_type, -1, cctx)
5275 == FAIL)
5276 goto theend;
5277 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005278 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005279 else if (*p != '=' && need_type(stacktype, member_type, -1,
5280 cctx) == FAIL)
5281 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005282 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005283 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005284 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005285 emsg(_(e_const_req_value));
5286 goto theend;
5287 }
5288 else if (!has_type || dest == dest_option)
5289 {
5290 emsg(_(e_type_req));
5291 goto theend;
5292 }
5293 else
5294 {
5295 // variables are always initialized
5296 if (ga_grow(instr, 1) == FAIL)
5297 goto theend;
5298 switch (member_type->tt_type)
5299 {
5300 case VAR_BOOL:
5301 generate_PUSHBOOL(cctx, VVAL_FALSE);
5302 break;
5303 case VAR_FLOAT:
5304#ifdef FEAT_FLOAT
5305 generate_PUSHF(cctx, 0.0);
5306#endif
5307 break;
5308 case VAR_STRING:
5309 generate_PUSHS(cctx, NULL);
5310 break;
5311 case VAR_BLOB:
5312 generate_PUSHBLOB(cctx, NULL);
5313 break;
5314 case VAR_FUNC:
5315 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5316 break;
5317 case VAR_LIST:
5318 generate_NEWLIST(cctx, 0);
5319 break;
5320 case VAR_DICT:
5321 generate_NEWDICT(cctx, 0);
5322 break;
5323 case VAR_JOB:
5324 generate_PUSHJOB(cctx, NULL);
5325 break;
5326 case VAR_CHANNEL:
5327 generate_PUSHCHANNEL(cctx, NULL);
5328 break;
5329 case VAR_NUMBER:
5330 case VAR_UNKNOWN:
5331 case VAR_ANY:
5332 case VAR_PARTIAL:
5333 case VAR_VOID:
5334 case VAR_SPECIAL: // cannot happen
5335 generate_PUSHNR(cctx, 0);
5336 break;
5337 }
5338 }
5339 if (var_count == 0)
5340 end = p;
5341 }
5342
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005343 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005344 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005345 break;
5346
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005347 if (oplen > 0 && *op != '=')
5348 {
5349 type_T *expected = &t_number;
5350 type_T *stacktype;
5351
5352 // TODO: if type is known use float or any operation
5353
5354 if (*op == '.')
5355 expected = &t_string;
5356 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5357 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5358 goto theend;
5359
5360 if (*op == '.')
5361 generate_instr_drop(cctx, ISN_CONCAT, 1);
5362 else
5363 {
5364 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5365
5366 if (isn == NULL)
5367 goto theend;
5368 switch (*op)
5369 {
5370 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5371 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5372 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5373 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5374 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5375 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005376 }
5377 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005378
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005379 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005380 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005381 int r;
5382
5383 // Compile the "idx" in "var[idx]".
5384 if (new_local)
5385 --cctx->ctx_locals.ga_len;
5386 p = skipwhite(var_start + varlen + 1);
5387 r = compile_expr0(&p, cctx);
5388 if (new_local)
5389 ++cctx->ctx_locals.ga_len;
5390 if (r == FAIL)
5391 goto theend;
5392 if (*skipwhite(p) != ']')
5393 {
5394 emsg(_(e_missbrac));
5395 goto theend;
5396 }
5397 if (type->tt_type == VAR_DICT
5398 && may_generate_2STRING(-1, cctx) == FAIL)
5399 goto theend;
5400 if (type->tt_type == VAR_LIST
5401 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005402 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005403 {
5404 emsg(_(e_number_exp));
5405 goto theend;
5406 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005407
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005408 // Load the dict or list. On the stack we then have:
5409 // - value
5410 // - index
5411 // - variable
5412 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005413
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005414 if (type->tt_type == VAR_LIST)
5415 {
5416 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5417 return FAIL;
5418 }
5419 else if (type->tt_type == VAR_DICT)
5420 {
5421 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5422 return FAIL;
5423 }
5424 else
5425 {
5426 emsg(_(e_listreq));
5427 goto theend;
5428 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005429 }
5430 else
5431 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005432 switch (dest)
5433 {
5434 case dest_option:
5435 generate_STOREOPT(cctx, name + 1, opt_flags);
5436 break;
5437 case dest_global:
5438 // include g: with the name, easier to execute that way
5439 generate_STORE(cctx, ISN_STOREG, 0, name);
5440 break;
5441 case dest_buffer:
5442 // include b: with the name, easier to execute that way
5443 generate_STORE(cctx, ISN_STOREB, 0, name);
5444 break;
5445 case dest_window:
5446 // include w: with the name, easier to execute that way
5447 generate_STORE(cctx, ISN_STOREW, 0, name);
5448 break;
5449 case dest_tab:
5450 // include t: with the name, easier to execute that way
5451 generate_STORE(cctx, ISN_STORET, 0, name);
5452 break;
5453 case dest_env:
5454 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5455 break;
5456 case dest_reg:
5457 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5458 break;
5459 case dest_vimvar:
5460 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5461 break;
5462 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005463 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005464 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5465 imported_T *import = NULL;
5466 int sid = current_sctx.sc_sid;
5467 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005468
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005469 if (name[1] != ':')
5470 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005471 import = find_imported(name, 0, cctx);
5472 if (import != NULL)
5473 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005474 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005475
5476 idx = get_script_item_idx(sid, rawname, TRUE);
5477 // TODO: specific type
5478 if (idx < 0)
5479 {
5480 char_u *name_s = name;
5481
5482 // Include s: in the name for store_var()
5483 if (name[1] != ':')
5484 {
5485 int len = (int)STRLEN(name) + 3;
5486
5487 name_s = alloc(len);
5488 if (name_s == NULL)
5489 name_s = name;
5490 else
5491 vim_snprintf((char *)name_s, len,
5492 "s:%s", name);
5493 }
5494 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005495 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005496 if (name_s != name)
5497 vim_free(name_s);
5498 }
5499 else
5500 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005501 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005502 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005503 break;
5504 case dest_local:
5505 if (lvar != NULL)
5506 {
5507 isn_T *isn = ((isn_T *)instr->ga_data)
5508 + instr->ga_len - 1;
5509
5510 // optimization: turn "var = 123" from ISN_PUSHNR +
5511 // ISN_STORE into ISN_STORENR
5512 if (!lvar->lv_from_outer
5513 && instr->ga_len == instr_count + 1
5514 && isn->isn_type == ISN_PUSHNR)
5515 {
5516 varnumber_T val = isn->isn_arg.number;
5517
5518 isn->isn_type = ISN_STORENR;
5519 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5520 isn->isn_arg.storenr.stnr_val = val;
5521 if (stack->ga_len > 0)
5522 --stack->ga_len;
5523 }
5524 else if (lvar->lv_from_outer)
5525 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005526 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005527 else
5528 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5529 }
5530 break;
5531 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005532 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005533
5534 if (var_idx + 1 < var_count)
5535 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005536 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005537
5538 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005539 if (var_count > 0 && !semicolon)
5540 {
5541 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5542 goto theend;
5543 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005544
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005545 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546
5547theend:
5548 vim_free(name);
5549 return ret;
5550}
5551
5552/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005553 * Check if "name" can be "unlet".
5554 */
5555 int
5556check_vim9_unlet(char_u *name)
5557{
5558 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5559 {
5560 semsg(_("E1081: Cannot unlet %s"), name);
5561 return FAIL;
5562 }
5563 return OK;
5564}
5565
5566/*
5567 * Callback passed to ex_unletlock().
5568 */
5569 static int
5570compile_unlet(
5571 lval_T *lvp,
5572 char_u *name_end,
5573 exarg_T *eap,
5574 int deep UNUSED,
5575 void *coookie)
5576{
5577 cctx_T *cctx = coookie;
5578
5579 if (lvp->ll_tv == NULL)
5580 {
5581 char_u *p = lvp->ll_name;
5582 int cc = *name_end;
5583 int ret = OK;
5584
5585 // Normal name. Only supports g:, w:, t: and b: namespaces.
5586 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005587 if (*p == '$')
5588 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5589 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005590 ret = FAIL;
5591 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005592 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005593
5594 *name_end = cc;
5595 return ret;
5596 }
5597
5598 // TODO: unlet {list}[idx]
5599 // TODO: unlet {dict}[key]
5600 emsg("Sorry, :unlet not fully implemented yet");
5601 return FAIL;
5602}
5603
5604/*
5605 * compile "unlet var", "lock var" and "unlock var"
5606 * "arg" points to "var".
5607 */
5608 static char_u *
5609compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5610{
5611 char_u *p = arg;
5612
5613 if (eap->cmdidx != CMD_unlet)
5614 {
5615 emsg("Sorry, :lock and unlock not implemented yet");
5616 return NULL;
5617 }
5618
5619 if (*p == '!')
5620 {
5621 p = skipwhite(p + 1);
5622 eap->forceit = TRUE;
5623 }
5624
5625 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5626 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5627}
5628
5629/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005630 * Compile an :import command.
5631 */
5632 static char_u *
5633compile_import(char_u *arg, cctx_T *cctx)
5634{
Bram Moolenaar5269bd22020-03-09 19:25:27 +01005635 return handle_import(arg, &cctx->ctx_imports, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005636}
5637
5638/*
5639 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5640 */
5641 static int
5642compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5643{
5644 garray_T *instr = &cctx->ctx_instr;
5645 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5646
5647 if (endlabel == NULL)
5648 return FAIL;
5649 endlabel->el_next = *el;
5650 *el = endlabel;
5651 endlabel->el_end_label = instr->ga_len;
5652
5653 generate_JUMP(cctx, when, 0);
5654 return OK;
5655}
5656
5657 static void
5658compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5659{
5660 garray_T *instr = &cctx->ctx_instr;
5661
5662 while (*el != NULL)
5663 {
5664 endlabel_T *cur = (*el);
5665 isn_T *isn;
5666
5667 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5668 isn->isn_arg.jump.jump_where = instr->ga_len;
5669 *el = cur->el_next;
5670 vim_free(cur);
5671 }
5672}
5673
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005674 static void
5675compile_free_jump_to_end(endlabel_T **el)
5676{
5677 while (*el != NULL)
5678 {
5679 endlabel_T *cur = (*el);
5680
5681 *el = cur->el_next;
5682 vim_free(cur);
5683 }
5684}
5685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005686/*
5687 * Create a new scope and set up the generic items.
5688 */
5689 static scope_T *
5690new_scope(cctx_T *cctx, scopetype_T type)
5691{
5692 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5693
5694 if (scope == NULL)
5695 return NULL;
5696 scope->se_outer = cctx->ctx_scope;
5697 cctx->ctx_scope = scope;
5698 scope->se_type = type;
5699 scope->se_local_count = cctx->ctx_locals.ga_len;
5700 return scope;
5701}
5702
5703/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005704 * Free the current scope and go back to the outer scope.
5705 */
5706 static void
5707drop_scope(cctx_T *cctx)
5708{
5709 scope_T *scope = cctx->ctx_scope;
5710
5711 if (scope == NULL)
5712 {
5713 iemsg("calling drop_scope() without a scope");
5714 return;
5715 }
5716 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005717 switch (scope->se_type)
5718 {
5719 case IF_SCOPE:
5720 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5721 case FOR_SCOPE:
5722 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5723 case WHILE_SCOPE:
5724 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5725 case TRY_SCOPE:
5726 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5727 case NO_SCOPE:
5728 case BLOCK_SCOPE:
5729 break;
5730 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005731 vim_free(scope);
5732}
5733
5734/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005735 * compile "if expr"
5736 *
5737 * "if expr" Produces instructions:
5738 * EVAL expr Push result of "expr"
5739 * JUMP_IF_FALSE end
5740 * ... body ...
5741 * end:
5742 *
5743 * "if expr | else" Produces instructions:
5744 * EVAL expr Push result of "expr"
5745 * JUMP_IF_FALSE else
5746 * ... body ...
5747 * JUMP_ALWAYS end
5748 * else:
5749 * ... body ...
5750 * end:
5751 *
5752 * "if expr1 | elseif expr2 | else" Produces instructions:
5753 * EVAL expr Push result of "expr"
5754 * JUMP_IF_FALSE elseif
5755 * ... body ...
5756 * JUMP_ALWAYS end
5757 * elseif:
5758 * EVAL expr Push result of "expr"
5759 * JUMP_IF_FALSE else
5760 * ... body ...
5761 * JUMP_ALWAYS end
5762 * else:
5763 * ... body ...
5764 * end:
5765 */
5766 static char_u *
5767compile_if(char_u *arg, cctx_T *cctx)
5768{
5769 char_u *p = arg;
5770 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005771 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005772 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005773 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005774 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005775
Bram Moolenaara5565e42020-05-09 15:44:01 +02005776 CLEAR_FIELD(ppconst);
5777 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005778 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005779 clear_ppconst(&ppconst);
5780 return NULL;
5781 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005782 if (cctx->ctx_skip == SKIP_YES)
5783 clear_ppconst(&ppconst);
5784 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005785 {
5786 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005787 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005788 clear_ppconst(&ppconst);
5789 }
5790 else
5791 {
5792 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005793 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005794 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005795 return NULL;
5796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005797
5798 scope = new_scope(cctx, IF_SCOPE);
5799 if (scope == NULL)
5800 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005801 scope->se_skip_save = skip_save;
5802 // "is_had_return" will be reset if any block does not end in :return
5803 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005804
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005805 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005806 {
5807 // "where" is set when ":elseif", "else" or ":endif" is found
5808 scope->se_u.se_if.is_if_label = instr->ga_len;
5809 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5810 }
5811 else
5812 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005813
5814 return p;
5815}
5816
5817 static char_u *
5818compile_elseif(char_u *arg, cctx_T *cctx)
5819{
5820 char_u *p = arg;
5821 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005822 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005823 isn_T *isn;
5824 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005825 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005826
5827 if (scope == NULL || scope->se_type != IF_SCOPE)
5828 {
5829 emsg(_(e_elseif_without_if));
5830 return NULL;
5831 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005832 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005833 if (!cctx->ctx_had_return)
5834 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005835
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005836 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005837 {
5838 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005839 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005840 return NULL;
5841 // previous "if" or "elseif" jumps here
5842 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5843 isn->isn_arg.jump.jump_where = instr->ga_len;
5844 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005845
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005846 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005847 CLEAR_FIELD(ppconst);
5848 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005849 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005850 clear_ppconst(&ppconst);
5851 return NULL;
5852 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005853 if (scope->se_skip_save == SKIP_YES)
5854 clear_ppconst(&ppconst);
5855 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005856 {
5857 // The expression results in a constant.
5858 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005859 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005860 clear_ppconst(&ppconst);
5861 scope->se_u.se_if.is_if_label = -1;
5862 }
5863 else
5864 {
5865 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005866 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005867 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005868 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005869
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005870 // "where" is set when ":elseif", "else" or ":endif" is found
5871 scope->se_u.se_if.is_if_label = instr->ga_len;
5872 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5873 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005874
5875 return p;
5876}
5877
5878 static char_u *
5879compile_else(char_u *arg, cctx_T *cctx)
5880{
5881 char_u *p = arg;
5882 garray_T *instr = &cctx->ctx_instr;
5883 isn_T *isn;
5884 scope_T *scope = cctx->ctx_scope;
5885
5886 if (scope == NULL || scope->se_type != IF_SCOPE)
5887 {
5888 emsg(_(e_else_without_if));
5889 return NULL;
5890 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005891 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005892 if (!cctx->ctx_had_return)
5893 scope->se_u.se_if.is_had_return = FALSE;
5894 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895
Bram Moolenaarefd88552020-06-18 20:50:10 +02005896 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005897 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005898 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005899 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005900 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005901 if (!cctx->ctx_had_return
5902 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5903 JUMP_ALWAYS, cctx) == FAIL)
5904 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005905 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005906
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005907 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005908 {
5909 if (scope->se_u.se_if.is_if_label >= 0)
5910 {
5911 // previous "if" or "elseif" jumps here
5912 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5913 isn->isn_arg.jump.jump_where = instr->ga_len;
5914 scope->se_u.se_if.is_if_label = -1;
5915 }
5916 }
5917
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005918 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005919 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005921
5922 return p;
5923}
5924
5925 static char_u *
5926compile_endif(char_u *arg, cctx_T *cctx)
5927{
5928 scope_T *scope = cctx->ctx_scope;
5929 ifscope_T *ifscope;
5930 garray_T *instr = &cctx->ctx_instr;
5931 isn_T *isn;
5932
5933 if (scope == NULL || scope->se_type != IF_SCOPE)
5934 {
5935 emsg(_(e_endif_without_if));
5936 return NULL;
5937 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005938 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005939 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005940 if (!cctx->ctx_had_return)
5941 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005942
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005943 if (scope->se_u.se_if.is_if_label >= 0)
5944 {
5945 // previous "if" or "elseif" jumps here
5946 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5947 isn->isn_arg.jump.jump_where = instr->ga_len;
5948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005949 // Fill in the "end" label in jumps at the end of the blocks.
5950 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005951 cctx->ctx_skip = scope->se_skip_save;
5952
5953 // If all the blocks end in :return and there is an :else then the
5954 // had_return flag is set.
5955 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005956
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005957 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005958 return arg;
5959}
5960
5961/*
5962 * compile "for var in expr"
5963 *
5964 * Produces instructions:
5965 * PUSHNR -1
5966 * STORE loop-idx Set index to -1
5967 * EVAL expr Push result of "expr"
5968 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5969 * - if beyond end, jump to "end"
5970 * - otherwise get item from list and push it
5971 * STORE var Store item in "var"
5972 * ... body ...
5973 * JUMP top Jump back to repeat
5974 * end: DROP Drop the result of "expr"
5975 *
5976 */
5977 static char_u *
5978compile_for(char_u *arg, cctx_T *cctx)
5979{
5980 char_u *p;
5981 size_t varlen;
5982 garray_T *instr = &cctx->ctx_instr;
5983 garray_T *stack = &cctx->ctx_type_stack;
5984 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005985 lvar_T *loop_lvar; // loop iteration variable
5986 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005987 type_T *vartype;
5988
5989 // TODO: list of variables: "for [key, value] in dict"
5990 // parse "var"
5991 for (p = arg; eval_isnamec1(*p); ++p)
5992 ;
5993 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005994 var_lvar = lookup_local(arg, varlen, cctx);
5995 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005996 {
5997 semsg(_("E1023: variable already defined: %s"), arg);
5998 return NULL;
5999 }
6000
6001 // consume "in"
6002 p = skipwhite(p);
6003 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6004 {
6005 emsg(_(e_missing_in));
6006 return NULL;
6007 }
6008 p = skipwhite(p + 2);
6009
6010
6011 scope = new_scope(cctx, FOR_SCOPE);
6012 if (scope == NULL)
6013 return NULL;
6014
6015 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006016 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6017 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006018 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006019 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006020 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006022 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006023
6024 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006025 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6026 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006027 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006028 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006029 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006030 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006031 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006032
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006033 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006034
6035 // compile "expr", it remains on the stack until "endfor"
6036 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006037 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006038 {
6039 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006040 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006041 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006042
6043 // now we know the type of "var"
6044 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6045 if (vartype->tt_type != VAR_LIST)
6046 {
6047 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006048 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006049 return NULL;
6050 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02006051 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006052 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006053
6054 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006055 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006056
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006057 generate_FOR(cctx, loop_lvar->lv_idx);
6058 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006059
6060 return arg;
6061}
6062
6063/*
6064 * compile "endfor"
6065 */
6066 static char_u *
6067compile_endfor(char_u *arg, cctx_T *cctx)
6068{
6069 garray_T *instr = &cctx->ctx_instr;
6070 scope_T *scope = cctx->ctx_scope;
6071 forscope_T *forscope;
6072 isn_T *isn;
6073
6074 if (scope == NULL || scope->se_type != FOR_SCOPE)
6075 {
6076 emsg(_(e_for));
6077 return NULL;
6078 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006079 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006080 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006081 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006082
6083 // At end of ":for" scope jump back to the FOR instruction.
6084 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6085
6086 // Fill in the "end" label in the FOR statement so it can jump here
6087 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6088 isn->isn_arg.forloop.for_end = instr->ga_len;
6089
6090 // Fill in the "end" label any BREAK statements
6091 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6092
6093 // Below the ":for" scope drop the "expr" list from the stack.
6094 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6095 return NULL;
6096
6097 vim_free(scope);
6098
6099 return arg;
6100}
6101
6102/*
6103 * compile "while expr"
6104 *
6105 * Produces instructions:
6106 * top: EVAL expr Push result of "expr"
6107 * JUMP_IF_FALSE end jump if false
6108 * ... body ...
6109 * JUMP top Jump back to repeat
6110 * end:
6111 *
6112 */
6113 static char_u *
6114compile_while(char_u *arg, cctx_T *cctx)
6115{
6116 char_u *p = arg;
6117 garray_T *instr = &cctx->ctx_instr;
6118 scope_T *scope;
6119
6120 scope = new_scope(cctx, WHILE_SCOPE);
6121 if (scope == NULL)
6122 return NULL;
6123
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006124 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006125
6126 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006127 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006128 return NULL;
6129
6130 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006131 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006132 JUMP_IF_FALSE, cctx) == FAIL)
6133 return FAIL;
6134
6135 return p;
6136}
6137
6138/*
6139 * compile "endwhile"
6140 */
6141 static char_u *
6142compile_endwhile(char_u *arg, cctx_T *cctx)
6143{
6144 scope_T *scope = cctx->ctx_scope;
6145
6146 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6147 {
6148 emsg(_(e_while));
6149 return NULL;
6150 }
6151 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006152 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006153
6154 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006155 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006156
6157 // Fill in the "end" label in the WHILE statement so it can jump here.
6158 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006159 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006160
6161 vim_free(scope);
6162
6163 return arg;
6164}
6165
6166/*
6167 * compile "continue"
6168 */
6169 static char_u *
6170compile_continue(char_u *arg, cctx_T *cctx)
6171{
6172 scope_T *scope = cctx->ctx_scope;
6173
6174 for (;;)
6175 {
6176 if (scope == NULL)
6177 {
6178 emsg(_(e_continue));
6179 return NULL;
6180 }
6181 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6182 break;
6183 scope = scope->se_outer;
6184 }
6185
6186 // Jump back to the FOR or WHILE instruction.
6187 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006188 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6189 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006190 return arg;
6191}
6192
6193/*
6194 * compile "break"
6195 */
6196 static char_u *
6197compile_break(char_u *arg, cctx_T *cctx)
6198{
6199 scope_T *scope = cctx->ctx_scope;
6200 endlabel_T **el;
6201
6202 for (;;)
6203 {
6204 if (scope == NULL)
6205 {
6206 emsg(_(e_break));
6207 return NULL;
6208 }
6209 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6210 break;
6211 scope = scope->se_outer;
6212 }
6213
6214 // Jump to the end of the FOR or WHILE loop.
6215 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006216 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006217 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006218 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006219 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6220 return FAIL;
6221
6222 return arg;
6223}
6224
6225/*
6226 * compile "{" start of block
6227 */
6228 static char_u *
6229compile_block(char_u *arg, cctx_T *cctx)
6230{
6231 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6232 return NULL;
6233 return skipwhite(arg + 1);
6234}
6235
6236/*
6237 * compile end of block: drop one scope
6238 */
6239 static void
6240compile_endblock(cctx_T *cctx)
6241{
6242 scope_T *scope = cctx->ctx_scope;
6243
6244 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006245 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006246 vim_free(scope);
6247}
6248
6249/*
6250 * compile "try"
6251 * Creates a new scope for the try-endtry, pointing to the first catch and
6252 * finally.
6253 * Creates another scope for the "try" block itself.
6254 * TRY instruction sets up exception handling at runtime.
6255 *
6256 * "try"
6257 * TRY -> catch1, -> finally push trystack entry
6258 * ... try block
6259 * "throw {exception}"
6260 * EVAL {exception}
6261 * THROW create exception
6262 * ... try block
6263 * " catch {expr}"
6264 * JUMP -> finally
6265 * catch1: PUSH exeception
6266 * EVAL {expr}
6267 * MATCH
6268 * JUMP nomatch -> catch2
6269 * CATCH remove exception
6270 * ... catch block
6271 * " catch"
6272 * JUMP -> finally
6273 * catch2: CATCH remove exception
6274 * ... catch block
6275 * " finally"
6276 * finally:
6277 * ... finally block
6278 * " endtry"
6279 * ENDTRY pop trystack entry, may rethrow
6280 */
6281 static char_u *
6282compile_try(char_u *arg, cctx_T *cctx)
6283{
6284 garray_T *instr = &cctx->ctx_instr;
6285 scope_T *try_scope;
6286 scope_T *scope;
6287
6288 // scope that holds the jumps that go to catch/finally/endtry
6289 try_scope = new_scope(cctx, TRY_SCOPE);
6290 if (try_scope == NULL)
6291 return NULL;
6292
6293 // "catch" is set when the first ":catch" is found.
6294 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006295 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296 if (generate_instr(cctx, ISN_TRY) == NULL)
6297 return NULL;
6298
6299 // scope for the try block itself
6300 scope = new_scope(cctx, BLOCK_SCOPE);
6301 if (scope == NULL)
6302 return NULL;
6303
6304 return arg;
6305}
6306
6307/*
6308 * compile "catch {expr}"
6309 */
6310 static char_u *
6311compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6312{
6313 scope_T *scope = cctx->ctx_scope;
6314 garray_T *instr = &cctx->ctx_instr;
6315 char_u *p;
6316 isn_T *isn;
6317
6318 // end block scope from :try or :catch
6319 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6320 compile_endblock(cctx);
6321 scope = cctx->ctx_scope;
6322
6323 // Error if not in a :try scope
6324 if (scope == NULL || scope->se_type != TRY_SCOPE)
6325 {
6326 emsg(_(e_catch));
6327 return NULL;
6328 }
6329
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006330 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006331 {
6332 emsg(_("E1033: catch unreachable after catch-all"));
6333 return NULL;
6334 }
6335
6336 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006337 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006338 JUMP_ALWAYS, cctx) == FAIL)
6339 return NULL;
6340
6341 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006342 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343 if (isn->isn_arg.try.try_catch == 0)
6344 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006345 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006346 {
6347 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006348 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006349 isn->isn_arg.jump.jump_where = instr->ga_len;
6350 }
6351
6352 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006353 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006354 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006355 scope->se_u.se_try.ts_caught_all = TRUE;
6356 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006357 }
6358 else
6359 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006360 char_u *end;
6361 char_u *pat;
6362 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006363 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006364 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006365
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006366 // Push v:exception, push {expr} and MATCH
6367 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6368
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006369 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006370 if (*end != *p)
6371 {
6372 semsg(_("E1067: Separator mismatch: %s"), p);
6373 vim_free(tofree);
6374 return FAIL;
6375 }
6376 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006377 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006378 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006379 len = (int)(end - tofree);
6380 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006381 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006382 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006383 if (pat == NULL)
6384 return FAIL;
6385 if (generate_PUSHS(cctx, pat) == FAIL)
6386 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006388 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6389 return NULL;
6390
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006391 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006392 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6393 return NULL;
6394 }
6395
6396 if (generate_instr(cctx, ISN_CATCH) == NULL)
6397 return NULL;
6398
6399 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6400 return NULL;
6401 return p;
6402}
6403
6404 static char_u *
6405compile_finally(char_u *arg, cctx_T *cctx)
6406{
6407 scope_T *scope = cctx->ctx_scope;
6408 garray_T *instr = &cctx->ctx_instr;
6409 isn_T *isn;
6410
6411 // end block scope from :try or :catch
6412 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6413 compile_endblock(cctx);
6414 scope = cctx->ctx_scope;
6415
6416 // Error if not in a :try scope
6417 if (scope == NULL || scope->se_type != TRY_SCOPE)
6418 {
6419 emsg(_(e_finally));
6420 return NULL;
6421 }
6422
6423 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006424 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006425 if (isn->isn_arg.try.try_finally != 0)
6426 {
6427 emsg(_(e_finally_dup));
6428 return NULL;
6429 }
6430
6431 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006432 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006433
Bram Moolenaar585fea72020-04-02 22:33:21 +02006434 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006435 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006436 {
6437 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006438 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006439 isn->isn_arg.jump.jump_where = instr->ga_len;
6440 }
6441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006442 // TODO: set index in ts_finally_label jumps
6443
6444 return arg;
6445}
6446
6447 static char_u *
6448compile_endtry(char_u *arg, cctx_T *cctx)
6449{
6450 scope_T *scope = cctx->ctx_scope;
6451 garray_T *instr = &cctx->ctx_instr;
6452 isn_T *isn;
6453
6454 // end block scope from :catch or :finally
6455 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6456 compile_endblock(cctx);
6457 scope = cctx->ctx_scope;
6458
6459 // Error if not in a :try scope
6460 if (scope == NULL || scope->se_type != TRY_SCOPE)
6461 {
6462 if (scope == NULL)
6463 emsg(_(e_no_endtry));
6464 else if (scope->se_type == WHILE_SCOPE)
6465 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006466 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006467 emsg(_(e_endfor));
6468 else
6469 emsg(_(e_endif));
6470 return NULL;
6471 }
6472
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006473 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006474 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6475 {
6476 emsg(_("E1032: missing :catch or :finally"));
6477 return NULL;
6478 }
6479
6480 // Fill in the "end" label in jumps at the end of the blocks, if not done
6481 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006482 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006483
6484 // End :catch or :finally scope: set value in ISN_TRY instruction
6485 if (isn->isn_arg.try.try_finally == 0)
6486 isn->isn_arg.try.try_finally = instr->ga_len;
6487 compile_endblock(cctx);
6488
6489 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6490 return NULL;
6491 return arg;
6492}
6493
6494/*
6495 * compile "throw {expr}"
6496 */
6497 static char_u *
6498compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6499{
6500 char_u *p = skipwhite(arg);
6501
Bram Moolenaara5565e42020-05-09 15:44:01 +02006502 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006503 return NULL;
6504 if (may_generate_2STRING(-1, cctx) == FAIL)
6505 return NULL;
6506 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6507 return NULL;
6508
6509 return p;
6510}
6511
6512/*
6513 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006514 * compile "echomsg expr"
6515 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006516 * compile "execute expr"
6517 */
6518 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006519compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006520{
6521 char_u *p = arg;
6522 int count = 0;
6523
6524 for (;;)
6525 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006526 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006527 return NULL;
6528 ++count;
6529 p = skipwhite(p);
6530 if (ends_excmd(*p))
6531 break;
6532 }
6533
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006534 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6535 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6536 else if (cmdidx == CMD_execute)
6537 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6538 else if (cmdidx == CMD_echomsg)
6539 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6540 else
6541 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 return p;
6543}
6544
6545/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006546 * A command that is not compiled, execute with legacy code.
6547 */
6548 static char_u *
6549compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6550{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006551 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006552 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006553
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006554 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006555 goto theend;
6556
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006557 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
6558 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006559 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6560 {
6561 // expand filename in "syntax include [@group] filename"
6562 has_expr = TRUE;
6563 eap->arg = skipwhite(eap->arg + 7);
6564 if (*eap->arg == '@')
6565 eap->arg = skiptowhite(eap->arg);
6566 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006567
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006568 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006569 {
6570 int count = 0;
6571 char_u *start = skipwhite(line);
6572
6573 // :cmd xxx`=expr1`yyy`=expr2`zzz
6574 // PUSHS ":cmd xxx"
6575 // eval expr1
6576 // PUSHS "yyy"
6577 // eval expr2
6578 // PUSHS "zzz"
6579 // EXECCONCAT 5
6580 for (;;)
6581 {
6582 if (p > start)
6583 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006584 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006585 ++count;
6586 }
6587 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006588 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006589 return NULL;
6590 may_generate_2STRING(-1, cctx);
6591 ++count;
6592 p = skipwhite(p);
6593 if (*p != '`')
6594 {
6595 emsg(_("E1083: missing backtick"));
6596 return NULL;
6597 }
6598 start = p + 1;
6599
6600 p = (char_u *)strstr((char *)start, "`=");
6601 if (p == NULL)
6602 {
6603 if (*skipwhite(start) != NUL)
6604 {
6605 generate_PUSHS(cctx, vim_strsave(start));
6606 ++count;
6607 }
6608 break;
6609 }
6610 }
6611 generate_EXECCONCAT(cctx, count);
6612 }
6613 else
6614 generate_EXEC(cctx, line);
6615
6616theend:
6617 return (char_u *)"";
6618}
6619
6620/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006621 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006622 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006623 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006624 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006625add_def_function(ufunc_T *ufunc)
6626{
6627 dfunc_T *dfunc;
6628
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006629 if (def_functions.ga_len == 0)
6630 {
6631 // The first position is not used, so that a zero uf_dfunc_idx means it
6632 // wasn't set.
6633 if (ga_grow(&def_functions, 1) == FAIL)
6634 return FAIL;
6635 ++def_functions.ga_len;
6636 }
6637
Bram Moolenaar09689a02020-05-09 22:50:08 +02006638 // Add the function to "def_functions".
6639 if (ga_grow(&def_functions, 1) == FAIL)
6640 return FAIL;
6641 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6642 CLEAR_POINTER(dfunc);
6643 dfunc->df_idx = def_functions.ga_len;
6644 ufunc->uf_dfunc_idx = dfunc->df_idx;
6645 dfunc->df_ufunc = ufunc;
6646 ++def_functions.ga_len;
6647 return OK;
6648}
6649
6650/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006651 * After ex_function() has collected all the function lines: parse and compile
6652 * the lines into instructions.
6653 * Adds the function to "def_functions".
6654 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6655 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006656 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006657 * This can be used recursively through compile_lambda(), which may reallocate
6658 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006659 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006660 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006661 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006662compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006663{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664 char_u *line = NULL;
6665 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006666 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006667 cctx_T cctx;
6668 garray_T *instr;
6669 int called_emsg_before = called_emsg;
6670 int ret = FAIL;
6671 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006672 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006673 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006675 // When using a function that was compiled before: Free old instructions.
6676 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006677 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006679 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6680 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006681 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006682 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006683 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006684 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685
Bram Moolenaara80faa82020-04-12 19:37:17 +02006686 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006687 cctx.ctx_ufunc = ufunc;
6688 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006689 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006690 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6691 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6692 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6693 cctx.ctx_type_list = &ufunc->uf_type_list;
6694 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6695 instr = &cctx.ctx_instr;
6696
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006697 // Set the context to the function, it may be compiled when called from
6698 // another script. Set the script version to the most modern one.
6699 // The line number will be set in next_line_from_context().
6700 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006701 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6702
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006703 // Make sure error messages are OK.
6704 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6705 if (do_estack_push)
6706 estack_push_ufunc(ufunc, 1);
6707
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006708 if (ufunc->uf_def_args.ga_len > 0)
6709 {
6710 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006711 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006712 int i;
6713 char_u *arg;
6714 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6715
6716 // Produce instructions for the default values of optional arguments.
6717 // Store the instruction index in uf_def_arg_idx[] so that we know
6718 // where to start when the function is called, depending on the number
6719 // of arguments.
6720 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6721 if (ufunc->uf_def_arg_idx == NULL)
6722 goto erret;
6723 for (i = 0; i < count; ++i)
6724 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006725 garray_T *stack = &cctx.ctx_type_stack;
6726 type_T *val_type;
6727 int arg_idx = first_def_arg + i;
6728
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006729 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6730 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006731 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006732 goto erret;
6733
6734 // If no type specified use the type of the default value.
6735 // Otherwise check that the default value type matches the
6736 // specified type.
6737 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6738 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6739 ufunc->uf_arg_types[arg_idx] = val_type;
6740 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6741 == FAIL)
6742 {
6743 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6744 arg_idx + 1);
6745 goto erret;
6746 }
6747
6748 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006749 goto erret;
6750 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006751 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6752 }
6753
6754 /*
6755 * Loop over all the lines of the function and generate instructions.
6756 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006757 for (;;)
6758 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006759 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006760 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006761 char_u *cmd;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006762
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006763 // Bail out on the first error to avoid a flood of errors and report
6764 // the right line number when inside try/catch.
6765 if (emsg_before != called_emsg)
6766 goto erret;
6767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006768 if (line != NULL && *line == '|')
6769 // the line continues after a '|'
6770 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006771 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006772 && !(*line == '#' && (line == cctx.ctx_line_start
6773 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006774 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006775 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006776 goto erret;
6777 }
6778 else
6779 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006780 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006781 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006782 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006783 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006784 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006785 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786
Bram Moolenaara80faa82020-04-12 19:37:17 +02006787 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006788 ea.cmdlinep = &line;
6789 ea.cmd = skipwhite(line);
6790
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006791 // Some things can be recognized by the first character.
6792 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006793 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006794 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006795 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006796 if (ea.cmd[1] != '{')
6797 {
6798 line = (char_u *)"";
6799 continue;
6800 }
6801 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006802
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006803 case '}':
6804 {
6805 // "}" ends a block scope
6806 scopetype_T stype = cctx.ctx_scope == NULL
6807 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006809 if (stype == BLOCK_SCOPE)
6810 {
6811 compile_endblock(&cctx);
6812 line = ea.cmd;
6813 }
6814 else
6815 {
6816 emsg(_("E1025: using } outside of a block scope"));
6817 goto erret;
6818 }
6819 if (line != NULL)
6820 line = skipwhite(ea.cmd + 1);
6821 continue;
6822 }
6823
6824 case '{':
6825 // "{" starts a block scope
6826 // "{'a': 1}->func() is something else
6827 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6828 {
6829 line = compile_block(ea.cmd, &cctx);
6830 continue;
6831 }
6832 break;
6833
6834 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006835 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006836 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006837 }
6838
6839 /*
6840 * COMMAND MODIFIERS
6841 */
6842 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6843 {
6844 if (errormsg != NULL)
6845 goto erret;
6846 // empty line or comment
6847 line = (char_u *)"";
6848 continue;
6849 }
6850
6851 // Skip ":call" to get to the function name.
6852 if (checkforcmd(&ea.cmd, "call", 3))
6853 ea.cmd = skipwhite(ea.cmd);
6854
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006855 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006856 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006857 char_u *pskip;
6858
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006859 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006860 // find what follows.
6861 // Skip over "var.member", "var[idx]" and the like.
6862 // Also "&opt = val", "$ENV = val" and "@r = val".
6863 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006864 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006865 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006866 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006867 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006868 char_u *var_end;
6869 int oplen;
6870 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006871
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006872 var_end = find_name_end(pskip, NULL, NULL,
6873 FNE_CHECK_START | FNE_INCL_BR);
6874 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006875 if (oplen > 0)
6876 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006877 size_t len = p - ea.cmd;
6878
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006879 // Recognize an assignment if we recognize the variable
6880 // name:
6881 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006882 // "local = expr" where "local" is a local var.
6883 // "script = expr" where "script" is a script-local var.
6884 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006885 // "&opt = expr"
6886 // "$ENV = expr"
6887 // "@r = expr"
6888 if (*ea.cmd == '&'
6889 || *ea.cmd == '$'
6890 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006891 || ((len) > 2 && ea.cmd[1] == ':')
6892 || lookup_local(ea.cmd, len, &cctx) != NULL
6893 || lookup_arg(ea.cmd, len, NULL, NULL,
6894 NULL, &cctx) == OK
6895 || lookup_script(ea.cmd, len) == OK
6896 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006897 {
6898 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006899 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006900 goto erret;
6901 continue;
6902 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006903 }
6904 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006905
6906 if (*ea.cmd == '[')
6907 {
6908 // [var, var] = expr
6909 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6910 if (line == NULL)
6911 goto erret;
6912 if (line != ea.cmd)
6913 continue;
6914 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006915 }
6916
6917 /*
6918 * COMMAND after range
6919 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006920 cmd = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006921 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006922 if (ea.cmd > cmd && !starts_with_colon)
6923 {
6924 emsg(_(e_colon_required));
6925 goto erret;
6926 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006927 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006928 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006929 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930
6931 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6932 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006933 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006934 {
6935 line += STRLEN(line);
6936 continue;
6937 }
6938
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006939 // Expression or function call.
6940 if (ea.cmdidx == CMD_eval)
6941 {
6942 p = ea.cmd;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006943 if (compile_expr0(&p, &cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944 goto erret;
6945
6946 // drop the return value
6947 generate_instr_drop(&cctx, ISN_DROP, 1);
6948 line = p;
6949 continue;
6950 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006951 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006952 iemsg("Command from find_ex_command() not handled");
6953 goto erret;
6954 }
6955
6956 p = skipwhite(p);
6957
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006958 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02006959 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006960 && ea.cmdidx != CMD_elseif
6961 && ea.cmdidx != CMD_else
6962 && ea.cmdidx != CMD_endif)
6963 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006964 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006965 continue;
6966 }
6967
Bram Moolenaarefd88552020-06-18 20:50:10 +02006968 if (ea.cmdidx != CMD_elseif
6969 && ea.cmdidx != CMD_else
6970 && ea.cmdidx != CMD_endif
6971 && ea.cmdidx != CMD_endfor
6972 && ea.cmdidx != CMD_endwhile
6973 && ea.cmdidx != CMD_catch
6974 && ea.cmdidx != CMD_finally
6975 && ea.cmdidx != CMD_endtry)
6976 {
6977 if (cctx.ctx_had_return)
6978 {
6979 emsg(_("E1095: Unreachable code after :return"));
6980 goto erret;
6981 }
6982 }
6983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006984 switch (ea.cmdidx)
6985 {
6986 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006987 ea.arg = p;
6988 line = compile_nested_function(&ea, &cctx);
6989 break;
6990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006991 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006992 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006993 goto erret;
6994
6995 case CMD_return:
6996 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006997 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006998 break;
6999
7000 case CMD_let:
7001 case CMD_const:
7002 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007003 if (line == p)
7004 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007005 break;
7006
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007007 case CMD_unlet:
7008 case CMD_unlockvar:
7009 case CMD_lockvar:
7010 line = compile_unletlock(p, &ea, &cctx);
7011 break;
7012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007013 case CMD_import:
7014 line = compile_import(p, &cctx);
7015 break;
7016
7017 case CMD_if:
7018 line = compile_if(p, &cctx);
7019 break;
7020 case CMD_elseif:
7021 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007022 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023 break;
7024 case CMD_else:
7025 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007026 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027 break;
7028 case CMD_endif:
7029 line = compile_endif(p, &cctx);
7030 break;
7031
7032 case CMD_while:
7033 line = compile_while(p, &cctx);
7034 break;
7035 case CMD_endwhile:
7036 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007037 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 break;
7039
7040 case CMD_for:
7041 line = compile_for(p, &cctx);
7042 break;
7043 case CMD_endfor:
7044 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007045 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007046 break;
7047 case CMD_continue:
7048 line = compile_continue(p, &cctx);
7049 break;
7050 case CMD_break:
7051 line = compile_break(p, &cctx);
7052 break;
7053
7054 case CMD_try:
7055 line = compile_try(p, &cctx);
7056 break;
7057 case CMD_catch:
7058 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007059 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 break;
7061 case CMD_finally:
7062 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007063 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064 break;
7065 case CMD_endtry:
7066 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007067 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007068 break;
7069 case CMD_throw:
7070 line = compile_throw(p, &cctx);
7071 break;
7072
7073 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007074 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007075 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007076 case CMD_echomsg:
7077 case CMD_echoerr:
7078 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007079 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007081 // TODO: other commands with an expression argument
7082
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007083 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007084 // Not recognized, execute with do_cmdline_cmd().
7085 ea.arg = p;
7086 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007087 break;
7088 }
7089 if (line == NULL)
7090 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007091 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092
7093 if (cctx.ctx_type_stack.ga_len < 0)
7094 {
7095 iemsg("Type stack underflow");
7096 goto erret;
7097 }
7098 }
7099
7100 if (cctx.ctx_scope != NULL)
7101 {
7102 if (cctx.ctx_scope->se_type == IF_SCOPE)
7103 emsg(_(e_endif));
7104 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7105 emsg(_(e_endwhile));
7106 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7107 emsg(_(e_endfor));
7108 else
7109 emsg(_("E1026: Missing }"));
7110 goto erret;
7111 }
7112
Bram Moolenaarefd88552020-06-18 20:50:10 +02007113 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007114 {
7115 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7116 {
7117 emsg(_("E1027: Missing return statement"));
7118 goto erret;
7119 }
7120
7121 // Return zero if there is no return at the end.
7122 generate_PUSHNR(&cctx, 0);
7123 generate_instr(&cctx, ISN_RETURN);
7124 }
7125
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007126 {
7127 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7128 + ufunc->uf_dfunc_idx;
7129 dfunc->df_deleted = FALSE;
7130 dfunc->df_instr = instr->ga_data;
7131 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007132 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007133 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007134 if (cctx.ctx_outer_used)
7135 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007136 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138
7139 ret = OK;
7140
7141erret:
7142 if (ret == FAIL)
7143 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007144 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007145 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7146 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007147
7148 for (idx = 0; idx < instr->ga_len; ++idx)
7149 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007150 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007151
Bram Moolenaar45a15082020-05-25 00:28:33 +02007152 // if using the last entry in the table we might as well remove it
7153 if (!dfunc->df_deleted
7154 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007155 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007156 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007157
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007158 while (cctx.ctx_scope != NULL)
7159 drop_scope(&cctx);
7160
Bram Moolenaar20431c92020-03-20 18:39:46 +01007161 // Don't execute this function body.
7162 ga_clear_strings(&ufunc->uf_lines);
7163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164 if (errormsg != NULL)
7165 emsg(errormsg);
7166 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007167 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168 }
7169
7170 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007171 if (do_estack_push)
7172 estack_pop();
7173
Bram Moolenaar20431c92020-03-20 18:39:46 +01007174 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007175 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007176 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007177 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007178}
7179
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007180 void
7181set_function_type(ufunc_T *ufunc)
7182{
7183 int varargs = ufunc->uf_va_name != NULL;
7184 int argcount = ufunc->uf_args.ga_len;
7185
7186 // Create a type for the function, with the return type and any
7187 // argument types.
7188 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7189 // The type is included in "tt_args".
7190 if (argcount > 0 || varargs)
7191 {
7192 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7193 argcount, &ufunc->uf_type_list);
7194 // Add argument types to the function type.
7195 if (func_type_add_arg_types(ufunc->uf_func_type,
7196 argcount + varargs,
7197 &ufunc->uf_type_list) == FAIL)
7198 return;
7199 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7200 ufunc->uf_func_type->tt_min_argcount =
7201 argcount - ufunc->uf_def_args.ga_len;
7202 if (ufunc->uf_arg_types == NULL)
7203 {
7204 int i;
7205
7206 // lambda does not have argument types.
7207 for (i = 0; i < argcount; ++i)
7208 ufunc->uf_func_type->tt_args[i] = &t_any;
7209 }
7210 else
7211 mch_memmove(ufunc->uf_func_type->tt_args,
7212 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7213 if (varargs)
7214 {
7215 ufunc->uf_func_type->tt_args[argcount] =
7216 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7217 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7218 }
7219 }
7220 else
7221 // No arguments, can use a predefined type.
7222 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7223 argcount, &ufunc->uf_type_list);
7224}
7225
7226
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007227/*
7228 * Delete an instruction, free what it contains.
7229 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007230 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007231delete_instr(isn_T *isn)
7232{
7233 switch (isn->isn_type)
7234 {
7235 case ISN_EXEC:
7236 case ISN_LOADENV:
7237 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007238 case ISN_LOADB:
7239 case ISN_LOADW:
7240 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007241 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007242 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007243 case ISN_PUSHEXC:
7244 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007245 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007246 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007247 case ISN_STOREB:
7248 case ISN_STOREW:
7249 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007250 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007251 vim_free(isn->isn_arg.string);
7252 break;
7253
7254 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007255 case ISN_STORES:
7256 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007257 break;
7258
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007259 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007260 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007261 vim_free(isn->isn_arg.unlet.ul_name);
7262 break;
7263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007264 case ISN_STOREOPT:
7265 vim_free(isn->isn_arg.storeopt.so_name);
7266 break;
7267
7268 case ISN_PUSHBLOB: // push blob isn_arg.blob
7269 blob_unref(isn->isn_arg.blob);
7270 break;
7271
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007272 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007273#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007274 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007275#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007276 break;
7277
7278 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007279#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007280 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007281#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007282 break;
7283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007284 case ISN_UCALL:
7285 vim_free(isn->isn_arg.ufunc.cuf_name);
7286 break;
7287
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007288 case ISN_FUNCREF:
7289 {
7290 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7291 + isn->isn_arg.funcref.fr_func;
7292 func_ptr_unref(dfunc->df_ufunc);
7293 }
7294 break;
7295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007296 case ISN_2BOOL:
7297 case ISN_2STRING:
7298 case ISN_ADDBLOB:
7299 case ISN_ADDLIST:
7300 case ISN_BCALL:
7301 case ISN_CATCH:
7302 case ISN_CHECKNR:
7303 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007304 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007305 case ISN_COMPAREANY:
7306 case ISN_COMPAREBLOB:
7307 case ISN_COMPAREBOOL:
7308 case ISN_COMPAREDICT:
7309 case ISN_COMPAREFLOAT:
7310 case ISN_COMPAREFUNC:
7311 case ISN_COMPARELIST:
7312 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007313 case ISN_COMPARESPECIAL:
7314 case ISN_COMPARESTRING:
7315 case ISN_CONCAT:
7316 case ISN_DCALL:
7317 case ISN_DROP:
7318 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007319 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007320 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007322 case ISN_EXECCONCAT:
7323 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007324 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007325 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007326 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007327 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007328 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007329 case ISN_JUMP:
7330 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007331 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007332 case ISN_LOADSCRIPT:
7333 case ISN_LOADREG:
7334 case ISN_LOADV:
7335 case ISN_NEGATENR:
7336 case ISN_NEWDICT:
7337 case ISN_NEWLIST:
7338 case ISN_OPNR:
7339 case ISN_OPFLOAT:
7340 case ISN_OPANY:
7341 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007342 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007343 case ISN_PUSHF:
7344 case ISN_PUSHNR:
7345 case ISN_PUSHBOOL:
7346 case ISN_PUSHSPEC:
7347 case ISN_RETURN:
7348 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007349 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007350 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007352 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007354 case ISN_STOREDICT:
7355 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356 case ISN_THROW:
7357 case ISN_TRY:
7358 // nothing allocated
7359 break;
7360 }
7361}
7362
7363/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007364 * Free all instructions for "dfunc".
7365 */
7366 static void
7367delete_def_function_contents(dfunc_T *dfunc)
7368{
7369 int idx;
7370
7371 ga_clear(&dfunc->df_def_args_isn);
7372
7373 if (dfunc->df_instr != NULL)
7374 {
7375 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7376 delete_instr(dfunc->df_instr + idx);
7377 VIM_CLEAR(dfunc->df_instr);
7378 }
7379
7380 dfunc->df_deleted = TRUE;
7381}
7382
7383/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007384 * When a user function is deleted, clear the contents of any associated def
7385 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007386 */
7387 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007388clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007389{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007390 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007391 {
7392 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7393 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007394
Bram Moolenaar20431c92020-03-20 18:39:46 +01007395 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007396 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397 }
7398}
7399
7400#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007401/*
7402 * Free all functions defined with ":def".
7403 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404 void
7405free_def_functions(void)
7406{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007407 int idx;
7408
7409 for (idx = 0; idx < def_functions.ga_len; ++idx)
7410 {
7411 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7412
7413 delete_def_function_contents(dfunc);
7414 }
7415
7416 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007417}
7418#endif
7419
7420
7421#endif // FEAT_EVAL