blob: 9c2eaf43292347d004bc5c180946e4a653ad00fa [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 Moolenaar086eb182020-07-01 16:00:44 +02002339 scriptitem_T *si;
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);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002616 char_u *exp_name;
2617 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002618 ufunc_T *ufunc;
2619 type_T *type;
2620
2621 // Used "import * as Name", need to lookup the member.
2622 if (*p != '.')
2623 {
2624 semsg(_("E1060: expected dot after name: %s"), start);
2625 return FAIL;
2626 }
2627 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002628 if (VIM_ISWHITE(*p))
2629 {
2630 emsg(_("E1074: no white space allowed after dot"));
2631 return FAIL;
2632 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002633
Bram Moolenaar1c991142020-07-04 13:15:31 +02002634 // isolate one name
2635 exp_name = p;
2636 while (eval_isnamec(*p))
2637 ++p;
2638 cc = *p;
2639 *p = NUL;
2640
2641 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2642 *p = cc;
2643 p = skipwhite(p);
2644
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002645 // TODO: what if it is a function?
2646 if (idx < 0)
2647 return FAIL;
2648 *end = p;
2649
2650 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2651 import->imp_sid,
2652 idx,
2653 type);
2654 }
2655 else
2656 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002657 // TODO: check this is a variable, not a function?
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002658 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2659 import->imp_sid,
2660 import->imp_var_vals_idx,
2661 import->imp_type);
2662 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 return OK;
2664 }
2665
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002666 if (error)
2667 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 return FAIL;
2669}
2670
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002671 static int
2672generate_funcref(cctx_T *cctx, char_u *name)
2673{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002674 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002675
2676 if (ufunc == NULL)
2677 return FAIL;
2678
2679 return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type);
2680}
2681
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682/*
2683 * Compile a variable name into a load instruction.
2684 * "end" points to just after the name.
2685 * When "error" is FALSE do not give an error when not found.
2686 */
2687 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002688compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689{
2690 type_T *type;
2691 char_u *name;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002692 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002694 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695
2696 if (*(*arg + 1) == ':')
2697 {
2698 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002699 if (end <= *arg + 2)
2700 name = vim_strsave((char_u *)"[empty]");
2701 else
2702 name = vim_strnsave(*arg + 2, end - (*arg + 2));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703 if (name == NULL)
2704 return FAIL;
2705
2706 if (**arg == 'v')
2707 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002708 res = generate_LOADV(cctx, name, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 }
2710 else if (**arg == 'g')
2711 {
2712 // Global variables can be defined later, thus we don't check if it
2713 // exists, give error at runtime.
2714 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
2715 }
2716 else if (**arg == 's')
2717 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002718 res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 }
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002720 else if (**arg == 'b')
2721 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002722 // Buffer-local variables can be defined later, thus we don't check
2723 // if it exists, give error at runtime.
2724 res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002725 }
2726 else if (**arg == 'w')
2727 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002728 // Window-local variables can be defined later, thus we don't check
2729 // if it exists, give error at runtime.
2730 res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002731 }
2732 else if (**arg == 't')
2733 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002734 // Tabpage-local variables can be defined later, thus we don't
2735 // check if it exists, give error at runtime.
2736 res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002737 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738 else
2739 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002740 semsg("E1075: Namespace not supported: %s", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 goto theend;
2742 }
2743 }
2744 else
2745 {
2746 size_t len = end - *arg;
2747 int idx;
2748 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002749 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750
2751 name = vim_strnsave(*arg, end - *arg);
2752 if (name == NULL)
2753 return FAIL;
2754
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002755 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002757 if (!gen_load_outer)
2758 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 }
2760 else
2761 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002762 lvar_T *lvar = lookup_local(*arg, len, cctx);
2763
2764 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002766 type = lvar->lv_type;
2767 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002768 if (lvar->lv_from_outer)
2769 gen_load_outer = TRUE;
2770 else
2771 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 }
2773 else
2774 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002775 // "var" can be script-local even without using "s:" if it
2776 // already exists.
2777 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2778 == SCRIPT_VERSION_VIM9
2779 || lookup_script(*arg, len) == OK)
2780 res = compile_load_scriptvar(cctx, name, *arg, &end,
2781 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002782
Bram Moolenaara5565e42020-05-09 15:44:01 +02002783 // When the name starts with an uppercase letter or "x:" it
2784 // can be a user defined function.
2785 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2786 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 }
2788 }
2789 if (gen_load)
2790 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002791 if (gen_load_outer)
2792 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 }
2794
2795 *arg = end;
2796
2797theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002798 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 semsg(_(e_var_notfound), name);
2800 vim_free(name);
2801 return res;
2802}
2803
2804/*
2805 * Compile the argument expressions.
2806 * "arg" points to just after the "(" and is advanced to after the ")"
2807 */
2808 static int
2809compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2810{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002811 char_u *p = *arg;
2812 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813
Bram Moolenaare6085c52020-04-12 20:19:16 +02002814 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002816 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2817 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002818 if (*p == ')')
2819 {
2820 *arg = p + 1;
2821 return OK;
2822 }
2823
Bram Moolenaara5565e42020-05-09 15:44:01 +02002824 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 return FAIL;
2826 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002827
2828 if (*p != ',' && *skipwhite(p) == ',')
2829 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002830 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002831 p = skipwhite(p);
2832 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002834 {
2835 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002836 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002837 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002838 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002839 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002840 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002842failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002843 emsg(_(e_missing_close));
2844 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845}
2846
2847/*
2848 * Compile a function call: name(arg1, arg2)
2849 * "arg" points to "name", "arg + varlen" to the "(".
2850 * "argcount_init" is 1 for "value->method()"
2851 * Instructions:
2852 * EVAL arg1
2853 * EVAL arg2
2854 * BCALL / DCALL / UCALL
2855 */
2856 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002857compile_call(
2858 char_u **arg,
2859 size_t varlen,
2860 cctx_T *cctx,
2861 ppconst_T *ppconst,
2862 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863{
2864 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002865 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 int argcount = argcount_init;
2867 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002868 char_u fname_buf[FLEN_FIXED + 1];
2869 char_u *tofree = NULL;
2870 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002872 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873
Bram Moolenaara5565e42020-05-09 15:44:01 +02002874 // we can evaluate "has('name')" at compile time
2875 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2876 {
2877 char_u *s = skipwhite(*arg + varlen + 1);
2878 typval_T argvars[2];
2879
2880 argvars[0].v_type = VAR_UNKNOWN;
2881 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002882 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002883 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002884 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002885 s = skipwhite(s);
2886 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2887 {
2888 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2889
2890 *arg = s + 1;
2891 argvars[1].v_type = VAR_UNKNOWN;
2892 tv->v_type = VAR_NUMBER;
2893 tv->vval.v_number = 0;
2894 f_has(argvars, tv);
2895 clear_tv(&argvars[0]);
2896 ++ppconst->pp_used;
2897 return OK;
2898 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002899 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002900 }
2901
2902 if (generate_ppconst(cctx, ppconst) == FAIL)
2903 return FAIL;
2904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 if (varlen >= sizeof(namebuf))
2906 {
2907 semsg(_("E1011: name too long: %s"), name);
2908 return FAIL;
2909 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002910 vim_strncpy(namebuf, *arg, varlen);
2911 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912
2913 *arg = skipwhite(*arg + varlen + 1);
2914 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002915 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002917 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 {
2919 int idx;
2920
2921 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002922 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 if (idx >= 0)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002924 res = generate_BCALL(cctx, idx, argcount);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002925 else
2926 semsg(_(e_unknownfunc), namebuf);
2927 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 }
2929
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002931 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002933 {
2934 res = generate_CALL(cctx, ufunc, argcount);
2935 goto theend;
2936 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937
2938 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002939 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002941 if (STRNCMP(namebuf, "g:", 2) != 0
2942 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002943 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002944 garray_T *stack = &cctx->ctx_type_stack;
2945 type_T *type;
2946
2947 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2948 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002949 goto theend;
2950 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002952 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002953 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002954 if (STRNCMP(namebuf, "g:", 2) == 0)
2955 res = generate_UCALL(cctx, name, argcount);
2956 else
2957 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002958
2959theend:
2960 vim_free(tofree);
2961 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962}
2963
2964// like NAMESPACE_CHAR but with 'a' and 'l'.
2965#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2966
2967/*
2968 * Find the end of a variable or function name. Unlike find_name_end() this
2969 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002970 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 * Return a pointer to just after the name. Equal to "arg" if there is no
2972 * valid name.
2973 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002974 static char_u *
2975to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976{
2977 char_u *p;
2978
2979 // Quick check for valid starting character.
2980 if (!eval_isnamec1(*arg))
2981 return arg;
2982
2983 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2984 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2985 // and can be used in slice "[n:]".
2986 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002987 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2989 break;
2990 return p;
2991}
2992
2993/*
2994 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002995 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 */
2997 char_u *
2998to_name_const_end(char_u *arg)
2999{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003000 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 typval_T rettv;
3002
3003 if (p == arg && *arg == '[')
3004 {
3005
3006 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003007 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 p = arg;
3009 }
3010 else if (p == arg && *arg == '#' && arg[1] == '{')
3011 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003012 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003014 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 p = arg;
3016 }
3017 else if (p == arg && *arg == '{')
3018 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003019 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003021 // Can be "{x -> ret}()".
3022 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003024 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 if (ret != OK)
3026 p = arg;
3027 }
3028
3029 return p;
3030}
3031
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032/*
3033 * parse a list: [expr, expr]
3034 * "*arg" points to the '['.
3035 */
3036 static int
3037compile_list(char_u **arg, cctx_T *cctx)
3038{
3039 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003040 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 int count = 0;
3042
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003043 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003045 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003046 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003047 semsg(_(e_list_end), *arg);
3048 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003049 }
3050 if (*p == ']')
3051 {
3052 ++p;
3053 // Allow for following comment, after at least one space.
3054 if (VIM_ISWHITE(*p) && *skipwhite(p) == '"')
3055 p += STRLEN(p);
3056 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003057 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003058 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 break;
3060 ++count;
3061 if (*p == ',')
3062 ++p;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003063 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 p = skipwhite(p);
3065 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003066 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067
3068 generate_NEWLIST(cctx, count);
3069 return OK;
3070}
3071
3072/*
3073 * parse a lambda: {arg, arg -> expr}
3074 * "*arg" points to the '{'.
3075 */
3076 static int
3077compile_lambda(char_u **arg, cctx_T *cctx)
3078{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 typval_T rettv;
3080 ufunc_T *ufunc;
3081
3082 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003083 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003087 ++ufunc->uf_refcount;
3088 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003089 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090
3091 // The function will have one line: "return {expr}".
3092 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003093 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003095 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003096 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 return FAIL;
3098}
3099
3100/*
3101 * Compile a lamda call: expr->{lambda}(args)
3102 * "arg" points to the "{".
3103 */
3104 static int
3105compile_lambda_call(char_u **arg, cctx_T *cctx)
3106{
3107 ufunc_T *ufunc;
3108 typval_T rettv;
3109 int argcount = 1;
3110 int ret = FAIL;
3111
3112 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003113 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 return FAIL;
3115
3116 if (**arg != '(')
3117 {
3118 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003119 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 else
3121 semsg(_(e_missing_paren), "lambda");
3122 clear_tv(&rettv);
3123 return FAIL;
3124 }
3125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 ufunc = rettv.vval.v_partial->pt_func;
3127 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003128 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003129 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003130
3131 // The function will have one line: "return {expr}".
3132 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003133 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134
3135 // compile the arguments
3136 *arg = skipwhite(*arg + 1);
3137 if (compile_arguments(arg, cctx, &argcount) == OK)
3138 // call the compiled function
3139 ret = generate_CALL(cctx, ufunc, argcount);
3140
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 return ret;
3142}
3143
3144/*
3145 * parse a dict: {'key': val} or #{key: val}
3146 * "*arg" points to the '{'.
3147 */
3148 static int
3149compile_dict(char_u **arg, cctx_T *cctx, int literal)
3150{
3151 garray_T *instr = &cctx->ctx_instr;
3152 int count = 0;
3153 dict_T *d = dict_alloc();
3154 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003155 char_u *whitep = *arg;
3156 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157
3158 if (d == NULL)
3159 return FAIL;
3160 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003161 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 {
3163 char_u *key = NULL;
3164
Bram Moolenaar23c55272020-06-21 16:58:13 +02003165 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003166 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003167 *arg = NULL;
3168 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003169 }
3170
3171 if (**arg == '}')
3172 break;
3173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 if (literal)
3175 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003176 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177
Bram Moolenaar2c330432020-04-13 14:41:35 +02003178 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 {
3180 semsg(_("E1014: Invalid key: %s"), *arg);
3181 return FAIL;
3182 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003183 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 if (generate_PUSHS(cctx, key) == FAIL)
3185 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003186 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 }
3188 else
3189 {
3190 isn_T *isn;
3191
Bram Moolenaara5565e42020-05-09 15:44:01 +02003192 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 return FAIL;
3194 // TODO: check type is string
3195 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3196 if (isn->isn_type == ISN_PUSHS)
3197 key = isn->isn_arg.string;
3198 }
3199
3200 // Check for duplicate keys, if using string keys.
3201 if (key != NULL)
3202 {
3203 item = dict_find(d, key, -1);
3204 if (item != NULL)
3205 {
3206 semsg(_(e_duplicate_key), key);
3207 goto failret;
3208 }
3209 item = dictitem_alloc(key);
3210 if (item != NULL)
3211 {
3212 item->di_tv.v_type = VAR_UNKNOWN;
3213 item->di_tv.v_lock = 0;
3214 if (dict_add(d, item) == FAIL)
3215 dictitem_free(item);
3216 }
3217 }
3218
3219 *arg = skipwhite(*arg);
3220 if (**arg != ':')
3221 {
3222 semsg(_(e_missing_dict_colon), *arg);
3223 return FAIL;
3224 }
3225
Bram Moolenaar2c330432020-04-13 14:41:35 +02003226 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003228 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 }
3233
Bram Moolenaara5565e42020-05-09 15:44:01 +02003234 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 return FAIL;
3236 ++count;
3237
Bram Moolenaar2c330432020-04-13 14:41:35 +02003238 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003239 *arg = skipwhite(*arg);
3240 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003241 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003242 *arg = NULL;
3243 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003244 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 if (**arg == '}')
3246 break;
3247 if (**arg != ',')
3248 {
3249 semsg(_(e_missing_dict_comma), *arg);
3250 goto failret;
3251 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003252 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 *arg = skipwhite(*arg + 1);
3254 }
3255
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 *arg = *arg + 1;
3257
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003258 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003259 p = skipwhite(*arg);
3260 if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p)))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003261 *arg += STRLEN(*arg);
3262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 dict_unref(d);
3264 return generate_NEWDICT(cctx, count);
3265
3266failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003267 if (*arg == NULL)
3268 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 dict_unref(d);
3270 return FAIL;
3271}
3272
3273/*
3274 * Compile "&option".
3275 */
3276 static int
3277compile_get_option(char_u **arg, cctx_T *cctx)
3278{
3279 typval_T rettv;
3280 char_u *start = *arg;
3281 int ret;
3282
3283 // parse the option and get the current value to get the type.
3284 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003285 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 if (ret == OK)
3287 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003288 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 char_u *name = vim_strnsave(start, *arg - start);
3290 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3291
3292 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3293 vim_free(name);
3294 }
3295 clear_tv(&rettv);
3296
3297 return ret;
3298}
3299
3300/*
3301 * Compile "$VAR".
3302 */
3303 static int
3304compile_get_env(char_u **arg, cctx_T *cctx)
3305{
3306 char_u *start = *arg;
3307 int len;
3308 int ret;
3309 char_u *name;
3310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 ++*arg;
3312 len = get_env_len(arg);
3313 if (len == 0)
3314 {
3315 semsg(_(e_syntax_at), start - 1);
3316 return FAIL;
3317 }
3318
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003319 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 name = vim_strnsave(start, len + 1);
3321 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3322 vim_free(name);
3323 return ret;
3324}
3325
3326/*
3327 * Compile "@r".
3328 */
3329 static int
3330compile_get_register(char_u **arg, cctx_T *cctx)
3331{
3332 int ret;
3333
3334 ++*arg;
3335 if (**arg == NUL)
3336 {
3337 semsg(_(e_syntax_at), *arg - 1);
3338 return FAIL;
3339 }
3340 if (!valid_yank_reg(**arg, TRUE))
3341 {
3342 emsg_invreg(**arg);
3343 return FAIL;
3344 }
3345 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3346 ++*arg;
3347 return ret;
3348}
3349
3350/*
3351 * Apply leading '!', '-' and '+' to constant "rettv".
3352 */
3353 static int
3354apply_leader(typval_T *rettv, char_u *start, char_u *end)
3355{
3356 char_u *p = end;
3357
3358 // this works from end to start
3359 while (p > start)
3360 {
3361 --p;
3362 if (*p == '-' || *p == '+')
3363 {
3364 // only '-' has an effect, for '+' we only check the type
3365#ifdef FEAT_FLOAT
3366 if (rettv->v_type == VAR_FLOAT)
3367 {
3368 if (*p == '-')
3369 rettv->vval.v_float = -rettv->vval.v_float;
3370 }
3371 else
3372#endif
3373 {
3374 varnumber_T val;
3375 int error = FALSE;
3376
3377 // tv_get_number_chk() accepts a string, but we don't want that
3378 // here
3379 if (check_not_string(rettv) == FAIL)
3380 return FAIL;
3381 val = tv_get_number_chk(rettv, &error);
3382 clear_tv(rettv);
3383 if (error)
3384 return FAIL;
3385 if (*p == '-')
3386 val = -val;
3387 rettv->v_type = VAR_NUMBER;
3388 rettv->vval.v_number = val;
3389 }
3390 }
3391 else
3392 {
3393 int v = tv2bool(rettv);
3394
3395 // '!' is permissive in the type.
3396 clear_tv(rettv);
3397 rettv->v_type = VAR_BOOL;
3398 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3399 }
3400 }
3401 return OK;
3402}
3403
3404/*
3405 * Recognize v: variables that are constants and set "rettv".
3406 */
3407 static void
3408get_vim_constant(char_u **arg, typval_T *rettv)
3409{
3410 if (STRNCMP(*arg, "v:true", 6) == 0)
3411 {
3412 rettv->v_type = VAR_BOOL;
3413 rettv->vval.v_number = VVAL_TRUE;
3414 *arg += 6;
3415 }
3416 else if (STRNCMP(*arg, "v:false", 7) == 0)
3417 {
3418 rettv->v_type = VAR_BOOL;
3419 rettv->vval.v_number = VVAL_FALSE;
3420 *arg += 7;
3421 }
3422 else if (STRNCMP(*arg, "v:null", 6) == 0)
3423 {
3424 rettv->v_type = VAR_SPECIAL;
3425 rettv->vval.v_number = VVAL_NULL;
3426 *arg += 6;
3427 }
3428 else if (STRNCMP(*arg, "v:none", 6) == 0)
3429 {
3430 rettv->v_type = VAR_SPECIAL;
3431 rettv->vval.v_number = VVAL_NONE;
3432 *arg += 6;
3433 }
3434}
3435
Bram Moolenaar61a89812020-05-07 16:58:17 +02003436 static exptype_T
3437get_compare_type(char_u *p, int *len, int *type_is)
3438{
3439 exptype_T type = EXPR_UNKNOWN;
3440 int i;
3441
3442 switch (p[0])
3443 {
3444 case '=': if (p[1] == '=')
3445 type = EXPR_EQUAL;
3446 else if (p[1] == '~')
3447 type = EXPR_MATCH;
3448 break;
3449 case '!': if (p[1] == '=')
3450 type = EXPR_NEQUAL;
3451 else if (p[1] == '~')
3452 type = EXPR_NOMATCH;
3453 break;
3454 case '>': if (p[1] != '=')
3455 {
3456 type = EXPR_GREATER;
3457 *len = 1;
3458 }
3459 else
3460 type = EXPR_GEQUAL;
3461 break;
3462 case '<': if (p[1] != '=')
3463 {
3464 type = EXPR_SMALLER;
3465 *len = 1;
3466 }
3467 else
3468 type = EXPR_SEQUAL;
3469 break;
3470 case 'i': if (p[1] == 's')
3471 {
3472 // "is" and "isnot"; but not a prefix of a name
3473 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3474 *len = 5;
3475 i = p[*len];
3476 if (!isalnum(i) && i != '_')
3477 {
3478 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3479 *type_is = TRUE;
3480 }
3481 }
3482 break;
3483 }
3484 return type;
3485}
3486
3487/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 * Compile code to apply '-', '+' and '!'.
3489 */
3490 static int
3491compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3492{
3493 char_u *p = end;
3494
3495 // this works from end to start
3496 while (p > start)
3497 {
3498 --p;
3499 if (*p == '-' || *p == '+')
3500 {
3501 int negate = *p == '-';
3502 isn_T *isn;
3503
3504 // TODO: check type
3505 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3506 {
3507 --p;
3508 if (*p == '-')
3509 negate = !negate;
3510 }
3511 // only '-' has an effect, for '+' we only check the type
3512 if (negate)
3513 isn = generate_instr(cctx, ISN_NEGATENR);
3514 else
3515 isn = generate_instr(cctx, ISN_CHECKNR);
3516 if (isn == NULL)
3517 return FAIL;
3518 }
3519 else
3520 {
3521 int invert = TRUE;
3522
3523 while (p > start && p[-1] == '!')
3524 {
3525 --p;
3526 invert = !invert;
3527 }
3528 if (generate_2BOOL(cctx, invert) == FAIL)
3529 return FAIL;
3530 }
3531 }
3532 return OK;
3533}
3534
3535/*
3536 * Compile whatever comes after "name" or "name()".
3537 */
3538 static int
3539compile_subscript(
3540 char_u **arg,
3541 cctx_T *cctx,
3542 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003543 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003544 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545{
3546 for (;;)
3547 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003548 char_u *p = skipwhite(*arg);
3549
3550 if (*p == NUL || (VIM_ISWHITE(**arg) && comment_start(p)))
3551 {
3552 char_u *next = peek_next_line(cctx);
3553
3554 // If a following line starts with "->{" or "->X" advance to that
3555 // line, so that a line break before "->" is allowed.
3556 if (next != NULL && next[0] == '-' && next[1] == '>'
3557 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
3558 {
3559 next = next_line_from_context(cctx, TRUE);
3560 if (next == NULL)
3561 return FAIL;
3562 *arg = skipwhite(next);
3563 }
3564 }
3565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 if (**arg == '(')
3567 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003568 garray_T *stack = &cctx->ctx_type_stack;
3569 type_T *type;
3570 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003572 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003573 return FAIL;
3574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003576 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 *arg = skipwhite(*arg + 1);
3579 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3580 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003581 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 return FAIL;
3583 }
3584 else if (**arg == '-' && (*arg)[1] == '>')
3585 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003586 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003587 return FAIL;
3588
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 // something->method()
3590 // Apply the '!', '-' and '+' first:
3591 // -1.0->func() works like (-1.0)->func()
3592 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3593 return FAIL;
3594 *start_leader = end_leader; // don't apply again later
3595
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003596 p = *arg + 2;
3597 *arg = skipwhite(p);
3598 if (may_get_next_line(p, arg, cctx) == FAIL)
3599 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 if (**arg == '{')
3601 {
3602 // lambda call: list->{lambda}
3603 if (compile_lambda_call(arg, cctx) == FAIL)
3604 return FAIL;
3605 }
3606 else
3607 {
3608 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003609 p = *arg;
3610 if (ASCII_ISALPHA(*p) && p[1] == ':')
3611 p += 2;
3612 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 ;
3614 if (*p != '(')
3615 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003616 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 return FAIL;
3618 }
3619 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003620 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 return FAIL;
3622 }
3623 }
3624 else if (**arg == '[')
3625 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003626 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003627 type_T **typep;
3628
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003629 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003630 // dict member: dict[key]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003631 // TODO: blob index
3632 // TODO: more arguments
3633 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003634 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003635 return FAIL;
3636
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003637 p = *arg + 1;
3638 *arg = skipwhite(p);
3639 if (may_get_next_line(p, arg, cctx) == FAIL)
3640 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003641 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 return FAIL;
3643
3644 if (**arg != ']')
3645 {
3646 emsg(_(e_missbrac));
3647 return FAIL;
3648 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003649 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003651 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
3652 if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003653 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003654 if ((*typep)->tt_type == VAR_LIST)
3655 *typep = (*typep)->tt_member;
3656 if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
3657 return FAIL;
3658 }
3659 else if ((*typep)->tt_type == VAR_DICT)
3660 {
3661 *typep = (*typep)->tt_member;
3662 if (may_generate_2STRING(-1, cctx) == FAIL)
3663 return FAIL;
3664 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3665 return FAIL;
3666 }
3667 else
3668 {
3669 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003670 return FAIL;
3671 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 }
3673 else if (**arg == '.' && (*arg)[1] != '.')
3674 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003675 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003676 return FAIL;
3677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 ++*arg;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003679 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3680 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003682 p = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 if (eval_isnamec1(*p))
3684 while (eval_isnamec(*p))
3685 MB_PTR_ADV(p);
3686 if (p == *arg)
3687 {
3688 semsg(_(e_syntax_at), *arg);
3689 return FAIL;
3690 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003691 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 return FAIL;
3693 *arg = p;
3694 }
3695 else
3696 break;
3697 }
3698
3699 // TODO - see handle_subscript():
3700 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3701 // Don't do this when "Func" is already a partial that was bound
3702 // explicitly (pt_auto is FALSE).
3703
3704 return OK;
3705}
3706
3707/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003708 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3709 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003711 * If the value is a constant "ppconst->pp_ret" will be set.
3712 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003713 *
3714 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 */
3716
3717/*
3718 * number number constant
3719 * 0zFFFFFFFF Blob constant
3720 * "string" string constant
3721 * 'string' literal string constant
3722 * &option-name option value
3723 * @r register contents
3724 * identifier variable value
3725 * function() function call
3726 * $VAR environment variable
3727 * (expression) nested expression
3728 * [expr, expr] List
3729 * {key: val, key: val} Dictionary
3730 * #{key: val, key: val} Dictionary with literal keys
3731 *
3732 * Also handle:
3733 * ! in front logical NOT
3734 * - in front unary minus
3735 * + in front unary plus (ignored)
3736 * trailing (arg) funcref/partial call
3737 * trailing [] subscript in String or List
3738 * trailing .name entry in Dictionary
3739 * trailing ->name() method call
3740 */
3741 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003742compile_expr7(
3743 char_u **arg,
3744 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003745 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 char_u *start_leader, *end_leader;
3748 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003749 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003750 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751
3752 /*
3753 * Skip '!', '-' and '+' characters. They are handled later.
3754 */
3755 start_leader = *arg;
3756 while (**arg == '!' || **arg == '-' || **arg == '+')
3757 *arg = skipwhite(*arg + 1);
3758 end_leader = *arg;
3759
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003760 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 switch (**arg)
3762 {
3763 /*
3764 * Number constant.
3765 */
3766 case '0': // also for blob starting with 0z
3767 case '1':
3768 case '2':
3769 case '3':
3770 case '4':
3771 case '5':
3772 case '6':
3773 case '7':
3774 case '8':
3775 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003776 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 return FAIL;
3778 break;
3779
3780 /*
3781 * String constant: "string".
3782 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003783 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 return FAIL;
3785 break;
3786
3787 /*
3788 * Literal string constant: 'str''ing'.
3789 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003790 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 return FAIL;
3792 break;
3793
3794 /*
3795 * Constant Vim variable.
3796 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003797 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 ret = NOTDONE;
3799 break;
3800
3801 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003802 * "true" constant
3803 */
3804 case 't': if (STRNCMP(*arg, "true", 4) == 0
3805 && !eval_isnamec((*arg)[4]))
3806 {
3807 *arg += 4;
3808 rettv->v_type = VAR_BOOL;
3809 rettv->vval.v_number = VVAL_TRUE;
3810 }
3811 else
3812 ret = NOTDONE;
3813 break;
3814
3815 /*
3816 * "false" constant
3817 */
3818 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3819 && !eval_isnamec((*arg)[5]))
3820 {
3821 *arg += 5;
3822 rettv->v_type = VAR_BOOL;
3823 rettv->vval.v_number = VVAL_FALSE;
3824 }
3825 else
3826 ret = NOTDONE;
3827 break;
3828
3829 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 * List: [expr, expr]
3831 */
3832 case '[': ret = compile_list(arg, cctx);
3833 break;
3834
3835 /*
3836 * Dictionary: #{key: val, key: val}
3837 */
3838 case '#': if ((*arg)[1] == '{')
3839 {
3840 ++*arg;
3841 ret = compile_dict(arg, cctx, TRUE);
3842 }
3843 else
3844 ret = NOTDONE;
3845 break;
3846
3847 /*
3848 * Lambda: {arg, arg -> expr}
3849 * Dictionary: {'key': val, 'key': val}
3850 */
3851 case '{': {
3852 char_u *start = skipwhite(*arg + 1);
3853
3854 // Find out what comes after the arguments.
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003855 // TODO: pass getline function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003857 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 if (ret != FAIL && *start == '>')
3859 ret = compile_lambda(arg, cctx);
3860 else
3861 ret = compile_dict(arg, cctx, FALSE);
3862 }
3863 break;
3864
3865 /*
3866 * Option value: &name
3867 */
3868 case '&': ret = compile_get_option(arg, cctx);
3869 break;
3870
3871 /*
3872 * Environment variable: $VAR.
3873 */
3874 case '$': ret = compile_get_env(arg, cctx);
3875 break;
3876
3877 /*
3878 * Register contents: @r.
3879 */
3880 case '@': ret = compile_get_register(arg, cctx);
3881 break;
3882 /*
3883 * nested expression: (expression).
3884 */
3885 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003886
3887 // recursive!
3888 if (ppconst->pp_used <= PPSIZE - 10)
3889 {
3890 ret = compile_expr1(arg, cctx, ppconst);
3891 }
3892 else
3893 {
3894 // Not enough space in ppconst, flush constants.
3895 if (generate_ppconst(cctx, ppconst) == FAIL)
3896 return FAIL;
3897 ret = compile_expr0(arg, cctx);
3898 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 *arg = skipwhite(*arg);
3900 if (**arg == ')')
3901 ++*arg;
3902 else if (ret == OK)
3903 {
3904 emsg(_(e_missing_close));
3905 ret = FAIL;
3906 }
3907 break;
3908
3909 default: ret = NOTDONE;
3910 break;
3911 }
3912 if (ret == FAIL)
3913 return FAIL;
3914
Bram Moolenaar1c747212020-05-09 18:28:34 +02003915 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 {
3917 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003918 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003920 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 return FAIL;
3922 }
3923 start_leader = end_leader; // don't apply again below
3924
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003925 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003926 clear_tv(rettv);
3927 else
3928 // A constant expression can possibly be handled compile time,
3929 // return the value instead of generating code.
3930 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 }
3932 else if (ret == NOTDONE)
3933 {
3934 char_u *p;
3935 int r;
3936
3937 if (!eval_isnamec1(**arg))
3938 {
3939 semsg(_("E1015: Name expected: %s"), *arg);
3940 return FAIL;
3941 }
3942
3943 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003944 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003946 {
3947 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003950 {
3951 if (generate_ppconst(cctx, ppconst) == FAIL)
3952 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003954 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 if (r == FAIL)
3956 return FAIL;
3957 }
3958
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003959 // Handle following "[]", ".member", etc.
3960 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003961 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003962 ppconst) == FAIL)
3963 return FAIL;
3964 if (ppconst->pp_used > 0)
3965 {
3966 // apply the '!', '-' and '+' before the constant
3967 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3968 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
3969 return FAIL;
3970 return OK;
3971 }
3972 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003974 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975}
3976
3977/*
3978 * * number multiplication
3979 * / number division
3980 * % number modulo
3981 */
3982 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003983compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984{
3985 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003986 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003987 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003989 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003990 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 return FAIL;
3992
3993 /*
3994 * Repeat computing, until no "*", "/" or "%" is following.
3995 */
3996 for (;;)
3997 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003998 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999 if (*op != '*' && *op != '/' && *op != '%')
4000 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004001 if (next != NULL)
4002 {
4003 *arg = next_line_from_context(cctx, TRUE);
4004 op = skipwhite(*arg);
4005 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004006
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004007 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 {
4009 char_u buf[3];
4010
4011 vim_strncpy(buf, op, 1);
4012 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004013 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 }
4015 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004016 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004017 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004019 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004020 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004022
4023 if (ppconst->pp_used == ppconst_used + 2
4024 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4025 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004026 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004027 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4028 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004029 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004031 // both are numbers: compute the result
4032 switch (*op)
4033 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004034 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004035 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004036 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004037 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004038 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004039 break;
4040 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004041 tv1->vval.v_number = res;
4042 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004043 }
4044 else
4045 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004046 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004047 generate_two_op(cctx, op);
4048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 }
4050
4051 return OK;
4052}
4053
4054/*
4055 * + number addition
4056 * - number subtraction
4057 * .. string concatenation
4058 */
4059 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004060compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061{
4062 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004063 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004065 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066
4067 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004068 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 return FAIL;
4070
4071 /*
4072 * Repeat computing, until no "+", "-" or ".." is following.
4073 */
4074 for (;;)
4075 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004076 op = may_peek_next_line(cctx, *arg, &next);
4077 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 break;
4079 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004080 if (next != NULL)
4081 {
4082 *arg = next_line_from_context(cctx, TRUE);
4083 op = skipwhite(*arg);
4084 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004086 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 {
4088 char_u buf[3];
4089
4090 vim_strncpy(buf, op, oplen);
4091 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004092 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 }
4094
4095 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004096 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004097 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004099 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004100 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 return FAIL;
4102
Bram Moolenaara5565e42020-05-09 15:44:01 +02004103 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004104 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004105 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4106 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4107 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4108 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004110 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4111 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004112
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004113 // concat/subtract/add constant numbers
4114 if (*op == '+')
4115 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4116 else if (*op == '-')
4117 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4118 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004119 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004120 // concatenate constant strings
4121 char_u *s1 = tv1->vval.v_string;
4122 char_u *s2 = tv2->vval.v_string;
4123 size_t len1 = STRLEN(s1);
4124
4125 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4126 if (tv1->vval.v_string == NULL)
4127 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004128 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004129 return FAIL;
4130 }
4131 mch_memmove(tv1->vval.v_string, s1, len1);
4132 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004133 vim_free(s1);
4134 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004135 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004136 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 }
4138 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004139 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004140 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004141 if (*op == '.')
4142 {
4143 if (may_generate_2STRING(-2, cctx) == FAIL
4144 || may_generate_2STRING(-1, cctx) == FAIL)
4145 return FAIL;
4146 generate_instr_drop(cctx, ISN_CONCAT, 1);
4147 }
4148 else
4149 generate_two_op(cctx, op);
4150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 }
4152
4153 return OK;
4154}
4155
4156/*
4157 * expr5a == expr5b
4158 * expr5a =~ expr5b
4159 * expr5a != expr5b
4160 * expr5a !~ expr5b
4161 * expr5a > expr5b
4162 * expr5a >= expr5b
4163 * expr5a < expr5b
4164 * expr5a <= expr5b
4165 * expr5a is expr5b
4166 * expr5a isnot expr5b
4167 *
4168 * Produces instructions:
4169 * EVAL expr5a Push result of "expr5a"
4170 * EVAL expr5b Push result of "expr5b"
4171 * COMPARE one of the compare instructions
4172 */
4173 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004174compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175{
4176 exptype_T type = EXPR_UNKNOWN;
4177 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004178 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004181 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182
4183 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004184 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 return FAIL;
4186
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004187 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004188 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189
4190 /*
4191 * If there is a comparative operator, use it.
4192 */
4193 if (type != EXPR_UNKNOWN)
4194 {
4195 int ic = FALSE; // Default: do not ignore case
4196
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004197 if (next != NULL)
4198 {
4199 *arg = next_line_from_context(cctx, TRUE);
4200 p = skipwhite(*arg);
4201 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 if (type_is && (p[len] == '?' || p[len] == '#'))
4203 {
4204 semsg(_(e_invexpr2), *arg);
4205 return FAIL;
4206 }
4207 // extra question mark appended: ignore case
4208 if (p[len] == '?')
4209 {
4210 ic = TRUE;
4211 ++len;
4212 }
4213 // extra '#' appended: match case (ignored)
4214 else if (p[len] == '#')
4215 ++len;
4216 // nothing appended: match case
4217
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004218 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 {
4220 char_u buf[7];
4221
4222 vim_strncpy(buf, p, len);
4223 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004224 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 }
4226
4227 // get the second variable
4228 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004229 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004230 return FAIL;
4231
Bram Moolenaara5565e42020-05-09 15:44:01 +02004232 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 return FAIL;
4234
Bram Moolenaara5565e42020-05-09 15:44:01 +02004235 if (ppconst->pp_used == ppconst_used + 2)
4236 {
4237 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4238 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4239 int ret;
4240
4241 // Both sides are a constant, compute the result now.
4242 // First check for a valid combination of types, this is more
4243 // strict than typval_compare().
4244 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
4245 ret = FAIL;
4246 else
4247 {
4248 ret = typval_compare(tv1, tv2, type, ic);
4249 tv1->v_type = VAR_BOOL;
4250 tv1->vval.v_number = tv1->vval.v_number
4251 ? VVAL_TRUE : VVAL_FALSE;
4252 clear_tv(tv2);
4253 --ppconst->pp_used;
4254 }
4255 return ret;
4256 }
4257
4258 generate_ppconst(cctx, ppconst);
4259 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 }
4261
4262 return OK;
4263}
4264
Bram Moolenaar7f141552020-05-09 17:35:53 +02004265static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267/*
4268 * Compile || or &&.
4269 */
4270 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004271compile_and_or(
4272 char_u **arg,
4273 cctx_T *cctx,
4274 char *op,
4275 ppconst_T *ppconst,
4276 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004278 char_u *next;
4279 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 int opchar = *op;
4281
4282 if (p[0] == opchar && p[1] == opchar)
4283 {
4284 garray_T *instr = &cctx->ctx_instr;
4285 garray_T end_ga;
4286
4287 /*
4288 * Repeat until there is no following "||" or "&&"
4289 */
4290 ga_init2(&end_ga, sizeof(int), 10);
4291 while (p[0] == opchar && p[1] == opchar)
4292 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004293 if (next != NULL)
4294 {
4295 *arg = next_line_from_context(cctx, TRUE);
4296 p = skipwhite(*arg);
4297 }
4298
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004299 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4300 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004302 return FAIL;
4303 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304
Bram Moolenaara5565e42020-05-09 15:44:01 +02004305 // TODO: use ppconst if the value is a constant
4306 generate_ppconst(cctx, ppconst);
4307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 if (ga_grow(&end_ga, 1) == FAIL)
4309 {
4310 ga_clear(&end_ga);
4311 return FAIL;
4312 }
4313 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4314 ++end_ga.ga_len;
4315 generate_JUMP(cctx, opchar == '|'
4316 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4317
4318 // eval the next expression
4319 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004320 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004321 return FAIL;
4322
Bram Moolenaara5565e42020-05-09 15:44:01 +02004323 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4324 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 {
4326 ga_clear(&end_ga);
4327 return FAIL;
4328 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004329
4330 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004332 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333
4334 // Fill in the end label in all jumps.
4335 while (end_ga.ga_len > 0)
4336 {
4337 isn_T *isn;
4338
4339 --end_ga.ga_len;
4340 isn = ((isn_T *)instr->ga_data)
4341 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4342 isn->isn_arg.jump.jump_where = instr->ga_len;
4343 }
4344 ga_clear(&end_ga);
4345 }
4346
4347 return OK;
4348}
4349
4350/*
4351 * expr4a && expr4a && expr4a logical AND
4352 *
4353 * Produces instructions:
4354 * EVAL expr4a Push result of "expr4a"
4355 * JUMP_AND_KEEP_IF_FALSE end
4356 * EVAL expr4b Push result of "expr4b"
4357 * JUMP_AND_KEEP_IF_FALSE end
4358 * EVAL expr4c Push result of "expr4c"
4359 * end:
4360 */
4361 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004362compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004364 int ppconst_used = ppconst->pp_used;
4365
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004367 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368 return FAIL;
4369
4370 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004371 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372}
4373
4374/*
4375 * expr3a || expr3b || expr3c logical OR
4376 *
4377 * Produces instructions:
4378 * EVAL expr3a Push result of "expr3a"
4379 * JUMP_AND_KEEP_IF_TRUE end
4380 * EVAL expr3b Push result of "expr3b"
4381 * JUMP_AND_KEEP_IF_TRUE end
4382 * EVAL expr3c Push result of "expr3c"
4383 * end:
4384 */
4385 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004386compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004388 int ppconst_used = ppconst->pp_used;
4389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004391 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 return FAIL;
4393
4394 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004395 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396}
4397
4398/*
4399 * Toplevel expression: expr2 ? expr1a : expr1b
4400 *
4401 * Produces instructions:
4402 * EVAL expr2 Push result of "expr"
4403 * JUMP_IF_FALSE alt jump if false
4404 * EVAL expr1a
4405 * JUMP_ALWAYS end
4406 * alt: EVAL expr1b
4407 * end:
4408 */
4409 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004410compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411{
4412 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004413 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004414 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415
Bram Moolenaar61a89812020-05-07 16:58:17 +02004416 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004417 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 return FAIL;
4419
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004420 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 if (*p == '?')
4422 {
4423 garray_T *instr = &cctx->ctx_instr;
4424 garray_T *stack = &cctx->ctx_type_stack;
4425 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004426 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004428 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004430 int has_const_expr = FALSE;
4431 int const_value = FALSE;
4432 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004434 if (next != NULL)
4435 {
4436 *arg = next_line_from_context(cctx, TRUE);
4437 p = skipwhite(*arg);
4438 }
4439
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004440 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4441 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004443 return FAIL;
4444 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445
Bram Moolenaara5565e42020-05-09 15:44:01 +02004446 if (ppconst->pp_used == ppconst_used + 1)
4447 {
4448 // the condition is a constant, we know whether the ? or the :
4449 // expression is to be evaluated.
4450 has_const_expr = TRUE;
4451 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4452 clear_tv(&ppconst->pp_tv[ppconst_used]);
4453 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004454 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4455 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004456 }
4457 else
4458 {
4459 generate_ppconst(cctx, ppconst);
4460 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462
4463 // evaluate the second expression; any type is accepted
4464 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004465 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004466 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004467 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004468 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469
Bram Moolenaara5565e42020-05-09 15:44:01 +02004470 if (!has_const_expr)
4471 {
4472 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473
Bram Moolenaara5565e42020-05-09 15:44:01 +02004474 // remember the type and drop it
4475 --stack->ga_len;
4476 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477
Bram Moolenaara5565e42020-05-09 15:44:01 +02004478 end_idx = instr->ga_len;
4479 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4480
4481 // jump here from JUMP_IF_FALSE
4482 isn = ((isn_T *)instr->ga_data) + alt_idx;
4483 isn->isn_arg.jump.jump_where = instr->ga_len;
4484 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485
4486 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004487 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 if (*p != ':')
4489 {
4490 emsg(_(e_missing_colon));
4491 return FAIL;
4492 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004493 if (next != NULL)
4494 {
4495 *arg = next_line_from_context(cctx, TRUE);
4496 p = skipwhite(*arg);
4497 }
4498
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004499 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4500 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004502 return FAIL;
4503 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504
4505 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004506 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004507 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4508 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004510 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004511 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004512 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004513 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514
Bram Moolenaara5565e42020-05-09 15:44:01 +02004515 if (!has_const_expr)
4516 {
4517 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518
Bram Moolenaara5565e42020-05-09 15:44:01 +02004519 // If the types differ, the result has a more generic type.
4520 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4521 common_type(type1, type2, &type2, cctx->ctx_type_list);
4522
4523 // jump here from JUMP_ALWAYS
4524 isn = ((isn_T *)instr->ga_data) + end_idx;
4525 isn->isn_arg.jump.jump_where = instr->ga_len;
4526 }
4527
4528 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 }
4530 return OK;
4531}
4532
4533/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004534 * Toplevel expression.
4535 */
4536 static int
4537compile_expr0(char_u **arg, cctx_T *cctx)
4538{
4539 ppconst_T ppconst;
4540
4541 CLEAR_FIELD(ppconst);
4542 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4543 {
4544 clear_ppconst(&ppconst);
4545 return FAIL;
4546 }
4547 if (generate_ppconst(cctx, &ppconst) == FAIL)
4548 return FAIL;
4549 return OK;
4550}
4551
4552/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 * compile "return [expr]"
4554 */
4555 static char_u *
4556compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4557{
4558 char_u *p = arg;
4559 garray_T *stack = &cctx->ctx_type_stack;
4560 type_T *stack_type;
4561
4562 if (*p != NUL && *p != '|' && *p != '\n')
4563 {
4564 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004565 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 return NULL;
4567
4568 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4569 if (set_return_type)
4570 cctx->ctx_ufunc->uf_ret_type = stack_type;
4571 else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
4572 == FAIL)
4573 return NULL;
4574 }
4575 else
4576 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004577 // "set_return_type" cannot be TRUE, only used for a lambda which
4578 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004579 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4580 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581 {
4582 emsg(_("E1003: Missing return value"));
4583 return NULL;
4584 }
4585
4586 // No argument, return zero.
4587 generate_PUSHNR(cctx, 0);
4588 }
4589
4590 if (generate_instr(cctx, ISN_RETURN) == NULL)
4591 return NULL;
4592
4593 // "return val | endif" is possible
4594 return skipwhite(p);
4595}
4596
4597/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004598 * Get a line from the compilation context, compatible with exarg_T getline().
4599 * Return a pointer to the line in allocated memory.
4600 * Return NULL for end-of-file or some error.
4601 */
4602 static char_u *
4603exarg_getline(
4604 int c UNUSED,
4605 void *cookie,
4606 int indent UNUSED,
4607 int do_concat UNUSED)
4608{
4609 cctx_T *cctx = (cctx_T *)cookie;
4610
4611 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4612 {
4613 iemsg("Heredoc got to end");
4614 return NULL;
4615 }
4616 ++cctx->ctx_lnum;
4617 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4618 [cctx->ctx_lnum]);
4619}
4620
4621/*
4622 * Compile a nested :def command.
4623 */
4624 static char_u *
4625compile_nested_function(exarg_T *eap, cctx_T *cctx)
4626{
4627 char_u *name_start = eap->arg;
4628 char_u *name_end = to_name_end(eap->arg, FALSE);
4629 char_u *name = get_lambda_name();
4630 lvar_T *lvar;
4631 ufunc_T *ufunc;
4632
4633 eap->arg = name_end;
4634 eap->getline = exarg_getline;
4635 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004636 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004637 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004638 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004639
Bram Moolenaar822ba242020-05-24 23:00:18 +02004640 if (ufunc == NULL)
4641 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004642 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004643 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004644 return NULL;
4645
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004646 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004647 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004648 TRUE, ufunc->uf_func_type);
4649
4650 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4651 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4652 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004653
Bram Moolenaar61a89812020-05-07 16:58:17 +02004654 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004655 return (char_u *)"";
4656}
4657
4658/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 * Return the length of an assignment operator, or zero if there isn't one.
4660 */
4661 int
4662assignment_len(char_u *p, int *heredoc)
4663{
4664 if (*p == '=')
4665 {
4666 if (p[1] == '<' && p[2] == '<')
4667 {
4668 *heredoc = TRUE;
4669 return 3;
4670 }
4671 return 1;
4672 }
4673 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4674 return 2;
4675 if (STRNCMP(p, "..=", 3) == 0)
4676 return 3;
4677 return 0;
4678}
4679
4680// words that cannot be used as a variable
4681static char *reserved[] = {
4682 "true",
4683 "false",
4684 NULL
4685};
4686
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004687typedef enum {
4688 dest_local,
4689 dest_option,
4690 dest_env,
4691 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004692 dest_buffer,
4693 dest_window,
4694 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004695 dest_vimvar,
4696 dest_script,
4697 dest_reg,
4698} assign_dest_T;
4699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004701 * Generate the load instruction for "name".
4702 */
4703 static void
4704generate_loadvar(
4705 cctx_T *cctx,
4706 assign_dest_T dest,
4707 char_u *name,
4708 lvar_T *lvar,
4709 type_T *type)
4710{
4711 switch (dest)
4712 {
4713 case dest_option:
4714 // TODO: check the option exists
4715 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4716 break;
4717 case dest_global:
4718 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4719 break;
4720 case dest_buffer:
4721 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4722 break;
4723 case dest_window:
4724 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4725 break;
4726 case dest_tab:
4727 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4728 break;
4729 case dest_script:
4730 compile_load_scriptvar(cctx,
4731 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4732 break;
4733 case dest_env:
4734 // Include $ in the name here
4735 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4736 break;
4737 case dest_reg:
4738 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4739 break;
4740 case dest_vimvar:
4741 generate_LOADV(cctx, name + 2, TRUE);
4742 break;
4743 case dest_local:
4744 if (lvar->lv_from_outer)
4745 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4746 NULL, type);
4747 else
4748 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4749 break;
4750 }
4751}
4752
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004753 void
4754vim9_declare_error(char_u *name)
4755{
4756 char *scope = "";
4757
4758 switch (*name)
4759 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004760 case 'g': scope = _("global"); break;
4761 case 'b': scope = _("buffer"); break;
4762 case 'w': scope = _("window"); break;
4763 case 't': scope = _("tab"); break;
4764 case 'v': scope = "v:"; break;
4765 case '$': semsg(_(e_declare_env_var), name); return;
4766 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004767 }
4768 semsg(_(e_declare_var), scope, name);
4769}
4770
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004771/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004772 * Compile declaration and assignment:
4773 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004774 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004775 * Return NULL for an error.
4776 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777 */
4778 static char_u *
4779compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4780{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004781 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004782 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004783 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784 char_u *ret = NULL;
4785 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004786 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004789 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 int oplen = 0;
4792 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004793 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004794 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004795 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004797 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004799 // Skip over the "var" or "[var, var]" to get to any "=".
4800 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4801 if (p == NULL)
4802 return *arg == '[' ? arg : NULL;
4803
4804 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004806 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004807 return NULL;
4808 }
4809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810 sp = p;
4811 p = skipwhite(p);
4812 op = p;
4813 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004814
4815 if (var_count > 0 && oplen == 0)
4816 // can be something like "[1, 2]->func()"
4817 return arg;
4818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004819 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4820 {
4821 char_u buf[4];
4822
4823 vim_strncpy(buf, op, oplen);
4824 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004825 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004826 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004828 if (heredoc)
4829 {
4830 list_T *l;
4831 listitem_T *li;
4832
4833 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004834 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004836 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837
4838 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004839 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840 {
4841 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4842 li->li_tv.vval.v_string = NULL;
4843 }
4844 generate_NEWLIST(cctx, l->lv_len);
4845 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004846 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847 list_free(l);
4848 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004849 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004851 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004853 // for "[var, var] = expr" evaluate the expression here, loop over the
4854 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004855
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004856 p = skipwhite(op + oplen);
4857 if (compile_expr0(&p, cctx) == FAIL)
4858 return NULL;
4859 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004861 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004863 type_T *stacktype;
4864
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004865 stacktype = stack->ga_len == 0 ? &t_void
4866 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004867 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004869 emsg(_(e_cannot_use_void));
4870 goto theend;
4871 }
4872 if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL)
4873 goto theend;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004874 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4875 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004876 }
4877 }
4878
4879 /*
4880 * Loop over variables in "[var, var] = expr".
4881 * For "var = expr" and "let var: type" this is done only once.
4882 */
4883 if (var_count > 0)
4884 var_start = skipwhite(arg + 1); // skip over the "["
4885 else
4886 var_start = arg;
4887 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4888 {
4889 char_u *var_end = skip_var_one(var_start, FALSE);
4890 size_t varlen;
4891 int new_local = FALSE;
4892 int opt_type;
4893 int opt_flags = 0;
4894 assign_dest_T dest = dest_local;
4895 int vimvaridx = -1;
4896 lvar_T *lvar = NULL;
4897 lvar_T arg_lvar;
4898 int has_type = FALSE;
4899 int has_index = FALSE;
4900 int instr_count = -1;
4901
4902 p = (*var_start == '&' || *var_start == '$'
4903 || *var_start == '@') ? var_start + 1 : var_start;
4904 p = to_name_end(p, TRUE);
4905
4906 // "a: type" is declaring variable "a" with a type, not "a:".
4907 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4908 --var_end;
4909 if (is_decl && p == var_start + 2 && p[-1] == ':')
4910 --p;
4911
4912 varlen = p - var_start;
4913 vim_free(name);
4914 name = vim_strnsave(var_start, varlen);
4915 if (name == NULL)
4916 return NULL;
4917 if (!heredoc)
4918 type = &t_any;
4919
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004920 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004921 {
4922 if (*var_start == '&')
4923 {
4924 int cc;
4925 long numval;
4926
4927 dest = dest_option;
4928 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004929 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004930 emsg(_(e_const_option));
4931 goto theend;
4932 }
4933 if (is_decl)
4934 {
4935 semsg(_("E1052: Cannot declare an option: %s"), var_start);
4936 goto theend;
4937 }
4938 p = var_start;
4939 p = find_option_end(&p, &opt_flags);
4940 if (p == NULL)
4941 {
4942 // cannot happen?
4943 emsg(_(e_letunexp));
4944 goto theend;
4945 }
4946 cc = *p;
4947 *p = NUL;
4948 opt_type = get_option_value(var_start + 1, &numval,
4949 NULL, opt_flags);
4950 *p = cc;
4951 if (opt_type == -3)
4952 {
4953 semsg(_(e_unknown_option), var_start);
4954 goto theend;
4955 }
4956 if (opt_type == -2 || opt_type == 0)
4957 type = &t_string;
4958 else
4959 type = &t_number; // both number and boolean option
4960 }
4961 else if (*var_start == '$')
4962 {
4963 dest = dest_env;
4964 type = &t_string;
4965 if (is_decl)
4966 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004967 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004968 goto theend;
4969 }
4970 }
4971 else if (*var_start == '@')
4972 {
4973 if (!valid_yank_reg(var_start[1], TRUE))
4974 {
4975 emsg_invreg(var_start[1]);
4976 goto theend;
4977 }
4978 dest = dest_reg;
4979 type = &t_string;
4980 if (is_decl)
4981 {
4982 semsg(_("E1066: Cannot declare a register: %s"), name);
4983 goto theend;
4984 }
4985 }
4986 else if (STRNCMP(var_start, "g:", 2) == 0)
4987 {
4988 dest = dest_global;
4989 if (is_decl)
4990 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004991 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004992 goto theend;
4993 }
4994 }
4995 else if (STRNCMP(var_start, "b:", 2) == 0)
4996 {
4997 dest = dest_buffer;
4998 if (is_decl)
4999 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005000 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005001 goto theend;
5002 }
5003 }
5004 else if (STRNCMP(var_start, "w:", 2) == 0)
5005 {
5006 dest = dest_window;
5007 if (is_decl)
5008 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005009 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005010 goto theend;
5011 }
5012 }
5013 else if (STRNCMP(var_start, "t:", 2) == 0)
5014 {
5015 dest = dest_tab;
5016 if (is_decl)
5017 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005018 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005019 goto theend;
5020 }
5021 }
5022 else if (STRNCMP(var_start, "v:", 2) == 0)
5023 {
5024 typval_T *vtv;
5025 int di_flags;
5026
5027 vimvaridx = find_vim_var(name + 2, &di_flags);
5028 if (vimvaridx < 0)
5029 {
5030 semsg(_(e_var_notfound), var_start);
5031 goto theend;
5032 }
5033 // We use the current value of "sandbox" here, is that OK?
5034 if (var_check_ro(di_flags, name, FALSE))
5035 goto theend;
5036 dest = dest_vimvar;
5037 vtv = get_vim_var_tv(vimvaridx);
5038 type = typval2type(vtv);
5039 if (is_decl)
5040 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005041 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005042 goto theend;
5043 }
5044 }
5045 else
5046 {
5047 int idx;
5048
5049 for (idx = 0; reserved[idx] != NULL; ++idx)
5050 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005051 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005052 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005053 goto theend;
5054 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005055
5056 lvar = lookup_local(var_start, varlen, cctx);
5057 if (lvar == NULL)
5058 {
5059 CLEAR_FIELD(arg_lvar);
5060 if (lookup_arg(var_start, varlen,
5061 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5062 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005063 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005064 if (is_decl)
5065 {
5066 semsg(_(e_used_as_arg), name);
5067 goto theend;
5068 }
5069 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005070 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005071 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005072 if (lvar != NULL)
5073 {
5074 if (is_decl)
5075 {
5076 semsg(_("E1017: Variable already declared: %s"), name);
5077 goto theend;
5078 }
5079 else if (lvar->lv_const)
5080 {
5081 semsg(_("E1018: Cannot assign to a constant: %s"),
5082 name);
5083 goto theend;
5084 }
5085 }
5086 else if (STRNCMP(var_start, "s:", 2) == 0
5087 || lookup_script(var_start, varlen) == OK
5088 || find_imported(var_start, varlen, cctx) != NULL)
5089 {
5090 dest = dest_script;
5091 if (is_decl)
5092 {
5093 semsg(_("E1054: Variable already declared in the script: %s"),
5094 name);
5095 goto theend;
5096 }
5097 }
5098 else if (name[1] == ':' && name[2] != NUL)
5099 {
5100 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5101 name);
5102 goto theend;
5103 }
5104 else if (!is_decl)
5105 {
5106 semsg(_("E1089: unknown variable: %s"), name);
5107 goto theend;
5108 }
5109 }
5110 }
5111
5112 // handle "a:name" as a name, not index "name" on "a"
5113 if (varlen > 1 || var_start[varlen] != ':')
5114 p = var_end;
5115
5116 if (dest != dest_option)
5117 {
5118 if (is_decl && *p == ':')
5119 {
5120 // parse optional type: "let var: type = expr"
5121 if (!VIM_ISWHITE(p[1]))
5122 {
5123 semsg(_(e_white_after), ":");
5124 goto theend;
5125 }
5126 p = skipwhite(p + 1);
5127 type = parse_type(&p, cctx->ctx_type_list);
5128 has_type = TRUE;
5129 }
5130 else if (lvar != NULL)
5131 type = lvar->lv_type;
5132 }
5133
5134 if (oplen == 3 && !heredoc && dest != dest_global
5135 && type->tt_type != VAR_STRING
5136 && type->tt_type != VAR_ANY)
5137 {
5138 emsg(_("E1019: Can only concatenate to string"));
5139 goto theend;
5140 }
5141
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005142 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005143 {
5144 if (oplen > 1 && !heredoc)
5145 {
5146 // +=, /=, etc. require an existing variable
5147 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5148 name);
5149 goto theend;
5150 }
5151
5152 // new local variable
5153 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5154 goto theend;
5155 lvar = reserve_local(cctx, var_start, varlen,
5156 cmdidx == CMD_const, type);
5157 if (lvar == NULL)
5158 goto theend;
5159 new_local = TRUE;
5160 }
5161
5162 member_type = type;
5163 if (var_end > var_start + varlen)
5164 {
5165 // Something follows after the variable: "var[idx]".
5166 if (is_decl)
5167 {
5168 emsg(_("E1087: cannot use an index when declaring a variable"));
5169 goto theend;
5170 }
5171
5172 if (var_start[varlen] == '[')
5173 {
5174 has_index = TRUE;
5175 if (type->tt_member == NULL)
5176 {
5177 semsg(_("E1088: cannot use an index on %s"), name);
5178 goto theend;
5179 }
5180 member_type = type->tt_member;
5181 }
5182 else
5183 {
5184 semsg("Not supported yet: %s", var_start);
5185 goto theend;
5186 }
5187 }
5188 else if (lvar == &arg_lvar)
5189 {
5190 semsg(_("E1090: Cannot assign to argument %s"), name);
5191 goto theend;
5192 }
5193
5194 if (!heredoc)
5195 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005196 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005197 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005198 if (oplen > 0 && var_count == 0)
5199 {
5200 // skip over the "=" and the expression
5201 p = skipwhite(op + oplen);
5202 compile_expr0(&p, cctx);
5203 }
5204 }
5205 else if (oplen > 0)
5206 {
5207 type_T *stacktype;
5208
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005209 // For "var = expr" evaluate the expression.
5210 if (var_count == 0)
5211 {
5212 int r;
5213
5214 // for "+=", "*=", "..=" etc. first load the current value
5215 if (*op != '=')
5216 {
5217 generate_loadvar(cctx, dest, name, lvar, type);
5218
5219 if (has_index)
5220 {
5221 // TODO: get member from list or dict
5222 emsg("Index with operation not supported yet");
5223 goto theend;
5224 }
5225 }
5226
5227 // Compile the expression. Temporarily hide the new local
5228 // variable here, it is not available to this expression.
5229 if (new_local)
5230 --cctx->ctx_locals.ga_len;
5231 instr_count = instr->ga_len;
5232 p = skipwhite(op + oplen);
5233 r = compile_expr0(&p, cctx);
5234 if (new_local)
5235 ++cctx->ctx_locals.ga_len;
5236 if (r == FAIL)
5237 goto theend;
5238 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005239 else if (semicolon && var_idx == var_count - 1)
5240 {
5241 // For "[var; var] = expr" get the rest of the list
5242 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5243 goto theend;
5244 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005245 else
5246 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005247 // For "[var, var] = expr" get the "var_idx" item from the
5248 // list.
5249 if (generate_GETITEM(cctx, var_idx) == FAIL)
5250 return FAIL;
5251 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005252
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005253 stacktype = stack->ga_len == 0 ? &t_void
5254 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5255 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005256 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005257 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005258 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005259 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005260 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005261 emsg(_(e_cannot_use_void));
5262 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005263 }
5264 else
5265 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005266 // An empty list or dict has a &t_void member,
5267 // for a variable that implies &t_any.
5268 if (stacktype == &t_list_empty)
5269 lvar->lv_type = &t_list_any;
5270 else if (stacktype == &t_dict_empty)
5271 lvar->lv_type = &t_dict_any;
5272 else
5273 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005274 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005275 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005276 else
5277 {
5278 type_T *use_type = lvar->lv_type;
5279
5280 if (has_index)
5281 {
5282 use_type = use_type->tt_member;
5283 if (use_type == NULL)
5284 use_type = &t_void;
5285 }
5286 if (need_type(stacktype, use_type, -1, cctx)
5287 == FAIL)
5288 goto theend;
5289 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005290 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005291 else if (*p != '=' && need_type(stacktype, member_type, -1,
5292 cctx) == FAIL)
5293 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005294 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005295 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005297 emsg(_(e_const_req_value));
5298 goto theend;
5299 }
5300 else if (!has_type || dest == dest_option)
5301 {
5302 emsg(_(e_type_req));
5303 goto theend;
5304 }
5305 else
5306 {
5307 // variables are always initialized
5308 if (ga_grow(instr, 1) == FAIL)
5309 goto theend;
5310 switch (member_type->tt_type)
5311 {
5312 case VAR_BOOL:
5313 generate_PUSHBOOL(cctx, VVAL_FALSE);
5314 break;
5315 case VAR_FLOAT:
5316#ifdef FEAT_FLOAT
5317 generate_PUSHF(cctx, 0.0);
5318#endif
5319 break;
5320 case VAR_STRING:
5321 generate_PUSHS(cctx, NULL);
5322 break;
5323 case VAR_BLOB:
5324 generate_PUSHBLOB(cctx, NULL);
5325 break;
5326 case VAR_FUNC:
5327 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5328 break;
5329 case VAR_LIST:
5330 generate_NEWLIST(cctx, 0);
5331 break;
5332 case VAR_DICT:
5333 generate_NEWDICT(cctx, 0);
5334 break;
5335 case VAR_JOB:
5336 generate_PUSHJOB(cctx, NULL);
5337 break;
5338 case VAR_CHANNEL:
5339 generate_PUSHCHANNEL(cctx, NULL);
5340 break;
5341 case VAR_NUMBER:
5342 case VAR_UNKNOWN:
5343 case VAR_ANY:
5344 case VAR_PARTIAL:
5345 case VAR_VOID:
5346 case VAR_SPECIAL: // cannot happen
5347 generate_PUSHNR(cctx, 0);
5348 break;
5349 }
5350 }
5351 if (var_count == 0)
5352 end = p;
5353 }
5354
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005355 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005356 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005357 break;
5358
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005359 if (oplen > 0 && *op != '=')
5360 {
5361 type_T *expected = &t_number;
5362 type_T *stacktype;
5363
5364 // TODO: if type is known use float or any operation
5365
5366 if (*op == '.')
5367 expected = &t_string;
5368 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5369 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5370 goto theend;
5371
5372 if (*op == '.')
5373 generate_instr_drop(cctx, ISN_CONCAT, 1);
5374 else
5375 {
5376 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5377
5378 if (isn == NULL)
5379 goto theend;
5380 switch (*op)
5381 {
5382 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5383 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5384 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5385 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5386 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5387 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005388 }
5389 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005390
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005391 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005392 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005393 int r;
5394
5395 // Compile the "idx" in "var[idx]".
5396 if (new_local)
5397 --cctx->ctx_locals.ga_len;
5398 p = skipwhite(var_start + varlen + 1);
5399 r = compile_expr0(&p, cctx);
5400 if (new_local)
5401 ++cctx->ctx_locals.ga_len;
5402 if (r == FAIL)
5403 goto theend;
5404 if (*skipwhite(p) != ']')
5405 {
5406 emsg(_(e_missbrac));
5407 goto theend;
5408 }
5409 if (type->tt_type == VAR_DICT
5410 && may_generate_2STRING(-1, cctx) == FAIL)
5411 goto theend;
5412 if (type->tt_type == VAR_LIST
5413 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005414 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005415 {
5416 emsg(_(e_number_exp));
5417 goto theend;
5418 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005419
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005420 // Load the dict or list. On the stack we then have:
5421 // - value
5422 // - index
5423 // - variable
5424 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005425
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005426 if (type->tt_type == VAR_LIST)
5427 {
5428 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5429 return FAIL;
5430 }
5431 else if (type->tt_type == VAR_DICT)
5432 {
5433 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5434 return FAIL;
5435 }
5436 else
5437 {
5438 emsg(_(e_listreq));
5439 goto theend;
5440 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005441 }
5442 else
5443 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005444 switch (dest)
5445 {
5446 case dest_option:
5447 generate_STOREOPT(cctx, name + 1, opt_flags);
5448 break;
5449 case dest_global:
5450 // include g: with the name, easier to execute that way
5451 generate_STORE(cctx, ISN_STOREG, 0, name);
5452 break;
5453 case dest_buffer:
5454 // include b: with the name, easier to execute that way
5455 generate_STORE(cctx, ISN_STOREB, 0, name);
5456 break;
5457 case dest_window:
5458 // include w: with the name, easier to execute that way
5459 generate_STORE(cctx, ISN_STOREW, 0, name);
5460 break;
5461 case dest_tab:
5462 // include t: with the name, easier to execute that way
5463 generate_STORE(cctx, ISN_STORET, 0, name);
5464 break;
5465 case dest_env:
5466 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5467 break;
5468 case dest_reg:
5469 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5470 break;
5471 case dest_vimvar:
5472 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5473 break;
5474 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005475 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005476 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5477 imported_T *import = NULL;
5478 int sid = current_sctx.sc_sid;
5479 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005480
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005481 if (name[1] != ':')
5482 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005483 import = find_imported(name, 0, cctx);
5484 if (import != NULL)
5485 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005486 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005487
5488 idx = get_script_item_idx(sid, rawname, TRUE);
5489 // TODO: specific type
5490 if (idx < 0)
5491 {
5492 char_u *name_s = name;
5493
5494 // Include s: in the name for store_var()
5495 if (name[1] != ':')
5496 {
5497 int len = (int)STRLEN(name) + 3;
5498
5499 name_s = alloc(len);
5500 if (name_s == NULL)
5501 name_s = name;
5502 else
5503 vim_snprintf((char *)name_s, len,
5504 "s:%s", name);
5505 }
5506 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005507 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005508 if (name_s != name)
5509 vim_free(name_s);
5510 }
5511 else
5512 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005513 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005514 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005515 break;
5516 case dest_local:
5517 if (lvar != NULL)
5518 {
5519 isn_T *isn = ((isn_T *)instr->ga_data)
5520 + instr->ga_len - 1;
5521
5522 // optimization: turn "var = 123" from ISN_PUSHNR +
5523 // ISN_STORE into ISN_STORENR
5524 if (!lvar->lv_from_outer
5525 && instr->ga_len == instr_count + 1
5526 && isn->isn_type == ISN_PUSHNR)
5527 {
5528 varnumber_T val = isn->isn_arg.number;
5529
5530 isn->isn_type = ISN_STORENR;
5531 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5532 isn->isn_arg.storenr.stnr_val = val;
5533 if (stack->ga_len > 0)
5534 --stack->ga_len;
5535 }
5536 else if (lvar->lv_from_outer)
5537 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005538 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005539 else
5540 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5541 }
5542 break;
5543 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005544 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005545
5546 if (var_idx + 1 < var_count)
5547 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005549
5550 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005551 if (var_count > 0 && !semicolon)
5552 {
5553 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5554 goto theend;
5555 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005556
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005557 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005558
5559theend:
5560 vim_free(name);
5561 return ret;
5562}
5563
5564/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005565 * Check if "name" can be "unlet".
5566 */
5567 int
5568check_vim9_unlet(char_u *name)
5569{
5570 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5571 {
5572 semsg(_("E1081: Cannot unlet %s"), name);
5573 return FAIL;
5574 }
5575 return OK;
5576}
5577
5578/*
5579 * Callback passed to ex_unletlock().
5580 */
5581 static int
5582compile_unlet(
5583 lval_T *lvp,
5584 char_u *name_end,
5585 exarg_T *eap,
5586 int deep UNUSED,
5587 void *coookie)
5588{
5589 cctx_T *cctx = coookie;
5590
5591 if (lvp->ll_tv == NULL)
5592 {
5593 char_u *p = lvp->ll_name;
5594 int cc = *name_end;
5595 int ret = OK;
5596
5597 // Normal name. Only supports g:, w:, t: and b: namespaces.
5598 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005599 if (*p == '$')
5600 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5601 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005602 ret = FAIL;
5603 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005604 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005605
5606 *name_end = cc;
5607 return ret;
5608 }
5609
5610 // TODO: unlet {list}[idx]
5611 // TODO: unlet {dict}[key]
5612 emsg("Sorry, :unlet not fully implemented yet");
5613 return FAIL;
5614}
5615
5616/*
5617 * compile "unlet var", "lock var" and "unlock var"
5618 * "arg" points to "var".
5619 */
5620 static char_u *
5621compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5622{
5623 char_u *p = arg;
5624
5625 if (eap->cmdidx != CMD_unlet)
5626 {
5627 emsg("Sorry, :lock and unlock not implemented yet");
5628 return NULL;
5629 }
5630
5631 if (*p == '!')
5632 {
5633 p = skipwhite(p + 1);
5634 eap->forceit = TRUE;
5635 }
5636
5637 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5638 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5639}
5640
5641/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005642 * Compile an :import command.
5643 */
5644 static char_u *
5645compile_import(char_u *arg, cctx_T *cctx)
5646{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005647 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005648}
5649
5650/*
5651 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5652 */
5653 static int
5654compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5655{
5656 garray_T *instr = &cctx->ctx_instr;
5657 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5658
5659 if (endlabel == NULL)
5660 return FAIL;
5661 endlabel->el_next = *el;
5662 *el = endlabel;
5663 endlabel->el_end_label = instr->ga_len;
5664
5665 generate_JUMP(cctx, when, 0);
5666 return OK;
5667}
5668
5669 static void
5670compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5671{
5672 garray_T *instr = &cctx->ctx_instr;
5673
5674 while (*el != NULL)
5675 {
5676 endlabel_T *cur = (*el);
5677 isn_T *isn;
5678
5679 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5680 isn->isn_arg.jump.jump_where = instr->ga_len;
5681 *el = cur->el_next;
5682 vim_free(cur);
5683 }
5684}
5685
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005686 static void
5687compile_free_jump_to_end(endlabel_T **el)
5688{
5689 while (*el != NULL)
5690 {
5691 endlabel_T *cur = (*el);
5692
5693 *el = cur->el_next;
5694 vim_free(cur);
5695 }
5696}
5697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005698/*
5699 * Create a new scope and set up the generic items.
5700 */
5701 static scope_T *
5702new_scope(cctx_T *cctx, scopetype_T type)
5703{
5704 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5705
5706 if (scope == NULL)
5707 return NULL;
5708 scope->se_outer = cctx->ctx_scope;
5709 cctx->ctx_scope = scope;
5710 scope->se_type = type;
5711 scope->se_local_count = cctx->ctx_locals.ga_len;
5712 return scope;
5713}
5714
5715/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005716 * Free the current scope and go back to the outer scope.
5717 */
5718 static void
5719drop_scope(cctx_T *cctx)
5720{
5721 scope_T *scope = cctx->ctx_scope;
5722
5723 if (scope == NULL)
5724 {
5725 iemsg("calling drop_scope() without a scope");
5726 return;
5727 }
5728 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005729 switch (scope->se_type)
5730 {
5731 case IF_SCOPE:
5732 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5733 case FOR_SCOPE:
5734 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5735 case WHILE_SCOPE:
5736 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5737 case TRY_SCOPE:
5738 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5739 case NO_SCOPE:
5740 case BLOCK_SCOPE:
5741 break;
5742 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005743 vim_free(scope);
5744}
5745
5746/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005747 * compile "if expr"
5748 *
5749 * "if expr" Produces instructions:
5750 * EVAL expr Push result of "expr"
5751 * JUMP_IF_FALSE end
5752 * ... body ...
5753 * end:
5754 *
5755 * "if expr | else" Produces instructions:
5756 * EVAL expr Push result of "expr"
5757 * JUMP_IF_FALSE else
5758 * ... body ...
5759 * JUMP_ALWAYS end
5760 * else:
5761 * ... body ...
5762 * end:
5763 *
5764 * "if expr1 | elseif expr2 | else" Produces instructions:
5765 * EVAL expr Push result of "expr"
5766 * JUMP_IF_FALSE elseif
5767 * ... body ...
5768 * JUMP_ALWAYS end
5769 * elseif:
5770 * EVAL expr Push result of "expr"
5771 * JUMP_IF_FALSE else
5772 * ... body ...
5773 * JUMP_ALWAYS end
5774 * else:
5775 * ... body ...
5776 * end:
5777 */
5778 static char_u *
5779compile_if(char_u *arg, cctx_T *cctx)
5780{
5781 char_u *p = arg;
5782 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005783 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005784 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005785 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005786 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005787
Bram Moolenaara5565e42020-05-09 15:44:01 +02005788 CLEAR_FIELD(ppconst);
5789 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005790 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005791 clear_ppconst(&ppconst);
5792 return NULL;
5793 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005794 if (cctx->ctx_skip == SKIP_YES)
5795 clear_ppconst(&ppconst);
5796 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005797 {
5798 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005799 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005800 clear_ppconst(&ppconst);
5801 }
5802 else
5803 {
5804 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005805 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005806 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005807 return NULL;
5808 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005809
5810 scope = new_scope(cctx, IF_SCOPE);
5811 if (scope == NULL)
5812 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005813 scope->se_skip_save = skip_save;
5814 // "is_had_return" will be reset if any block does not end in :return
5815 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005816
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005817 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005818 {
5819 // "where" is set when ":elseif", "else" or ":endif" is found
5820 scope->se_u.se_if.is_if_label = instr->ga_len;
5821 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5822 }
5823 else
5824 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005825
5826 return p;
5827}
5828
5829 static char_u *
5830compile_elseif(char_u *arg, cctx_T *cctx)
5831{
5832 char_u *p = arg;
5833 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005834 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005835 isn_T *isn;
5836 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005837 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005838
5839 if (scope == NULL || scope->se_type != IF_SCOPE)
5840 {
5841 emsg(_(e_elseif_without_if));
5842 return NULL;
5843 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005844 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005845 if (!cctx->ctx_had_return)
5846 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005847
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005848 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005849 {
5850 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005851 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005852 return NULL;
5853 // previous "if" or "elseif" jumps here
5854 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5855 isn->isn_arg.jump.jump_where = instr->ga_len;
5856 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005857
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005858 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005859 CLEAR_FIELD(ppconst);
5860 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005861 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005862 clear_ppconst(&ppconst);
5863 return NULL;
5864 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005865 if (scope->se_skip_save == SKIP_YES)
5866 clear_ppconst(&ppconst);
5867 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005868 {
5869 // The expression results in a constant.
5870 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005871 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005872 clear_ppconst(&ppconst);
5873 scope->se_u.se_if.is_if_label = -1;
5874 }
5875 else
5876 {
5877 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005878 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005879 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005880 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005881
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005882 // "where" is set when ":elseif", "else" or ":endif" is found
5883 scope->se_u.se_if.is_if_label = instr->ga_len;
5884 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5885 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005886
5887 return p;
5888}
5889
5890 static char_u *
5891compile_else(char_u *arg, cctx_T *cctx)
5892{
5893 char_u *p = arg;
5894 garray_T *instr = &cctx->ctx_instr;
5895 isn_T *isn;
5896 scope_T *scope = cctx->ctx_scope;
5897
5898 if (scope == NULL || scope->se_type != IF_SCOPE)
5899 {
5900 emsg(_(e_else_without_if));
5901 return NULL;
5902 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005903 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005904 if (!cctx->ctx_had_return)
5905 scope->se_u.se_if.is_had_return = FALSE;
5906 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005907
Bram Moolenaarefd88552020-06-18 20:50:10 +02005908 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005909 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005910 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005911 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005912 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005913 if (!cctx->ctx_had_return
5914 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5915 JUMP_ALWAYS, cctx) == FAIL)
5916 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005917 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005918
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005919 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005920 {
5921 if (scope->se_u.se_if.is_if_label >= 0)
5922 {
5923 // previous "if" or "elseif" jumps here
5924 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5925 isn->isn_arg.jump.jump_where = instr->ga_len;
5926 scope->se_u.se_if.is_if_label = -1;
5927 }
5928 }
5929
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005930 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005931 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5932 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005933
5934 return p;
5935}
5936
5937 static char_u *
5938compile_endif(char_u *arg, cctx_T *cctx)
5939{
5940 scope_T *scope = cctx->ctx_scope;
5941 ifscope_T *ifscope;
5942 garray_T *instr = &cctx->ctx_instr;
5943 isn_T *isn;
5944
5945 if (scope == NULL || scope->se_type != IF_SCOPE)
5946 {
5947 emsg(_(e_endif_without_if));
5948 return NULL;
5949 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005950 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005951 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005952 if (!cctx->ctx_had_return)
5953 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005954
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005955 if (scope->se_u.se_if.is_if_label >= 0)
5956 {
5957 // previous "if" or "elseif" jumps here
5958 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5959 isn->isn_arg.jump.jump_where = instr->ga_len;
5960 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005961 // Fill in the "end" label in jumps at the end of the blocks.
5962 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005963 cctx->ctx_skip = scope->se_skip_save;
5964
5965 // If all the blocks end in :return and there is an :else then the
5966 // had_return flag is set.
5967 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005968
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005969 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005970 return arg;
5971}
5972
5973/*
5974 * compile "for var in expr"
5975 *
5976 * Produces instructions:
5977 * PUSHNR -1
5978 * STORE loop-idx Set index to -1
5979 * EVAL expr Push result of "expr"
5980 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5981 * - if beyond end, jump to "end"
5982 * - otherwise get item from list and push it
5983 * STORE var Store item in "var"
5984 * ... body ...
5985 * JUMP top Jump back to repeat
5986 * end: DROP Drop the result of "expr"
5987 *
5988 */
5989 static char_u *
5990compile_for(char_u *arg, cctx_T *cctx)
5991{
5992 char_u *p;
5993 size_t varlen;
5994 garray_T *instr = &cctx->ctx_instr;
5995 garray_T *stack = &cctx->ctx_type_stack;
5996 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005997 lvar_T *loop_lvar; // loop iteration variable
5998 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005999 type_T *vartype;
6000
6001 // TODO: list of variables: "for [key, value] in dict"
6002 // parse "var"
6003 for (p = arg; eval_isnamec1(*p); ++p)
6004 ;
6005 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006006 var_lvar = lookup_local(arg, varlen, cctx);
6007 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006008 {
6009 semsg(_("E1023: variable already defined: %s"), arg);
6010 return NULL;
6011 }
6012
6013 // consume "in"
6014 p = skipwhite(p);
6015 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6016 {
6017 emsg(_(e_missing_in));
6018 return NULL;
6019 }
6020 p = skipwhite(p + 2);
6021
6022
6023 scope = new_scope(cctx, FOR_SCOPE);
6024 if (scope == NULL)
6025 return NULL;
6026
6027 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006028 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6029 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006030 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006031 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006032 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006033 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006035
6036 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006037 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6038 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006039 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006040 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006041 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006042 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006043 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006044
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006045 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006046
6047 // compile "expr", it remains on the stack until "endfor"
6048 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006049 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006050 {
6051 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006052 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006053 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006054
6055 // now we know the type of "var"
6056 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6057 if (vartype->tt_type != VAR_LIST)
6058 {
6059 emsg(_("E1024: need a List to iterate over"));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006060 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061 return NULL;
6062 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02006063 if (vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006064 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006065
6066 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006067 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006068
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006069 generate_FOR(cctx, loop_lvar->lv_idx);
6070 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006071
6072 return arg;
6073}
6074
6075/*
6076 * compile "endfor"
6077 */
6078 static char_u *
6079compile_endfor(char_u *arg, cctx_T *cctx)
6080{
6081 garray_T *instr = &cctx->ctx_instr;
6082 scope_T *scope = cctx->ctx_scope;
6083 forscope_T *forscope;
6084 isn_T *isn;
6085
6086 if (scope == NULL || scope->se_type != FOR_SCOPE)
6087 {
6088 emsg(_(e_for));
6089 return NULL;
6090 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006091 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006092 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006093 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006094
6095 // At end of ":for" scope jump back to the FOR instruction.
6096 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6097
6098 // Fill in the "end" label in the FOR statement so it can jump here
6099 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6100 isn->isn_arg.forloop.for_end = instr->ga_len;
6101
6102 // Fill in the "end" label any BREAK statements
6103 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6104
6105 // Below the ":for" scope drop the "expr" list from the stack.
6106 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6107 return NULL;
6108
6109 vim_free(scope);
6110
6111 return arg;
6112}
6113
6114/*
6115 * compile "while expr"
6116 *
6117 * Produces instructions:
6118 * top: EVAL expr Push result of "expr"
6119 * JUMP_IF_FALSE end jump if false
6120 * ... body ...
6121 * JUMP top Jump back to repeat
6122 * end:
6123 *
6124 */
6125 static char_u *
6126compile_while(char_u *arg, cctx_T *cctx)
6127{
6128 char_u *p = arg;
6129 garray_T *instr = &cctx->ctx_instr;
6130 scope_T *scope;
6131
6132 scope = new_scope(cctx, WHILE_SCOPE);
6133 if (scope == NULL)
6134 return NULL;
6135
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006136 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137
6138 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006139 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006140 return NULL;
6141
6142 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006143 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006144 JUMP_IF_FALSE, cctx) == FAIL)
6145 return FAIL;
6146
6147 return p;
6148}
6149
6150/*
6151 * compile "endwhile"
6152 */
6153 static char_u *
6154compile_endwhile(char_u *arg, cctx_T *cctx)
6155{
6156 scope_T *scope = cctx->ctx_scope;
6157
6158 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6159 {
6160 emsg(_(e_while));
6161 return NULL;
6162 }
6163 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006164 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006165
6166 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006167 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006168
6169 // Fill in the "end" label in the WHILE statement so it can jump here.
6170 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006171 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006172
6173 vim_free(scope);
6174
6175 return arg;
6176}
6177
6178/*
6179 * compile "continue"
6180 */
6181 static char_u *
6182compile_continue(char_u *arg, cctx_T *cctx)
6183{
6184 scope_T *scope = cctx->ctx_scope;
6185
6186 for (;;)
6187 {
6188 if (scope == NULL)
6189 {
6190 emsg(_(e_continue));
6191 return NULL;
6192 }
6193 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6194 break;
6195 scope = scope->se_outer;
6196 }
6197
6198 // Jump back to the FOR or WHILE instruction.
6199 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006200 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6201 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006202 return arg;
6203}
6204
6205/*
6206 * compile "break"
6207 */
6208 static char_u *
6209compile_break(char_u *arg, cctx_T *cctx)
6210{
6211 scope_T *scope = cctx->ctx_scope;
6212 endlabel_T **el;
6213
6214 for (;;)
6215 {
6216 if (scope == NULL)
6217 {
6218 emsg(_(e_break));
6219 return NULL;
6220 }
6221 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6222 break;
6223 scope = scope->se_outer;
6224 }
6225
6226 // Jump to the end of the FOR or WHILE loop.
6227 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006228 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006229 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006230 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006231 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6232 return FAIL;
6233
6234 return arg;
6235}
6236
6237/*
6238 * compile "{" start of block
6239 */
6240 static char_u *
6241compile_block(char_u *arg, cctx_T *cctx)
6242{
6243 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6244 return NULL;
6245 return skipwhite(arg + 1);
6246}
6247
6248/*
6249 * compile end of block: drop one scope
6250 */
6251 static void
6252compile_endblock(cctx_T *cctx)
6253{
6254 scope_T *scope = cctx->ctx_scope;
6255
6256 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006257 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006258 vim_free(scope);
6259}
6260
6261/*
6262 * compile "try"
6263 * Creates a new scope for the try-endtry, pointing to the first catch and
6264 * finally.
6265 * Creates another scope for the "try" block itself.
6266 * TRY instruction sets up exception handling at runtime.
6267 *
6268 * "try"
6269 * TRY -> catch1, -> finally push trystack entry
6270 * ... try block
6271 * "throw {exception}"
6272 * EVAL {exception}
6273 * THROW create exception
6274 * ... try block
6275 * " catch {expr}"
6276 * JUMP -> finally
6277 * catch1: PUSH exeception
6278 * EVAL {expr}
6279 * MATCH
6280 * JUMP nomatch -> catch2
6281 * CATCH remove exception
6282 * ... catch block
6283 * " catch"
6284 * JUMP -> finally
6285 * catch2: CATCH remove exception
6286 * ... catch block
6287 * " finally"
6288 * finally:
6289 * ... finally block
6290 * " endtry"
6291 * ENDTRY pop trystack entry, may rethrow
6292 */
6293 static char_u *
6294compile_try(char_u *arg, cctx_T *cctx)
6295{
6296 garray_T *instr = &cctx->ctx_instr;
6297 scope_T *try_scope;
6298 scope_T *scope;
6299
6300 // scope that holds the jumps that go to catch/finally/endtry
6301 try_scope = new_scope(cctx, TRY_SCOPE);
6302 if (try_scope == NULL)
6303 return NULL;
6304
6305 // "catch" is set when the first ":catch" is found.
6306 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006307 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 if (generate_instr(cctx, ISN_TRY) == NULL)
6309 return NULL;
6310
6311 // scope for the try block itself
6312 scope = new_scope(cctx, BLOCK_SCOPE);
6313 if (scope == NULL)
6314 return NULL;
6315
6316 return arg;
6317}
6318
6319/*
6320 * compile "catch {expr}"
6321 */
6322 static char_u *
6323compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6324{
6325 scope_T *scope = cctx->ctx_scope;
6326 garray_T *instr = &cctx->ctx_instr;
6327 char_u *p;
6328 isn_T *isn;
6329
6330 // end block scope from :try or :catch
6331 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6332 compile_endblock(cctx);
6333 scope = cctx->ctx_scope;
6334
6335 // Error if not in a :try scope
6336 if (scope == NULL || scope->se_type != TRY_SCOPE)
6337 {
6338 emsg(_(e_catch));
6339 return NULL;
6340 }
6341
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006342 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343 {
6344 emsg(_("E1033: catch unreachable after catch-all"));
6345 return NULL;
6346 }
6347
6348 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006349 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006350 JUMP_ALWAYS, cctx) == FAIL)
6351 return NULL;
6352
6353 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006354 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006355 if (isn->isn_arg.try.try_catch == 0)
6356 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006357 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006358 {
6359 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006360 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006361 isn->isn_arg.jump.jump_where = instr->ga_len;
6362 }
6363
6364 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006365 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006366 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006367 scope->se_u.se_try.ts_caught_all = TRUE;
6368 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006369 }
6370 else
6371 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006372 char_u *end;
6373 char_u *pat;
6374 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006375 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006376 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006378 // Push v:exception, push {expr} and MATCH
6379 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6380
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006381 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006382 if (*end != *p)
6383 {
6384 semsg(_("E1067: Separator mismatch: %s"), p);
6385 vim_free(tofree);
6386 return FAIL;
6387 }
6388 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006389 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006390 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006391 len = (int)(end - tofree);
6392 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006393 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006394 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006395 if (pat == NULL)
6396 return FAIL;
6397 if (generate_PUSHS(cctx, pat) == FAIL)
6398 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6401 return NULL;
6402
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006403 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006404 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6405 return NULL;
6406 }
6407
6408 if (generate_instr(cctx, ISN_CATCH) == NULL)
6409 return NULL;
6410
6411 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6412 return NULL;
6413 return p;
6414}
6415
6416 static char_u *
6417compile_finally(char_u *arg, cctx_T *cctx)
6418{
6419 scope_T *scope = cctx->ctx_scope;
6420 garray_T *instr = &cctx->ctx_instr;
6421 isn_T *isn;
6422
6423 // end block scope from :try or :catch
6424 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6425 compile_endblock(cctx);
6426 scope = cctx->ctx_scope;
6427
6428 // Error if not in a :try scope
6429 if (scope == NULL || scope->se_type != TRY_SCOPE)
6430 {
6431 emsg(_(e_finally));
6432 return NULL;
6433 }
6434
6435 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006436 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006437 if (isn->isn_arg.try.try_finally != 0)
6438 {
6439 emsg(_(e_finally_dup));
6440 return NULL;
6441 }
6442
6443 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006444 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006445
Bram Moolenaar585fea72020-04-02 22:33:21 +02006446 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006447 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006448 {
6449 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006450 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006451 isn->isn_arg.jump.jump_where = instr->ga_len;
6452 }
6453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006454 // TODO: set index in ts_finally_label jumps
6455
6456 return arg;
6457}
6458
6459 static char_u *
6460compile_endtry(char_u *arg, cctx_T *cctx)
6461{
6462 scope_T *scope = cctx->ctx_scope;
6463 garray_T *instr = &cctx->ctx_instr;
6464 isn_T *isn;
6465
6466 // end block scope from :catch or :finally
6467 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6468 compile_endblock(cctx);
6469 scope = cctx->ctx_scope;
6470
6471 // Error if not in a :try scope
6472 if (scope == NULL || scope->se_type != TRY_SCOPE)
6473 {
6474 if (scope == NULL)
6475 emsg(_(e_no_endtry));
6476 else if (scope->se_type == WHILE_SCOPE)
6477 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006478 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006479 emsg(_(e_endfor));
6480 else
6481 emsg(_(e_endif));
6482 return NULL;
6483 }
6484
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006485 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6487 {
6488 emsg(_("E1032: missing :catch or :finally"));
6489 return NULL;
6490 }
6491
6492 // Fill in the "end" label in jumps at the end of the blocks, if not done
6493 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006494 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006495
6496 // End :catch or :finally scope: set value in ISN_TRY instruction
6497 if (isn->isn_arg.try.try_finally == 0)
6498 isn->isn_arg.try.try_finally = instr->ga_len;
6499 compile_endblock(cctx);
6500
6501 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6502 return NULL;
6503 return arg;
6504}
6505
6506/*
6507 * compile "throw {expr}"
6508 */
6509 static char_u *
6510compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6511{
6512 char_u *p = skipwhite(arg);
6513
Bram Moolenaara5565e42020-05-09 15:44:01 +02006514 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006515 return NULL;
6516 if (may_generate_2STRING(-1, cctx) == FAIL)
6517 return NULL;
6518 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6519 return NULL;
6520
6521 return p;
6522}
6523
6524/*
6525 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006526 * compile "echomsg expr"
6527 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006528 * compile "execute expr"
6529 */
6530 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006531compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006532{
6533 char_u *p = arg;
6534 int count = 0;
6535
6536 for (;;)
6537 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006538 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006539 return NULL;
6540 ++count;
6541 p = skipwhite(p);
6542 if (ends_excmd(*p))
6543 break;
6544 }
6545
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006546 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6547 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6548 else if (cmdidx == CMD_execute)
6549 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6550 else if (cmdidx == CMD_echomsg)
6551 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6552 else
6553 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006554 return p;
6555}
6556
6557/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006558 * A command that is not compiled, execute with legacy code.
6559 */
6560 static char_u *
6561compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6562{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006563 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006564 int has_expr = FALSE;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006565
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006566 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006567 goto theend;
6568
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006569 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
6570 has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND));
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006571 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6572 {
6573 // expand filename in "syntax include [@group] filename"
6574 has_expr = TRUE;
6575 eap->arg = skipwhite(eap->arg + 7);
6576 if (*eap->arg == '@')
6577 eap->arg = skiptowhite(eap->arg);
6578 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006579
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006580 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006581 {
6582 int count = 0;
6583 char_u *start = skipwhite(line);
6584
6585 // :cmd xxx`=expr1`yyy`=expr2`zzz
6586 // PUSHS ":cmd xxx"
6587 // eval expr1
6588 // PUSHS "yyy"
6589 // eval expr2
6590 // PUSHS "zzz"
6591 // EXECCONCAT 5
6592 for (;;)
6593 {
6594 if (p > start)
6595 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006596 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006597 ++count;
6598 }
6599 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006600 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006601 return NULL;
6602 may_generate_2STRING(-1, cctx);
6603 ++count;
6604 p = skipwhite(p);
6605 if (*p != '`')
6606 {
6607 emsg(_("E1083: missing backtick"));
6608 return NULL;
6609 }
6610 start = p + 1;
6611
6612 p = (char_u *)strstr((char *)start, "`=");
6613 if (p == NULL)
6614 {
6615 if (*skipwhite(start) != NUL)
6616 {
6617 generate_PUSHS(cctx, vim_strsave(start));
6618 ++count;
6619 }
6620 break;
6621 }
6622 }
6623 generate_EXECCONCAT(cctx, count);
6624 }
6625 else
6626 generate_EXEC(cctx, line);
6627
6628theend:
6629 return (char_u *)"";
6630}
6631
6632/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006633 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006634 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006635 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006636 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006637add_def_function(ufunc_T *ufunc)
6638{
6639 dfunc_T *dfunc;
6640
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006641 if (def_functions.ga_len == 0)
6642 {
6643 // The first position is not used, so that a zero uf_dfunc_idx means it
6644 // wasn't set.
6645 if (ga_grow(&def_functions, 1) == FAIL)
6646 return FAIL;
6647 ++def_functions.ga_len;
6648 }
6649
Bram Moolenaar09689a02020-05-09 22:50:08 +02006650 // Add the function to "def_functions".
6651 if (ga_grow(&def_functions, 1) == FAIL)
6652 return FAIL;
6653 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6654 CLEAR_POINTER(dfunc);
6655 dfunc->df_idx = def_functions.ga_len;
6656 ufunc->uf_dfunc_idx = dfunc->df_idx;
6657 dfunc->df_ufunc = ufunc;
6658 ++def_functions.ga_len;
6659 return OK;
6660}
6661
6662/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006663 * After ex_function() has collected all the function lines: parse and compile
6664 * the lines into instructions.
6665 * Adds the function to "def_functions".
6666 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6667 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006668 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006669 * This can be used recursively through compile_lambda(), which may reallocate
6670 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006671 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006672 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006673 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006674compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006675{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006676 char_u *line = NULL;
6677 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006679 cctx_T cctx;
6680 garray_T *instr;
6681 int called_emsg_before = called_emsg;
6682 int ret = FAIL;
6683 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006684 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006685 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006686
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006687 // When using a function that was compiled before: Free old instructions.
6688 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006689 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006690 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006691 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6692 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006693 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006694 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006695 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006696 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006697
Bram Moolenaara80faa82020-04-12 19:37:17 +02006698 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006699 cctx.ctx_ufunc = ufunc;
6700 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006701 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006702 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6703 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6704 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6705 cctx.ctx_type_list = &ufunc->uf_type_list;
6706 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6707 instr = &cctx.ctx_instr;
6708
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006709 // Set the context to the function, it may be compiled when called from
6710 // another script. Set the script version to the most modern one.
6711 // The line number will be set in next_line_from_context().
6712 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6714
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006715 // Make sure error messages are OK.
6716 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6717 if (do_estack_push)
6718 estack_push_ufunc(ufunc, 1);
6719
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006720 if (ufunc->uf_def_args.ga_len > 0)
6721 {
6722 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006723 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006724 int i;
6725 char_u *arg;
6726 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6727
6728 // Produce instructions for the default values of optional arguments.
6729 // Store the instruction index in uf_def_arg_idx[] so that we know
6730 // where to start when the function is called, depending on the number
6731 // of arguments.
6732 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6733 if (ufunc->uf_def_arg_idx == NULL)
6734 goto erret;
6735 for (i = 0; i < count; ++i)
6736 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006737 garray_T *stack = &cctx.ctx_type_stack;
6738 type_T *val_type;
6739 int arg_idx = first_def_arg + i;
6740
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006741 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6742 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006743 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006744 goto erret;
6745
6746 // If no type specified use the type of the default value.
6747 // Otherwise check that the default value type matches the
6748 // specified type.
6749 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6750 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6751 ufunc->uf_arg_types[arg_idx] = val_type;
6752 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6753 == FAIL)
6754 {
6755 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6756 arg_idx + 1);
6757 goto erret;
6758 }
6759
6760 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006761 goto erret;
6762 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006763 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6764 }
6765
6766 /*
6767 * Loop over all the lines of the function and generate instructions.
6768 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006769 for (;;)
6770 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006771 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006772 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006773 char_u *cmd;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006774
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006775 // Bail out on the first error to avoid a flood of errors and report
6776 // the right line number when inside try/catch.
6777 if (emsg_before != called_emsg)
6778 goto erret;
6779
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006780 if (line != NULL && *line == '|')
6781 // the line continues after a '|'
6782 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006783 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006784 && !(*line == '#' && (line == cctx.ctx_line_start
6785 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006787 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006788 goto erret;
6789 }
6790 else
6791 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006792 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006793 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006794 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006795 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006796 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006797 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006798
Bram Moolenaara80faa82020-04-12 19:37:17 +02006799 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006800 ea.cmdlinep = &line;
6801 ea.cmd = skipwhite(line);
6802
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006803 // Some things can be recognized by the first character.
6804 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006806 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006807 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006808 if (ea.cmd[1] != '{')
6809 {
6810 line = (char_u *)"";
6811 continue;
6812 }
6813 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006814
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006815 case '}':
6816 {
6817 // "}" ends a block scope
6818 scopetype_T stype = cctx.ctx_scope == NULL
6819 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006820
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006821 if (stype == BLOCK_SCOPE)
6822 {
6823 compile_endblock(&cctx);
6824 line = ea.cmd;
6825 }
6826 else
6827 {
6828 emsg(_("E1025: using } outside of a block scope"));
6829 goto erret;
6830 }
6831 if (line != NULL)
6832 line = skipwhite(ea.cmd + 1);
6833 continue;
6834 }
6835
6836 case '{':
6837 // "{" starts a block scope
6838 // "{'a': 1}->func() is something else
6839 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6840 {
6841 line = compile_block(ea.cmd, &cctx);
6842 continue;
6843 }
6844 break;
6845
6846 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006847 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006848 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006849 }
6850
6851 /*
6852 * COMMAND MODIFIERS
6853 */
6854 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6855 {
6856 if (errormsg != NULL)
6857 goto erret;
6858 // empty line or comment
6859 line = (char_u *)"";
6860 continue;
6861 }
6862
6863 // Skip ":call" to get to the function name.
6864 if (checkforcmd(&ea.cmd, "call", 3))
6865 ea.cmd = skipwhite(ea.cmd);
6866
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006867 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006869 char_u *pskip;
6870
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006871 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006872 // find what follows.
6873 // Skip over "var.member", "var[idx]" and the like.
6874 // Also "&opt = val", "$ENV = val" and "@r = val".
6875 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006876 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006877 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006878 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006879 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006880 char_u *var_end;
6881 int oplen;
6882 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006883
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006884 var_end = find_name_end(pskip, NULL, NULL,
6885 FNE_CHECK_START | FNE_INCL_BR);
6886 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006887 if (oplen > 0)
6888 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006889 size_t len = p - ea.cmd;
6890
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006891 // Recognize an assignment if we recognize the variable
6892 // name:
6893 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006894 // "local = expr" where "local" is a local var.
6895 // "script = expr" where "script" is a script-local var.
6896 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006897 // "&opt = expr"
6898 // "$ENV = expr"
6899 // "@r = expr"
6900 if (*ea.cmd == '&'
6901 || *ea.cmd == '$'
6902 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006903 || ((len) > 2 && ea.cmd[1] == ':')
6904 || lookup_local(ea.cmd, len, &cctx) != NULL
6905 || lookup_arg(ea.cmd, len, NULL, NULL,
6906 NULL, &cctx) == OK
6907 || lookup_script(ea.cmd, len) == OK
6908 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006909 {
6910 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006911 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006912 goto erret;
6913 continue;
6914 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006915 }
6916 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006917
6918 if (*ea.cmd == '[')
6919 {
6920 // [var, var] = expr
6921 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6922 if (line == NULL)
6923 goto erret;
6924 if (line != ea.cmd)
6925 continue;
6926 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927 }
6928
6929 /*
6930 * COMMAND after range
6931 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006932 cmd = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006934 if (ea.cmd > cmd && !starts_with_colon)
6935 {
6936 emsg(_(e_colon_required));
6937 goto erret;
6938 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006939 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006940 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006941 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006942
6943 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6944 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006945 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006946 {
6947 line += STRLEN(line);
6948 continue;
6949 }
6950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006951 // Expression or function call.
6952 if (ea.cmdidx == CMD_eval)
6953 {
6954 p = ea.cmd;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006955 if (compile_expr0(&p, &cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006956 goto erret;
6957
6958 // drop the return value
6959 generate_instr_drop(&cctx, ISN_DROP, 1);
6960 line = p;
6961 continue;
6962 }
Bram Moolenaar585fea72020-04-02 22:33:21 +02006963 // CMD_let cannot happen, compile_assignment() above is used
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006964 iemsg("Command from find_ex_command() not handled");
6965 goto erret;
6966 }
6967
6968 p = skipwhite(p);
6969
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006970 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02006971 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006972 && ea.cmdidx != CMD_elseif
6973 && ea.cmdidx != CMD_else
6974 && ea.cmdidx != CMD_endif)
6975 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006976 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006977 continue;
6978 }
6979
Bram Moolenaarefd88552020-06-18 20:50:10 +02006980 if (ea.cmdidx != CMD_elseif
6981 && ea.cmdidx != CMD_else
6982 && ea.cmdidx != CMD_endif
6983 && ea.cmdidx != CMD_endfor
6984 && ea.cmdidx != CMD_endwhile
6985 && ea.cmdidx != CMD_catch
6986 && ea.cmdidx != CMD_finally
6987 && ea.cmdidx != CMD_endtry)
6988 {
6989 if (cctx.ctx_had_return)
6990 {
6991 emsg(_("E1095: Unreachable code after :return"));
6992 goto erret;
6993 }
6994 }
6995
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006996 switch (ea.cmdidx)
6997 {
6998 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006999 ea.arg = p;
7000 line = compile_nested_function(&ea, &cctx);
7001 break;
7002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007003 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007004 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007005 goto erret;
7006
7007 case CMD_return:
7008 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007009 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007010 break;
7011
7012 case CMD_let:
7013 case CMD_const:
7014 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007015 if (line == p)
7016 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007017 break;
7018
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007019 case CMD_unlet:
7020 case CMD_unlockvar:
7021 case CMD_lockvar:
7022 line = compile_unletlock(p, &ea, &cctx);
7023 break;
7024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007025 case CMD_import:
7026 line = compile_import(p, &cctx);
7027 break;
7028
7029 case CMD_if:
7030 line = compile_if(p, &cctx);
7031 break;
7032 case CMD_elseif:
7033 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007034 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007035 break;
7036 case CMD_else:
7037 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007038 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007039 break;
7040 case CMD_endif:
7041 line = compile_endif(p, &cctx);
7042 break;
7043
7044 case CMD_while:
7045 line = compile_while(p, &cctx);
7046 break;
7047 case CMD_endwhile:
7048 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007049 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007050 break;
7051
7052 case CMD_for:
7053 line = compile_for(p, &cctx);
7054 break;
7055 case CMD_endfor:
7056 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007057 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007058 break;
7059 case CMD_continue:
7060 line = compile_continue(p, &cctx);
7061 break;
7062 case CMD_break:
7063 line = compile_break(p, &cctx);
7064 break;
7065
7066 case CMD_try:
7067 line = compile_try(p, &cctx);
7068 break;
7069 case CMD_catch:
7070 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007071 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 break;
7073 case CMD_finally:
7074 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007075 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007076 break;
7077 case CMD_endtry:
7078 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007079 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080 break;
7081 case CMD_throw:
7082 line = compile_throw(p, &cctx);
7083 break;
7084
7085 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007086 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007087 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007088 case CMD_echomsg:
7089 case CMD_echoerr:
7090 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007091 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007093 // TODO: other commands with an expression argument
7094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007095 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007096 // Not recognized, execute with do_cmdline_cmd().
7097 ea.arg = p;
7098 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007099 break;
7100 }
7101 if (line == NULL)
7102 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007103 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007104
7105 if (cctx.ctx_type_stack.ga_len < 0)
7106 {
7107 iemsg("Type stack underflow");
7108 goto erret;
7109 }
7110 }
7111
7112 if (cctx.ctx_scope != NULL)
7113 {
7114 if (cctx.ctx_scope->se_type == IF_SCOPE)
7115 emsg(_(e_endif));
7116 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7117 emsg(_(e_endwhile));
7118 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7119 emsg(_(e_endfor));
7120 else
7121 emsg(_("E1026: Missing }"));
7122 goto erret;
7123 }
7124
Bram Moolenaarefd88552020-06-18 20:50:10 +02007125 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007126 {
7127 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7128 {
7129 emsg(_("E1027: Missing return statement"));
7130 goto erret;
7131 }
7132
7133 // Return zero if there is no return at the end.
7134 generate_PUSHNR(&cctx, 0);
7135 generate_instr(&cctx, ISN_RETURN);
7136 }
7137
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007138 {
7139 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7140 + ufunc->uf_dfunc_idx;
7141 dfunc->df_deleted = FALSE;
7142 dfunc->df_instr = instr->ga_data;
7143 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007144 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007145 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007146 if (cctx.ctx_outer_used)
7147 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007148 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007150
7151 ret = OK;
7152
7153erret:
7154 if (ret == FAIL)
7155 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007156 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007157 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7158 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007159
7160 for (idx = 0; idx < instr->ga_len; ++idx)
7161 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007162 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007163
Bram Moolenaar45a15082020-05-25 00:28:33 +02007164 // if using the last entry in the table we might as well remove it
7165 if (!dfunc->df_deleted
7166 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007167 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007168 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007169
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007170 while (cctx.ctx_scope != NULL)
7171 drop_scope(&cctx);
7172
Bram Moolenaar20431c92020-03-20 18:39:46 +01007173 // Don't execute this function body.
7174 ga_clear_strings(&ufunc->uf_lines);
7175
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007176 if (errormsg != NULL)
7177 emsg(errormsg);
7178 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007179 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007180 }
7181
7182 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007183 if (do_estack_push)
7184 estack_pop();
7185
Bram Moolenaar20431c92020-03-20 18:39:46 +01007186 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007187 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007188 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007189 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007190}
7191
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007192 void
7193set_function_type(ufunc_T *ufunc)
7194{
7195 int varargs = ufunc->uf_va_name != NULL;
7196 int argcount = ufunc->uf_args.ga_len;
7197
7198 // Create a type for the function, with the return type and any
7199 // argument types.
7200 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7201 // The type is included in "tt_args".
7202 if (argcount > 0 || varargs)
7203 {
7204 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7205 argcount, &ufunc->uf_type_list);
7206 // Add argument types to the function type.
7207 if (func_type_add_arg_types(ufunc->uf_func_type,
7208 argcount + varargs,
7209 &ufunc->uf_type_list) == FAIL)
7210 return;
7211 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7212 ufunc->uf_func_type->tt_min_argcount =
7213 argcount - ufunc->uf_def_args.ga_len;
7214 if (ufunc->uf_arg_types == NULL)
7215 {
7216 int i;
7217
7218 // lambda does not have argument types.
7219 for (i = 0; i < argcount; ++i)
7220 ufunc->uf_func_type->tt_args[i] = &t_any;
7221 }
7222 else
7223 mch_memmove(ufunc->uf_func_type->tt_args,
7224 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7225 if (varargs)
7226 {
7227 ufunc->uf_func_type->tt_args[argcount] =
7228 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7229 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7230 }
7231 }
7232 else
7233 // No arguments, can use a predefined type.
7234 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7235 argcount, &ufunc->uf_type_list);
7236}
7237
7238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007239/*
7240 * Delete an instruction, free what it contains.
7241 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007242 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007243delete_instr(isn_T *isn)
7244{
7245 switch (isn->isn_type)
7246 {
7247 case ISN_EXEC:
7248 case ISN_LOADENV:
7249 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007250 case ISN_LOADB:
7251 case ISN_LOADW:
7252 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007253 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007254 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007255 case ISN_PUSHEXC:
7256 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007257 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007259 case ISN_STOREB:
7260 case ISN_STOREW:
7261 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007262 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007263 vim_free(isn->isn_arg.string);
7264 break;
7265
7266 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007267 case ISN_STORES:
7268 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007269 break;
7270
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007271 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007272 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007273 vim_free(isn->isn_arg.unlet.ul_name);
7274 break;
7275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007276 case ISN_STOREOPT:
7277 vim_free(isn->isn_arg.storeopt.so_name);
7278 break;
7279
7280 case ISN_PUSHBLOB: // push blob isn_arg.blob
7281 blob_unref(isn->isn_arg.blob);
7282 break;
7283
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007284 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007285#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007286 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007287#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007288 break;
7289
7290 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007291#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007292 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007293#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007294 break;
7295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007296 case ISN_UCALL:
7297 vim_free(isn->isn_arg.ufunc.cuf_name);
7298 break;
7299
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007300 case ISN_FUNCREF:
7301 {
7302 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7303 + isn->isn_arg.funcref.fr_func;
7304 func_ptr_unref(dfunc->df_ufunc);
7305 }
7306 break;
7307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007308 case ISN_2BOOL:
7309 case ISN_2STRING:
7310 case ISN_ADDBLOB:
7311 case ISN_ADDLIST:
7312 case ISN_BCALL:
7313 case ISN_CATCH:
7314 case ISN_CHECKNR:
7315 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007316 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007317 case ISN_COMPAREANY:
7318 case ISN_COMPAREBLOB:
7319 case ISN_COMPAREBOOL:
7320 case ISN_COMPAREDICT:
7321 case ISN_COMPAREFLOAT:
7322 case ISN_COMPAREFUNC:
7323 case ISN_COMPARELIST:
7324 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007325 case ISN_COMPARESPECIAL:
7326 case ISN_COMPARESTRING:
7327 case ISN_CONCAT:
7328 case ISN_DCALL:
7329 case ISN_DROP:
7330 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007331 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007332 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007333 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007334 case ISN_EXECCONCAT:
7335 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007336 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007337 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007338 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007339 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007340 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007341 case ISN_JUMP:
7342 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007343 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 case ISN_LOADSCRIPT:
7345 case ISN_LOADREG:
7346 case ISN_LOADV:
7347 case ISN_NEGATENR:
7348 case ISN_NEWDICT:
7349 case ISN_NEWLIST:
7350 case ISN_OPNR:
7351 case ISN_OPFLOAT:
7352 case ISN_OPANY:
7353 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007354 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007355 case ISN_PUSHF:
7356 case ISN_PUSHNR:
7357 case ISN_PUSHBOOL:
7358 case ISN_PUSHSPEC:
7359 case ISN_RETURN:
7360 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007361 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007362 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007363 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007364 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007366 case ISN_STOREDICT:
7367 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368 case ISN_THROW:
7369 case ISN_TRY:
7370 // nothing allocated
7371 break;
7372 }
7373}
7374
7375/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007376 * Free all instructions for "dfunc".
7377 */
7378 static void
7379delete_def_function_contents(dfunc_T *dfunc)
7380{
7381 int idx;
7382
7383 ga_clear(&dfunc->df_def_args_isn);
7384
7385 if (dfunc->df_instr != NULL)
7386 {
7387 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7388 delete_instr(dfunc->df_instr + idx);
7389 VIM_CLEAR(dfunc->df_instr);
7390 }
7391
7392 dfunc->df_deleted = TRUE;
7393}
7394
7395/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007396 * When a user function is deleted, clear the contents of any associated def
7397 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007398 */
7399 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007400clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007401{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007402 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007403 {
7404 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7405 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007406
Bram Moolenaar20431c92020-03-20 18:39:46 +01007407 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007408 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007409 }
7410}
7411
7412#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007413/*
7414 * Free all functions defined with ":def".
7415 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007416 void
7417free_def_functions(void)
7418{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007419 int idx;
7420
7421 for (idx = 0; idx < def_functions.ga_len; ++idx)
7422 {
7423 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7424
7425 delete_def_function_contents(dfunc);
7426 }
7427
7428 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007429}
7430#endif
7431
7432
7433#endif // FEAT_EVAL