blob: ff6668fe46ceca5ecf1031cc05c88c4a977b7bfe [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
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200291check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100292{
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.
3855 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003856 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 if (ret != FAIL && *start == '>')
3858 ret = compile_lambda(arg, cctx);
3859 else
3860 ret = compile_dict(arg, cctx, FALSE);
3861 }
3862 break;
3863
3864 /*
3865 * Option value: &name
3866 */
3867 case '&': ret = compile_get_option(arg, cctx);
3868 break;
3869
3870 /*
3871 * Environment variable: $VAR.
3872 */
3873 case '$': ret = compile_get_env(arg, cctx);
3874 break;
3875
3876 /*
3877 * Register contents: @r.
3878 */
3879 case '@': ret = compile_get_register(arg, cctx);
3880 break;
3881 /*
3882 * nested expression: (expression).
3883 */
3884 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003885
3886 // recursive!
3887 if (ppconst->pp_used <= PPSIZE - 10)
3888 {
3889 ret = compile_expr1(arg, cctx, ppconst);
3890 }
3891 else
3892 {
3893 // Not enough space in ppconst, flush constants.
3894 if (generate_ppconst(cctx, ppconst) == FAIL)
3895 return FAIL;
3896 ret = compile_expr0(arg, cctx);
3897 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 *arg = skipwhite(*arg);
3899 if (**arg == ')')
3900 ++*arg;
3901 else if (ret == OK)
3902 {
3903 emsg(_(e_missing_close));
3904 ret = FAIL;
3905 }
3906 break;
3907
3908 default: ret = NOTDONE;
3909 break;
3910 }
3911 if (ret == FAIL)
3912 return FAIL;
3913
Bram Moolenaar1c747212020-05-09 18:28:34 +02003914 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 {
3916 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003917 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003919 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 return FAIL;
3921 }
3922 start_leader = end_leader; // don't apply again below
3923
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003924 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003925 clear_tv(rettv);
3926 else
3927 // A constant expression can possibly be handled compile time,
3928 // return the value instead of generating code.
3929 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 }
3931 else if (ret == NOTDONE)
3932 {
3933 char_u *p;
3934 int r;
3935
3936 if (!eval_isnamec1(**arg))
3937 {
3938 semsg(_("E1015: Name expected: %s"), *arg);
3939 return FAIL;
3940 }
3941
3942 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003943 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003945 {
3946 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3947 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003949 {
3950 if (generate_ppconst(cctx, ppconst) == FAIL)
3951 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 if (r == FAIL)
3955 return FAIL;
3956 }
3957
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003958 // Handle following "[]", ".member", etc.
3959 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003960 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003961 ppconst) == FAIL)
3962 return FAIL;
3963 if (ppconst->pp_used > 0)
3964 {
3965 // apply the '!', '-' and '+' before the constant
3966 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3967 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
3968 return FAIL;
3969 return OK;
3970 }
3971 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003973 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974}
3975
3976/*
3977 * * number multiplication
3978 * / number division
3979 * % number modulo
3980 */
3981 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003982compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983{
3984 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003985 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003986 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003988 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003989 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 return FAIL;
3991
3992 /*
3993 * Repeat computing, until no "*", "/" or "%" is following.
3994 */
3995 for (;;)
3996 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003997 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 if (*op != '*' && *op != '/' && *op != '%')
3999 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004000 if (next != NULL)
4001 {
4002 *arg = next_line_from_context(cctx, TRUE);
4003 op = skipwhite(*arg);
4004 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004005
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004006 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 {
4008 char_u buf[3];
4009
4010 vim_strncpy(buf, op, 1);
4011 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004012 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 }
4014 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004015 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004016 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004018 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004019 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004021
4022 if (ppconst->pp_used == ppconst_used + 2
4023 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4024 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004025 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004026 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4027 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004028 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004030 // both are numbers: compute the result
4031 switch (*op)
4032 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004033 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004034 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004035 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004036 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004037 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004038 break;
4039 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004040 tv1->vval.v_number = res;
4041 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004042 }
4043 else
4044 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004045 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004046 generate_two_op(cctx, op);
4047 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 }
4049
4050 return OK;
4051}
4052
4053/*
4054 * + number addition
4055 * - number subtraction
4056 * .. string concatenation
4057 */
4058 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004059compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060{
4061 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004062 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004064 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065
4066 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004067 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 return FAIL;
4069
4070 /*
4071 * Repeat computing, until no "+", "-" or ".." is following.
4072 */
4073 for (;;)
4074 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004075 op = may_peek_next_line(cctx, *arg, &next);
4076 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 break;
4078 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004079 if (next != NULL)
4080 {
4081 *arg = next_line_from_context(cctx, TRUE);
4082 op = skipwhite(*arg);
4083 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004085 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 {
4087 char_u buf[3];
4088
4089 vim_strncpy(buf, op, oplen);
4090 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004091 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 }
4093
4094 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004095 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004096 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004098 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004099 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 return FAIL;
4101
Bram Moolenaara5565e42020-05-09 15:44:01 +02004102 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004103 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004104 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4105 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4106 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4107 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004109 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4110 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004111
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004112 // concat/subtract/add constant numbers
4113 if (*op == '+')
4114 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4115 else if (*op == '-')
4116 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4117 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004118 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004119 // concatenate constant strings
4120 char_u *s1 = tv1->vval.v_string;
4121 char_u *s2 = tv2->vval.v_string;
4122 size_t len1 = STRLEN(s1);
4123
4124 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4125 if (tv1->vval.v_string == NULL)
4126 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004127 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004128 return FAIL;
4129 }
4130 mch_memmove(tv1->vval.v_string, s1, len1);
4131 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004132 vim_free(s1);
4133 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004134 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004135 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 }
4137 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004138 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004139 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004140 if (*op == '.')
4141 {
4142 if (may_generate_2STRING(-2, cctx) == FAIL
4143 || may_generate_2STRING(-1, cctx) == FAIL)
4144 return FAIL;
4145 generate_instr_drop(cctx, ISN_CONCAT, 1);
4146 }
4147 else
4148 generate_two_op(cctx, op);
4149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 }
4151
4152 return OK;
4153}
4154
4155/*
4156 * expr5a == expr5b
4157 * expr5a =~ expr5b
4158 * expr5a != expr5b
4159 * expr5a !~ expr5b
4160 * expr5a > expr5b
4161 * expr5a >= expr5b
4162 * expr5a < expr5b
4163 * expr5a <= expr5b
4164 * expr5a is expr5b
4165 * expr5a isnot expr5b
4166 *
4167 * Produces instructions:
4168 * EVAL expr5a Push result of "expr5a"
4169 * EVAL expr5b Push result of "expr5b"
4170 * COMPARE one of the compare instructions
4171 */
4172 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004173compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174{
4175 exptype_T type = EXPR_UNKNOWN;
4176 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004177 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004180 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181
4182 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004183 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 return FAIL;
4185
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004186 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004187 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188
4189 /*
4190 * If there is a comparative operator, use it.
4191 */
4192 if (type != EXPR_UNKNOWN)
4193 {
4194 int ic = FALSE; // Default: do not ignore case
4195
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004196 if (next != NULL)
4197 {
4198 *arg = next_line_from_context(cctx, TRUE);
4199 p = skipwhite(*arg);
4200 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 if (type_is && (p[len] == '?' || p[len] == '#'))
4202 {
4203 semsg(_(e_invexpr2), *arg);
4204 return FAIL;
4205 }
4206 // extra question mark appended: ignore case
4207 if (p[len] == '?')
4208 {
4209 ic = TRUE;
4210 ++len;
4211 }
4212 // extra '#' appended: match case (ignored)
4213 else if (p[len] == '#')
4214 ++len;
4215 // nothing appended: match case
4216
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004217 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 {
4219 char_u buf[7];
4220
4221 vim_strncpy(buf, p, len);
4222 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004223 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 }
4225
4226 // get the second variable
4227 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004228 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004229 return FAIL;
4230
Bram Moolenaara5565e42020-05-09 15:44:01 +02004231 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 return FAIL;
4233
Bram Moolenaara5565e42020-05-09 15:44:01 +02004234 if (ppconst->pp_used == ppconst_used + 2)
4235 {
4236 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4237 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4238 int ret;
4239
4240 // Both sides are a constant, compute the result now.
4241 // First check for a valid combination of types, this is more
4242 // strict than typval_compare().
4243 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
4244 ret = FAIL;
4245 else
4246 {
4247 ret = typval_compare(tv1, tv2, type, ic);
4248 tv1->v_type = VAR_BOOL;
4249 tv1->vval.v_number = tv1->vval.v_number
4250 ? VVAL_TRUE : VVAL_FALSE;
4251 clear_tv(tv2);
4252 --ppconst->pp_used;
4253 }
4254 return ret;
4255 }
4256
4257 generate_ppconst(cctx, ppconst);
4258 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 }
4260
4261 return OK;
4262}
4263
Bram Moolenaar7f141552020-05-09 17:35:53 +02004264static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266/*
4267 * Compile || or &&.
4268 */
4269 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004270compile_and_or(
4271 char_u **arg,
4272 cctx_T *cctx,
4273 char *op,
4274 ppconst_T *ppconst,
4275 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004277 char_u *next;
4278 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 int opchar = *op;
4280
4281 if (p[0] == opchar && p[1] == opchar)
4282 {
4283 garray_T *instr = &cctx->ctx_instr;
4284 garray_T end_ga;
4285
4286 /*
4287 * Repeat until there is no following "||" or "&&"
4288 */
4289 ga_init2(&end_ga, sizeof(int), 10);
4290 while (p[0] == opchar && p[1] == opchar)
4291 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004292 if (next != NULL)
4293 {
4294 *arg = next_line_from_context(cctx, TRUE);
4295 p = skipwhite(*arg);
4296 }
4297
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004298 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4299 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004301 return FAIL;
4302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303
Bram Moolenaara5565e42020-05-09 15:44:01 +02004304 // TODO: use ppconst if the value is a constant
4305 generate_ppconst(cctx, ppconst);
4306
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 if (ga_grow(&end_ga, 1) == FAIL)
4308 {
4309 ga_clear(&end_ga);
4310 return FAIL;
4311 }
4312 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4313 ++end_ga.ga_len;
4314 generate_JUMP(cctx, opchar == '|'
4315 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4316
4317 // eval the next expression
4318 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004319 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004320 return FAIL;
4321
Bram Moolenaara5565e42020-05-09 15:44:01 +02004322 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4323 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 {
4325 ga_clear(&end_ga);
4326 return FAIL;
4327 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004328
4329 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004331 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332
4333 // Fill in the end label in all jumps.
4334 while (end_ga.ga_len > 0)
4335 {
4336 isn_T *isn;
4337
4338 --end_ga.ga_len;
4339 isn = ((isn_T *)instr->ga_data)
4340 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4341 isn->isn_arg.jump.jump_where = instr->ga_len;
4342 }
4343 ga_clear(&end_ga);
4344 }
4345
4346 return OK;
4347}
4348
4349/*
4350 * expr4a && expr4a && expr4a logical AND
4351 *
4352 * Produces instructions:
4353 * EVAL expr4a Push result of "expr4a"
4354 * JUMP_AND_KEEP_IF_FALSE end
4355 * EVAL expr4b Push result of "expr4b"
4356 * JUMP_AND_KEEP_IF_FALSE end
4357 * EVAL expr4c Push result of "expr4c"
4358 * end:
4359 */
4360 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004361compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004363 int ppconst_used = ppconst->pp_used;
4364
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004366 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 return FAIL;
4368
4369 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004370 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371}
4372
4373/*
4374 * expr3a || expr3b || expr3c logical OR
4375 *
4376 * Produces instructions:
4377 * EVAL expr3a Push result of "expr3a"
4378 * JUMP_AND_KEEP_IF_TRUE end
4379 * EVAL expr3b Push result of "expr3b"
4380 * JUMP_AND_KEEP_IF_TRUE end
4381 * EVAL expr3c Push result of "expr3c"
4382 * end:
4383 */
4384 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004385compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004387 int ppconst_used = ppconst->pp_used;
4388
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004390 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391 return FAIL;
4392
4393 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004394 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395}
4396
4397/*
4398 * Toplevel expression: expr2 ? expr1a : expr1b
4399 *
4400 * Produces instructions:
4401 * EVAL expr2 Push result of "expr"
4402 * JUMP_IF_FALSE alt jump if false
4403 * EVAL expr1a
4404 * JUMP_ALWAYS end
4405 * alt: EVAL expr1b
4406 * end:
4407 */
4408 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004409compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410{
4411 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004412 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004413 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414
Bram Moolenaar61a89812020-05-07 16:58:17 +02004415 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004416 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 return FAIL;
4418
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004419 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 if (*p == '?')
4421 {
4422 garray_T *instr = &cctx->ctx_instr;
4423 garray_T *stack = &cctx->ctx_type_stack;
4424 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004425 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004427 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004428 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004429 int has_const_expr = FALSE;
4430 int const_value = FALSE;
4431 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004433 if (next != NULL)
4434 {
4435 *arg = next_line_from_context(cctx, TRUE);
4436 p = skipwhite(*arg);
4437 }
4438
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004439 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4440 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004442 return FAIL;
4443 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444
Bram Moolenaara5565e42020-05-09 15:44:01 +02004445 if (ppconst->pp_used == ppconst_used + 1)
4446 {
4447 // the condition is a constant, we know whether the ? or the :
4448 // expression is to be evaluated.
4449 has_const_expr = TRUE;
4450 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4451 clear_tv(&ppconst->pp_tv[ppconst_used]);
4452 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004453 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4454 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004455 }
4456 else
4457 {
4458 generate_ppconst(cctx, ppconst);
4459 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4460 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461
4462 // evaluate the second expression; any type is accepted
4463 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004464 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004465 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004466 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004467 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468
Bram Moolenaara5565e42020-05-09 15:44:01 +02004469 if (!has_const_expr)
4470 {
4471 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472
Bram Moolenaara5565e42020-05-09 15:44:01 +02004473 // remember the type and drop it
4474 --stack->ga_len;
4475 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476
Bram Moolenaara5565e42020-05-09 15:44:01 +02004477 end_idx = instr->ga_len;
4478 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4479
4480 // jump here from JUMP_IF_FALSE
4481 isn = ((isn_T *)instr->ga_data) + alt_idx;
4482 isn->isn_arg.jump.jump_where = instr->ga_len;
4483 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484
4485 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004486 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 if (*p != ':')
4488 {
4489 emsg(_(e_missing_colon));
4490 return FAIL;
4491 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004492 if (next != NULL)
4493 {
4494 *arg = next_line_from_context(cctx, TRUE);
4495 p = skipwhite(*arg);
4496 }
4497
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004498 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4499 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004501 return FAIL;
4502 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503
4504 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004505 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004506 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4507 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004509 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004510 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004511 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004512 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513
Bram Moolenaara5565e42020-05-09 15:44:01 +02004514 if (!has_const_expr)
4515 {
4516 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517
Bram Moolenaara5565e42020-05-09 15:44:01 +02004518 // If the types differ, the result has a more generic type.
4519 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4520 common_type(type1, type2, &type2, cctx->ctx_type_list);
4521
4522 // jump here from JUMP_ALWAYS
4523 isn = ((isn_T *)instr->ga_data) + end_idx;
4524 isn->isn_arg.jump.jump_where = instr->ga_len;
4525 }
4526
4527 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528 }
4529 return OK;
4530}
4531
4532/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004533 * Toplevel expression.
4534 */
4535 static int
4536compile_expr0(char_u **arg, cctx_T *cctx)
4537{
4538 ppconst_T ppconst;
4539
4540 CLEAR_FIELD(ppconst);
4541 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4542 {
4543 clear_ppconst(&ppconst);
4544 return FAIL;
4545 }
4546 if (generate_ppconst(cctx, &ppconst) == FAIL)
4547 return FAIL;
4548 return OK;
4549}
4550
4551/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 * compile "return [expr]"
4553 */
4554 static char_u *
4555compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4556{
4557 char_u *p = arg;
4558 garray_T *stack = &cctx->ctx_type_stack;
4559 type_T *stack_type;
4560
4561 if (*p != NUL && *p != '|' && *p != '\n')
4562 {
4563 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004564 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 return NULL;
4566
4567 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4568 if (set_return_type)
4569 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004570 else
4571 {
4572 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4573 && stack_type->tt_type != VAR_VOID
4574 && stack_type->tt_type != VAR_UNKNOWN)
4575 {
4576 emsg(_("E1096: Returning a value in a function without a return type"));
4577 return NULL;
4578 }
4579 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 == FAIL)
4581 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004582 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583 }
4584 else
4585 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004586 // "set_return_type" cannot be TRUE, only used for a lambda which
4587 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004588 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4589 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590 {
4591 emsg(_("E1003: Missing return value"));
4592 return NULL;
4593 }
4594
4595 // No argument, return zero.
4596 generate_PUSHNR(cctx, 0);
4597 }
4598
4599 if (generate_instr(cctx, ISN_RETURN) == NULL)
4600 return NULL;
4601
4602 // "return val | endif" is possible
4603 return skipwhite(p);
4604}
4605
4606/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004607 * Get a line from the compilation context, compatible with exarg_T getline().
4608 * Return a pointer to the line in allocated memory.
4609 * Return NULL for end-of-file or some error.
4610 */
4611 static char_u *
4612exarg_getline(
4613 int c UNUSED,
4614 void *cookie,
4615 int indent UNUSED,
4616 int do_concat UNUSED)
4617{
4618 cctx_T *cctx = (cctx_T *)cookie;
4619
4620 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4621 {
4622 iemsg("Heredoc got to end");
4623 return NULL;
4624 }
4625 ++cctx->ctx_lnum;
4626 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4627 [cctx->ctx_lnum]);
4628}
4629
4630/*
4631 * Compile a nested :def command.
4632 */
4633 static char_u *
4634compile_nested_function(exarg_T *eap, cctx_T *cctx)
4635{
4636 char_u *name_start = eap->arg;
4637 char_u *name_end = to_name_end(eap->arg, FALSE);
4638 char_u *name = get_lambda_name();
4639 lvar_T *lvar;
4640 ufunc_T *ufunc;
4641
4642 eap->arg = name_end;
4643 eap->getline = exarg_getline;
4644 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004645 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004646 eap->forceit = FALSE;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004647 ufunc = def_function(eap, name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004648
Bram Moolenaar822ba242020-05-24 23:00:18 +02004649 if (ufunc == NULL)
4650 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004651 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004652 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004653 return NULL;
4654
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004655 // Define a local variable for the function reference.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004656 lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004657 TRUE, ufunc->uf_func_type);
4658
4659 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
4660 || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
4661 return NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004662
Bram Moolenaar61a89812020-05-07 16:58:17 +02004663 // TODO: warning for trailing text?
Bram Moolenaar04b12692020-05-04 23:24:44 +02004664 return (char_u *)"";
4665}
4666
4667/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 * Return the length of an assignment operator, or zero if there isn't one.
4669 */
4670 int
4671assignment_len(char_u *p, int *heredoc)
4672{
4673 if (*p == '=')
4674 {
4675 if (p[1] == '<' && p[2] == '<')
4676 {
4677 *heredoc = TRUE;
4678 return 3;
4679 }
4680 return 1;
4681 }
4682 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4683 return 2;
4684 if (STRNCMP(p, "..=", 3) == 0)
4685 return 3;
4686 return 0;
4687}
4688
4689// words that cannot be used as a variable
4690static char *reserved[] = {
4691 "true",
4692 "false",
4693 NULL
4694};
4695
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004696typedef enum {
4697 dest_local,
4698 dest_option,
4699 dest_env,
4700 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004701 dest_buffer,
4702 dest_window,
4703 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004704 dest_vimvar,
4705 dest_script,
4706 dest_reg,
4707} assign_dest_T;
4708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004710 * Generate the load instruction for "name".
4711 */
4712 static void
4713generate_loadvar(
4714 cctx_T *cctx,
4715 assign_dest_T dest,
4716 char_u *name,
4717 lvar_T *lvar,
4718 type_T *type)
4719{
4720 switch (dest)
4721 {
4722 case dest_option:
4723 // TODO: check the option exists
4724 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4725 break;
4726 case dest_global:
4727 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4728 break;
4729 case dest_buffer:
4730 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4731 break;
4732 case dest_window:
4733 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4734 break;
4735 case dest_tab:
4736 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4737 break;
4738 case dest_script:
4739 compile_load_scriptvar(cctx,
4740 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4741 break;
4742 case dest_env:
4743 // Include $ in the name here
4744 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4745 break;
4746 case dest_reg:
4747 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4748 break;
4749 case dest_vimvar:
4750 generate_LOADV(cctx, name + 2, TRUE);
4751 break;
4752 case dest_local:
4753 if (lvar->lv_from_outer)
4754 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4755 NULL, type);
4756 else
4757 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4758 break;
4759 }
4760}
4761
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004762 void
4763vim9_declare_error(char_u *name)
4764{
4765 char *scope = "";
4766
4767 switch (*name)
4768 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004769 case 'g': scope = _("global"); break;
4770 case 'b': scope = _("buffer"); break;
4771 case 'w': scope = _("window"); break;
4772 case 't': scope = _("tab"); break;
4773 case 'v': scope = "v:"; break;
4774 case '$': semsg(_(e_declare_env_var), name); return;
4775 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004776 }
4777 semsg(_(e_declare_var), scope, name);
4778}
4779
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004780/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004781 * Compile declaration and assignment:
4782 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004783 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004784 * Return NULL for an error.
4785 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 */
4787 static char_u *
4788compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4789{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004790 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004792 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793 char_u *ret = NULL;
4794 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004795 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004797 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004798 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800 int oplen = 0;
4801 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004802 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004803 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004804 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004807
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004808 // Skip over the "var" or "[var, var]" to get to any "=".
4809 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4810 if (p == NULL)
4811 return *arg == '[' ? arg : NULL;
4812
4813 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004815 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 return NULL;
4817 }
4818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004819 sp = p;
4820 p = skipwhite(p);
4821 op = p;
4822 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004823
4824 if (var_count > 0 && oplen == 0)
4825 // can be something like "[1, 2]->func()"
4826 return arg;
4827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004828 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4829 {
4830 char_u buf[4];
4831
4832 vim_strncpy(buf, op, oplen);
4833 semsg(_(e_white_both), buf);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004834 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004835 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 if (heredoc)
4838 {
4839 list_T *l;
4840 listitem_T *li;
4841
4842 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004843 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004845 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846
4847 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004848 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849 {
4850 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4851 li->li_tv.vval.v_string = NULL;
4852 }
4853 generate_NEWLIST(cctx, l->lv_len);
4854 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004855 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856 list_free(l);
4857 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004858 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004859 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004860 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004862 // for "[var, var] = expr" evaluate the expression here, loop over the
4863 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004864
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004865 p = skipwhite(op + oplen);
4866 if (compile_expr0(&p, cctx) == FAIL)
4867 return NULL;
4868 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004870 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004872 type_T *stacktype;
4873
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004874 stacktype = stack->ga_len == 0 ? &t_void
4875 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004876 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004878 emsg(_(e_cannot_use_void));
4879 goto theend;
4880 }
4881 if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL)
4882 goto theend;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004883 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4884 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004885 }
4886 }
4887
4888 /*
4889 * Loop over variables in "[var, var] = expr".
4890 * For "var = expr" and "let var: type" this is done only once.
4891 */
4892 if (var_count > 0)
4893 var_start = skipwhite(arg + 1); // skip over the "["
4894 else
4895 var_start = arg;
4896 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4897 {
4898 char_u *var_end = skip_var_one(var_start, FALSE);
4899 size_t varlen;
4900 int new_local = FALSE;
4901 int opt_type;
4902 int opt_flags = 0;
4903 assign_dest_T dest = dest_local;
4904 int vimvaridx = -1;
4905 lvar_T *lvar = NULL;
4906 lvar_T arg_lvar;
4907 int has_type = FALSE;
4908 int has_index = FALSE;
4909 int instr_count = -1;
4910
4911 p = (*var_start == '&' || *var_start == '$'
4912 || *var_start == '@') ? var_start + 1 : var_start;
4913 p = to_name_end(p, TRUE);
4914
4915 // "a: type" is declaring variable "a" with a type, not "a:".
4916 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4917 --var_end;
4918 if (is_decl && p == var_start + 2 && p[-1] == ':')
4919 --p;
4920
4921 varlen = p - var_start;
4922 vim_free(name);
4923 name = vim_strnsave(var_start, varlen);
4924 if (name == NULL)
4925 return NULL;
4926 if (!heredoc)
4927 type = &t_any;
4928
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004929 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004930 {
4931 if (*var_start == '&')
4932 {
4933 int cc;
4934 long numval;
4935
4936 dest = dest_option;
4937 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004939 emsg(_(e_const_option));
4940 goto theend;
4941 }
4942 if (is_decl)
4943 {
4944 semsg(_("E1052: Cannot declare an option: %s"), var_start);
4945 goto theend;
4946 }
4947 p = var_start;
4948 p = find_option_end(&p, &opt_flags);
4949 if (p == NULL)
4950 {
4951 // cannot happen?
4952 emsg(_(e_letunexp));
4953 goto theend;
4954 }
4955 cc = *p;
4956 *p = NUL;
4957 opt_type = get_option_value(var_start + 1, &numval,
4958 NULL, opt_flags);
4959 *p = cc;
4960 if (opt_type == -3)
4961 {
4962 semsg(_(e_unknown_option), var_start);
4963 goto theend;
4964 }
4965 if (opt_type == -2 || opt_type == 0)
4966 type = &t_string;
4967 else
4968 type = &t_number; // both number and boolean option
4969 }
4970 else if (*var_start == '$')
4971 {
4972 dest = dest_env;
4973 type = &t_string;
4974 if (is_decl)
4975 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004976 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004977 goto theend;
4978 }
4979 }
4980 else if (*var_start == '@')
4981 {
4982 if (!valid_yank_reg(var_start[1], TRUE))
4983 {
4984 emsg_invreg(var_start[1]);
4985 goto theend;
4986 }
4987 dest = dest_reg;
4988 type = &t_string;
4989 if (is_decl)
4990 {
4991 semsg(_("E1066: Cannot declare a register: %s"), name);
4992 goto theend;
4993 }
4994 }
4995 else if (STRNCMP(var_start, "g:", 2) == 0)
4996 {
4997 dest = dest_global;
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, "b:", 2) == 0)
5005 {
5006 dest = dest_buffer;
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, "w:", 2) == 0)
5014 {
5015 dest = dest_window;
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, "t:", 2) == 0)
5023 {
5024 dest = dest_tab;
5025 if (is_decl)
5026 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005027 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005028 goto theend;
5029 }
5030 }
5031 else if (STRNCMP(var_start, "v:", 2) == 0)
5032 {
5033 typval_T *vtv;
5034 int di_flags;
5035
5036 vimvaridx = find_vim_var(name + 2, &di_flags);
5037 if (vimvaridx < 0)
5038 {
5039 semsg(_(e_var_notfound), var_start);
5040 goto theend;
5041 }
5042 // We use the current value of "sandbox" here, is that OK?
5043 if (var_check_ro(di_flags, name, FALSE))
5044 goto theend;
5045 dest = dest_vimvar;
5046 vtv = get_vim_var_tv(vimvaridx);
5047 type = typval2type(vtv);
5048 if (is_decl)
5049 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005050 vim9_declare_error(name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005051 goto theend;
5052 }
5053 }
5054 else
5055 {
5056 int idx;
5057
5058 for (idx = 0; reserved[idx] != NULL; ++idx)
5059 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005060 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005061 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005062 goto theend;
5063 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005064
5065 lvar = lookup_local(var_start, varlen, cctx);
5066 if (lvar == NULL)
5067 {
5068 CLEAR_FIELD(arg_lvar);
5069 if (lookup_arg(var_start, varlen,
5070 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5071 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005072 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005073 if (is_decl)
5074 {
5075 semsg(_(e_used_as_arg), name);
5076 goto theend;
5077 }
5078 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005079 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005080 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005081 if (lvar != NULL)
5082 {
5083 if (is_decl)
5084 {
5085 semsg(_("E1017: Variable already declared: %s"), name);
5086 goto theend;
5087 }
5088 else if (lvar->lv_const)
5089 {
5090 semsg(_("E1018: Cannot assign to a constant: %s"),
5091 name);
5092 goto theend;
5093 }
5094 }
5095 else if (STRNCMP(var_start, "s:", 2) == 0
5096 || lookup_script(var_start, varlen) == OK
5097 || find_imported(var_start, varlen, cctx) != NULL)
5098 {
5099 dest = dest_script;
5100 if (is_decl)
5101 {
5102 semsg(_("E1054: Variable already declared in the script: %s"),
5103 name);
5104 goto theend;
5105 }
5106 }
5107 else if (name[1] == ':' && name[2] != NUL)
5108 {
5109 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5110 name);
5111 goto theend;
5112 }
5113 else if (!is_decl)
5114 {
5115 semsg(_("E1089: unknown variable: %s"), name);
5116 goto theend;
5117 }
5118 }
5119 }
5120
5121 // handle "a:name" as a name, not index "name" on "a"
5122 if (varlen > 1 || var_start[varlen] != ':')
5123 p = var_end;
5124
5125 if (dest != dest_option)
5126 {
5127 if (is_decl && *p == ':')
5128 {
5129 // parse optional type: "let var: type = expr"
5130 if (!VIM_ISWHITE(p[1]))
5131 {
5132 semsg(_(e_white_after), ":");
5133 goto theend;
5134 }
5135 p = skipwhite(p + 1);
5136 type = parse_type(&p, cctx->ctx_type_list);
5137 has_type = TRUE;
5138 }
5139 else if (lvar != NULL)
5140 type = lvar->lv_type;
5141 }
5142
5143 if (oplen == 3 && !heredoc && dest != dest_global
5144 && type->tt_type != VAR_STRING
5145 && type->tt_type != VAR_ANY)
5146 {
5147 emsg(_("E1019: Can only concatenate to string"));
5148 goto theend;
5149 }
5150
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005151 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005152 {
5153 if (oplen > 1 && !heredoc)
5154 {
5155 // +=, /=, etc. require an existing variable
5156 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5157 name);
5158 goto theend;
5159 }
5160
5161 // new local variable
5162 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5163 goto theend;
5164 lvar = reserve_local(cctx, var_start, varlen,
5165 cmdidx == CMD_const, type);
5166 if (lvar == NULL)
5167 goto theend;
5168 new_local = TRUE;
5169 }
5170
5171 member_type = type;
5172 if (var_end > var_start + varlen)
5173 {
5174 // Something follows after the variable: "var[idx]".
5175 if (is_decl)
5176 {
5177 emsg(_("E1087: cannot use an index when declaring a variable"));
5178 goto theend;
5179 }
5180
5181 if (var_start[varlen] == '[')
5182 {
5183 has_index = TRUE;
5184 if (type->tt_member == NULL)
5185 {
5186 semsg(_("E1088: cannot use an index on %s"), name);
5187 goto theend;
5188 }
5189 member_type = type->tt_member;
5190 }
5191 else
5192 {
5193 semsg("Not supported yet: %s", var_start);
5194 goto theend;
5195 }
5196 }
5197 else if (lvar == &arg_lvar)
5198 {
5199 semsg(_("E1090: Cannot assign to argument %s"), name);
5200 goto theend;
5201 }
5202
5203 if (!heredoc)
5204 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005205 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005206 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005207 if (oplen > 0 && var_count == 0)
5208 {
5209 // skip over the "=" and the expression
5210 p = skipwhite(op + oplen);
5211 compile_expr0(&p, cctx);
5212 }
5213 }
5214 else if (oplen > 0)
5215 {
5216 type_T *stacktype;
5217
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005218 // For "var = expr" evaluate the expression.
5219 if (var_count == 0)
5220 {
5221 int r;
5222
5223 // for "+=", "*=", "..=" etc. first load the current value
5224 if (*op != '=')
5225 {
5226 generate_loadvar(cctx, dest, name, lvar, type);
5227
5228 if (has_index)
5229 {
5230 // TODO: get member from list or dict
5231 emsg("Index with operation not supported yet");
5232 goto theend;
5233 }
5234 }
5235
5236 // Compile the expression. Temporarily hide the new local
5237 // variable here, it is not available to this expression.
5238 if (new_local)
5239 --cctx->ctx_locals.ga_len;
5240 instr_count = instr->ga_len;
5241 p = skipwhite(op + oplen);
5242 r = compile_expr0(&p, cctx);
5243 if (new_local)
5244 ++cctx->ctx_locals.ga_len;
5245 if (r == FAIL)
5246 goto theend;
5247 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005248 else if (semicolon && var_idx == var_count - 1)
5249 {
5250 // For "[var; var] = expr" get the rest of the list
5251 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5252 goto theend;
5253 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005254 else
5255 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005256 // For "[var, var] = expr" get the "var_idx" item from the
5257 // list.
5258 if (generate_GETITEM(cctx, var_idx) == FAIL)
5259 return FAIL;
5260 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005261
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005262 stacktype = stack->ga_len == 0 ? &t_void
5263 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5264 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005265 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005266 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005267 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005268 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005269 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005270 emsg(_(e_cannot_use_void));
5271 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005272 }
5273 else
5274 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005275 // An empty list or dict has a &t_void member,
5276 // for a variable that implies &t_any.
5277 if (stacktype == &t_list_empty)
5278 lvar->lv_type = &t_list_any;
5279 else if (stacktype == &t_dict_empty)
5280 lvar->lv_type = &t_dict_any;
5281 else
5282 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005283 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005284 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005285 else
5286 {
5287 type_T *use_type = lvar->lv_type;
5288
5289 if (has_index)
5290 {
5291 use_type = use_type->tt_member;
5292 if (use_type == NULL)
5293 use_type = &t_void;
5294 }
5295 if (need_type(stacktype, use_type, -1, cctx)
5296 == FAIL)
5297 goto theend;
5298 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005299 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005300 else if (*p != '=' && need_type(stacktype, member_type, -1,
5301 cctx) == FAIL)
5302 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005303 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005304 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005305 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005306 emsg(_(e_const_req_value));
5307 goto theend;
5308 }
5309 else if (!has_type || dest == dest_option)
5310 {
5311 emsg(_(e_type_req));
5312 goto theend;
5313 }
5314 else
5315 {
5316 // variables are always initialized
5317 if (ga_grow(instr, 1) == FAIL)
5318 goto theend;
5319 switch (member_type->tt_type)
5320 {
5321 case VAR_BOOL:
5322 generate_PUSHBOOL(cctx, VVAL_FALSE);
5323 break;
5324 case VAR_FLOAT:
5325#ifdef FEAT_FLOAT
5326 generate_PUSHF(cctx, 0.0);
5327#endif
5328 break;
5329 case VAR_STRING:
5330 generate_PUSHS(cctx, NULL);
5331 break;
5332 case VAR_BLOB:
5333 generate_PUSHBLOB(cctx, NULL);
5334 break;
5335 case VAR_FUNC:
5336 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5337 break;
5338 case VAR_LIST:
5339 generate_NEWLIST(cctx, 0);
5340 break;
5341 case VAR_DICT:
5342 generate_NEWDICT(cctx, 0);
5343 break;
5344 case VAR_JOB:
5345 generate_PUSHJOB(cctx, NULL);
5346 break;
5347 case VAR_CHANNEL:
5348 generate_PUSHCHANNEL(cctx, NULL);
5349 break;
5350 case VAR_NUMBER:
5351 case VAR_UNKNOWN:
5352 case VAR_ANY:
5353 case VAR_PARTIAL:
5354 case VAR_VOID:
5355 case VAR_SPECIAL: // cannot happen
5356 generate_PUSHNR(cctx, 0);
5357 break;
5358 }
5359 }
5360 if (var_count == 0)
5361 end = p;
5362 }
5363
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005364 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005365 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005366 break;
5367
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005368 if (oplen > 0 && *op != '=')
5369 {
5370 type_T *expected = &t_number;
5371 type_T *stacktype;
5372
5373 // TODO: if type is known use float or any operation
5374
5375 if (*op == '.')
5376 expected = &t_string;
5377 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5378 if (need_type(stacktype, expected, -1, cctx) == FAIL)
5379 goto theend;
5380
5381 if (*op == '.')
5382 generate_instr_drop(cctx, ISN_CONCAT, 1);
5383 else
5384 {
5385 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5386
5387 if (isn == NULL)
5388 goto theend;
5389 switch (*op)
5390 {
5391 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5392 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5393 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5394 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5395 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5396 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005397 }
5398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005399
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005400 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005401 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005402 int r;
5403
5404 // Compile the "idx" in "var[idx]".
5405 if (new_local)
5406 --cctx->ctx_locals.ga_len;
5407 p = skipwhite(var_start + varlen + 1);
5408 r = compile_expr0(&p, cctx);
5409 if (new_local)
5410 ++cctx->ctx_locals.ga_len;
5411 if (r == FAIL)
5412 goto theend;
5413 if (*skipwhite(p) != ']')
5414 {
5415 emsg(_(e_missbrac));
5416 goto theend;
5417 }
5418 if (type->tt_type == VAR_DICT
5419 && may_generate_2STRING(-1, cctx) == FAIL)
5420 goto theend;
5421 if (type->tt_type == VAR_LIST
5422 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005423 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005424 {
5425 emsg(_(e_number_exp));
5426 goto theend;
5427 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005428
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005429 // Load the dict or list. On the stack we then have:
5430 // - value
5431 // - index
5432 // - variable
5433 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005434
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005435 if (type->tt_type == VAR_LIST)
5436 {
5437 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5438 return FAIL;
5439 }
5440 else if (type->tt_type == VAR_DICT)
5441 {
5442 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5443 return FAIL;
5444 }
5445 else
5446 {
5447 emsg(_(e_listreq));
5448 goto theend;
5449 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005450 }
5451 else
5452 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005453 switch (dest)
5454 {
5455 case dest_option:
5456 generate_STOREOPT(cctx, name + 1, opt_flags);
5457 break;
5458 case dest_global:
5459 // include g: with the name, easier to execute that way
5460 generate_STORE(cctx, ISN_STOREG, 0, name);
5461 break;
5462 case dest_buffer:
5463 // include b: with the name, easier to execute that way
5464 generate_STORE(cctx, ISN_STOREB, 0, name);
5465 break;
5466 case dest_window:
5467 // include w: with the name, easier to execute that way
5468 generate_STORE(cctx, ISN_STOREW, 0, name);
5469 break;
5470 case dest_tab:
5471 // include t: with the name, easier to execute that way
5472 generate_STORE(cctx, ISN_STORET, 0, name);
5473 break;
5474 case dest_env:
5475 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5476 break;
5477 case dest_reg:
5478 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5479 break;
5480 case dest_vimvar:
5481 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5482 break;
5483 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005484 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005485 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5486 imported_T *import = NULL;
5487 int sid = current_sctx.sc_sid;
5488 int idx;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005489
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005490 if (name[1] != ':')
5491 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005492 import = find_imported(name, 0, cctx);
5493 if (import != NULL)
5494 sid = import->imp_sid;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005495 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005496
5497 idx = get_script_item_idx(sid, rawname, TRUE);
5498 // TODO: specific type
5499 if (idx < 0)
5500 {
5501 char_u *name_s = name;
5502
5503 // Include s: in the name for store_var()
5504 if (name[1] != ':')
5505 {
5506 int len = (int)STRLEN(name) + 3;
5507
5508 name_s = alloc(len);
5509 if (name_s == NULL)
5510 name_s = name;
5511 else
5512 vim_snprintf((char *)name_s, len,
5513 "s:%s", name);
5514 }
5515 generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005516 &t_any);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005517 if (name_s != name)
5518 vim_free(name_s);
5519 }
5520 else
5521 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005522 sid, idx, &t_any);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005523 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005524 break;
5525 case dest_local:
5526 if (lvar != NULL)
5527 {
5528 isn_T *isn = ((isn_T *)instr->ga_data)
5529 + instr->ga_len - 1;
5530
5531 // optimization: turn "var = 123" from ISN_PUSHNR +
5532 // ISN_STORE into ISN_STORENR
5533 if (!lvar->lv_from_outer
5534 && instr->ga_len == instr_count + 1
5535 && isn->isn_type == ISN_PUSHNR)
5536 {
5537 varnumber_T val = isn->isn_arg.number;
5538
5539 isn->isn_type = ISN_STORENR;
5540 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5541 isn->isn_arg.storenr.stnr_val = val;
5542 if (stack->ga_len > 0)
5543 --stack->ga_len;
5544 }
5545 else if (lvar->lv_from_outer)
5546 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005547 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005548 else
5549 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5550 }
5551 break;
5552 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005553 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005554
5555 if (var_idx + 1 < var_count)
5556 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005557 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005558
5559 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005560 if (var_count > 0 && !semicolon)
5561 {
5562 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5563 goto theend;
5564 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005565
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005566 ret = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005567
5568theend:
5569 vim_free(name);
5570 return ret;
5571}
5572
5573/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005574 * Check if "name" can be "unlet".
5575 */
5576 int
5577check_vim9_unlet(char_u *name)
5578{
5579 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5580 {
5581 semsg(_("E1081: Cannot unlet %s"), name);
5582 return FAIL;
5583 }
5584 return OK;
5585}
5586
5587/*
5588 * Callback passed to ex_unletlock().
5589 */
5590 static int
5591compile_unlet(
5592 lval_T *lvp,
5593 char_u *name_end,
5594 exarg_T *eap,
5595 int deep UNUSED,
5596 void *coookie)
5597{
5598 cctx_T *cctx = coookie;
5599
5600 if (lvp->ll_tv == NULL)
5601 {
5602 char_u *p = lvp->ll_name;
5603 int cc = *name_end;
5604 int ret = OK;
5605
5606 // Normal name. Only supports g:, w:, t: and b: namespaces.
5607 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005608 if (*p == '$')
5609 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5610 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005611 ret = FAIL;
5612 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005613 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005614
5615 *name_end = cc;
5616 return ret;
5617 }
5618
5619 // TODO: unlet {list}[idx]
5620 // TODO: unlet {dict}[key]
5621 emsg("Sorry, :unlet not fully implemented yet");
5622 return FAIL;
5623}
5624
5625/*
5626 * compile "unlet var", "lock var" and "unlock var"
5627 * "arg" points to "var".
5628 */
5629 static char_u *
5630compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5631{
5632 char_u *p = arg;
5633
5634 if (eap->cmdidx != CMD_unlet)
5635 {
5636 emsg("Sorry, :lock and unlock not implemented yet");
5637 return NULL;
5638 }
5639
5640 if (*p == '!')
5641 {
5642 p = skipwhite(p + 1);
5643 eap->forceit = TRUE;
5644 }
5645
5646 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5647 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5648}
5649
5650/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005651 * Compile an :import command.
5652 */
5653 static char_u *
5654compile_import(char_u *arg, cctx_T *cctx)
5655{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005656 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005657}
5658
5659/*
5660 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5661 */
5662 static int
5663compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5664{
5665 garray_T *instr = &cctx->ctx_instr;
5666 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5667
5668 if (endlabel == NULL)
5669 return FAIL;
5670 endlabel->el_next = *el;
5671 *el = endlabel;
5672 endlabel->el_end_label = instr->ga_len;
5673
5674 generate_JUMP(cctx, when, 0);
5675 return OK;
5676}
5677
5678 static void
5679compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5680{
5681 garray_T *instr = &cctx->ctx_instr;
5682
5683 while (*el != NULL)
5684 {
5685 endlabel_T *cur = (*el);
5686 isn_T *isn;
5687
5688 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5689 isn->isn_arg.jump.jump_where = instr->ga_len;
5690 *el = cur->el_next;
5691 vim_free(cur);
5692 }
5693}
5694
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005695 static void
5696compile_free_jump_to_end(endlabel_T **el)
5697{
5698 while (*el != NULL)
5699 {
5700 endlabel_T *cur = (*el);
5701
5702 *el = cur->el_next;
5703 vim_free(cur);
5704 }
5705}
5706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005707/*
5708 * Create a new scope and set up the generic items.
5709 */
5710 static scope_T *
5711new_scope(cctx_T *cctx, scopetype_T type)
5712{
5713 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5714
5715 if (scope == NULL)
5716 return NULL;
5717 scope->se_outer = cctx->ctx_scope;
5718 cctx->ctx_scope = scope;
5719 scope->se_type = type;
5720 scope->se_local_count = cctx->ctx_locals.ga_len;
5721 return scope;
5722}
5723
5724/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005725 * Free the current scope and go back to the outer scope.
5726 */
5727 static void
5728drop_scope(cctx_T *cctx)
5729{
5730 scope_T *scope = cctx->ctx_scope;
5731
5732 if (scope == NULL)
5733 {
5734 iemsg("calling drop_scope() without a scope");
5735 return;
5736 }
5737 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005738 switch (scope->se_type)
5739 {
5740 case IF_SCOPE:
5741 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5742 case FOR_SCOPE:
5743 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5744 case WHILE_SCOPE:
5745 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5746 case TRY_SCOPE:
5747 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5748 case NO_SCOPE:
5749 case BLOCK_SCOPE:
5750 break;
5751 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005752 vim_free(scope);
5753}
5754
5755/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005756 * compile "if expr"
5757 *
5758 * "if expr" Produces instructions:
5759 * EVAL expr Push result of "expr"
5760 * JUMP_IF_FALSE end
5761 * ... body ...
5762 * end:
5763 *
5764 * "if expr | else" Produces instructions:
5765 * EVAL expr Push result of "expr"
5766 * JUMP_IF_FALSE else
5767 * ... body ...
5768 * JUMP_ALWAYS end
5769 * else:
5770 * ... body ...
5771 * end:
5772 *
5773 * "if expr1 | elseif expr2 | else" Produces instructions:
5774 * EVAL expr Push result of "expr"
5775 * JUMP_IF_FALSE elseif
5776 * ... body ...
5777 * JUMP_ALWAYS end
5778 * elseif:
5779 * EVAL expr Push result of "expr"
5780 * JUMP_IF_FALSE else
5781 * ... body ...
5782 * JUMP_ALWAYS end
5783 * else:
5784 * ... body ...
5785 * end:
5786 */
5787 static char_u *
5788compile_if(char_u *arg, cctx_T *cctx)
5789{
5790 char_u *p = arg;
5791 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005792 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005793 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005794 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005795 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005796
Bram Moolenaara5565e42020-05-09 15:44:01 +02005797 CLEAR_FIELD(ppconst);
5798 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005799 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005800 clear_ppconst(&ppconst);
5801 return NULL;
5802 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005803 if (cctx->ctx_skip == SKIP_YES)
5804 clear_ppconst(&ppconst);
5805 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005806 {
5807 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005808 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005809 clear_ppconst(&ppconst);
5810 }
5811 else
5812 {
5813 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005814 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005815 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005816 return NULL;
5817 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005818
5819 scope = new_scope(cctx, IF_SCOPE);
5820 if (scope == NULL)
5821 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005822 scope->se_skip_save = skip_save;
5823 // "is_had_return" will be reset if any block does not end in :return
5824 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005825
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005826 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005827 {
5828 // "where" is set when ":elseif", "else" or ":endif" is found
5829 scope->se_u.se_if.is_if_label = instr->ga_len;
5830 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5831 }
5832 else
5833 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005834
5835 return p;
5836}
5837
5838 static char_u *
5839compile_elseif(char_u *arg, cctx_T *cctx)
5840{
5841 char_u *p = arg;
5842 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005843 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005844 isn_T *isn;
5845 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005846 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005847
5848 if (scope == NULL || scope->se_type != IF_SCOPE)
5849 {
5850 emsg(_(e_elseif_without_if));
5851 return NULL;
5852 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005853 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005854 if (!cctx->ctx_had_return)
5855 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005856
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005857 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005858 {
5859 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005860 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005861 return NULL;
5862 // previous "if" or "elseif" jumps here
5863 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5864 isn->isn_arg.jump.jump_where = instr->ga_len;
5865 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005866
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005867 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005868 CLEAR_FIELD(ppconst);
5869 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005870 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005871 clear_ppconst(&ppconst);
5872 return NULL;
5873 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005874 if (scope->se_skip_save == SKIP_YES)
5875 clear_ppconst(&ppconst);
5876 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005877 {
5878 // The expression results in a constant.
5879 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005880 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005881 clear_ppconst(&ppconst);
5882 scope->se_u.se_if.is_if_label = -1;
5883 }
5884 else
5885 {
5886 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005887 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005888 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005889 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005890
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005891 // "where" is set when ":elseif", "else" or ":endif" is found
5892 scope->se_u.se_if.is_if_label = instr->ga_len;
5893 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895
5896 return p;
5897}
5898
5899 static char_u *
5900compile_else(char_u *arg, cctx_T *cctx)
5901{
5902 char_u *p = arg;
5903 garray_T *instr = &cctx->ctx_instr;
5904 isn_T *isn;
5905 scope_T *scope = cctx->ctx_scope;
5906
5907 if (scope == NULL || scope->se_type != IF_SCOPE)
5908 {
5909 emsg(_(e_else_without_if));
5910 return NULL;
5911 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005912 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005913 if (!cctx->ctx_had_return)
5914 scope->se_u.se_if.is_had_return = FALSE;
5915 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005916
Bram Moolenaarefd88552020-06-18 20:50:10 +02005917 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005918 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005919 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005920 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005921 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005922 if (!cctx->ctx_had_return
5923 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5924 JUMP_ALWAYS, cctx) == FAIL)
5925 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005926 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005927
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005928 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005929 {
5930 if (scope->se_u.se_if.is_if_label >= 0)
5931 {
5932 // previous "if" or "elseif" jumps here
5933 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5934 isn->isn_arg.jump.jump_where = instr->ga_len;
5935 scope->se_u.se_if.is_if_label = -1;
5936 }
5937 }
5938
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005939 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005940 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5941 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005942
5943 return p;
5944}
5945
5946 static char_u *
5947compile_endif(char_u *arg, cctx_T *cctx)
5948{
5949 scope_T *scope = cctx->ctx_scope;
5950 ifscope_T *ifscope;
5951 garray_T *instr = &cctx->ctx_instr;
5952 isn_T *isn;
5953
5954 if (scope == NULL || scope->se_type != IF_SCOPE)
5955 {
5956 emsg(_(e_endif_without_if));
5957 return NULL;
5958 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005959 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005960 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005961 if (!cctx->ctx_had_return)
5962 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005963
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005964 if (scope->se_u.se_if.is_if_label >= 0)
5965 {
5966 // previous "if" or "elseif" jumps here
5967 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5968 isn->isn_arg.jump.jump_where = instr->ga_len;
5969 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005970 // Fill in the "end" label in jumps at the end of the blocks.
5971 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005972 cctx->ctx_skip = scope->se_skip_save;
5973
5974 // If all the blocks end in :return and there is an :else then the
5975 // had_return flag is set.
5976 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005978 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005979 return arg;
5980}
5981
5982/*
5983 * compile "for var in expr"
5984 *
5985 * Produces instructions:
5986 * PUSHNR -1
5987 * STORE loop-idx Set index to -1
5988 * EVAL expr Push result of "expr"
5989 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5990 * - if beyond end, jump to "end"
5991 * - otherwise get item from list and push it
5992 * STORE var Store item in "var"
5993 * ... body ...
5994 * JUMP top Jump back to repeat
5995 * end: DROP Drop the result of "expr"
5996 *
5997 */
5998 static char_u *
5999compile_for(char_u *arg, cctx_T *cctx)
6000{
6001 char_u *p;
6002 size_t varlen;
6003 garray_T *instr = &cctx->ctx_instr;
6004 garray_T *stack = &cctx->ctx_type_stack;
6005 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006006 lvar_T *loop_lvar; // loop iteration variable
6007 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006008 type_T *vartype;
6009
6010 // TODO: list of variables: "for [key, value] in dict"
6011 // parse "var"
6012 for (p = arg; eval_isnamec1(*p); ++p)
6013 ;
6014 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006015 var_lvar = lookup_local(arg, varlen, cctx);
6016 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006017 {
6018 semsg(_("E1023: variable already defined: %s"), arg);
6019 return NULL;
6020 }
6021
6022 // consume "in"
6023 p = skipwhite(p);
6024 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6025 {
6026 emsg(_(e_missing_in));
6027 return NULL;
6028 }
6029 p = skipwhite(p + 2);
6030
6031
6032 scope = new_scope(cctx, FOR_SCOPE);
6033 if (scope == NULL)
6034 return NULL;
6035
6036 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006037 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6038 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006039 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006040 // out of memory
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
6045 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006046 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6047 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006048 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006049 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006050 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006051 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006052 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006053
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006054 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006055
6056 // compile "expr", it remains on the stack until "endfor"
6057 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006058 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006059 {
6060 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006062 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006063
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006064 // Now that we know the type of "var", check that it is a list, now or at
6065 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006067 if (need_type(vartype, &t_list_any, -1, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006068 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006069 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006070 return NULL;
6071 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006072 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006073 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006074
6075 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006076 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006077
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006078 generate_FOR(cctx, loop_lvar->lv_idx);
6079 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006080
6081 return arg;
6082}
6083
6084/*
6085 * compile "endfor"
6086 */
6087 static char_u *
6088compile_endfor(char_u *arg, cctx_T *cctx)
6089{
6090 garray_T *instr = &cctx->ctx_instr;
6091 scope_T *scope = cctx->ctx_scope;
6092 forscope_T *forscope;
6093 isn_T *isn;
6094
6095 if (scope == NULL || scope->se_type != FOR_SCOPE)
6096 {
6097 emsg(_(e_for));
6098 return NULL;
6099 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006100 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006101 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006102 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006103
6104 // At end of ":for" scope jump back to the FOR instruction.
6105 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6106
6107 // Fill in the "end" label in the FOR statement so it can jump here
6108 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6109 isn->isn_arg.forloop.for_end = instr->ga_len;
6110
6111 // Fill in the "end" label any BREAK statements
6112 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6113
6114 // Below the ":for" scope drop the "expr" list from the stack.
6115 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6116 return NULL;
6117
6118 vim_free(scope);
6119
6120 return arg;
6121}
6122
6123/*
6124 * compile "while expr"
6125 *
6126 * Produces instructions:
6127 * top: EVAL expr Push result of "expr"
6128 * JUMP_IF_FALSE end jump if false
6129 * ... body ...
6130 * JUMP top Jump back to repeat
6131 * end:
6132 *
6133 */
6134 static char_u *
6135compile_while(char_u *arg, cctx_T *cctx)
6136{
6137 char_u *p = arg;
6138 garray_T *instr = &cctx->ctx_instr;
6139 scope_T *scope;
6140
6141 scope = new_scope(cctx, WHILE_SCOPE);
6142 if (scope == NULL)
6143 return NULL;
6144
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006145 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006146
6147 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006148 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006149 return NULL;
6150
6151 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006152 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006153 JUMP_IF_FALSE, cctx) == FAIL)
6154 return FAIL;
6155
6156 return p;
6157}
6158
6159/*
6160 * compile "endwhile"
6161 */
6162 static char_u *
6163compile_endwhile(char_u *arg, cctx_T *cctx)
6164{
6165 scope_T *scope = cctx->ctx_scope;
6166
6167 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6168 {
6169 emsg(_(e_while));
6170 return NULL;
6171 }
6172 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006173 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006174
6175 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006176 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006177
6178 // Fill in the "end" label in the WHILE statement so it can jump here.
6179 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006180 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006181
6182 vim_free(scope);
6183
6184 return arg;
6185}
6186
6187/*
6188 * compile "continue"
6189 */
6190 static char_u *
6191compile_continue(char_u *arg, cctx_T *cctx)
6192{
6193 scope_T *scope = cctx->ctx_scope;
6194
6195 for (;;)
6196 {
6197 if (scope == NULL)
6198 {
6199 emsg(_(e_continue));
6200 return NULL;
6201 }
6202 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6203 break;
6204 scope = scope->se_outer;
6205 }
6206
6207 // Jump back to the FOR or WHILE instruction.
6208 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006209 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6210 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006211 return arg;
6212}
6213
6214/*
6215 * compile "break"
6216 */
6217 static char_u *
6218compile_break(char_u *arg, cctx_T *cctx)
6219{
6220 scope_T *scope = cctx->ctx_scope;
6221 endlabel_T **el;
6222
6223 for (;;)
6224 {
6225 if (scope == NULL)
6226 {
6227 emsg(_(e_break));
6228 return NULL;
6229 }
6230 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6231 break;
6232 scope = scope->se_outer;
6233 }
6234
6235 // Jump to the end of the FOR or WHILE loop.
6236 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006237 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006238 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006239 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006240 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6241 return FAIL;
6242
6243 return arg;
6244}
6245
6246/*
6247 * compile "{" start of block
6248 */
6249 static char_u *
6250compile_block(char_u *arg, cctx_T *cctx)
6251{
6252 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6253 return NULL;
6254 return skipwhite(arg + 1);
6255}
6256
6257/*
6258 * compile end of block: drop one scope
6259 */
6260 static void
6261compile_endblock(cctx_T *cctx)
6262{
6263 scope_T *scope = cctx->ctx_scope;
6264
6265 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006266 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267 vim_free(scope);
6268}
6269
6270/*
6271 * compile "try"
6272 * Creates a new scope for the try-endtry, pointing to the first catch and
6273 * finally.
6274 * Creates another scope for the "try" block itself.
6275 * TRY instruction sets up exception handling at runtime.
6276 *
6277 * "try"
6278 * TRY -> catch1, -> finally push trystack entry
6279 * ... try block
6280 * "throw {exception}"
6281 * EVAL {exception}
6282 * THROW create exception
6283 * ... try block
6284 * " catch {expr}"
6285 * JUMP -> finally
6286 * catch1: PUSH exeception
6287 * EVAL {expr}
6288 * MATCH
6289 * JUMP nomatch -> catch2
6290 * CATCH remove exception
6291 * ... catch block
6292 * " catch"
6293 * JUMP -> finally
6294 * catch2: CATCH remove exception
6295 * ... catch block
6296 * " finally"
6297 * finally:
6298 * ... finally block
6299 * " endtry"
6300 * ENDTRY pop trystack entry, may rethrow
6301 */
6302 static char_u *
6303compile_try(char_u *arg, cctx_T *cctx)
6304{
6305 garray_T *instr = &cctx->ctx_instr;
6306 scope_T *try_scope;
6307 scope_T *scope;
6308
6309 // scope that holds the jumps that go to catch/finally/endtry
6310 try_scope = new_scope(cctx, TRY_SCOPE);
6311 if (try_scope == NULL)
6312 return NULL;
6313
6314 // "catch" is set when the first ":catch" is found.
6315 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006316 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006317 if (generate_instr(cctx, ISN_TRY) == NULL)
6318 return NULL;
6319
6320 // scope for the try block itself
6321 scope = new_scope(cctx, BLOCK_SCOPE);
6322 if (scope == NULL)
6323 return NULL;
6324
6325 return arg;
6326}
6327
6328/*
6329 * compile "catch {expr}"
6330 */
6331 static char_u *
6332compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6333{
6334 scope_T *scope = cctx->ctx_scope;
6335 garray_T *instr = &cctx->ctx_instr;
6336 char_u *p;
6337 isn_T *isn;
6338
6339 // end block scope from :try or :catch
6340 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6341 compile_endblock(cctx);
6342 scope = cctx->ctx_scope;
6343
6344 // Error if not in a :try scope
6345 if (scope == NULL || scope->se_type != TRY_SCOPE)
6346 {
6347 emsg(_(e_catch));
6348 return NULL;
6349 }
6350
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006351 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006352 {
6353 emsg(_("E1033: catch unreachable after catch-all"));
6354 return NULL;
6355 }
6356
6357 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006358 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006359 JUMP_ALWAYS, cctx) == FAIL)
6360 return NULL;
6361
6362 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006363 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006364 if (isn->isn_arg.try.try_catch == 0)
6365 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006366 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006367 {
6368 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006369 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006370 isn->isn_arg.jump.jump_where = instr->ga_len;
6371 }
6372
6373 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006374 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006375 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006376 scope->se_u.se_try.ts_caught_all = TRUE;
6377 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006378 }
6379 else
6380 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006381 char_u *end;
6382 char_u *pat;
6383 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006384 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006385 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006387 // Push v:exception, push {expr} and MATCH
6388 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6389
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006390 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006391 if (*end != *p)
6392 {
6393 semsg(_("E1067: Separator mismatch: %s"), p);
6394 vim_free(tofree);
6395 return FAIL;
6396 }
6397 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006398 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006399 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006400 len = (int)(end - tofree);
6401 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006402 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006403 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006404 if (pat == NULL)
6405 return FAIL;
6406 if (generate_PUSHS(cctx, pat) == FAIL)
6407 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006409 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6410 return NULL;
6411
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006412 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006413 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6414 return NULL;
6415 }
6416
6417 if (generate_instr(cctx, ISN_CATCH) == NULL)
6418 return NULL;
6419
6420 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6421 return NULL;
6422 return p;
6423}
6424
6425 static char_u *
6426compile_finally(char_u *arg, cctx_T *cctx)
6427{
6428 scope_T *scope = cctx->ctx_scope;
6429 garray_T *instr = &cctx->ctx_instr;
6430 isn_T *isn;
6431
6432 // end block scope from :try or :catch
6433 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6434 compile_endblock(cctx);
6435 scope = cctx->ctx_scope;
6436
6437 // Error if not in a :try scope
6438 if (scope == NULL || scope->se_type != TRY_SCOPE)
6439 {
6440 emsg(_(e_finally));
6441 return NULL;
6442 }
6443
6444 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006445 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006446 if (isn->isn_arg.try.try_finally != 0)
6447 {
6448 emsg(_(e_finally_dup));
6449 return NULL;
6450 }
6451
6452 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006453 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006454
Bram Moolenaar585fea72020-04-02 22:33:21 +02006455 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006456 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006457 {
6458 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006459 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006460 isn->isn_arg.jump.jump_where = instr->ga_len;
6461 }
6462
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006463 // TODO: set index in ts_finally_label jumps
6464
6465 return arg;
6466}
6467
6468 static char_u *
6469compile_endtry(char_u *arg, cctx_T *cctx)
6470{
6471 scope_T *scope = cctx->ctx_scope;
6472 garray_T *instr = &cctx->ctx_instr;
6473 isn_T *isn;
6474
6475 // end block scope from :catch or :finally
6476 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6477 compile_endblock(cctx);
6478 scope = cctx->ctx_scope;
6479
6480 // Error if not in a :try scope
6481 if (scope == NULL || scope->se_type != TRY_SCOPE)
6482 {
6483 if (scope == NULL)
6484 emsg(_(e_no_endtry));
6485 else if (scope->se_type == WHILE_SCOPE)
6486 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006487 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006488 emsg(_(e_endfor));
6489 else
6490 emsg(_(e_endif));
6491 return NULL;
6492 }
6493
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006494 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006495 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6496 {
6497 emsg(_("E1032: missing :catch or :finally"));
6498 return NULL;
6499 }
6500
6501 // Fill in the "end" label in jumps at the end of the blocks, if not done
6502 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006503 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006504
6505 // End :catch or :finally scope: set value in ISN_TRY instruction
6506 if (isn->isn_arg.try.try_finally == 0)
6507 isn->isn_arg.try.try_finally = instr->ga_len;
6508 compile_endblock(cctx);
6509
6510 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6511 return NULL;
6512 return arg;
6513}
6514
6515/*
6516 * compile "throw {expr}"
6517 */
6518 static char_u *
6519compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6520{
6521 char_u *p = skipwhite(arg);
6522
Bram Moolenaara5565e42020-05-09 15:44:01 +02006523 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006524 return NULL;
6525 if (may_generate_2STRING(-1, cctx) == FAIL)
6526 return NULL;
6527 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6528 return NULL;
6529
6530 return p;
6531}
6532
6533/*
6534 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006535 * compile "echomsg expr"
6536 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006537 * compile "execute expr"
6538 */
6539 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006540compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006541{
6542 char_u *p = arg;
6543 int count = 0;
6544
6545 for (;;)
6546 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006547 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006548 return NULL;
6549 ++count;
6550 p = skipwhite(p);
6551 if (ends_excmd(*p))
6552 break;
6553 }
6554
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006555 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6556 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6557 else if (cmdidx == CMD_execute)
6558 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6559 else if (cmdidx == CMD_echomsg)
6560 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6561 else
6562 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006563 return p;
6564}
6565
6566/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006567 * A command that is not compiled, execute with legacy code.
6568 */
6569 static char_u *
6570compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6571{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006572 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006573 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006574 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006575
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006576 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006577 goto theend;
6578
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006579 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006580 {
6581 long argt = excmd_get_argt(eap->cmdidx);
6582 int usefilter = FALSE;
6583
6584 has_expr = argt & (EX_XFILE | EX_EXPAND);
6585
6586 // If the command can be followed by a bar, find the bar and truncate
6587 // it, so that the following command can be compiled.
6588 // The '|' is overwritten with a NUL, it is put back below.
6589 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6590 && *eap->arg == '!')
6591 // :w !filter or :r !filter or :r! filter
6592 usefilter = TRUE;
6593 if ((argt & EX_TRLBAR) && !usefilter)
6594 {
6595 separate_nextcmd(eap);
6596 if (eap->nextcmd != NULL)
6597 nextcmd = eap->nextcmd;
6598 }
6599 }
6600
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006601 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6602 {
6603 // expand filename in "syntax include [@group] filename"
6604 has_expr = TRUE;
6605 eap->arg = skipwhite(eap->arg + 7);
6606 if (*eap->arg == '@')
6607 eap->arg = skiptowhite(eap->arg);
6608 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006609
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006610 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006611 {
6612 int count = 0;
6613 char_u *start = skipwhite(line);
6614
6615 // :cmd xxx`=expr1`yyy`=expr2`zzz
6616 // PUSHS ":cmd xxx"
6617 // eval expr1
6618 // PUSHS "yyy"
6619 // eval expr2
6620 // PUSHS "zzz"
6621 // EXECCONCAT 5
6622 for (;;)
6623 {
6624 if (p > start)
6625 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006626 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006627 ++count;
6628 }
6629 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006630 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006631 return NULL;
6632 may_generate_2STRING(-1, cctx);
6633 ++count;
6634 p = skipwhite(p);
6635 if (*p != '`')
6636 {
6637 emsg(_("E1083: missing backtick"));
6638 return NULL;
6639 }
6640 start = p + 1;
6641
6642 p = (char_u *)strstr((char *)start, "`=");
6643 if (p == NULL)
6644 {
6645 if (*skipwhite(start) != NUL)
6646 {
6647 generate_PUSHS(cctx, vim_strsave(start));
6648 ++count;
6649 }
6650 break;
6651 }
6652 }
6653 generate_EXECCONCAT(cctx, count);
6654 }
6655 else
6656 generate_EXEC(cctx, line);
6657
6658theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006659 if (*nextcmd != NUL)
6660 {
6661 // the parser expects a pointer to the bar, put it back
6662 --nextcmd;
6663 *nextcmd = '|';
6664 }
6665
6666 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006667}
6668
6669/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006670 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006671 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006672 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006673 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006674add_def_function(ufunc_T *ufunc)
6675{
6676 dfunc_T *dfunc;
6677
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006678 if (def_functions.ga_len == 0)
6679 {
6680 // The first position is not used, so that a zero uf_dfunc_idx means it
6681 // wasn't set.
6682 if (ga_grow(&def_functions, 1) == FAIL)
6683 return FAIL;
6684 ++def_functions.ga_len;
6685 }
6686
Bram Moolenaar09689a02020-05-09 22:50:08 +02006687 // Add the function to "def_functions".
6688 if (ga_grow(&def_functions, 1) == FAIL)
6689 return FAIL;
6690 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6691 CLEAR_POINTER(dfunc);
6692 dfunc->df_idx = def_functions.ga_len;
6693 ufunc->uf_dfunc_idx = dfunc->df_idx;
6694 dfunc->df_ufunc = ufunc;
6695 ++def_functions.ga_len;
6696 return OK;
6697}
6698
6699/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700 * After ex_function() has collected all the function lines: parse and compile
6701 * the lines into instructions.
6702 * Adds the function to "def_functions".
6703 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6704 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006705 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006706 * This can be used recursively through compile_lambda(), which may reallocate
6707 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006708 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006709 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006710 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006711compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006712{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713 char_u *line = NULL;
6714 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006715 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006716 cctx_T cctx;
6717 garray_T *instr;
6718 int called_emsg_before = called_emsg;
6719 int ret = FAIL;
6720 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006721 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006722 int emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006724 // When using a function that was compiled before: Free old instructions.
6725 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006726 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006727 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006728 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6729 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006730 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006731 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02006732 else if (add_def_function(ufunc) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006733 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734
Bram Moolenaara80faa82020-04-12 19:37:17 +02006735 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006736 cctx.ctx_ufunc = ufunc;
6737 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006738 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006739 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6740 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6741 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6742 cctx.ctx_type_list = &ufunc->uf_type_list;
6743 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6744 instr = &cctx.ctx_instr;
6745
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006746 // Set the context to the function, it may be compiled when called from
6747 // another script. Set the script version to the most modern one.
6748 // The line number will be set in next_line_from_context().
6749 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006750 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6751
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006752 // Make sure error messages are OK.
6753 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6754 if (do_estack_push)
6755 estack_push_ufunc(ufunc, 1);
6756
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006757 if (ufunc->uf_def_args.ga_len > 0)
6758 {
6759 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006760 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006761 int i;
6762 char_u *arg;
6763 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
6764
6765 // Produce instructions for the default values of optional arguments.
6766 // Store the instruction index in uf_def_arg_idx[] so that we know
6767 // where to start when the function is called, depending on the number
6768 // of arguments.
6769 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6770 if (ufunc->uf_def_arg_idx == NULL)
6771 goto erret;
6772 for (i = 0; i < count; ++i)
6773 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006774 garray_T *stack = &cctx.ctx_type_stack;
6775 type_T *val_type;
6776 int arg_idx = first_def_arg + i;
6777
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006778 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6779 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006780 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006781 goto erret;
6782
6783 // If no type specified use the type of the default value.
6784 // Otherwise check that the default value type matches the
6785 // specified type.
6786 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6787 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
6788 ufunc->uf_arg_types[arg_idx] = val_type;
6789 else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE)
6790 == FAIL)
6791 {
6792 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6793 arg_idx + 1);
6794 goto erret;
6795 }
6796
6797 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006798 goto erret;
6799 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006800 ufunc->uf_def_arg_idx[count] = instr->ga_len;
6801 }
6802
6803 /*
6804 * Loop over all the lines of the function and generate instructions.
6805 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006806 for (;;)
6807 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006808 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006809 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006810 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006811 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006812
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006813 // Bail out on the first error to avoid a flood of errors and report
6814 // the right line number when inside try/catch.
6815 if (emsg_before != called_emsg)
6816 goto erret;
6817
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006818 if (line != NULL && *line == '|')
6819 // the line continues after a '|'
6820 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006821 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006822 && !(*line == '#' && (line == cctx.ctx_line_start
6823 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006824 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006825 semsg(_("E488: Trailing characters: %s"), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006826 goto erret;
6827 }
6828 else
6829 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006830 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006831 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006832 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006833 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006834 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006835 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006836
Bram Moolenaara80faa82020-04-12 19:37:17 +02006837 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006838 ea.cmdlinep = &line;
6839 ea.cmd = skipwhite(line);
6840
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006841 // Some things can be recognized by the first character.
6842 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006843 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006844 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006845 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006846 if (ea.cmd[1] != '{')
6847 {
6848 line = (char_u *)"";
6849 continue;
6850 }
6851 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006852
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006853 case '}':
6854 {
6855 // "}" ends a block scope
6856 scopetype_T stype = cctx.ctx_scope == NULL
6857 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006858
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006859 if (stype == BLOCK_SCOPE)
6860 {
6861 compile_endblock(&cctx);
6862 line = ea.cmd;
6863 }
6864 else
6865 {
6866 emsg(_("E1025: using } outside of a block scope"));
6867 goto erret;
6868 }
6869 if (line != NULL)
6870 line = skipwhite(ea.cmd + 1);
6871 continue;
6872 }
6873
6874 case '{':
6875 // "{" starts a block scope
6876 // "{'a': 1}->func() is something else
6877 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6878 {
6879 line = compile_block(ea.cmd, &cctx);
6880 continue;
6881 }
6882 break;
6883
6884 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006885 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006886 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006887 }
6888
6889 /*
6890 * COMMAND MODIFIERS
6891 */
6892 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6893 {
6894 if (errormsg != NULL)
6895 goto erret;
6896 // empty line or comment
6897 line = (char_u *)"";
6898 continue;
6899 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006900 // TODO: use modifiers in the command
6901 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006902
6903 // Skip ":call" to get to the function name.
6904 if (checkforcmd(&ea.cmd, "call", 3))
6905 ea.cmd = skipwhite(ea.cmd);
6906
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006907 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006908 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006909 char_u *pskip;
6910
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006911 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006912 // find what follows.
6913 // Skip over "var.member", "var[idx]" and the like.
6914 // Also "&opt = val", "$ENV = val" and "@r = val".
6915 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006916 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006917 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006918 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006919 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006920 char_u *var_end;
6921 int oplen;
6922 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006923
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006924 var_end = find_name_end(pskip, NULL, NULL,
6925 FNE_CHECK_START | FNE_INCL_BR);
6926 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006927 if (oplen > 0)
6928 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006929 size_t len = p - ea.cmd;
6930
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006931 // Recognize an assignment if we recognize the variable
6932 // name:
6933 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006934 // "local = expr" where "local" is a local var.
6935 // "script = expr" where "script" is a script-local var.
6936 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006937 // "&opt = expr"
6938 // "$ENV = expr"
6939 // "@r = expr"
6940 if (*ea.cmd == '&'
6941 || *ea.cmd == '$'
6942 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006943 || ((len) > 2 && ea.cmd[1] == ':')
6944 || lookup_local(ea.cmd, len, &cctx) != NULL
6945 || lookup_arg(ea.cmd, len, NULL, NULL,
6946 NULL, &cctx) == OK
6947 || lookup_script(ea.cmd, len) == OK
6948 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006949 {
6950 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006951 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006952 goto erret;
6953 continue;
6954 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006955 }
6956 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006957
6958 if (*ea.cmd == '[')
6959 {
6960 // [var, var] = expr
6961 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6962 if (line == NULL)
6963 goto erret;
6964 if (line != ea.cmd)
6965 continue;
6966 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006967 }
6968
6969 /*
6970 * COMMAND after range
6971 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006972 cmd = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006974 if (ea.cmd > cmd && !starts_with_colon)
6975 {
6976 emsg(_(e_colon_required));
6977 goto erret;
6978 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006979 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006980 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006981 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006982
6983 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6984 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006985 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006986 {
6987 line += STRLEN(line);
6988 continue;
6989 }
6990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006991 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006992 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006993 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006994 // CMD_let cannot happen, compile_assignment() above is used
6995 iemsg("Command from find_ex_command() not handled");
6996 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006998 }
6999
7000 p = skipwhite(p);
7001
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007002 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007003 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007004 && ea.cmdidx != CMD_elseif
7005 && ea.cmdidx != CMD_else
7006 && ea.cmdidx != CMD_endif)
7007 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007008 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007009 continue;
7010 }
7011
Bram Moolenaarefd88552020-06-18 20:50:10 +02007012 if (ea.cmdidx != CMD_elseif
7013 && ea.cmdidx != CMD_else
7014 && ea.cmdidx != CMD_endif
7015 && ea.cmdidx != CMD_endfor
7016 && ea.cmdidx != CMD_endwhile
7017 && ea.cmdidx != CMD_catch
7018 && ea.cmdidx != CMD_finally
7019 && ea.cmdidx != CMD_endtry)
7020 {
7021 if (cctx.ctx_had_return)
7022 {
7023 emsg(_("E1095: Unreachable code after :return"));
7024 goto erret;
7025 }
7026 }
7027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007028 switch (ea.cmdidx)
7029 {
7030 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007031 ea.arg = p;
7032 line = compile_nested_function(&ea, &cctx);
7033 break;
7034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007035 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007036 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007037 goto erret;
7038
7039 case CMD_return:
7040 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007041 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007042 break;
7043
7044 case CMD_let:
7045 case CMD_const:
7046 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007047 if (line == p)
7048 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007049 break;
7050
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007051 case CMD_unlet:
7052 case CMD_unlockvar:
7053 case CMD_lockvar:
7054 line = compile_unletlock(p, &ea, &cctx);
7055 break;
7056
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057 case CMD_import:
7058 line = compile_import(p, &cctx);
7059 break;
7060
7061 case CMD_if:
7062 line = compile_if(p, &cctx);
7063 break;
7064 case CMD_elseif:
7065 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007066 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007067 break;
7068 case CMD_else:
7069 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007070 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071 break;
7072 case CMD_endif:
7073 line = compile_endif(p, &cctx);
7074 break;
7075
7076 case CMD_while:
7077 line = compile_while(p, &cctx);
7078 break;
7079 case CMD_endwhile:
7080 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007081 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007082 break;
7083
7084 case CMD_for:
7085 line = compile_for(p, &cctx);
7086 break;
7087 case CMD_endfor:
7088 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007089 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007090 break;
7091 case CMD_continue:
7092 line = compile_continue(p, &cctx);
7093 break;
7094 case CMD_break:
7095 line = compile_break(p, &cctx);
7096 break;
7097
7098 case CMD_try:
7099 line = compile_try(p, &cctx);
7100 break;
7101 case CMD_catch:
7102 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007103 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007104 break;
7105 case CMD_finally:
7106 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007107 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007108 break;
7109 case CMD_endtry:
7110 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007111 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007112 break;
7113 case CMD_throw:
7114 line = compile_throw(p, &cctx);
7115 break;
7116
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007117 case CMD_eval:
7118 if (compile_expr0(&p, &cctx) == FAIL)
7119 goto erret;
7120
7121 // drop the return value
7122 generate_instr_drop(&cctx, ISN_DROP, 1);
7123
7124 line = skipwhite(p);
7125 break;
7126
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007127 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007128 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007129 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007130 case CMD_echomsg:
7131 case CMD_echoerr:
7132 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007133 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007134
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007135 // TODO: other commands with an expression argument
7136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007137 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007138 // Not recognized, execute with do_cmdline_cmd().
7139 ea.arg = p;
7140 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007141 break;
7142 }
7143 if (line == NULL)
7144 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007145 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007146
7147 if (cctx.ctx_type_stack.ga_len < 0)
7148 {
7149 iemsg("Type stack underflow");
7150 goto erret;
7151 }
7152 }
7153
7154 if (cctx.ctx_scope != NULL)
7155 {
7156 if (cctx.ctx_scope->se_type == IF_SCOPE)
7157 emsg(_(e_endif));
7158 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7159 emsg(_(e_endwhile));
7160 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7161 emsg(_(e_endfor));
7162 else
7163 emsg(_("E1026: Missing }"));
7164 goto erret;
7165 }
7166
Bram Moolenaarefd88552020-06-18 20:50:10 +02007167 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168 {
7169 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7170 {
7171 emsg(_("E1027: Missing return statement"));
7172 goto erret;
7173 }
7174
7175 // Return zero if there is no return at the end.
7176 generate_PUSHNR(&cctx, 0);
7177 generate_instr(&cctx, ISN_RETURN);
7178 }
7179
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007180 {
7181 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7182 + ufunc->uf_dfunc_idx;
7183 dfunc->df_deleted = FALSE;
7184 dfunc->df_instr = instr->ga_data;
7185 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007186 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007187 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007188 if (cctx.ctx_outer_used)
7189 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007190 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007191 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007192
7193 ret = OK;
7194
7195erret:
7196 if (ret == FAIL)
7197 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007198 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007199 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7200 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007201
7202 for (idx = 0; idx < instr->ga_len; ++idx)
7203 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007204 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007205
Bram Moolenaar45a15082020-05-25 00:28:33 +02007206 // if using the last entry in the table we might as well remove it
7207 if (!dfunc->df_deleted
7208 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007209 --def_functions.ga_len;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007210 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007211
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007212 while (cctx.ctx_scope != NULL)
7213 drop_scope(&cctx);
7214
Bram Moolenaar20431c92020-03-20 18:39:46 +01007215 // Don't execute this function body.
7216 ga_clear_strings(&ufunc->uf_lines);
7217
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007218 if (errormsg != NULL)
7219 emsg(errormsg);
7220 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007221 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007222 }
7223
7224 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007225 if (do_estack_push)
7226 estack_pop();
7227
Bram Moolenaar20431c92020-03-20 18:39:46 +01007228 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007229 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007230 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007231 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232}
7233
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007234 void
7235set_function_type(ufunc_T *ufunc)
7236{
7237 int varargs = ufunc->uf_va_name != NULL;
7238 int argcount = ufunc->uf_args.ga_len;
7239
7240 // Create a type for the function, with the return type and any
7241 // argument types.
7242 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7243 // The type is included in "tt_args".
7244 if (argcount > 0 || varargs)
7245 {
7246 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7247 argcount, &ufunc->uf_type_list);
7248 // Add argument types to the function type.
7249 if (func_type_add_arg_types(ufunc->uf_func_type,
7250 argcount + varargs,
7251 &ufunc->uf_type_list) == FAIL)
7252 return;
7253 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7254 ufunc->uf_func_type->tt_min_argcount =
7255 argcount - ufunc->uf_def_args.ga_len;
7256 if (ufunc->uf_arg_types == NULL)
7257 {
7258 int i;
7259
7260 // lambda does not have argument types.
7261 for (i = 0; i < argcount; ++i)
7262 ufunc->uf_func_type->tt_args[i] = &t_any;
7263 }
7264 else
7265 mch_memmove(ufunc->uf_func_type->tt_args,
7266 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7267 if (varargs)
7268 {
7269 ufunc->uf_func_type->tt_args[argcount] =
7270 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7271 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7272 }
7273 }
7274 else
7275 // No arguments, can use a predefined type.
7276 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7277 argcount, &ufunc->uf_type_list);
7278}
7279
7280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281/*
7282 * Delete an instruction, free what it contains.
7283 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007284 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007285delete_instr(isn_T *isn)
7286{
7287 switch (isn->isn_type)
7288 {
7289 case ISN_EXEC:
7290 case ISN_LOADENV:
7291 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007292 case ISN_LOADB:
7293 case ISN_LOADW:
7294 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007295 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007296 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007297 case ISN_PUSHEXC:
7298 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007299 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007300 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007301 case ISN_STOREB:
7302 case ISN_STOREW:
7303 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007304 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007305 vim_free(isn->isn_arg.string);
7306 break;
7307
7308 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007309 case ISN_STORES:
7310 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007311 break;
7312
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007313 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007314 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007315 vim_free(isn->isn_arg.unlet.ul_name);
7316 break;
7317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007318 case ISN_STOREOPT:
7319 vim_free(isn->isn_arg.storeopt.so_name);
7320 break;
7321
7322 case ISN_PUSHBLOB: // push blob isn_arg.blob
7323 blob_unref(isn->isn_arg.blob);
7324 break;
7325
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007326 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007327#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007328 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007329#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007330 break;
7331
7332 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007333#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007334 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007335#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007336 break;
7337
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007338 case ISN_UCALL:
7339 vim_free(isn->isn_arg.ufunc.cuf_name);
7340 break;
7341
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007342 case ISN_FUNCREF:
7343 {
7344 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7345 + isn->isn_arg.funcref.fr_func;
7346 func_ptr_unref(dfunc->df_ufunc);
7347 }
7348 break;
7349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007350 case ISN_2BOOL:
7351 case ISN_2STRING:
7352 case ISN_ADDBLOB:
7353 case ISN_ADDLIST:
7354 case ISN_BCALL:
7355 case ISN_CATCH:
7356 case ISN_CHECKNR:
7357 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007358 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007359 case ISN_COMPAREANY:
7360 case ISN_COMPAREBLOB:
7361 case ISN_COMPAREBOOL:
7362 case ISN_COMPAREDICT:
7363 case ISN_COMPAREFLOAT:
7364 case ISN_COMPAREFUNC:
7365 case ISN_COMPARELIST:
7366 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367 case ISN_COMPARESPECIAL:
7368 case ISN_COMPARESTRING:
7369 case ISN_CONCAT:
7370 case ISN_DCALL:
7371 case ISN_DROP:
7372 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007373 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007374 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007375 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007376 case ISN_EXECCONCAT:
7377 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378 case ISN_FOR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 case ISN_INDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007380 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007381 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007382 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383 case ISN_JUMP:
7384 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007385 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007386 case ISN_LOADSCRIPT:
7387 case ISN_LOADREG:
7388 case ISN_LOADV:
7389 case ISN_NEGATENR:
7390 case ISN_NEWDICT:
7391 case ISN_NEWLIST:
7392 case ISN_OPNR:
7393 case ISN_OPFLOAT:
7394 case ISN_OPANY:
7395 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007396 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397 case ISN_PUSHF:
7398 case ISN_PUSHNR:
7399 case ISN_PUSHBOOL:
7400 case ISN_PUSHSPEC:
7401 case ISN_RETURN:
7402 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007403 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007404 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007405 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007406 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007407 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007408 case ISN_STOREDICT:
7409 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007410 case ISN_THROW:
7411 case ISN_TRY:
7412 // nothing allocated
7413 break;
7414 }
7415}
7416
7417/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007418 * Free all instructions for "dfunc".
7419 */
7420 static void
7421delete_def_function_contents(dfunc_T *dfunc)
7422{
7423 int idx;
7424
7425 ga_clear(&dfunc->df_def_args_isn);
7426
7427 if (dfunc->df_instr != NULL)
7428 {
7429 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7430 delete_instr(dfunc->df_instr + idx);
7431 VIM_CLEAR(dfunc->df_instr);
7432 }
7433
7434 dfunc->df_deleted = TRUE;
7435}
7436
7437/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007438 * When a user function is deleted, clear the contents of any associated def
7439 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007440 */
7441 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007442clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007443{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007444 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007445 {
7446 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7447 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007448
Bram Moolenaar20431c92020-03-20 18:39:46 +01007449 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007450 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007451 }
7452}
7453
7454#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007455/*
7456 * Free all functions defined with ":def".
7457 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007458 void
7459free_def_functions(void)
7460{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007461 int idx;
7462
7463 for (idx = 0; idx < def_functions.ga_len; ++idx)
7464 {
7465 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7466
7467 delete_def_function_contents(dfunc);
7468 }
7469
7470 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007471}
7472#endif
7473
7474
7475#endif // FEAT_EVAL