blob: f8381d7837d3bc39d85e2b25a1436731999a8181 [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 Moolenaar2f8ce0a2020-07-19 19:47:35 +0200150static char e_namespace[] = N_("E1075: Namespace not supported: %s");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151
Bram Moolenaar20431c92020-03-20 18:39:46 +0100152static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200153static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100154
155/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200156 * Lookup variable "name" in the local scope and return it.
157 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100160lookup_local(char_u *name, size_t len, cctx_T *cctx)
161{
162 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200163 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100165 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200166 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167
168 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
170 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200171 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100172 if (STRNCMP(name, lvar->lv_name, len) == 0
173 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174 {
175 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200176 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100178 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200179
180 // Find local in outer function scope.
181 if (cctx->ctx_outer != NULL)
182 {
183 lvar = lookup_local(name, len, cctx->ctx_outer);
184 if (lvar != NULL)
185 {
186 // TODO: are there situations we should not mark the outer scope as
187 // used?
188 cctx->ctx_outer_used = TRUE;
189 lvar->lv_from_outer = TRUE;
190 return lvar;
191 }
192 }
193
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200194 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100195}
196
197/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200198 * Lookup an argument in the current function and an enclosing function.
199 * Returns the argument index in "idxp"
200 * Returns the argument type in "type"
201 * Sets "gen_load_outer" to TRUE if found in outer scope.
202 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100203 */
204 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200205lookup_arg(
206 char_u *name,
207 size_t len,
208 int *idxp,
209 type_T **type,
210 int *gen_load_outer,
211 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100212{
213 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200214 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100215
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100216 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200217 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
219 {
220 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
221
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200222 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
223 {
224 if (idxp != NULL)
225 {
226 // Arguments are located above the frame pointer. One further
227 // if there is a vararg argument
228 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
229 + STACK_FRAME_SIZE)
230 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
231
232 if (cctx->ctx_ufunc->uf_arg_types != NULL)
233 *type = cctx->ctx_ufunc->uf_arg_types[idx];
234 else
235 *type = &t_any;
236 }
237 return OK;
238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200241 va_name = cctx->ctx_ufunc->uf_va_name;
242 if (va_name != NULL
243 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
244 {
245 if (idxp != NULL)
246 {
247 // varargs is always the last argument
248 *idxp = -STACK_FRAME_SIZE - 1;
249 *type = cctx->ctx_ufunc->uf_va_type;
250 }
251 return OK;
252 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200254 if (cctx->ctx_outer != NULL)
255 {
256 // Lookup the name for an argument of the outer function.
257 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
258 == OK)
259 {
260 *gen_load_outer = TRUE;
261 return OK;
262 }
263 }
264
265 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266}
267
268/*
269 * Lookup a variable in the current script.
270 * Returns OK or FAIL.
271 */
272 static int
273lookup_script(char_u *name, size_t len)
274{
275 int cc;
276 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
277 dictitem_T *di;
278
279 cc = name[len];
280 name[len] = NUL;
281 di = find_var_in_ht(ht, 0, name, TRUE);
282 name[len] = cc;
283 return di == NULL ? FAIL: OK;
284}
285
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100286/*
287 * Check if "p[len]" is already defined, either in script "import_sid" or in
288 * compilation context "cctx".
289 * Return FAIL and give an error if it defined.
290 */
291 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200292check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100293{
Bram Moolenaarad486a02020-08-01 23:22:18 +0200294 int c = p[len];
295
296 p[len] = NUL;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100297 if (lookup_script(p, len) == OK
298 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200299 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaareef21022020-08-01 22:16:43 +0200300 || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200301 || find_imported(p, len, cctx) != NULL
302 || find_func_even_dead(p, FALSE, cctx) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100303 {
Bram Moolenaarad486a02020-08-01 23:22:18 +0200304 p[len] = c;
Bram Moolenaareef21022020-08-01 22:16:43 +0200305 semsg(_(e_already_defined), p);
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100306 return FAIL;
307 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200308 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100309 return OK;
310}
311
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200312/*
313 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
314 * be freed later.
315 */
316 static type_T *
317alloc_type(garray_T *type_gap)
318{
319 type_T *type;
320
321 if (ga_grow(type_gap, 1) == FAIL)
322 return NULL;
323 type = ALLOC_CLEAR_ONE(type_T);
324 if (type != NULL)
325 {
326 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
327 ++type_gap->ga_len;
328 }
329 return type;
330}
331
Bram Moolenaar6110e792020-07-08 19:35:21 +0200332 void
333clear_type_list(garray_T *gap)
334{
335 while (gap->ga_len > 0)
336 vim_free(((type_T **)gap->ga_data)[--gap->ga_len]);
337 ga_clear(gap);
338}
339
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200341get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100342{
343 type_T *type;
344
345 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200346 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100347 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200348 if (member_type->tt_type == VAR_VOID
349 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100350 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100351 if (member_type->tt_type == VAR_BOOL)
352 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100353 if (member_type->tt_type == VAR_NUMBER)
354 return &t_list_number;
355 if (member_type->tt_type == VAR_STRING)
356 return &t_list_string;
357
358 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200359 type = alloc_type(type_gap);
360 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100361 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100362 type->tt_type = VAR_LIST;
363 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200364 type->tt_argcount = 0;
365 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366 return type;
367}
368
369 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200370get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100371{
372 type_T *type;
373
374 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200375 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100376 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200377 if (member_type->tt_type == VAR_VOID
378 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100379 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100380 if (member_type->tt_type == VAR_BOOL)
381 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100382 if (member_type->tt_type == VAR_NUMBER)
383 return &t_dict_number;
384 if (member_type->tt_type == VAR_STRING)
385 return &t_dict_string;
386
387 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200388 type = alloc_type(type_gap);
389 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100390 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391 type->tt_type = VAR_DICT;
392 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200393 type->tt_argcount = 0;
394 type->tt_args = NULL;
395 return type;
396}
397
398/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200399 * Allocate a new type for a function.
400 */
401 static type_T *
402alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
403{
404 type_T *type = alloc_type(type_gap);
405
406 if (type == NULL)
407 return &t_any;
408 type->tt_type = VAR_FUNC;
409 type->tt_member = ret_type;
410 type->tt_argcount = argcount;
411 type->tt_args = NULL;
412 return type;
413}
414
415/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200416 * Get a function type, based on the return type "ret_type".
417 * If "argcount" is -1 or 0 a predefined type can be used.
418 * If "argcount" > 0 always create a new type, so that arguments can be added.
419 */
420 static type_T *
421get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
422{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200423 // recognize commonly used types
424 if (argcount <= 0)
425 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200426 if (ret_type == &t_unknown)
427 {
428 // (argcount == 0) is not possible
429 return &t_func_unknown;
430 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200431 if (ret_type == &t_void)
432 {
433 if (argcount == 0)
434 return &t_func_0_void;
435 else
436 return &t_func_void;
437 }
438 if (ret_type == &t_any)
439 {
440 if (argcount == 0)
441 return &t_func_0_any;
442 else
443 return &t_func_any;
444 }
445 if (ret_type == &t_number)
446 {
447 if (argcount == 0)
448 return &t_func_0_number;
449 else
450 return &t_func_number;
451 }
452 if (ret_type == &t_string)
453 {
454 if (argcount == 0)
455 return &t_func_0_string;
456 else
457 return &t_func_string;
458 }
459 }
460
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200461 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462}
463
Bram Moolenaara8c17702020-04-01 21:17:24 +0200464/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200465 * For a function type, reserve space for "argcount" argument types (including
466 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200467 */
468 static int
469func_type_add_arg_types(
470 type_T *functype,
471 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200472 garray_T *type_gap)
473{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200474 // To make it easy to free the space needed for the argument types, add the
475 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200476 if (ga_grow(type_gap, 1) == FAIL)
477 return FAIL;
478 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
479 if (functype->tt_args == NULL)
480 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200481 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
482 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200483 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200484 return OK;
485}
486
487/*
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200488 * Get a type_T for a typval_T.
489 * "type_list" is used to temporarily create types in.
Bram Moolenaara8c17702020-04-01 21:17:24 +0200490 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200491 type_T *
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200492typval2type(typval_T *tv, garray_T *type_gap)
Bram Moolenaara8c17702020-04-01 21:17:24 +0200493{
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200494 type_T *actual;
495 type_T *member_type;
496
Bram Moolenaara8c17702020-04-01 21:17:24 +0200497 if (tv->v_type == VAR_NUMBER)
498 return &t_number;
499 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200500 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200501 if (tv->v_type == VAR_STRING)
502 return &t_string;
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200503
504 if (tv->v_type == VAR_LIST
505 && tv->vval.v_list != NULL
506 && tv->vval.v_list->lv_first != NULL)
507 {
508 // Use the type of the first member, it is the most specific.
509 member_type = typval2type(&tv->vval.v_list->lv_first->li_tv, type_gap);
510 return get_list_type(member_type, type_gap);
511 }
512
513 if (tv->v_type == VAR_DICT
514 && tv->vval.v_dict != NULL
515 && tv->vval.v_dict->dv_hashtab.ht_used > 0)
516 {
517 dict_iterator_T iter;
518 typval_T *value;
519
520 // Use the type of the first value, it is the most specific.
521 dict_iterate_start(tv, &iter);
522 dict_iterate_next(&iter, &value);
523 member_type = typval2type(value, type_gap);
524 return get_dict_type(member_type, type_gap);
525 }
526
527 if (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
528 {
529 char_u *name = NULL;
530 ufunc_T *ufunc = NULL;
531
532 if (tv->v_type == VAR_PARTIAL)
533 {
534 if (tv->vval.v_partial->pt_func != NULL)
535 ufunc = tv->vval.v_partial->pt_func;
536 else
537 name = tv->vval.v_partial->pt_name;
538 }
539 else
540 name = tv->vval.v_string;
541 if (name != NULL)
542 // TODO: how about a builtin function?
543 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200544 if (ufunc != NULL)
545 {
546 // May need to get the argument types from default values by
547 // compiling the function.
548 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
549 && compile_def_function(ufunc, TRUE, NULL) == FAIL)
550 return NULL;
551 if (ufunc->uf_func_type != NULL)
552 return ufunc->uf_func_type;
553 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200554 }
555
556 actual = alloc_type(type_gap);
557 if (actual == NULL)
558 return NULL;
559 actual->tt_type = tv->v_type;
560 actual->tt_member = &t_any;
561
562 return actual;
563}
564
565/*
566 * Get a type_T for a typval_T, used for v: variables.
567 * "type_list" is used to temporarily create types in.
568 */
569 type_T *
570typval2type_vimvar(typval_T *tv, garray_T *type_gap)
571{
Bram Moolenaara8c17702020-04-01 21:17:24 +0200572 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
573 return &t_list_string;
574 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
575 return &t_dict_any;
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200576 return typval2type(tv, type_gap);
577}
578
579
580/*
581 * Return FAIL if "expected" and "actual" don't match.
582 */
583 int
584check_typval_type(type_T *expected, typval_T *actual_tv)
585{
586 garray_T type_list;
587 type_T *actual_type;
588 int res = FAIL;
589
590 ga_init2(&type_list, sizeof(type_T *), 10);
591 actual_type = typval2type(actual_tv, &type_list);
592 if (actual_type != NULL)
593 res = check_type(expected, actual_type, TRUE);
594 clear_type_list(&type_list);
595 return res;
Bram Moolenaara8c17702020-04-01 21:17:24 +0200596}
597
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200598 static void
599type_mismatch(type_T *expected, type_T *actual)
600{
601 char *tofree1, *tofree2;
602
603 semsg(_("E1013: type mismatch, expected %s but got %s"),
604 type_name(expected, &tofree1), type_name(actual, &tofree2));
605 vim_free(tofree1);
606 vim_free(tofree2);
607}
608
609 static void
610arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
611{
612 char *tofree1, *tofree2;
613
614 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
615 argidx,
616 type_name(expected, &tofree1), type_name(actual, &tofree2));
617 vim_free(tofree1);
618 vim_free(tofree2);
619}
620
621/*
622 * Check if the expected and actual types match.
623 * Does not allow for assigning "any" to a specific type.
624 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200625 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200626check_type(type_T *expected, type_T *actual, int give_msg)
627{
628 int ret = OK;
629
630 // When expected is "unknown" we accept any actual type.
631 // When expected is "any" we accept any actual type except "void".
632 if (expected->tt_type != VAR_UNKNOWN
633 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
634
635 {
636 if (expected->tt_type != actual->tt_type)
637 {
638 if (give_msg)
639 type_mismatch(expected, actual);
640 return FAIL;
641 }
642 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
643 {
644 // "unknown" is used for an empty list or dict
645 if (actual->tt_member != &t_unknown)
646 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
647 }
648 else if (expected->tt_type == VAR_FUNC)
649 {
650 if (expected->tt_member != &t_unknown)
651 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
652 if (ret == OK && expected->tt_argcount != -1
653 && (actual->tt_argcount < expected->tt_min_argcount
654 || actual->tt_argcount > expected->tt_argcount))
655 ret = FAIL;
Bram Moolenaarb8070e32020-07-23 20:56:04 +0200656 if (expected->tt_args != NULL && actual->tt_args != NULL)
657 {
658 int i;
659
660 for (i = 0; i < expected->tt_argcount; ++i)
661 if (check_type(expected->tt_args[i], actual->tt_args[i],
662 FALSE) == FAIL)
663 {
664 ret = FAIL;
665 break;
666 }
667 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200668 }
669 if (ret == FAIL && give_msg)
670 type_mismatch(expected, actual);
671 }
672 return ret;
673}
674
Bram Moolenaar65b95452020-07-19 14:03:09 +0200675
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676/////////////////////////////////////////////////////////////////////
677// Following generate_ functions expect the caller to call ga_grow().
678
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200679#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
680#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100681
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682/*
683 * Generate an instruction without arguments.
684 * Returns a pointer to the new instruction, NULL if failed.
685 */
686 static isn_T *
687generate_instr(cctx_T *cctx, isntype_T isn_type)
688{
689 garray_T *instr = &cctx->ctx_instr;
690 isn_T *isn;
691
Bram Moolenaar080457c2020-03-03 21:53:32 +0100692 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693 if (ga_grow(instr, 1) == FAIL)
694 return NULL;
695 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
696 isn->isn_type = isn_type;
697 isn->isn_lnum = cctx->ctx_lnum + 1;
698 ++instr->ga_len;
699
700 return isn;
701}
702
703/*
704 * Generate an instruction without arguments.
705 * "drop" will be removed from the stack.
706 * Returns a pointer to the new instruction, NULL if failed.
707 */
708 static isn_T *
709generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
710{
711 garray_T *stack = &cctx->ctx_type_stack;
712
Bram Moolenaar080457c2020-03-03 21:53:32 +0100713 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714 stack->ga_len -= drop;
715 return generate_instr(cctx, isn_type);
716}
717
718/*
719 * Generate instruction "isn_type" and put "type" on the type stack.
720 */
721 static isn_T *
722generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
723{
724 isn_T *isn;
725 garray_T *stack = &cctx->ctx_type_stack;
726
727 if ((isn = generate_instr(cctx, isn_type)) == NULL)
728 return NULL;
729
730 if (ga_grow(stack, 1) == FAIL)
731 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200732 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733 ++stack->ga_len;
734
735 return isn;
736}
737
738/*
739 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
740 */
741 static int
742may_generate_2STRING(int offset, cctx_T *cctx)
743{
744 isn_T *isn;
745 garray_T *stack = &cctx->ctx_type_stack;
746 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
747
748 if ((*type)->tt_type == VAR_STRING)
749 return OK;
750 *type = &t_string;
751
752 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
753 return FAIL;
754 isn->isn_arg.number = offset;
755
756 return OK;
757}
758
759 static int
760check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
761{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200762 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200764 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 {
766 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200767 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768 else
769 semsg(_("E1036: %c requires number or float arguments"), *op);
770 return FAIL;
771 }
772 return OK;
773}
774
775/*
776 * Generate an instruction with two arguments. The instruction depends on the
777 * type of the arguments.
778 */
779 static int
780generate_two_op(cctx_T *cctx, char_u *op)
781{
782 garray_T *stack = &cctx->ctx_type_stack;
783 type_T *type1;
784 type_T *type2;
785 vartype_T vartype;
786 isn_T *isn;
787
Bram Moolenaar080457c2020-03-03 21:53:32 +0100788 RETURN_OK_IF_SKIP(cctx);
789
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100790 // Get the known type of the two items on the stack. If they are matching
791 // use a type-specific instruction. Otherwise fall back to runtime type
792 // checking.
793 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
794 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200795 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796 if (type1->tt_type == type2->tt_type
797 && (type1->tt_type == VAR_NUMBER
798 || type1->tt_type == VAR_LIST
799#ifdef FEAT_FLOAT
800 || type1->tt_type == VAR_FLOAT
801#endif
802 || type1->tt_type == VAR_BLOB))
803 vartype = type1->tt_type;
804
805 switch (*op)
806 {
807 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200808 && type1->tt_type != VAR_ANY
809 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 && check_number_or_float(
811 type1->tt_type, type2->tt_type, op) == FAIL)
812 return FAIL;
813 isn = generate_instr_drop(cctx,
814 vartype == VAR_NUMBER ? ISN_OPNR
815 : vartype == VAR_LIST ? ISN_ADDLIST
816 : vartype == VAR_BLOB ? ISN_ADDBLOB
817#ifdef FEAT_FLOAT
818 : vartype == VAR_FLOAT ? ISN_OPFLOAT
819#endif
820 : ISN_OPANY, 1);
821 if (isn != NULL)
822 isn->isn_arg.op.op_type = EXPR_ADD;
823 break;
824
825 case '-':
826 case '*':
827 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
828 op) == FAIL)
829 return FAIL;
830 if (vartype == VAR_NUMBER)
831 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
832#ifdef FEAT_FLOAT
833 else if (vartype == VAR_FLOAT)
834 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
835#endif
836 else
837 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
838 if (isn != NULL)
839 isn->isn_arg.op.op_type = *op == '*'
840 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
841 break;
842
Bram Moolenaar4c683752020-04-05 21:38:23 +0200843 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100844 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200845 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846 && type2->tt_type != VAR_NUMBER))
847 {
848 emsg(_("E1035: % requires number arguments"));
849 return FAIL;
850 }
851 isn = generate_instr_drop(cctx,
852 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
853 if (isn != NULL)
854 isn->isn_arg.op.op_type = EXPR_REM;
855 break;
856 }
857
858 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200859 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 {
861 type_T *type = &t_any;
862
863#ifdef FEAT_FLOAT
864 // float+number and number+float results in float
865 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
866 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
867 type = &t_float;
868#endif
869 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
870 }
871
872 return OK;
873}
874
875/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200876 * Get the instruction to use for comparing "type1" with "type2"
877 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200879 static isntype_T
880get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881{
882 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883
Bram Moolenaar4c683752020-04-05 21:38:23 +0200884 if (type1 == VAR_UNKNOWN)
885 type1 = VAR_ANY;
886 if (type2 == VAR_UNKNOWN)
887 type2 = VAR_ANY;
888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889 if (type1 == type2)
890 {
891 switch (type1)
892 {
893 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
894 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
895 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
896 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
897 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
898 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
899 case VAR_LIST: isntype = ISN_COMPARELIST; break;
900 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
901 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100902 default: isntype = ISN_COMPAREANY; break;
903 }
904 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200905 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
907 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
908 isntype = ISN_COMPAREANY;
909
910 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
911 && (isntype == ISN_COMPAREBOOL
912 || isntype == ISN_COMPARESPECIAL
913 || isntype == ISN_COMPARENR
914 || isntype == ISN_COMPAREFLOAT))
915 {
916 semsg(_("E1037: Cannot use \"%s\" with %s"),
917 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200918 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100919 }
920 if (isntype == ISN_DROP
921 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
922 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
923 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
924 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
925 && exptype != EXPR_IS && exptype != EXPR_ISNOT
926 && (type1 == VAR_BLOB || type2 == VAR_BLOB
927 || type1 == VAR_LIST || type2 == VAR_LIST))))
928 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100929 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200931 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200933 return isntype;
934}
935
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200936 int
937check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
938{
939 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
940 return FAIL;
941 return OK;
942}
943
Bram Moolenaara5565e42020-05-09 15:44:01 +0200944/*
945 * Generate an ISN_COMPARE* instruction with a boolean result.
946 */
947 static int
948generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
949{
950 isntype_T isntype;
951 isn_T *isn;
952 garray_T *stack = &cctx->ctx_type_stack;
953 vartype_T type1;
954 vartype_T type2;
955
956 RETURN_OK_IF_SKIP(cctx);
957
958 // Get the known type of the two items on the stack. If they are matching
959 // use a type-specific instruction. Otherwise fall back to runtime type
960 // checking.
961 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
962 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
963 isntype = get_compare_isn(exptype, type1, type2);
964 if (isntype == ISN_DROP)
965 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100966
967 if ((isn = generate_instr(cctx, isntype)) == NULL)
968 return FAIL;
969 isn->isn_arg.op.op_type = exptype;
970 isn->isn_arg.op.op_ic = ic;
971
972 // takes two arguments, puts one bool back
973 if (stack->ga_len >= 2)
974 {
975 --stack->ga_len;
976 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
977 }
978
979 return OK;
980}
981
982/*
983 * Generate an ISN_2BOOL instruction.
984 */
985 static int
986generate_2BOOL(cctx_T *cctx, int invert)
987{
988 isn_T *isn;
989 garray_T *stack = &cctx->ctx_type_stack;
990
Bram Moolenaar080457c2020-03-03 21:53:32 +0100991 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100992 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
993 return FAIL;
994 isn->isn_arg.number = invert;
995
996 // type becomes bool
997 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
998
999 return OK;
1000}
1001
1002 static int
1003generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
1004{
1005 isn_T *isn;
1006 garray_T *stack = &cctx->ctx_type_stack;
1007
Bram Moolenaar080457c2020-03-03 21:53:32 +01001008 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001009 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
1010 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +02001011 // TODO: whole type, e.g. for a function also arg and return types
1012 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 isn->isn_arg.type.ct_off = offset;
1014
1015 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001016 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001017
1018 return OK;
1019}
1020
1021/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001022 * Check that
1023 * - "actual" is "expected" type or
1024 * - "actual" is a type that can be "expected" type: add a runtime check; or
1025 * - return FAIL.
1026 */
1027 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001028need_type(
1029 type_T *actual,
1030 type_T *expected,
1031 int offset,
1032 cctx_T *cctx,
1033 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001034{
1035 if (check_type(expected, actual, FALSE) == OK)
1036 return OK;
1037 if (actual->tt_type != VAR_ANY
1038 && actual->tt_type != VAR_UNKNOWN
1039 && !(actual->tt_type == VAR_FUNC
1040 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
1041 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001042 if (!silent)
1043 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001044 return FAIL;
1045 }
1046 generate_TYPECHECK(cctx, expected, offset);
1047 return OK;
1048}
1049
1050/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051 * Generate an ISN_PUSHNR instruction.
1052 */
1053 static int
1054generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1055{
1056 isn_T *isn;
1057
Bram Moolenaar080457c2020-03-03 21:53:32 +01001058 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1060 return FAIL;
1061 isn->isn_arg.number = number;
1062
1063 return OK;
1064}
1065
1066/*
1067 * Generate an ISN_PUSHBOOL instruction.
1068 */
1069 static int
1070generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1071{
1072 isn_T *isn;
1073
Bram Moolenaar080457c2020-03-03 21:53:32 +01001074 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1076 return FAIL;
1077 isn->isn_arg.number = number;
1078
1079 return OK;
1080}
1081
1082/*
1083 * Generate an ISN_PUSHSPEC instruction.
1084 */
1085 static int
1086generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1087{
1088 isn_T *isn;
1089
Bram Moolenaar080457c2020-03-03 21:53:32 +01001090 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1092 return FAIL;
1093 isn->isn_arg.number = number;
1094
1095 return OK;
1096}
1097
1098#ifdef FEAT_FLOAT
1099/*
1100 * Generate an ISN_PUSHF instruction.
1101 */
1102 static int
1103generate_PUSHF(cctx_T *cctx, float_T fnumber)
1104{
1105 isn_T *isn;
1106
Bram Moolenaar080457c2020-03-03 21:53:32 +01001107 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1109 return FAIL;
1110 isn->isn_arg.fnumber = fnumber;
1111
1112 return OK;
1113}
1114#endif
1115
1116/*
1117 * Generate an ISN_PUSHS instruction.
1118 * Consumes "str".
1119 */
1120 static int
1121generate_PUSHS(cctx_T *cctx, char_u *str)
1122{
1123 isn_T *isn;
1124
Bram Moolenaar080457c2020-03-03 21:53:32 +01001125 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1127 return FAIL;
1128 isn->isn_arg.string = str;
1129
1130 return OK;
1131}
1132
1133/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001134 * Generate an ISN_PUSHCHANNEL instruction.
1135 * Consumes "channel".
1136 */
1137 static int
1138generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1139{
1140 isn_T *isn;
1141
Bram Moolenaar080457c2020-03-03 21:53:32 +01001142 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001143 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1144 return FAIL;
1145 isn->isn_arg.channel = channel;
1146
1147 return OK;
1148}
1149
1150/*
1151 * Generate an ISN_PUSHJOB instruction.
1152 * Consumes "job".
1153 */
1154 static int
1155generate_PUSHJOB(cctx_T *cctx, job_T *job)
1156{
1157 isn_T *isn;
1158
Bram Moolenaar080457c2020-03-03 21:53:32 +01001159 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001160 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001161 return FAIL;
1162 isn->isn_arg.job = job;
1163
1164 return OK;
1165}
1166
1167/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168 * Generate an ISN_PUSHBLOB instruction.
1169 * Consumes "blob".
1170 */
1171 static int
1172generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1173{
1174 isn_T *isn;
1175
Bram Moolenaar080457c2020-03-03 21:53:32 +01001176 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1178 return FAIL;
1179 isn->isn_arg.blob = blob;
1180
1181 return OK;
1182}
1183
1184/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001185 * Generate an ISN_PUSHFUNC instruction with name "name".
1186 * Consumes "name".
1187 */
1188 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001189generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001190{
1191 isn_T *isn;
1192
Bram Moolenaar080457c2020-03-03 21:53:32 +01001193 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001194 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001195 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001196 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001197
1198 return OK;
1199}
1200
1201/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001202 * Generate an ISN_GETITEM instruction with "index".
1203 */
1204 static int
1205generate_GETITEM(cctx_T *cctx, int index)
1206{
1207 isn_T *isn;
1208 garray_T *stack = &cctx->ctx_type_stack;
1209 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1210 type_T *item_type = &t_any;
1211
1212 RETURN_OK_IF_SKIP(cctx);
1213
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001214 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001215 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001216 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001217 emsg(_(e_listreq));
1218 return FAIL;
1219 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001220 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001221 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1222 return FAIL;
1223 isn->isn_arg.number = index;
1224
1225 // add the item type to the type stack
1226 if (ga_grow(stack, 1) == FAIL)
1227 return FAIL;
1228 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1229 ++stack->ga_len;
1230 return OK;
1231}
1232
1233/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001234 * Generate an ISN_SLICE instruction with "count".
1235 */
1236 static int
1237generate_SLICE(cctx_T *cctx, int count)
1238{
1239 isn_T *isn;
1240
1241 RETURN_OK_IF_SKIP(cctx);
1242 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1243 return FAIL;
1244 isn->isn_arg.number = count;
1245 return OK;
1246}
1247
1248/*
1249 * Generate an ISN_CHECKLEN instruction with "min_len".
1250 */
1251 static int
1252generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1253{
1254 isn_T *isn;
1255
1256 RETURN_OK_IF_SKIP(cctx);
1257
1258 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1259 return FAIL;
1260 isn->isn_arg.checklen.cl_min_len = min_len;
1261 isn->isn_arg.checklen.cl_more_OK = more_OK;
1262
1263 return OK;
1264}
1265
1266/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 * Generate an ISN_STORE instruction.
1268 */
1269 static int
1270generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1271{
1272 isn_T *isn;
1273
Bram Moolenaar080457c2020-03-03 21:53:32 +01001274 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1276 return FAIL;
1277 if (name != NULL)
1278 isn->isn_arg.string = vim_strsave(name);
1279 else
1280 isn->isn_arg.number = idx;
1281
1282 return OK;
1283}
1284
1285/*
1286 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1287 */
1288 static int
1289generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1290{
1291 isn_T *isn;
1292
Bram Moolenaar080457c2020-03-03 21:53:32 +01001293 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001294 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1295 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001296 isn->isn_arg.storenr.stnr_idx = idx;
1297 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298
1299 return OK;
1300}
1301
1302/*
1303 * Generate an ISN_STOREOPT instruction
1304 */
1305 static int
1306generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1307{
1308 isn_T *isn;
1309
Bram Moolenaar080457c2020-03-03 21:53:32 +01001310 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001311 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1312 return FAIL;
1313 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1314 isn->isn_arg.storeopt.so_flags = opt_flags;
1315
1316 return OK;
1317}
1318
1319/*
1320 * Generate an ISN_LOAD or similar instruction.
1321 */
1322 static int
1323generate_LOAD(
1324 cctx_T *cctx,
1325 isntype_T isn_type,
1326 int idx,
1327 char_u *name,
1328 type_T *type)
1329{
1330 isn_T *isn;
1331
Bram Moolenaar080457c2020-03-03 21:53:32 +01001332 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1334 return FAIL;
1335 if (name != NULL)
1336 isn->isn_arg.string = vim_strsave(name);
1337 else
1338 isn->isn_arg.number = idx;
1339
1340 return OK;
1341}
1342
1343/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001344 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001345 */
1346 static int
1347generate_LOADV(
1348 cctx_T *cctx,
1349 char_u *name,
1350 int error)
1351{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001352 int di_flags;
1353 int vidx = find_vim_var(name, &di_flags);
1354 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001355
Bram Moolenaar080457c2020-03-03 21:53:32 +01001356 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001357 if (vidx < 0)
1358 {
1359 if (error)
1360 semsg(_(e_var_notfound), name);
1361 return FAIL;
1362 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001363 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001364
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001365 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001366}
1367
1368/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001369 * Generate an ISN_UNLET instruction.
1370 */
1371 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001372generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001373{
1374 isn_T *isn;
1375
1376 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001377 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001378 return FAIL;
1379 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1380 isn->isn_arg.unlet.ul_forceit = forceit;
1381
1382 return OK;
1383}
1384
1385/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386 * Generate an ISN_LOADS instruction.
1387 */
1388 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001389generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001391 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001392 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001393 int sid,
1394 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395{
1396 isn_T *isn;
1397
Bram Moolenaar080457c2020-03-03 21:53:32 +01001398 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001399 if (isn_type == ISN_LOADS)
1400 isn = generate_instr_type(cctx, isn_type, type);
1401 else
1402 isn = generate_instr_drop(cctx, isn_type, 1);
1403 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001405 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1406 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407
1408 return OK;
1409}
1410
1411/*
1412 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1413 */
1414 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001415generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 cctx_T *cctx,
1417 isntype_T isn_type,
1418 int sid,
1419 int idx,
1420 type_T *type)
1421{
1422 isn_T *isn;
1423
Bram Moolenaar080457c2020-03-03 21:53:32 +01001424 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 if (isn_type == ISN_LOADSCRIPT)
1426 isn = generate_instr_type(cctx, isn_type, type);
1427 else
1428 isn = generate_instr_drop(cctx, isn_type, 1);
1429 if (isn == NULL)
1430 return FAIL;
1431 isn->isn_arg.script.script_sid = sid;
1432 isn->isn_arg.script.script_idx = idx;
1433 return OK;
1434}
1435
1436/*
1437 * Generate an ISN_NEWLIST instruction.
1438 */
1439 static int
1440generate_NEWLIST(cctx_T *cctx, int count)
1441{
1442 isn_T *isn;
1443 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 type_T *type;
1445 type_T *member;
1446
Bram Moolenaar080457c2020-03-03 21:53:32 +01001447 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1449 return FAIL;
1450 isn->isn_arg.number = count;
1451
1452 // drop the value types
1453 stack->ga_len -= count;
1454
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001455 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001456 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 if (count > 0)
1458 member = ((type_T **)stack->ga_data)[stack->ga_len];
1459 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001460 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001461 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462
1463 // add the list type to the type stack
1464 if (ga_grow(stack, 1) == FAIL)
1465 return FAIL;
1466 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1467 ++stack->ga_len;
1468
1469 return OK;
1470}
1471
1472/*
1473 * Generate an ISN_NEWDICT instruction.
1474 */
1475 static int
1476generate_NEWDICT(cctx_T *cctx, int count)
1477{
1478 isn_T *isn;
1479 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480 type_T *type;
1481 type_T *member;
1482
Bram Moolenaar080457c2020-03-03 21:53:32 +01001483 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001484 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1485 return FAIL;
1486 isn->isn_arg.number = count;
1487
1488 // drop the key and value types
1489 stack->ga_len -= 2 * count;
1490
Bram Moolenaar436472f2020-02-20 22:54:43 +01001491 // Use the first value type for the list member type. Use "void" for an
1492 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001493 if (count > 0)
1494 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1495 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001496 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001497 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001498
1499 // add the dict type to the type stack
1500 if (ga_grow(stack, 1) == FAIL)
1501 return FAIL;
1502 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1503 ++stack->ga_len;
1504
1505 return OK;
1506}
1507
1508/*
1509 * Generate an ISN_FUNCREF instruction.
1510 */
1511 static int
1512generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1513{
1514 isn_T *isn;
1515 garray_T *stack = &cctx->ctx_type_stack;
1516
Bram Moolenaar080457c2020-03-03 21:53:32 +01001517 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1519 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001520 isn->isn_arg.funcref.fr_func = dfunc_idx;
1521 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522
1523 if (ga_grow(stack, 1) == FAIL)
1524 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001525 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526 // TODO: argument and return types
1527 ++stack->ga_len;
1528
1529 return OK;
1530}
1531
1532/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001533 * Generate an ISN_NEWFUNC instruction.
1534 */
1535 static int
1536generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1537{
1538 isn_T *isn;
1539 char_u *name;
1540
1541 RETURN_OK_IF_SKIP(cctx);
1542 name = vim_strsave(lambda_name);
1543 if (name == NULL)
1544 return FAIL;
1545 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1546 return FAIL;
1547 isn->isn_arg.newfunc.nf_lambda = name;
1548 isn->isn_arg.newfunc.nf_global = func_name;
1549
1550 return OK;
1551}
1552
1553/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 * Generate an ISN_JUMP instruction.
1555 */
1556 static int
1557generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1558{
1559 isn_T *isn;
1560 garray_T *stack = &cctx->ctx_type_stack;
1561
Bram Moolenaar080457c2020-03-03 21:53:32 +01001562 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1564 return FAIL;
1565 isn->isn_arg.jump.jump_when = when;
1566 isn->isn_arg.jump.jump_where = where;
1567
1568 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1569 --stack->ga_len;
1570
1571 return OK;
1572}
1573
1574 static int
1575generate_FOR(cctx_T *cctx, int loop_idx)
1576{
1577 isn_T *isn;
1578 garray_T *stack = &cctx->ctx_type_stack;
1579
Bram Moolenaar080457c2020-03-03 21:53:32 +01001580 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1582 return FAIL;
1583 isn->isn_arg.forloop.for_idx = loop_idx;
1584
1585 if (ga_grow(stack, 1) == FAIL)
1586 return FAIL;
1587 // type doesn't matter, will be stored next
1588 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1589 ++stack->ga_len;
1590
1591 return OK;
1592}
1593
1594/*
1595 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001596 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 * Return FAIL if the number of arguments is wrong.
1598 */
1599 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001600generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601{
1602 isn_T *isn;
1603 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001604 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001605 type_T *argtypes[MAX_FUNC_ARGS];
1606 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607
Bram Moolenaar080457c2020-03-03 21:53:32 +01001608 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001609 argoff = check_internal_func(func_idx, argcount);
1610 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 return FAIL;
1612
Bram Moolenaar389df252020-07-09 21:20:47 +02001613 if (method_call && argoff > 1)
1614 {
1615 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1616 return FAIL;
1617 isn->isn_arg.shuffle.shfl_item = argcount;
1618 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1619 }
1620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1622 return FAIL;
1623 isn->isn_arg.bfunc.cbf_idx = func_idx;
1624 isn->isn_arg.bfunc.cbf_argcount = argcount;
1625
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001626 for (i = 0; i < argcount; ++i)
1627 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 stack->ga_len -= argcount; // drop the arguments
1630 if (ga_grow(stack, 1) == FAIL)
1631 return FAIL;
1632 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001633 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634 ++stack->ga_len; // add return value
1635
1636 return OK;
1637}
1638
1639/*
1640 * Generate an ISN_DCALL or ISN_UCALL instruction.
1641 * Return FAIL if the number of arguments is wrong.
1642 */
1643 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001644generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645{
1646 isn_T *isn;
1647 garray_T *stack = &cctx->ctx_type_stack;
1648 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001649 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650
Bram Moolenaar080457c2020-03-03 21:53:32 +01001651 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 if (argcount > regular_args && !has_varargs(ufunc))
1653 {
1654 semsg(_(e_toomanyarg), ufunc->uf_name);
1655 return FAIL;
1656 }
1657 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1658 {
1659 semsg(_(e_toofewarg), ufunc->uf_name);
1660 return FAIL;
1661 }
1662
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001663 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001664 {
1665 int i;
1666
1667 for (i = 0; i < argcount; ++i)
1668 {
1669 type_T *expected;
1670 type_T *actual;
1671
1672 if (i < regular_args)
1673 {
1674 if (ufunc->uf_arg_types == NULL)
1675 continue;
1676 expected = ufunc->uf_arg_types[i];
1677 }
1678 else
1679 expected = ufunc->uf_va_type->tt_member;
1680 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001681 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001682 {
1683 arg_type_mismatch(expected, actual, i + 1);
1684 return FAIL;
1685 }
1686 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001687 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001688 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001689 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001690 }
1691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001693 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001694 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001696 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 {
1698 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1699 isn->isn_arg.dfunc.cdf_argcount = argcount;
1700 }
1701 else
1702 {
1703 // A user function may be deleted and redefined later, can't use the
1704 // ufunc pointer, need to look it up again at runtime.
1705 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1706 isn->isn_arg.ufunc.cuf_argcount = argcount;
1707 }
1708
1709 stack->ga_len -= argcount; // drop the arguments
1710 if (ga_grow(stack, 1) == FAIL)
1711 return FAIL;
1712 // add return value
1713 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1714 ++stack->ga_len;
1715
1716 return OK;
1717}
1718
1719/*
1720 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1721 */
1722 static int
1723generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1724{
1725 isn_T *isn;
1726 garray_T *stack = &cctx->ctx_type_stack;
1727
Bram Moolenaar080457c2020-03-03 21:53:32 +01001728 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001729 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1730 return FAIL;
1731 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1732 isn->isn_arg.ufunc.cuf_argcount = argcount;
1733
1734 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001735 if (ga_grow(stack, 1) == FAIL)
1736 return FAIL;
1737 // add return value
1738 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1739 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740
1741 return OK;
1742}
1743
1744/*
1745 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001746 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 */
1748 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001749generate_PCALL(
1750 cctx_T *cctx,
1751 int argcount,
1752 char_u *name,
1753 type_T *type,
1754 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755{
1756 isn_T *isn;
1757 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001758 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001759
Bram Moolenaar080457c2020-03-03 21:53:32 +01001760 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001761
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001762 if (type->tt_type == VAR_ANY)
1763 ret_type = &t_any;
1764 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001765 {
1766 if (type->tt_argcount != -1)
1767 {
1768 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1769
1770 if (argcount < type->tt_min_argcount - varargs)
1771 {
1772 semsg(_(e_toofewarg), "[reference]");
1773 return FAIL;
1774 }
1775 if (!varargs && argcount > type->tt_argcount)
1776 {
1777 semsg(_(e_toomanyarg), "[reference]");
1778 return FAIL;
1779 }
1780 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001781 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001782 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001783 else
1784 {
1785 semsg(_("E1085: Not a callable type: %s"), name);
1786 return FAIL;
1787 }
1788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1790 return FAIL;
1791 isn->isn_arg.pfunc.cpf_top = at_top;
1792 isn->isn_arg.pfunc.cpf_argcount = argcount;
1793
1794 stack->ga_len -= argcount; // drop the arguments
1795
1796 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001797 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001799 // If partial is above the arguments it must be cleared and replaced with
1800 // the return value.
1801 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1802 return FAIL;
1803
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 return OK;
1805}
1806
1807/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001808 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 */
1810 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001811generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812{
1813 isn_T *isn;
1814 garray_T *stack = &cctx->ctx_type_stack;
1815 type_T *type;
1816
Bram Moolenaar080457c2020-03-03 21:53:32 +01001817 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001818 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001820 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001822 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001823 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001824 if (type->tt_type != VAR_DICT && type != &t_any)
1825 {
1826 emsg(_(e_dictreq));
1827 return FAIL;
1828 }
1829 // change dict type to dict member type
1830 if (type->tt_type == VAR_DICT)
1831 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832
1833 return OK;
1834}
1835
1836/*
1837 * Generate an ISN_ECHO instruction.
1838 */
1839 static int
1840generate_ECHO(cctx_T *cctx, int with_white, int count)
1841{
1842 isn_T *isn;
1843
Bram Moolenaar080457c2020-03-03 21:53:32 +01001844 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1846 return FAIL;
1847 isn->isn_arg.echo.echo_with_white = with_white;
1848 isn->isn_arg.echo.echo_count = count;
1849
1850 return OK;
1851}
1852
Bram Moolenaarad39c092020-02-26 18:23:43 +01001853/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001854 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001855 */
1856 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001857generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001858{
1859 isn_T *isn;
1860
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001861 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001862 return FAIL;
1863 isn->isn_arg.number = count;
1864
1865 return OK;
1866}
1867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868 static int
1869generate_EXEC(cctx_T *cctx, char_u *line)
1870{
1871 isn_T *isn;
1872
Bram Moolenaar080457c2020-03-03 21:53:32 +01001873 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1875 return FAIL;
1876 isn->isn_arg.string = vim_strsave(line);
1877 return OK;
1878}
1879
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001880 static int
1881generate_EXECCONCAT(cctx_T *cctx, int count)
1882{
1883 isn_T *isn;
1884
1885 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1886 return FAIL;
1887 isn->isn_arg.number = count;
1888 return OK;
1889}
1890
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891/*
1892 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001893 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001895 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001896reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1897{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 lvar_T *lvar;
1899
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001900 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001902 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001903 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 }
1905
1906 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001907 return NULL;
1908 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001909
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001910 // Every local variable uses the next entry on the stack. We could re-use
1911 // the last ones when leaving a scope, but then variables used in a closure
1912 // might get overwritten. To keep things simple do not re-use stack
1913 // entries. This is less efficient, but memory is cheap these days.
1914 lvar->lv_idx = cctx->ctx_locals_count++;
1915
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001916 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 lvar->lv_const = isConst;
1918 lvar->lv_type = type;
1919
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001920 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921}
1922
1923/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001924 * Remove local variables above "new_top".
1925 */
1926 static void
1927unwind_locals(cctx_T *cctx, int new_top)
1928{
1929 if (cctx->ctx_locals.ga_len > new_top)
1930 {
1931 int idx;
1932 lvar_T *lvar;
1933
1934 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1935 {
1936 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1937 vim_free(lvar->lv_name);
1938 }
1939 }
1940 cctx->ctx_locals.ga_len = new_top;
1941}
1942
1943/*
1944 * Free all local variables.
1945 */
1946 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001947free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001948{
1949 unwind_locals(cctx, 0);
1950 ga_clear(&cctx->ctx_locals);
1951}
1952
1953/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001954 * Skip over a type definition and return a pointer to just after it.
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001955 * When "optional" is TRUE then a leading "?" is accepted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 */
1957 char_u *
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001958skip_type(char_u *start, int optional)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959{
1960 char_u *p = start;
1961
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001962 if (optional && *p == '?')
1963 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 while (ASCII_ISALNUM(*p) || *p == '_')
1965 ++p;
1966
1967 // Skip over "<type>"; this is permissive about white space.
1968 if (*skipwhite(p) == '<')
1969 {
1970 p = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001971 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 p = skipwhite(p);
1973 if (*p == '>')
1974 ++p;
1975 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001976 else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1])))
1977 && STRNCMP("func", start, 4) == 0)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001978 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001979 if (*p == '(')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001980 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001981 // handle func(args): type
1982 ++p;
1983 while (*p != ')' && *p != NUL)
1984 {
1985 char_u *sp = p;
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001986
Bram Moolenaarace61322020-07-26 18:16:58 +02001987 if (STRNCMP(p, "...", 3) == 0)
1988 p += 3;
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001989 p = skip_type(p, TRUE);
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001990 if (p == sp)
1991 return p; // syntax error
1992 if (*p == ',')
1993 p = skipwhite(p + 1);
1994 }
1995 if (*p == ')')
1996 {
1997 if (p[1] == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001998 p = skip_type(skipwhite(p + 2), FALSE);
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001999 else
Bram Moolenaarbfba8652020-07-23 20:09:10 +02002000 ++p;
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002001 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002002 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002003 else
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002004 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002005 // handle func: return_type
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02002006 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002007 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002008 }
2009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 return p;
2011}
2012
2013/*
2014 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02002015 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 * Returns NULL in case of failure.
2017 */
2018 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02002019parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020{
2021 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002022 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023
2024 if (**arg != '<')
2025 {
2026 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02002027 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028 else
2029 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01002030 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 }
2032 *arg = skipwhite(*arg + 1);
2033
Bram Moolenaard77a8522020-04-03 21:59:57 +02002034 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035
2036 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002037 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 {
2039 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01002040 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002041 }
2042 ++*arg;
2043
2044 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002045 return get_list_type(member_type, type_gap);
2046 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047}
2048
2049/*
2050 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02002051 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 */
2053 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02002054parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055{
2056 char_u *p = *arg;
2057 size_t len;
2058
2059 // skip over the first word
2060 while (ASCII_ISALNUM(*p) || *p == '_')
2061 ++p;
2062 len = p - *arg;
2063
2064 switch (**arg)
2065 {
2066 case 'a':
2067 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
2068 {
2069 *arg += len;
2070 return &t_any;
2071 }
2072 break;
2073 case 'b':
2074 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
2075 {
2076 *arg += len;
2077 return &t_bool;
2078 }
2079 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
2080 {
2081 *arg += len;
2082 return &t_blob;
2083 }
2084 break;
2085 case 'c':
2086 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
2087 {
2088 *arg += len;
2089 return &t_channel;
2090 }
2091 break;
2092 case 'd':
2093 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
2094 {
2095 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002096 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 }
2098 break;
2099 case 'f':
2100 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
2101 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01002102#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002103 *arg += len;
2104 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01002105#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002106 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01002107 return &t_any;
2108#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 }
2110 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
2111 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002112 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002113 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002114 int argcount = -1;
2115 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002116 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002117 type_T *arg_type[MAX_FUNC_ARGS + 1];
2118
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002119 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002121 if (**arg == '(')
2122 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002123 // "func" may or may not return a value, "func()" does
2124 // not return a value.
2125 ret_type = &t_void;
2126
Bram Moolenaard77a8522020-04-03 21:59:57 +02002127 p = ++*arg;
2128 argcount = 0;
2129 while (*p != NUL && *p != ')')
2130 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002131 if (*p == '?')
2132 {
2133 if (first_optional == -1)
2134 first_optional = argcount;
2135 ++p;
2136 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002137 else if (STRNCMP(p, "...", 3) == 0)
2138 {
2139 flags |= TTFLAG_VARARGS;
2140 p += 3;
2141 }
Bram Moolenaar01865ad2020-07-26 18:33:09 +02002142 else if (first_optional != -1)
2143 {
2144 emsg(_("E1007: mandatory argument after optional argument"));
2145 return &t_any;
2146 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002147
2148 arg_type[argcount++] = parse_type(&p, type_gap);
2149
2150 // Nothing comes after "...{type}".
2151 if (flags & TTFLAG_VARARGS)
2152 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002153
Bram Moolenaard77a8522020-04-03 21:59:57 +02002154 if (*p != ',' && *skipwhite(p) == ',')
2155 {
2156 semsg(_(e_no_white_before), ",");
2157 return &t_any;
2158 }
2159 if (*p == ',')
2160 {
2161 ++p;
2162 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002163 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002164 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002165 return &t_any;
2166 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002167 }
2168 p = skipwhite(p);
2169 if (argcount == MAX_FUNC_ARGS)
2170 {
2171 emsg(_("E740: Too many argument types"));
2172 return &t_any;
2173 }
2174 }
2175
2176 p = skipwhite(p);
2177 if (*p != ')')
2178 {
2179 emsg(_(e_missing_close));
2180 return &t_any;
2181 }
2182 *arg = p + 1;
2183 }
2184 if (**arg == ':')
2185 {
2186 // parse return type
2187 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002188 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002189 semsg(_(e_white_after), ":");
2190 *arg = skipwhite(*arg);
2191 ret_type = parse_type(arg, type_gap);
2192 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002193 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002194 type = get_func_type(ret_type, argcount, type_gap);
2195 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002196 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002197 type = alloc_func_type(ret_type, argcount, type_gap);
2198 type->tt_flags = flags;
2199 if (argcount > 0)
2200 {
2201 type->tt_argcount = argcount;
2202 type->tt_min_argcount = first_optional == -1
2203 ? argcount : first_optional;
2204 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002205 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002206 return &t_any;
2207 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002208 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002209 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002210 }
2211 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 }
2213 break;
2214 case 'j':
2215 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2216 {
2217 *arg += len;
2218 return &t_job;
2219 }
2220 break;
2221 case 'l':
2222 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2223 {
2224 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002225 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226 }
2227 break;
2228 case 'n':
2229 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2230 {
2231 *arg += len;
2232 return &t_number;
2233 }
2234 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 case 's':
2236 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2237 {
2238 *arg += len;
2239 return &t_string;
2240 }
2241 break;
2242 case 'v':
2243 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2244 {
2245 *arg += len;
2246 return &t_void;
2247 }
2248 break;
2249 }
2250
2251 semsg(_("E1010: Type not recognized: %s"), *arg);
2252 return &t_any;
2253}
2254
2255/*
2256 * Check if "type1" and "type2" are exactly the same.
2257 */
2258 static int
2259equal_type(type_T *type1, type_T *type2)
2260{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002261 int i;
2262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 if (type1->tt_type != type2->tt_type)
2264 return FALSE;
2265 switch (type1->tt_type)
2266 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002268 case VAR_ANY:
2269 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 case VAR_SPECIAL:
2271 case VAR_BOOL:
2272 case VAR_NUMBER:
2273 case VAR_FLOAT:
2274 case VAR_STRING:
2275 case VAR_BLOB:
2276 case VAR_JOB:
2277 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002278 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 case VAR_LIST:
2280 case VAR_DICT:
2281 return equal_type(type1->tt_member, type2->tt_member);
2282 case VAR_FUNC:
2283 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002284 if (!equal_type(type1->tt_member, type2->tt_member)
2285 || type1->tt_argcount != type2->tt_argcount)
2286 return FALSE;
2287 if (type1->tt_argcount < 0
2288 || type1->tt_args == NULL || type2->tt_args == NULL)
2289 return TRUE;
2290 for (i = 0; i < type1->tt_argcount; ++i)
2291 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2292 return FALSE;
2293 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294 }
2295 return TRUE;
2296}
2297
2298/*
2299 * Find the common type of "type1" and "type2" and put it in "dest".
2300 * "type2" and "dest" may be the same.
2301 */
2302 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002303common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304{
2305 if (equal_type(type1, type2))
2306 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002307 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 return;
2309 }
2310
2311 if (type1->tt_type == type2->tt_type)
2312 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2314 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002315 type_T *common;
2316
Bram Moolenaard77a8522020-04-03 21:59:57 +02002317 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002318 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002319 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002320 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002321 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 return;
2323 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002324 if (type1->tt_type == VAR_FUNC)
2325 {
2326 type_T *common;
2327
2328 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2329 if (type1->tt_argcount == type2->tt_argcount
2330 && type1->tt_argcount >= 0)
2331 {
2332 int argcount = type1->tt_argcount;
2333 int i;
2334
2335 *dest = alloc_func_type(common, argcount, type_gap);
2336 if (type1->tt_args != NULL && type2->tt_args != NULL)
2337 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002338 if (func_type_add_arg_types(*dest, argcount,
2339 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002340 for (i = 0; i < argcount; ++i)
2341 common_type(type1->tt_args[i], type2->tt_args[i],
2342 &(*dest)->tt_args[i], type_gap);
2343 }
2344 }
2345 else
2346 *dest = alloc_func_type(common, -1, type_gap);
2347 return;
2348 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002349 }
2350
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002351 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352}
2353
2354 char *
2355vartype_name(vartype_T type)
2356{
2357 switch (type)
2358 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002359 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002360 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 case VAR_SPECIAL: return "special";
2363 case VAR_BOOL: return "bool";
2364 case VAR_NUMBER: return "number";
2365 case VAR_FLOAT: return "float";
2366 case VAR_STRING: return "string";
2367 case VAR_BLOB: return "blob";
2368 case VAR_JOB: return "job";
2369 case VAR_CHANNEL: return "channel";
2370 case VAR_LIST: return "list";
2371 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002372
2373 case VAR_FUNC:
2374 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002376 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377}
2378
2379/*
2380 * Return the name of a type.
2381 * The result may be in allocated memory, in which case "tofree" is set.
2382 */
2383 char *
2384type_name(type_T *type, char **tofree)
2385{
2386 char *name = vartype_name(type->tt_type);
2387
2388 *tofree = NULL;
2389 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2390 {
2391 char *member_free;
2392 char *member_name = type_name(type->tt_member, &member_free);
2393 size_t len;
2394
2395 len = STRLEN(name) + STRLEN(member_name) + 3;
2396 *tofree = alloc(len);
2397 if (*tofree != NULL)
2398 {
2399 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2400 vim_free(member_free);
2401 return *tofree;
2402 }
2403 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002404 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002405 {
2406 garray_T ga;
2407 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002408 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002409
2410 ga_init2(&ga, 1, 100);
2411 if (ga_grow(&ga, 20) == FAIL)
2412 return "[unknown]";
2413 *tofree = ga.ga_data;
2414 STRCPY(ga.ga_data, "func(");
2415 ga.ga_len += 5;
2416
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002417 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002418 {
2419 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002420 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002421 int len;
2422
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002423 if (type->tt_args == NULL)
2424 arg_type = "[unknown]";
2425 else
2426 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002427 if (i > 0)
2428 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002429 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002430 ga.ga_len += 2;
2431 }
2432 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002433 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002434 {
2435 vim_free(arg_free);
2436 return "[unknown]";
2437 }
2438 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002439 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002440 {
2441 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2442 ga.ga_len += 3;
2443 }
2444 else if (i >= type->tt_min_argcount)
2445 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002446 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002447 ga.ga_len += len;
2448 vim_free(arg_free);
2449 }
2450
2451 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002452 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002453 else
2454 {
2455 char *ret_free;
2456 char *ret_name = type_name(type->tt_member, &ret_free);
2457 int len;
2458
2459 len = (int)STRLEN(ret_name) + 4;
2460 if (ga_grow(&ga, len) == FAIL)
2461 {
2462 vim_free(ret_free);
2463 return "[unknown]";
2464 }
2465 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002466 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2467 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002468 vim_free(ret_free);
2469 }
2470 return ga.ga_data;
2471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472
2473 return name;
2474}
2475
2476/*
2477 * Find "name" in script-local items of script "sid".
2478 * Returns the index in "sn_var_vals" if found.
2479 * If found but not in "sn_var_vals" returns -1.
2480 * If not found returns -2.
2481 */
2482 int
2483get_script_item_idx(int sid, char_u *name, int check_writable)
2484{
2485 hashtab_T *ht;
2486 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002487 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 int idx;
2489
2490 // First look the name up in the hashtable.
2491 if (sid <= 0 || sid > script_items.ga_len)
2492 return -1;
2493 ht = &SCRIPT_VARS(sid);
2494 di = find_var_in_ht(ht, 0, name, TRUE);
2495 if (di == NULL)
2496 return -2;
2497
2498 // Now find the svar_T index in sn_var_vals.
2499 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2500 {
2501 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2502
2503 if (sv->sv_tv == &di->di_tv)
2504 {
2505 if (check_writable && sv->sv_const)
2506 semsg(_(e_readonlyvar), name);
2507 return idx;
2508 }
2509 }
2510 return -1;
2511}
2512
2513/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002514 * Find "name" in imported items of the current script or in "cctx" if not
2515 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 */
2517 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002518find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002520 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 int idx;
2522
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002523 if (current_sctx.sc_sid <= 0)
2524 return NULL;
2525 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 if (cctx != NULL)
2527 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2528 {
2529 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2530 + idx;
2531
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002532 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2533 : STRLEN(import->imp_name) == len
2534 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 return import;
2536 }
2537
2538 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2539 {
2540 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2541
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002542 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2543 : STRLEN(import->imp_name) == len
2544 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 return import;
2546 }
2547 return NULL;
2548}
2549
2550/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002551 * Free all imported variables.
2552 */
2553 static void
2554free_imported(cctx_T *cctx)
2555{
2556 int idx;
2557
2558 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2559 {
2560 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2561
2562 vim_free(import->imp_name);
2563 }
2564 ga_clear(&cctx->ctx_imports);
2565}
2566
2567/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002568 * Return TRUE if "p" points at a "#" but not at "#{".
2569 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002570 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002571vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002572{
2573 return p[0] == '#' && p[1] != '{';
2574}
2575
2576/*
2577 * Return a pointer to the next line that isn't empty or only contains a
2578 * comment. Skips over white space.
2579 * Returns NULL if there is none.
2580 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002581 char_u *
2582peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002583{
2584 int lnum = cctx->ctx_lnum;
2585
2586 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2587 {
2588 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002589 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002590
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002591 if (line == NULL)
2592 break;
2593 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002594 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002595 return p;
2596 }
2597 return NULL;
2598}
2599
2600/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002601 * Called when checking for a following operator at "arg". When the rest of
2602 * the line is empty or only a comment, peek the next line. If there is a next
2603 * line return a pointer to it and set "nextp".
2604 * Otherwise skip over white space.
2605 */
2606 static char_u *
2607may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2608{
2609 char_u *p = skipwhite(arg);
2610
2611 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002612 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002613 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002614 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002615 if (*nextp != NULL)
2616 return *nextp;
2617 }
2618 return p;
2619}
2620
2621/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002622 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002623 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002624 * Returns NULL when at the end.
2625 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002626 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002627next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002628{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002629 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002630
2631 do
2632 {
2633 ++cctx->ctx_lnum;
2634 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002635 {
2636 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002637 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002638 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002639 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002640 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002641 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002642 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002643 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002644 return line;
2645}
2646
2647/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002648 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002649 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002650 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2651 */
2652 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002653may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002654{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002655 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002656 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002657 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002658
2659 if (next == NULL)
2660 return FAIL;
2661 *arg = skipwhite(next);
2662 }
2663 return OK;
2664}
2665
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002666/*
2667 * Idem, and give an error when failed.
2668 */
2669 static int
2670may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2671{
2672 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2673 {
2674 emsg(_("E1097: line incomplete"));
2675 return FAIL;
2676 }
2677 return OK;
2678}
2679
2680
Bram Moolenaara5565e42020-05-09 15:44:01 +02002681// Structure passed between the compile_expr* functions to keep track of
2682// constants that have been parsed but for which no code was produced yet. If
2683// possible expressions on these constants are applied at compile time. If
2684// that is not possible, the code to push the constants needs to be generated
2685// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002686// Using 50 should be more than enough of 5 levels of ().
2687#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002688typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002689 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002690 int pp_used; // active entries in pp_tv[]
2691} ppconst_T;
2692
Bram Moolenaar1c747212020-05-09 18:28:34 +02002693static int compile_expr0(char_u **arg, cctx_T *cctx);
2694static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2695
Bram Moolenaara5565e42020-05-09 15:44:01 +02002696/*
2697 * Generate a PUSH instruction for "tv".
2698 * "tv" will be consumed or cleared.
2699 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2700 */
2701 static int
2702generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2703{
2704 if (tv != NULL)
2705 {
2706 switch (tv->v_type)
2707 {
2708 case VAR_UNKNOWN:
2709 break;
2710 case VAR_BOOL:
2711 generate_PUSHBOOL(cctx, tv->vval.v_number);
2712 break;
2713 case VAR_SPECIAL:
2714 generate_PUSHSPEC(cctx, tv->vval.v_number);
2715 break;
2716 case VAR_NUMBER:
2717 generate_PUSHNR(cctx, tv->vval.v_number);
2718 break;
2719#ifdef FEAT_FLOAT
2720 case VAR_FLOAT:
2721 generate_PUSHF(cctx, tv->vval.v_float);
2722 break;
2723#endif
2724 case VAR_BLOB:
2725 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2726 tv->vval.v_blob = NULL;
2727 break;
2728 case VAR_STRING:
2729 generate_PUSHS(cctx, tv->vval.v_string);
2730 tv->vval.v_string = NULL;
2731 break;
2732 default:
2733 iemsg("constant type not supported");
2734 clear_tv(tv);
2735 return FAIL;
2736 }
2737 tv->v_type = VAR_UNKNOWN;
2738 }
2739 return OK;
2740}
2741
2742/*
2743 * Generate code for any ppconst entries.
2744 */
2745 static int
2746generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2747{
2748 int i;
2749 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002750 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002751
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002752 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002753 for (i = 0; i < ppconst->pp_used; ++i)
2754 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2755 ret = FAIL;
2756 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002757 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002758 return ret;
2759}
2760
2761/*
2762 * Clear ppconst constants. Used when failing.
2763 */
2764 static void
2765clear_ppconst(ppconst_T *ppconst)
2766{
2767 int i;
2768
2769 for (i = 0; i < ppconst->pp_used; ++i)
2770 clear_tv(&ppconst->pp_tv[i]);
2771 ppconst->pp_used = 0;
2772}
2773
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002774/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002775 * Generate an instruction to load script-local variable "name", without the
2776 * leading "s:".
2777 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 */
2779 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002780compile_load_scriptvar(
2781 cctx_T *cctx,
2782 char_u *name, // variable NUL terminated
2783 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002784 char_u **end, // end of variable
2785 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002787 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2789 imported_T *import;
2790
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002791 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002792 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002793 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002794 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2795 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 }
2797 if (idx >= 0)
2798 {
2799 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2800
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002801 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 current_sctx.sc_sid, idx, sv->sv_type);
2803 return OK;
2804 }
2805
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002806 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 if (import != NULL)
2808 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002809 if (import->imp_all)
2810 {
2811 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002812 char_u *exp_name;
2813 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002814 ufunc_T *ufunc;
2815 type_T *type;
2816
2817 // Used "import * as Name", need to lookup the member.
2818 if (*p != '.')
2819 {
2820 semsg(_("E1060: expected dot after name: %s"), start);
2821 return FAIL;
2822 }
2823 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002824 if (VIM_ISWHITE(*p))
2825 {
2826 emsg(_("E1074: no white space allowed after dot"));
2827 return FAIL;
2828 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002829
Bram Moolenaar1c991142020-07-04 13:15:31 +02002830 // isolate one name
2831 exp_name = p;
2832 while (eval_isnamec(*p))
2833 ++p;
2834 cc = *p;
2835 *p = NUL;
2836
2837 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2838 *p = cc;
2839 p = skipwhite(p);
2840
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002841 // TODO: what if it is a function?
2842 if (idx < 0)
2843 return FAIL;
2844 *end = p;
2845
2846 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2847 import->imp_sid,
2848 idx,
2849 type);
2850 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002851 else if (import->imp_funcname != NULL)
2852 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002853 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002854 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2855 import->imp_sid,
2856 import->imp_var_vals_idx,
2857 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 return OK;
2859 }
2860
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002861 if (error)
2862 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 return FAIL;
2864}
2865
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002866 static int
2867generate_funcref(cctx_T *cctx, char_u *name)
2868{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002869 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002870
2871 if (ufunc == NULL)
2872 return FAIL;
2873
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002874 // Need to compile any default values to get the argument types.
2875 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2876 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2877 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002878 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002879}
2880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881/*
2882 * Compile a variable name into a load instruction.
2883 * "end" points to just after the name.
2884 * When "error" is FALSE do not give an error when not found.
2885 */
2886 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002887compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888{
2889 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002890 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002891 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002893 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894
2895 if (*(*arg + 1) == ':')
2896 {
2897 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002898 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002899 {
2900 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002902 switch (**arg)
2903 {
2904 case 'g': isn_type = ISN_LOADGDICT; break;
2905 case 'w': isn_type = ISN_LOADWDICT; break;
2906 case 't': isn_type = ISN_LOADTDICT; break;
2907 case 'b': isn_type = ISN_LOADBDICT; break;
2908 default:
2909 semsg(_(e_namespace), *arg);
2910 goto theend;
2911 }
2912 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2913 goto theend;
2914 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002915 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 else
2917 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002918 isntype_T isn_type = ISN_DROP;
2919
2920 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2921 if (name == NULL)
2922 return FAIL;
2923
2924 switch (**arg)
2925 {
2926 case 'v': res = generate_LOADV(cctx, name, error);
2927 break;
2928 case 's': res = compile_load_scriptvar(cctx, name,
2929 NULL, NULL, error);
2930 break;
2931 case 'g': isn_type = ISN_LOADG; break;
2932 case 'w': isn_type = ISN_LOADW; break;
2933 case 't': isn_type = ISN_LOADT; break;
2934 case 'b': isn_type = ISN_LOADB; break;
2935 default: semsg(_(e_namespace), *arg);
2936 goto theend;
2937 }
2938 if (isn_type != ISN_DROP)
2939 {
2940 // Global, Buffer-local, Window-local and Tabpage-local
2941 // variables can be defined later, thus we don't check if it
2942 // exists, give error at runtime.
2943 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2944 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 }
2946 }
2947 else
2948 {
2949 size_t len = end - *arg;
2950 int idx;
2951 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002952 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953
2954 name = vim_strnsave(*arg, end - *arg);
2955 if (name == NULL)
2956 return FAIL;
2957
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002958 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002960 if (!gen_load_outer)
2961 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 }
2963 else
2964 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002965 lvar_T *lvar = lookup_local(*arg, len, cctx);
2966
2967 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002969 type = lvar->lv_type;
2970 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002971 if (lvar->lv_from_outer)
2972 gen_load_outer = TRUE;
2973 else
2974 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 }
2976 else
2977 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002978 // "var" can be script-local even without using "s:" if it
2979 // already exists.
2980 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2981 == SCRIPT_VERSION_VIM9
2982 || lookup_script(*arg, len) == OK)
2983 res = compile_load_scriptvar(cctx, name, *arg, &end,
2984 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002985
Bram Moolenaara5565e42020-05-09 15:44:01 +02002986 // When the name starts with an uppercase letter or "x:" it
2987 // can be a user defined function.
2988 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2989 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 }
2991 }
2992 if (gen_load)
2993 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002994 if (gen_load_outer)
2995 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 }
2997
2998 *arg = end;
2999
3000theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003001 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 semsg(_(e_var_notfound), name);
3003 vim_free(name);
3004 return res;
3005}
3006
3007/*
3008 * Compile the argument expressions.
3009 * "arg" points to just after the "(" and is advanced to after the ")"
3010 */
3011 static int
3012compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
3013{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003014 char_u *p = *arg;
3015 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016
Bram Moolenaare6085c52020-04-12 20:19:16 +02003017 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003019 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3020 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003021 if (*p == ')')
3022 {
3023 *arg = p + 1;
3024 return OK;
3025 }
3026
Bram Moolenaara5565e42020-05-09 15:44:01 +02003027 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 return FAIL;
3029 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003030
3031 if (*p != ',' && *skipwhite(p) == ',')
3032 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02003033 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003034 p = skipwhite(p);
3035 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003037 {
3038 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003039 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02003040 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003041 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003042 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003043 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003045failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003046 emsg(_(e_missing_close));
3047 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048}
3049
3050/*
3051 * Compile a function call: name(arg1, arg2)
3052 * "arg" points to "name", "arg + varlen" to the "(".
3053 * "argcount_init" is 1 for "value->method()"
3054 * Instructions:
3055 * EVAL arg1
3056 * EVAL arg2
3057 * BCALL / DCALL / UCALL
3058 */
3059 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003060compile_call(
3061 char_u **arg,
3062 size_t varlen,
3063 cctx_T *cctx,
3064 ppconst_T *ppconst,
3065 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066{
3067 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003068 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 int argcount = argcount_init;
3070 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003071 char_u fname_buf[FLEN_FIXED + 1];
3072 char_u *tofree = NULL;
3073 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003075 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076
Bram Moolenaara5565e42020-05-09 15:44:01 +02003077 // we can evaluate "has('name')" at compile time
3078 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3079 {
3080 char_u *s = skipwhite(*arg + varlen + 1);
3081 typval_T argvars[2];
3082
3083 argvars[0].v_type = VAR_UNKNOWN;
3084 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003085 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003086 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003087 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003088 s = skipwhite(s);
3089 if (*s == ')' && argvars[0].v_type == VAR_STRING)
3090 {
3091 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3092
3093 *arg = s + 1;
3094 argvars[1].v_type = VAR_UNKNOWN;
3095 tv->v_type = VAR_NUMBER;
3096 tv->vval.v_number = 0;
3097 f_has(argvars, tv);
3098 clear_tv(&argvars[0]);
3099 ++ppconst->pp_used;
3100 return OK;
3101 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003102 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003103 }
3104
3105 if (generate_ppconst(cctx, ppconst) == FAIL)
3106 return FAIL;
3107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 if (varlen >= sizeof(namebuf))
3109 {
3110 semsg(_("E1011: name too long: %s"), name);
3111 return FAIL;
3112 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003113 vim_strncpy(namebuf, *arg, varlen);
3114 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115
3116 *arg = skipwhite(*arg + varlen + 1);
3117 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003118 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003120 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 {
3122 int idx;
3123
3124 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003125 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02003127 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003128 else
3129 semsg(_(e_unknownfunc), namebuf);
3130 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 }
3132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003134 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003136 {
3137 res = generate_CALL(cctx, ufunc, argcount);
3138 goto theend;
3139 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140
3141 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003142 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003144 if (STRNCMP(namebuf, "g:", 2) != 0
3145 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003146 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003147 garray_T *stack = &cctx->ctx_type_stack;
3148 type_T *type;
3149
3150 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3151 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003152 goto theend;
3153 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003155 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003156 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003157 if (STRNCMP(namebuf, "g:", 2) == 0)
3158 res = generate_UCALL(cctx, name, argcount);
3159 else
3160 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003161
3162theend:
3163 vim_free(tofree);
3164 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165}
3166
3167// like NAMESPACE_CHAR but with 'a' and 'l'.
3168#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3169
3170/*
3171 * Find the end of a variable or function name. Unlike find_name_end() this
3172 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003173 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 * Return a pointer to just after the name. Equal to "arg" if there is no
3175 * valid name.
3176 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003177 static char_u *
3178to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179{
3180 char_u *p;
3181
3182 // Quick check for valid starting character.
3183 if (!eval_isnamec1(*arg))
3184 return arg;
3185
3186 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3187 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3188 // and can be used in slice "[n:]".
3189 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003190 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3192 break;
3193 return p;
3194}
3195
3196/*
3197 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003198 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 */
3200 char_u *
3201to_name_const_end(char_u *arg)
3202{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003203 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 typval_T rettv;
3205
3206 if (p == arg && *arg == '[')
3207 {
3208
3209 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003210 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 p = arg;
3212 }
3213 else if (p == arg && *arg == '#' && arg[1] == '{')
3214 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003215 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003217 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 p = arg;
3219 }
3220 else if (p == arg && *arg == '{')
3221 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003222 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003224 // Can be "{x -> ret}()".
3225 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003227 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 if (ret != OK)
3229 p = arg;
3230 }
3231
3232 return p;
3233}
3234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235/*
3236 * parse a list: [expr, expr]
3237 * "*arg" points to the '['.
3238 */
3239 static int
3240compile_list(char_u **arg, cctx_T *cctx)
3241{
3242 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003243 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 int count = 0;
3245
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003246 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003248 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003249 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003250 semsg(_(e_list_end), *arg);
3251 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003252 }
3253 if (*p == ']')
3254 {
3255 ++p;
3256 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003257 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003258 p += STRLEN(p);
3259 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003260 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003261 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 break;
3263 ++count;
3264 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003265 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003267 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3268 {
3269 semsg(_(e_white_after), ",");
3270 return FAIL;
3271 }
3272 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003273 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 p = skipwhite(p);
3275 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003276 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277
3278 generate_NEWLIST(cctx, count);
3279 return OK;
3280}
3281
3282/*
3283 * parse a lambda: {arg, arg -> expr}
3284 * "*arg" points to the '{'.
3285 */
3286 static int
3287compile_lambda(char_u **arg, cctx_T *cctx)
3288{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 typval_T rettv;
3290 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003291 evalarg_T evalarg;
3292
3293 CLEAR_FIELD(evalarg);
3294 evalarg.eval_flags = EVAL_EVALUATE;
3295 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296
3297 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003298 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003302 ++ufunc->uf_refcount;
3303 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003304 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305
3306 // The function will have one line: "return {expr}".
3307 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003308 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003310 clear_evalarg(&evalarg, NULL);
3311
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003312 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003313 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003314
3315 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 return FAIL;
3317}
3318
3319/*
3320 * Compile a lamda call: expr->{lambda}(args)
3321 * "arg" points to the "{".
3322 */
3323 static int
3324compile_lambda_call(char_u **arg, cctx_T *cctx)
3325{
3326 ufunc_T *ufunc;
3327 typval_T rettv;
3328 int argcount = 1;
3329 int ret = FAIL;
3330
3331 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003332 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 return FAIL;
3334
3335 if (**arg != '(')
3336 {
3337 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003338 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 else
3340 semsg(_(e_missing_paren), "lambda");
3341 clear_tv(&rettv);
3342 return FAIL;
3343 }
3344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 ufunc = rettv.vval.v_partial->pt_func;
3346 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003347 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003348 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003349
3350 // The function will have one line: "return {expr}".
3351 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003352 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353
3354 // compile the arguments
3355 *arg = skipwhite(*arg + 1);
3356 if (compile_arguments(arg, cctx, &argcount) == OK)
3357 // call the compiled function
3358 ret = generate_CALL(cctx, ufunc, argcount);
3359
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003360 if (ret == FAIL)
3361 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 return ret;
3363}
3364
3365/*
3366 * parse a dict: {'key': val} or #{key: val}
3367 * "*arg" points to the '{'.
3368 */
3369 static int
3370compile_dict(char_u **arg, cctx_T *cctx, int literal)
3371{
3372 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003373 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 int count = 0;
3375 dict_T *d = dict_alloc();
3376 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003377 char_u *whitep = *arg;
3378 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379
3380 if (d == NULL)
3381 return FAIL;
3382 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003383 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 {
3385 char_u *key = NULL;
3386
Bram Moolenaar23c55272020-06-21 16:58:13 +02003387 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003388 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003389 *arg = NULL;
3390 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003391 }
3392
3393 if (**arg == '}')
3394 break;
3395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 if (literal)
3397 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003398 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399
Bram Moolenaar2c330432020-04-13 14:41:35 +02003400 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 {
3402 semsg(_("E1014: Invalid key: %s"), *arg);
3403 return FAIL;
3404 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003405 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 if (generate_PUSHS(cctx, key) == FAIL)
3407 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003408 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 }
3410 else
3411 {
3412 isn_T *isn;
3413
Bram Moolenaara5565e42020-05-09 15:44:01 +02003414 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3417 if (isn->isn_type == ISN_PUSHS)
3418 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003419 else
3420 {
3421 type_T *keytype = ((type_T **)stack->ga_data)
3422 [stack->ga_len - 1];
3423 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
3424 return FAIL;
3425 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 }
3427
3428 // Check for duplicate keys, if using string keys.
3429 if (key != NULL)
3430 {
3431 item = dict_find(d, key, -1);
3432 if (item != NULL)
3433 {
3434 semsg(_(e_duplicate_key), key);
3435 goto failret;
3436 }
3437 item = dictitem_alloc(key);
3438 if (item != NULL)
3439 {
3440 item->di_tv.v_type = VAR_UNKNOWN;
3441 item->di_tv.v_lock = 0;
3442 if (dict_add(d, item) == FAIL)
3443 dictitem_free(item);
3444 }
3445 }
3446
3447 *arg = skipwhite(*arg);
3448 if (**arg != ':')
3449 {
3450 semsg(_(e_missing_dict_colon), *arg);
3451 return FAIL;
3452 }
3453
Bram Moolenaar2c330432020-04-13 14:41:35 +02003454 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003456 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003457 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003458 *arg = NULL;
3459 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003460 }
3461
Bram Moolenaara5565e42020-05-09 15:44:01 +02003462 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 return FAIL;
3464 ++count;
3465
Bram Moolenaar2c330432020-04-13 14:41:35 +02003466 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003467 *arg = skipwhite(*arg);
3468 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003469 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003470 *arg = NULL;
3471 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003472 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 if (**arg == '}')
3474 break;
3475 if (**arg != ',')
3476 {
3477 semsg(_(e_missing_dict_comma), *arg);
3478 goto failret;
3479 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003480 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 *arg = skipwhite(*arg + 1);
3482 }
3483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 *arg = *arg + 1;
3485
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003486 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003487 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003488 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003489 *arg += STRLEN(*arg);
3490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 dict_unref(d);
3492 return generate_NEWDICT(cctx, count);
3493
3494failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003495 if (*arg == NULL)
3496 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 dict_unref(d);
3498 return FAIL;
3499}
3500
3501/*
3502 * Compile "&option".
3503 */
3504 static int
3505compile_get_option(char_u **arg, cctx_T *cctx)
3506{
3507 typval_T rettv;
3508 char_u *start = *arg;
3509 int ret;
3510
3511 // parse the option and get the current value to get the type.
3512 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003513 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 if (ret == OK)
3515 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003516 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 char_u *name = vim_strnsave(start, *arg - start);
3518 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3519
3520 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3521 vim_free(name);
3522 }
3523 clear_tv(&rettv);
3524
3525 return ret;
3526}
3527
3528/*
3529 * Compile "$VAR".
3530 */
3531 static int
3532compile_get_env(char_u **arg, cctx_T *cctx)
3533{
3534 char_u *start = *arg;
3535 int len;
3536 int ret;
3537 char_u *name;
3538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 ++*arg;
3540 len = get_env_len(arg);
3541 if (len == 0)
3542 {
3543 semsg(_(e_syntax_at), start - 1);
3544 return FAIL;
3545 }
3546
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003547 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 name = vim_strnsave(start, len + 1);
3549 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3550 vim_free(name);
3551 return ret;
3552}
3553
3554/*
3555 * Compile "@r".
3556 */
3557 static int
3558compile_get_register(char_u **arg, cctx_T *cctx)
3559{
3560 int ret;
3561
3562 ++*arg;
3563 if (**arg == NUL)
3564 {
3565 semsg(_(e_syntax_at), *arg - 1);
3566 return FAIL;
3567 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003568 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 {
3570 emsg_invreg(**arg);
3571 return FAIL;
3572 }
3573 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3574 ++*arg;
3575 return ret;
3576}
3577
3578/*
3579 * Apply leading '!', '-' and '+' to constant "rettv".
3580 */
3581 static int
3582apply_leader(typval_T *rettv, char_u *start, char_u *end)
3583{
3584 char_u *p = end;
3585
3586 // this works from end to start
3587 while (p > start)
3588 {
3589 --p;
3590 if (*p == '-' || *p == '+')
3591 {
3592 // only '-' has an effect, for '+' we only check the type
3593#ifdef FEAT_FLOAT
3594 if (rettv->v_type == VAR_FLOAT)
3595 {
3596 if (*p == '-')
3597 rettv->vval.v_float = -rettv->vval.v_float;
3598 }
3599 else
3600#endif
3601 {
3602 varnumber_T val;
3603 int error = FALSE;
3604
3605 // tv_get_number_chk() accepts a string, but we don't want that
3606 // here
3607 if (check_not_string(rettv) == FAIL)
3608 return FAIL;
3609 val = tv_get_number_chk(rettv, &error);
3610 clear_tv(rettv);
3611 if (error)
3612 return FAIL;
3613 if (*p == '-')
3614 val = -val;
3615 rettv->v_type = VAR_NUMBER;
3616 rettv->vval.v_number = val;
3617 }
3618 }
3619 else
3620 {
3621 int v = tv2bool(rettv);
3622
3623 // '!' is permissive in the type.
3624 clear_tv(rettv);
3625 rettv->v_type = VAR_BOOL;
3626 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3627 }
3628 }
3629 return OK;
3630}
3631
3632/*
3633 * Recognize v: variables that are constants and set "rettv".
3634 */
3635 static void
3636get_vim_constant(char_u **arg, typval_T *rettv)
3637{
3638 if (STRNCMP(*arg, "v:true", 6) == 0)
3639 {
3640 rettv->v_type = VAR_BOOL;
3641 rettv->vval.v_number = VVAL_TRUE;
3642 *arg += 6;
3643 }
3644 else if (STRNCMP(*arg, "v:false", 7) == 0)
3645 {
3646 rettv->v_type = VAR_BOOL;
3647 rettv->vval.v_number = VVAL_FALSE;
3648 *arg += 7;
3649 }
3650 else if (STRNCMP(*arg, "v:null", 6) == 0)
3651 {
3652 rettv->v_type = VAR_SPECIAL;
3653 rettv->vval.v_number = VVAL_NULL;
3654 *arg += 6;
3655 }
3656 else if (STRNCMP(*arg, "v:none", 6) == 0)
3657 {
3658 rettv->v_type = VAR_SPECIAL;
3659 rettv->vval.v_number = VVAL_NONE;
3660 *arg += 6;
3661 }
3662}
3663
Bram Moolenaar696ba232020-07-29 21:20:41 +02003664 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003665get_compare_type(char_u *p, int *len, int *type_is)
3666{
3667 exptype_T type = EXPR_UNKNOWN;
3668 int i;
3669
3670 switch (p[0])
3671 {
3672 case '=': if (p[1] == '=')
3673 type = EXPR_EQUAL;
3674 else if (p[1] == '~')
3675 type = EXPR_MATCH;
3676 break;
3677 case '!': if (p[1] == '=')
3678 type = EXPR_NEQUAL;
3679 else if (p[1] == '~')
3680 type = EXPR_NOMATCH;
3681 break;
3682 case '>': if (p[1] != '=')
3683 {
3684 type = EXPR_GREATER;
3685 *len = 1;
3686 }
3687 else
3688 type = EXPR_GEQUAL;
3689 break;
3690 case '<': if (p[1] != '=')
3691 {
3692 type = EXPR_SMALLER;
3693 *len = 1;
3694 }
3695 else
3696 type = EXPR_SEQUAL;
3697 break;
3698 case 'i': if (p[1] == 's')
3699 {
3700 // "is" and "isnot"; but not a prefix of a name
3701 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3702 *len = 5;
3703 i = p[*len];
3704 if (!isalnum(i) && i != '_')
3705 {
3706 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3707 *type_is = TRUE;
3708 }
3709 }
3710 break;
3711 }
3712 return type;
3713}
3714
3715/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 * Compile code to apply '-', '+' and '!'.
3717 */
3718 static int
3719compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3720{
3721 char_u *p = end;
3722
3723 // this works from end to start
3724 while (p > start)
3725 {
3726 --p;
3727 if (*p == '-' || *p == '+')
3728 {
3729 int negate = *p == '-';
3730 isn_T *isn;
3731
3732 // TODO: check type
3733 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3734 {
3735 --p;
3736 if (*p == '-')
3737 negate = !negate;
3738 }
3739 // only '-' has an effect, for '+' we only check the type
3740 if (negate)
3741 isn = generate_instr(cctx, ISN_NEGATENR);
3742 else
3743 isn = generate_instr(cctx, ISN_CHECKNR);
3744 if (isn == NULL)
3745 return FAIL;
3746 }
3747 else
3748 {
3749 int invert = TRUE;
3750
3751 while (p > start && p[-1] == '!')
3752 {
3753 --p;
3754 invert = !invert;
3755 }
3756 if (generate_2BOOL(cctx, invert) == FAIL)
3757 return FAIL;
3758 }
3759 }
3760 return OK;
3761}
3762
3763/*
3764 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003765 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 */
3767 static int
3768compile_subscript(
3769 char_u **arg,
3770 cctx_T *cctx,
3771 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003772 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003773 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774{
3775 for (;;)
3776 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003777 char_u *p = skipwhite(*arg);
3778
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003779 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003780 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003781 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003782
3783 // If a following line starts with "->{" or "->X" advance to that
3784 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003785 // Also if a following line starts with ".x".
3786 if (next != NULL &&
3787 ((next[0] == '-' && next[1] == '>'
3788 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003789 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003790 {
3791 next = next_line_from_context(cctx, TRUE);
3792 if (next == NULL)
3793 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003794 *arg = next;
3795 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003796 }
3797 }
3798
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003799 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3800 // not a function call.
3801 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003803 garray_T *stack = &cctx->ctx_type_stack;
3804 type_T *type;
3805 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003807 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003808 return FAIL;
3809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003811 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3812
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003813 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3815 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003816 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 return FAIL;
3818 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003819 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003821 char_u *pstart = p;
3822
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003823 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003824 return FAIL;
3825
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 // something->method()
3827 // Apply the '!', '-' and '+' first:
3828 // -1.0->func() works like (-1.0)->func()
3829 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3830 return FAIL;
3831 *start_leader = end_leader; // don't apply again later
3832
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003833 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003834 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003835 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 if (**arg == '{')
3837 {
3838 // lambda call: list->{lambda}
3839 if (compile_lambda_call(arg, cctx) == FAIL)
3840 return FAIL;
3841 }
3842 else
3843 {
3844 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003845 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003846 if (!eval_isnamec1(*p))
3847 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003848 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003849 return FAIL;
3850 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003851 if (ASCII_ISALPHA(*p) && p[1] == ':')
3852 p += 2;
3853 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 ;
3855 if (*p != '(')
3856 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003857 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 return FAIL;
3859 }
3860 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003861 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 return FAIL;
3863 }
3864 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003865 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003867 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003868 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003869 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003870
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003871 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003872 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003873 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003874 // TODO: blob index
3875 // TODO: more arguments
3876 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003877 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003878 return FAIL;
3879
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003880 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003881 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003882 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003883 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003884 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 return FAIL;
3886
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003887 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3888 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 if (**arg != ']')
3890 {
3891 emsg(_(e_missbrac));
3892 return FAIL;
3893 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003894 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003896 // We can index a list and a dict. If we don't know the type
3897 // we can use the index value type.
3898 // TODO: If we don't know use an instruction to figure it out at
3899 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003900 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003901 vtype = (*typep)->tt_type;
3902 if (*typep == &t_any)
3903 {
3904 type_T *valtype = ((type_T **)stack->ga_data)
3905 [stack->ga_len - 1];
3906 if (valtype == &t_string)
3907 vtype = VAR_DICT;
3908 }
3909 if (vtype == VAR_DICT)
3910 {
3911 if ((*typep)->tt_type == VAR_DICT)
3912 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003913 else
3914 {
3915 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3916 return FAIL;
3917 *typep = &t_any;
3918 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003919 if (may_generate_2STRING(-1, cctx) == FAIL)
3920 return FAIL;
3921 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3922 return FAIL;
3923 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003924 else if (vtype == VAR_STRING)
3925 {
3926 *typep = &t_number;
3927 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3928 return FAIL;
3929 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003930 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003931 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003932 if ((*typep)->tt_type == VAR_LIST)
3933 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003934 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003935 return FAIL;
3936 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003937 else
3938 {
3939 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003940 return FAIL;
3941 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003943 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003945 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003946 return FAIL;
3947
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003948 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003949 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3950 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003952 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003953 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 while (eval_isnamec(*p))
3955 MB_PTR_ADV(p);
3956 if (p == *arg)
3957 {
3958 semsg(_(e_syntax_at), *arg);
3959 return FAIL;
3960 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003961 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 return FAIL;
3963 *arg = p;
3964 }
3965 else
3966 break;
3967 }
3968
3969 // TODO - see handle_subscript():
3970 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3971 // Don't do this when "Func" is already a partial that was bound
3972 // explicitly (pt_auto is FALSE).
3973
3974 return OK;
3975}
3976
3977/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003978 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3979 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003981 * If the value is a constant "ppconst->pp_ret" will be set.
3982 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003983 *
3984 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 */
3986
3987/*
3988 * number number constant
3989 * 0zFFFFFFFF Blob constant
3990 * "string" string constant
3991 * 'string' literal string constant
3992 * &option-name option value
3993 * @r register contents
3994 * identifier variable value
3995 * function() function call
3996 * $VAR environment variable
3997 * (expression) nested expression
3998 * [expr, expr] List
3999 * {key: val, key: val} Dictionary
4000 * #{key: val, key: val} Dictionary with literal keys
4001 *
4002 * Also handle:
4003 * ! in front logical NOT
4004 * - in front unary minus
4005 * + in front unary plus (ignored)
4006 * trailing (arg) funcref/partial call
4007 * trailing [] subscript in String or List
4008 * trailing .name entry in Dictionary
4009 * trailing ->name() method call
4010 */
4011 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004012compile_expr7(
4013 char_u **arg,
4014 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004015 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 char_u *start_leader, *end_leader;
4018 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004019 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004020 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021
4022 /*
4023 * Skip '!', '-' and '+' characters. They are handled later.
4024 */
4025 start_leader = *arg;
4026 while (**arg == '!' || **arg == '-' || **arg == '+')
4027 *arg = skipwhite(*arg + 1);
4028 end_leader = *arg;
4029
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004030 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 switch (**arg)
4032 {
4033 /*
4034 * Number constant.
4035 */
4036 case '0': // also for blob starting with 0z
4037 case '1':
4038 case '2':
4039 case '3':
4040 case '4':
4041 case '5':
4042 case '6':
4043 case '7':
4044 case '8':
4045 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004046 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 return FAIL;
4048 break;
4049
4050 /*
4051 * String constant: "string".
4052 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004053 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 return FAIL;
4055 break;
4056
4057 /*
4058 * Literal string constant: 'str''ing'.
4059 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004060 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 return FAIL;
4062 break;
4063
4064 /*
4065 * Constant Vim variable.
4066 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004067 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 ret = NOTDONE;
4069 break;
4070
4071 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004072 * "true" constant
4073 */
4074 case 't': if (STRNCMP(*arg, "true", 4) == 0
4075 && !eval_isnamec((*arg)[4]))
4076 {
4077 *arg += 4;
4078 rettv->v_type = VAR_BOOL;
4079 rettv->vval.v_number = VVAL_TRUE;
4080 }
4081 else
4082 ret = NOTDONE;
4083 break;
4084
4085 /*
4086 * "false" constant
4087 */
4088 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4089 && !eval_isnamec((*arg)[5]))
4090 {
4091 *arg += 5;
4092 rettv->v_type = VAR_BOOL;
4093 rettv->vval.v_number = VVAL_FALSE;
4094 }
4095 else
4096 ret = NOTDONE;
4097 break;
4098
4099 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 * List: [expr, expr]
4101 */
4102 case '[': ret = compile_list(arg, cctx);
4103 break;
4104
4105 /*
4106 * Dictionary: #{key: val, key: val}
4107 */
4108 case '#': if ((*arg)[1] == '{')
4109 {
4110 ++*arg;
4111 ret = compile_dict(arg, cctx, TRUE);
4112 }
4113 else
4114 ret = NOTDONE;
4115 break;
4116
4117 /*
4118 * Lambda: {arg, arg -> expr}
4119 * Dictionary: {'key': val, 'key': val}
4120 */
4121 case '{': {
4122 char_u *start = skipwhite(*arg + 1);
4123
4124 // Find out what comes after the arguments.
4125 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004126 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 if (ret != FAIL && *start == '>')
4128 ret = compile_lambda(arg, cctx);
4129 else
4130 ret = compile_dict(arg, cctx, FALSE);
4131 }
4132 break;
4133
4134 /*
4135 * Option value: &name
4136 */
4137 case '&': ret = compile_get_option(arg, cctx);
4138 break;
4139
4140 /*
4141 * Environment variable: $VAR.
4142 */
4143 case '$': ret = compile_get_env(arg, cctx);
4144 break;
4145
4146 /*
4147 * Register contents: @r.
4148 */
4149 case '@': ret = compile_get_register(arg, cctx);
4150 break;
4151 /*
4152 * nested expression: (expression).
4153 */
4154 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02004155
4156 // recursive!
4157 if (ppconst->pp_used <= PPSIZE - 10)
4158 {
4159 ret = compile_expr1(arg, cctx, ppconst);
4160 }
4161 else
4162 {
4163 // Not enough space in ppconst, flush constants.
4164 if (generate_ppconst(cctx, ppconst) == FAIL)
4165 return FAIL;
4166 ret = compile_expr0(arg, cctx);
4167 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 *arg = skipwhite(*arg);
4169 if (**arg == ')')
4170 ++*arg;
4171 else if (ret == OK)
4172 {
4173 emsg(_(e_missing_close));
4174 ret = FAIL;
4175 }
4176 break;
4177
4178 default: ret = NOTDONE;
4179 break;
4180 }
4181 if (ret == FAIL)
4182 return FAIL;
4183
Bram Moolenaar1c747212020-05-09 18:28:34 +02004184 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 {
4186 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004187 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004189 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 return FAIL;
4191 }
4192 start_leader = end_leader; // don't apply again below
4193
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004194 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004195 clear_tv(rettv);
4196 else
4197 // A constant expression can possibly be handled compile time,
4198 // return the value instead of generating code.
4199 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200 }
4201 else if (ret == NOTDONE)
4202 {
4203 char_u *p;
4204 int r;
4205
4206 if (!eval_isnamec1(**arg))
4207 {
4208 semsg(_("E1015: Name expected: %s"), *arg);
4209 return FAIL;
4210 }
4211
4212 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004213 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004215 {
4216 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004219 {
4220 if (generate_ppconst(cctx, ppconst) == FAIL)
4221 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004223 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 if (r == FAIL)
4225 return FAIL;
4226 }
4227
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004228 // Handle following "[]", ".member", etc.
4229 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004230 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004231 ppconst) == FAIL)
4232 return FAIL;
4233 if (ppconst->pp_used > 0)
4234 {
4235 // apply the '!', '-' and '+' before the constant
4236 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4237 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4238 return FAIL;
4239 return OK;
4240 }
4241 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004243 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244}
4245
4246/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004247 * Give the "white on both sides" error, taking the operator from "p[len]".
4248 */
4249 void
4250error_white_both(char_u *op, int len)
4251{
4252 char_u buf[10];
4253
4254 vim_strncpy(buf, op, len);
4255 semsg(_(e_white_both), buf);
4256}
4257
4258/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 * * number multiplication
4260 * / number division
4261 * % number modulo
4262 */
4263 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004264compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265{
4266 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004267 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004268 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004270 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004271 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 return FAIL;
4273
4274 /*
4275 * Repeat computing, until no "*", "/" or "%" is following.
4276 */
4277 for (;;)
4278 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004279 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 if (*op != '*' && *op != '/' && *op != '%')
4281 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004282 if (next != NULL)
4283 {
4284 *arg = next_line_from_context(cctx, TRUE);
4285 op = skipwhite(*arg);
4286 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004287
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004288 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004290 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004291 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292 }
4293 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004294 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004295 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004297 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004298 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004300
4301 if (ppconst->pp_used == ppconst_used + 2
4302 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4303 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004304 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004305 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4306 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004307 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004309 // both are numbers: compute the result
4310 switch (*op)
4311 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004312 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004313 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004314 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004315 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004316 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004317 break;
4318 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004319 tv1->vval.v_number = res;
4320 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004321 }
4322 else
4323 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004324 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004325 generate_two_op(cctx, op);
4326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 }
4328
4329 return OK;
4330}
4331
4332/*
4333 * + number addition
4334 * - number subtraction
4335 * .. string concatenation
4336 */
4337 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004338compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339{
4340 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004341 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004343 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344
4345 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004346 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347 return FAIL;
4348
4349 /*
4350 * Repeat computing, until no "+", "-" or ".." is following.
4351 */
4352 for (;;)
4353 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004354 op = may_peek_next_line(cctx, *arg, &next);
4355 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 break;
4357 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004358 if (next != NULL)
4359 {
4360 *arg = next_line_from_context(cctx, TRUE);
4361 op = skipwhite(*arg);
4362 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004364 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004366 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004367 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368 }
4369
4370 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004371 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004372 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004374 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004375 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 return FAIL;
4377
Bram Moolenaara5565e42020-05-09 15:44:01 +02004378 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004379 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004380 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4381 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4382 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4383 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004385 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4386 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004387
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004388 // concat/subtract/add constant numbers
4389 if (*op == '+')
4390 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4391 else if (*op == '-')
4392 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4393 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004394 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004395 // concatenate constant strings
4396 char_u *s1 = tv1->vval.v_string;
4397 char_u *s2 = tv2->vval.v_string;
4398 size_t len1 = STRLEN(s1);
4399
4400 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4401 if (tv1->vval.v_string == NULL)
4402 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004403 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004404 return FAIL;
4405 }
4406 mch_memmove(tv1->vval.v_string, s1, len1);
4407 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004408 vim_free(s1);
4409 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004410 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004411 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 }
4413 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004414 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004415 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004416 if (*op == '.')
4417 {
4418 if (may_generate_2STRING(-2, cctx) == FAIL
4419 || may_generate_2STRING(-1, cctx) == FAIL)
4420 return FAIL;
4421 generate_instr_drop(cctx, ISN_CONCAT, 1);
4422 }
4423 else
4424 generate_two_op(cctx, op);
4425 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 }
4427
4428 return OK;
4429}
4430
4431/*
4432 * expr5a == expr5b
4433 * expr5a =~ expr5b
4434 * expr5a != expr5b
4435 * expr5a !~ expr5b
4436 * expr5a > expr5b
4437 * expr5a >= expr5b
4438 * expr5a < expr5b
4439 * expr5a <= expr5b
4440 * expr5a is expr5b
4441 * expr5a isnot expr5b
4442 *
4443 * Produces instructions:
4444 * EVAL expr5a Push result of "expr5a"
4445 * EVAL expr5b Push result of "expr5b"
4446 * COMPARE one of the compare instructions
4447 */
4448 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004449compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450{
4451 exptype_T type = EXPR_UNKNOWN;
4452 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004453 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004456 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457
4458 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004459 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 return FAIL;
4461
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004462 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004463 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464
4465 /*
4466 * If there is a comparative operator, use it.
4467 */
4468 if (type != EXPR_UNKNOWN)
4469 {
4470 int ic = FALSE; // Default: do not ignore case
4471
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004472 if (next != NULL)
4473 {
4474 *arg = next_line_from_context(cctx, TRUE);
4475 p = skipwhite(*arg);
4476 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477 if (type_is && (p[len] == '?' || p[len] == '#'))
4478 {
4479 semsg(_(e_invexpr2), *arg);
4480 return FAIL;
4481 }
4482 // extra question mark appended: ignore case
4483 if (p[len] == '?')
4484 {
4485 ic = TRUE;
4486 ++len;
4487 }
4488 // extra '#' appended: match case (ignored)
4489 else if (p[len] == '#')
4490 ++len;
4491 // nothing appended: match case
4492
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004493 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004495 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004496 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497 }
4498
4499 // get the second variable
4500 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004501 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004502 return FAIL;
4503
Bram Moolenaara5565e42020-05-09 15:44:01 +02004504 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505 return FAIL;
4506
Bram Moolenaara5565e42020-05-09 15:44:01 +02004507 if (ppconst->pp_used == ppconst_used + 2)
4508 {
4509 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4510 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4511 int ret;
4512
4513 // Both sides are a constant, compute the result now.
4514 // First check for a valid combination of types, this is more
4515 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004516 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004517 ret = FAIL;
4518 else
4519 {
4520 ret = typval_compare(tv1, tv2, type, ic);
4521 tv1->v_type = VAR_BOOL;
4522 tv1->vval.v_number = tv1->vval.v_number
4523 ? VVAL_TRUE : VVAL_FALSE;
4524 clear_tv(tv2);
4525 --ppconst->pp_used;
4526 }
4527 return ret;
4528 }
4529
4530 generate_ppconst(cctx, ppconst);
4531 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532 }
4533
4534 return OK;
4535}
4536
Bram Moolenaar7f141552020-05-09 17:35:53 +02004537static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539/*
4540 * Compile || or &&.
4541 */
4542 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004543compile_and_or(
4544 char_u **arg,
4545 cctx_T *cctx,
4546 char *op,
4547 ppconst_T *ppconst,
4548 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004550 char_u *next;
4551 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 int opchar = *op;
4553
4554 if (p[0] == opchar && p[1] == opchar)
4555 {
4556 garray_T *instr = &cctx->ctx_instr;
4557 garray_T end_ga;
4558
4559 /*
4560 * Repeat until there is no following "||" or "&&"
4561 */
4562 ga_init2(&end_ga, sizeof(int), 10);
4563 while (p[0] == opchar && p[1] == opchar)
4564 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004565 if (next != NULL)
4566 {
4567 *arg = next_line_from_context(cctx, TRUE);
4568 p = skipwhite(*arg);
4569 }
4570
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004571 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4572 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004574 return FAIL;
4575 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576
Bram Moolenaara5565e42020-05-09 15:44:01 +02004577 // TODO: use ppconst if the value is a constant
4578 generate_ppconst(cctx, ppconst);
4579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 if (ga_grow(&end_ga, 1) == FAIL)
4581 {
4582 ga_clear(&end_ga);
4583 return FAIL;
4584 }
4585 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4586 ++end_ga.ga_len;
4587 generate_JUMP(cctx, opchar == '|'
4588 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4589
4590 // eval the next expression
4591 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004592 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004593 return FAIL;
4594
Bram Moolenaara5565e42020-05-09 15:44:01 +02004595 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4596 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 {
4598 ga_clear(&end_ga);
4599 return FAIL;
4600 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004601
4602 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004604 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605
4606 // Fill in the end label in all jumps.
4607 while (end_ga.ga_len > 0)
4608 {
4609 isn_T *isn;
4610
4611 --end_ga.ga_len;
4612 isn = ((isn_T *)instr->ga_data)
4613 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4614 isn->isn_arg.jump.jump_where = instr->ga_len;
4615 }
4616 ga_clear(&end_ga);
4617 }
4618
4619 return OK;
4620}
4621
4622/*
4623 * expr4a && expr4a && expr4a logical AND
4624 *
4625 * Produces instructions:
4626 * EVAL expr4a Push result of "expr4a"
4627 * JUMP_AND_KEEP_IF_FALSE end
4628 * EVAL expr4b Push result of "expr4b"
4629 * JUMP_AND_KEEP_IF_FALSE end
4630 * EVAL expr4c Push result of "expr4c"
4631 * end:
4632 */
4633 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004634compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004636 int ppconst_used = ppconst->pp_used;
4637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004639 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 return FAIL;
4641
4642 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004643 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644}
4645
4646/*
4647 * expr3a || expr3b || expr3c logical OR
4648 *
4649 * Produces instructions:
4650 * EVAL expr3a Push result of "expr3a"
4651 * JUMP_AND_KEEP_IF_TRUE end
4652 * EVAL expr3b Push result of "expr3b"
4653 * JUMP_AND_KEEP_IF_TRUE end
4654 * EVAL expr3c Push result of "expr3c"
4655 * end:
4656 */
4657 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004658compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004660 int ppconst_used = ppconst->pp_used;
4661
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004663 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 return FAIL;
4665
4666 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004667 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668}
4669
4670/*
4671 * Toplevel expression: expr2 ? expr1a : expr1b
4672 *
4673 * Produces instructions:
4674 * EVAL expr2 Push result of "expr"
4675 * JUMP_IF_FALSE alt jump if false
4676 * EVAL expr1a
4677 * JUMP_ALWAYS end
4678 * alt: EVAL expr1b
4679 * end:
4680 */
4681 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004682compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683{
4684 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004685 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004686 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687
Bram Moolenaar61a89812020-05-07 16:58:17 +02004688 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004689 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690 return FAIL;
4691
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004692 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 if (*p == '?')
4694 {
4695 garray_T *instr = &cctx->ctx_instr;
4696 garray_T *stack = &cctx->ctx_type_stack;
4697 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004698 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004700 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004702 int has_const_expr = FALSE;
4703 int const_value = FALSE;
4704 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004706 if (next != NULL)
4707 {
4708 *arg = next_line_from_context(cctx, TRUE);
4709 p = skipwhite(*arg);
4710 }
4711
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004712 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4713 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004715 return FAIL;
4716 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717
Bram Moolenaara5565e42020-05-09 15:44:01 +02004718 if (ppconst->pp_used == ppconst_used + 1)
4719 {
4720 // the condition is a constant, we know whether the ? or the :
4721 // expression is to be evaluated.
4722 has_const_expr = TRUE;
4723 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4724 clear_tv(&ppconst->pp_tv[ppconst_used]);
4725 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004726 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4727 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004728 }
4729 else
4730 {
4731 generate_ppconst(cctx, ppconst);
4732 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4733 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734
4735 // evaluate the second expression; any type is accepted
4736 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004737 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004738 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004739 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004740 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004741
Bram Moolenaara5565e42020-05-09 15:44:01 +02004742 if (!has_const_expr)
4743 {
4744 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745
Bram Moolenaara5565e42020-05-09 15:44:01 +02004746 // remember the type and drop it
4747 --stack->ga_len;
4748 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749
Bram Moolenaara5565e42020-05-09 15:44:01 +02004750 end_idx = instr->ga_len;
4751 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4752
4753 // jump here from JUMP_IF_FALSE
4754 isn = ((isn_T *)instr->ga_data) + alt_idx;
4755 isn->isn_arg.jump.jump_where = instr->ga_len;
4756 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757
4758 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004759 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004760 if (*p != ':')
4761 {
4762 emsg(_(e_missing_colon));
4763 return FAIL;
4764 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004765 if (next != NULL)
4766 {
4767 *arg = next_line_from_context(cctx, TRUE);
4768 p = skipwhite(*arg);
4769 }
4770
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004771 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4772 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004774 return FAIL;
4775 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776
4777 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004778 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004779 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4780 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004782 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004783 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004784 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004785 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786
Bram Moolenaara5565e42020-05-09 15:44:01 +02004787 if (!has_const_expr)
4788 {
4789 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790
Bram Moolenaara5565e42020-05-09 15:44:01 +02004791 // If the types differ, the result has a more generic type.
4792 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4793 common_type(type1, type2, &type2, cctx->ctx_type_list);
4794
4795 // jump here from JUMP_ALWAYS
4796 isn = ((isn_T *)instr->ga_data) + end_idx;
4797 isn->isn_arg.jump.jump_where = instr->ga_len;
4798 }
4799
4800 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801 }
4802 return OK;
4803}
4804
4805/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004806 * Toplevel expression.
4807 */
4808 static int
4809compile_expr0(char_u **arg, cctx_T *cctx)
4810{
4811 ppconst_T ppconst;
4812
4813 CLEAR_FIELD(ppconst);
4814 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4815 {
4816 clear_ppconst(&ppconst);
4817 return FAIL;
4818 }
4819 if (generate_ppconst(cctx, &ppconst) == FAIL)
4820 return FAIL;
4821 return OK;
4822}
4823
4824/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825 * compile "return [expr]"
4826 */
4827 static char_u *
4828compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4829{
4830 char_u *p = arg;
4831 garray_T *stack = &cctx->ctx_type_stack;
4832 type_T *stack_type;
4833
4834 if (*p != NUL && *p != '|' && *p != '\n')
4835 {
4836 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004837 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 return NULL;
4839
4840 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4841 if (set_return_type)
4842 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004843 else
4844 {
4845 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4846 && stack_type->tt_type != VAR_VOID
4847 && stack_type->tt_type != VAR_UNKNOWN)
4848 {
4849 emsg(_("E1096: Returning a value in a function without a return type"));
4850 return NULL;
4851 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004852 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4853 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004855 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856 }
4857 else
4858 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004859 // "set_return_type" cannot be TRUE, only used for a lambda which
4860 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004861 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4862 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 {
4864 emsg(_("E1003: Missing return value"));
4865 return NULL;
4866 }
4867
4868 // No argument, return zero.
4869 generate_PUSHNR(cctx, 0);
4870 }
4871
4872 if (generate_instr(cctx, ISN_RETURN) == NULL)
4873 return NULL;
4874
4875 // "return val | endif" is possible
4876 return skipwhite(p);
4877}
4878
4879/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004880 * Get a line from the compilation context, compatible with exarg_T getline().
4881 * Return a pointer to the line in allocated memory.
4882 * Return NULL for end-of-file or some error.
4883 */
4884 static char_u *
4885exarg_getline(
4886 int c UNUSED,
4887 void *cookie,
4888 int indent UNUSED,
4889 int do_concat UNUSED)
4890{
4891 cctx_T *cctx = (cctx_T *)cookie;
4892
4893 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4894 {
4895 iemsg("Heredoc got to end");
4896 return NULL;
4897 }
4898 ++cctx->ctx_lnum;
4899 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4900 [cctx->ctx_lnum]);
4901}
4902
4903/*
4904 * Compile a nested :def command.
4905 */
4906 static char_u *
4907compile_nested_function(exarg_T *eap, cctx_T *cctx)
4908{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004909 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004910 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004911 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004912 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004913 lvar_T *lvar;
4914 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004915 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004916
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004917 // Only g:Func() can use a namespace.
4918 if (name_start[1] == ':' && !is_global)
4919 {
4920 semsg(_(e_namespace), name_start);
4921 return NULL;
4922 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004923 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4924 return NULL;
4925
Bram Moolenaar04b12692020-05-04 23:24:44 +02004926 eap->arg = name_end;
4927 eap->getline = exarg_getline;
4928 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004929 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004930 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004931 lambda_name = get_lambda_name();
4932 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004933
Bram Moolenaar822ba242020-05-24 23:00:18 +02004934 if (ufunc == NULL)
4935 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004936 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004937 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004938 return NULL;
4939
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004940 if (is_global)
4941 {
4942 char_u *func_name = vim_strnsave(name_start + 2,
4943 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004944
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004945 if (func_name == NULL)
4946 r = FAIL;
4947 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004948 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004949 }
4950 else
4951 {
4952 // Define a local variable for the function reference.
4953 lvar = reserve_local(cctx, name_start, name_end - name_start,
4954 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004955 if (lvar == NULL)
4956 return NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004957 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL)
4958 return NULL;
4959 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4960 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004961
Bram Moolenaar61a89812020-05-07 16:58:17 +02004962 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004963 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004964}
4965
4966/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967 * Return the length of an assignment operator, or zero if there isn't one.
4968 */
4969 int
4970assignment_len(char_u *p, int *heredoc)
4971{
4972 if (*p == '=')
4973 {
4974 if (p[1] == '<' && p[2] == '<')
4975 {
4976 *heredoc = TRUE;
4977 return 3;
4978 }
4979 return 1;
4980 }
4981 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4982 return 2;
4983 if (STRNCMP(p, "..=", 3) == 0)
4984 return 3;
4985 return 0;
4986}
4987
4988// words that cannot be used as a variable
4989static char *reserved[] = {
4990 "true",
4991 "false",
4992 NULL
4993};
4994
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004995typedef enum {
4996 dest_local,
4997 dest_option,
4998 dest_env,
4999 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005000 dest_buffer,
5001 dest_window,
5002 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005003 dest_vimvar,
5004 dest_script,
5005 dest_reg,
5006} assign_dest_T;
5007
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005009 * Generate the load instruction for "name".
5010 */
5011 static void
5012generate_loadvar(
5013 cctx_T *cctx,
5014 assign_dest_T dest,
5015 char_u *name,
5016 lvar_T *lvar,
5017 type_T *type)
5018{
5019 switch (dest)
5020 {
5021 case dest_option:
5022 // TODO: check the option exists
5023 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5024 break;
5025 case dest_global:
5026 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5027 break;
5028 case dest_buffer:
5029 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5030 break;
5031 case dest_window:
5032 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5033 break;
5034 case dest_tab:
5035 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5036 break;
5037 case dest_script:
5038 compile_load_scriptvar(cctx,
5039 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5040 break;
5041 case dest_env:
5042 // Include $ in the name here
5043 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5044 break;
5045 case dest_reg:
5046 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5047 break;
5048 case dest_vimvar:
5049 generate_LOADV(cctx, name + 2, TRUE);
5050 break;
5051 case dest_local:
5052 if (lvar->lv_from_outer)
5053 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5054 NULL, type);
5055 else
5056 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5057 break;
5058 }
5059}
5060
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005061 void
5062vim9_declare_error(char_u *name)
5063{
5064 char *scope = "";
5065
5066 switch (*name)
5067 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005068 case 'g': scope = _("global"); break;
5069 case 'b': scope = _("buffer"); break;
5070 case 'w': scope = _("window"); break;
5071 case 't': scope = _("tab"); break;
5072 case 'v': scope = "v:"; break;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005073 case '$': semsg(_(e_declare_env_var), name);
5074 return;
5075 case '&': semsg(_("E1052: Cannot declare an option: %s"), name);
5076 return;
5077 case '@': semsg(_("E1066: Cannot declare a register: %s"), name);
5078 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005079 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005080 }
5081 semsg(_(e_declare_var), scope, name);
5082}
5083
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005084/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005085 * Compile declaration and assignment:
5086 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005087 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005088 * Return NULL for an error.
5089 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090 */
5091 static char_u *
5092compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5093{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005094 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005096 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005097 char_u *ret = NULL;
5098 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005099 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005100 int scriptvar_sid = 0;
5101 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005104 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106 int oplen = 0;
5107 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005108 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005109 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005110 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005113
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005114 // Skip over the "var" or "[var, var]" to get to any "=".
5115 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5116 if (p == NULL)
5117 return *arg == '[' ? arg : NULL;
5118
5119 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005120 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005121 // TODO: should we allow this, and figure out type inference from list
5122 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005123 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005124 return NULL;
5125 }
5126
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005127 sp = p;
5128 p = skipwhite(p);
5129 op = p;
5130 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005131
5132 if (var_count > 0 && oplen == 0)
5133 // can be something like "[1, 2]->func()"
5134 return arg;
5135
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005136 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5137 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005138 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005139 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005140 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142 if (heredoc)
5143 {
5144 list_T *l;
5145 listitem_T *li;
5146
5147 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005148 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005149 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005150 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151
5152 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005153 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005154 {
5155 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5156 li->li_tv.vval.v_string = NULL;
5157 }
5158 generate_NEWLIST(cctx, l->lv_len);
5159 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005160 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005161 list_free(l);
5162 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005163 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005165 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005166 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005167 // for "[var, var] = expr" evaluate the expression here, loop over the
5168 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005169
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005170 p = skipwhite(op + oplen);
5171 if (compile_expr0(&p, cctx) == FAIL)
5172 return NULL;
5173 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005174
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005175 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005177 type_T *stacktype;
5178
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005179 stacktype = stack->ga_len == 0 ? &t_void
5180 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005181 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005183 emsg(_(e_cannot_use_void));
5184 goto theend;
5185 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005186 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005187 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005188 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005189 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5190 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005191 }
5192 }
5193
5194 /*
5195 * Loop over variables in "[var, var] = expr".
5196 * For "var = expr" and "let var: type" this is done only once.
5197 */
5198 if (var_count > 0)
5199 var_start = skipwhite(arg + 1); // skip over the "["
5200 else
5201 var_start = arg;
5202 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5203 {
5204 char_u *var_end = skip_var_one(var_start, FALSE);
5205 size_t varlen;
5206 int new_local = FALSE;
5207 int opt_type;
5208 int opt_flags = 0;
5209 assign_dest_T dest = dest_local;
5210 int vimvaridx = -1;
5211 lvar_T *lvar = NULL;
5212 lvar_T arg_lvar;
5213 int has_type = FALSE;
5214 int has_index = FALSE;
5215 int instr_count = -1;
5216
Bram Moolenaar65821722020-08-02 18:58:54 +02005217 if (*var_start == '@')
5218 p = var_start + 2;
5219 else
5220 {
5221 p = (*var_start == '&' || *var_start == '$')
5222 ? var_start + 1 : var_start;
5223 p = to_name_end(p, TRUE);
5224 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005225
5226 // "a: type" is declaring variable "a" with a type, not "a:".
5227 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5228 --var_end;
5229 if (is_decl && p == var_start + 2 && p[-1] == ':')
5230 --p;
5231
5232 varlen = p - var_start;
5233 vim_free(name);
5234 name = vim_strnsave(var_start, varlen);
5235 if (name == NULL)
5236 return NULL;
5237 if (!heredoc)
5238 type = &t_any;
5239
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005240 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005241 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005242 int declare_error = FALSE;
5243
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005244 if (*var_start == '&')
5245 {
5246 int cc;
5247 long numval;
5248
5249 dest = dest_option;
5250 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005251 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005252 emsg(_(e_const_option));
5253 goto theend;
5254 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005255 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005256 p = var_start;
5257 p = find_option_end(&p, &opt_flags);
5258 if (p == NULL)
5259 {
5260 // cannot happen?
5261 emsg(_(e_letunexp));
5262 goto theend;
5263 }
5264 cc = *p;
5265 *p = NUL;
5266 opt_type = get_option_value(var_start + 1, &numval,
5267 NULL, opt_flags);
5268 *p = cc;
5269 if (opt_type == -3)
5270 {
5271 semsg(_(e_unknown_option), var_start);
5272 goto theend;
5273 }
5274 if (opt_type == -2 || opt_type == 0)
5275 type = &t_string;
5276 else
5277 type = &t_number; // both number and boolean option
5278 }
5279 else if (*var_start == '$')
5280 {
5281 dest = dest_env;
5282 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005283 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005284 }
5285 else if (*var_start == '@')
5286 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005287 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005288 {
5289 emsg_invreg(var_start[1]);
5290 goto theend;
5291 }
5292 dest = dest_reg;
5293 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005294 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005295 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005296 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005297 {
5298 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005299 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005300 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005301 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005302 {
5303 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005304 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005305 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005306 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005307 {
5308 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005309 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005310 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005311 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005312 {
5313 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005314 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005315 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005316 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005317 {
5318 typval_T *vtv;
5319 int di_flags;
5320
5321 vimvaridx = find_vim_var(name + 2, &di_flags);
5322 if (vimvaridx < 0)
5323 {
5324 semsg(_(e_var_notfound), var_start);
5325 goto theend;
5326 }
5327 // We use the current value of "sandbox" here, is that OK?
5328 if (var_check_ro(di_flags, name, FALSE))
5329 goto theend;
5330 dest = dest_vimvar;
5331 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005332 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005333 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005334 }
5335 else
5336 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005337 int idx;
5338 imported_T *import = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005339
5340 for (idx = 0; reserved[idx] != NULL; ++idx)
5341 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005342 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005343 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005344 goto theend;
5345 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005346
5347 lvar = lookup_local(var_start, varlen, cctx);
5348 if (lvar == NULL)
5349 {
5350 CLEAR_FIELD(arg_lvar);
5351 if (lookup_arg(var_start, varlen,
5352 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5353 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005354 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005355 if (is_decl)
5356 {
5357 semsg(_(e_used_as_arg), name);
5358 goto theend;
5359 }
5360 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005361 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005362 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005363 if (lvar != NULL)
5364 {
5365 if (is_decl)
5366 {
5367 semsg(_("E1017: Variable already declared: %s"), name);
5368 goto theend;
5369 }
5370 else if (lvar->lv_const)
5371 {
5372 semsg(_("E1018: Cannot assign to a constant: %s"),
5373 name);
5374 goto theend;
5375 }
5376 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005377 else if ((varlen > 1 && STRNCMP(var_start, "s:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005378 || lookup_script(var_start, varlen) == OK
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005379 || (import = find_imported(var_start, varlen, cctx))
5380 != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005381 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005382 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5383
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005384 if (is_decl)
5385 {
Bram Moolenaar33afa242020-07-29 19:18:00 +02005386 if ((varlen > 1 && STRNCMP(var_start, "s:", 2) == 0))
5387 semsg(_("E1101: Cannot declare a script variable in a function: %s"),
5388 name);
5389 else
5390 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005391 name);
5392 goto theend;
5393 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005394 dest = dest_script;
5395
5396 // existing script-local variables should have a type
5397 scriptvar_sid = current_sctx.sc_sid;
5398 if (import != NULL)
5399 scriptvar_sid = import->imp_sid;
5400 scriptvar_idx = get_script_item_idx(scriptvar_sid,
5401 rawname, TRUE);
5402 if (scriptvar_idx >= 0)
5403 {
5404 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5405 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
5406 + scriptvar_idx;
5407 type = sv->sv_type;
5408 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005409 }
5410 else if (name[1] == ':' && name[2] != NUL)
5411 {
5412 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5413 name);
5414 goto theend;
5415 }
5416 else if (!is_decl)
5417 {
5418 semsg(_("E1089: unknown variable: %s"), name);
5419 goto theend;
5420 }
5421 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005422
5423 if (declare_error)
5424 {
5425 vim9_declare_error(name);
5426 goto theend;
5427 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005428 }
5429
5430 // handle "a:name" as a name, not index "name" on "a"
5431 if (varlen > 1 || var_start[varlen] != ':')
5432 p = var_end;
5433
5434 if (dest != dest_option)
5435 {
5436 if (is_decl && *p == ':')
5437 {
5438 // parse optional type: "let var: type = expr"
5439 if (!VIM_ISWHITE(p[1]))
5440 {
5441 semsg(_(e_white_after), ":");
5442 goto theend;
5443 }
5444 p = skipwhite(p + 1);
5445 type = parse_type(&p, cctx->ctx_type_list);
5446 has_type = TRUE;
5447 }
5448 else if (lvar != NULL)
5449 type = lvar->lv_type;
5450 }
5451
5452 if (oplen == 3 && !heredoc && dest != dest_global
5453 && type->tt_type != VAR_STRING
5454 && type->tt_type != VAR_ANY)
5455 {
5456 emsg(_("E1019: Can only concatenate to string"));
5457 goto theend;
5458 }
5459
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005460 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005461 {
5462 if (oplen > 1 && !heredoc)
5463 {
5464 // +=, /=, etc. require an existing variable
5465 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5466 name);
5467 goto theend;
5468 }
5469
5470 // new local variable
5471 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5472 goto theend;
5473 lvar = reserve_local(cctx, var_start, varlen,
5474 cmdidx == CMD_const, type);
5475 if (lvar == NULL)
5476 goto theend;
5477 new_local = TRUE;
5478 }
5479
5480 member_type = type;
5481 if (var_end > var_start + varlen)
5482 {
5483 // Something follows after the variable: "var[idx]".
5484 if (is_decl)
5485 {
5486 emsg(_("E1087: cannot use an index when declaring a variable"));
5487 goto theend;
5488 }
5489
5490 if (var_start[varlen] == '[')
5491 {
5492 has_index = TRUE;
5493 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005494 member_type = &t_any;
5495 else
5496 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005497 }
5498 else
5499 {
5500 semsg("Not supported yet: %s", var_start);
5501 goto theend;
5502 }
5503 }
5504 else if (lvar == &arg_lvar)
5505 {
5506 semsg(_("E1090: Cannot assign to argument %s"), name);
5507 goto theend;
5508 }
5509
5510 if (!heredoc)
5511 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005512 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005513 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005514 if (oplen > 0 && var_count == 0)
5515 {
5516 // skip over the "=" and the expression
5517 p = skipwhite(op + oplen);
5518 compile_expr0(&p, cctx);
5519 }
5520 }
5521 else if (oplen > 0)
5522 {
5523 type_T *stacktype;
5524
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005525 // For "var = expr" evaluate the expression.
5526 if (var_count == 0)
5527 {
5528 int r;
5529
5530 // for "+=", "*=", "..=" etc. first load the current value
5531 if (*op != '=')
5532 {
5533 generate_loadvar(cctx, dest, name, lvar, type);
5534
5535 if (has_index)
5536 {
5537 // TODO: get member from list or dict
5538 emsg("Index with operation not supported yet");
5539 goto theend;
5540 }
5541 }
5542
5543 // Compile the expression. Temporarily hide the new local
5544 // variable here, it is not available to this expression.
5545 if (new_local)
5546 --cctx->ctx_locals.ga_len;
5547 instr_count = instr->ga_len;
5548 p = skipwhite(op + oplen);
5549 r = compile_expr0(&p, cctx);
5550 if (new_local)
5551 ++cctx->ctx_locals.ga_len;
5552 if (r == FAIL)
5553 goto theend;
5554 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005555 else if (semicolon && var_idx == var_count - 1)
5556 {
5557 // For "[var; var] = expr" get the rest of the list
5558 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5559 goto theend;
5560 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005561 else
5562 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005563 // For "[var, var] = expr" get the "var_idx" item from the
5564 // list.
5565 if (generate_GETITEM(cctx, var_idx) == FAIL)
5566 return FAIL;
5567 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005568
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005569 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005570 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005571 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005572 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005573 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005574 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005575 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005576 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005577 emsg(_(e_cannot_use_void));
5578 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005579 }
5580 else
5581 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005582 // An empty list or dict has a &t_void member,
5583 // for a variable that implies &t_any.
5584 if (stacktype == &t_list_empty)
5585 lvar->lv_type = &t_list_any;
5586 else if (stacktype == &t_dict_empty)
5587 lvar->lv_type = &t_dict_any;
5588 else
5589 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005590 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005591 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005592 else
5593 {
5594 type_T *use_type = lvar->lv_type;
5595
5596 if (has_index)
5597 {
5598 use_type = use_type->tt_member;
5599 if (use_type == NULL)
5600 use_type = &t_void;
5601 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005602 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005603 == FAIL)
5604 goto theend;
5605 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005606 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005607 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005608 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005609 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005610 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005611 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005612 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005613 emsg(_(e_const_req_value));
5614 goto theend;
5615 }
5616 else if (!has_type || dest == dest_option)
5617 {
5618 emsg(_(e_type_req));
5619 goto theend;
5620 }
5621 else
5622 {
5623 // variables are always initialized
5624 if (ga_grow(instr, 1) == FAIL)
5625 goto theend;
5626 switch (member_type->tt_type)
5627 {
5628 case VAR_BOOL:
5629 generate_PUSHBOOL(cctx, VVAL_FALSE);
5630 break;
5631 case VAR_FLOAT:
5632#ifdef FEAT_FLOAT
5633 generate_PUSHF(cctx, 0.0);
5634#endif
5635 break;
5636 case VAR_STRING:
5637 generate_PUSHS(cctx, NULL);
5638 break;
5639 case VAR_BLOB:
5640 generate_PUSHBLOB(cctx, NULL);
5641 break;
5642 case VAR_FUNC:
5643 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5644 break;
5645 case VAR_LIST:
5646 generate_NEWLIST(cctx, 0);
5647 break;
5648 case VAR_DICT:
5649 generate_NEWDICT(cctx, 0);
5650 break;
5651 case VAR_JOB:
5652 generate_PUSHJOB(cctx, NULL);
5653 break;
5654 case VAR_CHANNEL:
5655 generate_PUSHCHANNEL(cctx, NULL);
5656 break;
5657 case VAR_NUMBER:
5658 case VAR_UNKNOWN:
5659 case VAR_ANY:
5660 case VAR_PARTIAL:
5661 case VAR_VOID:
5662 case VAR_SPECIAL: // cannot happen
5663 generate_PUSHNR(cctx, 0);
5664 break;
5665 }
5666 }
5667 if (var_count == 0)
5668 end = p;
5669 }
5670
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005671 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005672 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005673 break;
5674
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005675 if (oplen > 0 && *op != '=')
5676 {
5677 type_T *expected = &t_number;
5678 type_T *stacktype;
5679
5680 // TODO: if type is known use float or any operation
5681
5682 if (*op == '.')
5683 expected = &t_string;
5684 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005685 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005686 goto theend;
5687
5688 if (*op == '.')
5689 generate_instr_drop(cctx, ISN_CONCAT, 1);
5690 else
5691 {
5692 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5693
5694 if (isn == NULL)
5695 goto theend;
5696 switch (*op)
5697 {
5698 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5699 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5700 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5701 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5702 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5703 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005704 }
5705 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005706
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005707 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005708 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005709 int r;
5710
5711 // Compile the "idx" in "var[idx]".
5712 if (new_local)
5713 --cctx->ctx_locals.ga_len;
5714 p = skipwhite(var_start + varlen + 1);
5715 r = compile_expr0(&p, cctx);
5716 if (new_local)
5717 ++cctx->ctx_locals.ga_len;
5718 if (r == FAIL)
5719 goto theend;
5720 if (*skipwhite(p) != ']')
5721 {
5722 emsg(_(e_missbrac));
5723 goto theend;
5724 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005725 if (type == &t_any)
5726 {
5727 type_T *idx_type = ((type_T **)stack->ga_data)[
5728 stack->ga_len - 1];
5729 // Index on variable of unknown type: guess the type from the
5730 // index type: number is dict, otherwise dict.
5731 // TODO: should do the assignment at runtime
5732 if (idx_type->tt_type == VAR_NUMBER)
5733 type = &t_list_any;
5734 else
5735 type = &t_dict_any;
5736 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005737 if (type->tt_type == VAR_DICT
5738 && may_generate_2STRING(-1, cctx) == FAIL)
5739 goto theend;
5740 if (type->tt_type == VAR_LIST
5741 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005742 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005743 {
5744 emsg(_(e_number_exp));
5745 goto theend;
5746 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005747
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005748 // Load the dict or list. On the stack we then have:
5749 // - value
5750 // - index
5751 // - variable
5752 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005753
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005754 if (type->tt_type == VAR_LIST)
5755 {
5756 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5757 return FAIL;
5758 }
5759 else if (type->tt_type == VAR_DICT)
5760 {
5761 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5762 return FAIL;
5763 }
5764 else
5765 {
5766 emsg(_(e_listreq));
5767 goto theend;
5768 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005769 }
5770 else
5771 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005772 switch (dest)
5773 {
5774 case dest_option:
5775 generate_STOREOPT(cctx, name + 1, opt_flags);
5776 break;
5777 case dest_global:
5778 // include g: with the name, easier to execute that way
5779 generate_STORE(cctx, ISN_STOREG, 0, name);
5780 break;
5781 case dest_buffer:
5782 // include b: with the name, easier to execute that way
5783 generate_STORE(cctx, ISN_STOREB, 0, name);
5784 break;
5785 case dest_window:
5786 // include w: with the name, easier to execute that way
5787 generate_STORE(cctx, ISN_STOREW, 0, name);
5788 break;
5789 case dest_tab:
5790 // include t: with the name, easier to execute that way
5791 generate_STORE(cctx, ISN_STORET, 0, name);
5792 break;
5793 case dest_env:
5794 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5795 break;
5796 case dest_reg:
5797 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5798 break;
5799 case dest_vimvar:
5800 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5801 break;
5802 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005803 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005804 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005805 {
5806 char_u *name_s = name;
5807
5808 // Include s: in the name for store_var()
5809 if (name[1] != ':')
5810 {
5811 int len = (int)STRLEN(name) + 3;
5812
5813 name_s = alloc(len);
5814 if (name_s == NULL)
5815 name_s = name;
5816 else
5817 vim_snprintf((char *)name_s, len,
5818 "s:%s", name);
5819 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005820 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5821 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005822 if (name_s != name)
5823 vim_free(name_s);
5824 }
5825 else
5826 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005827 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005828 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005829 break;
5830 case dest_local:
5831 if (lvar != NULL)
5832 {
5833 isn_T *isn = ((isn_T *)instr->ga_data)
5834 + instr->ga_len - 1;
5835
5836 // optimization: turn "var = 123" from ISN_PUSHNR +
5837 // ISN_STORE into ISN_STORENR
5838 if (!lvar->lv_from_outer
5839 && instr->ga_len == instr_count + 1
5840 && isn->isn_type == ISN_PUSHNR)
5841 {
5842 varnumber_T val = isn->isn_arg.number;
5843
5844 isn->isn_type = ISN_STORENR;
5845 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5846 isn->isn_arg.storenr.stnr_val = val;
5847 if (stack->ga_len > 0)
5848 --stack->ga_len;
5849 }
5850 else if (lvar->lv_from_outer)
5851 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005852 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005853 else
5854 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5855 }
5856 break;
5857 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005858 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005859
5860 if (var_idx + 1 < var_count)
5861 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005862 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005863
5864 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005865 if (var_count > 0 && !semicolon)
5866 {
5867 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5868 goto theend;
5869 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005870
Bram Moolenaarb2097502020-07-19 17:17:02 +02005871 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005872
5873theend:
5874 vim_free(name);
5875 return ret;
5876}
5877
5878/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005879 * Check if "name" can be "unlet".
5880 */
5881 int
5882check_vim9_unlet(char_u *name)
5883{
5884 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5885 {
5886 semsg(_("E1081: Cannot unlet %s"), name);
5887 return FAIL;
5888 }
5889 return OK;
5890}
5891
5892/*
5893 * Callback passed to ex_unletlock().
5894 */
5895 static int
5896compile_unlet(
5897 lval_T *lvp,
5898 char_u *name_end,
5899 exarg_T *eap,
5900 int deep UNUSED,
5901 void *coookie)
5902{
5903 cctx_T *cctx = coookie;
5904
5905 if (lvp->ll_tv == NULL)
5906 {
5907 char_u *p = lvp->ll_name;
5908 int cc = *name_end;
5909 int ret = OK;
5910
5911 // Normal name. Only supports g:, w:, t: and b: namespaces.
5912 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005913 if (*p == '$')
5914 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5915 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005916 ret = FAIL;
5917 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005918 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005919
5920 *name_end = cc;
5921 return ret;
5922 }
5923
5924 // TODO: unlet {list}[idx]
5925 // TODO: unlet {dict}[key]
5926 emsg("Sorry, :unlet not fully implemented yet");
5927 return FAIL;
5928}
5929
5930/*
5931 * compile "unlet var", "lock var" and "unlock var"
5932 * "arg" points to "var".
5933 */
5934 static char_u *
5935compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5936{
5937 char_u *p = arg;
5938
5939 if (eap->cmdidx != CMD_unlet)
5940 {
5941 emsg("Sorry, :lock and unlock not implemented yet");
5942 return NULL;
5943 }
5944
5945 if (*p == '!')
5946 {
5947 p = skipwhite(p + 1);
5948 eap->forceit = TRUE;
5949 }
5950
5951 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5952 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5953}
5954
5955/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005956 * Compile an :import command.
5957 */
5958 static char_u *
5959compile_import(char_u *arg, cctx_T *cctx)
5960{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005961 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005962}
5963
5964/*
5965 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5966 */
5967 static int
5968compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5969{
5970 garray_T *instr = &cctx->ctx_instr;
5971 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5972
5973 if (endlabel == NULL)
5974 return FAIL;
5975 endlabel->el_next = *el;
5976 *el = endlabel;
5977 endlabel->el_end_label = instr->ga_len;
5978
5979 generate_JUMP(cctx, when, 0);
5980 return OK;
5981}
5982
5983 static void
5984compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5985{
5986 garray_T *instr = &cctx->ctx_instr;
5987
5988 while (*el != NULL)
5989 {
5990 endlabel_T *cur = (*el);
5991 isn_T *isn;
5992
5993 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5994 isn->isn_arg.jump.jump_where = instr->ga_len;
5995 *el = cur->el_next;
5996 vim_free(cur);
5997 }
5998}
5999
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006000 static void
6001compile_free_jump_to_end(endlabel_T **el)
6002{
6003 while (*el != NULL)
6004 {
6005 endlabel_T *cur = (*el);
6006
6007 *el = cur->el_next;
6008 vim_free(cur);
6009 }
6010}
6011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006012/*
6013 * Create a new scope and set up the generic items.
6014 */
6015 static scope_T *
6016new_scope(cctx_T *cctx, scopetype_T type)
6017{
6018 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6019
6020 if (scope == NULL)
6021 return NULL;
6022 scope->se_outer = cctx->ctx_scope;
6023 cctx->ctx_scope = scope;
6024 scope->se_type = type;
6025 scope->se_local_count = cctx->ctx_locals.ga_len;
6026 return scope;
6027}
6028
6029/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006030 * Free the current scope and go back to the outer scope.
6031 */
6032 static void
6033drop_scope(cctx_T *cctx)
6034{
6035 scope_T *scope = cctx->ctx_scope;
6036
6037 if (scope == NULL)
6038 {
6039 iemsg("calling drop_scope() without a scope");
6040 return;
6041 }
6042 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006043 switch (scope->se_type)
6044 {
6045 case IF_SCOPE:
6046 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6047 case FOR_SCOPE:
6048 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6049 case WHILE_SCOPE:
6050 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6051 case TRY_SCOPE:
6052 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6053 case NO_SCOPE:
6054 case BLOCK_SCOPE:
6055 break;
6056 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006057 vim_free(scope);
6058}
6059
6060/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061 * compile "if expr"
6062 *
6063 * "if expr" Produces instructions:
6064 * EVAL expr Push result of "expr"
6065 * JUMP_IF_FALSE end
6066 * ... body ...
6067 * end:
6068 *
6069 * "if expr | else" Produces instructions:
6070 * EVAL expr Push result of "expr"
6071 * JUMP_IF_FALSE else
6072 * ... body ...
6073 * JUMP_ALWAYS end
6074 * else:
6075 * ... body ...
6076 * end:
6077 *
6078 * "if expr1 | elseif expr2 | else" Produces instructions:
6079 * EVAL expr Push result of "expr"
6080 * JUMP_IF_FALSE elseif
6081 * ... body ...
6082 * JUMP_ALWAYS end
6083 * elseif:
6084 * EVAL expr Push result of "expr"
6085 * JUMP_IF_FALSE else
6086 * ... body ...
6087 * JUMP_ALWAYS end
6088 * else:
6089 * ... body ...
6090 * end:
6091 */
6092 static char_u *
6093compile_if(char_u *arg, cctx_T *cctx)
6094{
6095 char_u *p = arg;
6096 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006097 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006098 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006099 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006100 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006101
Bram Moolenaara5565e42020-05-09 15:44:01 +02006102 CLEAR_FIELD(ppconst);
6103 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006104 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006105 clear_ppconst(&ppconst);
6106 return NULL;
6107 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006108 if (cctx->ctx_skip == SKIP_YES)
6109 clear_ppconst(&ppconst);
6110 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006111 {
6112 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006113 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006114 clear_ppconst(&ppconst);
6115 }
6116 else
6117 {
6118 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006119 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006120 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006121 return NULL;
6122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006123
6124 scope = new_scope(cctx, IF_SCOPE);
6125 if (scope == NULL)
6126 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006127 scope->se_skip_save = skip_save;
6128 // "is_had_return" will be reset if any block does not end in :return
6129 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006130
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006131 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006132 {
6133 // "where" is set when ":elseif", "else" or ":endif" is found
6134 scope->se_u.se_if.is_if_label = instr->ga_len;
6135 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6136 }
6137 else
6138 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006139
6140 return p;
6141}
6142
6143 static char_u *
6144compile_elseif(char_u *arg, cctx_T *cctx)
6145{
6146 char_u *p = arg;
6147 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006148 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006149 isn_T *isn;
6150 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006151 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006152
6153 if (scope == NULL || scope->se_type != IF_SCOPE)
6154 {
6155 emsg(_(e_elseif_without_if));
6156 return NULL;
6157 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006158 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006159 if (!cctx->ctx_had_return)
6160 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006161
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006162 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006163 {
6164 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006165 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006166 return NULL;
6167 // previous "if" or "elseif" jumps here
6168 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6169 isn->isn_arg.jump.jump_where = instr->ga_len;
6170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006171
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006172 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006173 CLEAR_FIELD(ppconst);
6174 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006175 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006176 clear_ppconst(&ppconst);
6177 return NULL;
6178 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006179 if (scope->se_skip_save == SKIP_YES)
6180 clear_ppconst(&ppconst);
6181 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006182 {
6183 // The expression results in a constant.
6184 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006185 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006186 clear_ppconst(&ppconst);
6187 scope->se_u.se_if.is_if_label = -1;
6188 }
6189 else
6190 {
6191 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006192 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006193 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006194 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006195
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006196 // "where" is set when ":elseif", "else" or ":endif" is found
6197 scope->se_u.se_if.is_if_label = instr->ga_len;
6198 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6199 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006200
6201 return p;
6202}
6203
6204 static char_u *
6205compile_else(char_u *arg, cctx_T *cctx)
6206{
6207 char_u *p = arg;
6208 garray_T *instr = &cctx->ctx_instr;
6209 isn_T *isn;
6210 scope_T *scope = cctx->ctx_scope;
6211
6212 if (scope == NULL || scope->se_type != IF_SCOPE)
6213 {
6214 emsg(_(e_else_without_if));
6215 return NULL;
6216 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006217 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006218 if (!cctx->ctx_had_return)
6219 scope->se_u.se_if.is_had_return = FALSE;
6220 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006221
Bram Moolenaarefd88552020-06-18 20:50:10 +02006222 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006223 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006224 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006225 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006226 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006227 if (!cctx->ctx_had_return
6228 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6229 JUMP_ALWAYS, cctx) == FAIL)
6230 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006231 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006232
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006233 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006234 {
6235 if (scope->se_u.se_if.is_if_label >= 0)
6236 {
6237 // previous "if" or "elseif" jumps here
6238 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6239 isn->isn_arg.jump.jump_where = instr->ga_len;
6240 scope->se_u.se_if.is_if_label = -1;
6241 }
6242 }
6243
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006244 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006245 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6246 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006247
6248 return p;
6249}
6250
6251 static char_u *
6252compile_endif(char_u *arg, cctx_T *cctx)
6253{
6254 scope_T *scope = cctx->ctx_scope;
6255 ifscope_T *ifscope;
6256 garray_T *instr = &cctx->ctx_instr;
6257 isn_T *isn;
6258
6259 if (scope == NULL || scope->se_type != IF_SCOPE)
6260 {
6261 emsg(_(e_endif_without_if));
6262 return NULL;
6263 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006264 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006265 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006266 if (!cctx->ctx_had_return)
6267 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006268
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006269 if (scope->se_u.se_if.is_if_label >= 0)
6270 {
6271 // previous "if" or "elseif" jumps here
6272 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6273 isn->isn_arg.jump.jump_where = instr->ga_len;
6274 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006275 // Fill in the "end" label in jumps at the end of the blocks.
6276 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006277 cctx->ctx_skip = scope->se_skip_save;
6278
6279 // If all the blocks end in :return and there is an :else then the
6280 // had_return flag is set.
6281 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006282
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006283 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006284 return arg;
6285}
6286
6287/*
6288 * compile "for var in expr"
6289 *
6290 * Produces instructions:
6291 * PUSHNR -1
6292 * STORE loop-idx Set index to -1
6293 * EVAL expr Push result of "expr"
6294 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6295 * - if beyond end, jump to "end"
6296 * - otherwise get item from list and push it
6297 * STORE var Store item in "var"
6298 * ... body ...
6299 * JUMP top Jump back to repeat
6300 * end: DROP Drop the result of "expr"
6301 *
6302 */
6303 static char_u *
6304compile_for(char_u *arg, cctx_T *cctx)
6305{
6306 char_u *p;
6307 size_t varlen;
6308 garray_T *instr = &cctx->ctx_instr;
6309 garray_T *stack = &cctx->ctx_type_stack;
6310 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006311 lvar_T *loop_lvar; // loop iteration variable
6312 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006313 type_T *vartype;
6314
6315 // TODO: list of variables: "for [key, value] in dict"
6316 // parse "var"
6317 for (p = arg; eval_isnamec1(*p); ++p)
6318 ;
6319 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006320 var_lvar = lookup_local(arg, varlen, cctx);
6321 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 {
6323 semsg(_("E1023: variable already defined: %s"), arg);
6324 return NULL;
6325 }
6326
6327 // consume "in"
6328 p = skipwhite(p);
6329 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6330 {
6331 emsg(_(e_missing_in));
6332 return NULL;
6333 }
6334 p = skipwhite(p + 2);
6335
6336
6337 scope = new_scope(cctx, FOR_SCOPE);
6338 if (scope == NULL)
6339 return NULL;
6340
6341 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006342 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6343 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006344 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006345 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006346 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006347 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006348 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006349
6350 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006351 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6352 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006353 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006354 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006355 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006356 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006357 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006358
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006359 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006360
6361 // compile "expr", it remains on the stack until "endfor"
6362 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006363 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006364 {
6365 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006366 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006367 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006368
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006369 // Now that we know the type of "var", check that it is a list, now or at
6370 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006371 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006372 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006373 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006374 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006375 return NULL;
6376 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006377 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006378 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006379
6380 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006381 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006382
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006383 generate_FOR(cctx, loop_lvar->lv_idx);
6384 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006385
6386 return arg;
6387}
6388
6389/*
6390 * compile "endfor"
6391 */
6392 static char_u *
6393compile_endfor(char_u *arg, cctx_T *cctx)
6394{
6395 garray_T *instr = &cctx->ctx_instr;
6396 scope_T *scope = cctx->ctx_scope;
6397 forscope_T *forscope;
6398 isn_T *isn;
6399
6400 if (scope == NULL || scope->se_type != FOR_SCOPE)
6401 {
6402 emsg(_(e_for));
6403 return NULL;
6404 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006405 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006407 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408
6409 // At end of ":for" scope jump back to the FOR instruction.
6410 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6411
6412 // Fill in the "end" label in the FOR statement so it can jump here
6413 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6414 isn->isn_arg.forloop.for_end = instr->ga_len;
6415
6416 // Fill in the "end" label any BREAK statements
6417 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6418
6419 // Below the ":for" scope drop the "expr" list from the stack.
6420 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6421 return NULL;
6422
6423 vim_free(scope);
6424
6425 return arg;
6426}
6427
6428/*
6429 * compile "while expr"
6430 *
6431 * Produces instructions:
6432 * top: EVAL expr Push result of "expr"
6433 * JUMP_IF_FALSE end jump if false
6434 * ... body ...
6435 * JUMP top Jump back to repeat
6436 * end:
6437 *
6438 */
6439 static char_u *
6440compile_while(char_u *arg, cctx_T *cctx)
6441{
6442 char_u *p = arg;
6443 garray_T *instr = &cctx->ctx_instr;
6444 scope_T *scope;
6445
6446 scope = new_scope(cctx, WHILE_SCOPE);
6447 if (scope == NULL)
6448 return NULL;
6449
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006450 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006451
6452 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006453 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006454 return NULL;
6455
6456 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006457 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006458 JUMP_IF_FALSE, cctx) == FAIL)
6459 return FAIL;
6460
6461 return p;
6462}
6463
6464/*
6465 * compile "endwhile"
6466 */
6467 static char_u *
6468compile_endwhile(char_u *arg, cctx_T *cctx)
6469{
6470 scope_T *scope = cctx->ctx_scope;
6471
6472 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6473 {
6474 emsg(_(e_while));
6475 return NULL;
6476 }
6477 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006478 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006479
6480 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006481 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006482
6483 // Fill in the "end" label in the WHILE statement so it can jump here.
6484 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006485 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486
6487 vim_free(scope);
6488
6489 return arg;
6490}
6491
6492/*
6493 * compile "continue"
6494 */
6495 static char_u *
6496compile_continue(char_u *arg, cctx_T *cctx)
6497{
6498 scope_T *scope = cctx->ctx_scope;
6499
6500 for (;;)
6501 {
6502 if (scope == NULL)
6503 {
6504 emsg(_(e_continue));
6505 return NULL;
6506 }
6507 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6508 break;
6509 scope = scope->se_outer;
6510 }
6511
6512 // Jump back to the FOR or WHILE instruction.
6513 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006514 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6515 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006516 return arg;
6517}
6518
6519/*
6520 * compile "break"
6521 */
6522 static char_u *
6523compile_break(char_u *arg, cctx_T *cctx)
6524{
6525 scope_T *scope = cctx->ctx_scope;
6526 endlabel_T **el;
6527
6528 for (;;)
6529 {
6530 if (scope == NULL)
6531 {
6532 emsg(_(e_break));
6533 return NULL;
6534 }
6535 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6536 break;
6537 scope = scope->se_outer;
6538 }
6539
6540 // Jump to the end of the FOR or WHILE loop.
6541 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006542 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006543 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006544 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006545 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6546 return FAIL;
6547
6548 return arg;
6549}
6550
6551/*
6552 * compile "{" start of block
6553 */
6554 static char_u *
6555compile_block(char_u *arg, cctx_T *cctx)
6556{
6557 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6558 return NULL;
6559 return skipwhite(arg + 1);
6560}
6561
6562/*
6563 * compile end of block: drop one scope
6564 */
6565 static void
6566compile_endblock(cctx_T *cctx)
6567{
6568 scope_T *scope = cctx->ctx_scope;
6569
6570 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006571 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006572 vim_free(scope);
6573}
6574
6575/*
6576 * compile "try"
6577 * Creates a new scope for the try-endtry, pointing to the first catch and
6578 * finally.
6579 * Creates another scope for the "try" block itself.
6580 * TRY instruction sets up exception handling at runtime.
6581 *
6582 * "try"
6583 * TRY -> catch1, -> finally push trystack entry
6584 * ... try block
6585 * "throw {exception}"
6586 * EVAL {exception}
6587 * THROW create exception
6588 * ... try block
6589 * " catch {expr}"
6590 * JUMP -> finally
6591 * catch1: PUSH exeception
6592 * EVAL {expr}
6593 * MATCH
6594 * JUMP nomatch -> catch2
6595 * CATCH remove exception
6596 * ... catch block
6597 * " catch"
6598 * JUMP -> finally
6599 * catch2: CATCH remove exception
6600 * ... catch block
6601 * " finally"
6602 * finally:
6603 * ... finally block
6604 * " endtry"
6605 * ENDTRY pop trystack entry, may rethrow
6606 */
6607 static char_u *
6608compile_try(char_u *arg, cctx_T *cctx)
6609{
6610 garray_T *instr = &cctx->ctx_instr;
6611 scope_T *try_scope;
6612 scope_T *scope;
6613
6614 // scope that holds the jumps that go to catch/finally/endtry
6615 try_scope = new_scope(cctx, TRY_SCOPE);
6616 if (try_scope == NULL)
6617 return NULL;
6618
6619 // "catch" is set when the first ":catch" is found.
6620 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006621 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006622 if (generate_instr(cctx, ISN_TRY) == NULL)
6623 return NULL;
6624
6625 // scope for the try block itself
6626 scope = new_scope(cctx, BLOCK_SCOPE);
6627 if (scope == NULL)
6628 return NULL;
6629
6630 return arg;
6631}
6632
6633/*
6634 * compile "catch {expr}"
6635 */
6636 static char_u *
6637compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6638{
6639 scope_T *scope = cctx->ctx_scope;
6640 garray_T *instr = &cctx->ctx_instr;
6641 char_u *p;
6642 isn_T *isn;
6643
6644 // end block scope from :try or :catch
6645 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6646 compile_endblock(cctx);
6647 scope = cctx->ctx_scope;
6648
6649 // Error if not in a :try scope
6650 if (scope == NULL || scope->se_type != TRY_SCOPE)
6651 {
6652 emsg(_(e_catch));
6653 return NULL;
6654 }
6655
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006656 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006657 {
6658 emsg(_("E1033: catch unreachable after catch-all"));
6659 return NULL;
6660 }
6661
6662 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006663 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664 JUMP_ALWAYS, cctx) == FAIL)
6665 return NULL;
6666
6667 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006668 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006669 if (isn->isn_arg.try.try_catch == 0)
6670 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006671 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006672 {
6673 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006674 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006675 isn->isn_arg.jump.jump_where = instr->ga_len;
6676 }
6677
6678 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006679 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006680 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006681 scope->se_u.se_try.ts_caught_all = TRUE;
6682 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683 }
6684 else
6685 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006686 char_u *end;
6687 char_u *pat;
6688 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006689 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006690 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006692 // Push v:exception, push {expr} and MATCH
6693 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6694
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006695 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006696 if (*end != *p)
6697 {
6698 semsg(_("E1067: Separator mismatch: %s"), p);
6699 vim_free(tofree);
6700 return FAIL;
6701 }
6702 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006703 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006704 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006705 len = (int)(end - tofree);
6706 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006707 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006708 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006709 if (pat == NULL)
6710 return FAIL;
6711 if (generate_PUSHS(cctx, pat) == FAIL)
6712 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006714 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6715 return NULL;
6716
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006717 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006718 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6719 return NULL;
6720 }
6721
6722 if (generate_instr(cctx, ISN_CATCH) == NULL)
6723 return NULL;
6724
6725 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6726 return NULL;
6727 return p;
6728}
6729
6730 static char_u *
6731compile_finally(char_u *arg, cctx_T *cctx)
6732{
6733 scope_T *scope = cctx->ctx_scope;
6734 garray_T *instr = &cctx->ctx_instr;
6735 isn_T *isn;
6736
6737 // end block scope from :try or :catch
6738 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6739 compile_endblock(cctx);
6740 scope = cctx->ctx_scope;
6741
6742 // Error if not in a :try scope
6743 if (scope == NULL || scope->se_type != TRY_SCOPE)
6744 {
6745 emsg(_(e_finally));
6746 return NULL;
6747 }
6748
6749 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006750 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006751 if (isn->isn_arg.try.try_finally != 0)
6752 {
6753 emsg(_(e_finally_dup));
6754 return NULL;
6755 }
6756
6757 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006758 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006759
Bram Moolenaar585fea72020-04-02 22:33:21 +02006760 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006761 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006762 {
6763 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006764 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006765 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006766 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006767 }
6768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006769 // TODO: set index in ts_finally_label jumps
6770
6771 return arg;
6772}
6773
6774 static char_u *
6775compile_endtry(char_u *arg, cctx_T *cctx)
6776{
6777 scope_T *scope = cctx->ctx_scope;
6778 garray_T *instr = &cctx->ctx_instr;
6779 isn_T *isn;
6780
6781 // end block scope from :catch or :finally
6782 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6783 compile_endblock(cctx);
6784 scope = cctx->ctx_scope;
6785
6786 // Error if not in a :try scope
6787 if (scope == NULL || scope->se_type != TRY_SCOPE)
6788 {
6789 if (scope == NULL)
6790 emsg(_(e_no_endtry));
6791 else if (scope->se_type == WHILE_SCOPE)
6792 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006793 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006794 emsg(_(e_endfor));
6795 else
6796 emsg(_(e_endif));
6797 return NULL;
6798 }
6799
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006800 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006801 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6802 {
6803 emsg(_("E1032: missing :catch or :finally"));
6804 return NULL;
6805 }
6806
6807 // Fill in the "end" label in jumps at the end of the blocks, if not done
6808 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006809 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006810
6811 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006812 if (isn->isn_arg.try.try_catch == 0)
6813 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006814 if (isn->isn_arg.try.try_finally == 0)
6815 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006816
6817 if (scope->se_u.se_try.ts_catch_label != 0)
6818 {
6819 // Last catch without match jumps here
6820 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6821 isn->isn_arg.jump.jump_where = instr->ga_len;
6822 }
6823
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006824 compile_endblock(cctx);
6825
6826 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6827 return NULL;
6828 return arg;
6829}
6830
6831/*
6832 * compile "throw {expr}"
6833 */
6834 static char_u *
6835compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6836{
6837 char_u *p = skipwhite(arg);
6838
Bram Moolenaara5565e42020-05-09 15:44:01 +02006839 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006840 return NULL;
6841 if (may_generate_2STRING(-1, cctx) == FAIL)
6842 return NULL;
6843 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6844 return NULL;
6845
6846 return p;
6847}
6848
6849/*
6850 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006851 * compile "echomsg expr"
6852 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006853 * compile "execute expr"
6854 */
6855 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006856compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006857{
6858 char_u *p = arg;
6859 int count = 0;
6860
6861 for (;;)
6862 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006863 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006864 return NULL;
6865 ++count;
6866 p = skipwhite(p);
6867 if (ends_excmd(*p))
6868 break;
6869 }
6870
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006871 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6872 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6873 else if (cmdidx == CMD_execute)
6874 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6875 else if (cmdidx == CMD_echomsg)
6876 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6877 else
6878 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006879 return p;
6880}
6881
6882/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006883 * A command that is not compiled, execute with legacy code.
6884 */
6885 static char_u *
6886compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6887{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006888 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006889 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006890 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006891
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006892 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006893 goto theend;
6894
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006895 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006896 {
6897 long argt = excmd_get_argt(eap->cmdidx);
6898 int usefilter = FALSE;
6899
6900 has_expr = argt & (EX_XFILE | EX_EXPAND);
6901
6902 // If the command can be followed by a bar, find the bar and truncate
6903 // it, so that the following command can be compiled.
6904 // The '|' is overwritten with a NUL, it is put back below.
6905 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6906 && *eap->arg == '!')
6907 // :w !filter or :r !filter or :r! filter
6908 usefilter = TRUE;
6909 if ((argt & EX_TRLBAR) && !usefilter)
6910 {
6911 separate_nextcmd(eap);
6912 if (eap->nextcmd != NULL)
6913 nextcmd = eap->nextcmd;
6914 }
6915 }
6916
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006917 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6918 {
6919 // expand filename in "syntax include [@group] filename"
6920 has_expr = TRUE;
6921 eap->arg = skipwhite(eap->arg + 7);
6922 if (*eap->arg == '@')
6923 eap->arg = skiptowhite(eap->arg);
6924 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006925
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006926 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006927 {
6928 int count = 0;
6929 char_u *start = skipwhite(line);
6930
6931 // :cmd xxx`=expr1`yyy`=expr2`zzz
6932 // PUSHS ":cmd xxx"
6933 // eval expr1
6934 // PUSHS "yyy"
6935 // eval expr2
6936 // PUSHS "zzz"
6937 // EXECCONCAT 5
6938 for (;;)
6939 {
6940 if (p > start)
6941 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006942 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006943 ++count;
6944 }
6945 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006946 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006947 return NULL;
6948 may_generate_2STRING(-1, cctx);
6949 ++count;
6950 p = skipwhite(p);
6951 if (*p != '`')
6952 {
6953 emsg(_("E1083: missing backtick"));
6954 return NULL;
6955 }
6956 start = p + 1;
6957
6958 p = (char_u *)strstr((char *)start, "`=");
6959 if (p == NULL)
6960 {
6961 if (*skipwhite(start) != NUL)
6962 {
6963 generate_PUSHS(cctx, vim_strsave(start));
6964 ++count;
6965 }
6966 break;
6967 }
6968 }
6969 generate_EXECCONCAT(cctx, count);
6970 }
6971 else
6972 generate_EXEC(cctx, line);
6973
6974theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006975 if (*nextcmd != NUL)
6976 {
6977 // the parser expects a pointer to the bar, put it back
6978 --nextcmd;
6979 *nextcmd = '|';
6980 }
6981
6982 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006983}
6984
6985/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006986 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006987 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006988 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006989 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006990add_def_function(ufunc_T *ufunc)
6991{
6992 dfunc_T *dfunc;
6993
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006994 if (def_functions.ga_len == 0)
6995 {
6996 // The first position is not used, so that a zero uf_dfunc_idx means it
6997 // wasn't set.
6998 if (ga_grow(&def_functions, 1) == FAIL)
6999 return FAIL;
7000 ++def_functions.ga_len;
7001 }
7002
Bram Moolenaar09689a02020-05-09 22:50:08 +02007003 // Add the function to "def_functions".
7004 if (ga_grow(&def_functions, 1) == FAIL)
7005 return FAIL;
7006 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7007 CLEAR_POINTER(dfunc);
7008 dfunc->df_idx = def_functions.ga_len;
7009 ufunc->uf_dfunc_idx = dfunc->df_idx;
7010 dfunc->df_ufunc = ufunc;
7011 ++def_functions.ga_len;
7012 return OK;
7013}
7014
7015/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007016 * After ex_function() has collected all the function lines: parse and compile
7017 * the lines into instructions.
7018 * Adds the function to "def_functions".
7019 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7020 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007021 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007022 * This can be used recursively through compile_lambda(), which may reallocate
7023 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007024 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007025 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007026 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007027compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007028{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007029 char_u *line = NULL;
7030 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007031 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007032 cctx_T cctx;
7033 garray_T *instr;
7034 int called_emsg_before = called_emsg;
7035 int ret = FAIL;
7036 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007037 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007038 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007039 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007041 // When using a function that was compiled before: Free old instructions.
7042 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007043 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007044 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007045 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7046 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007047 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007048 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007049 else
7050 {
7051 if (add_def_function(ufunc) == FAIL)
7052 return FAIL;
7053 new_def_function = TRUE;
7054 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007055
Bram Moolenaar985116a2020-07-12 17:31:09 +02007056 ufunc->uf_def_status = UF_COMPILING;
7057
Bram Moolenaara80faa82020-04-12 19:37:17 +02007058 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007059 cctx.ctx_ufunc = ufunc;
7060 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007061 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7063 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7064 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7065 cctx.ctx_type_list = &ufunc->uf_type_list;
7066 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7067 instr = &cctx.ctx_instr;
7068
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007069 // Set the context to the function, it may be compiled when called from
7070 // another script. Set the script version to the most modern one.
7071 // The line number will be set in next_line_from_context().
7072 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007073 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7074
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007075 // Make sure error messages are OK.
7076 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7077 if (do_estack_push)
7078 estack_push_ufunc(ufunc, 1);
7079
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007080 if (ufunc->uf_def_args.ga_len > 0)
7081 {
7082 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007083 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007084 int i;
7085 char_u *arg;
7086 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007087 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007088
7089 // Produce instructions for the default values of optional arguments.
7090 // Store the instruction index in uf_def_arg_idx[] so that we know
7091 // where to start when the function is called, depending on the number
7092 // of arguments.
7093 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7094 if (ufunc->uf_def_arg_idx == NULL)
7095 goto erret;
7096 for (i = 0; i < count; ++i)
7097 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007098 garray_T *stack = &cctx.ctx_type_stack;
7099 type_T *val_type;
7100 int arg_idx = first_def_arg + i;
7101
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007102 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7103 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007104 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007105 goto erret;
7106
7107 // If no type specified use the type of the default value.
7108 // Otherwise check that the default value type matches the
7109 // specified type.
7110 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7111 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007112 {
7113 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007114 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007115 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02007116 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007117 == FAIL)
7118 {
7119 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
7120 arg_idx + 1);
7121 goto erret;
7122 }
7123
7124 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007125 goto erret;
7126 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007127 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007128
7129 if (did_set_arg_type)
7130 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007131 }
7132
7133 /*
7134 * Loop over all the lines of the function and generate instructions.
7135 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007136 for (;;)
7137 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007138 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007139 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007140 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007141 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007142
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007143 // Bail out on the first error to avoid a flood of errors and report
7144 // the right line number when inside try/catch.
7145 if (emsg_before != called_emsg)
7146 goto erret;
7147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007148 if (line != NULL && *line == '|')
7149 // the line continues after a '|'
7150 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007151 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007152 && !(*line == '#' && (line == cctx.ctx_line_start
7153 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007154 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007155 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007156 goto erret;
7157 }
7158 else
7159 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007160 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007161 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007162 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007163 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007165 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007166
Bram Moolenaara80faa82020-04-12 19:37:17 +02007167 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168 ea.cmdlinep = &line;
7169 ea.cmd = skipwhite(line);
7170
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007171 // Some things can be recognized by the first character.
7172 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007173 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007174 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007175 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007176 if (ea.cmd[1] != '{')
7177 {
7178 line = (char_u *)"";
7179 continue;
7180 }
7181 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007182
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007183 case '}':
7184 {
7185 // "}" ends a block scope
7186 scopetype_T stype = cctx.ctx_scope == NULL
7187 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007188
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007189 if (stype == BLOCK_SCOPE)
7190 {
7191 compile_endblock(&cctx);
7192 line = ea.cmd;
7193 }
7194 else
7195 {
7196 emsg(_("E1025: using } outside of a block scope"));
7197 goto erret;
7198 }
7199 if (line != NULL)
7200 line = skipwhite(ea.cmd + 1);
7201 continue;
7202 }
7203
7204 case '{':
7205 // "{" starts a block scope
7206 // "{'a': 1}->func() is something else
7207 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7208 {
7209 line = compile_block(ea.cmd, &cctx);
7210 continue;
7211 }
7212 break;
7213
7214 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007215 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007216 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007217 }
7218
7219 /*
7220 * COMMAND MODIFIERS
7221 */
7222 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7223 {
7224 if (errormsg != NULL)
7225 goto erret;
7226 // empty line or comment
7227 line = (char_u *)"";
7228 continue;
7229 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007230 // TODO: use modifiers in the command
7231 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02007232 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007233
7234 // Skip ":call" to get to the function name.
7235 if (checkforcmd(&ea.cmd, "call", 3))
7236 ea.cmd = skipwhite(ea.cmd);
7237
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007238 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007239 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007240 char_u *pskip;
7241
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007242 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007243 // find what follows.
7244 // Skip over "var.member", "var[idx]" and the like.
7245 // Also "&opt = val", "$ENV = val" and "@r = val".
7246 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007247 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007248 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007249 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007250 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007251 char_u *var_end;
7252 int oplen;
7253 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007254
Bram Moolenaar65821722020-08-02 18:58:54 +02007255 if (ea.cmd[0] == '@')
7256 var_end = ea.cmd + 2;
7257 else
7258 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007259 FNE_CHECK_START | FNE_INCL_BR);
7260 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007261 if (oplen > 0)
7262 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007263 size_t len = p - ea.cmd;
7264
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007265 // Recognize an assignment if we recognize the variable
7266 // name:
7267 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007268 // "local = expr" where "local" is a local var.
7269 // "script = expr" where "script" is a script-local var.
7270 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007271 // "&opt = expr"
7272 // "$ENV = expr"
7273 // "@r = expr"
7274 if (*ea.cmd == '&'
7275 || *ea.cmd == '$'
7276 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007277 || ((len) > 2 && ea.cmd[1] == ':')
7278 || lookup_local(ea.cmd, len, &cctx) != NULL
7279 || lookup_arg(ea.cmd, len, NULL, NULL,
7280 NULL, &cctx) == OK
7281 || lookup_script(ea.cmd, len) == OK
7282 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007283 {
7284 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007285 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007286 goto erret;
7287 continue;
7288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007289 }
7290 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007291
7292 if (*ea.cmd == '[')
7293 {
7294 // [var, var] = expr
7295 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7296 if (line == NULL)
7297 goto erret;
7298 if (line != ea.cmd)
7299 continue;
7300 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007301 }
7302
7303 /*
7304 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007305 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007306 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007307 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007308 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007309 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007310 ea.cmd = skip_range(ea.cmd, NULL);
7311 if (ea.cmd > cmd && !starts_with_colon)
7312 {
7313 emsg(_(e_colon_required));
7314 goto erret;
7315 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007316 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007317 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007318 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007319 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007320
7321 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7322 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007323 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007324 {
7325 line += STRLEN(line);
7326 continue;
7327 }
7328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007329 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007330 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007331 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007332 // CMD_let cannot happen, compile_assignment() above is used
7333 iemsg("Command from find_ex_command() not handled");
7334 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007335 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007336 }
7337
7338 p = skipwhite(p);
7339
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007340 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007341 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007342 && ea.cmdidx != CMD_elseif
7343 && ea.cmdidx != CMD_else
7344 && ea.cmdidx != CMD_endif)
7345 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007346 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007347 continue;
7348 }
7349
Bram Moolenaarefd88552020-06-18 20:50:10 +02007350 if (ea.cmdidx != CMD_elseif
7351 && ea.cmdidx != CMD_else
7352 && ea.cmdidx != CMD_endif
7353 && ea.cmdidx != CMD_endfor
7354 && ea.cmdidx != CMD_endwhile
7355 && ea.cmdidx != CMD_catch
7356 && ea.cmdidx != CMD_finally
7357 && ea.cmdidx != CMD_endtry)
7358 {
7359 if (cctx.ctx_had_return)
7360 {
7361 emsg(_("E1095: Unreachable code after :return"));
7362 goto erret;
7363 }
7364 }
7365
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366 switch (ea.cmdidx)
7367 {
7368 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007369 ea.arg = p;
7370 line = compile_nested_function(&ea, &cctx);
7371 break;
7372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007373 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007374 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007375 goto erret;
7376
7377 case CMD_return:
7378 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007379 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007380 break;
7381
7382 case CMD_let:
7383 case CMD_const:
7384 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007385 if (line == p)
7386 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387 break;
7388
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007389 case CMD_unlet:
7390 case CMD_unlockvar:
7391 case CMD_lockvar:
7392 line = compile_unletlock(p, &ea, &cctx);
7393 break;
7394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007395 case CMD_import:
7396 line = compile_import(p, &cctx);
7397 break;
7398
7399 case CMD_if:
7400 line = compile_if(p, &cctx);
7401 break;
7402 case CMD_elseif:
7403 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007404 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007405 break;
7406 case CMD_else:
7407 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007408 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007409 break;
7410 case CMD_endif:
7411 line = compile_endif(p, &cctx);
7412 break;
7413
7414 case CMD_while:
7415 line = compile_while(p, &cctx);
7416 break;
7417 case CMD_endwhile:
7418 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007419 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007420 break;
7421
7422 case CMD_for:
7423 line = compile_for(p, &cctx);
7424 break;
7425 case CMD_endfor:
7426 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007427 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007428 break;
7429 case CMD_continue:
7430 line = compile_continue(p, &cctx);
7431 break;
7432 case CMD_break:
7433 line = compile_break(p, &cctx);
7434 break;
7435
7436 case CMD_try:
7437 line = compile_try(p, &cctx);
7438 break;
7439 case CMD_catch:
7440 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007441 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007442 break;
7443 case CMD_finally:
7444 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007445 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446 break;
7447 case CMD_endtry:
7448 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007449 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450 break;
7451 case CMD_throw:
7452 line = compile_throw(p, &cctx);
7453 break;
7454
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007455 case CMD_eval:
7456 if (compile_expr0(&p, &cctx) == FAIL)
7457 goto erret;
7458
7459 // drop the return value
7460 generate_instr_drop(&cctx, ISN_DROP, 1);
7461
7462 line = skipwhite(p);
7463 break;
7464
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007465 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007466 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007467 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007468 case CMD_echomsg:
7469 case CMD_echoerr:
7470 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007471 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007473 // TODO: other commands with an expression argument
7474
Bram Moolenaarae616492020-07-28 20:07:27 +02007475 case CMD_append:
7476 case CMD_change:
7477 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007478 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007479 case CMD_xit:
7480 not_in_vim9(&ea);
7481 goto erret;
7482
Bram Moolenaar002262f2020-07-08 17:47:57 +02007483 case CMD_SIZE:
7484 semsg(_("E476: Invalid command: %s"), ea.cmd);
7485 goto erret;
7486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007488 // Not recognized, execute with do_cmdline_cmd().
7489 ea.arg = p;
7490 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007491 break;
7492 }
7493 if (line == NULL)
7494 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007495 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496
7497 if (cctx.ctx_type_stack.ga_len < 0)
7498 {
7499 iemsg("Type stack underflow");
7500 goto erret;
7501 }
7502 }
7503
7504 if (cctx.ctx_scope != NULL)
7505 {
7506 if (cctx.ctx_scope->se_type == IF_SCOPE)
7507 emsg(_(e_endif));
7508 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7509 emsg(_(e_endwhile));
7510 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7511 emsg(_(e_endfor));
7512 else
7513 emsg(_("E1026: Missing }"));
7514 goto erret;
7515 }
7516
Bram Moolenaarefd88552020-06-18 20:50:10 +02007517 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007518 {
7519 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7520 {
7521 emsg(_("E1027: Missing return statement"));
7522 goto erret;
7523 }
7524
7525 // Return zero if there is no return at the end.
7526 generate_PUSHNR(&cctx, 0);
7527 generate_instr(&cctx, ISN_RETURN);
7528 }
7529
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007530 {
7531 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7532 + ufunc->uf_dfunc_idx;
7533 dfunc->df_deleted = FALSE;
7534 dfunc->df_instr = instr->ga_data;
7535 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007536 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007537 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007538 if (cctx.ctx_outer_used)
7539 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007540 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007541 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007542
7543 ret = OK;
7544
7545erret:
7546 if (ret == FAIL)
7547 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007548 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007549 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7550 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007551
7552 for (idx = 0; idx < instr->ga_len; ++idx)
7553 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007554 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007555
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007556 // If using the last entry in the table and it was added above, we
7557 // might as well remove it.
7558 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007559 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007560 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007561 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007562 ufunc->uf_dfunc_idx = 0;
7563 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007564 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007565
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007566 while (cctx.ctx_scope != NULL)
7567 drop_scope(&cctx);
7568
Bram Moolenaar20431c92020-03-20 18:39:46 +01007569 // Don't execute this function body.
7570 ga_clear_strings(&ufunc->uf_lines);
7571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007572 if (errormsg != NULL)
7573 emsg(errormsg);
7574 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007575 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007576 }
7577
7578 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007579 if (do_estack_push)
7580 estack_pop();
7581
Bram Moolenaar20431c92020-03-20 18:39:46 +01007582 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007583 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007584 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007585 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007586}
7587
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007588 void
7589set_function_type(ufunc_T *ufunc)
7590{
7591 int varargs = ufunc->uf_va_name != NULL;
7592 int argcount = ufunc->uf_args.ga_len;
7593
7594 // Create a type for the function, with the return type and any
7595 // argument types.
7596 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7597 // The type is included in "tt_args".
7598 if (argcount > 0 || varargs)
7599 {
7600 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7601 argcount, &ufunc->uf_type_list);
7602 // Add argument types to the function type.
7603 if (func_type_add_arg_types(ufunc->uf_func_type,
7604 argcount + varargs,
7605 &ufunc->uf_type_list) == FAIL)
7606 return;
7607 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7608 ufunc->uf_func_type->tt_min_argcount =
7609 argcount - ufunc->uf_def_args.ga_len;
7610 if (ufunc->uf_arg_types == NULL)
7611 {
7612 int i;
7613
7614 // lambda does not have argument types.
7615 for (i = 0; i < argcount; ++i)
7616 ufunc->uf_func_type->tt_args[i] = &t_any;
7617 }
7618 else
7619 mch_memmove(ufunc->uf_func_type->tt_args,
7620 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7621 if (varargs)
7622 {
7623 ufunc->uf_func_type->tt_args[argcount] =
7624 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7625 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7626 }
7627 }
7628 else
7629 // No arguments, can use a predefined type.
7630 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7631 argcount, &ufunc->uf_type_list);
7632}
7633
7634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007635/*
7636 * Delete an instruction, free what it contains.
7637 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007638 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007639delete_instr(isn_T *isn)
7640{
7641 switch (isn->isn_type)
7642 {
7643 case ISN_EXEC:
7644 case ISN_LOADENV:
7645 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007646 case ISN_LOADB:
7647 case ISN_LOADW:
7648 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007649 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007650 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007651 case ISN_PUSHEXC:
7652 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007653 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007654 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007655 case ISN_STOREB:
7656 case ISN_STOREW:
7657 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007658 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007659 vim_free(isn->isn_arg.string);
7660 break;
7661
7662 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007663 case ISN_STORES:
7664 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007665 break;
7666
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007667 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007668 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007669 vim_free(isn->isn_arg.unlet.ul_name);
7670 break;
7671
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007672 case ISN_STOREOPT:
7673 vim_free(isn->isn_arg.storeopt.so_name);
7674 break;
7675
7676 case ISN_PUSHBLOB: // push blob isn_arg.blob
7677 blob_unref(isn->isn_arg.blob);
7678 break;
7679
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007680 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007681#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007682 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007683#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007684 break;
7685
7686 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007687#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007688 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007689#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007690 break;
7691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007692 case ISN_UCALL:
7693 vim_free(isn->isn_arg.ufunc.cuf_name);
7694 break;
7695
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007696 case ISN_FUNCREF:
7697 {
7698 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7699 + isn->isn_arg.funcref.fr_func;
7700 func_ptr_unref(dfunc->df_ufunc);
7701 }
7702 break;
7703
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007704 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007705 {
7706 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7707 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7708
7709 if (ufunc != NULL)
7710 {
7711 // Clear uf_dfunc_idx so that the function is deleted.
7712 clear_def_function(ufunc);
7713 ufunc->uf_dfunc_idx = 0;
7714 func_ptr_unref(ufunc);
7715 }
7716
7717 vim_free(lambda);
7718 vim_free(isn->isn_arg.newfunc.nf_global);
7719 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007720 break;
7721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007722 case ISN_2BOOL:
7723 case ISN_2STRING:
7724 case ISN_ADDBLOB:
7725 case ISN_ADDLIST:
7726 case ISN_BCALL:
7727 case ISN_CATCH:
7728 case ISN_CHECKNR:
7729 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007730 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007731 case ISN_COMPAREANY:
7732 case ISN_COMPAREBLOB:
7733 case ISN_COMPAREBOOL:
7734 case ISN_COMPAREDICT:
7735 case ISN_COMPAREFLOAT:
7736 case ISN_COMPAREFUNC:
7737 case ISN_COMPARELIST:
7738 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007739 case ISN_COMPARESPECIAL:
7740 case ISN_COMPARESTRING:
7741 case ISN_CONCAT:
7742 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007743 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007744 case ISN_DROP:
7745 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007746 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007747 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007748 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007749 case ISN_EXECCONCAT:
7750 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007751 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007752 case ISN_LISTINDEX:
7753 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007754 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007755 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007756 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007757 case ISN_JUMP:
7758 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007759 case ISN_LOADBDICT:
7760 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007761 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007762 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007763 case ISN_LOADSCRIPT:
7764 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007765 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007766 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007767 case ISN_NEGATENR:
7768 case ISN_NEWDICT:
7769 case ISN_NEWLIST:
7770 case ISN_OPNR:
7771 case ISN_OPFLOAT:
7772 case ISN_OPANY:
7773 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007774 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007775 case ISN_PUSHF:
7776 case ISN_PUSHNR:
7777 case ISN_PUSHBOOL:
7778 case ISN_PUSHSPEC:
7779 case ISN_RETURN:
7780 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007781 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007782 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007783 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007784 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007785 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007786 case ISN_STOREDICT:
7787 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007788 case ISN_THROW:
7789 case ISN_TRY:
7790 // nothing allocated
7791 break;
7792 }
7793}
7794
7795/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007796 * Free all instructions for "dfunc".
7797 */
7798 static void
7799delete_def_function_contents(dfunc_T *dfunc)
7800{
7801 int idx;
7802
7803 ga_clear(&dfunc->df_def_args_isn);
7804
7805 if (dfunc->df_instr != NULL)
7806 {
7807 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7808 delete_instr(dfunc->df_instr + idx);
7809 VIM_CLEAR(dfunc->df_instr);
7810 }
7811
7812 dfunc->df_deleted = TRUE;
7813}
7814
7815/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007816 * When a user function is deleted, clear the contents of any associated def
7817 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007818 */
7819 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007820clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007821{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007822 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007823 {
7824 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7825 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007826
Bram Moolenaar20431c92020-03-20 18:39:46 +01007827 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007828 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007829 }
7830}
7831
7832#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007833/*
7834 * Free all functions defined with ":def".
7835 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007836 void
7837free_def_functions(void)
7838{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007839 int idx;
7840
7841 for (idx = 0; idx < def_functions.ga_len; ++idx)
7842 {
7843 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7844
7845 delete_def_function_contents(dfunc);
7846 }
7847
7848 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007849}
7850#endif
7851
7852
7853#endif // FEAT_EVAL