blob: 25da7f562c8ab72c2019ef2dbac034c541a8b1b4 [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 Moolenaarf9b2b492020-08-05 14:34:14 +0200151static char e_unknown_var[] = N_("E1089: unknown variable: %s");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152
Bram Moolenaar20431c92020-03-20 18:39:46 +0100153static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar0b76b422020-04-07 22:05:08 +0200154static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155
156/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200157 * Lookup variable "name" in the local scope and return it.
158 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200160 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161lookup_local(char_u *name, size_t len, cctx_T *cctx)
162{
163 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100166 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200167 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200168
169 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100170 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
171 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173 if (STRNCMP(name, lvar->lv_name, len) == 0
174 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200175 {
176 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200177 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200180
181 // Find local in outer function scope.
182 if (cctx->ctx_outer != NULL)
183 {
184 lvar = lookup_local(name, len, cctx->ctx_outer);
185 if (lvar != NULL)
186 {
187 // TODO: are there situations we should not mark the outer scope as
188 // used?
189 cctx->ctx_outer_used = TRUE;
190 lvar->lv_from_outer = TRUE;
191 return lvar;
192 }
193 }
194
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200195 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196}
197
198/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200199 * Lookup an argument in the current function and an enclosing function.
200 * Returns the argument index in "idxp"
201 * Returns the argument type in "type"
202 * Sets "gen_load_outer" to TRUE if found in outer scope.
203 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204 */
205 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200206lookup_arg(
207 char_u *name,
208 size_t len,
209 int *idxp,
210 type_T **type,
211 int *gen_load_outer,
212 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213{
214 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100217 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200218 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
220 {
221 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
222
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200223 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
224 {
225 if (idxp != NULL)
226 {
227 // Arguments are located above the frame pointer. One further
228 // if there is a vararg argument
229 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
230 + STACK_FRAME_SIZE)
231 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
232
233 if (cctx->ctx_ufunc->uf_arg_types != NULL)
234 *type = cctx->ctx_ufunc->uf_arg_types[idx];
235 else
236 *type = &t_any;
237 }
238 return OK;
239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200242 va_name = cctx->ctx_ufunc->uf_va_name;
243 if (va_name != NULL
244 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
245 {
246 if (idxp != NULL)
247 {
248 // varargs is always the last argument
249 *idxp = -STACK_FRAME_SIZE - 1;
250 *type = cctx->ctx_ufunc->uf_va_type;
251 }
252 return OK;
253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200255 if (cctx->ctx_outer != NULL)
256 {
257 // Lookup the name for an argument of the outer function.
258 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
259 == OK)
260 {
261 *gen_load_outer = TRUE;
262 return OK;
263 }
264 }
265
266 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267}
268
269/*
270 * Lookup a variable in the current script.
271 * Returns OK or FAIL.
272 */
273 static int
274lookup_script(char_u *name, size_t len)
275{
276 int cc;
277 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
278 dictitem_T *di;
279
280 cc = name[len];
281 name[len] = NUL;
282 di = find_var_in_ht(ht, 0, name, TRUE);
283 name[len] = cc;
284 return di == NULL ? FAIL: OK;
285}
286
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100287/*
288 * Check if "p[len]" is already defined, either in script "import_sid" or in
289 * compilation context "cctx".
290 * Return FAIL and give an error if it defined.
291 */
292 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200293check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100294{
Bram Moolenaarad486a02020-08-01 23:22:18 +0200295 int c = p[len];
296
297 p[len] = NUL;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100298 if (lookup_script(p, len) == OK
299 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200300 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaareef21022020-08-01 22:16:43 +0200301 || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200302 || find_imported(p, len, cctx) != NULL
303 || find_func_even_dead(p, FALSE, cctx) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100304 {
Bram Moolenaarad486a02020-08-01 23:22:18 +0200305 p[len] = c;
Bram Moolenaareef21022020-08-01 22:16:43 +0200306 semsg(_(e_already_defined), p);
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100307 return FAIL;
308 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200309 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100310 return OK;
311}
312
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200313/*
314 * Allocate memory for a type_T and add the pointer to type_gap, so that it can
315 * be freed later.
316 */
317 static type_T *
318alloc_type(garray_T *type_gap)
319{
320 type_T *type;
321
322 if (ga_grow(type_gap, 1) == FAIL)
323 return NULL;
324 type = ALLOC_CLEAR_ONE(type_T);
325 if (type != NULL)
326 {
327 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
328 ++type_gap->ga_len;
329 }
330 return type;
331}
332
Bram Moolenaar6110e792020-07-08 19:35:21 +0200333 void
334clear_type_list(garray_T *gap)
335{
336 while (gap->ga_len > 0)
337 vim_free(((type_T **)gap->ga_data)[--gap->ga_len]);
338 ga_clear(gap);
339}
340
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100341 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200342get_list_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100343{
344 type_T *type;
345
346 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200347 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100348 return &t_list_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200349 if (member_type->tt_type == VAR_VOID
350 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100351 return &t_list_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100352 if (member_type->tt_type == VAR_BOOL)
353 return &t_list_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100354 if (member_type->tt_type == VAR_NUMBER)
355 return &t_list_number;
356 if (member_type->tt_type == VAR_STRING)
357 return &t_list_string;
358
359 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200360 type = alloc_type(type_gap);
361 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100362 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363 type->tt_type = VAR_LIST;
364 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200365 type->tt_argcount = 0;
366 type->tt_args = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100367 return type;
368}
369
370 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +0200371get_dict_type(type_T *member_type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100372{
373 type_T *type;
374
375 // recognize commonly used types
Bram Moolenaar4c683752020-04-05 21:38:23 +0200376 if (member_type->tt_type == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100377 return &t_dict_any;
Bram Moolenaar4c683752020-04-05 21:38:23 +0200378 if (member_type->tt_type == VAR_VOID
379 || member_type->tt_type == VAR_UNKNOWN)
Bram Moolenaar436472f2020-02-20 22:54:43 +0100380 return &t_dict_empty;
Bram Moolenaar0c2ca582020-02-25 22:58:29 +0100381 if (member_type->tt_type == VAR_BOOL)
382 return &t_dict_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 if (member_type->tt_type == VAR_NUMBER)
384 return &t_dict_number;
385 if (member_type->tt_type == VAR_STRING)
386 return &t_dict_string;
387
388 // Not a common type, create a new entry.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200389 type = alloc_type(type_gap);
390 if (type == NULL)
Bram Moolenaar599c89c2020-03-28 14:53:20 +0100391 return &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100392 type->tt_type = VAR_DICT;
393 type->tt_member = member_type;
Bram Moolenaard77a8522020-04-03 21:59:57 +0200394 type->tt_argcount = 0;
395 type->tt_args = NULL;
396 return type;
397}
398
399/*
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200400 * Allocate a new type for a function.
401 */
402 static type_T *
403alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
404{
405 type_T *type = alloc_type(type_gap);
406
407 if (type == NULL)
408 return &t_any;
409 type->tt_type = VAR_FUNC;
410 type->tt_member = ret_type;
411 type->tt_argcount = argcount;
412 type->tt_args = NULL;
413 return type;
414}
415
416/*
Bram Moolenaard77a8522020-04-03 21:59:57 +0200417 * Get a function type, based on the return type "ret_type".
418 * If "argcount" is -1 or 0 a predefined type can be used.
419 * If "argcount" > 0 always create a new type, so that arguments can be added.
420 */
421 static type_T *
422get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
423{
Bram Moolenaard77a8522020-04-03 21:59:57 +0200424 // recognize commonly used types
425 if (argcount <= 0)
426 {
Bram Moolenaarec5929d2020-04-07 20:53:39 +0200427 if (ret_type == &t_unknown)
428 {
429 // (argcount == 0) is not possible
430 return &t_func_unknown;
431 }
Bram Moolenaard77a8522020-04-03 21:59:57 +0200432 if (ret_type == &t_void)
433 {
434 if (argcount == 0)
435 return &t_func_0_void;
436 else
437 return &t_func_void;
438 }
439 if (ret_type == &t_any)
440 {
441 if (argcount == 0)
442 return &t_func_0_any;
443 else
444 return &t_func_any;
445 }
446 if (ret_type == &t_number)
447 {
448 if (argcount == 0)
449 return &t_func_0_number;
450 else
451 return &t_func_number;
452 }
453 if (ret_type == &t_string)
454 {
455 if (argcount == 0)
456 return &t_func_0_string;
457 else
458 return &t_func_string;
459 }
460 }
461
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200462 return alloc_func_type(ret_type, argcount, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100463}
464
Bram Moolenaara8c17702020-04-01 21:17:24 +0200465/*
Bram Moolenaar5d905c22020-04-05 18:20:45 +0200466 * For a function type, reserve space for "argcount" argument types (including
467 * vararg).
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200468 */
469 static int
470func_type_add_arg_types(
471 type_T *functype,
472 int argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200473 garray_T *type_gap)
474{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200475 // To make it easy to free the space needed for the argument types, add the
476 // pointer to type_gap.
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200477 if (ga_grow(type_gap, 1) == FAIL)
478 return FAIL;
479 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
480 if (functype->tt_args == NULL)
481 return FAIL;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +0200482 ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
483 (void *)functype->tt_args;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200484 ++type_gap->ga_len;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200485 return OK;
486}
487
488/*
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200489 * Get a type_T for a typval_T.
490 * "type_list" is used to temporarily create types in.
Bram Moolenaara8c17702020-04-01 21:17:24 +0200491 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200492 type_T *
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200493typval2type(typval_T *tv, garray_T *type_gap)
Bram Moolenaara8c17702020-04-01 21:17:24 +0200494{
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200495 type_T *actual;
496 type_T *member_type;
497
Bram Moolenaara8c17702020-04-01 21:17:24 +0200498 if (tv->v_type == VAR_NUMBER)
499 return &t_number;
500 if (tv->v_type == VAR_BOOL)
Bram Moolenaar9c8bb7c2020-04-09 21:08:09 +0200501 return &t_bool; // not used
Bram Moolenaara8c17702020-04-01 21:17:24 +0200502 if (tv->v_type == VAR_STRING)
503 return &t_string;
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200504
Bram Moolenaara71e2632020-08-05 15:11:03 +0200505 if (tv->v_type == VAR_LIST)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200506 {
Bram Moolenaara71e2632020-08-05 15:11:03 +0200507 if (tv->vval.v_list == NULL || tv->vval.v_list->lv_first == NULL)
508 return &t_list_empty;
509
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200510 // Use the type of the first member, it is the most specific.
511 member_type = typval2type(&tv->vval.v_list->lv_first->li_tv, type_gap);
512 return get_list_type(member_type, type_gap);
513 }
514
Bram Moolenaara71e2632020-08-05 15:11:03 +0200515 if (tv->v_type == VAR_DICT)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200516 {
517 dict_iterator_T iter;
518 typval_T *value;
519
Bram Moolenaara71e2632020-08-05 15:11:03 +0200520 if (tv->vval.v_dict == NULL
521 || tv->vval.v_dict->dv_hashtab.ht_used == 0)
522 return &t_dict_empty;
523
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200524 // Use the type of the first value, it is the most specific.
525 dict_iterate_start(tv, &iter);
526 dict_iterate_next(&iter, &value);
527 member_type = typval2type(value, type_gap);
528 return get_dict_type(member_type, type_gap);
529 }
530
531 if (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
532 {
533 char_u *name = NULL;
534 ufunc_T *ufunc = NULL;
535
536 if (tv->v_type == VAR_PARTIAL)
537 {
538 if (tv->vval.v_partial->pt_func != NULL)
539 ufunc = tv->vval.v_partial->pt_func;
540 else
541 name = tv->vval.v_partial->pt_name;
542 }
543 else
544 name = tv->vval.v_string;
545 if (name != NULL)
546 // TODO: how about a builtin function?
547 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200548 if (ufunc != NULL)
549 {
550 // May need to get the argument types from default values by
551 // compiling the function.
552 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
553 && compile_def_function(ufunc, TRUE, NULL) == FAIL)
554 return NULL;
555 if (ufunc->uf_func_type != NULL)
556 return ufunc->uf_func_type;
557 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200558 }
559
560 actual = alloc_type(type_gap);
561 if (actual == NULL)
562 return NULL;
563 actual->tt_type = tv->v_type;
564 actual->tt_member = &t_any;
565
566 return actual;
567}
568
569/*
570 * Get a type_T for a typval_T, used for v: variables.
571 * "type_list" is used to temporarily create types in.
572 */
573 type_T *
574typval2type_vimvar(typval_T *tv, garray_T *type_gap)
575{
Bram Moolenaara8c17702020-04-01 21:17:24 +0200576 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
577 return &t_list_string;
578 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
579 return &t_dict_any;
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200580 return typval2type(tv, type_gap);
581}
582
583
584/*
585 * Return FAIL if "expected" and "actual" don't match.
586 */
587 int
588check_typval_type(type_T *expected, typval_T *actual_tv)
589{
590 garray_T type_list;
591 type_T *actual_type;
592 int res = FAIL;
593
594 ga_init2(&type_list, sizeof(type_T *), 10);
595 actual_type = typval2type(actual_tv, &type_list);
596 if (actual_type != NULL)
597 res = check_type(expected, actual_type, TRUE);
598 clear_type_list(&type_list);
599 return res;
Bram Moolenaara8c17702020-04-01 21:17:24 +0200600}
601
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200602 static void
603type_mismatch(type_T *expected, type_T *actual)
604{
605 char *tofree1, *tofree2;
606
607 semsg(_("E1013: type mismatch, expected %s but got %s"),
608 type_name(expected, &tofree1), type_name(actual, &tofree2));
609 vim_free(tofree1);
610 vim_free(tofree2);
611}
612
613 static void
614arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
615{
616 char *tofree1, *tofree2;
617
618 semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
619 argidx,
620 type_name(expected, &tofree1), type_name(actual, &tofree2));
621 vim_free(tofree1);
622 vim_free(tofree2);
623}
624
625/*
626 * Check if the expected and actual types match.
627 * Does not allow for assigning "any" to a specific type.
628 */
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200629 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200630check_type(type_T *expected, type_T *actual, int give_msg)
631{
632 int ret = OK;
633
634 // When expected is "unknown" we accept any actual type.
635 // When expected is "any" we accept any actual type except "void".
636 if (expected->tt_type != VAR_UNKNOWN
637 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
638
639 {
640 if (expected->tt_type != actual->tt_type)
641 {
642 if (give_msg)
643 type_mismatch(expected, actual);
644 return FAIL;
645 }
646 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
647 {
648 // "unknown" is used for an empty list or dict
649 if (actual->tt_member != &t_unknown)
650 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
651 }
652 else if (expected->tt_type == VAR_FUNC)
653 {
654 if (expected->tt_member != &t_unknown)
655 ret = check_type(expected->tt_member, actual->tt_member, FALSE);
656 if (ret == OK && expected->tt_argcount != -1
657 && (actual->tt_argcount < expected->tt_min_argcount
658 || actual->tt_argcount > expected->tt_argcount))
659 ret = FAIL;
Bram Moolenaarb8070e32020-07-23 20:56:04 +0200660 if (expected->tt_args != NULL && actual->tt_args != NULL)
661 {
662 int i;
663
664 for (i = 0; i < expected->tt_argcount; ++i)
665 if (check_type(expected->tt_args[i], actual->tt_args[i],
666 FALSE) == FAIL)
667 {
668 ret = FAIL;
669 break;
670 }
671 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200672 }
673 if (ret == FAIL && give_msg)
674 type_mismatch(expected, actual);
675 }
676 return ret;
677}
678
Bram Moolenaar65b95452020-07-19 14:03:09 +0200679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100680/////////////////////////////////////////////////////////////////////
681// Following generate_ functions expect the caller to call ga_grow().
682
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200683#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
684#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100686/*
687 * Generate an instruction without arguments.
688 * Returns a pointer to the new instruction, NULL if failed.
689 */
690 static isn_T *
691generate_instr(cctx_T *cctx, isntype_T isn_type)
692{
693 garray_T *instr = &cctx->ctx_instr;
694 isn_T *isn;
695
Bram Moolenaar080457c2020-03-03 21:53:32 +0100696 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 if (ga_grow(instr, 1) == FAIL)
698 return NULL;
699 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
700 isn->isn_type = isn_type;
701 isn->isn_lnum = cctx->ctx_lnum + 1;
702 ++instr->ga_len;
703
704 return isn;
705}
706
707/*
708 * Generate an instruction without arguments.
709 * "drop" will be removed from the stack.
710 * Returns a pointer to the new instruction, NULL if failed.
711 */
712 static isn_T *
713generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
714{
715 garray_T *stack = &cctx->ctx_type_stack;
716
Bram Moolenaar080457c2020-03-03 21:53:32 +0100717 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 stack->ga_len -= drop;
719 return generate_instr(cctx, isn_type);
720}
721
722/*
723 * Generate instruction "isn_type" and put "type" on the type stack.
724 */
725 static isn_T *
726generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
727{
728 isn_T *isn;
729 garray_T *stack = &cctx->ctx_type_stack;
730
731 if ((isn = generate_instr(cctx, isn_type)) == NULL)
732 return NULL;
733
734 if (ga_grow(stack, 1) == FAIL)
735 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200736 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737 ++stack->ga_len;
738
739 return isn;
740}
741
742/*
743 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
744 */
745 static int
746may_generate_2STRING(int offset, cctx_T *cctx)
747{
748 isn_T *isn;
749 garray_T *stack = &cctx->ctx_type_stack;
750 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
751
752 if ((*type)->tt_type == VAR_STRING)
753 return OK;
754 *type = &t_string;
755
756 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
757 return FAIL;
758 isn->isn_arg.number = offset;
759
760 return OK;
761}
762
763 static int
764check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
765{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200766 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200768 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 {
770 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200771 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772 else
773 semsg(_("E1036: %c requires number or float arguments"), *op);
774 return FAIL;
775 }
776 return OK;
777}
778
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200779 static int
780generate_add_instr(
781 cctx_T *cctx,
782 vartype_T vartype,
783 type_T *type1,
784 type_T *type2)
785{
786 isn_T *isn = generate_instr_drop(cctx,
787 vartype == VAR_NUMBER ? ISN_OPNR
788 : vartype == VAR_LIST ? ISN_ADDLIST
789 : vartype == VAR_BLOB ? ISN_ADDBLOB
790#ifdef FEAT_FLOAT
791 : vartype == VAR_FLOAT ? ISN_OPFLOAT
792#endif
793 : ISN_OPANY, 1);
794
795 if (vartype != VAR_LIST && vartype != VAR_BLOB
796 && type1->tt_type != VAR_ANY
797 && type2->tt_type != VAR_ANY
798 && check_number_or_float(
799 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
800 return FAIL;
801
802 if (isn != NULL)
803 isn->isn_arg.op.op_type = EXPR_ADD;
804 return isn == NULL ? FAIL : OK;
805}
806
807/*
808 * Get the type to use for an instruction for an operation on "type1" and
809 * "type2". If they are matching use a type-specific instruction. Otherwise
810 * fall back to runtime type checking.
811 */
812 static vartype_T
813operator_type(type_T *type1, type_T *type2)
814{
815 if (type1->tt_type == type2->tt_type
816 && (type1->tt_type == VAR_NUMBER
817 || type1->tt_type == VAR_LIST
818#ifdef FEAT_FLOAT
819 || type1->tt_type == VAR_FLOAT
820#endif
821 || type1->tt_type == VAR_BLOB))
822 return type1->tt_type;
823 return VAR_ANY;
824}
825
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826/*
827 * Generate an instruction with two arguments. The instruction depends on the
828 * type of the arguments.
829 */
830 static int
831generate_two_op(cctx_T *cctx, char_u *op)
832{
833 garray_T *stack = &cctx->ctx_type_stack;
834 type_T *type1;
835 type_T *type2;
836 vartype_T vartype;
837 isn_T *isn;
838
Bram Moolenaar080457c2020-03-03 21:53:32 +0100839 RETURN_OK_IF_SKIP(cctx);
840
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200841 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100842 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
843 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200844 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845
846 switch (*op)
847 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200848 case '+':
849 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100850 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851 break;
852
853 case '-':
854 case '*':
855 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
856 op) == FAIL)
857 return FAIL;
858 if (vartype == VAR_NUMBER)
859 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
860#ifdef FEAT_FLOAT
861 else if (vartype == VAR_FLOAT)
862 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
863#endif
864 else
865 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
866 if (isn != NULL)
867 isn->isn_arg.op.op_type = *op == '*'
868 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
869 break;
870
Bram Moolenaar4c683752020-04-05 21:38:23 +0200871 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200873 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 && type2->tt_type != VAR_NUMBER))
875 {
876 emsg(_("E1035: % requires number arguments"));
877 return FAIL;
878 }
879 isn = generate_instr_drop(cctx,
880 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
881 if (isn != NULL)
882 isn->isn_arg.op.op_type = EXPR_REM;
883 break;
884 }
885
886 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200887 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888 {
889 type_T *type = &t_any;
890
891#ifdef FEAT_FLOAT
892 // float+number and number+float results in float
893 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
894 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
895 type = &t_float;
896#endif
897 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
898 }
899
900 return OK;
901}
902
903/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200904 * Get the instruction to use for comparing "type1" with "type2"
905 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200907 static isntype_T
908get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100909{
910 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100911
Bram Moolenaar4c683752020-04-05 21:38:23 +0200912 if (type1 == VAR_UNKNOWN)
913 type1 = VAR_ANY;
914 if (type2 == VAR_UNKNOWN)
915 type2 = VAR_ANY;
916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917 if (type1 == type2)
918 {
919 switch (type1)
920 {
921 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
922 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
923 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
924 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
925 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
926 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
927 case VAR_LIST: isntype = ISN_COMPARELIST; break;
928 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
929 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 default: isntype = ISN_COMPAREANY; break;
931 }
932 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200933 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
935 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
936 isntype = ISN_COMPAREANY;
937
938 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
939 && (isntype == ISN_COMPAREBOOL
940 || isntype == ISN_COMPARESPECIAL
941 || isntype == ISN_COMPARENR
942 || isntype == ISN_COMPAREFLOAT))
943 {
944 semsg(_("E1037: Cannot use \"%s\" with %s"),
945 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200946 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100947 }
948 if (isntype == ISN_DROP
949 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
950 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
951 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
952 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
953 && exptype != EXPR_IS && exptype != EXPR_ISNOT
954 && (type1 == VAR_BLOB || type2 == VAR_BLOB
955 || type1 == VAR_LIST || type2 == VAR_LIST))))
956 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100957 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200959 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100960 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200961 return isntype;
962}
963
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200964 int
965check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
966{
967 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
968 return FAIL;
969 return OK;
970}
971
Bram Moolenaara5565e42020-05-09 15:44:01 +0200972/*
973 * Generate an ISN_COMPARE* instruction with a boolean result.
974 */
975 static int
976generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
977{
978 isntype_T isntype;
979 isn_T *isn;
980 garray_T *stack = &cctx->ctx_type_stack;
981 vartype_T type1;
982 vartype_T type2;
983
984 RETURN_OK_IF_SKIP(cctx);
985
986 // Get the known type of the two items on the stack. If they are matching
987 // use a type-specific instruction. Otherwise fall back to runtime type
988 // checking.
989 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
990 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
991 isntype = get_compare_isn(exptype, type1, type2);
992 if (isntype == ISN_DROP)
993 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994
995 if ((isn = generate_instr(cctx, isntype)) == NULL)
996 return FAIL;
997 isn->isn_arg.op.op_type = exptype;
998 isn->isn_arg.op.op_ic = ic;
999
1000 // takes two arguments, puts one bool back
1001 if (stack->ga_len >= 2)
1002 {
1003 --stack->ga_len;
1004 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
1005 }
1006
1007 return OK;
1008}
1009
1010/*
1011 * Generate an ISN_2BOOL instruction.
1012 */
1013 static int
1014generate_2BOOL(cctx_T *cctx, int invert)
1015{
1016 isn_T *isn;
1017 garray_T *stack = &cctx->ctx_type_stack;
1018
Bram Moolenaar080457c2020-03-03 21:53:32 +01001019 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
1021 return FAIL;
1022 isn->isn_arg.number = invert;
1023
1024 // type becomes bool
1025 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
1026
1027 return OK;
1028}
1029
1030 static int
1031generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
1032{
1033 isn_T *isn;
1034 garray_T *stack = &cctx->ctx_type_stack;
1035
Bram Moolenaar080457c2020-03-03 21:53:32 +01001036 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001037 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
1038 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +02001039 // TODO: whole type, e.g. for a function also arg and return types
1040 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041 isn->isn_arg.type.ct_off = offset;
1042
1043 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001044 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001045
1046 return OK;
1047}
1048
1049/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001050 * Check that
1051 * - "actual" is "expected" type or
1052 * - "actual" is a type that can be "expected" type: add a runtime check; or
1053 * - return FAIL.
1054 */
1055 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001056need_type(
1057 type_T *actual,
1058 type_T *expected,
1059 int offset,
1060 cctx_T *cctx,
1061 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001062{
1063 if (check_type(expected, actual, FALSE) == OK)
1064 return OK;
1065 if (actual->tt_type != VAR_ANY
1066 && actual->tt_type != VAR_UNKNOWN
1067 && !(actual->tt_type == VAR_FUNC
1068 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
1069 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001070 if (!silent)
1071 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001072 return FAIL;
1073 }
1074 generate_TYPECHECK(cctx, expected, offset);
1075 return OK;
1076}
1077
1078/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001079 * Generate an ISN_PUSHNR instruction.
1080 */
1081 static int
1082generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1083{
1084 isn_T *isn;
1085
Bram Moolenaar080457c2020-03-03 21:53:32 +01001086 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1088 return FAIL;
1089 isn->isn_arg.number = number;
1090
1091 return OK;
1092}
1093
1094/*
1095 * Generate an ISN_PUSHBOOL instruction.
1096 */
1097 static int
1098generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1099{
1100 isn_T *isn;
1101
Bram Moolenaar080457c2020-03-03 21:53:32 +01001102 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001103 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1104 return FAIL;
1105 isn->isn_arg.number = number;
1106
1107 return OK;
1108}
1109
1110/*
1111 * Generate an ISN_PUSHSPEC instruction.
1112 */
1113 static int
1114generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1115{
1116 isn_T *isn;
1117
Bram Moolenaar080457c2020-03-03 21:53:32 +01001118 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1120 return FAIL;
1121 isn->isn_arg.number = number;
1122
1123 return OK;
1124}
1125
1126#ifdef FEAT_FLOAT
1127/*
1128 * Generate an ISN_PUSHF instruction.
1129 */
1130 static int
1131generate_PUSHF(cctx_T *cctx, float_T fnumber)
1132{
1133 isn_T *isn;
1134
Bram Moolenaar080457c2020-03-03 21:53:32 +01001135 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001136 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1137 return FAIL;
1138 isn->isn_arg.fnumber = fnumber;
1139
1140 return OK;
1141}
1142#endif
1143
1144/*
1145 * Generate an ISN_PUSHS instruction.
1146 * Consumes "str".
1147 */
1148 static int
1149generate_PUSHS(cctx_T *cctx, char_u *str)
1150{
1151 isn_T *isn;
1152
Bram Moolenaar080457c2020-03-03 21:53:32 +01001153 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1155 return FAIL;
1156 isn->isn_arg.string = str;
1157
1158 return OK;
1159}
1160
1161/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001162 * Generate an ISN_PUSHCHANNEL instruction.
1163 * Consumes "channel".
1164 */
1165 static int
1166generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1167{
1168 isn_T *isn;
1169
Bram Moolenaar080457c2020-03-03 21:53:32 +01001170 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001171 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1172 return FAIL;
1173 isn->isn_arg.channel = channel;
1174
1175 return OK;
1176}
1177
1178/*
1179 * Generate an ISN_PUSHJOB instruction.
1180 * Consumes "job".
1181 */
1182 static int
1183generate_PUSHJOB(cctx_T *cctx, job_T *job)
1184{
1185 isn_T *isn;
1186
Bram Moolenaar080457c2020-03-03 21:53:32 +01001187 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001188 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001189 return FAIL;
1190 isn->isn_arg.job = job;
1191
1192 return OK;
1193}
1194
1195/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196 * Generate an ISN_PUSHBLOB instruction.
1197 * Consumes "blob".
1198 */
1199 static int
1200generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1201{
1202 isn_T *isn;
1203
Bram Moolenaar080457c2020-03-03 21:53:32 +01001204 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001205 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1206 return FAIL;
1207 isn->isn_arg.blob = blob;
1208
1209 return OK;
1210}
1211
1212/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001213 * Generate an ISN_PUSHFUNC instruction with name "name".
1214 * Consumes "name".
1215 */
1216 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001217generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001218{
1219 isn_T *isn;
1220
Bram Moolenaar080457c2020-03-03 21:53:32 +01001221 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001222 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001223 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001224 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001225
1226 return OK;
1227}
1228
1229/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001230 * Generate an ISN_GETITEM instruction with "index".
1231 */
1232 static int
1233generate_GETITEM(cctx_T *cctx, int index)
1234{
1235 isn_T *isn;
1236 garray_T *stack = &cctx->ctx_type_stack;
1237 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1238 type_T *item_type = &t_any;
1239
1240 RETURN_OK_IF_SKIP(cctx);
1241
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001242 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001243 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001244 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001245 emsg(_(e_listreq));
1246 return FAIL;
1247 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001248 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001249 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1250 return FAIL;
1251 isn->isn_arg.number = index;
1252
1253 // add the item type to the type stack
1254 if (ga_grow(stack, 1) == FAIL)
1255 return FAIL;
1256 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1257 ++stack->ga_len;
1258 return OK;
1259}
1260
1261/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001262 * Generate an ISN_SLICE instruction with "count".
1263 */
1264 static int
1265generate_SLICE(cctx_T *cctx, int count)
1266{
1267 isn_T *isn;
1268
1269 RETURN_OK_IF_SKIP(cctx);
1270 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1271 return FAIL;
1272 isn->isn_arg.number = count;
1273 return OK;
1274}
1275
1276/*
1277 * Generate an ISN_CHECKLEN instruction with "min_len".
1278 */
1279 static int
1280generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1281{
1282 isn_T *isn;
1283
1284 RETURN_OK_IF_SKIP(cctx);
1285
1286 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1287 return FAIL;
1288 isn->isn_arg.checklen.cl_min_len = min_len;
1289 isn->isn_arg.checklen.cl_more_OK = more_OK;
1290
1291 return OK;
1292}
1293
1294/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 * Generate an ISN_STORE instruction.
1296 */
1297 static int
1298generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1299{
1300 isn_T *isn;
1301
Bram Moolenaar080457c2020-03-03 21:53:32 +01001302 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1304 return FAIL;
1305 if (name != NULL)
1306 isn->isn_arg.string = vim_strsave(name);
1307 else
1308 isn->isn_arg.number = idx;
1309
1310 return OK;
1311}
1312
1313/*
1314 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1315 */
1316 static int
1317generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1318{
1319 isn_T *isn;
1320
Bram Moolenaar080457c2020-03-03 21:53:32 +01001321 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1323 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001324 isn->isn_arg.storenr.stnr_idx = idx;
1325 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326
1327 return OK;
1328}
1329
1330/*
1331 * Generate an ISN_STOREOPT instruction
1332 */
1333 static int
1334generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1335{
1336 isn_T *isn;
1337
Bram Moolenaar080457c2020-03-03 21:53:32 +01001338 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001339 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 return FAIL;
1341 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1342 isn->isn_arg.storeopt.so_flags = opt_flags;
1343
1344 return OK;
1345}
1346
1347/*
1348 * Generate an ISN_LOAD or similar instruction.
1349 */
1350 static int
1351generate_LOAD(
1352 cctx_T *cctx,
1353 isntype_T isn_type,
1354 int idx,
1355 char_u *name,
1356 type_T *type)
1357{
1358 isn_T *isn;
1359
Bram Moolenaar080457c2020-03-03 21:53:32 +01001360 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1362 return FAIL;
1363 if (name != NULL)
1364 isn->isn_arg.string = vim_strsave(name);
1365 else
1366 isn->isn_arg.number = idx;
1367
1368 return OK;
1369}
1370
1371/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001372 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001373 */
1374 static int
1375generate_LOADV(
1376 cctx_T *cctx,
1377 char_u *name,
1378 int error)
1379{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001380 int di_flags;
1381 int vidx = find_vim_var(name, &di_flags);
1382 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001383
Bram Moolenaar080457c2020-03-03 21:53:32 +01001384 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001385 if (vidx < 0)
1386 {
1387 if (error)
1388 semsg(_(e_var_notfound), name);
1389 return FAIL;
1390 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001391 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001392
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001393 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001394}
1395
1396/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001397 * Generate an ISN_UNLET instruction.
1398 */
1399 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001400generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001401{
1402 isn_T *isn;
1403
1404 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001405 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001406 return FAIL;
1407 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1408 isn->isn_arg.unlet.ul_forceit = forceit;
1409
1410 return OK;
1411}
1412
1413/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414 * Generate an ISN_LOADS instruction.
1415 */
1416 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001417generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001419 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001421 int sid,
1422 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423{
1424 isn_T *isn;
1425
Bram Moolenaar080457c2020-03-03 21:53:32 +01001426 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001427 if (isn_type == ISN_LOADS)
1428 isn = generate_instr_type(cctx, isn_type, type);
1429 else
1430 isn = generate_instr_drop(cctx, isn_type, 1);
1431 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001433 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1434 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435
1436 return OK;
1437}
1438
1439/*
1440 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1441 */
1442 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001443generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 cctx_T *cctx,
1445 isntype_T isn_type,
1446 int sid,
1447 int idx,
1448 type_T *type)
1449{
1450 isn_T *isn;
1451
Bram Moolenaar080457c2020-03-03 21:53:32 +01001452 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453 if (isn_type == ISN_LOADSCRIPT)
1454 isn = generate_instr_type(cctx, isn_type, type);
1455 else
1456 isn = generate_instr_drop(cctx, isn_type, 1);
1457 if (isn == NULL)
1458 return FAIL;
1459 isn->isn_arg.script.script_sid = sid;
1460 isn->isn_arg.script.script_idx = idx;
1461 return OK;
1462}
1463
1464/*
1465 * Generate an ISN_NEWLIST instruction.
1466 */
1467 static int
1468generate_NEWLIST(cctx_T *cctx, int count)
1469{
1470 isn_T *isn;
1471 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 type_T *type;
1473 type_T *member;
1474
Bram Moolenaar080457c2020-03-03 21:53:32 +01001475 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1477 return FAIL;
1478 isn->isn_arg.number = count;
1479
1480 // drop the value types
1481 stack->ga_len -= count;
1482
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001483 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001484 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 if (count > 0)
1486 member = ((type_T **)stack->ga_data)[stack->ga_len];
1487 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001488 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001489 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001490
1491 // add the list type to the type stack
1492 if (ga_grow(stack, 1) == FAIL)
1493 return FAIL;
1494 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1495 ++stack->ga_len;
1496
1497 return OK;
1498}
1499
1500/*
1501 * Generate an ISN_NEWDICT instruction.
1502 */
1503 static int
1504generate_NEWDICT(cctx_T *cctx, int count)
1505{
1506 isn_T *isn;
1507 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508 type_T *type;
1509 type_T *member;
1510
Bram Moolenaar080457c2020-03-03 21:53:32 +01001511 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1513 return FAIL;
1514 isn->isn_arg.number = count;
1515
1516 // drop the key and value types
1517 stack->ga_len -= 2 * count;
1518
Bram Moolenaar436472f2020-02-20 22:54:43 +01001519 // Use the first value type for the list member type. Use "void" for an
1520 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 if (count > 0)
1522 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1523 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001524 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001525 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526
1527 // add the dict type to the type stack
1528 if (ga_grow(stack, 1) == FAIL)
1529 return FAIL;
1530 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1531 ++stack->ga_len;
1532
1533 return OK;
1534}
1535
1536/*
1537 * Generate an ISN_FUNCREF instruction.
1538 */
1539 static int
1540generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1541{
1542 isn_T *isn;
1543 garray_T *stack = &cctx->ctx_type_stack;
1544
Bram Moolenaar080457c2020-03-03 21:53:32 +01001545 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1547 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001548 isn->isn_arg.funcref.fr_func = dfunc_idx;
1549 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550
1551 if (ga_grow(stack, 1) == FAIL)
1552 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001553 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 // TODO: argument and return types
1555 ++stack->ga_len;
1556
1557 return OK;
1558}
1559
1560/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001561 * Generate an ISN_NEWFUNC instruction.
1562 */
1563 static int
1564generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1565{
1566 isn_T *isn;
1567 char_u *name;
1568
1569 RETURN_OK_IF_SKIP(cctx);
1570 name = vim_strsave(lambda_name);
1571 if (name == NULL)
1572 return FAIL;
1573 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1574 return FAIL;
1575 isn->isn_arg.newfunc.nf_lambda = name;
1576 isn->isn_arg.newfunc.nf_global = func_name;
1577
1578 return OK;
1579}
1580
1581/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582 * Generate an ISN_JUMP instruction.
1583 */
1584 static int
1585generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1586{
1587 isn_T *isn;
1588 garray_T *stack = &cctx->ctx_type_stack;
1589
Bram Moolenaar080457c2020-03-03 21:53:32 +01001590 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1592 return FAIL;
1593 isn->isn_arg.jump.jump_when = when;
1594 isn->isn_arg.jump.jump_where = where;
1595
1596 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1597 --stack->ga_len;
1598
1599 return OK;
1600}
1601
1602 static int
1603generate_FOR(cctx_T *cctx, int loop_idx)
1604{
1605 isn_T *isn;
1606 garray_T *stack = &cctx->ctx_type_stack;
1607
Bram Moolenaar080457c2020-03-03 21:53:32 +01001608 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1610 return FAIL;
1611 isn->isn_arg.forloop.for_idx = loop_idx;
1612
1613 if (ga_grow(stack, 1) == FAIL)
1614 return FAIL;
1615 // type doesn't matter, will be stored next
1616 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1617 ++stack->ga_len;
1618
1619 return OK;
1620}
1621
1622/*
1623 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001624 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625 * Return FAIL if the number of arguments is wrong.
1626 */
1627 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001628generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629{
1630 isn_T *isn;
1631 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001632 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001633 type_T *argtypes[MAX_FUNC_ARGS];
1634 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635
Bram Moolenaar080457c2020-03-03 21:53:32 +01001636 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001637 argoff = check_internal_func(func_idx, argcount);
1638 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 return FAIL;
1640
Bram Moolenaar389df252020-07-09 21:20:47 +02001641 if (method_call && argoff > 1)
1642 {
1643 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1644 return FAIL;
1645 isn->isn_arg.shuffle.shfl_item = argcount;
1646 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1647 }
1648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1650 return FAIL;
1651 isn->isn_arg.bfunc.cbf_idx = func_idx;
1652 isn->isn_arg.bfunc.cbf_argcount = argcount;
1653
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001654 for (i = 0; i < argcount; ++i)
1655 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 stack->ga_len -= argcount; // drop the arguments
1658 if (ga_grow(stack, 1) == FAIL)
1659 return FAIL;
1660 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001661 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001662 ++stack->ga_len; // add return value
1663
1664 return OK;
1665}
1666
1667/*
1668 * Generate an ISN_DCALL or ISN_UCALL instruction.
1669 * Return FAIL if the number of arguments is wrong.
1670 */
1671 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001672generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673{
1674 isn_T *isn;
1675 garray_T *stack = &cctx->ctx_type_stack;
1676 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001677 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678
Bram Moolenaar080457c2020-03-03 21:53:32 +01001679 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 if (argcount > regular_args && !has_varargs(ufunc))
1681 {
1682 semsg(_(e_toomanyarg), ufunc->uf_name);
1683 return FAIL;
1684 }
1685 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1686 {
1687 semsg(_(e_toofewarg), ufunc->uf_name);
1688 return FAIL;
1689 }
1690
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001691 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001692 {
1693 int i;
1694
1695 for (i = 0; i < argcount; ++i)
1696 {
1697 type_T *expected;
1698 type_T *actual;
1699
1700 if (i < regular_args)
1701 {
1702 if (ufunc->uf_arg_types == NULL)
1703 continue;
1704 expected = ufunc->uf_arg_types[i];
1705 }
1706 else
1707 expected = ufunc->uf_va_type->tt_member;
1708 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001709 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001710 {
1711 arg_type_mismatch(expected, actual, i + 1);
1712 return FAIL;
1713 }
1714 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001715 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001716 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001717 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001718 }
1719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001721 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001722 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001724 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 {
1726 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1727 isn->isn_arg.dfunc.cdf_argcount = argcount;
1728 }
1729 else
1730 {
1731 // A user function may be deleted and redefined later, can't use the
1732 // ufunc pointer, need to look it up again at runtime.
1733 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1734 isn->isn_arg.ufunc.cuf_argcount = argcount;
1735 }
1736
1737 stack->ga_len -= argcount; // drop the arguments
1738 if (ga_grow(stack, 1) == FAIL)
1739 return FAIL;
1740 // add return value
1741 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1742 ++stack->ga_len;
1743
1744 return OK;
1745}
1746
1747/*
1748 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1749 */
1750 static int
1751generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1752{
1753 isn_T *isn;
1754 garray_T *stack = &cctx->ctx_type_stack;
1755
Bram Moolenaar080457c2020-03-03 21:53:32 +01001756 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1758 return FAIL;
1759 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1760 isn->isn_arg.ufunc.cuf_argcount = argcount;
1761
1762 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001763 if (ga_grow(stack, 1) == FAIL)
1764 return FAIL;
1765 // add return value
1766 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1767 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768
1769 return OK;
1770}
1771
1772/*
1773 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001774 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 */
1776 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001777generate_PCALL(
1778 cctx_T *cctx,
1779 int argcount,
1780 char_u *name,
1781 type_T *type,
1782 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783{
1784 isn_T *isn;
1785 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001786 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787
Bram Moolenaar080457c2020-03-03 21:53:32 +01001788 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001789
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001790 if (type->tt_type == VAR_ANY)
1791 ret_type = &t_any;
1792 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001793 {
1794 if (type->tt_argcount != -1)
1795 {
1796 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1797
1798 if (argcount < type->tt_min_argcount - varargs)
1799 {
1800 semsg(_(e_toofewarg), "[reference]");
1801 return FAIL;
1802 }
1803 if (!varargs && argcount > type->tt_argcount)
1804 {
1805 semsg(_(e_toomanyarg), "[reference]");
1806 return FAIL;
1807 }
1808 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001809 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001810 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001811 else
1812 {
1813 semsg(_("E1085: Not a callable type: %s"), name);
1814 return FAIL;
1815 }
1816
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1818 return FAIL;
1819 isn->isn_arg.pfunc.cpf_top = at_top;
1820 isn->isn_arg.pfunc.cpf_argcount = argcount;
1821
1822 stack->ga_len -= argcount; // drop the arguments
1823
1824 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001825 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001827 // If partial is above the arguments it must be cleared and replaced with
1828 // the return value.
1829 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1830 return FAIL;
1831
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832 return OK;
1833}
1834
1835/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001836 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 */
1838 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001839generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840{
1841 isn_T *isn;
1842 garray_T *stack = &cctx->ctx_type_stack;
1843 type_T *type;
1844
Bram Moolenaar080457c2020-03-03 21:53:32 +01001845 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001846 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001848 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001850 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001852 if (type->tt_type != VAR_DICT && type != &t_any)
1853 {
1854 emsg(_(e_dictreq));
1855 return FAIL;
1856 }
1857 // change dict type to dict member type
1858 if (type->tt_type == VAR_DICT)
1859 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860
1861 return OK;
1862}
1863
1864/*
1865 * Generate an ISN_ECHO instruction.
1866 */
1867 static int
1868generate_ECHO(cctx_T *cctx, int with_white, int count)
1869{
1870 isn_T *isn;
1871
Bram Moolenaar080457c2020-03-03 21:53:32 +01001872 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1874 return FAIL;
1875 isn->isn_arg.echo.echo_with_white = with_white;
1876 isn->isn_arg.echo.echo_count = count;
1877
1878 return OK;
1879}
1880
Bram Moolenaarad39c092020-02-26 18:23:43 +01001881/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001882 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001883 */
1884 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001885generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001886{
1887 isn_T *isn;
1888
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001889 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001890 return FAIL;
1891 isn->isn_arg.number = count;
1892
1893 return OK;
1894}
1895
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001896 static int
1897generate_EXEC(cctx_T *cctx, char_u *line)
1898{
1899 isn_T *isn;
1900
Bram Moolenaar080457c2020-03-03 21:53:32 +01001901 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1903 return FAIL;
1904 isn->isn_arg.string = vim_strsave(line);
1905 return OK;
1906}
1907
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001908 static int
1909generate_EXECCONCAT(cctx_T *cctx, int count)
1910{
1911 isn_T *isn;
1912
1913 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1914 return FAIL;
1915 isn->isn_arg.number = count;
1916 return OK;
1917}
1918
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919/*
1920 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001921 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001923 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1925{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 lvar_T *lvar;
1927
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001928 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001930 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001931 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 }
1933
1934 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001935 return NULL;
1936 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001938 // Every local variable uses the next entry on the stack. We could re-use
1939 // the last ones when leaving a scope, but then variables used in a closure
1940 // might get overwritten. To keep things simple do not re-use stack
1941 // entries. This is less efficient, but memory is cheap these days.
1942 lvar->lv_idx = cctx->ctx_locals_count++;
1943
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001944 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 lvar->lv_const = isConst;
1946 lvar->lv_type = type;
1947
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001948 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949}
1950
1951/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001952 * Remove local variables above "new_top".
1953 */
1954 static void
1955unwind_locals(cctx_T *cctx, int new_top)
1956{
1957 if (cctx->ctx_locals.ga_len > new_top)
1958 {
1959 int idx;
1960 lvar_T *lvar;
1961
1962 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1963 {
1964 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1965 vim_free(lvar->lv_name);
1966 }
1967 }
1968 cctx->ctx_locals.ga_len = new_top;
1969}
1970
1971/*
1972 * Free all local variables.
1973 */
1974 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001975free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001976{
1977 unwind_locals(cctx, 0);
1978 ga_clear(&cctx->ctx_locals);
1979}
1980
1981/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 * Skip over a type definition and return a pointer to just after it.
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001983 * When "optional" is TRUE then a leading "?" is accepted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 */
1985 char_u *
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001986skip_type(char_u *start, int optional)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987{
1988 char_u *p = start;
1989
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001990 if (optional && *p == '?')
1991 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992 while (ASCII_ISALNUM(*p) || *p == '_')
1993 ++p;
1994
1995 // Skip over "<type>"; this is permissive about white space.
1996 if (*skipwhite(p) == '<')
1997 {
1998 p = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001999 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 p = skipwhite(p);
2001 if (*p == '>')
2002 ++p;
2003 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002004 else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1])))
2005 && STRNCMP("func", start, 4) == 0)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002006 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002007 if (*p == '(')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002008 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002009 // handle func(args): type
2010 ++p;
2011 while (*p != ')' && *p != NUL)
2012 {
2013 char_u *sp = p;
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02002014
Bram Moolenaarace61322020-07-26 18:16:58 +02002015 if (STRNCMP(p, "...", 3) == 0)
2016 p += 3;
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02002017 p = skip_type(p, TRUE);
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002018 if (p == sp)
2019 return p; // syntax error
2020 if (*p == ',')
2021 p = skipwhite(p + 1);
2022 }
2023 if (*p == ')')
2024 {
2025 if (p[1] == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02002026 p = skip_type(skipwhite(p + 2), FALSE);
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002027 else
Bram Moolenaarbfba8652020-07-23 20:09:10 +02002028 ++p;
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002029 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002030 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002031 else
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002032 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002033 // handle func: return_type
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02002034 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002035 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002036 }
2037
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 return p;
2039}
2040
2041/*
2042 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02002043 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044 * Returns NULL in case of failure.
2045 */
2046 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02002047parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048{
2049 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002050 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051
2052 if (**arg != '<')
2053 {
2054 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02002055 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 else
2057 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01002058 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059 }
2060 *arg = skipwhite(*arg + 1);
2061
Bram Moolenaard77a8522020-04-03 21:59:57 +02002062 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063
2064 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002065 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 {
2067 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01002068 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069 }
2070 ++*arg;
2071
2072 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002073 return get_list_type(member_type, type_gap);
2074 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075}
2076
2077/*
2078 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02002079 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080 */
2081 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02002082parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083{
2084 char_u *p = *arg;
2085 size_t len;
2086
2087 // skip over the first word
2088 while (ASCII_ISALNUM(*p) || *p == '_')
2089 ++p;
2090 len = p - *arg;
2091
2092 switch (**arg)
2093 {
2094 case 'a':
2095 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
2096 {
2097 *arg += len;
2098 return &t_any;
2099 }
2100 break;
2101 case 'b':
2102 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
2103 {
2104 *arg += len;
2105 return &t_bool;
2106 }
2107 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
2108 {
2109 *arg += len;
2110 return &t_blob;
2111 }
2112 break;
2113 case 'c':
2114 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
2115 {
2116 *arg += len;
2117 return &t_channel;
2118 }
2119 break;
2120 case 'd':
2121 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
2122 {
2123 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002124 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 }
2126 break;
2127 case 'f':
2128 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
2129 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01002130#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 *arg += len;
2132 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01002133#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002134 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01002135 return &t_any;
2136#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 }
2138 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
2139 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002140 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002141 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002142 int argcount = -1;
2143 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002144 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002145 type_T *arg_type[MAX_FUNC_ARGS + 1];
2146
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002147 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002149 if (**arg == '(')
2150 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002151 // "func" may or may not return a value, "func()" does
2152 // not return a value.
2153 ret_type = &t_void;
2154
Bram Moolenaard77a8522020-04-03 21:59:57 +02002155 p = ++*arg;
2156 argcount = 0;
2157 while (*p != NUL && *p != ')')
2158 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002159 if (*p == '?')
2160 {
2161 if (first_optional == -1)
2162 first_optional = argcount;
2163 ++p;
2164 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002165 else if (STRNCMP(p, "...", 3) == 0)
2166 {
2167 flags |= TTFLAG_VARARGS;
2168 p += 3;
2169 }
Bram Moolenaar01865ad2020-07-26 18:33:09 +02002170 else if (first_optional != -1)
2171 {
2172 emsg(_("E1007: mandatory argument after optional argument"));
2173 return &t_any;
2174 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002175
2176 arg_type[argcount++] = parse_type(&p, type_gap);
2177
2178 // Nothing comes after "...{type}".
2179 if (flags & TTFLAG_VARARGS)
2180 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002181
Bram Moolenaard77a8522020-04-03 21:59:57 +02002182 if (*p != ',' && *skipwhite(p) == ',')
2183 {
2184 semsg(_(e_no_white_before), ",");
2185 return &t_any;
2186 }
2187 if (*p == ',')
2188 {
2189 ++p;
2190 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002191 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002192 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002193 return &t_any;
2194 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002195 }
2196 p = skipwhite(p);
2197 if (argcount == MAX_FUNC_ARGS)
2198 {
2199 emsg(_("E740: Too many argument types"));
2200 return &t_any;
2201 }
2202 }
2203
2204 p = skipwhite(p);
2205 if (*p != ')')
2206 {
2207 emsg(_(e_missing_close));
2208 return &t_any;
2209 }
2210 *arg = p + 1;
2211 }
2212 if (**arg == ':')
2213 {
2214 // parse return type
2215 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002216 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002217 semsg(_(e_white_after), ":");
2218 *arg = skipwhite(*arg);
2219 ret_type = parse_type(arg, type_gap);
2220 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002221 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002222 type = get_func_type(ret_type, argcount, type_gap);
2223 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002224 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002225 type = alloc_func_type(ret_type, argcount, type_gap);
2226 type->tt_flags = flags;
2227 if (argcount > 0)
2228 {
2229 type->tt_argcount = argcount;
2230 type->tt_min_argcount = first_optional == -1
2231 ? argcount : first_optional;
2232 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002233 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002234 return &t_any;
2235 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002236 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002237 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002238 }
2239 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002240 }
2241 break;
2242 case 'j':
2243 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2244 {
2245 *arg += len;
2246 return &t_job;
2247 }
2248 break;
2249 case 'l':
2250 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2251 {
2252 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002253 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254 }
2255 break;
2256 case 'n':
2257 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2258 {
2259 *arg += len;
2260 return &t_number;
2261 }
2262 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 case 's':
2264 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2265 {
2266 *arg += len;
2267 return &t_string;
2268 }
2269 break;
2270 case 'v':
2271 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2272 {
2273 *arg += len;
2274 return &t_void;
2275 }
2276 break;
2277 }
2278
2279 semsg(_("E1010: Type not recognized: %s"), *arg);
2280 return &t_any;
2281}
2282
2283/*
2284 * Check if "type1" and "type2" are exactly the same.
2285 */
2286 static int
2287equal_type(type_T *type1, type_T *type2)
2288{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002289 int i;
2290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002291 if (type1->tt_type != type2->tt_type)
2292 return FALSE;
2293 switch (type1->tt_type)
2294 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002296 case VAR_ANY:
2297 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 case VAR_SPECIAL:
2299 case VAR_BOOL:
2300 case VAR_NUMBER:
2301 case VAR_FLOAT:
2302 case VAR_STRING:
2303 case VAR_BLOB:
2304 case VAR_JOB:
2305 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002306 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 case VAR_LIST:
2308 case VAR_DICT:
2309 return equal_type(type1->tt_member, type2->tt_member);
2310 case VAR_FUNC:
2311 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002312 if (!equal_type(type1->tt_member, type2->tt_member)
2313 || type1->tt_argcount != type2->tt_argcount)
2314 return FALSE;
2315 if (type1->tt_argcount < 0
2316 || type1->tt_args == NULL || type2->tt_args == NULL)
2317 return TRUE;
2318 for (i = 0; i < type1->tt_argcount; ++i)
2319 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2320 return FALSE;
2321 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 }
2323 return TRUE;
2324}
2325
2326/*
2327 * Find the common type of "type1" and "type2" and put it in "dest".
2328 * "type2" and "dest" may be the same.
2329 */
2330 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002331common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332{
2333 if (equal_type(type1, type2))
2334 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002335 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336 return;
2337 }
2338
2339 if (type1->tt_type == type2->tt_type)
2340 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2342 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002343 type_T *common;
2344
Bram Moolenaard77a8522020-04-03 21:59:57 +02002345 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002346 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002347 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002348 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002349 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 return;
2351 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002352 if (type1->tt_type == VAR_FUNC)
2353 {
2354 type_T *common;
2355
2356 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2357 if (type1->tt_argcount == type2->tt_argcount
2358 && type1->tt_argcount >= 0)
2359 {
2360 int argcount = type1->tt_argcount;
2361 int i;
2362
2363 *dest = alloc_func_type(common, argcount, type_gap);
2364 if (type1->tt_args != NULL && type2->tt_args != NULL)
2365 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002366 if (func_type_add_arg_types(*dest, argcount,
2367 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002368 for (i = 0; i < argcount; ++i)
2369 common_type(type1->tt_args[i], type2->tt_args[i],
2370 &(*dest)->tt_args[i], type_gap);
2371 }
2372 }
2373 else
2374 *dest = alloc_func_type(common, -1, type_gap);
2375 return;
2376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377 }
2378
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002379 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380}
2381
2382 char *
2383vartype_name(vartype_T type)
2384{
2385 switch (type)
2386 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002387 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002388 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 case VAR_SPECIAL: return "special";
2391 case VAR_BOOL: return "bool";
2392 case VAR_NUMBER: return "number";
2393 case VAR_FLOAT: return "float";
2394 case VAR_STRING: return "string";
2395 case VAR_BLOB: return "blob";
2396 case VAR_JOB: return "job";
2397 case VAR_CHANNEL: return "channel";
2398 case VAR_LIST: return "list";
2399 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002400
2401 case VAR_FUNC:
2402 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002404 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405}
2406
2407/*
2408 * Return the name of a type.
2409 * The result may be in allocated memory, in which case "tofree" is set.
2410 */
2411 char *
2412type_name(type_T *type, char **tofree)
2413{
2414 char *name = vartype_name(type->tt_type);
2415
2416 *tofree = NULL;
2417 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2418 {
2419 char *member_free;
2420 char *member_name = type_name(type->tt_member, &member_free);
2421 size_t len;
2422
2423 len = STRLEN(name) + STRLEN(member_name) + 3;
2424 *tofree = alloc(len);
2425 if (*tofree != NULL)
2426 {
2427 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2428 vim_free(member_free);
2429 return *tofree;
2430 }
2431 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002432 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002433 {
2434 garray_T ga;
2435 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002436 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002437
2438 ga_init2(&ga, 1, 100);
2439 if (ga_grow(&ga, 20) == FAIL)
2440 return "[unknown]";
2441 *tofree = ga.ga_data;
2442 STRCPY(ga.ga_data, "func(");
2443 ga.ga_len += 5;
2444
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002445 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002446 {
2447 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002448 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002449 int len;
2450
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002451 if (type->tt_args == NULL)
2452 arg_type = "[unknown]";
2453 else
2454 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002455 if (i > 0)
2456 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002457 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002458 ga.ga_len += 2;
2459 }
2460 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002461 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002462 {
2463 vim_free(arg_free);
2464 return "[unknown]";
2465 }
2466 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002467 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002468 {
2469 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2470 ga.ga_len += 3;
2471 }
2472 else if (i >= type->tt_min_argcount)
2473 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002474 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002475 ga.ga_len += len;
2476 vim_free(arg_free);
2477 }
2478
2479 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002480 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002481 else
2482 {
2483 char *ret_free;
2484 char *ret_name = type_name(type->tt_member, &ret_free);
2485 int len;
2486
2487 len = (int)STRLEN(ret_name) + 4;
2488 if (ga_grow(&ga, len) == FAIL)
2489 {
2490 vim_free(ret_free);
2491 return "[unknown]";
2492 }
2493 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002494 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2495 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002496 vim_free(ret_free);
2497 }
2498 return ga.ga_data;
2499 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500
2501 return name;
2502}
2503
2504/*
2505 * Find "name" in script-local items of script "sid".
2506 * Returns the index in "sn_var_vals" if found.
2507 * If found but not in "sn_var_vals" returns -1.
2508 * If not found returns -2.
2509 */
2510 int
2511get_script_item_idx(int sid, char_u *name, int check_writable)
2512{
2513 hashtab_T *ht;
2514 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002515 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 int idx;
2517
2518 // First look the name up in the hashtable.
2519 if (sid <= 0 || sid > script_items.ga_len)
2520 return -1;
2521 ht = &SCRIPT_VARS(sid);
2522 di = find_var_in_ht(ht, 0, name, TRUE);
2523 if (di == NULL)
2524 return -2;
2525
2526 // Now find the svar_T index in sn_var_vals.
2527 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2528 {
2529 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2530
2531 if (sv->sv_tv == &di->di_tv)
2532 {
2533 if (check_writable && sv->sv_const)
2534 semsg(_(e_readonlyvar), name);
2535 return idx;
2536 }
2537 }
2538 return -1;
2539}
2540
2541/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002542 * Find "name" in imported items of the current script or in "cctx" if not
2543 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 */
2545 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002546find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002548 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 int idx;
2550
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002551 if (current_sctx.sc_sid <= 0)
2552 return NULL;
2553 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 if (cctx != NULL)
2555 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2556 {
2557 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2558 + idx;
2559
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002560 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2561 : STRLEN(import->imp_name) == len
2562 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 return import;
2564 }
2565
2566 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2567 {
2568 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2569
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002570 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2571 : STRLEN(import->imp_name) == len
2572 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002573 return import;
2574 }
2575 return NULL;
2576}
2577
2578/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002579 * Free all imported variables.
2580 */
2581 static void
2582free_imported(cctx_T *cctx)
2583{
2584 int idx;
2585
2586 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2587 {
2588 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2589
2590 vim_free(import->imp_name);
2591 }
2592 ga_clear(&cctx->ctx_imports);
2593}
2594
2595/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002596 * Return TRUE if "p" points at a "#" but not at "#{".
2597 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002598 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002599vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002600{
2601 return p[0] == '#' && p[1] != '{';
2602}
2603
2604/*
2605 * Return a pointer to the next line that isn't empty or only contains a
2606 * comment. Skips over white space.
2607 * Returns NULL if there is none.
2608 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002609 char_u *
2610peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002611{
2612 int lnum = cctx->ctx_lnum;
2613
2614 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2615 {
2616 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002617 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002618
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002619 if (line == NULL)
2620 break;
2621 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002622 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002623 return p;
2624 }
2625 return NULL;
2626}
2627
2628/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002629 * Called when checking for a following operator at "arg". When the rest of
2630 * the line is empty or only a comment, peek the next line. If there is a next
2631 * line return a pointer to it and set "nextp".
2632 * Otherwise skip over white space.
2633 */
2634 static char_u *
2635may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2636{
2637 char_u *p = skipwhite(arg);
2638
2639 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002640 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002641 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002642 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002643 if (*nextp != NULL)
2644 return *nextp;
2645 }
2646 return p;
2647}
2648
2649/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002650 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002651 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002652 * Returns NULL when at the end.
2653 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002654 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002655next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002656{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002657 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002658
2659 do
2660 {
2661 ++cctx->ctx_lnum;
2662 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002663 {
2664 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002665 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002666 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002667 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002668 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002669 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002670 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002671 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002672 return line;
2673}
2674
2675/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002676 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002677 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002678 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2679 */
2680 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002681may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002682{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002683 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002684 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002685 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002686
2687 if (next == NULL)
2688 return FAIL;
2689 *arg = skipwhite(next);
2690 }
2691 return OK;
2692}
2693
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002694/*
2695 * Idem, and give an error when failed.
2696 */
2697 static int
2698may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2699{
2700 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2701 {
2702 emsg(_("E1097: line incomplete"));
2703 return FAIL;
2704 }
2705 return OK;
2706}
2707
2708
Bram Moolenaara5565e42020-05-09 15:44:01 +02002709// Structure passed between the compile_expr* functions to keep track of
2710// constants that have been parsed but for which no code was produced yet. If
2711// possible expressions on these constants are applied at compile time. If
2712// that is not possible, the code to push the constants needs to be generated
2713// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002714// Using 50 should be more than enough of 5 levels of ().
2715#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002716typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002717 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002718 int pp_used; // active entries in pp_tv[]
2719} ppconst_T;
2720
Bram Moolenaar1c747212020-05-09 18:28:34 +02002721static int compile_expr0(char_u **arg, cctx_T *cctx);
2722static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2723
Bram Moolenaara5565e42020-05-09 15:44:01 +02002724/*
2725 * Generate a PUSH instruction for "tv".
2726 * "tv" will be consumed or cleared.
2727 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2728 */
2729 static int
2730generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2731{
2732 if (tv != NULL)
2733 {
2734 switch (tv->v_type)
2735 {
2736 case VAR_UNKNOWN:
2737 break;
2738 case VAR_BOOL:
2739 generate_PUSHBOOL(cctx, tv->vval.v_number);
2740 break;
2741 case VAR_SPECIAL:
2742 generate_PUSHSPEC(cctx, tv->vval.v_number);
2743 break;
2744 case VAR_NUMBER:
2745 generate_PUSHNR(cctx, tv->vval.v_number);
2746 break;
2747#ifdef FEAT_FLOAT
2748 case VAR_FLOAT:
2749 generate_PUSHF(cctx, tv->vval.v_float);
2750 break;
2751#endif
2752 case VAR_BLOB:
2753 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2754 tv->vval.v_blob = NULL;
2755 break;
2756 case VAR_STRING:
2757 generate_PUSHS(cctx, tv->vval.v_string);
2758 tv->vval.v_string = NULL;
2759 break;
2760 default:
2761 iemsg("constant type not supported");
2762 clear_tv(tv);
2763 return FAIL;
2764 }
2765 tv->v_type = VAR_UNKNOWN;
2766 }
2767 return OK;
2768}
2769
2770/*
2771 * Generate code for any ppconst entries.
2772 */
2773 static int
2774generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2775{
2776 int i;
2777 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002778 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002779
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002780 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002781 for (i = 0; i < ppconst->pp_used; ++i)
2782 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2783 ret = FAIL;
2784 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002785 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002786 return ret;
2787}
2788
2789/*
2790 * Clear ppconst constants. Used when failing.
2791 */
2792 static void
2793clear_ppconst(ppconst_T *ppconst)
2794{
2795 int i;
2796
2797 for (i = 0; i < ppconst->pp_used; ++i)
2798 clear_tv(&ppconst->pp_tv[i]);
2799 ppconst->pp_used = 0;
2800}
2801
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002802/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002803 * Generate an instruction to load script-local variable "name", without the
2804 * leading "s:".
2805 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 */
2807 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002808compile_load_scriptvar(
2809 cctx_T *cctx,
2810 char_u *name, // variable NUL terminated
2811 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002812 char_u **end, // end of variable
2813 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002815 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2817 imported_T *import;
2818
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002819 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002821 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002822 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2823 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 }
2825 if (idx >= 0)
2826 {
2827 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2828
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002829 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 current_sctx.sc_sid, idx, sv->sv_type);
2831 return OK;
2832 }
2833
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002834 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 if (import != NULL)
2836 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002837 if (import->imp_all)
2838 {
2839 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002840 char_u *exp_name;
2841 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002842 ufunc_T *ufunc;
2843 type_T *type;
2844
2845 // Used "import * as Name", need to lookup the member.
2846 if (*p != '.')
2847 {
2848 semsg(_("E1060: expected dot after name: %s"), start);
2849 return FAIL;
2850 }
2851 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002852 if (VIM_ISWHITE(*p))
2853 {
2854 emsg(_("E1074: no white space allowed after dot"));
2855 return FAIL;
2856 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002857
Bram Moolenaar1c991142020-07-04 13:15:31 +02002858 // isolate one name
2859 exp_name = p;
2860 while (eval_isnamec(*p))
2861 ++p;
2862 cc = *p;
2863 *p = NUL;
2864
2865 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2866 *p = cc;
2867 p = skipwhite(p);
2868
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002869 // TODO: what if it is a function?
2870 if (idx < 0)
2871 return FAIL;
2872 *end = p;
2873
2874 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2875 import->imp_sid,
2876 idx,
2877 type);
2878 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002879 else if (import->imp_funcname != NULL)
2880 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002881 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002882 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2883 import->imp_sid,
2884 import->imp_var_vals_idx,
2885 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 return OK;
2887 }
2888
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002889 if (error)
2890 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 return FAIL;
2892}
2893
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002894 static int
2895generate_funcref(cctx_T *cctx, char_u *name)
2896{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002897 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002898
2899 if (ufunc == NULL)
2900 return FAIL;
2901
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002902 // Need to compile any default values to get the argument types.
2903 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2904 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2905 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002906 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002907}
2908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909/*
2910 * Compile a variable name into a load instruction.
2911 * "end" points to just after the name.
2912 * When "error" is FALSE do not give an error when not found.
2913 */
2914 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002915compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916{
2917 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002918 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002919 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002921 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922
2923 if (*(*arg + 1) == ':')
2924 {
2925 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002926 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002927 {
2928 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002930 switch (**arg)
2931 {
2932 case 'g': isn_type = ISN_LOADGDICT; break;
2933 case 'w': isn_type = ISN_LOADWDICT; break;
2934 case 't': isn_type = ISN_LOADTDICT; break;
2935 case 'b': isn_type = ISN_LOADBDICT; break;
2936 default:
2937 semsg(_(e_namespace), *arg);
2938 goto theend;
2939 }
2940 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2941 goto theend;
2942 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002943 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 else
2945 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002946 isntype_T isn_type = ISN_DROP;
2947
2948 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2949 if (name == NULL)
2950 return FAIL;
2951
2952 switch (**arg)
2953 {
2954 case 'v': res = generate_LOADV(cctx, name, error);
2955 break;
2956 case 's': res = compile_load_scriptvar(cctx, name,
2957 NULL, NULL, error);
2958 break;
2959 case 'g': isn_type = ISN_LOADG; break;
2960 case 'w': isn_type = ISN_LOADW; break;
2961 case 't': isn_type = ISN_LOADT; break;
2962 case 'b': isn_type = ISN_LOADB; break;
2963 default: semsg(_(e_namespace), *arg);
2964 goto theend;
2965 }
2966 if (isn_type != ISN_DROP)
2967 {
2968 // Global, Buffer-local, Window-local and Tabpage-local
2969 // variables can be defined later, thus we don't check if it
2970 // exists, give error at runtime.
2971 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2972 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 }
2974 }
2975 else
2976 {
2977 size_t len = end - *arg;
2978 int idx;
2979 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002980 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981
2982 name = vim_strnsave(*arg, end - *arg);
2983 if (name == NULL)
2984 return FAIL;
2985
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002986 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002988 if (!gen_load_outer)
2989 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 }
2991 else
2992 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002993 lvar_T *lvar = lookup_local(*arg, len, cctx);
2994
2995 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002997 type = lvar->lv_type;
2998 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002999 if (lvar->lv_from_outer)
3000 gen_load_outer = TRUE;
3001 else
3002 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 }
3004 else
3005 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003006 // "var" can be script-local even without using "s:" if it
3007 // already exists.
3008 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
3009 == SCRIPT_VERSION_VIM9
3010 || lookup_script(*arg, len) == OK)
3011 res = compile_load_scriptvar(cctx, name, *arg, &end,
3012 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003013
Bram Moolenaara5565e42020-05-09 15:44:01 +02003014 // When the name starts with an uppercase letter or "x:" it
3015 // can be a user defined function.
3016 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
3017 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 }
3019 }
3020 if (gen_load)
3021 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003022 if (gen_load_outer)
3023 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 }
3025
3026 *arg = end;
3027
3028theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003029 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 semsg(_(e_var_notfound), name);
3031 vim_free(name);
3032 return res;
3033}
3034
3035/*
3036 * Compile the argument expressions.
3037 * "arg" points to just after the "(" and is advanced to after the ")"
3038 */
3039 static int
3040compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
3041{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003042 char_u *p = *arg;
3043 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044
Bram Moolenaare6085c52020-04-12 20:19:16 +02003045 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003047 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3048 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003049 if (*p == ')')
3050 {
3051 *arg = p + 1;
3052 return OK;
3053 }
3054
Bram Moolenaara5565e42020-05-09 15:44:01 +02003055 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 return FAIL;
3057 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003058
3059 if (*p != ',' && *skipwhite(p) == ',')
3060 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02003061 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003062 p = skipwhite(p);
3063 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003065 {
3066 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003067 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02003068 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003069 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003070 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003071 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003073failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003074 emsg(_(e_missing_close));
3075 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076}
3077
3078/*
3079 * Compile a function call: name(arg1, arg2)
3080 * "arg" points to "name", "arg + varlen" to the "(".
3081 * "argcount_init" is 1 for "value->method()"
3082 * Instructions:
3083 * EVAL arg1
3084 * EVAL arg2
3085 * BCALL / DCALL / UCALL
3086 */
3087 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003088compile_call(
3089 char_u **arg,
3090 size_t varlen,
3091 cctx_T *cctx,
3092 ppconst_T *ppconst,
3093 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094{
3095 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003096 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 int argcount = argcount_init;
3098 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003099 char_u fname_buf[FLEN_FIXED + 1];
3100 char_u *tofree = NULL;
3101 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003103 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104
Bram Moolenaara5565e42020-05-09 15:44:01 +02003105 // we can evaluate "has('name')" at compile time
3106 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3107 {
3108 char_u *s = skipwhite(*arg + varlen + 1);
3109 typval_T argvars[2];
3110
3111 argvars[0].v_type = VAR_UNKNOWN;
3112 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003113 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003114 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003115 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003116 s = skipwhite(s);
3117 if (*s == ')' && argvars[0].v_type == VAR_STRING)
3118 {
3119 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3120
3121 *arg = s + 1;
3122 argvars[1].v_type = VAR_UNKNOWN;
3123 tv->v_type = VAR_NUMBER;
3124 tv->vval.v_number = 0;
3125 f_has(argvars, tv);
3126 clear_tv(&argvars[0]);
3127 ++ppconst->pp_used;
3128 return OK;
3129 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003130 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003131 }
3132
3133 if (generate_ppconst(cctx, ppconst) == FAIL)
3134 return FAIL;
3135
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 if (varlen >= sizeof(namebuf))
3137 {
3138 semsg(_("E1011: name too long: %s"), name);
3139 return FAIL;
3140 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003141 vim_strncpy(namebuf, *arg, varlen);
3142 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143
3144 *arg = skipwhite(*arg + varlen + 1);
3145 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003146 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003148 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 {
3150 int idx;
3151
3152 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003153 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02003155 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003156 else
3157 semsg(_(e_unknownfunc), namebuf);
3158 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 }
3160
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003162 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003164 {
3165 res = generate_CALL(cctx, ufunc, argcount);
3166 goto theend;
3167 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168
3169 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003170 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003172 if (STRNCMP(namebuf, "g:", 2) != 0
3173 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003174 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003175 garray_T *stack = &cctx->ctx_type_stack;
3176 type_T *type;
3177
3178 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3179 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003180 goto theend;
3181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003183 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003184 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003185 if (STRNCMP(namebuf, "g:", 2) == 0)
3186 res = generate_UCALL(cctx, name, argcount);
3187 else
3188 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003189
3190theend:
3191 vim_free(tofree);
3192 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193}
3194
3195// like NAMESPACE_CHAR but with 'a' and 'l'.
3196#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3197
3198/*
3199 * Find the end of a variable or function name. Unlike find_name_end() this
3200 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003201 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 * Return a pointer to just after the name. Equal to "arg" if there is no
3203 * valid name.
3204 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003205 static char_u *
3206to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207{
3208 char_u *p;
3209
3210 // Quick check for valid starting character.
3211 if (!eval_isnamec1(*arg))
3212 return arg;
3213
3214 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3215 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3216 // and can be used in slice "[n:]".
3217 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003218 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3220 break;
3221 return p;
3222}
3223
3224/*
3225 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003226 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 */
3228 char_u *
3229to_name_const_end(char_u *arg)
3230{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003231 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 typval_T rettv;
3233
3234 if (p == arg && *arg == '[')
3235 {
3236
3237 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003238 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 p = arg;
3240 }
3241 else if (p == arg && *arg == '#' && arg[1] == '{')
3242 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003243 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003245 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 p = arg;
3247 }
3248 else if (p == arg && *arg == '{')
3249 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003250 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003252 // Can be "{x -> ret}()".
3253 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003255 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 if (ret != OK)
3257 p = arg;
3258 }
3259
3260 return p;
3261}
3262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263/*
3264 * parse a list: [expr, expr]
3265 * "*arg" points to the '['.
3266 */
3267 static int
3268compile_list(char_u **arg, cctx_T *cctx)
3269{
3270 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003271 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 int count = 0;
3273
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003274 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003276 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003277 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003278 semsg(_(e_list_end), *arg);
3279 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003280 }
3281 if (*p == ']')
3282 {
3283 ++p;
3284 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003285 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003286 p += STRLEN(p);
3287 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003288 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003289 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 break;
3291 ++count;
3292 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003293 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003295 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3296 {
3297 semsg(_(e_white_after), ",");
3298 return FAIL;
3299 }
3300 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003301 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 p = skipwhite(p);
3303 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003304 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305
3306 generate_NEWLIST(cctx, count);
3307 return OK;
3308}
3309
3310/*
3311 * parse a lambda: {arg, arg -> expr}
3312 * "*arg" points to the '{'.
3313 */
3314 static int
3315compile_lambda(char_u **arg, cctx_T *cctx)
3316{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 typval_T rettv;
3318 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003319 evalarg_T evalarg;
3320
3321 CLEAR_FIELD(evalarg);
3322 evalarg.eval_flags = EVAL_EVALUATE;
3323 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324
3325 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003326 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003330 ++ufunc->uf_refcount;
3331 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003332 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333
3334 // The function will have one line: "return {expr}".
3335 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003336 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003338 clear_evalarg(&evalarg, NULL);
3339
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003340 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003341 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003342
3343 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 return FAIL;
3345}
3346
3347/*
3348 * Compile a lamda call: expr->{lambda}(args)
3349 * "arg" points to the "{".
3350 */
3351 static int
3352compile_lambda_call(char_u **arg, cctx_T *cctx)
3353{
3354 ufunc_T *ufunc;
3355 typval_T rettv;
3356 int argcount = 1;
3357 int ret = FAIL;
3358
3359 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003360 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 return FAIL;
3362
3363 if (**arg != '(')
3364 {
3365 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003366 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 else
3368 semsg(_(e_missing_paren), "lambda");
3369 clear_tv(&rettv);
3370 return FAIL;
3371 }
3372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 ufunc = rettv.vval.v_partial->pt_func;
3374 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003375 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003376 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003377
3378 // The function will have one line: "return {expr}".
3379 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003380 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381
3382 // compile the arguments
3383 *arg = skipwhite(*arg + 1);
3384 if (compile_arguments(arg, cctx, &argcount) == OK)
3385 // call the compiled function
3386 ret = generate_CALL(cctx, ufunc, argcount);
3387
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003388 if (ret == FAIL)
3389 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 return ret;
3391}
3392
3393/*
3394 * parse a dict: {'key': val} or #{key: val}
3395 * "*arg" points to the '{'.
3396 */
3397 static int
3398compile_dict(char_u **arg, cctx_T *cctx, int literal)
3399{
3400 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003401 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 int count = 0;
3403 dict_T *d = dict_alloc();
3404 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003405 char_u *whitep = *arg;
3406 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407
3408 if (d == NULL)
3409 return FAIL;
3410 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003411 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 {
3413 char_u *key = NULL;
3414
Bram Moolenaar23c55272020-06-21 16:58:13 +02003415 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003416 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003417 *arg = NULL;
3418 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003419 }
3420
3421 if (**arg == '}')
3422 break;
3423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 if (literal)
3425 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003426 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427
Bram Moolenaar2c330432020-04-13 14:41:35 +02003428 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 {
3430 semsg(_("E1014: Invalid key: %s"), *arg);
3431 return FAIL;
3432 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003433 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 if (generate_PUSHS(cctx, key) == FAIL)
3435 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003436 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 }
3438 else
3439 {
3440 isn_T *isn;
3441
Bram Moolenaara5565e42020-05-09 15:44:01 +02003442 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3445 if (isn->isn_type == ISN_PUSHS)
3446 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003447 else
3448 {
3449 type_T *keytype = ((type_T **)stack->ga_data)
3450 [stack->ga_len - 1];
3451 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
3452 return FAIL;
3453 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 }
3455
3456 // Check for duplicate keys, if using string keys.
3457 if (key != NULL)
3458 {
3459 item = dict_find(d, key, -1);
3460 if (item != NULL)
3461 {
3462 semsg(_(e_duplicate_key), key);
3463 goto failret;
3464 }
3465 item = dictitem_alloc(key);
3466 if (item != NULL)
3467 {
3468 item->di_tv.v_type = VAR_UNKNOWN;
3469 item->di_tv.v_lock = 0;
3470 if (dict_add(d, item) == FAIL)
3471 dictitem_free(item);
3472 }
3473 }
3474
3475 *arg = skipwhite(*arg);
3476 if (**arg != ':')
3477 {
3478 semsg(_(e_missing_dict_colon), *arg);
3479 return FAIL;
3480 }
3481
Bram Moolenaar2c330432020-04-13 14:41:35 +02003482 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003483 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003484 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003485 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003486 *arg = NULL;
3487 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003488 }
3489
Bram Moolenaara5565e42020-05-09 15:44:01 +02003490 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 return FAIL;
3492 ++count;
3493
Bram Moolenaar2c330432020-04-13 14:41:35 +02003494 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003495 *arg = skipwhite(*arg);
3496 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003497 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003498 *arg = NULL;
3499 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003500 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 if (**arg == '}')
3502 break;
3503 if (**arg != ',')
3504 {
3505 semsg(_(e_missing_dict_comma), *arg);
3506 goto failret;
3507 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003508 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 *arg = skipwhite(*arg + 1);
3510 }
3511
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 *arg = *arg + 1;
3513
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003514 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003515 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003516 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003517 *arg += STRLEN(*arg);
3518
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 dict_unref(d);
3520 return generate_NEWDICT(cctx, count);
3521
3522failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003523 if (*arg == NULL)
3524 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 dict_unref(d);
3526 return FAIL;
3527}
3528
3529/*
3530 * Compile "&option".
3531 */
3532 static int
3533compile_get_option(char_u **arg, cctx_T *cctx)
3534{
3535 typval_T rettv;
3536 char_u *start = *arg;
3537 int ret;
3538
3539 // parse the option and get the current value to get the type.
3540 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003541 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 if (ret == OK)
3543 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003544 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 char_u *name = vim_strnsave(start, *arg - start);
3546 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3547
3548 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3549 vim_free(name);
3550 }
3551 clear_tv(&rettv);
3552
3553 return ret;
3554}
3555
3556/*
3557 * Compile "$VAR".
3558 */
3559 static int
3560compile_get_env(char_u **arg, cctx_T *cctx)
3561{
3562 char_u *start = *arg;
3563 int len;
3564 int ret;
3565 char_u *name;
3566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 ++*arg;
3568 len = get_env_len(arg);
3569 if (len == 0)
3570 {
3571 semsg(_(e_syntax_at), start - 1);
3572 return FAIL;
3573 }
3574
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003575 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 name = vim_strnsave(start, len + 1);
3577 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3578 vim_free(name);
3579 return ret;
3580}
3581
3582/*
3583 * Compile "@r".
3584 */
3585 static int
3586compile_get_register(char_u **arg, cctx_T *cctx)
3587{
3588 int ret;
3589
3590 ++*arg;
3591 if (**arg == NUL)
3592 {
3593 semsg(_(e_syntax_at), *arg - 1);
3594 return FAIL;
3595 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003596 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 {
3598 emsg_invreg(**arg);
3599 return FAIL;
3600 }
3601 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3602 ++*arg;
3603 return ret;
3604}
3605
3606/*
3607 * Apply leading '!', '-' and '+' to constant "rettv".
3608 */
3609 static int
3610apply_leader(typval_T *rettv, char_u *start, char_u *end)
3611{
3612 char_u *p = end;
3613
3614 // this works from end to start
3615 while (p > start)
3616 {
3617 --p;
3618 if (*p == '-' || *p == '+')
3619 {
3620 // only '-' has an effect, for '+' we only check the type
3621#ifdef FEAT_FLOAT
3622 if (rettv->v_type == VAR_FLOAT)
3623 {
3624 if (*p == '-')
3625 rettv->vval.v_float = -rettv->vval.v_float;
3626 }
3627 else
3628#endif
3629 {
3630 varnumber_T val;
3631 int error = FALSE;
3632
3633 // tv_get_number_chk() accepts a string, but we don't want that
3634 // here
3635 if (check_not_string(rettv) == FAIL)
3636 return FAIL;
3637 val = tv_get_number_chk(rettv, &error);
3638 clear_tv(rettv);
3639 if (error)
3640 return FAIL;
3641 if (*p == '-')
3642 val = -val;
3643 rettv->v_type = VAR_NUMBER;
3644 rettv->vval.v_number = val;
3645 }
3646 }
3647 else
3648 {
3649 int v = tv2bool(rettv);
3650
3651 // '!' is permissive in the type.
3652 clear_tv(rettv);
3653 rettv->v_type = VAR_BOOL;
3654 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3655 }
3656 }
3657 return OK;
3658}
3659
3660/*
3661 * Recognize v: variables that are constants and set "rettv".
3662 */
3663 static void
3664get_vim_constant(char_u **arg, typval_T *rettv)
3665{
3666 if (STRNCMP(*arg, "v:true", 6) == 0)
3667 {
3668 rettv->v_type = VAR_BOOL;
3669 rettv->vval.v_number = VVAL_TRUE;
3670 *arg += 6;
3671 }
3672 else if (STRNCMP(*arg, "v:false", 7) == 0)
3673 {
3674 rettv->v_type = VAR_BOOL;
3675 rettv->vval.v_number = VVAL_FALSE;
3676 *arg += 7;
3677 }
3678 else if (STRNCMP(*arg, "v:null", 6) == 0)
3679 {
3680 rettv->v_type = VAR_SPECIAL;
3681 rettv->vval.v_number = VVAL_NULL;
3682 *arg += 6;
3683 }
3684 else if (STRNCMP(*arg, "v:none", 6) == 0)
3685 {
3686 rettv->v_type = VAR_SPECIAL;
3687 rettv->vval.v_number = VVAL_NONE;
3688 *arg += 6;
3689 }
3690}
3691
Bram Moolenaar696ba232020-07-29 21:20:41 +02003692 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003693get_compare_type(char_u *p, int *len, int *type_is)
3694{
3695 exptype_T type = EXPR_UNKNOWN;
3696 int i;
3697
3698 switch (p[0])
3699 {
3700 case '=': if (p[1] == '=')
3701 type = EXPR_EQUAL;
3702 else if (p[1] == '~')
3703 type = EXPR_MATCH;
3704 break;
3705 case '!': if (p[1] == '=')
3706 type = EXPR_NEQUAL;
3707 else if (p[1] == '~')
3708 type = EXPR_NOMATCH;
3709 break;
3710 case '>': if (p[1] != '=')
3711 {
3712 type = EXPR_GREATER;
3713 *len = 1;
3714 }
3715 else
3716 type = EXPR_GEQUAL;
3717 break;
3718 case '<': if (p[1] != '=')
3719 {
3720 type = EXPR_SMALLER;
3721 *len = 1;
3722 }
3723 else
3724 type = EXPR_SEQUAL;
3725 break;
3726 case 'i': if (p[1] == 's')
3727 {
3728 // "is" and "isnot"; but not a prefix of a name
3729 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3730 *len = 5;
3731 i = p[*len];
3732 if (!isalnum(i) && i != '_')
3733 {
3734 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3735 *type_is = TRUE;
3736 }
3737 }
3738 break;
3739 }
3740 return type;
3741}
3742
3743/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 * Compile code to apply '-', '+' and '!'.
3745 */
3746 static int
3747compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3748{
3749 char_u *p = end;
3750
3751 // this works from end to start
3752 while (p > start)
3753 {
3754 --p;
3755 if (*p == '-' || *p == '+')
3756 {
3757 int negate = *p == '-';
3758 isn_T *isn;
3759
3760 // TODO: check type
3761 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3762 {
3763 --p;
3764 if (*p == '-')
3765 negate = !negate;
3766 }
3767 // only '-' has an effect, for '+' we only check the type
3768 if (negate)
3769 isn = generate_instr(cctx, ISN_NEGATENR);
3770 else
3771 isn = generate_instr(cctx, ISN_CHECKNR);
3772 if (isn == NULL)
3773 return FAIL;
3774 }
3775 else
3776 {
3777 int invert = TRUE;
3778
3779 while (p > start && p[-1] == '!')
3780 {
3781 --p;
3782 invert = !invert;
3783 }
3784 if (generate_2BOOL(cctx, invert) == FAIL)
3785 return FAIL;
3786 }
3787 }
3788 return OK;
3789}
3790
3791/*
3792 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003793 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 */
3795 static int
3796compile_subscript(
3797 char_u **arg,
3798 cctx_T *cctx,
3799 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003800 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003801 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802{
3803 for (;;)
3804 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003805 char_u *p = skipwhite(*arg);
3806
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003807 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003808 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003809 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003810
3811 // If a following line starts with "->{" or "->X" advance to that
3812 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003813 // Also if a following line starts with ".x".
3814 if (next != NULL &&
3815 ((next[0] == '-' && next[1] == '>'
3816 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003817 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003818 {
3819 next = next_line_from_context(cctx, TRUE);
3820 if (next == NULL)
3821 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003822 *arg = next;
3823 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003824 }
3825 }
3826
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003827 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3828 // not a function call.
3829 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003831 garray_T *stack = &cctx->ctx_type_stack;
3832 type_T *type;
3833 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003835 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003836 return FAIL;
3837
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003839 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3840
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003841 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3843 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003844 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 return FAIL;
3846 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003847 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003849 char_u *pstart = p;
3850
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003851 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003852 return FAIL;
3853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 // something->method()
3855 // Apply the '!', '-' and '+' first:
3856 // -1.0->func() works like (-1.0)->func()
3857 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3858 return FAIL;
3859 *start_leader = end_leader; // don't apply again later
3860
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003861 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003862 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003863 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 if (**arg == '{')
3865 {
3866 // lambda call: list->{lambda}
3867 if (compile_lambda_call(arg, cctx) == FAIL)
3868 return FAIL;
3869 }
3870 else
3871 {
3872 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003873 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003874 if (!eval_isnamec1(*p))
3875 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003876 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003877 return FAIL;
3878 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003879 if (ASCII_ISALPHA(*p) && p[1] == ':')
3880 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003881 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 ;
3883 if (*p != '(')
3884 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003885 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 return FAIL;
3887 }
3888 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003889 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 return FAIL;
3891 }
3892 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003893 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003895 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003896 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003897 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003898
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003899 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003900 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003901 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003902 // TODO: blob index
3903 // TODO: more arguments
3904 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003905 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003906 return FAIL;
3907
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003908 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003909 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003910 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003911 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003912 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 return FAIL;
3914
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003915 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3916 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 if (**arg != ']')
3918 {
3919 emsg(_(e_missbrac));
3920 return FAIL;
3921 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003922 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003924 // We can index a list and a dict. If we don't know the type
3925 // we can use the index value type.
3926 // TODO: If we don't know use an instruction to figure it out at
3927 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003928 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003929 vtype = (*typep)->tt_type;
3930 if (*typep == &t_any)
3931 {
3932 type_T *valtype = ((type_T **)stack->ga_data)
3933 [stack->ga_len - 1];
3934 if (valtype == &t_string)
3935 vtype = VAR_DICT;
3936 }
3937 if (vtype == VAR_DICT)
3938 {
3939 if ((*typep)->tt_type == VAR_DICT)
3940 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003941 else
3942 {
3943 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3944 return FAIL;
3945 *typep = &t_any;
3946 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003947 if (may_generate_2STRING(-1, cctx) == FAIL)
3948 return FAIL;
3949 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3950 return FAIL;
3951 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003952 else if (vtype == VAR_STRING)
3953 {
3954 *typep = &t_number;
3955 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3956 return FAIL;
3957 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003958 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003959 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003960 if ((*typep)->tt_type == VAR_LIST)
3961 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003962 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003963 return FAIL;
3964 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003965 else
3966 {
3967 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003968 return FAIL;
3969 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003971 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003973 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003974 return FAIL;
3975
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003976 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003977 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3978 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003980 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003981 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 while (eval_isnamec(*p))
3983 MB_PTR_ADV(p);
3984 if (p == *arg)
3985 {
3986 semsg(_(e_syntax_at), *arg);
3987 return FAIL;
3988 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003989 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 return FAIL;
3991 *arg = p;
3992 }
3993 else
3994 break;
3995 }
3996
3997 // TODO - see handle_subscript():
3998 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3999 // Don't do this when "Func" is already a partial that was bound
4000 // explicitly (pt_auto is FALSE).
4001
4002 return OK;
4003}
4004
4005/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004006 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4007 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004009 * If the value is a constant "ppconst->pp_ret" will be set.
4010 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004011 *
4012 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 */
4014
4015/*
4016 * number number constant
4017 * 0zFFFFFFFF Blob constant
4018 * "string" string constant
4019 * 'string' literal string constant
4020 * &option-name option value
4021 * @r register contents
4022 * identifier variable value
4023 * function() function call
4024 * $VAR environment variable
4025 * (expression) nested expression
4026 * [expr, expr] List
4027 * {key: val, key: val} Dictionary
4028 * #{key: val, key: val} Dictionary with literal keys
4029 *
4030 * Also handle:
4031 * ! in front logical NOT
4032 * - in front unary minus
4033 * + in front unary plus (ignored)
4034 * trailing (arg) funcref/partial call
4035 * trailing [] subscript in String or List
4036 * trailing .name entry in Dictionary
4037 * trailing ->name() method call
4038 */
4039 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004040compile_expr7(
4041 char_u **arg,
4042 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004043 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 char_u *start_leader, *end_leader;
4046 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004047 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004048 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049
4050 /*
4051 * Skip '!', '-' and '+' characters. They are handled later.
4052 */
4053 start_leader = *arg;
4054 while (**arg == '!' || **arg == '-' || **arg == '+')
4055 *arg = skipwhite(*arg + 1);
4056 end_leader = *arg;
4057
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004058 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 switch (**arg)
4060 {
4061 /*
4062 * Number constant.
4063 */
4064 case '0': // also for blob starting with 0z
4065 case '1':
4066 case '2':
4067 case '3':
4068 case '4':
4069 case '5':
4070 case '6':
4071 case '7':
4072 case '8':
4073 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004074 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 return FAIL;
4076 break;
4077
4078 /*
4079 * String constant: "string".
4080 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004081 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 return FAIL;
4083 break;
4084
4085 /*
4086 * Literal string constant: 'str''ing'.
4087 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004088 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 return FAIL;
4090 break;
4091
4092 /*
4093 * Constant Vim variable.
4094 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004095 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 ret = NOTDONE;
4097 break;
4098
4099 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004100 * "true" constant
4101 */
4102 case 't': if (STRNCMP(*arg, "true", 4) == 0
4103 && !eval_isnamec((*arg)[4]))
4104 {
4105 *arg += 4;
4106 rettv->v_type = VAR_BOOL;
4107 rettv->vval.v_number = VVAL_TRUE;
4108 }
4109 else
4110 ret = NOTDONE;
4111 break;
4112
4113 /*
4114 * "false" constant
4115 */
4116 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4117 && !eval_isnamec((*arg)[5]))
4118 {
4119 *arg += 5;
4120 rettv->v_type = VAR_BOOL;
4121 rettv->vval.v_number = VVAL_FALSE;
4122 }
4123 else
4124 ret = NOTDONE;
4125 break;
4126
4127 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 * List: [expr, expr]
4129 */
4130 case '[': ret = compile_list(arg, cctx);
4131 break;
4132
4133 /*
4134 * Dictionary: #{key: val, key: val}
4135 */
4136 case '#': if ((*arg)[1] == '{')
4137 {
4138 ++*arg;
4139 ret = compile_dict(arg, cctx, TRUE);
4140 }
4141 else
4142 ret = NOTDONE;
4143 break;
4144
4145 /*
4146 * Lambda: {arg, arg -> expr}
4147 * Dictionary: {'key': val, 'key': val}
4148 */
4149 case '{': {
4150 char_u *start = skipwhite(*arg + 1);
4151
4152 // Find out what comes after the arguments.
4153 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004154 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 if (ret != FAIL && *start == '>')
4156 ret = compile_lambda(arg, cctx);
4157 else
4158 ret = compile_dict(arg, cctx, FALSE);
4159 }
4160 break;
4161
4162 /*
4163 * Option value: &name
4164 */
4165 case '&': ret = compile_get_option(arg, cctx);
4166 break;
4167
4168 /*
4169 * Environment variable: $VAR.
4170 */
4171 case '$': ret = compile_get_env(arg, cctx);
4172 break;
4173
4174 /*
4175 * Register contents: @r.
4176 */
4177 case '@': ret = compile_get_register(arg, cctx);
4178 break;
4179 /*
4180 * nested expression: (expression).
4181 */
4182 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02004183
4184 // recursive!
4185 if (ppconst->pp_used <= PPSIZE - 10)
4186 {
4187 ret = compile_expr1(arg, cctx, ppconst);
4188 }
4189 else
4190 {
4191 // Not enough space in ppconst, flush constants.
4192 if (generate_ppconst(cctx, ppconst) == FAIL)
4193 return FAIL;
4194 ret = compile_expr0(arg, cctx);
4195 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 *arg = skipwhite(*arg);
4197 if (**arg == ')')
4198 ++*arg;
4199 else if (ret == OK)
4200 {
4201 emsg(_(e_missing_close));
4202 ret = FAIL;
4203 }
4204 break;
4205
4206 default: ret = NOTDONE;
4207 break;
4208 }
4209 if (ret == FAIL)
4210 return FAIL;
4211
Bram Moolenaar1c747212020-05-09 18:28:34 +02004212 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 {
4214 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004215 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004217 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 return FAIL;
4219 }
4220 start_leader = end_leader; // don't apply again below
4221
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004222 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004223 clear_tv(rettv);
4224 else
4225 // A constant expression can possibly be handled compile time,
4226 // return the value instead of generating code.
4227 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 }
4229 else if (ret == NOTDONE)
4230 {
4231 char_u *p;
4232 int r;
4233
4234 if (!eval_isnamec1(**arg))
4235 {
4236 semsg(_("E1015: Name expected: %s"), *arg);
4237 return FAIL;
4238 }
4239
4240 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004241 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004243 {
4244 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004247 {
4248 if (generate_ppconst(cctx, ppconst) == FAIL)
4249 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004251 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 if (r == FAIL)
4253 return FAIL;
4254 }
4255
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004256 // Handle following "[]", ".member", etc.
4257 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004258 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004259 ppconst) == FAIL)
4260 return FAIL;
4261 if (ppconst->pp_used > 0)
4262 {
4263 // apply the '!', '-' and '+' before the constant
4264 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4265 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4266 return FAIL;
4267 return OK;
4268 }
4269 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004271 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272}
4273
4274/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004275 * Give the "white on both sides" error, taking the operator from "p[len]".
4276 */
4277 void
4278error_white_both(char_u *op, int len)
4279{
4280 char_u buf[10];
4281
4282 vim_strncpy(buf, op, len);
4283 semsg(_(e_white_both), buf);
4284}
4285
4286/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 * * number multiplication
4288 * / number division
4289 * % number modulo
4290 */
4291 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004292compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293{
4294 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004295 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004296 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004298 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004299 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 return FAIL;
4301
4302 /*
4303 * Repeat computing, until no "*", "/" or "%" is following.
4304 */
4305 for (;;)
4306 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004307 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 if (*op != '*' && *op != '/' && *op != '%')
4309 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004310 if (next != NULL)
4311 {
4312 *arg = next_line_from_context(cctx, TRUE);
4313 op = skipwhite(*arg);
4314 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004315
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004316 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004318 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004319 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 }
4321 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004322 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004323 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004325 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004326 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004328
4329 if (ppconst->pp_used == ppconst_used + 2
4330 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4331 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004332 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004333 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4334 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004335 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004337 // both are numbers: compute the result
4338 switch (*op)
4339 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004340 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004341 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004342 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004343 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004344 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004345 break;
4346 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004347 tv1->vval.v_number = res;
4348 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004349 }
4350 else
4351 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004352 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004353 generate_two_op(cctx, op);
4354 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 }
4356
4357 return OK;
4358}
4359
4360/*
4361 * + number addition
4362 * - number subtraction
4363 * .. string concatenation
4364 */
4365 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004366compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367{
4368 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004369 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004371 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372
4373 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004374 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 return FAIL;
4376
4377 /*
4378 * Repeat computing, until no "+", "-" or ".." is following.
4379 */
4380 for (;;)
4381 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004382 op = may_peek_next_line(cctx, *arg, &next);
4383 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 break;
4385 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004386 if (next != NULL)
4387 {
4388 *arg = next_line_from_context(cctx, TRUE);
4389 op = skipwhite(*arg);
4390 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004392 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004394 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004395 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 }
4397
4398 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004399 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004400 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004402 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004403 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 return FAIL;
4405
Bram Moolenaara5565e42020-05-09 15:44:01 +02004406 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004407 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004408 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4409 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4410 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4411 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004413 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4414 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004415
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004416 // concat/subtract/add constant numbers
4417 if (*op == '+')
4418 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4419 else if (*op == '-')
4420 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4421 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004422 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004423 // concatenate constant strings
4424 char_u *s1 = tv1->vval.v_string;
4425 char_u *s2 = tv2->vval.v_string;
4426 size_t len1 = STRLEN(s1);
4427
4428 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4429 if (tv1->vval.v_string == NULL)
4430 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004431 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004432 return FAIL;
4433 }
4434 mch_memmove(tv1->vval.v_string, s1, len1);
4435 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004436 vim_free(s1);
4437 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004438 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004439 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 }
4441 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004442 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004443 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004444 if (*op == '.')
4445 {
4446 if (may_generate_2STRING(-2, cctx) == FAIL
4447 || may_generate_2STRING(-1, cctx) == FAIL)
4448 return FAIL;
4449 generate_instr_drop(cctx, ISN_CONCAT, 1);
4450 }
4451 else
4452 generate_two_op(cctx, op);
4453 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454 }
4455
4456 return OK;
4457}
4458
4459/*
4460 * expr5a == expr5b
4461 * expr5a =~ expr5b
4462 * expr5a != expr5b
4463 * expr5a !~ expr5b
4464 * expr5a > expr5b
4465 * expr5a >= expr5b
4466 * expr5a < expr5b
4467 * expr5a <= expr5b
4468 * expr5a is expr5b
4469 * expr5a isnot expr5b
4470 *
4471 * Produces instructions:
4472 * EVAL expr5a Push result of "expr5a"
4473 * EVAL expr5b Push result of "expr5b"
4474 * COMPARE one of the compare instructions
4475 */
4476 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004477compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478{
4479 exptype_T type = EXPR_UNKNOWN;
4480 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004481 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004483 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004484 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485
4486 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004487 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 return FAIL;
4489
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004490 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004491 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492
4493 /*
4494 * If there is a comparative operator, use it.
4495 */
4496 if (type != EXPR_UNKNOWN)
4497 {
4498 int ic = FALSE; // Default: do not ignore case
4499
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004500 if (next != NULL)
4501 {
4502 *arg = next_line_from_context(cctx, TRUE);
4503 p = skipwhite(*arg);
4504 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505 if (type_is && (p[len] == '?' || p[len] == '#'))
4506 {
4507 semsg(_(e_invexpr2), *arg);
4508 return FAIL;
4509 }
4510 // extra question mark appended: ignore case
4511 if (p[len] == '?')
4512 {
4513 ic = TRUE;
4514 ++len;
4515 }
4516 // extra '#' appended: match case (ignored)
4517 else if (p[len] == '#')
4518 ++len;
4519 // nothing appended: match case
4520
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004521 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004523 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004524 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 }
4526
4527 // get the second variable
4528 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004529 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004530 return FAIL;
4531
Bram Moolenaara5565e42020-05-09 15:44:01 +02004532 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 return FAIL;
4534
Bram Moolenaara5565e42020-05-09 15:44:01 +02004535 if (ppconst->pp_used == ppconst_used + 2)
4536 {
4537 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4538 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4539 int ret;
4540
4541 // Both sides are a constant, compute the result now.
4542 // First check for a valid combination of types, this is more
4543 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004544 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004545 ret = FAIL;
4546 else
4547 {
4548 ret = typval_compare(tv1, tv2, type, ic);
4549 tv1->v_type = VAR_BOOL;
4550 tv1->vval.v_number = tv1->vval.v_number
4551 ? VVAL_TRUE : VVAL_FALSE;
4552 clear_tv(tv2);
4553 --ppconst->pp_used;
4554 }
4555 return ret;
4556 }
4557
4558 generate_ppconst(cctx, ppconst);
4559 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560 }
4561
4562 return OK;
4563}
4564
Bram Moolenaar7f141552020-05-09 17:35:53 +02004565static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567/*
4568 * Compile || or &&.
4569 */
4570 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004571compile_and_or(
4572 char_u **arg,
4573 cctx_T *cctx,
4574 char *op,
4575 ppconst_T *ppconst,
4576 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004578 char_u *next;
4579 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 int opchar = *op;
4581
4582 if (p[0] == opchar && p[1] == opchar)
4583 {
4584 garray_T *instr = &cctx->ctx_instr;
4585 garray_T end_ga;
4586
4587 /*
4588 * Repeat until there is no following "||" or "&&"
4589 */
4590 ga_init2(&end_ga, sizeof(int), 10);
4591 while (p[0] == opchar && p[1] == opchar)
4592 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004593 if (next != NULL)
4594 {
4595 *arg = next_line_from_context(cctx, TRUE);
4596 p = skipwhite(*arg);
4597 }
4598
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004599 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4600 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004602 return FAIL;
4603 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604
Bram Moolenaara5565e42020-05-09 15:44:01 +02004605 // TODO: use ppconst if the value is a constant
4606 generate_ppconst(cctx, ppconst);
4607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 if (ga_grow(&end_ga, 1) == FAIL)
4609 {
4610 ga_clear(&end_ga);
4611 return FAIL;
4612 }
4613 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4614 ++end_ga.ga_len;
4615 generate_JUMP(cctx, opchar == '|'
4616 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4617
4618 // eval the next expression
4619 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004620 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004621 return FAIL;
4622
Bram Moolenaara5565e42020-05-09 15:44:01 +02004623 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4624 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625 {
4626 ga_clear(&end_ga);
4627 return FAIL;
4628 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004629
4630 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004632 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633
4634 // Fill in the end label in all jumps.
4635 while (end_ga.ga_len > 0)
4636 {
4637 isn_T *isn;
4638
4639 --end_ga.ga_len;
4640 isn = ((isn_T *)instr->ga_data)
4641 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4642 isn->isn_arg.jump.jump_where = instr->ga_len;
4643 }
4644 ga_clear(&end_ga);
4645 }
4646
4647 return OK;
4648}
4649
4650/*
4651 * expr4a && expr4a && expr4a logical AND
4652 *
4653 * Produces instructions:
4654 * EVAL expr4a Push result of "expr4a"
4655 * JUMP_AND_KEEP_IF_FALSE end
4656 * EVAL expr4b Push result of "expr4b"
4657 * JUMP_AND_KEEP_IF_FALSE end
4658 * EVAL expr4c Push result of "expr4c"
4659 * end:
4660 */
4661 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004662compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004664 int ppconst_used = ppconst->pp_used;
4665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004667 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 return FAIL;
4669
4670 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004671 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672}
4673
4674/*
4675 * expr3a || expr3b || expr3c logical OR
4676 *
4677 * Produces instructions:
4678 * EVAL expr3a Push result of "expr3a"
4679 * JUMP_AND_KEEP_IF_TRUE end
4680 * EVAL expr3b Push result of "expr3b"
4681 * JUMP_AND_KEEP_IF_TRUE end
4682 * EVAL expr3c Push result of "expr3c"
4683 * end:
4684 */
4685 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004686compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004688 int ppconst_used = ppconst->pp_used;
4689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004691 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 return FAIL;
4693
4694 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004695 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696}
4697
4698/*
4699 * Toplevel expression: expr2 ? expr1a : expr1b
4700 *
4701 * Produces instructions:
4702 * EVAL expr2 Push result of "expr"
4703 * JUMP_IF_FALSE alt jump if false
4704 * EVAL expr1a
4705 * JUMP_ALWAYS end
4706 * alt: EVAL expr1b
4707 * end:
4708 */
4709 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004710compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004711{
4712 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004713 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004714 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715
Bram Moolenaar61a89812020-05-07 16:58:17 +02004716 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004717 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718 return FAIL;
4719
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004720 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 if (*p == '?')
4722 {
4723 garray_T *instr = &cctx->ctx_instr;
4724 garray_T *stack = &cctx->ctx_type_stack;
4725 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004726 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004728 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004730 int has_const_expr = FALSE;
4731 int const_value = FALSE;
4732 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004733
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004734 if (next != NULL)
4735 {
4736 *arg = next_line_from_context(cctx, TRUE);
4737 p = skipwhite(*arg);
4738 }
4739
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004740 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4741 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004743 return FAIL;
4744 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745
Bram Moolenaara5565e42020-05-09 15:44:01 +02004746 if (ppconst->pp_used == ppconst_used + 1)
4747 {
4748 // the condition is a constant, we know whether the ? or the :
4749 // expression is to be evaluated.
4750 has_const_expr = TRUE;
4751 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4752 clear_tv(&ppconst->pp_tv[ppconst_used]);
4753 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004754 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4755 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004756 }
4757 else
4758 {
4759 generate_ppconst(cctx, ppconst);
4760 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4761 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762
4763 // evaluate the second expression; any type is accepted
4764 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004765 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004766 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004767 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004768 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769
Bram Moolenaara5565e42020-05-09 15:44:01 +02004770 if (!has_const_expr)
4771 {
4772 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773
Bram Moolenaara5565e42020-05-09 15:44:01 +02004774 // remember the type and drop it
4775 --stack->ga_len;
4776 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777
Bram Moolenaara5565e42020-05-09 15:44:01 +02004778 end_idx = instr->ga_len;
4779 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4780
4781 // jump here from JUMP_IF_FALSE
4782 isn = ((isn_T *)instr->ga_data) + alt_idx;
4783 isn->isn_arg.jump.jump_where = instr->ga_len;
4784 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785
4786 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004787 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788 if (*p != ':')
4789 {
4790 emsg(_(e_missing_colon));
4791 return FAIL;
4792 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004793 if (next != NULL)
4794 {
4795 *arg = next_line_from_context(cctx, TRUE);
4796 p = skipwhite(*arg);
4797 }
4798
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004799 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4800 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004802 return FAIL;
4803 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004804
4805 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004806 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004807 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4808 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004810 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004811 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004812 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004813 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814
Bram Moolenaara5565e42020-05-09 15:44:01 +02004815 if (!has_const_expr)
4816 {
4817 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004818
Bram Moolenaara5565e42020-05-09 15:44:01 +02004819 // If the types differ, the result has a more generic type.
4820 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4821 common_type(type1, type2, &type2, cctx->ctx_type_list);
4822
4823 // jump here from JUMP_ALWAYS
4824 isn = ((isn_T *)instr->ga_data) + end_idx;
4825 isn->isn_arg.jump.jump_where = instr->ga_len;
4826 }
4827
4828 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 }
4830 return OK;
4831}
4832
4833/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004834 * Toplevel expression.
4835 */
4836 static int
4837compile_expr0(char_u **arg, cctx_T *cctx)
4838{
4839 ppconst_T ppconst;
4840
4841 CLEAR_FIELD(ppconst);
4842 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4843 {
4844 clear_ppconst(&ppconst);
4845 return FAIL;
4846 }
4847 if (generate_ppconst(cctx, &ppconst) == FAIL)
4848 return FAIL;
4849 return OK;
4850}
4851
4852/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 * compile "return [expr]"
4854 */
4855 static char_u *
4856compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4857{
4858 char_u *p = arg;
4859 garray_T *stack = &cctx->ctx_type_stack;
4860 type_T *stack_type;
4861
4862 if (*p != NUL && *p != '|' && *p != '\n')
4863 {
4864 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004865 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004866 return NULL;
4867
4868 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4869 if (set_return_type)
4870 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004871 else
4872 {
4873 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4874 && stack_type->tt_type != VAR_VOID
4875 && stack_type->tt_type != VAR_UNKNOWN)
4876 {
4877 emsg(_("E1096: Returning a value in a function without a return type"));
4878 return NULL;
4879 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004880 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4881 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004882 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004883 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884 }
4885 else
4886 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004887 // "set_return_type" cannot be TRUE, only used for a lambda which
4888 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004889 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4890 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891 {
4892 emsg(_("E1003: Missing return value"));
4893 return NULL;
4894 }
4895
4896 // No argument, return zero.
4897 generate_PUSHNR(cctx, 0);
4898 }
4899
4900 if (generate_instr(cctx, ISN_RETURN) == NULL)
4901 return NULL;
4902
4903 // "return val | endif" is possible
4904 return skipwhite(p);
4905}
4906
4907/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004908 * Get a line from the compilation context, compatible with exarg_T getline().
4909 * Return a pointer to the line in allocated memory.
4910 * Return NULL for end-of-file or some error.
4911 */
4912 static char_u *
4913exarg_getline(
4914 int c UNUSED,
4915 void *cookie,
4916 int indent UNUSED,
4917 int do_concat UNUSED)
4918{
4919 cctx_T *cctx = (cctx_T *)cookie;
4920
4921 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4922 {
4923 iemsg("Heredoc got to end");
4924 return NULL;
4925 }
4926 ++cctx->ctx_lnum;
4927 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4928 [cctx->ctx_lnum]);
4929}
4930
4931/*
4932 * Compile a nested :def command.
4933 */
4934 static char_u *
4935compile_nested_function(exarg_T *eap, cctx_T *cctx)
4936{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004937 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004938 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004939 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004940 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004941 lvar_T *lvar;
4942 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004943 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004944
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004945 // Only g:Func() can use a namespace.
4946 if (name_start[1] == ':' && !is_global)
4947 {
4948 semsg(_(e_namespace), name_start);
4949 return NULL;
4950 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004951 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4952 return NULL;
4953
Bram Moolenaar04b12692020-05-04 23:24:44 +02004954 eap->arg = name_end;
4955 eap->getline = exarg_getline;
4956 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004957 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004958 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004959 lambda_name = get_lambda_name();
4960 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004961
Bram Moolenaar822ba242020-05-24 23:00:18 +02004962 if (ufunc == NULL)
4963 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004964 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004965 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004966 return NULL;
4967
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004968 if (is_global)
4969 {
4970 char_u *func_name = vim_strnsave(name_start + 2,
4971 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004972
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004973 if (func_name == NULL)
4974 r = FAIL;
4975 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004976 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004977 }
4978 else
4979 {
4980 // Define a local variable for the function reference.
4981 lvar = reserve_local(cctx, name_start, name_end - name_start,
4982 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004983 if (lvar == NULL)
4984 return NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004985 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL)
4986 return NULL;
4987 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4988 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004989
Bram Moolenaar61a89812020-05-07 16:58:17 +02004990 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004991 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004992}
4993
4994/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995 * Return the length of an assignment operator, or zero if there isn't one.
4996 */
4997 int
4998assignment_len(char_u *p, int *heredoc)
4999{
5000 if (*p == '=')
5001 {
5002 if (p[1] == '<' && p[2] == '<')
5003 {
5004 *heredoc = TRUE;
5005 return 3;
5006 }
5007 return 1;
5008 }
5009 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5010 return 2;
5011 if (STRNCMP(p, "..=", 3) == 0)
5012 return 3;
5013 return 0;
5014}
5015
5016// words that cannot be used as a variable
5017static char *reserved[] = {
5018 "true",
5019 "false",
5020 NULL
5021};
5022
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005023typedef enum {
5024 dest_local,
5025 dest_option,
5026 dest_env,
5027 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005028 dest_buffer,
5029 dest_window,
5030 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005031 dest_vimvar,
5032 dest_script,
5033 dest_reg,
5034} assign_dest_T;
5035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005037 * Generate the load instruction for "name".
5038 */
5039 static void
5040generate_loadvar(
5041 cctx_T *cctx,
5042 assign_dest_T dest,
5043 char_u *name,
5044 lvar_T *lvar,
5045 type_T *type)
5046{
5047 switch (dest)
5048 {
5049 case dest_option:
5050 // TODO: check the option exists
5051 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5052 break;
5053 case dest_global:
5054 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5055 break;
5056 case dest_buffer:
5057 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5058 break;
5059 case dest_window:
5060 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5061 break;
5062 case dest_tab:
5063 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5064 break;
5065 case dest_script:
5066 compile_load_scriptvar(cctx,
5067 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5068 break;
5069 case dest_env:
5070 // Include $ in the name here
5071 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5072 break;
5073 case dest_reg:
5074 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5075 break;
5076 case dest_vimvar:
5077 generate_LOADV(cctx, name + 2, TRUE);
5078 break;
5079 case dest_local:
5080 if (lvar->lv_from_outer)
5081 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5082 NULL, type);
5083 else
5084 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5085 break;
5086 }
5087}
5088
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005089 void
5090vim9_declare_error(char_u *name)
5091{
5092 char *scope = "";
5093
5094 switch (*name)
5095 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005096 case 'g': scope = _("global"); break;
5097 case 'b': scope = _("buffer"); break;
5098 case 'w': scope = _("window"); break;
5099 case 't': scope = _("tab"); break;
5100 case 'v': scope = "v:"; break;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005101 case '$': semsg(_(e_declare_env_var), name);
5102 return;
5103 case '&': semsg(_("E1052: Cannot declare an option: %s"), name);
5104 return;
5105 case '@': semsg(_("E1066: Cannot declare a register: %s"), name);
5106 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005107 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005108 }
5109 semsg(_(e_declare_var), scope, name);
5110}
5111
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005112/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005113 * Compile declaration and assignment:
5114 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005115 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005116 * Return NULL for an error.
5117 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005118 */
5119 static char_u *
5120compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5121{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005122 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005123 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005124 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005125 char_u *ret = NULL;
5126 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005127 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005128 int scriptvar_sid = 0;
5129 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005132 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005133 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005134 int oplen = 0;
5135 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005136 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005137 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005138 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005140 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005141
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005142 // Skip over the "var" or "[var, var]" to get to any "=".
5143 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5144 if (p == NULL)
5145 return *arg == '[' ? arg : NULL;
5146
5147 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005149 // TODO: should we allow this, and figure out type inference from list
5150 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005151 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005152 return NULL;
5153 }
5154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005155 sp = p;
5156 p = skipwhite(p);
5157 op = p;
5158 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005159
5160 if (var_count > 0 && oplen == 0)
5161 // can be something like "[1, 2]->func()"
5162 return arg;
5163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5165 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005166 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005167 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005168 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005169
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005170 if (heredoc)
5171 {
5172 list_T *l;
5173 listitem_T *li;
5174
5175 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005176 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005178 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179
5180 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005181 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182 {
5183 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5184 li->li_tv.vval.v_string = NULL;
5185 }
5186 generate_NEWLIST(cctx, l->lv_len);
5187 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005188 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005189 list_free(l);
5190 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005191 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005192 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005193 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005194 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005195 // for "[var, var] = expr" evaluate the expression here, loop over the
5196 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005197
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005198 p = skipwhite(op + oplen);
5199 if (compile_expr0(&p, cctx) == FAIL)
5200 return NULL;
5201 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005203 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005205 type_T *stacktype;
5206
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005207 stacktype = stack->ga_len == 0 ? &t_void
5208 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005209 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005211 emsg(_(e_cannot_use_void));
5212 goto theend;
5213 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005214 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005215 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005216 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005217 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5218 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005219 }
5220 }
5221
5222 /*
5223 * Loop over variables in "[var, var] = expr".
5224 * For "var = expr" and "let var: type" this is done only once.
5225 */
5226 if (var_count > 0)
5227 var_start = skipwhite(arg + 1); // skip over the "["
5228 else
5229 var_start = arg;
5230 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5231 {
5232 char_u *var_end = skip_var_one(var_start, FALSE);
5233 size_t varlen;
5234 int new_local = FALSE;
5235 int opt_type;
5236 int opt_flags = 0;
5237 assign_dest_T dest = dest_local;
5238 int vimvaridx = -1;
5239 lvar_T *lvar = NULL;
5240 lvar_T arg_lvar;
5241 int has_type = FALSE;
5242 int has_index = FALSE;
5243 int instr_count = -1;
5244
Bram Moolenaar65821722020-08-02 18:58:54 +02005245 if (*var_start == '@')
5246 p = var_start + 2;
5247 else
5248 {
5249 p = (*var_start == '&' || *var_start == '$')
5250 ? var_start + 1 : var_start;
5251 p = to_name_end(p, TRUE);
5252 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005253
5254 // "a: type" is declaring variable "a" with a type, not "a:".
5255 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5256 --var_end;
5257 if (is_decl && p == var_start + 2 && p[-1] == ':')
5258 --p;
5259
5260 varlen = p - var_start;
5261 vim_free(name);
5262 name = vim_strnsave(var_start, varlen);
5263 if (name == NULL)
5264 return NULL;
5265 if (!heredoc)
5266 type = &t_any;
5267
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005268 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005269 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005270 int declare_error = FALSE;
5271
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005272 if (*var_start == '&')
5273 {
5274 int cc;
5275 long numval;
5276
5277 dest = dest_option;
5278 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005279 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005280 emsg(_(e_const_option));
5281 goto theend;
5282 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005283 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005284 p = var_start;
5285 p = find_option_end(&p, &opt_flags);
5286 if (p == NULL)
5287 {
5288 // cannot happen?
5289 emsg(_(e_letunexp));
5290 goto theend;
5291 }
5292 cc = *p;
5293 *p = NUL;
5294 opt_type = get_option_value(var_start + 1, &numval,
5295 NULL, opt_flags);
5296 *p = cc;
5297 if (opt_type == -3)
5298 {
5299 semsg(_(e_unknown_option), var_start);
5300 goto theend;
5301 }
5302 if (opt_type == -2 || opt_type == 0)
5303 type = &t_string;
5304 else
5305 type = &t_number; // both number and boolean option
5306 }
5307 else if (*var_start == '$')
5308 {
5309 dest = dest_env;
5310 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005311 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005312 }
5313 else if (*var_start == '@')
5314 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005315 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005316 {
5317 emsg_invreg(var_start[1]);
5318 goto theend;
5319 }
5320 dest = dest_reg;
5321 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005322 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005323 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005324 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005325 {
5326 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005327 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005328 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005329 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005330 {
5331 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005332 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005333 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005334 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005335 {
5336 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005337 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005338 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005339 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005340 {
5341 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005342 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005343 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005344 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005345 {
5346 typval_T *vtv;
5347 int di_flags;
5348
5349 vimvaridx = find_vim_var(name + 2, &di_flags);
5350 if (vimvaridx < 0)
5351 {
5352 semsg(_(e_var_notfound), var_start);
5353 goto theend;
5354 }
5355 // We use the current value of "sandbox" here, is that OK?
5356 if (var_check_ro(di_flags, name, FALSE))
5357 goto theend;
5358 dest = dest_vimvar;
5359 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005360 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005361 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005362 }
5363 else
5364 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005365 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005366
5367 for (idx = 0; reserved[idx] != NULL; ++idx)
5368 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005369 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005370 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005371 goto theend;
5372 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005373
5374 lvar = lookup_local(var_start, varlen, cctx);
5375 if (lvar == NULL)
5376 {
5377 CLEAR_FIELD(arg_lvar);
5378 if (lookup_arg(var_start, varlen,
5379 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5380 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005381 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005382 if (is_decl)
5383 {
5384 semsg(_(e_used_as_arg), name);
5385 goto theend;
5386 }
5387 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005388 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005389 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005390 if (lvar != NULL)
5391 {
5392 if (is_decl)
5393 {
5394 semsg(_("E1017: Variable already declared: %s"), name);
5395 goto theend;
5396 }
5397 else if (lvar->lv_const)
5398 {
5399 semsg(_("E1018: Cannot assign to a constant: %s"),
5400 name);
5401 goto theend;
5402 }
5403 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005404 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005405 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005406 int script_namespace = varlen > 1
5407 && STRNCMP(var_start, "s:", 2) == 0;
5408 int script_var = (script_namespace
5409 ? lookup_script(var_start + 2, varlen - 2)
5410 : lookup_script(var_start, varlen)) == OK;
5411 imported_T *import =
5412 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005413
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005414 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005415 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005416 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5417
5418 if (is_decl)
5419 {
5420 if (script_namespace)
5421 semsg(_("E1101: Cannot declare a script variable in a function: %s"),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005422 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005423 else
5424 semsg(_("E1054: Variable already declared in the script: %s"),
5425 name);
5426 goto theend;
5427 }
5428 else if (cctx->ctx_ufunc->uf_script_ctx_version
5429 == SCRIPT_VERSION_VIM9
5430 && script_namespace
5431 && !script_var && import == NULL)
5432 {
5433 semsg(_(e_unknown_var), name);
5434 goto theend;
5435 }
5436
5437 dest = dest_script;
5438
5439 // existing script-local variables should have a type
5440 scriptvar_sid = current_sctx.sc_sid;
5441 if (import != NULL)
5442 scriptvar_sid = import->imp_sid;
5443 scriptvar_idx = get_script_item_idx(scriptvar_sid,
5444 rawname, TRUE);
5445 if (scriptvar_idx >= 0)
5446 {
5447 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5448 svar_T *sv =
5449 ((svar_T *)si->sn_var_vals.ga_data)
5450 + scriptvar_idx;
5451 type = sv->sv_type;
5452 }
5453 }
5454 else if (name[1] == ':' && name[2] != NUL)
5455 {
5456 semsg(_("E1082: Cannot use a namespaced variable: %s"),
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005457 name);
5458 goto theend;
5459 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005460 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005461 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005462 semsg(_(e_unknown_var), name);
5463 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005464 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005465 else if (check_defined(var_start, varlen, cctx) == FAIL)
5466 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005467 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005468 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005469
5470 if (declare_error)
5471 {
5472 vim9_declare_error(name);
5473 goto theend;
5474 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005475 }
5476
5477 // handle "a:name" as a name, not index "name" on "a"
5478 if (varlen > 1 || var_start[varlen] != ':')
5479 p = var_end;
5480
5481 if (dest != dest_option)
5482 {
5483 if (is_decl && *p == ':')
5484 {
5485 // parse optional type: "let var: type = expr"
5486 if (!VIM_ISWHITE(p[1]))
5487 {
5488 semsg(_(e_white_after), ":");
5489 goto theend;
5490 }
5491 p = skipwhite(p + 1);
5492 type = parse_type(&p, cctx->ctx_type_list);
5493 has_type = TRUE;
5494 }
5495 else if (lvar != NULL)
5496 type = lvar->lv_type;
5497 }
5498
5499 if (oplen == 3 && !heredoc && dest != dest_global
5500 && type->tt_type != VAR_STRING
5501 && type->tt_type != VAR_ANY)
5502 {
5503 emsg(_("E1019: Can only concatenate to string"));
5504 goto theend;
5505 }
5506
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005507 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005508 {
5509 if (oplen > 1 && !heredoc)
5510 {
5511 // +=, /=, etc. require an existing variable
5512 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5513 name);
5514 goto theend;
5515 }
5516
5517 // new local variable
5518 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5519 goto theend;
5520 lvar = reserve_local(cctx, var_start, varlen,
5521 cmdidx == CMD_const, type);
5522 if (lvar == NULL)
5523 goto theend;
5524 new_local = TRUE;
5525 }
5526
5527 member_type = type;
5528 if (var_end > var_start + varlen)
5529 {
5530 // Something follows after the variable: "var[idx]".
5531 if (is_decl)
5532 {
5533 emsg(_("E1087: cannot use an index when declaring a variable"));
5534 goto theend;
5535 }
5536
5537 if (var_start[varlen] == '[')
5538 {
5539 has_index = TRUE;
5540 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005541 member_type = &t_any;
5542 else
5543 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005544 }
5545 else
5546 {
5547 semsg("Not supported yet: %s", var_start);
5548 goto theend;
5549 }
5550 }
5551 else if (lvar == &arg_lvar)
5552 {
5553 semsg(_("E1090: Cannot assign to argument %s"), name);
5554 goto theend;
5555 }
5556
5557 if (!heredoc)
5558 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005559 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005560 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005561 if (oplen > 0 && var_count == 0)
5562 {
5563 // skip over the "=" and the expression
5564 p = skipwhite(op + oplen);
5565 compile_expr0(&p, cctx);
5566 }
5567 }
5568 else if (oplen > 0)
5569 {
5570 type_T *stacktype;
5571
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005572 // For "var = expr" evaluate the expression.
5573 if (var_count == 0)
5574 {
5575 int r;
5576
5577 // for "+=", "*=", "..=" etc. first load the current value
5578 if (*op != '=')
5579 {
5580 generate_loadvar(cctx, dest, name, lvar, type);
5581
5582 if (has_index)
5583 {
5584 // TODO: get member from list or dict
5585 emsg("Index with operation not supported yet");
5586 goto theend;
5587 }
5588 }
5589
5590 // Compile the expression. Temporarily hide the new local
5591 // variable here, it is not available to this expression.
5592 if (new_local)
5593 --cctx->ctx_locals.ga_len;
5594 instr_count = instr->ga_len;
5595 p = skipwhite(op + oplen);
5596 r = compile_expr0(&p, cctx);
5597 if (new_local)
5598 ++cctx->ctx_locals.ga_len;
5599 if (r == FAIL)
5600 goto theend;
5601 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005602 else if (semicolon && var_idx == var_count - 1)
5603 {
5604 // For "[var; var] = expr" get the rest of the list
5605 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5606 goto theend;
5607 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005608 else
5609 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005610 // For "[var, var] = expr" get the "var_idx" item from the
5611 // list.
5612 if (generate_GETITEM(cctx, var_idx) == FAIL)
5613 return FAIL;
5614 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005615
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005616 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005617 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005618 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005619 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005620 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005621 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005622 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005623 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005624 emsg(_(e_cannot_use_void));
5625 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005626 }
5627 else
5628 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005629 // An empty list or dict has a &t_void member,
5630 // for a variable that implies &t_any.
5631 if (stacktype == &t_list_empty)
5632 lvar->lv_type = &t_list_any;
5633 else if (stacktype == &t_dict_empty)
5634 lvar->lv_type = &t_dict_any;
5635 else
5636 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005637 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005638 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005639 else
5640 {
5641 type_T *use_type = lvar->lv_type;
5642
5643 if (has_index)
5644 {
5645 use_type = use_type->tt_member;
5646 if (use_type == NULL)
5647 use_type = &t_void;
5648 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005649 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005650 == FAIL)
5651 goto theend;
5652 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005653 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005654 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005655 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005656 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005657 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005658 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005659 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005660 emsg(_(e_const_req_value));
5661 goto theend;
5662 }
5663 else if (!has_type || dest == dest_option)
5664 {
5665 emsg(_(e_type_req));
5666 goto theend;
5667 }
5668 else
5669 {
5670 // variables are always initialized
5671 if (ga_grow(instr, 1) == FAIL)
5672 goto theend;
5673 switch (member_type->tt_type)
5674 {
5675 case VAR_BOOL:
5676 generate_PUSHBOOL(cctx, VVAL_FALSE);
5677 break;
5678 case VAR_FLOAT:
5679#ifdef FEAT_FLOAT
5680 generate_PUSHF(cctx, 0.0);
5681#endif
5682 break;
5683 case VAR_STRING:
5684 generate_PUSHS(cctx, NULL);
5685 break;
5686 case VAR_BLOB:
5687 generate_PUSHBLOB(cctx, NULL);
5688 break;
5689 case VAR_FUNC:
5690 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5691 break;
5692 case VAR_LIST:
5693 generate_NEWLIST(cctx, 0);
5694 break;
5695 case VAR_DICT:
5696 generate_NEWDICT(cctx, 0);
5697 break;
5698 case VAR_JOB:
5699 generate_PUSHJOB(cctx, NULL);
5700 break;
5701 case VAR_CHANNEL:
5702 generate_PUSHCHANNEL(cctx, NULL);
5703 break;
5704 case VAR_NUMBER:
5705 case VAR_UNKNOWN:
5706 case VAR_ANY:
5707 case VAR_PARTIAL:
5708 case VAR_VOID:
5709 case VAR_SPECIAL: // cannot happen
5710 generate_PUSHNR(cctx, 0);
5711 break;
5712 }
5713 }
5714 if (var_count == 0)
5715 end = p;
5716 }
5717
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005718 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005719 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005720 break;
5721
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005722 if (oplen > 0 && *op != '=')
5723 {
5724 type_T *expected = &t_number;
5725 type_T *stacktype;
5726
5727 // TODO: if type is known use float or any operation
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005728 // TODO: check operator matches variable type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005729
5730 if (*op == '.')
5731 expected = &t_string;
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005732 else if (*op == '+')
5733 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005734 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005735 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005736 goto theend;
5737
5738 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005739 {
5740 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5741 goto theend;
5742 }
5743 else if (*op == '+')
5744 {
5745 if (generate_add_instr(cctx,
5746 operator_type(member_type, stacktype),
5747 member_type, stacktype) == FAIL)
5748 goto theend;
5749 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005750 else
5751 {
5752 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5753
5754 if (isn == NULL)
5755 goto theend;
5756 switch (*op)
5757 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005758 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5759 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5760 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5761 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5762 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005763 }
5764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005765
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005766 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005767 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005768 int r;
5769
5770 // Compile the "idx" in "var[idx]".
5771 if (new_local)
5772 --cctx->ctx_locals.ga_len;
5773 p = skipwhite(var_start + varlen + 1);
5774 r = compile_expr0(&p, cctx);
5775 if (new_local)
5776 ++cctx->ctx_locals.ga_len;
5777 if (r == FAIL)
5778 goto theend;
5779 if (*skipwhite(p) != ']')
5780 {
5781 emsg(_(e_missbrac));
5782 goto theend;
5783 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005784 if (type == &t_any)
5785 {
5786 type_T *idx_type = ((type_T **)stack->ga_data)[
5787 stack->ga_len - 1];
5788 // Index on variable of unknown type: guess the type from the
5789 // index type: number is dict, otherwise dict.
5790 // TODO: should do the assignment at runtime
5791 if (idx_type->tt_type == VAR_NUMBER)
5792 type = &t_list_any;
5793 else
5794 type = &t_dict_any;
5795 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005796 if (type->tt_type == VAR_DICT
5797 && may_generate_2STRING(-1, cctx) == FAIL)
5798 goto theend;
5799 if (type->tt_type == VAR_LIST
5800 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005801 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005802 {
5803 emsg(_(e_number_exp));
5804 goto theend;
5805 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005806
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005807 // Load the dict or list. On the stack we then have:
5808 // - value
5809 // - index
5810 // - variable
5811 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005812
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005813 if (type->tt_type == VAR_LIST)
5814 {
5815 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5816 return FAIL;
5817 }
5818 else if (type->tt_type == VAR_DICT)
5819 {
5820 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5821 return FAIL;
5822 }
5823 else
5824 {
5825 emsg(_(e_listreq));
5826 goto theend;
5827 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005828 }
5829 else
5830 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005831 switch (dest)
5832 {
5833 case dest_option:
5834 generate_STOREOPT(cctx, name + 1, opt_flags);
5835 break;
5836 case dest_global:
5837 // include g: with the name, easier to execute that way
5838 generate_STORE(cctx, ISN_STOREG, 0, name);
5839 break;
5840 case dest_buffer:
5841 // include b: with the name, easier to execute that way
5842 generate_STORE(cctx, ISN_STOREB, 0, name);
5843 break;
5844 case dest_window:
5845 // include w: with the name, easier to execute that way
5846 generate_STORE(cctx, ISN_STOREW, 0, name);
5847 break;
5848 case dest_tab:
5849 // include t: with the name, easier to execute that way
5850 generate_STORE(cctx, ISN_STORET, 0, name);
5851 break;
5852 case dest_env:
5853 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5854 break;
5855 case dest_reg:
5856 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5857 break;
5858 case dest_vimvar:
5859 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5860 break;
5861 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005862 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005863 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005864 {
5865 char_u *name_s = name;
5866
5867 // Include s: in the name for store_var()
5868 if (name[1] != ':')
5869 {
5870 int len = (int)STRLEN(name) + 3;
5871
5872 name_s = alloc(len);
5873 if (name_s == NULL)
5874 name_s = name;
5875 else
5876 vim_snprintf((char *)name_s, len,
5877 "s:%s", name);
5878 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005879 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5880 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005881 if (name_s != name)
5882 vim_free(name_s);
5883 }
5884 else
5885 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005886 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005887 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005888 break;
5889 case dest_local:
5890 if (lvar != NULL)
5891 {
5892 isn_T *isn = ((isn_T *)instr->ga_data)
5893 + instr->ga_len - 1;
5894
5895 // optimization: turn "var = 123" from ISN_PUSHNR +
5896 // ISN_STORE into ISN_STORENR
5897 if (!lvar->lv_from_outer
5898 && instr->ga_len == instr_count + 1
5899 && isn->isn_type == ISN_PUSHNR)
5900 {
5901 varnumber_T val = isn->isn_arg.number;
5902
5903 isn->isn_type = ISN_STORENR;
5904 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5905 isn->isn_arg.storenr.stnr_val = val;
5906 if (stack->ga_len > 0)
5907 --stack->ga_len;
5908 }
5909 else if (lvar->lv_from_outer)
5910 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005911 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005912 else
5913 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5914 }
5915 break;
5916 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005917 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005918
5919 if (var_idx + 1 < var_count)
5920 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005921 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005922
5923 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005924 if (var_count > 0 && !semicolon)
5925 {
5926 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5927 goto theend;
5928 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005929
Bram Moolenaarb2097502020-07-19 17:17:02 +02005930 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005931
5932theend:
5933 vim_free(name);
5934 return ret;
5935}
5936
5937/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005938 * Check if "name" can be "unlet".
5939 */
5940 int
5941check_vim9_unlet(char_u *name)
5942{
5943 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5944 {
5945 semsg(_("E1081: Cannot unlet %s"), name);
5946 return FAIL;
5947 }
5948 return OK;
5949}
5950
5951/*
5952 * Callback passed to ex_unletlock().
5953 */
5954 static int
5955compile_unlet(
5956 lval_T *lvp,
5957 char_u *name_end,
5958 exarg_T *eap,
5959 int deep UNUSED,
5960 void *coookie)
5961{
5962 cctx_T *cctx = coookie;
5963
5964 if (lvp->ll_tv == NULL)
5965 {
5966 char_u *p = lvp->ll_name;
5967 int cc = *name_end;
5968 int ret = OK;
5969
5970 // Normal name. Only supports g:, w:, t: and b: namespaces.
5971 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005972 if (*p == '$')
5973 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5974 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005975 ret = FAIL;
5976 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005977 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005978
5979 *name_end = cc;
5980 return ret;
5981 }
5982
5983 // TODO: unlet {list}[idx]
5984 // TODO: unlet {dict}[key]
5985 emsg("Sorry, :unlet not fully implemented yet");
5986 return FAIL;
5987}
5988
5989/*
5990 * compile "unlet var", "lock var" and "unlock var"
5991 * "arg" points to "var".
5992 */
5993 static char_u *
5994compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5995{
5996 char_u *p = arg;
5997
5998 if (eap->cmdidx != CMD_unlet)
5999 {
6000 emsg("Sorry, :lock and unlock not implemented yet");
6001 return NULL;
6002 }
6003
6004 if (*p == '!')
6005 {
6006 p = skipwhite(p + 1);
6007 eap->forceit = TRUE;
6008 }
6009
6010 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
6011 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6012}
6013
6014/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015 * Compile an :import command.
6016 */
6017 static char_u *
6018compile_import(char_u *arg, cctx_T *cctx)
6019{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006020 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021}
6022
6023/*
6024 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6025 */
6026 static int
6027compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6028{
6029 garray_T *instr = &cctx->ctx_instr;
6030 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6031
6032 if (endlabel == NULL)
6033 return FAIL;
6034 endlabel->el_next = *el;
6035 *el = endlabel;
6036 endlabel->el_end_label = instr->ga_len;
6037
6038 generate_JUMP(cctx, when, 0);
6039 return OK;
6040}
6041
6042 static void
6043compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6044{
6045 garray_T *instr = &cctx->ctx_instr;
6046
6047 while (*el != NULL)
6048 {
6049 endlabel_T *cur = (*el);
6050 isn_T *isn;
6051
6052 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6053 isn->isn_arg.jump.jump_where = instr->ga_len;
6054 *el = cur->el_next;
6055 vim_free(cur);
6056 }
6057}
6058
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006059 static void
6060compile_free_jump_to_end(endlabel_T **el)
6061{
6062 while (*el != NULL)
6063 {
6064 endlabel_T *cur = (*el);
6065
6066 *el = cur->el_next;
6067 vim_free(cur);
6068 }
6069}
6070
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006071/*
6072 * Create a new scope and set up the generic items.
6073 */
6074 static scope_T *
6075new_scope(cctx_T *cctx, scopetype_T type)
6076{
6077 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6078
6079 if (scope == NULL)
6080 return NULL;
6081 scope->se_outer = cctx->ctx_scope;
6082 cctx->ctx_scope = scope;
6083 scope->se_type = type;
6084 scope->se_local_count = cctx->ctx_locals.ga_len;
6085 return scope;
6086}
6087
6088/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006089 * Free the current scope and go back to the outer scope.
6090 */
6091 static void
6092drop_scope(cctx_T *cctx)
6093{
6094 scope_T *scope = cctx->ctx_scope;
6095
6096 if (scope == NULL)
6097 {
6098 iemsg("calling drop_scope() without a scope");
6099 return;
6100 }
6101 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006102 switch (scope->se_type)
6103 {
6104 case IF_SCOPE:
6105 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6106 case FOR_SCOPE:
6107 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6108 case WHILE_SCOPE:
6109 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6110 case TRY_SCOPE:
6111 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6112 case NO_SCOPE:
6113 case BLOCK_SCOPE:
6114 break;
6115 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006116 vim_free(scope);
6117}
6118
6119/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006120 * compile "if expr"
6121 *
6122 * "if expr" Produces instructions:
6123 * EVAL expr Push result of "expr"
6124 * JUMP_IF_FALSE end
6125 * ... body ...
6126 * end:
6127 *
6128 * "if expr | else" Produces instructions:
6129 * EVAL expr Push result of "expr"
6130 * JUMP_IF_FALSE else
6131 * ... body ...
6132 * JUMP_ALWAYS end
6133 * else:
6134 * ... body ...
6135 * end:
6136 *
6137 * "if expr1 | elseif expr2 | else" Produces instructions:
6138 * EVAL expr Push result of "expr"
6139 * JUMP_IF_FALSE elseif
6140 * ... body ...
6141 * JUMP_ALWAYS end
6142 * elseif:
6143 * EVAL expr Push result of "expr"
6144 * JUMP_IF_FALSE else
6145 * ... body ...
6146 * JUMP_ALWAYS end
6147 * else:
6148 * ... body ...
6149 * end:
6150 */
6151 static char_u *
6152compile_if(char_u *arg, cctx_T *cctx)
6153{
6154 char_u *p = arg;
6155 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006156 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006157 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006158 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006159 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006160
Bram Moolenaara5565e42020-05-09 15:44:01 +02006161 CLEAR_FIELD(ppconst);
6162 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006163 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006164 clear_ppconst(&ppconst);
6165 return NULL;
6166 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006167 if (cctx->ctx_skip == SKIP_YES)
6168 clear_ppconst(&ppconst);
6169 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006170 {
6171 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006172 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006173 clear_ppconst(&ppconst);
6174 }
6175 else
6176 {
6177 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006178 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006179 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006180 return NULL;
6181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006182
6183 scope = new_scope(cctx, IF_SCOPE);
6184 if (scope == NULL)
6185 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006186 scope->se_skip_save = skip_save;
6187 // "is_had_return" will be reset if any block does not end in :return
6188 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006189
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006190 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006191 {
6192 // "where" is set when ":elseif", "else" or ":endif" is found
6193 scope->se_u.se_if.is_if_label = instr->ga_len;
6194 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6195 }
6196 else
6197 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006198
6199 return p;
6200}
6201
6202 static char_u *
6203compile_elseif(char_u *arg, cctx_T *cctx)
6204{
6205 char_u *p = arg;
6206 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006207 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006208 isn_T *isn;
6209 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006210 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006211
6212 if (scope == NULL || scope->se_type != IF_SCOPE)
6213 {
6214 emsg(_(e_elseif_without_if));
6215 return NULL;
6216 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006217 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006218 if (!cctx->ctx_had_return)
6219 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006220
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006221 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006222 {
6223 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006224 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006225 return NULL;
6226 // previous "if" or "elseif" jumps here
6227 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6228 isn->isn_arg.jump.jump_where = instr->ga_len;
6229 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006230
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006231 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006232 CLEAR_FIELD(ppconst);
6233 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006234 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006235 clear_ppconst(&ppconst);
6236 return NULL;
6237 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006238 if (scope->se_skip_save == SKIP_YES)
6239 clear_ppconst(&ppconst);
6240 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006241 {
6242 // The expression results in a constant.
6243 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006244 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006245 clear_ppconst(&ppconst);
6246 scope->se_u.se_if.is_if_label = -1;
6247 }
6248 else
6249 {
6250 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006251 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006252 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006253 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006254
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006255 // "where" is set when ":elseif", "else" or ":endif" is found
6256 scope->se_u.se_if.is_if_label = instr->ga_len;
6257 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6258 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259
6260 return p;
6261}
6262
6263 static char_u *
6264compile_else(char_u *arg, cctx_T *cctx)
6265{
6266 char_u *p = arg;
6267 garray_T *instr = &cctx->ctx_instr;
6268 isn_T *isn;
6269 scope_T *scope = cctx->ctx_scope;
6270
6271 if (scope == NULL || scope->se_type != IF_SCOPE)
6272 {
6273 emsg(_(e_else_without_if));
6274 return NULL;
6275 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006276 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006277 if (!cctx->ctx_had_return)
6278 scope->se_u.se_if.is_had_return = FALSE;
6279 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006280
Bram Moolenaarefd88552020-06-18 20:50:10 +02006281 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006282 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006283 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006284 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006285 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006286 if (!cctx->ctx_had_return
6287 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6288 JUMP_ALWAYS, cctx) == FAIL)
6289 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006290 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006291
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006292 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006293 {
6294 if (scope->se_u.se_if.is_if_label >= 0)
6295 {
6296 // previous "if" or "elseif" jumps here
6297 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6298 isn->isn_arg.jump.jump_where = instr->ga_len;
6299 scope->se_u.se_if.is_if_label = -1;
6300 }
6301 }
6302
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006303 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006304 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6305 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006306
6307 return p;
6308}
6309
6310 static char_u *
6311compile_endif(char_u *arg, cctx_T *cctx)
6312{
6313 scope_T *scope = cctx->ctx_scope;
6314 ifscope_T *ifscope;
6315 garray_T *instr = &cctx->ctx_instr;
6316 isn_T *isn;
6317
6318 if (scope == NULL || scope->se_type != IF_SCOPE)
6319 {
6320 emsg(_(e_endif_without_if));
6321 return NULL;
6322 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006323 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006324 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006325 if (!cctx->ctx_had_return)
6326 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006327
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006328 if (scope->se_u.se_if.is_if_label >= 0)
6329 {
6330 // previous "if" or "elseif" jumps here
6331 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6332 isn->isn_arg.jump.jump_where = instr->ga_len;
6333 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006334 // Fill in the "end" label in jumps at the end of the blocks.
6335 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006336 cctx->ctx_skip = scope->se_skip_save;
6337
6338 // If all the blocks end in :return and there is an :else then the
6339 // had_return flag is set.
6340 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006341
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006342 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343 return arg;
6344}
6345
6346/*
6347 * compile "for var in expr"
6348 *
6349 * Produces instructions:
6350 * PUSHNR -1
6351 * STORE loop-idx Set index to -1
6352 * EVAL expr Push result of "expr"
6353 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6354 * - if beyond end, jump to "end"
6355 * - otherwise get item from list and push it
6356 * STORE var Store item in "var"
6357 * ... body ...
6358 * JUMP top Jump back to repeat
6359 * end: DROP Drop the result of "expr"
6360 *
6361 */
6362 static char_u *
6363compile_for(char_u *arg, cctx_T *cctx)
6364{
6365 char_u *p;
6366 size_t varlen;
6367 garray_T *instr = &cctx->ctx_instr;
6368 garray_T *stack = &cctx->ctx_type_stack;
6369 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006370 lvar_T *loop_lvar; // loop iteration variable
6371 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006372 type_T *vartype;
6373
6374 // TODO: list of variables: "for [key, value] in dict"
6375 // parse "var"
6376 for (p = arg; eval_isnamec1(*p); ++p)
6377 ;
6378 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006379 var_lvar = lookup_local(arg, varlen, cctx);
6380 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006381 {
6382 semsg(_("E1023: variable already defined: %s"), arg);
6383 return NULL;
6384 }
6385
6386 // consume "in"
6387 p = skipwhite(p);
6388 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6389 {
6390 emsg(_(e_missing_in));
6391 return NULL;
6392 }
6393 p = skipwhite(p + 2);
6394
6395
6396 scope = new_scope(cctx, FOR_SCOPE);
6397 if (scope == NULL)
6398 return NULL;
6399
6400 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006401 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6402 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006403 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006404 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006405 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006407 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408
6409 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006410 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6411 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006412 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006413 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006414 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006415 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006416 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006417
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006418 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006419
6420 // compile "expr", it remains on the stack until "endfor"
6421 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006422 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006423 {
6424 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006425 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006426 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006427
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006428 // Now that we know the type of "var", check that it is a list, now or at
6429 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006430 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006431 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006432 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006433 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006434 return NULL;
6435 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006436 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006437 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006438
6439 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006440 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006441
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006442 generate_FOR(cctx, loop_lvar->lv_idx);
6443 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006444
6445 return arg;
6446}
6447
6448/*
6449 * compile "endfor"
6450 */
6451 static char_u *
6452compile_endfor(char_u *arg, cctx_T *cctx)
6453{
6454 garray_T *instr = &cctx->ctx_instr;
6455 scope_T *scope = cctx->ctx_scope;
6456 forscope_T *forscope;
6457 isn_T *isn;
6458
6459 if (scope == NULL || scope->se_type != FOR_SCOPE)
6460 {
6461 emsg(_(e_for));
6462 return NULL;
6463 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006464 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006465 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006466 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006467
6468 // At end of ":for" scope jump back to the FOR instruction.
6469 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6470
6471 // Fill in the "end" label in the FOR statement so it can jump here
6472 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6473 isn->isn_arg.forloop.for_end = instr->ga_len;
6474
6475 // Fill in the "end" label any BREAK statements
6476 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6477
6478 // Below the ":for" scope drop the "expr" list from the stack.
6479 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6480 return NULL;
6481
6482 vim_free(scope);
6483
6484 return arg;
6485}
6486
6487/*
6488 * compile "while expr"
6489 *
6490 * Produces instructions:
6491 * top: EVAL expr Push result of "expr"
6492 * JUMP_IF_FALSE end jump if false
6493 * ... body ...
6494 * JUMP top Jump back to repeat
6495 * end:
6496 *
6497 */
6498 static char_u *
6499compile_while(char_u *arg, cctx_T *cctx)
6500{
6501 char_u *p = arg;
6502 garray_T *instr = &cctx->ctx_instr;
6503 scope_T *scope;
6504
6505 scope = new_scope(cctx, WHILE_SCOPE);
6506 if (scope == NULL)
6507 return NULL;
6508
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006509 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006510
6511 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006512 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006513 return NULL;
6514
6515 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006516 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006517 JUMP_IF_FALSE, cctx) == FAIL)
6518 return FAIL;
6519
6520 return p;
6521}
6522
6523/*
6524 * compile "endwhile"
6525 */
6526 static char_u *
6527compile_endwhile(char_u *arg, cctx_T *cctx)
6528{
6529 scope_T *scope = cctx->ctx_scope;
6530
6531 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6532 {
6533 emsg(_(e_while));
6534 return NULL;
6535 }
6536 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006537 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006538
6539 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006540 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006541
6542 // Fill in the "end" label in the WHILE statement so it can jump here.
6543 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006544 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006545
6546 vim_free(scope);
6547
6548 return arg;
6549}
6550
6551/*
6552 * compile "continue"
6553 */
6554 static char_u *
6555compile_continue(char_u *arg, cctx_T *cctx)
6556{
6557 scope_T *scope = cctx->ctx_scope;
6558
6559 for (;;)
6560 {
6561 if (scope == NULL)
6562 {
6563 emsg(_(e_continue));
6564 return NULL;
6565 }
6566 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6567 break;
6568 scope = scope->se_outer;
6569 }
6570
6571 // Jump back to the FOR or WHILE instruction.
6572 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006573 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6574 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006575 return arg;
6576}
6577
6578/*
6579 * compile "break"
6580 */
6581 static char_u *
6582compile_break(char_u *arg, cctx_T *cctx)
6583{
6584 scope_T *scope = cctx->ctx_scope;
6585 endlabel_T **el;
6586
6587 for (;;)
6588 {
6589 if (scope == NULL)
6590 {
6591 emsg(_(e_break));
6592 return NULL;
6593 }
6594 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6595 break;
6596 scope = scope->se_outer;
6597 }
6598
6599 // Jump to the end of the FOR or WHILE loop.
6600 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006601 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006603 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006604 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6605 return FAIL;
6606
6607 return arg;
6608}
6609
6610/*
6611 * compile "{" start of block
6612 */
6613 static char_u *
6614compile_block(char_u *arg, cctx_T *cctx)
6615{
6616 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6617 return NULL;
6618 return skipwhite(arg + 1);
6619}
6620
6621/*
6622 * compile end of block: drop one scope
6623 */
6624 static void
6625compile_endblock(cctx_T *cctx)
6626{
6627 scope_T *scope = cctx->ctx_scope;
6628
6629 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006630 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006631 vim_free(scope);
6632}
6633
6634/*
6635 * compile "try"
6636 * Creates a new scope for the try-endtry, pointing to the first catch and
6637 * finally.
6638 * Creates another scope for the "try" block itself.
6639 * TRY instruction sets up exception handling at runtime.
6640 *
6641 * "try"
6642 * TRY -> catch1, -> finally push trystack entry
6643 * ... try block
6644 * "throw {exception}"
6645 * EVAL {exception}
6646 * THROW create exception
6647 * ... try block
6648 * " catch {expr}"
6649 * JUMP -> finally
6650 * catch1: PUSH exeception
6651 * EVAL {expr}
6652 * MATCH
6653 * JUMP nomatch -> catch2
6654 * CATCH remove exception
6655 * ... catch block
6656 * " catch"
6657 * JUMP -> finally
6658 * catch2: CATCH remove exception
6659 * ... catch block
6660 * " finally"
6661 * finally:
6662 * ... finally block
6663 * " endtry"
6664 * ENDTRY pop trystack entry, may rethrow
6665 */
6666 static char_u *
6667compile_try(char_u *arg, cctx_T *cctx)
6668{
6669 garray_T *instr = &cctx->ctx_instr;
6670 scope_T *try_scope;
6671 scope_T *scope;
6672
6673 // scope that holds the jumps that go to catch/finally/endtry
6674 try_scope = new_scope(cctx, TRY_SCOPE);
6675 if (try_scope == NULL)
6676 return NULL;
6677
6678 // "catch" is set when the first ":catch" is found.
6679 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006680 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006681 if (generate_instr(cctx, ISN_TRY) == NULL)
6682 return NULL;
6683
6684 // scope for the try block itself
6685 scope = new_scope(cctx, BLOCK_SCOPE);
6686 if (scope == NULL)
6687 return NULL;
6688
6689 return arg;
6690}
6691
6692/*
6693 * compile "catch {expr}"
6694 */
6695 static char_u *
6696compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6697{
6698 scope_T *scope = cctx->ctx_scope;
6699 garray_T *instr = &cctx->ctx_instr;
6700 char_u *p;
6701 isn_T *isn;
6702
6703 // end block scope from :try or :catch
6704 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6705 compile_endblock(cctx);
6706 scope = cctx->ctx_scope;
6707
6708 // Error if not in a :try scope
6709 if (scope == NULL || scope->se_type != TRY_SCOPE)
6710 {
6711 emsg(_(e_catch));
6712 return NULL;
6713 }
6714
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006715 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006716 {
6717 emsg(_("E1033: catch unreachable after catch-all"));
6718 return NULL;
6719 }
6720
6721 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006722 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723 JUMP_ALWAYS, cctx) == FAIL)
6724 return NULL;
6725
6726 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006727 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006728 if (isn->isn_arg.try.try_catch == 0)
6729 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006730 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006731 {
6732 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006733 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734 isn->isn_arg.jump.jump_where = instr->ga_len;
6735 }
6736
6737 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006738 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006739 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006740 scope->se_u.se_try.ts_caught_all = TRUE;
6741 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006742 }
6743 else
6744 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006745 char_u *end;
6746 char_u *pat;
6747 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006748 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006749 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006750
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006751 // Push v:exception, push {expr} and MATCH
6752 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6753
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006754 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006755 if (*end != *p)
6756 {
6757 semsg(_("E1067: Separator mismatch: %s"), p);
6758 vim_free(tofree);
6759 return FAIL;
6760 }
6761 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006762 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006763 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006764 len = (int)(end - tofree);
6765 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006766 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006767 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006768 if (pat == NULL)
6769 return FAIL;
6770 if (generate_PUSHS(cctx, pat) == FAIL)
6771 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006773 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6774 return NULL;
6775
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006776 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6778 return NULL;
6779 }
6780
6781 if (generate_instr(cctx, ISN_CATCH) == NULL)
6782 return NULL;
6783
6784 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6785 return NULL;
6786 return p;
6787}
6788
6789 static char_u *
6790compile_finally(char_u *arg, cctx_T *cctx)
6791{
6792 scope_T *scope = cctx->ctx_scope;
6793 garray_T *instr = &cctx->ctx_instr;
6794 isn_T *isn;
6795
6796 // end block scope from :try or :catch
6797 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6798 compile_endblock(cctx);
6799 scope = cctx->ctx_scope;
6800
6801 // Error if not in a :try scope
6802 if (scope == NULL || scope->se_type != TRY_SCOPE)
6803 {
6804 emsg(_(e_finally));
6805 return NULL;
6806 }
6807
6808 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006809 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006810 if (isn->isn_arg.try.try_finally != 0)
6811 {
6812 emsg(_(e_finally_dup));
6813 return NULL;
6814 }
6815
6816 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006817 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006818
Bram Moolenaar585fea72020-04-02 22:33:21 +02006819 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006820 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006821 {
6822 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006823 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006824 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006825 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006826 }
6827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006828 // TODO: set index in ts_finally_label jumps
6829
6830 return arg;
6831}
6832
6833 static char_u *
6834compile_endtry(char_u *arg, cctx_T *cctx)
6835{
6836 scope_T *scope = cctx->ctx_scope;
6837 garray_T *instr = &cctx->ctx_instr;
6838 isn_T *isn;
6839
6840 // end block scope from :catch or :finally
6841 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6842 compile_endblock(cctx);
6843 scope = cctx->ctx_scope;
6844
6845 // Error if not in a :try scope
6846 if (scope == NULL || scope->se_type != TRY_SCOPE)
6847 {
6848 if (scope == NULL)
6849 emsg(_(e_no_endtry));
6850 else if (scope->se_type == WHILE_SCOPE)
6851 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006852 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006853 emsg(_(e_endfor));
6854 else
6855 emsg(_(e_endif));
6856 return NULL;
6857 }
6858
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006859 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006860 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6861 {
6862 emsg(_("E1032: missing :catch or :finally"));
6863 return NULL;
6864 }
6865
6866 // Fill in the "end" label in jumps at the end of the blocks, if not done
6867 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006868 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006869
6870 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006871 if (isn->isn_arg.try.try_catch == 0)
6872 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006873 if (isn->isn_arg.try.try_finally == 0)
6874 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006875
6876 if (scope->se_u.se_try.ts_catch_label != 0)
6877 {
6878 // Last catch without match jumps here
6879 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6880 isn->isn_arg.jump.jump_where = instr->ga_len;
6881 }
6882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006883 compile_endblock(cctx);
6884
6885 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6886 return NULL;
6887 return arg;
6888}
6889
6890/*
6891 * compile "throw {expr}"
6892 */
6893 static char_u *
6894compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6895{
6896 char_u *p = skipwhite(arg);
6897
Bram Moolenaara5565e42020-05-09 15:44:01 +02006898 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006899 return NULL;
6900 if (may_generate_2STRING(-1, cctx) == FAIL)
6901 return NULL;
6902 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6903 return NULL;
6904
6905 return p;
6906}
6907
6908/*
6909 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006910 * compile "echomsg expr"
6911 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006912 * compile "execute expr"
6913 */
6914 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006915compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006916{
6917 char_u *p = arg;
6918 int count = 0;
6919
6920 for (;;)
6921 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006922 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006923 return NULL;
6924 ++count;
6925 p = skipwhite(p);
6926 if (ends_excmd(*p))
6927 break;
6928 }
6929
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006930 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6931 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6932 else if (cmdidx == CMD_execute)
6933 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6934 else if (cmdidx == CMD_echomsg)
6935 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6936 else
6937 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938 return p;
6939}
6940
6941/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006942 * A command that is not compiled, execute with legacy code.
6943 */
6944 static char_u *
6945compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6946{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006947 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006948 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006949 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006950
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006951 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006952 goto theend;
6953
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006954 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006955 {
6956 long argt = excmd_get_argt(eap->cmdidx);
6957 int usefilter = FALSE;
6958
6959 has_expr = argt & (EX_XFILE | EX_EXPAND);
6960
6961 // If the command can be followed by a bar, find the bar and truncate
6962 // it, so that the following command can be compiled.
6963 // The '|' is overwritten with a NUL, it is put back below.
6964 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6965 && *eap->arg == '!')
6966 // :w !filter or :r !filter or :r! filter
6967 usefilter = TRUE;
6968 if ((argt & EX_TRLBAR) && !usefilter)
6969 {
6970 separate_nextcmd(eap);
6971 if (eap->nextcmd != NULL)
6972 nextcmd = eap->nextcmd;
6973 }
6974 }
6975
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006976 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6977 {
6978 // expand filename in "syntax include [@group] filename"
6979 has_expr = TRUE;
6980 eap->arg = skipwhite(eap->arg + 7);
6981 if (*eap->arg == '@')
6982 eap->arg = skiptowhite(eap->arg);
6983 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006984
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006985 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006986 {
6987 int count = 0;
6988 char_u *start = skipwhite(line);
6989
6990 // :cmd xxx`=expr1`yyy`=expr2`zzz
6991 // PUSHS ":cmd xxx"
6992 // eval expr1
6993 // PUSHS "yyy"
6994 // eval expr2
6995 // PUSHS "zzz"
6996 // EXECCONCAT 5
6997 for (;;)
6998 {
6999 if (p > start)
7000 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007001 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007002 ++count;
7003 }
7004 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007005 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007006 return NULL;
7007 may_generate_2STRING(-1, cctx);
7008 ++count;
7009 p = skipwhite(p);
7010 if (*p != '`')
7011 {
7012 emsg(_("E1083: missing backtick"));
7013 return NULL;
7014 }
7015 start = p + 1;
7016
7017 p = (char_u *)strstr((char *)start, "`=");
7018 if (p == NULL)
7019 {
7020 if (*skipwhite(start) != NUL)
7021 {
7022 generate_PUSHS(cctx, vim_strsave(start));
7023 ++count;
7024 }
7025 break;
7026 }
7027 }
7028 generate_EXECCONCAT(cctx, count);
7029 }
7030 else
7031 generate_EXEC(cctx, line);
7032
7033theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007034 if (*nextcmd != NUL)
7035 {
7036 // the parser expects a pointer to the bar, put it back
7037 --nextcmd;
7038 *nextcmd = '|';
7039 }
7040
7041 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007042}
7043
7044/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007045 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007046 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007047 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007048 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007049add_def_function(ufunc_T *ufunc)
7050{
7051 dfunc_T *dfunc;
7052
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007053 if (def_functions.ga_len == 0)
7054 {
7055 // The first position is not used, so that a zero uf_dfunc_idx means it
7056 // wasn't set.
7057 if (ga_grow(&def_functions, 1) == FAIL)
7058 return FAIL;
7059 ++def_functions.ga_len;
7060 }
7061
Bram Moolenaar09689a02020-05-09 22:50:08 +02007062 // Add the function to "def_functions".
7063 if (ga_grow(&def_functions, 1) == FAIL)
7064 return FAIL;
7065 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7066 CLEAR_POINTER(dfunc);
7067 dfunc->df_idx = def_functions.ga_len;
7068 ufunc->uf_dfunc_idx = dfunc->df_idx;
7069 dfunc->df_ufunc = ufunc;
7070 ++def_functions.ga_len;
7071 return OK;
7072}
7073
7074/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007075 * After ex_function() has collected all the function lines: parse and compile
7076 * the lines into instructions.
7077 * Adds the function to "def_functions".
7078 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7079 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007080 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007081 * This can be used recursively through compile_lambda(), which may reallocate
7082 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007083 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007085 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007086compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007087{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007088 char_u *line = NULL;
7089 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007090 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007091 cctx_T cctx;
7092 garray_T *instr;
7093 int called_emsg_before = called_emsg;
7094 int ret = FAIL;
7095 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007096 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007097 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007098 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007099
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007100 // When using a function that was compiled before: Free old instructions.
7101 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007102 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007103 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007104 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7105 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007106 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007108 else
7109 {
7110 if (add_def_function(ufunc) == FAIL)
7111 return FAIL;
7112 new_def_function = TRUE;
7113 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007114
Bram Moolenaar985116a2020-07-12 17:31:09 +02007115 ufunc->uf_def_status = UF_COMPILING;
7116
Bram Moolenaara80faa82020-04-12 19:37:17 +02007117 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007118 cctx.ctx_ufunc = ufunc;
7119 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007120 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007121 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7122 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7123 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7124 cctx.ctx_type_list = &ufunc->uf_type_list;
7125 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7126 instr = &cctx.ctx_instr;
7127
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007128 // Set the context to the function, it may be compiled when called from
7129 // another script. Set the script version to the most modern one.
7130 // The line number will be set in next_line_from_context().
7131 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007132 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7133
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007134 // Make sure error messages are OK.
7135 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7136 if (do_estack_push)
7137 estack_push_ufunc(ufunc, 1);
7138
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007139 if (ufunc->uf_def_args.ga_len > 0)
7140 {
7141 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007142 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007143 int i;
7144 char_u *arg;
7145 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007146 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007147
7148 // Produce instructions for the default values of optional arguments.
7149 // Store the instruction index in uf_def_arg_idx[] so that we know
7150 // where to start when the function is called, depending on the number
7151 // of arguments.
7152 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7153 if (ufunc->uf_def_arg_idx == NULL)
7154 goto erret;
7155 for (i = 0; i < count; ++i)
7156 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007157 garray_T *stack = &cctx.ctx_type_stack;
7158 type_T *val_type;
7159 int arg_idx = first_def_arg + i;
7160
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007161 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7162 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007163 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007164 goto erret;
7165
7166 // If no type specified use the type of the default value.
7167 // Otherwise check that the default value type matches the
7168 // specified type.
7169 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7170 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007171 {
7172 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007173 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007174 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02007175 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007176 == FAIL)
7177 {
7178 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
7179 arg_idx + 1);
7180 goto erret;
7181 }
7182
7183 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007184 goto erret;
7185 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007186 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007187
7188 if (did_set_arg_type)
7189 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007190 }
7191
7192 /*
7193 * Loop over all the lines of the function and generate instructions.
7194 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007195 for (;;)
7196 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007197 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007198 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007199 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007200 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007201
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007202 // Bail out on the first error to avoid a flood of errors and report
7203 // the right line number when inside try/catch.
7204 if (emsg_before != called_emsg)
7205 goto erret;
7206
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007207 if (line != NULL && *line == '|')
7208 // the line continues after a '|'
7209 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007210 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007211 && !(*line == '#' && (line == cctx.ctx_line_start
7212 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007213 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007214 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007215 goto erret;
7216 }
7217 else
7218 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007219 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007220 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007221 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007222 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007223 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007224 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225
Bram Moolenaara80faa82020-04-12 19:37:17 +02007226 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007227 ea.cmdlinep = &line;
7228 ea.cmd = skipwhite(line);
7229
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007230 // Some things can be recognized by the first character.
7231 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007233 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007234 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007235 if (ea.cmd[1] != '{')
7236 {
7237 line = (char_u *)"";
7238 continue;
7239 }
7240 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007241
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007242 case '}':
7243 {
7244 // "}" ends a block scope
7245 scopetype_T stype = cctx.ctx_scope == NULL
7246 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007247
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007248 if (stype == BLOCK_SCOPE)
7249 {
7250 compile_endblock(&cctx);
7251 line = ea.cmd;
7252 }
7253 else
7254 {
7255 emsg(_("E1025: using } outside of a block scope"));
7256 goto erret;
7257 }
7258 if (line != NULL)
7259 line = skipwhite(ea.cmd + 1);
7260 continue;
7261 }
7262
7263 case '{':
7264 // "{" starts a block scope
7265 // "{'a': 1}->func() is something else
7266 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7267 {
7268 line = compile_block(ea.cmd, &cctx);
7269 continue;
7270 }
7271 break;
7272
7273 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007274 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007275 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007276 }
7277
7278 /*
7279 * COMMAND MODIFIERS
7280 */
7281 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7282 {
7283 if (errormsg != NULL)
7284 goto erret;
7285 // empty line or comment
7286 line = (char_u *)"";
7287 continue;
7288 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007289 // TODO: use modifiers in the command
7290 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02007291 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007292
7293 // Skip ":call" to get to the function name.
7294 if (checkforcmd(&ea.cmd, "call", 3))
7295 ea.cmd = skipwhite(ea.cmd);
7296
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007297 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007298 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007299 char_u *pskip;
7300
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007301 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007302 // find what follows.
7303 // Skip over "var.member", "var[idx]" and the like.
7304 // Also "&opt = val", "$ENV = val" and "@r = val".
7305 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007306 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007307 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007308 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007309 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007310 char_u *var_end;
7311 int oplen;
7312 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007313
Bram Moolenaar65821722020-08-02 18:58:54 +02007314 if (ea.cmd[0] == '@')
7315 var_end = ea.cmd + 2;
7316 else
7317 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007318 FNE_CHECK_START | FNE_INCL_BR);
7319 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007320 if (oplen > 0)
7321 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007322 size_t len = p - ea.cmd;
7323
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007324 // Recognize an assignment if we recognize the variable
7325 // name:
7326 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007327 // "local = expr" where "local" is a local var.
7328 // "script = expr" where "script" is a script-local var.
7329 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007330 // "&opt = expr"
7331 // "$ENV = expr"
7332 // "@r = expr"
7333 if (*ea.cmd == '&'
7334 || *ea.cmd == '$'
7335 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007336 || ((len) > 2 && ea.cmd[1] == ':')
7337 || lookup_local(ea.cmd, len, &cctx) != NULL
7338 || lookup_arg(ea.cmd, len, NULL, NULL,
7339 NULL, &cctx) == OK
7340 || lookup_script(ea.cmd, len) == OK
7341 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007342 {
7343 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007344 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007345 goto erret;
7346 continue;
7347 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007348 }
7349 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007350
7351 if (*ea.cmd == '[')
7352 {
7353 // [var, var] = expr
7354 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7355 if (line == NULL)
7356 goto erret;
7357 if (line != ea.cmd)
7358 continue;
7359 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007360 }
7361
7362 /*
7363 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007364 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007366 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007367 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007368 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007369 ea.cmd = skip_range(ea.cmd, NULL);
7370 if (ea.cmd > cmd && !starts_with_colon)
7371 {
7372 emsg(_(e_colon_required));
7373 goto erret;
7374 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007375 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007376 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007377 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007378 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379
7380 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7381 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007382 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007383 {
7384 line += STRLEN(line);
7385 continue;
7386 }
7387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007389 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007390 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007391 // CMD_let cannot happen, compile_assignment() above is used
7392 iemsg("Command from find_ex_command() not handled");
7393 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007394 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007395 }
7396
7397 p = skipwhite(p);
7398
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007399 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007400 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007401 && ea.cmdidx != CMD_elseif
7402 && ea.cmdidx != CMD_else
7403 && ea.cmdidx != CMD_endif)
7404 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007405 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007406 continue;
7407 }
7408
Bram Moolenaarefd88552020-06-18 20:50:10 +02007409 if (ea.cmdidx != CMD_elseif
7410 && ea.cmdidx != CMD_else
7411 && ea.cmdidx != CMD_endif
7412 && ea.cmdidx != CMD_endfor
7413 && ea.cmdidx != CMD_endwhile
7414 && ea.cmdidx != CMD_catch
7415 && ea.cmdidx != CMD_finally
7416 && ea.cmdidx != CMD_endtry)
7417 {
7418 if (cctx.ctx_had_return)
7419 {
7420 emsg(_("E1095: Unreachable code after :return"));
7421 goto erret;
7422 }
7423 }
7424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007425 switch (ea.cmdidx)
7426 {
7427 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007428 ea.arg = p;
7429 line = compile_nested_function(&ea, &cctx);
7430 break;
7431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007433 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007434 goto erret;
7435
7436 case CMD_return:
7437 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007438 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 break;
7440
7441 case CMD_let:
7442 case CMD_const:
7443 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007444 if (line == p)
7445 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446 break;
7447
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007448 case CMD_unlet:
7449 case CMD_unlockvar:
7450 case CMD_lockvar:
7451 line = compile_unletlock(p, &ea, &cctx);
7452 break;
7453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007454 case CMD_import:
7455 line = compile_import(p, &cctx);
7456 break;
7457
7458 case CMD_if:
7459 line = compile_if(p, &cctx);
7460 break;
7461 case CMD_elseif:
7462 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007463 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464 break;
7465 case CMD_else:
7466 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007467 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468 break;
7469 case CMD_endif:
7470 line = compile_endif(p, &cctx);
7471 break;
7472
7473 case CMD_while:
7474 line = compile_while(p, &cctx);
7475 break;
7476 case CMD_endwhile:
7477 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007478 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007479 break;
7480
7481 case CMD_for:
7482 line = compile_for(p, &cctx);
7483 break;
7484 case CMD_endfor:
7485 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007486 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487 break;
7488 case CMD_continue:
7489 line = compile_continue(p, &cctx);
7490 break;
7491 case CMD_break:
7492 line = compile_break(p, &cctx);
7493 break;
7494
7495 case CMD_try:
7496 line = compile_try(p, &cctx);
7497 break;
7498 case CMD_catch:
7499 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007500 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007501 break;
7502 case CMD_finally:
7503 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007504 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007505 break;
7506 case CMD_endtry:
7507 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007508 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007509 break;
7510 case CMD_throw:
7511 line = compile_throw(p, &cctx);
7512 break;
7513
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007514 case CMD_eval:
7515 if (compile_expr0(&p, &cctx) == FAIL)
7516 goto erret;
7517
7518 // drop the return value
7519 generate_instr_drop(&cctx, ISN_DROP, 1);
7520
7521 line = skipwhite(p);
7522 break;
7523
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007524 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007525 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007526 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007527 case CMD_echomsg:
7528 case CMD_echoerr:
7529 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007530 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007531
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007532 // TODO: other commands with an expression argument
7533
Bram Moolenaarae616492020-07-28 20:07:27 +02007534 case CMD_append:
7535 case CMD_change:
7536 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007537 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007538 case CMD_xit:
7539 not_in_vim9(&ea);
7540 goto erret;
7541
Bram Moolenaar002262f2020-07-08 17:47:57 +02007542 case CMD_SIZE:
7543 semsg(_("E476: Invalid command: %s"), ea.cmd);
7544 goto erret;
7545
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007546 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007547 // Not recognized, execute with do_cmdline_cmd().
7548 ea.arg = p;
7549 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007550 break;
7551 }
7552 if (line == NULL)
7553 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007554 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007555
7556 if (cctx.ctx_type_stack.ga_len < 0)
7557 {
7558 iemsg("Type stack underflow");
7559 goto erret;
7560 }
7561 }
7562
7563 if (cctx.ctx_scope != NULL)
7564 {
7565 if (cctx.ctx_scope->se_type == IF_SCOPE)
7566 emsg(_(e_endif));
7567 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7568 emsg(_(e_endwhile));
7569 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7570 emsg(_(e_endfor));
7571 else
7572 emsg(_("E1026: Missing }"));
7573 goto erret;
7574 }
7575
Bram Moolenaarefd88552020-06-18 20:50:10 +02007576 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007577 {
7578 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7579 {
7580 emsg(_("E1027: Missing return statement"));
7581 goto erret;
7582 }
7583
7584 // Return zero if there is no return at the end.
7585 generate_PUSHNR(&cctx, 0);
7586 generate_instr(&cctx, ISN_RETURN);
7587 }
7588
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007589 {
7590 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7591 + ufunc->uf_dfunc_idx;
7592 dfunc->df_deleted = FALSE;
7593 dfunc->df_instr = instr->ga_data;
7594 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007595 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007596 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007597 if (cctx.ctx_outer_used)
7598 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007599 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007600 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007601
7602 ret = OK;
7603
7604erret:
7605 if (ret == FAIL)
7606 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007607 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007608 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7609 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007610
7611 for (idx = 0; idx < instr->ga_len; ++idx)
7612 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007613 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007614
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007615 // If using the last entry in the table and it was added above, we
7616 // might as well remove it.
7617 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007618 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007619 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007620 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007621 ufunc->uf_dfunc_idx = 0;
7622 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007623 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007624
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007625 while (cctx.ctx_scope != NULL)
7626 drop_scope(&cctx);
7627
Bram Moolenaar20431c92020-03-20 18:39:46 +01007628 // Don't execute this function body.
7629 ga_clear_strings(&ufunc->uf_lines);
7630
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007631 if (errormsg != NULL)
7632 emsg(errormsg);
7633 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007634 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007635 }
7636
7637 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007638 if (do_estack_push)
7639 estack_pop();
7640
Bram Moolenaar20431c92020-03-20 18:39:46 +01007641 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007642 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007643 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007644 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007645}
7646
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007647 void
7648set_function_type(ufunc_T *ufunc)
7649{
7650 int varargs = ufunc->uf_va_name != NULL;
7651 int argcount = ufunc->uf_args.ga_len;
7652
7653 // Create a type for the function, with the return type and any
7654 // argument types.
7655 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7656 // The type is included in "tt_args".
7657 if (argcount > 0 || varargs)
7658 {
7659 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7660 argcount, &ufunc->uf_type_list);
7661 // Add argument types to the function type.
7662 if (func_type_add_arg_types(ufunc->uf_func_type,
7663 argcount + varargs,
7664 &ufunc->uf_type_list) == FAIL)
7665 return;
7666 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7667 ufunc->uf_func_type->tt_min_argcount =
7668 argcount - ufunc->uf_def_args.ga_len;
7669 if (ufunc->uf_arg_types == NULL)
7670 {
7671 int i;
7672
7673 // lambda does not have argument types.
7674 for (i = 0; i < argcount; ++i)
7675 ufunc->uf_func_type->tt_args[i] = &t_any;
7676 }
7677 else
7678 mch_memmove(ufunc->uf_func_type->tt_args,
7679 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7680 if (varargs)
7681 {
7682 ufunc->uf_func_type->tt_args[argcount] =
7683 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7684 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7685 }
7686 }
7687 else
7688 // No arguments, can use a predefined type.
7689 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7690 argcount, &ufunc->uf_type_list);
7691}
7692
7693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007694/*
7695 * Delete an instruction, free what it contains.
7696 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007697 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007698delete_instr(isn_T *isn)
7699{
7700 switch (isn->isn_type)
7701 {
7702 case ISN_EXEC:
7703 case ISN_LOADENV:
7704 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007705 case ISN_LOADB:
7706 case ISN_LOADW:
7707 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007708 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007709 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007710 case ISN_PUSHEXC:
7711 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007712 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007714 case ISN_STOREB:
7715 case ISN_STOREW:
7716 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007717 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007718 vim_free(isn->isn_arg.string);
7719 break;
7720
7721 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007722 case ISN_STORES:
7723 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007724 break;
7725
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007726 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007727 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007728 vim_free(isn->isn_arg.unlet.ul_name);
7729 break;
7730
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007731 case ISN_STOREOPT:
7732 vim_free(isn->isn_arg.storeopt.so_name);
7733 break;
7734
7735 case ISN_PUSHBLOB: // push blob isn_arg.blob
7736 blob_unref(isn->isn_arg.blob);
7737 break;
7738
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007739 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007740#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007741 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007742#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007743 break;
7744
7745 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007746#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007747 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007748#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007749 break;
7750
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007751 case ISN_UCALL:
7752 vim_free(isn->isn_arg.ufunc.cuf_name);
7753 break;
7754
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007755 case ISN_FUNCREF:
7756 {
7757 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7758 + isn->isn_arg.funcref.fr_func;
7759 func_ptr_unref(dfunc->df_ufunc);
7760 }
7761 break;
7762
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007763 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007764 {
7765 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7766 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7767
7768 if (ufunc != NULL)
7769 {
7770 // Clear uf_dfunc_idx so that the function is deleted.
7771 clear_def_function(ufunc);
7772 ufunc->uf_dfunc_idx = 0;
7773 func_ptr_unref(ufunc);
7774 }
7775
7776 vim_free(lambda);
7777 vim_free(isn->isn_arg.newfunc.nf_global);
7778 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007779 break;
7780
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007781 case ISN_2BOOL:
7782 case ISN_2STRING:
7783 case ISN_ADDBLOB:
7784 case ISN_ADDLIST:
7785 case ISN_BCALL:
7786 case ISN_CATCH:
7787 case ISN_CHECKNR:
7788 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007789 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007790 case ISN_COMPAREANY:
7791 case ISN_COMPAREBLOB:
7792 case ISN_COMPAREBOOL:
7793 case ISN_COMPAREDICT:
7794 case ISN_COMPAREFLOAT:
7795 case ISN_COMPAREFUNC:
7796 case ISN_COMPARELIST:
7797 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007798 case ISN_COMPARESPECIAL:
7799 case ISN_COMPARESTRING:
7800 case ISN_CONCAT:
7801 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007802 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803 case ISN_DROP:
7804 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007805 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007806 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007807 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007808 case ISN_EXECCONCAT:
7809 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007810 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007811 case ISN_LISTINDEX:
7812 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007813 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007814 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007815 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007816 case ISN_JUMP:
7817 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007818 case ISN_LOADBDICT:
7819 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007820 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007821 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007822 case ISN_LOADSCRIPT:
7823 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007824 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007825 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007826 case ISN_NEGATENR:
7827 case ISN_NEWDICT:
7828 case ISN_NEWLIST:
7829 case ISN_OPNR:
7830 case ISN_OPFLOAT:
7831 case ISN_OPANY:
7832 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007833 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007834 case ISN_PUSHF:
7835 case ISN_PUSHNR:
7836 case ISN_PUSHBOOL:
7837 case ISN_PUSHSPEC:
7838 case ISN_RETURN:
7839 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007840 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007841 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007842 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007843 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007844 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007845 case ISN_STOREDICT:
7846 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007847 case ISN_THROW:
7848 case ISN_TRY:
7849 // nothing allocated
7850 break;
7851 }
7852}
7853
7854/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007855 * Free all instructions for "dfunc".
7856 */
7857 static void
7858delete_def_function_contents(dfunc_T *dfunc)
7859{
7860 int idx;
7861
7862 ga_clear(&dfunc->df_def_args_isn);
7863
7864 if (dfunc->df_instr != NULL)
7865 {
7866 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7867 delete_instr(dfunc->df_instr + idx);
7868 VIM_CLEAR(dfunc->df_instr);
7869 }
7870
7871 dfunc->df_deleted = TRUE;
7872}
7873
7874/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007875 * When a user function is deleted, clear the contents of any associated def
7876 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007877 */
7878 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007879clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007880{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007881 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007882 {
7883 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7884 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007885
Bram Moolenaar20431c92020-03-20 18:39:46 +01007886 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007887 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007888 }
7889}
7890
7891#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007892/*
7893 * Free all functions defined with ":def".
7894 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007895 void
7896free_def_functions(void)
7897{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007898 int idx;
7899
7900 for (idx = 0; idx < def_functions.ga_len; ++idx)
7901 {
7902 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7903
7904 delete_def_function_contents(dfunc);
7905 }
7906
7907 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007908}
7909#endif
7910
7911
7912#endif // FEAT_EVAL