blob: 5999c39614a796b436c07038fa3820ef26e97bf8 [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 }
3568 if (!valid_yank_reg(**arg, TRUE))
3569 {
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/*
4247 * * number multiplication
4248 * / number division
4249 * % number modulo
4250 */
4251 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004252compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253{
4254 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004255 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004256 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004258 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004259 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 return FAIL;
4261
4262 /*
4263 * Repeat computing, until no "*", "/" or "%" is following.
4264 */
4265 for (;;)
4266 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004267 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 if (*op != '*' && *op != '/' && *op != '%')
4269 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004270 if (next != NULL)
4271 {
4272 *arg = next_line_from_context(cctx, TRUE);
4273 op = skipwhite(*arg);
4274 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004275
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004276 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 {
4278 char_u buf[3];
4279
4280 vim_strncpy(buf, op, 1);
4281 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004282 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 }
4284 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004285 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004286 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004288 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004289 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004291
4292 if (ppconst->pp_used == ppconst_used + 2
4293 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4294 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004295 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004296 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4297 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004298 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004300 // both are numbers: compute the result
4301 switch (*op)
4302 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004303 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004304 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004305 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004306 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004307 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004308 break;
4309 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004310 tv1->vval.v_number = res;
4311 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004312 }
4313 else
4314 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004315 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004316 generate_two_op(cctx, op);
4317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 }
4319
4320 return OK;
4321}
4322
4323/*
4324 * + number addition
4325 * - number subtraction
4326 * .. string concatenation
4327 */
4328 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004329compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330{
4331 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004332 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004334 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335
4336 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004337 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 return FAIL;
4339
4340 /*
4341 * Repeat computing, until no "+", "-" or ".." is following.
4342 */
4343 for (;;)
4344 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004345 op = may_peek_next_line(cctx, *arg, &next);
4346 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347 break;
4348 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004349 if (next != NULL)
4350 {
4351 *arg = next_line_from_context(cctx, TRUE);
4352 op = skipwhite(*arg);
4353 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004355 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 {
4357 char_u buf[3];
4358
4359 vim_strncpy(buf, op, oplen);
4360 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004361 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 }
4363
4364 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004365 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004366 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004368 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004369 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 return FAIL;
4371
Bram Moolenaara5565e42020-05-09 15:44:01 +02004372 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004373 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004374 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4375 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4376 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4377 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004379 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4380 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004381
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004382 // concat/subtract/add constant numbers
4383 if (*op == '+')
4384 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4385 else if (*op == '-')
4386 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4387 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004388 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004389 // concatenate constant strings
4390 char_u *s1 = tv1->vval.v_string;
4391 char_u *s2 = tv2->vval.v_string;
4392 size_t len1 = STRLEN(s1);
4393
4394 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4395 if (tv1->vval.v_string == NULL)
4396 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004397 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004398 return FAIL;
4399 }
4400 mch_memmove(tv1->vval.v_string, s1, len1);
4401 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004402 vim_free(s1);
4403 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004404 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004405 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 }
4407 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004408 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004409 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004410 if (*op == '.')
4411 {
4412 if (may_generate_2STRING(-2, cctx) == FAIL
4413 || may_generate_2STRING(-1, cctx) == FAIL)
4414 return FAIL;
4415 generate_instr_drop(cctx, ISN_CONCAT, 1);
4416 }
4417 else
4418 generate_two_op(cctx, op);
4419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 }
4421
4422 return OK;
4423}
4424
4425/*
4426 * expr5a == expr5b
4427 * expr5a =~ expr5b
4428 * expr5a != expr5b
4429 * expr5a !~ expr5b
4430 * expr5a > expr5b
4431 * expr5a >= expr5b
4432 * expr5a < expr5b
4433 * expr5a <= expr5b
4434 * expr5a is expr5b
4435 * expr5a isnot expr5b
4436 *
4437 * Produces instructions:
4438 * EVAL expr5a Push result of "expr5a"
4439 * EVAL expr5b Push result of "expr5b"
4440 * COMPARE one of the compare instructions
4441 */
4442 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004443compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444{
4445 exptype_T type = EXPR_UNKNOWN;
4446 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004447 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004450 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451
4452 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004453 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454 return FAIL;
4455
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004456 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004457 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458
4459 /*
4460 * If there is a comparative operator, use it.
4461 */
4462 if (type != EXPR_UNKNOWN)
4463 {
4464 int ic = FALSE; // Default: do not ignore case
4465
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004466 if (next != NULL)
4467 {
4468 *arg = next_line_from_context(cctx, TRUE);
4469 p = skipwhite(*arg);
4470 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471 if (type_is && (p[len] == '?' || p[len] == '#'))
4472 {
4473 semsg(_(e_invexpr2), *arg);
4474 return FAIL;
4475 }
4476 // extra question mark appended: ignore case
4477 if (p[len] == '?')
4478 {
4479 ic = TRUE;
4480 ++len;
4481 }
4482 // extra '#' appended: match case (ignored)
4483 else if (p[len] == '#')
4484 ++len;
4485 // nothing appended: match case
4486
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004487 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 {
4489 char_u buf[7];
4490
4491 vim_strncpy(buf, p, len);
4492 semsg(_(e_white_both), buf);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004493 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 }
4495
4496 // get the second variable
4497 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004498 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004499 return FAIL;
4500
Bram Moolenaara5565e42020-05-09 15:44:01 +02004501 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502 return FAIL;
4503
Bram Moolenaara5565e42020-05-09 15:44:01 +02004504 if (ppconst->pp_used == ppconst_used + 2)
4505 {
4506 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4507 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4508 int ret;
4509
4510 // Both sides are a constant, compute the result now.
4511 // First check for a valid combination of types, this is more
4512 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004513 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004514 ret = FAIL;
4515 else
4516 {
4517 ret = typval_compare(tv1, tv2, type, ic);
4518 tv1->v_type = VAR_BOOL;
4519 tv1->vval.v_number = tv1->vval.v_number
4520 ? VVAL_TRUE : VVAL_FALSE;
4521 clear_tv(tv2);
4522 --ppconst->pp_used;
4523 }
4524 return ret;
4525 }
4526
4527 generate_ppconst(cctx, ppconst);
4528 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 }
4530
4531 return OK;
4532}
4533
Bram Moolenaar7f141552020-05-09 17:35:53 +02004534static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536/*
4537 * Compile || or &&.
4538 */
4539 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004540compile_and_or(
4541 char_u **arg,
4542 cctx_T *cctx,
4543 char *op,
4544 ppconst_T *ppconst,
4545 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004547 char_u *next;
4548 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 int opchar = *op;
4550
4551 if (p[0] == opchar && p[1] == opchar)
4552 {
4553 garray_T *instr = &cctx->ctx_instr;
4554 garray_T end_ga;
4555
4556 /*
4557 * Repeat until there is no following "||" or "&&"
4558 */
4559 ga_init2(&end_ga, sizeof(int), 10);
4560 while (p[0] == opchar && p[1] == opchar)
4561 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004562 if (next != NULL)
4563 {
4564 *arg = next_line_from_context(cctx, TRUE);
4565 p = skipwhite(*arg);
4566 }
4567
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004568 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4569 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004571 return FAIL;
4572 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573
Bram Moolenaara5565e42020-05-09 15:44:01 +02004574 // TODO: use ppconst if the value is a constant
4575 generate_ppconst(cctx, ppconst);
4576
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577 if (ga_grow(&end_ga, 1) == FAIL)
4578 {
4579 ga_clear(&end_ga);
4580 return FAIL;
4581 }
4582 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4583 ++end_ga.ga_len;
4584 generate_JUMP(cctx, opchar == '|'
4585 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4586
4587 // eval the next expression
4588 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004589 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004590 return FAIL;
4591
Bram Moolenaara5565e42020-05-09 15:44:01 +02004592 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4593 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 {
4595 ga_clear(&end_ga);
4596 return FAIL;
4597 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004598
4599 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004601 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602
4603 // Fill in the end label in all jumps.
4604 while (end_ga.ga_len > 0)
4605 {
4606 isn_T *isn;
4607
4608 --end_ga.ga_len;
4609 isn = ((isn_T *)instr->ga_data)
4610 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4611 isn->isn_arg.jump.jump_where = instr->ga_len;
4612 }
4613 ga_clear(&end_ga);
4614 }
4615
4616 return OK;
4617}
4618
4619/*
4620 * expr4a && expr4a && expr4a logical AND
4621 *
4622 * Produces instructions:
4623 * EVAL expr4a Push result of "expr4a"
4624 * JUMP_AND_KEEP_IF_FALSE end
4625 * EVAL expr4b Push result of "expr4b"
4626 * JUMP_AND_KEEP_IF_FALSE end
4627 * EVAL expr4c Push result of "expr4c"
4628 * end:
4629 */
4630 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004631compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004633 int ppconst_used = ppconst->pp_used;
4634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004636 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637 return FAIL;
4638
4639 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004640 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641}
4642
4643/*
4644 * expr3a || expr3b || expr3c logical OR
4645 *
4646 * Produces instructions:
4647 * EVAL expr3a Push result of "expr3a"
4648 * JUMP_AND_KEEP_IF_TRUE end
4649 * EVAL expr3b Push result of "expr3b"
4650 * JUMP_AND_KEEP_IF_TRUE end
4651 * EVAL expr3c Push result of "expr3c"
4652 * end:
4653 */
4654 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004655compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004657 int ppconst_used = ppconst->pp_used;
4658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004660 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 return FAIL;
4662
4663 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004664 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665}
4666
4667/*
4668 * Toplevel expression: expr2 ? expr1a : expr1b
4669 *
4670 * Produces instructions:
4671 * EVAL expr2 Push result of "expr"
4672 * JUMP_IF_FALSE alt jump if false
4673 * EVAL expr1a
4674 * JUMP_ALWAYS end
4675 * alt: EVAL expr1b
4676 * end:
4677 */
4678 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004679compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680{
4681 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004682 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004683 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684
Bram Moolenaar61a89812020-05-07 16:58:17 +02004685 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004686 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687 return FAIL;
4688
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004689 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690 if (*p == '?')
4691 {
4692 garray_T *instr = &cctx->ctx_instr;
4693 garray_T *stack = &cctx->ctx_type_stack;
4694 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004695 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004697 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004699 int has_const_expr = FALSE;
4700 int const_value = FALSE;
4701 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004703 if (next != NULL)
4704 {
4705 *arg = next_line_from_context(cctx, TRUE);
4706 p = skipwhite(*arg);
4707 }
4708
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004709 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4710 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004711 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004712 return FAIL;
4713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714
Bram Moolenaara5565e42020-05-09 15:44:01 +02004715 if (ppconst->pp_used == ppconst_used + 1)
4716 {
4717 // the condition is a constant, we know whether the ? or the :
4718 // expression is to be evaluated.
4719 has_const_expr = TRUE;
4720 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4721 clear_tv(&ppconst->pp_tv[ppconst_used]);
4722 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004723 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4724 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004725 }
4726 else
4727 {
4728 generate_ppconst(cctx, ppconst);
4729 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4730 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731
4732 // evaluate the second expression; any type is accepted
4733 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004734 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004735 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004736 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004737 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738
Bram Moolenaara5565e42020-05-09 15:44:01 +02004739 if (!has_const_expr)
4740 {
4741 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742
Bram Moolenaara5565e42020-05-09 15:44:01 +02004743 // remember the type and drop it
4744 --stack->ga_len;
4745 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746
Bram Moolenaara5565e42020-05-09 15:44:01 +02004747 end_idx = instr->ga_len;
4748 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4749
4750 // jump here from JUMP_IF_FALSE
4751 isn = ((isn_T *)instr->ga_data) + alt_idx;
4752 isn->isn_arg.jump.jump_where = instr->ga_len;
4753 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004754
4755 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004756 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757 if (*p != ':')
4758 {
4759 emsg(_(e_missing_colon));
4760 return FAIL;
4761 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004762 if (next != NULL)
4763 {
4764 *arg = next_line_from_context(cctx, TRUE);
4765 p = skipwhite(*arg);
4766 }
4767
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004768 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4769 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004771 return FAIL;
4772 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773
4774 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004775 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004776 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4777 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004779 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004780 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004781 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004782 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004783
Bram Moolenaara5565e42020-05-09 15:44:01 +02004784 if (!has_const_expr)
4785 {
4786 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787
Bram Moolenaara5565e42020-05-09 15:44:01 +02004788 // If the types differ, the result has a more generic type.
4789 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4790 common_type(type1, type2, &type2, cctx->ctx_type_list);
4791
4792 // jump here from JUMP_ALWAYS
4793 isn = ((isn_T *)instr->ga_data) + end_idx;
4794 isn->isn_arg.jump.jump_where = instr->ga_len;
4795 }
4796
4797 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 }
4799 return OK;
4800}
4801
4802/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004803 * Toplevel expression.
4804 */
4805 static int
4806compile_expr0(char_u **arg, cctx_T *cctx)
4807{
4808 ppconst_T ppconst;
4809
4810 CLEAR_FIELD(ppconst);
4811 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4812 {
4813 clear_ppconst(&ppconst);
4814 return FAIL;
4815 }
4816 if (generate_ppconst(cctx, &ppconst) == FAIL)
4817 return FAIL;
4818 return OK;
4819}
4820
4821/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 * compile "return [expr]"
4823 */
4824 static char_u *
4825compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4826{
4827 char_u *p = arg;
4828 garray_T *stack = &cctx->ctx_type_stack;
4829 type_T *stack_type;
4830
4831 if (*p != NUL && *p != '|' && *p != '\n')
4832 {
4833 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004834 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835 return NULL;
4836
4837 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4838 if (set_return_type)
4839 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004840 else
4841 {
4842 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4843 && stack_type->tt_type != VAR_VOID
4844 && stack_type->tt_type != VAR_UNKNOWN)
4845 {
4846 emsg(_("E1096: Returning a value in a function without a return type"));
4847 return NULL;
4848 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004849 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4850 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004852 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 }
4854 else
4855 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004856 // "set_return_type" cannot be TRUE, only used for a lambda which
4857 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004858 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4859 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860 {
4861 emsg(_("E1003: Missing return value"));
4862 return NULL;
4863 }
4864
4865 // No argument, return zero.
4866 generate_PUSHNR(cctx, 0);
4867 }
4868
4869 if (generate_instr(cctx, ISN_RETURN) == NULL)
4870 return NULL;
4871
4872 // "return val | endif" is possible
4873 return skipwhite(p);
4874}
4875
4876/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004877 * Get a line from the compilation context, compatible with exarg_T getline().
4878 * Return a pointer to the line in allocated memory.
4879 * Return NULL for end-of-file or some error.
4880 */
4881 static char_u *
4882exarg_getline(
4883 int c UNUSED,
4884 void *cookie,
4885 int indent UNUSED,
4886 int do_concat UNUSED)
4887{
4888 cctx_T *cctx = (cctx_T *)cookie;
4889
4890 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4891 {
4892 iemsg("Heredoc got to end");
4893 return NULL;
4894 }
4895 ++cctx->ctx_lnum;
4896 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4897 [cctx->ctx_lnum]);
4898}
4899
4900/*
4901 * Compile a nested :def command.
4902 */
4903 static char_u *
4904compile_nested_function(exarg_T *eap, cctx_T *cctx)
4905{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004906 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004907 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004908 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004909 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004910 lvar_T *lvar;
4911 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004912 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004913
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004914 // Only g:Func() can use a namespace.
4915 if (name_start[1] == ':' && !is_global)
4916 {
4917 semsg(_(e_namespace), name_start);
4918 return NULL;
4919 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004920 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4921 return NULL;
4922
Bram Moolenaar04b12692020-05-04 23:24:44 +02004923 eap->arg = name_end;
4924 eap->getline = exarg_getline;
4925 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004926 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004927 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004928 lambda_name = get_lambda_name();
4929 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004930
Bram Moolenaar822ba242020-05-24 23:00:18 +02004931 if (ufunc == NULL)
4932 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004933 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004934 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004935 return NULL;
4936
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004937 if (is_global)
4938 {
4939 char_u *func_name = vim_strnsave(name_start + 2,
4940 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004941
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004942 if (func_name == NULL)
4943 r = FAIL;
4944 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004945 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004946 }
4947 else
4948 {
4949 // Define a local variable for the function reference.
4950 lvar = reserve_local(cctx, name_start, name_end - name_start,
4951 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004952 if (lvar == NULL)
4953 return NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004954 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL)
4955 return NULL;
4956 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4957 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004958
Bram Moolenaar61a89812020-05-07 16:58:17 +02004959 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004960 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004961}
4962
4963/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964 * Return the length of an assignment operator, or zero if there isn't one.
4965 */
4966 int
4967assignment_len(char_u *p, int *heredoc)
4968{
4969 if (*p == '=')
4970 {
4971 if (p[1] == '<' && p[2] == '<')
4972 {
4973 *heredoc = TRUE;
4974 return 3;
4975 }
4976 return 1;
4977 }
4978 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4979 return 2;
4980 if (STRNCMP(p, "..=", 3) == 0)
4981 return 3;
4982 return 0;
4983}
4984
4985// words that cannot be used as a variable
4986static char *reserved[] = {
4987 "true",
4988 "false",
4989 NULL
4990};
4991
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004992typedef enum {
4993 dest_local,
4994 dest_option,
4995 dest_env,
4996 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004997 dest_buffer,
4998 dest_window,
4999 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005000 dest_vimvar,
5001 dest_script,
5002 dest_reg,
5003} assign_dest_T;
5004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005006 * Generate the load instruction for "name".
5007 */
5008 static void
5009generate_loadvar(
5010 cctx_T *cctx,
5011 assign_dest_T dest,
5012 char_u *name,
5013 lvar_T *lvar,
5014 type_T *type)
5015{
5016 switch (dest)
5017 {
5018 case dest_option:
5019 // TODO: check the option exists
5020 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5021 break;
5022 case dest_global:
5023 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5024 break;
5025 case dest_buffer:
5026 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5027 break;
5028 case dest_window:
5029 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5030 break;
5031 case dest_tab:
5032 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5033 break;
5034 case dest_script:
5035 compile_load_scriptvar(cctx,
5036 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5037 break;
5038 case dest_env:
5039 // Include $ in the name here
5040 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5041 break;
5042 case dest_reg:
5043 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5044 break;
5045 case dest_vimvar:
5046 generate_LOADV(cctx, name + 2, TRUE);
5047 break;
5048 case dest_local:
5049 if (lvar->lv_from_outer)
5050 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5051 NULL, type);
5052 else
5053 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5054 break;
5055 }
5056}
5057
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005058 void
5059vim9_declare_error(char_u *name)
5060{
5061 char *scope = "";
5062
5063 switch (*name)
5064 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005065 case 'g': scope = _("global"); break;
5066 case 'b': scope = _("buffer"); break;
5067 case 'w': scope = _("window"); break;
5068 case 't': scope = _("tab"); break;
5069 case 'v': scope = "v:"; break;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005070 case '$': semsg(_(e_declare_env_var), name);
5071 return;
5072 case '&': semsg(_("E1052: Cannot declare an option: %s"), name);
5073 return;
5074 case '@': semsg(_("E1066: Cannot declare a register: %s"), name);
5075 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005076 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005077 }
5078 semsg(_(e_declare_var), scope, name);
5079}
5080
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005081/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005082 * Compile declaration and assignment:
5083 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005085 * Return NULL for an error.
5086 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005087 */
5088 static char_u *
5089compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5090{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005091 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005092 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005093 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094 char_u *ret = NULL;
5095 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005096 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005097 int scriptvar_sid = 0;
5098 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005099 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005100 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005101 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103 int oplen = 0;
5104 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005105 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005106 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005107 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005108 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005111 // Skip over the "var" or "[var, var]" to get to any "=".
5112 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5113 if (p == NULL)
5114 return *arg == '[' ? arg : NULL;
5115
5116 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005117 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005118 // TODO: should we allow this, and figure out type inference from list
5119 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005120 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121 return NULL;
5122 }
5123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005124 sp = p;
5125 p = skipwhite(p);
5126 op = p;
5127 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005128
5129 if (var_count > 0 && oplen == 0)
5130 // can be something like "[1, 2]->func()"
5131 return arg;
5132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005133 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5134 {
5135 char_u buf[4];
5136
5137 vim_strncpy(buf, op, oplen);
5138 semsg(_(e_white_both), buf);
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
5217 p = (*var_start == '&' || *var_start == '$'
5218 || *var_start == '@') ? var_start + 1 : var_start;
5219 p = to_name_end(p, TRUE);
5220
5221 // "a: type" is declaring variable "a" with a type, not "a:".
5222 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5223 --var_end;
5224 if (is_decl && p == var_start + 2 && p[-1] == ':')
5225 --p;
5226
5227 varlen = p - var_start;
5228 vim_free(name);
5229 name = vim_strnsave(var_start, varlen);
5230 if (name == NULL)
5231 return NULL;
5232 if (!heredoc)
5233 type = &t_any;
5234
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005235 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005236 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005237 int declare_error = FALSE;
5238
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005239 if (*var_start == '&')
5240 {
5241 int cc;
5242 long numval;
5243
5244 dest = dest_option;
5245 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005246 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005247 emsg(_(e_const_option));
5248 goto theend;
5249 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005250 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005251 p = var_start;
5252 p = find_option_end(&p, &opt_flags);
5253 if (p == NULL)
5254 {
5255 // cannot happen?
5256 emsg(_(e_letunexp));
5257 goto theend;
5258 }
5259 cc = *p;
5260 *p = NUL;
5261 opt_type = get_option_value(var_start + 1, &numval,
5262 NULL, opt_flags);
5263 *p = cc;
5264 if (opt_type == -3)
5265 {
5266 semsg(_(e_unknown_option), var_start);
5267 goto theend;
5268 }
5269 if (opt_type == -2 || opt_type == 0)
5270 type = &t_string;
5271 else
5272 type = &t_number; // both number and boolean option
5273 }
5274 else if (*var_start == '$')
5275 {
5276 dest = dest_env;
5277 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005278 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005279 }
5280 else if (*var_start == '@')
5281 {
5282 if (!valid_yank_reg(var_start[1], TRUE))
5283 {
5284 emsg_invreg(var_start[1]);
5285 goto theend;
5286 }
5287 dest = dest_reg;
5288 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005289 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005290 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005291 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005292 {
5293 dest = dest_global;
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, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005297 {
5298 dest = dest_buffer;
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, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005302 {
5303 dest = dest_window;
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, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005307 {
5308 dest = dest_tab;
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, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005312 {
5313 typval_T *vtv;
5314 int di_flags;
5315
5316 vimvaridx = find_vim_var(name + 2, &di_flags);
5317 if (vimvaridx < 0)
5318 {
5319 semsg(_(e_var_notfound), var_start);
5320 goto theend;
5321 }
5322 // We use the current value of "sandbox" here, is that OK?
5323 if (var_check_ro(di_flags, name, FALSE))
5324 goto theend;
5325 dest = dest_vimvar;
5326 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005327 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005328 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005329 }
5330 else
5331 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005332 int idx;
5333 imported_T *import = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005334
5335 for (idx = 0; reserved[idx] != NULL; ++idx)
5336 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005337 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005338 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005339 goto theend;
5340 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005341
5342 lvar = lookup_local(var_start, varlen, cctx);
5343 if (lvar == NULL)
5344 {
5345 CLEAR_FIELD(arg_lvar);
5346 if (lookup_arg(var_start, varlen,
5347 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5348 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005349 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005350 if (is_decl)
5351 {
5352 semsg(_(e_used_as_arg), name);
5353 goto theend;
5354 }
5355 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005356 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005357 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005358 if (lvar != NULL)
5359 {
5360 if (is_decl)
5361 {
5362 semsg(_("E1017: Variable already declared: %s"), name);
5363 goto theend;
5364 }
5365 else if (lvar->lv_const)
5366 {
5367 semsg(_("E1018: Cannot assign to a constant: %s"),
5368 name);
5369 goto theend;
5370 }
5371 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005372 else if ((varlen > 1 && STRNCMP(var_start, "s:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005373 || lookup_script(var_start, varlen) == OK
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005374 || (import = find_imported(var_start, varlen, cctx))
5375 != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005376 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005377 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5378
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005379 if (is_decl)
5380 {
Bram Moolenaar33afa242020-07-29 19:18:00 +02005381 if ((varlen > 1 && STRNCMP(var_start, "s:", 2) == 0))
5382 semsg(_("E1101: Cannot declare a script variable in a function: %s"),
5383 name);
5384 else
5385 semsg(_("E1054: Variable already declared in the script: %s"),
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005386 name);
5387 goto theend;
5388 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005389 dest = dest_script;
5390
5391 // existing script-local variables should have a type
5392 scriptvar_sid = current_sctx.sc_sid;
5393 if (import != NULL)
5394 scriptvar_sid = import->imp_sid;
5395 scriptvar_idx = get_script_item_idx(scriptvar_sid,
5396 rawname, TRUE);
5397 if (scriptvar_idx >= 0)
5398 {
5399 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5400 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
5401 + scriptvar_idx;
5402 type = sv->sv_type;
5403 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005404 }
5405 else if (name[1] == ':' && name[2] != NUL)
5406 {
5407 semsg(_("E1082: Cannot use a namespaced variable: %s"),
5408 name);
5409 goto theend;
5410 }
5411 else if (!is_decl)
5412 {
5413 semsg(_("E1089: unknown variable: %s"), name);
5414 goto theend;
5415 }
5416 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005417
5418 if (declare_error)
5419 {
5420 vim9_declare_error(name);
5421 goto theend;
5422 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005423 }
5424
5425 // handle "a:name" as a name, not index "name" on "a"
5426 if (varlen > 1 || var_start[varlen] != ':')
5427 p = var_end;
5428
5429 if (dest != dest_option)
5430 {
5431 if (is_decl && *p == ':')
5432 {
5433 // parse optional type: "let var: type = expr"
5434 if (!VIM_ISWHITE(p[1]))
5435 {
5436 semsg(_(e_white_after), ":");
5437 goto theend;
5438 }
5439 p = skipwhite(p + 1);
5440 type = parse_type(&p, cctx->ctx_type_list);
5441 has_type = TRUE;
5442 }
5443 else if (lvar != NULL)
5444 type = lvar->lv_type;
5445 }
5446
5447 if (oplen == 3 && !heredoc && dest != dest_global
5448 && type->tt_type != VAR_STRING
5449 && type->tt_type != VAR_ANY)
5450 {
5451 emsg(_("E1019: Can only concatenate to string"));
5452 goto theend;
5453 }
5454
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005455 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005456 {
5457 if (oplen > 1 && !heredoc)
5458 {
5459 // +=, /=, etc. require an existing variable
5460 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5461 name);
5462 goto theend;
5463 }
5464
5465 // new local variable
5466 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5467 goto theend;
5468 lvar = reserve_local(cctx, var_start, varlen,
5469 cmdidx == CMD_const, type);
5470 if (lvar == NULL)
5471 goto theend;
5472 new_local = TRUE;
5473 }
5474
5475 member_type = type;
5476 if (var_end > var_start + varlen)
5477 {
5478 // Something follows after the variable: "var[idx]".
5479 if (is_decl)
5480 {
5481 emsg(_("E1087: cannot use an index when declaring a variable"));
5482 goto theend;
5483 }
5484
5485 if (var_start[varlen] == '[')
5486 {
5487 has_index = TRUE;
5488 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005489 member_type = &t_any;
5490 else
5491 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005492 }
5493 else
5494 {
5495 semsg("Not supported yet: %s", var_start);
5496 goto theend;
5497 }
5498 }
5499 else if (lvar == &arg_lvar)
5500 {
5501 semsg(_("E1090: Cannot assign to argument %s"), name);
5502 goto theend;
5503 }
5504
5505 if (!heredoc)
5506 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005507 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005508 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005509 if (oplen > 0 && var_count == 0)
5510 {
5511 // skip over the "=" and the expression
5512 p = skipwhite(op + oplen);
5513 compile_expr0(&p, cctx);
5514 }
5515 }
5516 else if (oplen > 0)
5517 {
5518 type_T *stacktype;
5519
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005520 // For "var = expr" evaluate the expression.
5521 if (var_count == 0)
5522 {
5523 int r;
5524
5525 // for "+=", "*=", "..=" etc. first load the current value
5526 if (*op != '=')
5527 {
5528 generate_loadvar(cctx, dest, name, lvar, type);
5529
5530 if (has_index)
5531 {
5532 // TODO: get member from list or dict
5533 emsg("Index with operation not supported yet");
5534 goto theend;
5535 }
5536 }
5537
5538 // Compile the expression. Temporarily hide the new local
5539 // variable here, it is not available to this expression.
5540 if (new_local)
5541 --cctx->ctx_locals.ga_len;
5542 instr_count = instr->ga_len;
5543 p = skipwhite(op + oplen);
5544 r = compile_expr0(&p, cctx);
5545 if (new_local)
5546 ++cctx->ctx_locals.ga_len;
5547 if (r == FAIL)
5548 goto theend;
5549 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005550 else if (semicolon && var_idx == var_count - 1)
5551 {
5552 // For "[var; var] = expr" get the rest of the list
5553 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5554 goto theend;
5555 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005556 else
5557 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005558 // For "[var, var] = expr" get the "var_idx" item from the
5559 // list.
5560 if (generate_GETITEM(cctx, var_idx) == FAIL)
5561 return FAIL;
5562 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005563
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005564 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005565 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005566 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005567 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005568 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005569 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005570 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005571 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005572 emsg(_(e_cannot_use_void));
5573 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005574 }
5575 else
5576 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005577 // An empty list or dict has a &t_void member,
5578 // for a variable that implies &t_any.
5579 if (stacktype == &t_list_empty)
5580 lvar->lv_type = &t_list_any;
5581 else if (stacktype == &t_dict_empty)
5582 lvar->lv_type = &t_dict_any;
5583 else
5584 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005585 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005586 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005587 else
5588 {
5589 type_T *use_type = lvar->lv_type;
5590
5591 if (has_index)
5592 {
5593 use_type = use_type->tt_member;
5594 if (use_type == NULL)
5595 use_type = &t_void;
5596 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005597 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005598 == FAIL)
5599 goto theend;
5600 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005601 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005602 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005603 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005604 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005606 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005607 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005608 emsg(_(e_const_req_value));
5609 goto theend;
5610 }
5611 else if (!has_type || dest == dest_option)
5612 {
5613 emsg(_(e_type_req));
5614 goto theend;
5615 }
5616 else
5617 {
5618 // variables are always initialized
5619 if (ga_grow(instr, 1) == FAIL)
5620 goto theend;
5621 switch (member_type->tt_type)
5622 {
5623 case VAR_BOOL:
5624 generate_PUSHBOOL(cctx, VVAL_FALSE);
5625 break;
5626 case VAR_FLOAT:
5627#ifdef FEAT_FLOAT
5628 generate_PUSHF(cctx, 0.0);
5629#endif
5630 break;
5631 case VAR_STRING:
5632 generate_PUSHS(cctx, NULL);
5633 break;
5634 case VAR_BLOB:
5635 generate_PUSHBLOB(cctx, NULL);
5636 break;
5637 case VAR_FUNC:
5638 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5639 break;
5640 case VAR_LIST:
5641 generate_NEWLIST(cctx, 0);
5642 break;
5643 case VAR_DICT:
5644 generate_NEWDICT(cctx, 0);
5645 break;
5646 case VAR_JOB:
5647 generate_PUSHJOB(cctx, NULL);
5648 break;
5649 case VAR_CHANNEL:
5650 generate_PUSHCHANNEL(cctx, NULL);
5651 break;
5652 case VAR_NUMBER:
5653 case VAR_UNKNOWN:
5654 case VAR_ANY:
5655 case VAR_PARTIAL:
5656 case VAR_VOID:
5657 case VAR_SPECIAL: // cannot happen
5658 generate_PUSHNR(cctx, 0);
5659 break;
5660 }
5661 }
5662 if (var_count == 0)
5663 end = p;
5664 }
5665
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005666 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005667 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005668 break;
5669
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005670 if (oplen > 0 && *op != '=')
5671 {
5672 type_T *expected = &t_number;
5673 type_T *stacktype;
5674
5675 // TODO: if type is known use float or any operation
5676
5677 if (*op == '.')
5678 expected = &t_string;
5679 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005680 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005681 goto theend;
5682
5683 if (*op == '.')
5684 generate_instr_drop(cctx, ISN_CONCAT, 1);
5685 else
5686 {
5687 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5688
5689 if (isn == NULL)
5690 goto theend;
5691 switch (*op)
5692 {
5693 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5694 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5695 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5696 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5697 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5698 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005699 }
5700 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005701
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005702 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005703 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005704 int r;
5705
5706 // Compile the "idx" in "var[idx]".
5707 if (new_local)
5708 --cctx->ctx_locals.ga_len;
5709 p = skipwhite(var_start + varlen + 1);
5710 r = compile_expr0(&p, cctx);
5711 if (new_local)
5712 ++cctx->ctx_locals.ga_len;
5713 if (r == FAIL)
5714 goto theend;
5715 if (*skipwhite(p) != ']')
5716 {
5717 emsg(_(e_missbrac));
5718 goto theend;
5719 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005720 if (type == &t_any)
5721 {
5722 type_T *idx_type = ((type_T **)stack->ga_data)[
5723 stack->ga_len - 1];
5724 // Index on variable of unknown type: guess the type from the
5725 // index type: number is dict, otherwise dict.
5726 // TODO: should do the assignment at runtime
5727 if (idx_type->tt_type == VAR_NUMBER)
5728 type = &t_list_any;
5729 else
5730 type = &t_dict_any;
5731 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005732 if (type->tt_type == VAR_DICT
5733 && may_generate_2STRING(-1, cctx) == FAIL)
5734 goto theend;
5735 if (type->tt_type == VAR_LIST
5736 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005737 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005738 {
5739 emsg(_(e_number_exp));
5740 goto theend;
5741 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005742
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005743 // Load the dict or list. On the stack we then have:
5744 // - value
5745 // - index
5746 // - variable
5747 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005748
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005749 if (type->tt_type == VAR_LIST)
5750 {
5751 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5752 return FAIL;
5753 }
5754 else if (type->tt_type == VAR_DICT)
5755 {
5756 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5757 return FAIL;
5758 }
5759 else
5760 {
5761 emsg(_(e_listreq));
5762 goto theend;
5763 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005764 }
5765 else
5766 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005767 switch (dest)
5768 {
5769 case dest_option:
5770 generate_STOREOPT(cctx, name + 1, opt_flags);
5771 break;
5772 case dest_global:
5773 // include g: with the name, easier to execute that way
5774 generate_STORE(cctx, ISN_STOREG, 0, name);
5775 break;
5776 case dest_buffer:
5777 // include b: with the name, easier to execute that way
5778 generate_STORE(cctx, ISN_STOREB, 0, name);
5779 break;
5780 case dest_window:
5781 // include w: with the name, easier to execute that way
5782 generate_STORE(cctx, ISN_STOREW, 0, name);
5783 break;
5784 case dest_tab:
5785 // include t: with the name, easier to execute that way
5786 generate_STORE(cctx, ISN_STORET, 0, name);
5787 break;
5788 case dest_env:
5789 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5790 break;
5791 case dest_reg:
5792 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5793 break;
5794 case dest_vimvar:
5795 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5796 break;
5797 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005798 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005799 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005800 {
5801 char_u *name_s = name;
5802
5803 // Include s: in the name for store_var()
5804 if (name[1] != ':')
5805 {
5806 int len = (int)STRLEN(name) + 3;
5807
5808 name_s = alloc(len);
5809 if (name_s == NULL)
5810 name_s = name;
5811 else
5812 vim_snprintf((char *)name_s, len,
5813 "s:%s", name);
5814 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005815 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5816 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005817 if (name_s != name)
5818 vim_free(name_s);
5819 }
5820 else
5821 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005822 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005823 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005824 break;
5825 case dest_local:
5826 if (lvar != NULL)
5827 {
5828 isn_T *isn = ((isn_T *)instr->ga_data)
5829 + instr->ga_len - 1;
5830
5831 // optimization: turn "var = 123" from ISN_PUSHNR +
5832 // ISN_STORE into ISN_STORENR
5833 if (!lvar->lv_from_outer
5834 && instr->ga_len == instr_count + 1
5835 && isn->isn_type == ISN_PUSHNR)
5836 {
5837 varnumber_T val = isn->isn_arg.number;
5838
5839 isn->isn_type = ISN_STORENR;
5840 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5841 isn->isn_arg.storenr.stnr_val = val;
5842 if (stack->ga_len > 0)
5843 --stack->ga_len;
5844 }
5845 else if (lvar->lv_from_outer)
5846 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005847 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005848 else
5849 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5850 }
5851 break;
5852 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005853 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005854
5855 if (var_idx + 1 < var_count)
5856 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005857 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005858
5859 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005860 if (var_count > 0 && !semicolon)
5861 {
5862 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5863 goto theend;
5864 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005865
Bram Moolenaarb2097502020-07-19 17:17:02 +02005866 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005867
5868theend:
5869 vim_free(name);
5870 return ret;
5871}
5872
5873/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005874 * Check if "name" can be "unlet".
5875 */
5876 int
5877check_vim9_unlet(char_u *name)
5878{
5879 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5880 {
5881 semsg(_("E1081: Cannot unlet %s"), name);
5882 return FAIL;
5883 }
5884 return OK;
5885}
5886
5887/*
5888 * Callback passed to ex_unletlock().
5889 */
5890 static int
5891compile_unlet(
5892 lval_T *lvp,
5893 char_u *name_end,
5894 exarg_T *eap,
5895 int deep UNUSED,
5896 void *coookie)
5897{
5898 cctx_T *cctx = coookie;
5899
5900 if (lvp->ll_tv == NULL)
5901 {
5902 char_u *p = lvp->ll_name;
5903 int cc = *name_end;
5904 int ret = OK;
5905
5906 // Normal name. Only supports g:, w:, t: and b: namespaces.
5907 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005908 if (*p == '$')
5909 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5910 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005911 ret = FAIL;
5912 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005913 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005914
5915 *name_end = cc;
5916 return ret;
5917 }
5918
5919 // TODO: unlet {list}[idx]
5920 // TODO: unlet {dict}[key]
5921 emsg("Sorry, :unlet not fully implemented yet");
5922 return FAIL;
5923}
5924
5925/*
5926 * compile "unlet var", "lock var" and "unlock var"
5927 * "arg" points to "var".
5928 */
5929 static char_u *
5930compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5931{
5932 char_u *p = arg;
5933
5934 if (eap->cmdidx != CMD_unlet)
5935 {
5936 emsg("Sorry, :lock and unlock not implemented yet");
5937 return NULL;
5938 }
5939
5940 if (*p == '!')
5941 {
5942 p = skipwhite(p + 1);
5943 eap->forceit = TRUE;
5944 }
5945
5946 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5947 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5948}
5949
5950/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005951 * Compile an :import command.
5952 */
5953 static char_u *
5954compile_import(char_u *arg, cctx_T *cctx)
5955{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005956 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005957}
5958
5959/*
5960 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5961 */
5962 static int
5963compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5964{
5965 garray_T *instr = &cctx->ctx_instr;
5966 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5967
5968 if (endlabel == NULL)
5969 return FAIL;
5970 endlabel->el_next = *el;
5971 *el = endlabel;
5972 endlabel->el_end_label = instr->ga_len;
5973
5974 generate_JUMP(cctx, when, 0);
5975 return OK;
5976}
5977
5978 static void
5979compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5980{
5981 garray_T *instr = &cctx->ctx_instr;
5982
5983 while (*el != NULL)
5984 {
5985 endlabel_T *cur = (*el);
5986 isn_T *isn;
5987
5988 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5989 isn->isn_arg.jump.jump_where = instr->ga_len;
5990 *el = cur->el_next;
5991 vim_free(cur);
5992 }
5993}
5994
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005995 static void
5996compile_free_jump_to_end(endlabel_T **el)
5997{
5998 while (*el != NULL)
5999 {
6000 endlabel_T *cur = (*el);
6001
6002 *el = cur->el_next;
6003 vim_free(cur);
6004 }
6005}
6006
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006007/*
6008 * Create a new scope and set up the generic items.
6009 */
6010 static scope_T *
6011new_scope(cctx_T *cctx, scopetype_T type)
6012{
6013 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6014
6015 if (scope == NULL)
6016 return NULL;
6017 scope->se_outer = cctx->ctx_scope;
6018 cctx->ctx_scope = scope;
6019 scope->se_type = type;
6020 scope->se_local_count = cctx->ctx_locals.ga_len;
6021 return scope;
6022}
6023
6024/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006025 * Free the current scope and go back to the outer scope.
6026 */
6027 static void
6028drop_scope(cctx_T *cctx)
6029{
6030 scope_T *scope = cctx->ctx_scope;
6031
6032 if (scope == NULL)
6033 {
6034 iemsg("calling drop_scope() without a scope");
6035 return;
6036 }
6037 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006038 switch (scope->se_type)
6039 {
6040 case IF_SCOPE:
6041 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6042 case FOR_SCOPE:
6043 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6044 case WHILE_SCOPE:
6045 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6046 case TRY_SCOPE:
6047 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6048 case NO_SCOPE:
6049 case BLOCK_SCOPE:
6050 break;
6051 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006052 vim_free(scope);
6053}
6054
6055/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006056 * compile "if expr"
6057 *
6058 * "if expr" Produces instructions:
6059 * EVAL expr Push result of "expr"
6060 * JUMP_IF_FALSE end
6061 * ... body ...
6062 * end:
6063 *
6064 * "if expr | else" Produces instructions:
6065 * EVAL expr Push result of "expr"
6066 * JUMP_IF_FALSE else
6067 * ... body ...
6068 * JUMP_ALWAYS end
6069 * else:
6070 * ... body ...
6071 * end:
6072 *
6073 * "if expr1 | elseif expr2 | else" Produces instructions:
6074 * EVAL expr Push result of "expr"
6075 * JUMP_IF_FALSE elseif
6076 * ... body ...
6077 * JUMP_ALWAYS end
6078 * elseif:
6079 * EVAL expr Push result of "expr"
6080 * JUMP_IF_FALSE else
6081 * ... body ...
6082 * JUMP_ALWAYS end
6083 * else:
6084 * ... body ...
6085 * end:
6086 */
6087 static char_u *
6088compile_if(char_u *arg, cctx_T *cctx)
6089{
6090 char_u *p = arg;
6091 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006092 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006093 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006094 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006095 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096
Bram Moolenaara5565e42020-05-09 15:44:01 +02006097 CLEAR_FIELD(ppconst);
6098 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006099 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006100 clear_ppconst(&ppconst);
6101 return NULL;
6102 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006103 if (cctx->ctx_skip == SKIP_YES)
6104 clear_ppconst(&ppconst);
6105 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006106 {
6107 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006108 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006109 clear_ppconst(&ppconst);
6110 }
6111 else
6112 {
6113 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006114 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006115 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006116 return NULL;
6117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006118
6119 scope = new_scope(cctx, IF_SCOPE);
6120 if (scope == NULL)
6121 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006122 scope->se_skip_save = skip_save;
6123 // "is_had_return" will be reset if any block does not end in :return
6124 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006125
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006126 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006127 {
6128 // "where" is set when ":elseif", "else" or ":endif" is found
6129 scope->se_u.se_if.is_if_label = instr->ga_len;
6130 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6131 }
6132 else
6133 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006134
6135 return p;
6136}
6137
6138 static char_u *
6139compile_elseif(char_u *arg, cctx_T *cctx)
6140{
6141 char_u *p = arg;
6142 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006143 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006144 isn_T *isn;
6145 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006146 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006147
6148 if (scope == NULL || scope->se_type != IF_SCOPE)
6149 {
6150 emsg(_(e_elseif_without_if));
6151 return NULL;
6152 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006153 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006154 if (!cctx->ctx_had_return)
6155 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006156
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006157 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006158 {
6159 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006160 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006161 return NULL;
6162 // previous "if" or "elseif" jumps here
6163 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6164 isn->isn_arg.jump.jump_where = instr->ga_len;
6165 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006166
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006167 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006168 CLEAR_FIELD(ppconst);
6169 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006170 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006171 clear_ppconst(&ppconst);
6172 return NULL;
6173 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006174 if (scope->se_skip_save == SKIP_YES)
6175 clear_ppconst(&ppconst);
6176 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006177 {
6178 // The expression results in a constant.
6179 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006180 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006181 clear_ppconst(&ppconst);
6182 scope->se_u.se_if.is_if_label = -1;
6183 }
6184 else
6185 {
6186 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006187 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006188 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006189 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006190
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006191 // "where" is set when ":elseif", "else" or ":endif" is found
6192 scope->se_u.se_if.is_if_label = instr->ga_len;
6193 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006195
6196 return p;
6197}
6198
6199 static char_u *
6200compile_else(char_u *arg, cctx_T *cctx)
6201{
6202 char_u *p = arg;
6203 garray_T *instr = &cctx->ctx_instr;
6204 isn_T *isn;
6205 scope_T *scope = cctx->ctx_scope;
6206
6207 if (scope == NULL || scope->se_type != IF_SCOPE)
6208 {
6209 emsg(_(e_else_without_if));
6210 return NULL;
6211 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006212 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006213 if (!cctx->ctx_had_return)
6214 scope->se_u.se_if.is_had_return = FALSE;
6215 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006216
Bram Moolenaarefd88552020-06-18 20:50:10 +02006217 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006218 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006219 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006220 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006221 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006222 if (!cctx->ctx_had_return
6223 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6224 JUMP_ALWAYS, cctx) == FAIL)
6225 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006226 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006227
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006228 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006229 {
6230 if (scope->se_u.se_if.is_if_label >= 0)
6231 {
6232 // previous "if" or "elseif" jumps here
6233 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6234 isn->isn_arg.jump.jump_where = instr->ga_len;
6235 scope->se_u.se_if.is_if_label = -1;
6236 }
6237 }
6238
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006239 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006240 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6241 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242
6243 return p;
6244}
6245
6246 static char_u *
6247compile_endif(char_u *arg, cctx_T *cctx)
6248{
6249 scope_T *scope = cctx->ctx_scope;
6250 ifscope_T *ifscope;
6251 garray_T *instr = &cctx->ctx_instr;
6252 isn_T *isn;
6253
6254 if (scope == NULL || scope->se_type != IF_SCOPE)
6255 {
6256 emsg(_(e_endif_without_if));
6257 return NULL;
6258 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006259 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006260 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006261 if (!cctx->ctx_had_return)
6262 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006263
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006264 if (scope->se_u.se_if.is_if_label >= 0)
6265 {
6266 // previous "if" or "elseif" jumps here
6267 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6268 isn->isn_arg.jump.jump_where = instr->ga_len;
6269 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006270 // Fill in the "end" label in jumps at the end of the blocks.
6271 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006272 cctx->ctx_skip = scope->se_skip_save;
6273
6274 // If all the blocks end in :return and there is an :else then the
6275 // had_return flag is set.
6276 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006278 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279 return arg;
6280}
6281
6282/*
6283 * compile "for var in expr"
6284 *
6285 * Produces instructions:
6286 * PUSHNR -1
6287 * STORE loop-idx Set index to -1
6288 * EVAL expr Push result of "expr"
6289 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6290 * - if beyond end, jump to "end"
6291 * - otherwise get item from list and push it
6292 * STORE var Store item in "var"
6293 * ... body ...
6294 * JUMP top Jump back to repeat
6295 * end: DROP Drop the result of "expr"
6296 *
6297 */
6298 static char_u *
6299compile_for(char_u *arg, cctx_T *cctx)
6300{
6301 char_u *p;
6302 size_t varlen;
6303 garray_T *instr = &cctx->ctx_instr;
6304 garray_T *stack = &cctx->ctx_type_stack;
6305 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006306 lvar_T *loop_lvar; // loop iteration variable
6307 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 type_T *vartype;
6309
6310 // TODO: list of variables: "for [key, value] in dict"
6311 // parse "var"
6312 for (p = arg; eval_isnamec1(*p); ++p)
6313 ;
6314 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006315 var_lvar = lookup_local(arg, varlen, cctx);
6316 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006317 {
6318 semsg(_("E1023: variable already defined: %s"), arg);
6319 return NULL;
6320 }
6321
6322 // consume "in"
6323 p = skipwhite(p);
6324 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6325 {
6326 emsg(_(e_missing_in));
6327 return NULL;
6328 }
6329 p = skipwhite(p + 2);
6330
6331
6332 scope = new_scope(cctx, FOR_SCOPE);
6333 if (scope == NULL)
6334 return NULL;
6335
6336 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006337 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6338 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006339 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006340 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006341 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006342 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006343 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006344
6345 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006346 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6347 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006348 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006349 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006350 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006351 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006352 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006354 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006355
6356 // compile "expr", it remains on the stack until "endfor"
6357 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006358 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006359 {
6360 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006361 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006362 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006363
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006364 // Now that we know the type of "var", check that it is a list, now or at
6365 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006366 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006367 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006368 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006369 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006370 return NULL;
6371 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006372 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006373 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006374
6375 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006376 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006377
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006378 generate_FOR(cctx, loop_lvar->lv_idx);
6379 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006380
6381 return arg;
6382}
6383
6384/*
6385 * compile "endfor"
6386 */
6387 static char_u *
6388compile_endfor(char_u *arg, cctx_T *cctx)
6389{
6390 garray_T *instr = &cctx->ctx_instr;
6391 scope_T *scope = cctx->ctx_scope;
6392 forscope_T *forscope;
6393 isn_T *isn;
6394
6395 if (scope == NULL || scope->se_type != FOR_SCOPE)
6396 {
6397 emsg(_(e_for));
6398 return NULL;
6399 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006400 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006401 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006402 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006403
6404 // At end of ":for" scope jump back to the FOR instruction.
6405 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6406
6407 // Fill in the "end" label in the FOR statement so it can jump here
6408 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6409 isn->isn_arg.forloop.for_end = instr->ga_len;
6410
6411 // Fill in the "end" label any BREAK statements
6412 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6413
6414 // Below the ":for" scope drop the "expr" list from the stack.
6415 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6416 return NULL;
6417
6418 vim_free(scope);
6419
6420 return arg;
6421}
6422
6423/*
6424 * compile "while expr"
6425 *
6426 * Produces instructions:
6427 * top: EVAL expr Push result of "expr"
6428 * JUMP_IF_FALSE end jump if false
6429 * ... body ...
6430 * JUMP top Jump back to repeat
6431 * end:
6432 *
6433 */
6434 static char_u *
6435compile_while(char_u *arg, cctx_T *cctx)
6436{
6437 char_u *p = arg;
6438 garray_T *instr = &cctx->ctx_instr;
6439 scope_T *scope;
6440
6441 scope = new_scope(cctx, WHILE_SCOPE);
6442 if (scope == NULL)
6443 return NULL;
6444
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006445 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006446
6447 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006448 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006449 return NULL;
6450
6451 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006452 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006453 JUMP_IF_FALSE, cctx) == FAIL)
6454 return FAIL;
6455
6456 return p;
6457}
6458
6459/*
6460 * compile "endwhile"
6461 */
6462 static char_u *
6463compile_endwhile(char_u *arg, cctx_T *cctx)
6464{
6465 scope_T *scope = cctx->ctx_scope;
6466
6467 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6468 {
6469 emsg(_(e_while));
6470 return NULL;
6471 }
6472 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006473 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006474
6475 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006476 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006477
6478 // Fill in the "end" label in the WHILE statement so it can jump here.
6479 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006480 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006481
6482 vim_free(scope);
6483
6484 return arg;
6485}
6486
6487/*
6488 * compile "continue"
6489 */
6490 static char_u *
6491compile_continue(char_u *arg, cctx_T *cctx)
6492{
6493 scope_T *scope = cctx->ctx_scope;
6494
6495 for (;;)
6496 {
6497 if (scope == NULL)
6498 {
6499 emsg(_(e_continue));
6500 return NULL;
6501 }
6502 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6503 break;
6504 scope = scope->se_outer;
6505 }
6506
6507 // Jump back to the FOR or WHILE instruction.
6508 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006509 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6510 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511 return arg;
6512}
6513
6514/*
6515 * compile "break"
6516 */
6517 static char_u *
6518compile_break(char_u *arg, cctx_T *cctx)
6519{
6520 scope_T *scope = cctx->ctx_scope;
6521 endlabel_T **el;
6522
6523 for (;;)
6524 {
6525 if (scope == NULL)
6526 {
6527 emsg(_(e_break));
6528 return NULL;
6529 }
6530 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6531 break;
6532 scope = scope->se_outer;
6533 }
6534
6535 // Jump to the end of the FOR or WHILE loop.
6536 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006537 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006538 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006539 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006540 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6541 return FAIL;
6542
6543 return arg;
6544}
6545
6546/*
6547 * compile "{" start of block
6548 */
6549 static char_u *
6550compile_block(char_u *arg, cctx_T *cctx)
6551{
6552 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6553 return NULL;
6554 return skipwhite(arg + 1);
6555}
6556
6557/*
6558 * compile end of block: drop one scope
6559 */
6560 static void
6561compile_endblock(cctx_T *cctx)
6562{
6563 scope_T *scope = cctx->ctx_scope;
6564
6565 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006566 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006567 vim_free(scope);
6568}
6569
6570/*
6571 * compile "try"
6572 * Creates a new scope for the try-endtry, pointing to the first catch and
6573 * finally.
6574 * Creates another scope for the "try" block itself.
6575 * TRY instruction sets up exception handling at runtime.
6576 *
6577 * "try"
6578 * TRY -> catch1, -> finally push trystack entry
6579 * ... try block
6580 * "throw {exception}"
6581 * EVAL {exception}
6582 * THROW create exception
6583 * ... try block
6584 * " catch {expr}"
6585 * JUMP -> finally
6586 * catch1: PUSH exeception
6587 * EVAL {expr}
6588 * MATCH
6589 * JUMP nomatch -> catch2
6590 * CATCH remove exception
6591 * ... catch block
6592 * " catch"
6593 * JUMP -> finally
6594 * catch2: CATCH remove exception
6595 * ... catch block
6596 * " finally"
6597 * finally:
6598 * ... finally block
6599 * " endtry"
6600 * ENDTRY pop trystack entry, may rethrow
6601 */
6602 static char_u *
6603compile_try(char_u *arg, cctx_T *cctx)
6604{
6605 garray_T *instr = &cctx->ctx_instr;
6606 scope_T *try_scope;
6607 scope_T *scope;
6608
6609 // scope that holds the jumps that go to catch/finally/endtry
6610 try_scope = new_scope(cctx, TRY_SCOPE);
6611 if (try_scope == NULL)
6612 return NULL;
6613
6614 // "catch" is set when the first ":catch" is found.
6615 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006616 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617 if (generate_instr(cctx, ISN_TRY) == NULL)
6618 return NULL;
6619
6620 // scope for the try block itself
6621 scope = new_scope(cctx, BLOCK_SCOPE);
6622 if (scope == NULL)
6623 return NULL;
6624
6625 return arg;
6626}
6627
6628/*
6629 * compile "catch {expr}"
6630 */
6631 static char_u *
6632compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6633{
6634 scope_T *scope = cctx->ctx_scope;
6635 garray_T *instr = &cctx->ctx_instr;
6636 char_u *p;
6637 isn_T *isn;
6638
6639 // end block scope from :try or :catch
6640 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6641 compile_endblock(cctx);
6642 scope = cctx->ctx_scope;
6643
6644 // Error if not in a :try scope
6645 if (scope == NULL || scope->se_type != TRY_SCOPE)
6646 {
6647 emsg(_(e_catch));
6648 return NULL;
6649 }
6650
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006651 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006652 {
6653 emsg(_("E1033: catch unreachable after catch-all"));
6654 return NULL;
6655 }
6656
6657 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006658 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006659 JUMP_ALWAYS, cctx) == FAIL)
6660 return NULL;
6661
6662 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006663 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664 if (isn->isn_arg.try.try_catch == 0)
6665 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006666 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006667 {
6668 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006669 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006670 isn->isn_arg.jump.jump_where = instr->ga_len;
6671 }
6672
6673 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006674 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006675 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006676 scope->se_u.se_try.ts_caught_all = TRUE;
6677 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 }
6679 else
6680 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006681 char_u *end;
6682 char_u *pat;
6683 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006684 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006685 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006686
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006687 // Push v:exception, push {expr} and MATCH
6688 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6689
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006690 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006691 if (*end != *p)
6692 {
6693 semsg(_("E1067: Separator mismatch: %s"), p);
6694 vim_free(tofree);
6695 return FAIL;
6696 }
6697 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006698 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006699 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006700 len = (int)(end - tofree);
6701 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006702 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006703 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006704 if (pat == NULL)
6705 return FAIL;
6706 if (generate_PUSHS(cctx, pat) == FAIL)
6707 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006709 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6710 return NULL;
6711
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006712 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6714 return NULL;
6715 }
6716
6717 if (generate_instr(cctx, ISN_CATCH) == NULL)
6718 return NULL;
6719
6720 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6721 return NULL;
6722 return p;
6723}
6724
6725 static char_u *
6726compile_finally(char_u *arg, cctx_T *cctx)
6727{
6728 scope_T *scope = cctx->ctx_scope;
6729 garray_T *instr = &cctx->ctx_instr;
6730 isn_T *isn;
6731
6732 // end block scope from :try or :catch
6733 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6734 compile_endblock(cctx);
6735 scope = cctx->ctx_scope;
6736
6737 // Error if not in a :try scope
6738 if (scope == NULL || scope->se_type != TRY_SCOPE)
6739 {
6740 emsg(_(e_finally));
6741 return NULL;
6742 }
6743
6744 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006745 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006746 if (isn->isn_arg.try.try_finally != 0)
6747 {
6748 emsg(_(e_finally_dup));
6749 return NULL;
6750 }
6751
6752 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006753 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006754
Bram Moolenaar585fea72020-04-02 22:33:21 +02006755 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006756 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006757 {
6758 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006759 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006760 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006761 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006762 }
6763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006764 // TODO: set index in ts_finally_label jumps
6765
6766 return arg;
6767}
6768
6769 static char_u *
6770compile_endtry(char_u *arg, cctx_T *cctx)
6771{
6772 scope_T *scope = cctx->ctx_scope;
6773 garray_T *instr = &cctx->ctx_instr;
6774 isn_T *isn;
6775
6776 // end block scope from :catch or :finally
6777 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6778 compile_endblock(cctx);
6779 scope = cctx->ctx_scope;
6780
6781 // Error if not in a :try scope
6782 if (scope == NULL || scope->se_type != TRY_SCOPE)
6783 {
6784 if (scope == NULL)
6785 emsg(_(e_no_endtry));
6786 else if (scope->se_type == WHILE_SCOPE)
6787 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006788 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006789 emsg(_(e_endfor));
6790 else
6791 emsg(_(e_endif));
6792 return NULL;
6793 }
6794
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006795 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006796 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6797 {
6798 emsg(_("E1032: missing :catch or :finally"));
6799 return NULL;
6800 }
6801
6802 // Fill in the "end" label in jumps at the end of the blocks, if not done
6803 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006804 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805
6806 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006807 if (isn->isn_arg.try.try_catch == 0)
6808 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006809 if (isn->isn_arg.try.try_finally == 0)
6810 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006811
6812 if (scope->se_u.se_try.ts_catch_label != 0)
6813 {
6814 // Last catch without match jumps here
6815 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6816 isn->isn_arg.jump.jump_where = instr->ga_len;
6817 }
6818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006819 compile_endblock(cctx);
6820
6821 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6822 return NULL;
6823 return arg;
6824}
6825
6826/*
6827 * compile "throw {expr}"
6828 */
6829 static char_u *
6830compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6831{
6832 char_u *p = skipwhite(arg);
6833
Bram Moolenaara5565e42020-05-09 15:44:01 +02006834 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006835 return NULL;
6836 if (may_generate_2STRING(-1, cctx) == FAIL)
6837 return NULL;
6838 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6839 return NULL;
6840
6841 return p;
6842}
6843
6844/*
6845 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006846 * compile "echomsg expr"
6847 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006848 * compile "execute expr"
6849 */
6850 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006851compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006852{
6853 char_u *p = arg;
6854 int count = 0;
6855
6856 for (;;)
6857 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006858 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006859 return NULL;
6860 ++count;
6861 p = skipwhite(p);
6862 if (ends_excmd(*p))
6863 break;
6864 }
6865
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006866 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6867 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6868 else if (cmdidx == CMD_execute)
6869 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6870 else if (cmdidx == CMD_echomsg)
6871 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6872 else
6873 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006874 return p;
6875}
6876
6877/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006878 * A command that is not compiled, execute with legacy code.
6879 */
6880 static char_u *
6881compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6882{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006883 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006884 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006885 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006886
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006887 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006888 goto theend;
6889
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006890 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006891 {
6892 long argt = excmd_get_argt(eap->cmdidx);
6893 int usefilter = FALSE;
6894
6895 has_expr = argt & (EX_XFILE | EX_EXPAND);
6896
6897 // If the command can be followed by a bar, find the bar and truncate
6898 // it, so that the following command can be compiled.
6899 // The '|' is overwritten with a NUL, it is put back below.
6900 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6901 && *eap->arg == '!')
6902 // :w !filter or :r !filter or :r! filter
6903 usefilter = TRUE;
6904 if ((argt & EX_TRLBAR) && !usefilter)
6905 {
6906 separate_nextcmd(eap);
6907 if (eap->nextcmd != NULL)
6908 nextcmd = eap->nextcmd;
6909 }
6910 }
6911
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006912 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6913 {
6914 // expand filename in "syntax include [@group] filename"
6915 has_expr = TRUE;
6916 eap->arg = skipwhite(eap->arg + 7);
6917 if (*eap->arg == '@')
6918 eap->arg = skiptowhite(eap->arg);
6919 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006920
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006921 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006922 {
6923 int count = 0;
6924 char_u *start = skipwhite(line);
6925
6926 // :cmd xxx`=expr1`yyy`=expr2`zzz
6927 // PUSHS ":cmd xxx"
6928 // eval expr1
6929 // PUSHS "yyy"
6930 // eval expr2
6931 // PUSHS "zzz"
6932 // EXECCONCAT 5
6933 for (;;)
6934 {
6935 if (p > start)
6936 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006937 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006938 ++count;
6939 }
6940 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006941 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006942 return NULL;
6943 may_generate_2STRING(-1, cctx);
6944 ++count;
6945 p = skipwhite(p);
6946 if (*p != '`')
6947 {
6948 emsg(_("E1083: missing backtick"));
6949 return NULL;
6950 }
6951 start = p + 1;
6952
6953 p = (char_u *)strstr((char *)start, "`=");
6954 if (p == NULL)
6955 {
6956 if (*skipwhite(start) != NUL)
6957 {
6958 generate_PUSHS(cctx, vim_strsave(start));
6959 ++count;
6960 }
6961 break;
6962 }
6963 }
6964 generate_EXECCONCAT(cctx, count);
6965 }
6966 else
6967 generate_EXEC(cctx, line);
6968
6969theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006970 if (*nextcmd != NUL)
6971 {
6972 // the parser expects a pointer to the bar, put it back
6973 --nextcmd;
6974 *nextcmd = '|';
6975 }
6976
6977 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006978}
6979
6980/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006981 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006982 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006983 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006984 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006985add_def_function(ufunc_T *ufunc)
6986{
6987 dfunc_T *dfunc;
6988
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006989 if (def_functions.ga_len == 0)
6990 {
6991 // The first position is not used, so that a zero uf_dfunc_idx means it
6992 // wasn't set.
6993 if (ga_grow(&def_functions, 1) == FAIL)
6994 return FAIL;
6995 ++def_functions.ga_len;
6996 }
6997
Bram Moolenaar09689a02020-05-09 22:50:08 +02006998 // Add the function to "def_functions".
6999 if (ga_grow(&def_functions, 1) == FAIL)
7000 return FAIL;
7001 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7002 CLEAR_POINTER(dfunc);
7003 dfunc->df_idx = def_functions.ga_len;
7004 ufunc->uf_dfunc_idx = dfunc->df_idx;
7005 dfunc->df_ufunc = ufunc;
7006 ++def_functions.ga_len;
7007 return OK;
7008}
7009
7010/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 * After ex_function() has collected all the function lines: parse and compile
7012 * the lines into instructions.
7013 * Adds the function to "def_functions".
7014 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7015 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007016 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007017 * This can be used recursively through compile_lambda(), which may reallocate
7018 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007019 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007021 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007022compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007024 char_u *line = NULL;
7025 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007026 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027 cctx_T cctx;
7028 garray_T *instr;
7029 int called_emsg_before = called_emsg;
7030 int ret = FAIL;
7031 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007032 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007033 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007034 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007035
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007036 // When using a function that was compiled before: Free old instructions.
7037 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007038 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007039 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007040 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7041 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007042 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007043 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007044 else
7045 {
7046 if (add_def_function(ufunc) == FAIL)
7047 return FAIL;
7048 new_def_function = TRUE;
7049 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007050
Bram Moolenaar985116a2020-07-12 17:31:09 +02007051 ufunc->uf_def_status = UF_COMPILING;
7052
Bram Moolenaara80faa82020-04-12 19:37:17 +02007053 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054 cctx.ctx_ufunc = ufunc;
7055 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007056 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7058 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7059 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7060 cctx.ctx_type_list = &ufunc->uf_type_list;
7061 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7062 instr = &cctx.ctx_instr;
7063
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007064 // Set the context to the function, it may be compiled when called from
7065 // another script. Set the script version to the most modern one.
7066 // The line number will be set in next_line_from_context().
7067 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007068 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7069
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007070 // Make sure error messages are OK.
7071 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7072 if (do_estack_push)
7073 estack_push_ufunc(ufunc, 1);
7074
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007075 if (ufunc->uf_def_args.ga_len > 0)
7076 {
7077 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007078 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007079 int i;
7080 char_u *arg;
7081 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007082 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007083
7084 // Produce instructions for the default values of optional arguments.
7085 // Store the instruction index in uf_def_arg_idx[] so that we know
7086 // where to start when the function is called, depending on the number
7087 // of arguments.
7088 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7089 if (ufunc->uf_def_arg_idx == NULL)
7090 goto erret;
7091 for (i = 0; i < count; ++i)
7092 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007093 garray_T *stack = &cctx.ctx_type_stack;
7094 type_T *val_type;
7095 int arg_idx = first_def_arg + i;
7096
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007097 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7098 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007099 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007100 goto erret;
7101
7102 // If no type specified use the type of the default value.
7103 // Otherwise check that the default value type matches the
7104 // specified type.
7105 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7106 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007107 {
7108 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007109 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007110 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02007111 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007112 == FAIL)
7113 {
7114 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
7115 arg_idx + 1);
7116 goto erret;
7117 }
7118
7119 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007120 goto erret;
7121 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007122 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007123
7124 if (did_set_arg_type)
7125 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007126 }
7127
7128 /*
7129 * Loop over all the lines of the function and generate instructions.
7130 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007131 for (;;)
7132 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007133 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007134 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007135 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007136 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007137
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007138 // Bail out on the first error to avoid a flood of errors and report
7139 // the right line number when inside try/catch.
7140 if (emsg_before != called_emsg)
7141 goto erret;
7142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007143 if (line != NULL && *line == '|')
7144 // the line continues after a '|'
7145 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007146 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007147 && !(*line == '#' && (line == cctx.ctx_line_start
7148 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007149 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007150 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151 goto erret;
7152 }
7153 else
7154 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007155 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007156 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007157 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007159 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007160 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007161
Bram Moolenaara80faa82020-04-12 19:37:17 +02007162 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007163 ea.cmdlinep = &line;
7164 ea.cmd = skipwhite(line);
7165
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007166 // Some things can be recognized by the first character.
7167 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007169 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007170 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007171 if (ea.cmd[1] != '{')
7172 {
7173 line = (char_u *)"";
7174 continue;
7175 }
7176 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007178 case '}':
7179 {
7180 // "}" ends a block scope
7181 scopetype_T stype = cctx.ctx_scope == NULL
7182 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007183
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007184 if (stype == BLOCK_SCOPE)
7185 {
7186 compile_endblock(&cctx);
7187 line = ea.cmd;
7188 }
7189 else
7190 {
7191 emsg(_("E1025: using } outside of a block scope"));
7192 goto erret;
7193 }
7194 if (line != NULL)
7195 line = skipwhite(ea.cmd + 1);
7196 continue;
7197 }
7198
7199 case '{':
7200 // "{" starts a block scope
7201 // "{'a': 1}->func() is something else
7202 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7203 {
7204 line = compile_block(ea.cmd, &cctx);
7205 continue;
7206 }
7207 break;
7208
7209 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007210 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007211 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007212 }
7213
7214 /*
7215 * COMMAND MODIFIERS
7216 */
7217 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7218 {
7219 if (errormsg != NULL)
7220 goto erret;
7221 // empty line or comment
7222 line = (char_u *)"";
7223 continue;
7224 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007225 // TODO: use modifiers in the command
7226 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02007227 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007228
7229 // Skip ":call" to get to the function name.
7230 if (checkforcmd(&ea.cmd, "call", 3))
7231 ea.cmd = skipwhite(ea.cmd);
7232
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007233 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007234 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007235 char_u *pskip;
7236
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007237 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007238 // find what follows.
7239 // Skip over "var.member", "var[idx]" and the like.
7240 // Also "&opt = val", "$ENV = val" and "@r = val".
7241 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007242 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007243 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007244 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007245 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007246 char_u *var_end;
7247 int oplen;
7248 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007249
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007250 var_end = find_name_end(pskip, NULL, NULL,
7251 FNE_CHECK_START | FNE_INCL_BR);
7252 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007253 if (oplen > 0)
7254 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007255 size_t len = p - ea.cmd;
7256
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007257 // Recognize an assignment if we recognize the variable
7258 // name:
7259 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007260 // "local = expr" where "local" is a local var.
7261 // "script = expr" where "script" is a script-local var.
7262 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007263 // "&opt = expr"
7264 // "$ENV = expr"
7265 // "@r = expr"
7266 if (*ea.cmd == '&'
7267 || *ea.cmd == '$'
7268 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007269 || ((len) > 2 && ea.cmd[1] == ':')
7270 || lookup_local(ea.cmd, len, &cctx) != NULL
7271 || lookup_arg(ea.cmd, len, NULL, NULL,
7272 NULL, &cctx) == OK
7273 || lookup_script(ea.cmd, len) == OK
7274 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007275 {
7276 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007277 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007278 goto erret;
7279 continue;
7280 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281 }
7282 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007283
7284 if (*ea.cmd == '[')
7285 {
7286 // [var, var] = expr
7287 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7288 if (line == NULL)
7289 goto erret;
7290 if (line != ea.cmd)
7291 continue;
7292 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007293 }
7294
7295 /*
7296 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007297 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007298 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007299 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007300 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007301 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007302 ea.cmd = skip_range(ea.cmd, NULL);
7303 if (ea.cmd > cmd && !starts_with_colon)
7304 {
7305 emsg(_(e_colon_required));
7306 goto erret;
7307 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007308 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007309 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007310 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007311 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007312
7313 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7314 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007315 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007316 {
7317 line += STRLEN(line);
7318 continue;
7319 }
7320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007322 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007323 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007324 // CMD_let cannot happen, compile_assignment() above is used
7325 iemsg("Command from find_ex_command() not handled");
7326 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007328 }
7329
7330 p = skipwhite(p);
7331
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007332 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007333 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007334 && ea.cmdidx != CMD_elseif
7335 && ea.cmdidx != CMD_else
7336 && ea.cmdidx != CMD_endif)
7337 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007338 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007339 continue;
7340 }
7341
Bram Moolenaarefd88552020-06-18 20:50:10 +02007342 if (ea.cmdidx != CMD_elseif
7343 && ea.cmdidx != CMD_else
7344 && ea.cmdidx != CMD_endif
7345 && ea.cmdidx != CMD_endfor
7346 && ea.cmdidx != CMD_endwhile
7347 && ea.cmdidx != CMD_catch
7348 && ea.cmdidx != CMD_finally
7349 && ea.cmdidx != CMD_endtry)
7350 {
7351 if (cctx.ctx_had_return)
7352 {
7353 emsg(_("E1095: Unreachable code after :return"));
7354 goto erret;
7355 }
7356 }
7357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007358 switch (ea.cmdidx)
7359 {
7360 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007361 ea.arg = p;
7362 line = compile_nested_function(&ea, &cctx);
7363 break;
7364
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007366 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367 goto erret;
7368
7369 case CMD_return:
7370 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007371 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007372 break;
7373
7374 case CMD_let:
7375 case CMD_const:
7376 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007377 if (line == p)
7378 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 break;
7380
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007381 case CMD_unlet:
7382 case CMD_unlockvar:
7383 case CMD_lockvar:
7384 line = compile_unletlock(p, &ea, &cctx);
7385 break;
7386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387 case CMD_import:
7388 line = compile_import(p, &cctx);
7389 break;
7390
7391 case CMD_if:
7392 line = compile_if(p, &cctx);
7393 break;
7394 case CMD_elseif:
7395 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007396 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397 break;
7398 case CMD_else:
7399 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007400 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007401 break;
7402 case CMD_endif:
7403 line = compile_endif(p, &cctx);
7404 break;
7405
7406 case CMD_while:
7407 line = compile_while(p, &cctx);
7408 break;
7409 case CMD_endwhile:
7410 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007411 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007412 break;
7413
7414 case CMD_for:
7415 line = compile_for(p, &cctx);
7416 break;
7417 case CMD_endfor:
7418 line = compile_endfor(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 case CMD_continue:
7422 line = compile_continue(p, &cctx);
7423 break;
7424 case CMD_break:
7425 line = compile_break(p, &cctx);
7426 break;
7427
7428 case CMD_try:
7429 line = compile_try(p, &cctx);
7430 break;
7431 case CMD_catch:
7432 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007433 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007434 break;
7435 case CMD_finally:
7436 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007437 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007438 break;
7439 case CMD_endtry:
7440 line = compile_endtry(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_throw:
7444 line = compile_throw(p, &cctx);
7445 break;
7446
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007447 case CMD_eval:
7448 if (compile_expr0(&p, &cctx) == FAIL)
7449 goto erret;
7450
7451 // drop the return value
7452 generate_instr_drop(&cctx, ISN_DROP, 1);
7453
7454 line = skipwhite(p);
7455 break;
7456
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007457 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007458 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007459 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007460 case CMD_echomsg:
7461 case CMD_echoerr:
7462 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007463 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007465 // TODO: other commands with an expression argument
7466
Bram Moolenaarae616492020-07-28 20:07:27 +02007467 case CMD_append:
7468 case CMD_change:
7469 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007470 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007471 case CMD_xit:
7472 not_in_vim9(&ea);
7473 goto erret;
7474
Bram Moolenaar002262f2020-07-08 17:47:57 +02007475 case CMD_SIZE:
7476 semsg(_("E476: Invalid command: %s"), ea.cmd);
7477 goto erret;
7478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007479 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007480 // Not recognized, execute with do_cmdline_cmd().
7481 ea.arg = p;
7482 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007483 break;
7484 }
7485 if (line == NULL)
7486 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007487 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007488
7489 if (cctx.ctx_type_stack.ga_len < 0)
7490 {
7491 iemsg("Type stack underflow");
7492 goto erret;
7493 }
7494 }
7495
7496 if (cctx.ctx_scope != NULL)
7497 {
7498 if (cctx.ctx_scope->se_type == IF_SCOPE)
7499 emsg(_(e_endif));
7500 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7501 emsg(_(e_endwhile));
7502 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7503 emsg(_(e_endfor));
7504 else
7505 emsg(_("E1026: Missing }"));
7506 goto erret;
7507 }
7508
Bram Moolenaarefd88552020-06-18 20:50:10 +02007509 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007510 {
7511 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7512 {
7513 emsg(_("E1027: Missing return statement"));
7514 goto erret;
7515 }
7516
7517 // Return zero if there is no return at the end.
7518 generate_PUSHNR(&cctx, 0);
7519 generate_instr(&cctx, ISN_RETURN);
7520 }
7521
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007522 {
7523 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7524 + ufunc->uf_dfunc_idx;
7525 dfunc->df_deleted = FALSE;
7526 dfunc->df_instr = instr->ga_data;
7527 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007528 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007529 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007530 if (cctx.ctx_outer_used)
7531 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007532 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007533 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007534
7535 ret = OK;
7536
7537erret:
7538 if (ret == FAIL)
7539 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007540 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007541 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7542 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007543
7544 for (idx = 0; idx < instr->ga_len; ++idx)
7545 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007546 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007547
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007548 // If using the last entry in the table and it was added above, we
7549 // might as well remove it.
7550 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007551 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007552 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007553 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007554 ufunc->uf_dfunc_idx = 0;
7555 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007556 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007557
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007558 while (cctx.ctx_scope != NULL)
7559 drop_scope(&cctx);
7560
Bram Moolenaar20431c92020-03-20 18:39:46 +01007561 // Don't execute this function body.
7562 ga_clear_strings(&ufunc->uf_lines);
7563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007564 if (errormsg != NULL)
7565 emsg(errormsg);
7566 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007567 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007568 }
7569
7570 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007571 if (do_estack_push)
7572 estack_pop();
7573
Bram Moolenaar20431c92020-03-20 18:39:46 +01007574 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007575 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007576 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007577 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007578}
7579
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007580 void
7581set_function_type(ufunc_T *ufunc)
7582{
7583 int varargs = ufunc->uf_va_name != NULL;
7584 int argcount = ufunc->uf_args.ga_len;
7585
7586 // Create a type for the function, with the return type and any
7587 // argument types.
7588 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7589 // The type is included in "tt_args".
7590 if (argcount > 0 || varargs)
7591 {
7592 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7593 argcount, &ufunc->uf_type_list);
7594 // Add argument types to the function type.
7595 if (func_type_add_arg_types(ufunc->uf_func_type,
7596 argcount + varargs,
7597 &ufunc->uf_type_list) == FAIL)
7598 return;
7599 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7600 ufunc->uf_func_type->tt_min_argcount =
7601 argcount - ufunc->uf_def_args.ga_len;
7602 if (ufunc->uf_arg_types == NULL)
7603 {
7604 int i;
7605
7606 // lambda does not have argument types.
7607 for (i = 0; i < argcount; ++i)
7608 ufunc->uf_func_type->tt_args[i] = &t_any;
7609 }
7610 else
7611 mch_memmove(ufunc->uf_func_type->tt_args,
7612 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7613 if (varargs)
7614 {
7615 ufunc->uf_func_type->tt_args[argcount] =
7616 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7617 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7618 }
7619 }
7620 else
7621 // No arguments, can use a predefined type.
7622 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7623 argcount, &ufunc->uf_type_list);
7624}
7625
7626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007627/*
7628 * Delete an instruction, free what it contains.
7629 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007630 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007631delete_instr(isn_T *isn)
7632{
7633 switch (isn->isn_type)
7634 {
7635 case ISN_EXEC:
7636 case ISN_LOADENV:
7637 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007638 case ISN_LOADB:
7639 case ISN_LOADW:
7640 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007641 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007642 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007643 case ISN_PUSHEXC:
7644 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007645 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007646 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007647 case ISN_STOREB:
7648 case ISN_STOREW:
7649 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007650 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007651 vim_free(isn->isn_arg.string);
7652 break;
7653
7654 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007655 case ISN_STORES:
7656 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007657 break;
7658
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007659 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007660 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007661 vim_free(isn->isn_arg.unlet.ul_name);
7662 break;
7663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007664 case ISN_STOREOPT:
7665 vim_free(isn->isn_arg.storeopt.so_name);
7666 break;
7667
7668 case ISN_PUSHBLOB: // push blob isn_arg.blob
7669 blob_unref(isn->isn_arg.blob);
7670 break;
7671
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007672 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007673#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007674 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007675#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007676 break;
7677
7678 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007679#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007680 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007681#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007682 break;
7683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007684 case ISN_UCALL:
7685 vim_free(isn->isn_arg.ufunc.cuf_name);
7686 break;
7687
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007688 case ISN_FUNCREF:
7689 {
7690 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7691 + isn->isn_arg.funcref.fr_func;
7692 func_ptr_unref(dfunc->df_ufunc);
7693 }
7694 break;
7695
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007696 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007697 {
7698 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7699 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7700
7701 if (ufunc != NULL)
7702 {
7703 // Clear uf_dfunc_idx so that the function is deleted.
7704 clear_def_function(ufunc);
7705 ufunc->uf_dfunc_idx = 0;
7706 func_ptr_unref(ufunc);
7707 }
7708
7709 vim_free(lambda);
7710 vim_free(isn->isn_arg.newfunc.nf_global);
7711 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007712 break;
7713
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714 case ISN_2BOOL:
7715 case ISN_2STRING:
7716 case ISN_ADDBLOB:
7717 case ISN_ADDLIST:
7718 case ISN_BCALL:
7719 case ISN_CATCH:
7720 case ISN_CHECKNR:
7721 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007722 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007723 case ISN_COMPAREANY:
7724 case ISN_COMPAREBLOB:
7725 case ISN_COMPAREBOOL:
7726 case ISN_COMPAREDICT:
7727 case ISN_COMPAREFLOAT:
7728 case ISN_COMPAREFUNC:
7729 case ISN_COMPARELIST:
7730 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007731 case ISN_COMPARESPECIAL:
7732 case ISN_COMPARESTRING:
7733 case ISN_CONCAT:
7734 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007735 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007736 case ISN_DROP:
7737 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007738 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007739 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007740 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007741 case ISN_EXECCONCAT:
7742 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007743 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007744 case ISN_LISTINDEX:
7745 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007746 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007747 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007748 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007749 case ISN_JUMP:
7750 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007751 case ISN_LOADBDICT:
7752 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007753 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007754 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007755 case ISN_LOADSCRIPT:
7756 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007757 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007758 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007759 case ISN_NEGATENR:
7760 case ISN_NEWDICT:
7761 case ISN_NEWLIST:
7762 case ISN_OPNR:
7763 case ISN_OPFLOAT:
7764 case ISN_OPANY:
7765 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007766 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007767 case ISN_PUSHF:
7768 case ISN_PUSHNR:
7769 case ISN_PUSHBOOL:
7770 case ISN_PUSHSPEC:
7771 case ISN_RETURN:
7772 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007773 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007774 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007775 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007776 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007777 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007778 case ISN_STOREDICT:
7779 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007780 case ISN_THROW:
7781 case ISN_TRY:
7782 // nothing allocated
7783 break;
7784 }
7785}
7786
7787/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007788 * Free all instructions for "dfunc".
7789 */
7790 static void
7791delete_def_function_contents(dfunc_T *dfunc)
7792{
7793 int idx;
7794
7795 ga_clear(&dfunc->df_def_args_isn);
7796
7797 if (dfunc->df_instr != NULL)
7798 {
7799 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7800 delete_instr(dfunc->df_instr + idx);
7801 VIM_CLEAR(dfunc->df_instr);
7802 }
7803
7804 dfunc->df_deleted = TRUE;
7805}
7806
7807/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007808 * When a user function is deleted, clear the contents of any associated def
7809 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007810 */
7811 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007812clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007813{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007814 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815 {
7816 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7817 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007818
Bram Moolenaar20431c92020-03-20 18:39:46 +01007819 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007820 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007821 }
7822}
7823
7824#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007825/*
7826 * Free all functions defined with ":def".
7827 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007828 void
7829free_def_functions(void)
7830{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007831 int idx;
7832
7833 for (idx = 0; idx < def_functions.ga_len; ++idx)
7834 {
7835 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7836
7837 delete_def_function_contents(dfunc);
7838 }
7839
7840 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007841}
7842#endif
7843
7844
7845#endif // FEAT_EVAL