blob: e87634b31bab524c4da286573ee69369a65d23f1 [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
779/*
780 * Generate an instruction with two arguments. The instruction depends on the
781 * type of the arguments.
782 */
783 static int
784generate_two_op(cctx_T *cctx, char_u *op)
785{
786 garray_T *stack = &cctx->ctx_type_stack;
787 type_T *type1;
788 type_T *type2;
789 vartype_T vartype;
790 isn_T *isn;
791
Bram Moolenaar080457c2020-03-03 21:53:32 +0100792 RETURN_OK_IF_SKIP(cctx);
793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100794 // Get the known type of the two items on the stack. If they are matching
795 // use a type-specific instruction. Otherwise fall back to runtime type
796 // checking.
797 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
798 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar4c683752020-04-05 21:38:23 +0200799 vartype = VAR_ANY;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 if (type1->tt_type == type2->tt_type
801 && (type1->tt_type == VAR_NUMBER
802 || type1->tt_type == VAR_LIST
803#ifdef FEAT_FLOAT
804 || type1->tt_type == VAR_FLOAT
805#endif
806 || type1->tt_type == VAR_BLOB))
807 vartype = type1->tt_type;
808
809 switch (*op)
810 {
811 case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
Bram Moolenaar4c683752020-04-05 21:38:23 +0200812 && type1->tt_type != VAR_ANY
813 && type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100814 && check_number_or_float(
815 type1->tt_type, type2->tt_type, op) == FAIL)
816 return FAIL;
817 isn = generate_instr_drop(cctx,
818 vartype == VAR_NUMBER ? ISN_OPNR
819 : vartype == VAR_LIST ? ISN_ADDLIST
820 : vartype == VAR_BLOB ? ISN_ADDBLOB
821#ifdef FEAT_FLOAT
822 : vartype == VAR_FLOAT ? ISN_OPFLOAT
823#endif
824 : ISN_OPANY, 1);
825 if (isn != NULL)
826 isn->isn_arg.op.op_type = EXPR_ADD;
827 break;
828
829 case '-':
830 case '*':
831 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
832 op) == FAIL)
833 return FAIL;
834 if (vartype == VAR_NUMBER)
835 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
836#ifdef FEAT_FLOAT
837 else if (vartype == VAR_FLOAT)
838 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
839#endif
840 else
841 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
842 if (isn != NULL)
843 isn->isn_arg.op.op_type = *op == '*'
844 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
845 break;
846
Bram Moolenaar4c683752020-04-05 21:38:23 +0200847 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200849 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100850 && type2->tt_type != VAR_NUMBER))
851 {
852 emsg(_("E1035: % requires number arguments"));
853 return FAIL;
854 }
855 isn = generate_instr_drop(cctx,
856 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
857 if (isn != NULL)
858 isn->isn_arg.op.op_type = EXPR_REM;
859 break;
860 }
861
862 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200863 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864 {
865 type_T *type = &t_any;
866
867#ifdef FEAT_FLOAT
868 // float+number and number+float results in float
869 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
870 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
871 type = &t_float;
872#endif
873 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
874 }
875
876 return OK;
877}
878
879/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200880 * Get the instruction to use for comparing "type1" with "type2"
881 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200883 static isntype_T
884get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885{
886 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887
Bram Moolenaar4c683752020-04-05 21:38:23 +0200888 if (type1 == VAR_UNKNOWN)
889 type1 = VAR_ANY;
890 if (type2 == VAR_UNKNOWN)
891 type2 = VAR_ANY;
892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893 if (type1 == type2)
894 {
895 switch (type1)
896 {
897 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
898 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
899 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
900 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
901 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
902 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
903 case VAR_LIST: isntype = ISN_COMPARELIST; break;
904 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
905 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906 default: isntype = ISN_COMPAREANY; break;
907 }
908 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200909 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100910 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
911 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
912 isntype = ISN_COMPAREANY;
913
914 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
915 && (isntype == ISN_COMPAREBOOL
916 || isntype == ISN_COMPARESPECIAL
917 || isntype == ISN_COMPARENR
918 || isntype == ISN_COMPAREFLOAT))
919 {
920 semsg(_("E1037: Cannot use \"%s\" with %s"),
921 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200922 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923 }
924 if (isntype == ISN_DROP
925 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
926 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
927 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
928 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
929 && exptype != EXPR_IS && exptype != EXPR_ISNOT
930 && (type1 == VAR_BLOB || type2 == VAR_BLOB
931 || type1 == VAR_LIST || type2 == VAR_LIST))))
932 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100933 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200935 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200937 return isntype;
938}
939
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200940 int
941check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
942{
943 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
944 return FAIL;
945 return OK;
946}
947
Bram Moolenaara5565e42020-05-09 15:44:01 +0200948/*
949 * Generate an ISN_COMPARE* instruction with a boolean result.
950 */
951 static int
952generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
953{
954 isntype_T isntype;
955 isn_T *isn;
956 garray_T *stack = &cctx->ctx_type_stack;
957 vartype_T type1;
958 vartype_T type2;
959
960 RETURN_OK_IF_SKIP(cctx);
961
962 // Get the known type of the two items on the stack. If they are matching
963 // use a type-specific instruction. Otherwise fall back to runtime type
964 // checking.
965 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
966 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
967 isntype = get_compare_isn(exptype, type1, type2);
968 if (isntype == ISN_DROP)
969 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970
971 if ((isn = generate_instr(cctx, isntype)) == NULL)
972 return FAIL;
973 isn->isn_arg.op.op_type = exptype;
974 isn->isn_arg.op.op_ic = ic;
975
976 // takes two arguments, puts one bool back
977 if (stack->ga_len >= 2)
978 {
979 --stack->ga_len;
980 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
981 }
982
983 return OK;
984}
985
986/*
987 * Generate an ISN_2BOOL instruction.
988 */
989 static int
990generate_2BOOL(cctx_T *cctx, int invert)
991{
992 isn_T *isn;
993 garray_T *stack = &cctx->ctx_type_stack;
994
Bram Moolenaar080457c2020-03-03 21:53:32 +0100995 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
997 return FAIL;
998 isn->isn_arg.number = invert;
999
1000 // type becomes bool
1001 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
1002
1003 return OK;
1004}
1005
1006 static int
1007generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
1008{
1009 isn_T *isn;
1010 garray_T *stack = &cctx->ctx_type_stack;
1011
Bram Moolenaar080457c2020-03-03 21:53:32 +01001012 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
1014 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +02001015 // TODO: whole type, e.g. for a function also arg and return types
1016 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001017 isn->isn_arg.type.ct_off = offset;
1018
1019 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001020 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001021
1022 return OK;
1023}
1024
1025/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001026 * Check that
1027 * - "actual" is "expected" type or
1028 * - "actual" is a type that can be "expected" type: add a runtime check; or
1029 * - return FAIL.
1030 */
1031 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001032need_type(
1033 type_T *actual,
1034 type_T *expected,
1035 int offset,
1036 cctx_T *cctx,
1037 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001038{
1039 if (check_type(expected, actual, FALSE) == OK)
1040 return OK;
1041 if (actual->tt_type != VAR_ANY
1042 && actual->tt_type != VAR_UNKNOWN
1043 && !(actual->tt_type == VAR_FUNC
1044 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
1045 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001046 if (!silent)
1047 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001048 return FAIL;
1049 }
1050 generate_TYPECHECK(cctx, expected, offset);
1051 return OK;
1052}
1053
1054/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 * Generate an ISN_PUSHNR instruction.
1056 */
1057 static int
1058generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1059{
1060 isn_T *isn;
1061
Bram Moolenaar080457c2020-03-03 21:53:32 +01001062 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1064 return FAIL;
1065 isn->isn_arg.number = number;
1066
1067 return OK;
1068}
1069
1070/*
1071 * Generate an ISN_PUSHBOOL instruction.
1072 */
1073 static int
1074generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1075{
1076 isn_T *isn;
1077
Bram Moolenaar080457c2020-03-03 21:53:32 +01001078 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001079 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1080 return FAIL;
1081 isn->isn_arg.number = number;
1082
1083 return OK;
1084}
1085
1086/*
1087 * Generate an ISN_PUSHSPEC instruction.
1088 */
1089 static int
1090generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1091{
1092 isn_T *isn;
1093
Bram Moolenaar080457c2020-03-03 21:53:32 +01001094 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001095 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1096 return FAIL;
1097 isn->isn_arg.number = number;
1098
1099 return OK;
1100}
1101
1102#ifdef FEAT_FLOAT
1103/*
1104 * Generate an ISN_PUSHF instruction.
1105 */
1106 static int
1107generate_PUSHF(cctx_T *cctx, float_T fnumber)
1108{
1109 isn_T *isn;
1110
Bram Moolenaar080457c2020-03-03 21:53:32 +01001111 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1113 return FAIL;
1114 isn->isn_arg.fnumber = fnumber;
1115
1116 return OK;
1117}
1118#endif
1119
1120/*
1121 * Generate an ISN_PUSHS instruction.
1122 * Consumes "str".
1123 */
1124 static int
1125generate_PUSHS(cctx_T *cctx, char_u *str)
1126{
1127 isn_T *isn;
1128
Bram Moolenaar080457c2020-03-03 21:53:32 +01001129 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1131 return FAIL;
1132 isn->isn_arg.string = str;
1133
1134 return OK;
1135}
1136
1137/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001138 * Generate an ISN_PUSHCHANNEL instruction.
1139 * Consumes "channel".
1140 */
1141 static int
1142generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1143{
1144 isn_T *isn;
1145
Bram Moolenaar080457c2020-03-03 21:53:32 +01001146 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001147 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1148 return FAIL;
1149 isn->isn_arg.channel = channel;
1150
1151 return OK;
1152}
1153
1154/*
1155 * Generate an ISN_PUSHJOB instruction.
1156 * Consumes "job".
1157 */
1158 static int
1159generate_PUSHJOB(cctx_T *cctx, job_T *job)
1160{
1161 isn_T *isn;
1162
Bram Moolenaar080457c2020-03-03 21:53:32 +01001163 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001164 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001165 return FAIL;
1166 isn->isn_arg.job = job;
1167
1168 return OK;
1169}
1170
1171/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172 * Generate an ISN_PUSHBLOB instruction.
1173 * Consumes "blob".
1174 */
1175 static int
1176generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1177{
1178 isn_T *isn;
1179
Bram Moolenaar080457c2020-03-03 21:53:32 +01001180 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1182 return FAIL;
1183 isn->isn_arg.blob = blob;
1184
1185 return OK;
1186}
1187
1188/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001189 * Generate an ISN_PUSHFUNC instruction with name "name".
1190 * Consumes "name".
1191 */
1192 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001193generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001194{
1195 isn_T *isn;
1196
Bram Moolenaar080457c2020-03-03 21:53:32 +01001197 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001198 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001199 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001200 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001201
1202 return OK;
1203}
1204
1205/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001206 * Generate an ISN_GETITEM instruction with "index".
1207 */
1208 static int
1209generate_GETITEM(cctx_T *cctx, int index)
1210{
1211 isn_T *isn;
1212 garray_T *stack = &cctx->ctx_type_stack;
1213 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1214 type_T *item_type = &t_any;
1215
1216 RETURN_OK_IF_SKIP(cctx);
1217
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001218 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001219 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001220 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001221 emsg(_(e_listreq));
1222 return FAIL;
1223 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001224 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001225 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1226 return FAIL;
1227 isn->isn_arg.number = index;
1228
1229 // add the item type to the type stack
1230 if (ga_grow(stack, 1) == FAIL)
1231 return FAIL;
1232 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1233 ++stack->ga_len;
1234 return OK;
1235}
1236
1237/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001238 * Generate an ISN_SLICE instruction with "count".
1239 */
1240 static int
1241generate_SLICE(cctx_T *cctx, int count)
1242{
1243 isn_T *isn;
1244
1245 RETURN_OK_IF_SKIP(cctx);
1246 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1247 return FAIL;
1248 isn->isn_arg.number = count;
1249 return OK;
1250}
1251
1252/*
1253 * Generate an ISN_CHECKLEN instruction with "min_len".
1254 */
1255 static int
1256generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1257{
1258 isn_T *isn;
1259
1260 RETURN_OK_IF_SKIP(cctx);
1261
1262 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1263 return FAIL;
1264 isn->isn_arg.checklen.cl_min_len = min_len;
1265 isn->isn_arg.checklen.cl_more_OK = more_OK;
1266
1267 return OK;
1268}
1269
1270/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 * Generate an ISN_STORE instruction.
1272 */
1273 static int
1274generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1275{
1276 isn_T *isn;
1277
Bram Moolenaar080457c2020-03-03 21:53:32 +01001278 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1280 return FAIL;
1281 if (name != NULL)
1282 isn->isn_arg.string = vim_strsave(name);
1283 else
1284 isn->isn_arg.number = idx;
1285
1286 return OK;
1287}
1288
1289/*
1290 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1291 */
1292 static int
1293generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1294{
1295 isn_T *isn;
1296
Bram Moolenaar080457c2020-03-03 21:53:32 +01001297 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1299 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001300 isn->isn_arg.storenr.stnr_idx = idx;
1301 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001302
1303 return OK;
1304}
1305
1306/*
1307 * Generate an ISN_STOREOPT instruction
1308 */
1309 static int
1310generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1311{
1312 isn_T *isn;
1313
Bram Moolenaar080457c2020-03-03 21:53:32 +01001314 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
1316 return FAIL;
1317 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1318 isn->isn_arg.storeopt.so_flags = opt_flags;
1319
1320 return OK;
1321}
1322
1323/*
1324 * Generate an ISN_LOAD or similar instruction.
1325 */
1326 static int
1327generate_LOAD(
1328 cctx_T *cctx,
1329 isntype_T isn_type,
1330 int idx,
1331 char_u *name,
1332 type_T *type)
1333{
1334 isn_T *isn;
1335
Bram Moolenaar080457c2020-03-03 21:53:32 +01001336 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1338 return FAIL;
1339 if (name != NULL)
1340 isn->isn_arg.string = vim_strsave(name);
1341 else
1342 isn->isn_arg.number = idx;
1343
1344 return OK;
1345}
1346
1347/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001348 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001349 */
1350 static int
1351generate_LOADV(
1352 cctx_T *cctx,
1353 char_u *name,
1354 int error)
1355{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001356 int di_flags;
1357 int vidx = find_vim_var(name, &di_flags);
1358 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001359
Bram Moolenaar080457c2020-03-03 21:53:32 +01001360 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001361 if (vidx < 0)
1362 {
1363 if (error)
1364 semsg(_(e_var_notfound), name);
1365 return FAIL;
1366 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001367 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001368
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001369 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001370}
1371
1372/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001373 * Generate an ISN_UNLET instruction.
1374 */
1375 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001376generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001377{
1378 isn_T *isn;
1379
1380 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001381 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001382 return FAIL;
1383 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1384 isn->isn_arg.unlet.ul_forceit = forceit;
1385
1386 return OK;
1387}
1388
1389/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390 * Generate an ISN_LOADS instruction.
1391 */
1392 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001393generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001395 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001397 int sid,
1398 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399{
1400 isn_T *isn;
1401
Bram Moolenaar080457c2020-03-03 21:53:32 +01001402 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001403 if (isn_type == ISN_LOADS)
1404 isn = generate_instr_type(cctx, isn_type, type);
1405 else
1406 isn = generate_instr_drop(cctx, isn_type, 1);
1407 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001409 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1410 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411
1412 return OK;
1413}
1414
1415/*
1416 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1417 */
1418 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001419generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 cctx_T *cctx,
1421 isntype_T isn_type,
1422 int sid,
1423 int idx,
1424 type_T *type)
1425{
1426 isn_T *isn;
1427
Bram Moolenaar080457c2020-03-03 21:53:32 +01001428 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 if (isn_type == ISN_LOADSCRIPT)
1430 isn = generate_instr_type(cctx, isn_type, type);
1431 else
1432 isn = generate_instr_drop(cctx, isn_type, 1);
1433 if (isn == NULL)
1434 return FAIL;
1435 isn->isn_arg.script.script_sid = sid;
1436 isn->isn_arg.script.script_idx = idx;
1437 return OK;
1438}
1439
1440/*
1441 * Generate an ISN_NEWLIST instruction.
1442 */
1443 static int
1444generate_NEWLIST(cctx_T *cctx, int count)
1445{
1446 isn_T *isn;
1447 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448 type_T *type;
1449 type_T *member;
1450
Bram Moolenaar080457c2020-03-03 21:53:32 +01001451 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001452 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1453 return FAIL;
1454 isn->isn_arg.number = count;
1455
1456 // drop the value types
1457 stack->ga_len -= count;
1458
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001459 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001460 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 if (count > 0)
1462 member = ((type_T **)stack->ga_data)[stack->ga_len];
1463 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001464 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001465 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466
1467 // add the list type to the type stack
1468 if (ga_grow(stack, 1) == FAIL)
1469 return FAIL;
1470 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1471 ++stack->ga_len;
1472
1473 return OK;
1474}
1475
1476/*
1477 * Generate an ISN_NEWDICT instruction.
1478 */
1479 static int
1480generate_NEWDICT(cctx_T *cctx, int count)
1481{
1482 isn_T *isn;
1483 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001484 type_T *type;
1485 type_T *member;
1486
Bram Moolenaar080457c2020-03-03 21:53:32 +01001487 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1489 return FAIL;
1490 isn->isn_arg.number = count;
1491
1492 // drop the key and value types
1493 stack->ga_len -= 2 * count;
1494
Bram Moolenaar436472f2020-02-20 22:54:43 +01001495 // Use the first value type for the list member type. Use "void" for an
1496 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497 if (count > 0)
1498 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1499 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001500 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001501 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502
1503 // add the dict type to the type stack
1504 if (ga_grow(stack, 1) == FAIL)
1505 return FAIL;
1506 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1507 ++stack->ga_len;
1508
1509 return OK;
1510}
1511
1512/*
1513 * Generate an ISN_FUNCREF instruction.
1514 */
1515 static int
1516generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
1517{
1518 isn_T *isn;
1519 garray_T *stack = &cctx->ctx_type_stack;
1520
Bram Moolenaar080457c2020-03-03 21:53:32 +01001521 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1523 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001524 isn->isn_arg.funcref.fr_func = dfunc_idx;
1525 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526
1527 if (ga_grow(stack, 1) == FAIL)
1528 return FAIL;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001529 ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 // TODO: argument and return types
1531 ++stack->ga_len;
1532
1533 return OK;
1534}
1535
1536/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001537 * Generate an ISN_NEWFUNC instruction.
1538 */
1539 static int
1540generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1541{
1542 isn_T *isn;
1543 char_u *name;
1544
1545 RETURN_OK_IF_SKIP(cctx);
1546 name = vim_strsave(lambda_name);
1547 if (name == NULL)
1548 return FAIL;
1549 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1550 return FAIL;
1551 isn->isn_arg.newfunc.nf_lambda = name;
1552 isn->isn_arg.newfunc.nf_global = func_name;
1553
1554 return OK;
1555}
1556
1557/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 * Generate an ISN_JUMP instruction.
1559 */
1560 static int
1561generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1562{
1563 isn_T *isn;
1564 garray_T *stack = &cctx->ctx_type_stack;
1565
Bram Moolenaar080457c2020-03-03 21:53:32 +01001566 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001567 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1568 return FAIL;
1569 isn->isn_arg.jump.jump_when = when;
1570 isn->isn_arg.jump.jump_where = where;
1571
1572 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1573 --stack->ga_len;
1574
1575 return OK;
1576}
1577
1578 static int
1579generate_FOR(cctx_T *cctx, int loop_idx)
1580{
1581 isn_T *isn;
1582 garray_T *stack = &cctx->ctx_type_stack;
1583
Bram Moolenaar080457c2020-03-03 21:53:32 +01001584 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1586 return FAIL;
1587 isn->isn_arg.forloop.for_idx = loop_idx;
1588
1589 if (ga_grow(stack, 1) == FAIL)
1590 return FAIL;
1591 // type doesn't matter, will be stored next
1592 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1593 ++stack->ga_len;
1594
1595 return OK;
1596}
1597
1598/*
1599 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001600 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 * Return FAIL if the number of arguments is wrong.
1602 */
1603 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001604generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605{
1606 isn_T *isn;
1607 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001608 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001609 type_T *argtypes[MAX_FUNC_ARGS];
1610 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611
Bram Moolenaar080457c2020-03-03 21:53:32 +01001612 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001613 argoff = check_internal_func(func_idx, argcount);
1614 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 return FAIL;
1616
Bram Moolenaar389df252020-07-09 21:20:47 +02001617 if (method_call && argoff > 1)
1618 {
1619 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1620 return FAIL;
1621 isn->isn_arg.shuffle.shfl_item = argcount;
1622 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1623 }
1624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1626 return FAIL;
1627 isn->isn_arg.bfunc.cbf_idx = func_idx;
1628 isn->isn_arg.bfunc.cbf_argcount = argcount;
1629
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001630 for (i = 0; i < argcount; ++i)
1631 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1632
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633 stack->ga_len -= argcount; // drop the arguments
1634 if (ga_grow(stack, 1) == FAIL)
1635 return FAIL;
1636 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001637 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638 ++stack->ga_len; // add return value
1639
1640 return OK;
1641}
1642
1643/*
1644 * Generate an ISN_DCALL or ISN_UCALL instruction.
1645 * Return FAIL if the number of arguments is wrong.
1646 */
1647 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001648generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649{
1650 isn_T *isn;
1651 garray_T *stack = &cctx->ctx_type_stack;
1652 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001653 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654
Bram Moolenaar080457c2020-03-03 21:53:32 +01001655 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656 if (argcount > regular_args && !has_varargs(ufunc))
1657 {
1658 semsg(_(e_toomanyarg), ufunc->uf_name);
1659 return FAIL;
1660 }
1661 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1662 {
1663 semsg(_(e_toofewarg), ufunc->uf_name);
1664 return FAIL;
1665 }
1666
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001667 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001668 {
1669 int i;
1670
1671 for (i = 0; i < argcount; ++i)
1672 {
1673 type_T *expected;
1674 type_T *actual;
1675
1676 if (i < regular_args)
1677 {
1678 if (ufunc->uf_arg_types == NULL)
1679 continue;
1680 expected = ufunc->uf_arg_types[i];
1681 }
1682 else
1683 expected = ufunc->uf_va_type->tt_member;
1684 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001685 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001686 {
1687 arg_type_mismatch(expected, actual, i + 1);
1688 return FAIL;
1689 }
1690 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001691 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar45a15082020-05-25 00:28:33 +02001692 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001693 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001694 }
1695
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001696 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001697 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001698 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001700 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 {
1702 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1703 isn->isn_arg.dfunc.cdf_argcount = argcount;
1704 }
1705 else
1706 {
1707 // A user function may be deleted and redefined later, can't use the
1708 // ufunc pointer, need to look it up again at runtime.
1709 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1710 isn->isn_arg.ufunc.cuf_argcount = argcount;
1711 }
1712
1713 stack->ga_len -= argcount; // drop the arguments
1714 if (ga_grow(stack, 1) == FAIL)
1715 return FAIL;
1716 // add return value
1717 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1718 ++stack->ga_len;
1719
1720 return OK;
1721}
1722
1723/*
1724 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1725 */
1726 static int
1727generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1728{
1729 isn_T *isn;
1730 garray_T *stack = &cctx->ctx_type_stack;
1731
Bram Moolenaar080457c2020-03-03 21:53:32 +01001732 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1734 return FAIL;
1735 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1736 isn->isn_arg.ufunc.cuf_argcount = argcount;
1737
1738 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001739 if (ga_grow(stack, 1) == FAIL)
1740 return FAIL;
1741 // add return value
1742 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1743 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744
1745 return OK;
1746}
1747
1748/*
1749 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001750 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 */
1752 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001753generate_PCALL(
1754 cctx_T *cctx,
1755 int argcount,
1756 char_u *name,
1757 type_T *type,
1758 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001759{
1760 isn_T *isn;
1761 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001762 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763
Bram Moolenaar080457c2020-03-03 21:53:32 +01001764 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001765
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001766 if (type->tt_type == VAR_ANY)
1767 ret_type = &t_any;
1768 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001769 {
1770 if (type->tt_argcount != -1)
1771 {
1772 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1773
1774 if (argcount < type->tt_min_argcount - varargs)
1775 {
1776 semsg(_(e_toofewarg), "[reference]");
1777 return FAIL;
1778 }
1779 if (!varargs && argcount > type->tt_argcount)
1780 {
1781 semsg(_(e_toomanyarg), "[reference]");
1782 return FAIL;
1783 }
1784 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001785 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001786 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001787 else
1788 {
1789 semsg(_("E1085: Not a callable type: %s"), name);
1790 return FAIL;
1791 }
1792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1794 return FAIL;
1795 isn->isn_arg.pfunc.cpf_top = at_top;
1796 isn->isn_arg.pfunc.cpf_argcount = argcount;
1797
1798 stack->ga_len -= argcount; // drop the arguments
1799
1800 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001801 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001803 // If partial is above the arguments it must be cleared and replaced with
1804 // the return value.
1805 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1806 return FAIL;
1807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 return OK;
1809}
1810
1811/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001812 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813 */
1814 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001815generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816{
1817 isn_T *isn;
1818 garray_T *stack = &cctx->ctx_type_stack;
1819 type_T *type;
1820
Bram Moolenaar080457c2020-03-03 21:53:32 +01001821 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001822 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001823 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001824 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001826 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001828 if (type->tt_type != VAR_DICT && type != &t_any)
1829 {
1830 emsg(_(e_dictreq));
1831 return FAIL;
1832 }
1833 // change dict type to dict member type
1834 if (type->tt_type == VAR_DICT)
1835 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836
1837 return OK;
1838}
1839
1840/*
1841 * Generate an ISN_ECHO instruction.
1842 */
1843 static int
1844generate_ECHO(cctx_T *cctx, int with_white, int count)
1845{
1846 isn_T *isn;
1847
Bram Moolenaar080457c2020-03-03 21:53:32 +01001848 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1850 return FAIL;
1851 isn->isn_arg.echo.echo_with_white = with_white;
1852 isn->isn_arg.echo.echo_count = count;
1853
1854 return OK;
1855}
1856
Bram Moolenaarad39c092020-02-26 18:23:43 +01001857/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001858 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001859 */
1860 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001861generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001862{
1863 isn_T *isn;
1864
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001865 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001866 return FAIL;
1867 isn->isn_arg.number = count;
1868
1869 return OK;
1870}
1871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872 static int
1873generate_EXEC(cctx_T *cctx, char_u *line)
1874{
1875 isn_T *isn;
1876
Bram Moolenaar080457c2020-03-03 21:53:32 +01001877 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001878 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1879 return FAIL;
1880 isn->isn_arg.string = vim_strsave(line);
1881 return OK;
1882}
1883
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001884 static int
1885generate_EXECCONCAT(cctx_T *cctx, int count)
1886{
1887 isn_T *isn;
1888
1889 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1890 return FAIL;
1891 isn->isn_arg.number = count;
1892 return OK;
1893}
1894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895/*
1896 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001897 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001899 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1901{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 lvar_T *lvar;
1903
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001904 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001906 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001907 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908 }
1909
1910 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001911 return NULL;
1912 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001913
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001914 // Every local variable uses the next entry on the stack. We could re-use
1915 // the last ones when leaving a scope, but then variables used in a closure
1916 // might get overwritten. To keep things simple do not re-use stack
1917 // entries. This is less efficient, but memory is cheap these days.
1918 lvar->lv_idx = cctx->ctx_locals_count++;
1919
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001920 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 lvar->lv_const = isConst;
1922 lvar->lv_type = type;
1923
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001924 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925}
1926
1927/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001928 * Remove local variables above "new_top".
1929 */
1930 static void
1931unwind_locals(cctx_T *cctx, int new_top)
1932{
1933 if (cctx->ctx_locals.ga_len > new_top)
1934 {
1935 int idx;
1936 lvar_T *lvar;
1937
1938 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1939 {
1940 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1941 vim_free(lvar->lv_name);
1942 }
1943 }
1944 cctx->ctx_locals.ga_len = new_top;
1945}
1946
1947/*
1948 * Free all local variables.
1949 */
1950 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001951free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001952{
1953 unwind_locals(cctx, 0);
1954 ga_clear(&cctx->ctx_locals);
1955}
1956
1957/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 * Skip over a type definition and return a pointer to just after it.
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001959 * When "optional" is TRUE then a leading "?" is accepted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 */
1961 char_u *
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001962skip_type(char_u *start, int optional)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963{
1964 char_u *p = start;
1965
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001966 if (optional && *p == '?')
1967 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 while (ASCII_ISALNUM(*p) || *p == '_')
1969 ++p;
1970
1971 // Skip over "<type>"; this is permissive about white space.
1972 if (*skipwhite(p) == '<')
1973 {
1974 p = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001975 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 p = skipwhite(p);
1977 if (*p == '>')
1978 ++p;
1979 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001980 else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1])))
1981 && STRNCMP("func", start, 4) == 0)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001982 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001983 if (*p == '(')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001984 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001985 // handle func(args): type
1986 ++p;
1987 while (*p != ')' && *p != NUL)
1988 {
1989 char_u *sp = p;
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02001990
Bram Moolenaarace61322020-07-26 18:16:58 +02001991 if (STRNCMP(p, "...", 3) == 0)
1992 p += 3;
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001993 p = skip_type(p, TRUE);
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02001994 if (p == sp)
1995 return p; // syntax error
1996 if (*p == ',')
1997 p = skipwhite(p + 1);
1998 }
1999 if (*p == ')')
2000 {
2001 if (p[1] == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02002002 p = skip_type(skipwhite(p + 2), FALSE);
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002003 else
Bram Moolenaarbfba8652020-07-23 20:09:10 +02002004 ++p;
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002005 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002006 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002007 else
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002008 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002009 // handle func: return_type
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02002010 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002011 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002012 }
2013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014 return p;
2015}
2016
2017/*
2018 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02002019 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 * Returns NULL in case of failure.
2021 */
2022 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02002023parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024{
2025 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002026 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027
2028 if (**arg != '<')
2029 {
2030 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02002031 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 else
2033 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01002034 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 }
2036 *arg = skipwhite(*arg + 1);
2037
Bram Moolenaard77a8522020-04-03 21:59:57 +02002038 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039
2040 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002041 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 {
2043 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01002044 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 }
2046 ++*arg;
2047
2048 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002049 return get_list_type(member_type, type_gap);
2050 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051}
2052
2053/*
2054 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02002055 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 */
2057 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02002058parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059{
2060 char_u *p = *arg;
2061 size_t len;
2062
2063 // skip over the first word
2064 while (ASCII_ISALNUM(*p) || *p == '_')
2065 ++p;
2066 len = p - *arg;
2067
2068 switch (**arg)
2069 {
2070 case 'a':
2071 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
2072 {
2073 *arg += len;
2074 return &t_any;
2075 }
2076 break;
2077 case 'b':
2078 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
2079 {
2080 *arg += len;
2081 return &t_bool;
2082 }
2083 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
2084 {
2085 *arg += len;
2086 return &t_blob;
2087 }
2088 break;
2089 case 'c':
2090 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
2091 {
2092 *arg += len;
2093 return &t_channel;
2094 }
2095 break;
2096 case 'd':
2097 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
2098 {
2099 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002100 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101 }
2102 break;
2103 case 'f':
2104 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
2105 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01002106#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 *arg += len;
2108 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01002109#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002110 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01002111 return &t_any;
2112#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 }
2114 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
2115 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002116 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002117 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002118 int argcount = -1;
2119 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002120 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002121 type_T *arg_type[MAX_FUNC_ARGS + 1];
2122
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002123 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002125 if (**arg == '(')
2126 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002127 // "func" may or may not return a value, "func()" does
2128 // not return a value.
2129 ret_type = &t_void;
2130
Bram Moolenaard77a8522020-04-03 21:59:57 +02002131 p = ++*arg;
2132 argcount = 0;
2133 while (*p != NUL && *p != ')')
2134 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002135 if (*p == '?')
2136 {
2137 if (first_optional == -1)
2138 first_optional = argcount;
2139 ++p;
2140 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002141 else if (STRNCMP(p, "...", 3) == 0)
2142 {
2143 flags |= TTFLAG_VARARGS;
2144 p += 3;
2145 }
Bram Moolenaar01865ad2020-07-26 18:33:09 +02002146 else if (first_optional != -1)
2147 {
2148 emsg(_("E1007: mandatory argument after optional argument"));
2149 return &t_any;
2150 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002151
2152 arg_type[argcount++] = parse_type(&p, type_gap);
2153
2154 // Nothing comes after "...{type}".
2155 if (flags & TTFLAG_VARARGS)
2156 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002157
Bram Moolenaard77a8522020-04-03 21:59:57 +02002158 if (*p != ',' && *skipwhite(p) == ',')
2159 {
2160 semsg(_(e_no_white_before), ",");
2161 return &t_any;
2162 }
2163 if (*p == ',')
2164 {
2165 ++p;
2166 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002167 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002168 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002169 return &t_any;
2170 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002171 }
2172 p = skipwhite(p);
2173 if (argcount == MAX_FUNC_ARGS)
2174 {
2175 emsg(_("E740: Too many argument types"));
2176 return &t_any;
2177 }
2178 }
2179
2180 p = skipwhite(p);
2181 if (*p != ')')
2182 {
2183 emsg(_(e_missing_close));
2184 return &t_any;
2185 }
2186 *arg = p + 1;
2187 }
2188 if (**arg == ':')
2189 {
2190 // parse return type
2191 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002192 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002193 semsg(_(e_white_after), ":");
2194 *arg = skipwhite(*arg);
2195 ret_type = parse_type(arg, type_gap);
2196 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002197 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002198 type = get_func_type(ret_type, argcount, type_gap);
2199 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002200 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002201 type = alloc_func_type(ret_type, argcount, type_gap);
2202 type->tt_flags = flags;
2203 if (argcount > 0)
2204 {
2205 type->tt_argcount = argcount;
2206 type->tt_min_argcount = first_optional == -1
2207 ? argcount : first_optional;
2208 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002209 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002210 return &t_any;
2211 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002212 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002213 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002214 }
2215 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216 }
2217 break;
2218 case 'j':
2219 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2220 {
2221 *arg += len;
2222 return &t_job;
2223 }
2224 break;
2225 case 'l':
2226 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2227 {
2228 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002229 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 }
2231 break;
2232 case 'n':
2233 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2234 {
2235 *arg += len;
2236 return &t_number;
2237 }
2238 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239 case 's':
2240 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2241 {
2242 *arg += len;
2243 return &t_string;
2244 }
2245 break;
2246 case 'v':
2247 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2248 {
2249 *arg += len;
2250 return &t_void;
2251 }
2252 break;
2253 }
2254
2255 semsg(_("E1010: Type not recognized: %s"), *arg);
2256 return &t_any;
2257}
2258
2259/*
2260 * Check if "type1" and "type2" are exactly the same.
2261 */
2262 static int
2263equal_type(type_T *type1, type_T *type2)
2264{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002265 int i;
2266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 if (type1->tt_type != type2->tt_type)
2268 return FALSE;
2269 switch (type1->tt_type)
2270 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002272 case VAR_ANY:
2273 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 case VAR_SPECIAL:
2275 case VAR_BOOL:
2276 case VAR_NUMBER:
2277 case VAR_FLOAT:
2278 case VAR_STRING:
2279 case VAR_BLOB:
2280 case VAR_JOB:
2281 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002282 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283 case VAR_LIST:
2284 case VAR_DICT:
2285 return equal_type(type1->tt_member, type2->tt_member);
2286 case VAR_FUNC:
2287 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002288 if (!equal_type(type1->tt_member, type2->tt_member)
2289 || type1->tt_argcount != type2->tt_argcount)
2290 return FALSE;
2291 if (type1->tt_argcount < 0
2292 || type1->tt_args == NULL || type2->tt_args == NULL)
2293 return TRUE;
2294 for (i = 0; i < type1->tt_argcount; ++i)
2295 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2296 return FALSE;
2297 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 }
2299 return TRUE;
2300}
2301
2302/*
2303 * Find the common type of "type1" and "type2" and put it in "dest".
2304 * "type2" and "dest" may be the same.
2305 */
2306 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002307common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308{
2309 if (equal_type(type1, type2))
2310 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002311 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 return;
2313 }
2314
2315 if (type1->tt_type == type2->tt_type)
2316 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2318 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002319 type_T *common;
2320
Bram Moolenaard77a8522020-04-03 21:59:57 +02002321 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002322 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002323 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002324 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002325 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326 return;
2327 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002328 if (type1->tt_type == VAR_FUNC)
2329 {
2330 type_T *common;
2331
2332 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2333 if (type1->tt_argcount == type2->tt_argcount
2334 && type1->tt_argcount >= 0)
2335 {
2336 int argcount = type1->tt_argcount;
2337 int i;
2338
2339 *dest = alloc_func_type(common, argcount, type_gap);
2340 if (type1->tt_args != NULL && type2->tt_args != NULL)
2341 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002342 if (func_type_add_arg_types(*dest, argcount,
2343 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002344 for (i = 0; i < argcount; ++i)
2345 common_type(type1->tt_args[i], type2->tt_args[i],
2346 &(*dest)->tt_args[i], type_gap);
2347 }
2348 }
2349 else
2350 *dest = alloc_func_type(common, -1, type_gap);
2351 return;
2352 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 }
2354
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002355 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002356}
2357
2358 char *
2359vartype_name(vartype_T type)
2360{
2361 switch (type)
2362 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002363 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002364 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002366 case VAR_SPECIAL: return "special";
2367 case VAR_BOOL: return "bool";
2368 case VAR_NUMBER: return "number";
2369 case VAR_FLOAT: return "float";
2370 case VAR_STRING: return "string";
2371 case VAR_BLOB: return "blob";
2372 case VAR_JOB: return "job";
2373 case VAR_CHANNEL: return "channel";
2374 case VAR_LIST: return "list";
2375 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002376
2377 case VAR_FUNC:
2378 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002380 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381}
2382
2383/*
2384 * Return the name of a type.
2385 * The result may be in allocated memory, in which case "tofree" is set.
2386 */
2387 char *
2388type_name(type_T *type, char **tofree)
2389{
2390 char *name = vartype_name(type->tt_type);
2391
2392 *tofree = NULL;
2393 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2394 {
2395 char *member_free;
2396 char *member_name = type_name(type->tt_member, &member_free);
2397 size_t len;
2398
2399 len = STRLEN(name) + STRLEN(member_name) + 3;
2400 *tofree = alloc(len);
2401 if (*tofree != NULL)
2402 {
2403 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2404 vim_free(member_free);
2405 return *tofree;
2406 }
2407 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002408 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002409 {
2410 garray_T ga;
2411 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002412 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002413
2414 ga_init2(&ga, 1, 100);
2415 if (ga_grow(&ga, 20) == FAIL)
2416 return "[unknown]";
2417 *tofree = ga.ga_data;
2418 STRCPY(ga.ga_data, "func(");
2419 ga.ga_len += 5;
2420
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002421 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002422 {
2423 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002424 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002425 int len;
2426
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002427 if (type->tt_args == NULL)
2428 arg_type = "[unknown]";
2429 else
2430 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002431 if (i > 0)
2432 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002433 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002434 ga.ga_len += 2;
2435 }
2436 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002437 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002438 {
2439 vim_free(arg_free);
2440 return "[unknown]";
2441 }
2442 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002443 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002444 {
2445 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2446 ga.ga_len += 3;
2447 }
2448 else if (i >= type->tt_min_argcount)
2449 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002450 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002451 ga.ga_len += len;
2452 vim_free(arg_free);
2453 }
2454
2455 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002456 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002457 else
2458 {
2459 char *ret_free;
2460 char *ret_name = type_name(type->tt_member, &ret_free);
2461 int len;
2462
2463 len = (int)STRLEN(ret_name) + 4;
2464 if (ga_grow(&ga, len) == FAIL)
2465 {
2466 vim_free(ret_free);
2467 return "[unknown]";
2468 }
2469 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002470 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2471 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002472 vim_free(ret_free);
2473 }
2474 return ga.ga_data;
2475 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476
2477 return name;
2478}
2479
2480/*
2481 * Find "name" in script-local items of script "sid".
2482 * Returns the index in "sn_var_vals" if found.
2483 * If found but not in "sn_var_vals" returns -1.
2484 * If not found returns -2.
2485 */
2486 int
2487get_script_item_idx(int sid, char_u *name, int check_writable)
2488{
2489 hashtab_T *ht;
2490 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002491 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 int idx;
2493
2494 // First look the name up in the hashtable.
2495 if (sid <= 0 || sid > script_items.ga_len)
2496 return -1;
2497 ht = &SCRIPT_VARS(sid);
2498 di = find_var_in_ht(ht, 0, name, TRUE);
2499 if (di == NULL)
2500 return -2;
2501
2502 // Now find the svar_T index in sn_var_vals.
2503 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2504 {
2505 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2506
2507 if (sv->sv_tv == &di->di_tv)
2508 {
2509 if (check_writable && sv->sv_const)
2510 semsg(_(e_readonlyvar), name);
2511 return idx;
2512 }
2513 }
2514 return -1;
2515}
2516
2517/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002518 * Find "name" in imported items of the current script or in "cctx" if not
2519 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 */
2521 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002522find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002524 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 int idx;
2526
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002527 if (current_sctx.sc_sid <= 0)
2528 return NULL;
2529 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 if (cctx != NULL)
2531 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2532 {
2533 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2534 + idx;
2535
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002536 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2537 : STRLEN(import->imp_name) == len
2538 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 return import;
2540 }
2541
2542 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2543 {
2544 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2545
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002546 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2547 : STRLEN(import->imp_name) == len
2548 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 return import;
2550 }
2551 return NULL;
2552}
2553
2554/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002555 * Free all imported variables.
2556 */
2557 static void
2558free_imported(cctx_T *cctx)
2559{
2560 int idx;
2561
2562 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2563 {
2564 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2565
2566 vim_free(import->imp_name);
2567 }
2568 ga_clear(&cctx->ctx_imports);
2569}
2570
2571/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002572 * Return TRUE if "p" points at a "#" but not at "#{".
2573 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002574 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002575vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002576{
2577 return p[0] == '#' && p[1] != '{';
2578}
2579
2580/*
2581 * Return a pointer to the next line that isn't empty or only contains a
2582 * comment. Skips over white space.
2583 * Returns NULL if there is none.
2584 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002585 char_u *
2586peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002587{
2588 int lnum = cctx->ctx_lnum;
2589
2590 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2591 {
2592 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002593 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002594
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002595 if (line == NULL)
2596 break;
2597 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002598 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002599 return p;
2600 }
2601 return NULL;
2602}
2603
2604/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002605 * Called when checking for a following operator at "arg". When the rest of
2606 * the line is empty or only a comment, peek the next line. If there is a next
2607 * line return a pointer to it and set "nextp".
2608 * Otherwise skip over white space.
2609 */
2610 static char_u *
2611may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2612{
2613 char_u *p = skipwhite(arg);
2614
2615 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002616 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002617 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002618 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002619 if (*nextp != NULL)
2620 return *nextp;
2621 }
2622 return p;
2623}
2624
2625/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002626 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002627 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002628 * Returns NULL when at the end.
2629 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002630 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002631next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002632{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002633 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002634
2635 do
2636 {
2637 ++cctx->ctx_lnum;
2638 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002639 {
2640 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002641 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002642 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002643 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002644 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002645 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002646 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002647 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002648 return line;
2649}
2650
2651/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002652 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002653 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002654 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2655 */
2656 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002657may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002658{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002659 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002660 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002661 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002662
2663 if (next == NULL)
2664 return FAIL;
2665 *arg = skipwhite(next);
2666 }
2667 return OK;
2668}
2669
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002670/*
2671 * Idem, and give an error when failed.
2672 */
2673 static int
2674may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2675{
2676 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2677 {
2678 emsg(_("E1097: line incomplete"));
2679 return FAIL;
2680 }
2681 return OK;
2682}
2683
2684
Bram Moolenaara5565e42020-05-09 15:44:01 +02002685// Structure passed between the compile_expr* functions to keep track of
2686// constants that have been parsed but for which no code was produced yet. If
2687// possible expressions on these constants are applied at compile time. If
2688// that is not possible, the code to push the constants needs to be generated
2689// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002690// Using 50 should be more than enough of 5 levels of ().
2691#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002692typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002693 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002694 int pp_used; // active entries in pp_tv[]
2695} ppconst_T;
2696
Bram Moolenaar1c747212020-05-09 18:28:34 +02002697static int compile_expr0(char_u **arg, cctx_T *cctx);
2698static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2699
Bram Moolenaara5565e42020-05-09 15:44:01 +02002700/*
2701 * Generate a PUSH instruction for "tv".
2702 * "tv" will be consumed or cleared.
2703 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2704 */
2705 static int
2706generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2707{
2708 if (tv != NULL)
2709 {
2710 switch (tv->v_type)
2711 {
2712 case VAR_UNKNOWN:
2713 break;
2714 case VAR_BOOL:
2715 generate_PUSHBOOL(cctx, tv->vval.v_number);
2716 break;
2717 case VAR_SPECIAL:
2718 generate_PUSHSPEC(cctx, tv->vval.v_number);
2719 break;
2720 case VAR_NUMBER:
2721 generate_PUSHNR(cctx, tv->vval.v_number);
2722 break;
2723#ifdef FEAT_FLOAT
2724 case VAR_FLOAT:
2725 generate_PUSHF(cctx, tv->vval.v_float);
2726 break;
2727#endif
2728 case VAR_BLOB:
2729 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2730 tv->vval.v_blob = NULL;
2731 break;
2732 case VAR_STRING:
2733 generate_PUSHS(cctx, tv->vval.v_string);
2734 tv->vval.v_string = NULL;
2735 break;
2736 default:
2737 iemsg("constant type not supported");
2738 clear_tv(tv);
2739 return FAIL;
2740 }
2741 tv->v_type = VAR_UNKNOWN;
2742 }
2743 return OK;
2744}
2745
2746/*
2747 * Generate code for any ppconst entries.
2748 */
2749 static int
2750generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2751{
2752 int i;
2753 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002754 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002755
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002756 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002757 for (i = 0; i < ppconst->pp_used; ++i)
2758 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2759 ret = FAIL;
2760 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002761 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002762 return ret;
2763}
2764
2765/*
2766 * Clear ppconst constants. Used when failing.
2767 */
2768 static void
2769clear_ppconst(ppconst_T *ppconst)
2770{
2771 int i;
2772
2773 for (i = 0; i < ppconst->pp_used; ++i)
2774 clear_tv(&ppconst->pp_tv[i]);
2775 ppconst->pp_used = 0;
2776}
2777
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002778/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002779 * Generate an instruction to load script-local variable "name", without the
2780 * leading "s:".
2781 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 */
2783 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002784compile_load_scriptvar(
2785 cctx_T *cctx,
2786 char_u *name, // variable NUL terminated
2787 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002788 char_u **end, // end of variable
2789 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002791 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002792 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2793 imported_T *import;
2794
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002795 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002797 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002798 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2799 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 }
2801 if (idx >= 0)
2802 {
2803 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2804
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002805 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 current_sctx.sc_sid, idx, sv->sv_type);
2807 return OK;
2808 }
2809
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002810 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 if (import != NULL)
2812 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002813 if (import->imp_all)
2814 {
2815 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002816 char_u *exp_name;
2817 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002818 ufunc_T *ufunc;
2819 type_T *type;
2820
2821 // Used "import * as Name", need to lookup the member.
2822 if (*p != '.')
2823 {
2824 semsg(_("E1060: expected dot after name: %s"), start);
2825 return FAIL;
2826 }
2827 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002828 if (VIM_ISWHITE(*p))
2829 {
2830 emsg(_("E1074: no white space allowed after dot"));
2831 return FAIL;
2832 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002833
Bram Moolenaar1c991142020-07-04 13:15:31 +02002834 // isolate one name
2835 exp_name = p;
2836 while (eval_isnamec(*p))
2837 ++p;
2838 cc = *p;
2839 *p = NUL;
2840
2841 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2842 *p = cc;
2843 p = skipwhite(p);
2844
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002845 // TODO: what if it is a function?
2846 if (idx < 0)
2847 return FAIL;
2848 *end = p;
2849
2850 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2851 import->imp_sid,
2852 idx,
2853 type);
2854 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002855 else if (import->imp_funcname != NULL)
2856 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002857 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002858 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2859 import->imp_sid,
2860 import->imp_var_vals_idx,
2861 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 return OK;
2863 }
2864
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002865 if (error)
2866 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 return FAIL;
2868}
2869
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002870 static int
2871generate_funcref(cctx_T *cctx, char_u *name)
2872{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002873 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002874
2875 if (ufunc == NULL)
2876 return FAIL;
2877
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002878 // Need to compile any default values to get the argument types.
2879 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2880 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2881 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002882 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002883}
2884
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885/*
2886 * Compile a variable name into a load instruction.
2887 * "end" points to just after the name.
2888 * When "error" is FALSE do not give an error when not found.
2889 */
2890 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002891compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892{
2893 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002894 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002895 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002897 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898
2899 if (*(*arg + 1) == ':')
2900 {
2901 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002902 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002903 {
2904 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002906 switch (**arg)
2907 {
2908 case 'g': isn_type = ISN_LOADGDICT; break;
2909 case 'w': isn_type = ISN_LOADWDICT; break;
2910 case 't': isn_type = ISN_LOADTDICT; break;
2911 case 'b': isn_type = ISN_LOADBDICT; break;
2912 default:
2913 semsg(_(e_namespace), *arg);
2914 goto theend;
2915 }
2916 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2917 goto theend;
2918 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 else
2921 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002922 isntype_T isn_type = ISN_DROP;
2923
2924 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2925 if (name == NULL)
2926 return FAIL;
2927
2928 switch (**arg)
2929 {
2930 case 'v': res = generate_LOADV(cctx, name, error);
2931 break;
2932 case 's': res = compile_load_scriptvar(cctx, name,
2933 NULL, NULL, error);
2934 break;
2935 case 'g': isn_type = ISN_LOADG; break;
2936 case 'w': isn_type = ISN_LOADW; break;
2937 case 't': isn_type = ISN_LOADT; break;
2938 case 'b': isn_type = ISN_LOADB; break;
2939 default: semsg(_(e_namespace), *arg);
2940 goto theend;
2941 }
2942 if (isn_type != ISN_DROP)
2943 {
2944 // Global, Buffer-local, Window-local and Tabpage-local
2945 // variables can be defined later, thus we don't check if it
2946 // exists, give error at runtime.
2947 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 }
2950 }
2951 else
2952 {
2953 size_t len = end - *arg;
2954 int idx;
2955 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002956 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957
2958 name = vim_strnsave(*arg, end - *arg);
2959 if (name == NULL)
2960 return FAIL;
2961
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002962 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002964 if (!gen_load_outer)
2965 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 }
2967 else
2968 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002969 lvar_T *lvar = lookup_local(*arg, len, cctx);
2970
2971 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002973 type = lvar->lv_type;
2974 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002975 if (lvar->lv_from_outer)
2976 gen_load_outer = TRUE;
2977 else
2978 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 }
2980 else
2981 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002982 // "var" can be script-local even without using "s:" if it
2983 // already exists.
2984 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2985 == SCRIPT_VERSION_VIM9
2986 || lookup_script(*arg, len) == OK)
2987 res = compile_load_scriptvar(cctx, name, *arg, &end,
2988 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002989
Bram Moolenaara5565e42020-05-09 15:44:01 +02002990 // When the name starts with an uppercase letter or "x:" it
2991 // can be a user defined function.
2992 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2993 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994 }
2995 }
2996 if (gen_load)
2997 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002998 if (gen_load_outer)
2999 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 }
3001
3002 *arg = end;
3003
3004theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003005 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 semsg(_(e_var_notfound), name);
3007 vim_free(name);
3008 return res;
3009}
3010
3011/*
3012 * Compile the argument expressions.
3013 * "arg" points to just after the "(" and is advanced to after the ")"
3014 */
3015 static int
3016compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
3017{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003018 char_u *p = *arg;
3019 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020
Bram Moolenaare6085c52020-04-12 20:19:16 +02003021 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003023 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3024 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003025 if (*p == ')')
3026 {
3027 *arg = p + 1;
3028 return OK;
3029 }
3030
Bram Moolenaara5565e42020-05-09 15:44:01 +02003031 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 return FAIL;
3033 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003034
3035 if (*p != ',' && *skipwhite(p) == ',')
3036 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02003037 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003038 p = skipwhite(p);
3039 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003041 {
3042 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003043 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02003044 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003045 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003046 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003047 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003049failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003050 emsg(_(e_missing_close));
3051 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052}
3053
3054/*
3055 * Compile a function call: name(arg1, arg2)
3056 * "arg" points to "name", "arg + varlen" to the "(".
3057 * "argcount_init" is 1 for "value->method()"
3058 * Instructions:
3059 * EVAL arg1
3060 * EVAL arg2
3061 * BCALL / DCALL / UCALL
3062 */
3063 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003064compile_call(
3065 char_u **arg,
3066 size_t varlen,
3067 cctx_T *cctx,
3068 ppconst_T *ppconst,
3069 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070{
3071 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003072 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 int argcount = argcount_init;
3074 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003075 char_u fname_buf[FLEN_FIXED + 1];
3076 char_u *tofree = NULL;
3077 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003079 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080
Bram Moolenaara5565e42020-05-09 15:44:01 +02003081 // we can evaluate "has('name')" at compile time
3082 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3083 {
3084 char_u *s = skipwhite(*arg + varlen + 1);
3085 typval_T argvars[2];
3086
3087 argvars[0].v_type = VAR_UNKNOWN;
3088 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003089 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003090 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003091 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003092 s = skipwhite(s);
3093 if (*s == ')' && argvars[0].v_type == VAR_STRING)
3094 {
3095 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3096
3097 *arg = s + 1;
3098 argvars[1].v_type = VAR_UNKNOWN;
3099 tv->v_type = VAR_NUMBER;
3100 tv->vval.v_number = 0;
3101 f_has(argvars, tv);
3102 clear_tv(&argvars[0]);
3103 ++ppconst->pp_used;
3104 return OK;
3105 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003106 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003107 }
3108
3109 if (generate_ppconst(cctx, ppconst) == FAIL)
3110 return FAIL;
3111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 if (varlen >= sizeof(namebuf))
3113 {
3114 semsg(_("E1011: name too long: %s"), name);
3115 return FAIL;
3116 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003117 vim_strncpy(namebuf, *arg, varlen);
3118 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119
3120 *arg = skipwhite(*arg + varlen + 1);
3121 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003122 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003124 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 {
3126 int idx;
3127
3128 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003129 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02003131 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003132 else
3133 semsg(_(e_unknownfunc), namebuf);
3134 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 }
3136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003138 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003140 {
3141 res = generate_CALL(cctx, ufunc, argcount);
3142 goto theend;
3143 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144
3145 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003146 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003148 if (STRNCMP(namebuf, "g:", 2) != 0
3149 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003150 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003151 garray_T *stack = &cctx->ctx_type_stack;
3152 type_T *type;
3153
3154 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3155 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003156 goto theend;
3157 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003159 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003160 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003161 if (STRNCMP(namebuf, "g:", 2) == 0)
3162 res = generate_UCALL(cctx, name, argcount);
3163 else
3164 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003165
3166theend:
3167 vim_free(tofree);
3168 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169}
3170
3171// like NAMESPACE_CHAR but with 'a' and 'l'.
3172#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3173
3174/*
3175 * Find the end of a variable or function name. Unlike find_name_end() this
3176 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003177 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 * Return a pointer to just after the name. Equal to "arg" if there is no
3179 * valid name.
3180 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003181 static char_u *
3182to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183{
3184 char_u *p;
3185
3186 // Quick check for valid starting character.
3187 if (!eval_isnamec1(*arg))
3188 return arg;
3189
3190 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3191 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3192 // and can be used in slice "[n:]".
3193 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003194 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3196 break;
3197 return p;
3198}
3199
3200/*
3201 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003202 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 */
3204 char_u *
3205to_name_const_end(char_u *arg)
3206{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003207 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 typval_T rettv;
3209
3210 if (p == arg && *arg == '[')
3211 {
3212
3213 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003214 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 p = arg;
3216 }
3217 else if (p == arg && *arg == '#' && arg[1] == '{')
3218 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003219 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003221 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 p = arg;
3223 }
3224 else if (p == arg && *arg == '{')
3225 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003226 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003228 // Can be "{x -> ret}()".
3229 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003231 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 if (ret != OK)
3233 p = arg;
3234 }
3235
3236 return p;
3237}
3238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239/*
3240 * parse a list: [expr, expr]
3241 * "*arg" points to the '['.
3242 */
3243 static int
3244compile_list(char_u **arg, cctx_T *cctx)
3245{
3246 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003247 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 int count = 0;
3249
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003250 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003252 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003253 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003254 semsg(_(e_list_end), *arg);
3255 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003256 }
3257 if (*p == ']')
3258 {
3259 ++p;
3260 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003261 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003262 p += STRLEN(p);
3263 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003264 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003265 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 break;
3267 ++count;
3268 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003269 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003271 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3272 {
3273 semsg(_(e_white_after), ",");
3274 return FAIL;
3275 }
3276 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003277 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 p = skipwhite(p);
3279 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003280 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281
3282 generate_NEWLIST(cctx, count);
3283 return OK;
3284}
3285
3286/*
3287 * parse a lambda: {arg, arg -> expr}
3288 * "*arg" points to the '{'.
3289 */
3290 static int
3291compile_lambda(char_u **arg, cctx_T *cctx)
3292{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 typval_T rettv;
3294 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003295 evalarg_T evalarg;
3296
3297 CLEAR_FIELD(evalarg);
3298 evalarg.eval_flags = EVAL_EVALUATE;
3299 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300
3301 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003302 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003306 ++ufunc->uf_refcount;
3307 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003308 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309
3310 // The function will have one line: "return {expr}".
3311 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003312 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003314 clear_evalarg(&evalarg, NULL);
3315
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003316 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02003317 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003318
3319 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 return FAIL;
3321}
3322
3323/*
3324 * Compile a lamda call: expr->{lambda}(args)
3325 * "arg" points to the "{".
3326 */
3327 static int
3328compile_lambda_call(char_u **arg, cctx_T *cctx)
3329{
3330 ufunc_T *ufunc;
3331 typval_T rettv;
3332 int argcount = 1;
3333 int ret = FAIL;
3334
3335 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003336 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 return FAIL;
3338
3339 if (**arg != '(')
3340 {
3341 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003342 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 else
3344 semsg(_(e_missing_paren), "lambda");
3345 clear_tv(&rettv);
3346 return FAIL;
3347 }
3348
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 ufunc = rettv.vval.v_partial->pt_func;
3350 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003351 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003352 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003353
3354 // The function will have one line: "return {expr}".
3355 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003356 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357
3358 // compile the arguments
3359 *arg = skipwhite(*arg + 1);
3360 if (compile_arguments(arg, cctx, &argcount) == OK)
3361 // call the compiled function
3362 ret = generate_CALL(cctx, ufunc, argcount);
3363
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003364 if (ret == FAIL)
3365 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 return ret;
3367}
3368
3369/*
3370 * parse a dict: {'key': val} or #{key: val}
3371 * "*arg" points to the '{'.
3372 */
3373 static int
3374compile_dict(char_u **arg, cctx_T *cctx, int literal)
3375{
3376 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003377 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 int count = 0;
3379 dict_T *d = dict_alloc();
3380 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003381 char_u *whitep = *arg;
3382 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383
3384 if (d == NULL)
3385 return FAIL;
3386 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003387 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 {
3389 char_u *key = NULL;
3390
Bram Moolenaar23c55272020-06-21 16:58:13 +02003391 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003392 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003393 *arg = NULL;
3394 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003395 }
3396
3397 if (**arg == '}')
3398 break;
3399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 if (literal)
3401 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003402 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403
Bram Moolenaar2c330432020-04-13 14:41:35 +02003404 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 {
3406 semsg(_("E1014: Invalid key: %s"), *arg);
3407 return FAIL;
3408 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003409 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 if (generate_PUSHS(cctx, key) == FAIL)
3411 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003412 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 }
3414 else
3415 {
3416 isn_T *isn;
3417
Bram Moolenaara5565e42020-05-09 15:44:01 +02003418 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3421 if (isn->isn_type == ISN_PUSHS)
3422 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003423 else
3424 {
3425 type_T *keytype = ((type_T **)stack->ga_data)
3426 [stack->ga_len - 1];
3427 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
3428 return FAIL;
3429 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 }
3431
3432 // Check for duplicate keys, if using string keys.
3433 if (key != NULL)
3434 {
3435 item = dict_find(d, key, -1);
3436 if (item != NULL)
3437 {
3438 semsg(_(e_duplicate_key), key);
3439 goto failret;
3440 }
3441 item = dictitem_alloc(key);
3442 if (item != NULL)
3443 {
3444 item->di_tv.v_type = VAR_UNKNOWN;
3445 item->di_tv.v_lock = 0;
3446 if (dict_add(d, item) == FAIL)
3447 dictitem_free(item);
3448 }
3449 }
3450
3451 *arg = skipwhite(*arg);
3452 if (**arg != ':')
3453 {
3454 semsg(_(e_missing_dict_colon), *arg);
3455 return FAIL;
3456 }
3457
Bram Moolenaar2c330432020-04-13 14:41:35 +02003458 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003460 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003461 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003462 *arg = NULL;
3463 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003464 }
3465
Bram Moolenaara5565e42020-05-09 15:44:01 +02003466 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 return FAIL;
3468 ++count;
3469
Bram Moolenaar2c330432020-04-13 14:41:35 +02003470 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003471 *arg = skipwhite(*arg);
3472 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003473 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003474 *arg = NULL;
3475 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003476 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 if (**arg == '}')
3478 break;
3479 if (**arg != ',')
3480 {
3481 semsg(_(e_missing_dict_comma), *arg);
3482 goto failret;
3483 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003484 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 *arg = skipwhite(*arg + 1);
3486 }
3487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 *arg = *arg + 1;
3489
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003490 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003491 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003492 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003493 *arg += STRLEN(*arg);
3494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 dict_unref(d);
3496 return generate_NEWDICT(cctx, count);
3497
3498failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003499 if (*arg == NULL)
3500 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 dict_unref(d);
3502 return FAIL;
3503}
3504
3505/*
3506 * Compile "&option".
3507 */
3508 static int
3509compile_get_option(char_u **arg, cctx_T *cctx)
3510{
3511 typval_T rettv;
3512 char_u *start = *arg;
3513 int ret;
3514
3515 // parse the option and get the current value to get the type.
3516 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003517 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 if (ret == OK)
3519 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003520 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 char_u *name = vim_strnsave(start, *arg - start);
3522 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3523
3524 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3525 vim_free(name);
3526 }
3527 clear_tv(&rettv);
3528
3529 return ret;
3530}
3531
3532/*
3533 * Compile "$VAR".
3534 */
3535 static int
3536compile_get_env(char_u **arg, cctx_T *cctx)
3537{
3538 char_u *start = *arg;
3539 int len;
3540 int ret;
3541 char_u *name;
3542
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 ++*arg;
3544 len = get_env_len(arg);
3545 if (len == 0)
3546 {
3547 semsg(_(e_syntax_at), start - 1);
3548 return FAIL;
3549 }
3550
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003551 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 name = vim_strnsave(start, len + 1);
3553 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3554 vim_free(name);
3555 return ret;
3556}
3557
3558/*
3559 * Compile "@r".
3560 */
3561 static int
3562compile_get_register(char_u **arg, cctx_T *cctx)
3563{
3564 int ret;
3565
3566 ++*arg;
3567 if (**arg == NUL)
3568 {
3569 semsg(_(e_syntax_at), *arg - 1);
3570 return FAIL;
3571 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003572 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 {
3574 emsg_invreg(**arg);
3575 return FAIL;
3576 }
3577 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3578 ++*arg;
3579 return ret;
3580}
3581
3582/*
3583 * Apply leading '!', '-' and '+' to constant "rettv".
3584 */
3585 static int
3586apply_leader(typval_T *rettv, char_u *start, char_u *end)
3587{
3588 char_u *p = end;
3589
3590 // this works from end to start
3591 while (p > start)
3592 {
3593 --p;
3594 if (*p == '-' || *p == '+')
3595 {
3596 // only '-' has an effect, for '+' we only check the type
3597#ifdef FEAT_FLOAT
3598 if (rettv->v_type == VAR_FLOAT)
3599 {
3600 if (*p == '-')
3601 rettv->vval.v_float = -rettv->vval.v_float;
3602 }
3603 else
3604#endif
3605 {
3606 varnumber_T val;
3607 int error = FALSE;
3608
3609 // tv_get_number_chk() accepts a string, but we don't want that
3610 // here
3611 if (check_not_string(rettv) == FAIL)
3612 return FAIL;
3613 val = tv_get_number_chk(rettv, &error);
3614 clear_tv(rettv);
3615 if (error)
3616 return FAIL;
3617 if (*p == '-')
3618 val = -val;
3619 rettv->v_type = VAR_NUMBER;
3620 rettv->vval.v_number = val;
3621 }
3622 }
3623 else
3624 {
3625 int v = tv2bool(rettv);
3626
3627 // '!' is permissive in the type.
3628 clear_tv(rettv);
3629 rettv->v_type = VAR_BOOL;
3630 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3631 }
3632 }
3633 return OK;
3634}
3635
3636/*
3637 * Recognize v: variables that are constants and set "rettv".
3638 */
3639 static void
3640get_vim_constant(char_u **arg, typval_T *rettv)
3641{
3642 if (STRNCMP(*arg, "v:true", 6) == 0)
3643 {
3644 rettv->v_type = VAR_BOOL;
3645 rettv->vval.v_number = VVAL_TRUE;
3646 *arg += 6;
3647 }
3648 else if (STRNCMP(*arg, "v:false", 7) == 0)
3649 {
3650 rettv->v_type = VAR_BOOL;
3651 rettv->vval.v_number = VVAL_FALSE;
3652 *arg += 7;
3653 }
3654 else if (STRNCMP(*arg, "v:null", 6) == 0)
3655 {
3656 rettv->v_type = VAR_SPECIAL;
3657 rettv->vval.v_number = VVAL_NULL;
3658 *arg += 6;
3659 }
3660 else if (STRNCMP(*arg, "v:none", 6) == 0)
3661 {
3662 rettv->v_type = VAR_SPECIAL;
3663 rettv->vval.v_number = VVAL_NONE;
3664 *arg += 6;
3665 }
3666}
3667
Bram Moolenaar696ba232020-07-29 21:20:41 +02003668 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003669get_compare_type(char_u *p, int *len, int *type_is)
3670{
3671 exptype_T type = EXPR_UNKNOWN;
3672 int i;
3673
3674 switch (p[0])
3675 {
3676 case '=': if (p[1] == '=')
3677 type = EXPR_EQUAL;
3678 else if (p[1] == '~')
3679 type = EXPR_MATCH;
3680 break;
3681 case '!': if (p[1] == '=')
3682 type = EXPR_NEQUAL;
3683 else if (p[1] == '~')
3684 type = EXPR_NOMATCH;
3685 break;
3686 case '>': if (p[1] != '=')
3687 {
3688 type = EXPR_GREATER;
3689 *len = 1;
3690 }
3691 else
3692 type = EXPR_GEQUAL;
3693 break;
3694 case '<': if (p[1] != '=')
3695 {
3696 type = EXPR_SMALLER;
3697 *len = 1;
3698 }
3699 else
3700 type = EXPR_SEQUAL;
3701 break;
3702 case 'i': if (p[1] == 's')
3703 {
3704 // "is" and "isnot"; but not a prefix of a name
3705 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3706 *len = 5;
3707 i = p[*len];
3708 if (!isalnum(i) && i != '_')
3709 {
3710 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3711 *type_is = TRUE;
3712 }
3713 }
3714 break;
3715 }
3716 return type;
3717}
3718
3719/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 * Compile code to apply '-', '+' and '!'.
3721 */
3722 static int
3723compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3724{
3725 char_u *p = end;
3726
3727 // this works from end to start
3728 while (p > start)
3729 {
3730 --p;
3731 if (*p == '-' || *p == '+')
3732 {
3733 int negate = *p == '-';
3734 isn_T *isn;
3735
3736 // TODO: check type
3737 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3738 {
3739 --p;
3740 if (*p == '-')
3741 negate = !negate;
3742 }
3743 // only '-' has an effect, for '+' we only check the type
3744 if (negate)
3745 isn = generate_instr(cctx, ISN_NEGATENR);
3746 else
3747 isn = generate_instr(cctx, ISN_CHECKNR);
3748 if (isn == NULL)
3749 return FAIL;
3750 }
3751 else
3752 {
3753 int invert = TRUE;
3754
3755 while (p > start && p[-1] == '!')
3756 {
3757 --p;
3758 invert = !invert;
3759 }
3760 if (generate_2BOOL(cctx, invert) == FAIL)
3761 return FAIL;
3762 }
3763 }
3764 return OK;
3765}
3766
3767/*
3768 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003769 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 */
3771 static int
3772compile_subscript(
3773 char_u **arg,
3774 cctx_T *cctx,
3775 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003776 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003777 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778{
3779 for (;;)
3780 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003781 char_u *p = skipwhite(*arg);
3782
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003783 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003784 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003785 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003786
3787 // If a following line starts with "->{" or "->X" advance to that
3788 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003789 // Also if a following line starts with ".x".
3790 if (next != NULL &&
3791 ((next[0] == '-' && next[1] == '>'
3792 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003793 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003794 {
3795 next = next_line_from_context(cctx, TRUE);
3796 if (next == NULL)
3797 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003798 *arg = next;
3799 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003800 }
3801 }
3802
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003803 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3804 // not a function call.
3805 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003807 garray_T *stack = &cctx->ctx_type_stack;
3808 type_T *type;
3809 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003811 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003812 return FAIL;
3813
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003815 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3816
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003817 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3819 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003820 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 return FAIL;
3822 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003823 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003825 char_u *pstart = p;
3826
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003827 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003828 return FAIL;
3829
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 // something->method()
3831 // Apply the '!', '-' and '+' first:
3832 // -1.0->func() works like (-1.0)->func()
3833 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3834 return FAIL;
3835 *start_leader = end_leader; // don't apply again later
3836
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003837 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003838 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003839 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 if (**arg == '{')
3841 {
3842 // lambda call: list->{lambda}
3843 if (compile_lambda_call(arg, cctx) == FAIL)
3844 return FAIL;
3845 }
3846 else
3847 {
3848 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003849 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003850 if (!eval_isnamec1(*p))
3851 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003852 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003853 return FAIL;
3854 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003855 if (ASCII_ISALPHA(*p) && p[1] == ':')
3856 p += 2;
3857 for ( ; eval_isnamec1(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 ;
3859 if (*p != '(')
3860 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003861 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 return FAIL;
3863 }
3864 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003865 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 return FAIL;
3867 }
3868 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003869 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003871 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003872 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003873 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003874
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003875 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003876 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003877 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003878 // TODO: blob index
3879 // TODO: more arguments
3880 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003881 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003882 return FAIL;
3883
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003884 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003885 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003886 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003887 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003888 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 return FAIL;
3890
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003891 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3892 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 if (**arg != ']')
3894 {
3895 emsg(_(e_missbrac));
3896 return FAIL;
3897 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003898 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003900 // We can index a list and a dict. If we don't know the type
3901 // we can use the index value type.
3902 // TODO: If we don't know use an instruction to figure it out at
3903 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003904 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003905 vtype = (*typep)->tt_type;
3906 if (*typep == &t_any)
3907 {
3908 type_T *valtype = ((type_T **)stack->ga_data)
3909 [stack->ga_len - 1];
3910 if (valtype == &t_string)
3911 vtype = VAR_DICT;
3912 }
3913 if (vtype == VAR_DICT)
3914 {
3915 if ((*typep)->tt_type == VAR_DICT)
3916 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003917 else
3918 {
3919 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3920 return FAIL;
3921 *typep = &t_any;
3922 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003923 if (may_generate_2STRING(-1, cctx) == FAIL)
3924 return FAIL;
3925 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3926 return FAIL;
3927 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003928 else if (vtype == VAR_STRING)
3929 {
3930 *typep = &t_number;
3931 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3932 return FAIL;
3933 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003934 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003935 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003936 if ((*typep)->tt_type == VAR_LIST)
3937 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003938 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003939 return FAIL;
3940 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003941 else
3942 {
3943 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003944 return FAIL;
3945 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003947 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003949 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003950 return FAIL;
3951
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003952 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003953 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3954 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003956 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003957 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 while (eval_isnamec(*p))
3959 MB_PTR_ADV(p);
3960 if (p == *arg)
3961 {
3962 semsg(_(e_syntax_at), *arg);
3963 return FAIL;
3964 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003965 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 return FAIL;
3967 *arg = p;
3968 }
3969 else
3970 break;
3971 }
3972
3973 // TODO - see handle_subscript():
3974 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3975 // Don't do this when "Func" is already a partial that was bound
3976 // explicitly (pt_auto is FALSE).
3977
3978 return OK;
3979}
3980
3981/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003982 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3983 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003985 * If the value is a constant "ppconst->pp_ret" will be set.
3986 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003987 *
3988 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 */
3990
3991/*
3992 * number number constant
3993 * 0zFFFFFFFF Blob constant
3994 * "string" string constant
3995 * 'string' literal string constant
3996 * &option-name option value
3997 * @r register contents
3998 * identifier variable value
3999 * function() function call
4000 * $VAR environment variable
4001 * (expression) nested expression
4002 * [expr, expr] List
4003 * {key: val, key: val} Dictionary
4004 * #{key: val, key: val} Dictionary with literal keys
4005 *
4006 * Also handle:
4007 * ! in front logical NOT
4008 * - in front unary minus
4009 * + in front unary plus (ignored)
4010 * trailing (arg) funcref/partial call
4011 * trailing [] subscript in String or List
4012 * trailing .name entry in Dictionary
4013 * trailing ->name() method call
4014 */
4015 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004016compile_expr7(
4017 char_u **arg,
4018 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004019 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 char_u *start_leader, *end_leader;
4022 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004023 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004024 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025
4026 /*
4027 * Skip '!', '-' and '+' characters. They are handled later.
4028 */
4029 start_leader = *arg;
4030 while (**arg == '!' || **arg == '-' || **arg == '+')
4031 *arg = skipwhite(*arg + 1);
4032 end_leader = *arg;
4033
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004034 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 switch (**arg)
4036 {
4037 /*
4038 * Number constant.
4039 */
4040 case '0': // also for blob starting with 0z
4041 case '1':
4042 case '2':
4043 case '3':
4044 case '4':
4045 case '5':
4046 case '6':
4047 case '7':
4048 case '8':
4049 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004050 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 return FAIL;
4052 break;
4053
4054 /*
4055 * String constant: "string".
4056 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004057 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 return FAIL;
4059 break;
4060
4061 /*
4062 * Literal string constant: 'str''ing'.
4063 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004064 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 return FAIL;
4066 break;
4067
4068 /*
4069 * Constant Vim variable.
4070 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004071 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 ret = NOTDONE;
4073 break;
4074
4075 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004076 * "true" constant
4077 */
4078 case 't': if (STRNCMP(*arg, "true", 4) == 0
4079 && !eval_isnamec((*arg)[4]))
4080 {
4081 *arg += 4;
4082 rettv->v_type = VAR_BOOL;
4083 rettv->vval.v_number = VVAL_TRUE;
4084 }
4085 else
4086 ret = NOTDONE;
4087 break;
4088
4089 /*
4090 * "false" constant
4091 */
4092 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4093 && !eval_isnamec((*arg)[5]))
4094 {
4095 *arg += 5;
4096 rettv->v_type = VAR_BOOL;
4097 rettv->vval.v_number = VVAL_FALSE;
4098 }
4099 else
4100 ret = NOTDONE;
4101 break;
4102
4103 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 * List: [expr, expr]
4105 */
4106 case '[': ret = compile_list(arg, cctx);
4107 break;
4108
4109 /*
4110 * Dictionary: #{key: val, key: val}
4111 */
4112 case '#': if ((*arg)[1] == '{')
4113 {
4114 ++*arg;
4115 ret = compile_dict(arg, cctx, TRUE);
4116 }
4117 else
4118 ret = NOTDONE;
4119 break;
4120
4121 /*
4122 * Lambda: {arg, arg -> expr}
4123 * Dictionary: {'key': val, 'key': val}
4124 */
4125 case '{': {
4126 char_u *start = skipwhite(*arg + 1);
4127
4128 // Find out what comes after the arguments.
4129 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004130 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 if (ret != FAIL && *start == '>')
4132 ret = compile_lambda(arg, cctx);
4133 else
4134 ret = compile_dict(arg, cctx, FALSE);
4135 }
4136 break;
4137
4138 /*
4139 * Option value: &name
4140 */
4141 case '&': ret = compile_get_option(arg, cctx);
4142 break;
4143
4144 /*
4145 * Environment variable: $VAR.
4146 */
4147 case '$': ret = compile_get_env(arg, cctx);
4148 break;
4149
4150 /*
4151 * Register contents: @r.
4152 */
4153 case '@': ret = compile_get_register(arg, cctx);
4154 break;
4155 /*
4156 * nested expression: (expression).
4157 */
4158 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02004159
4160 // recursive!
4161 if (ppconst->pp_used <= PPSIZE - 10)
4162 {
4163 ret = compile_expr1(arg, cctx, ppconst);
4164 }
4165 else
4166 {
4167 // Not enough space in ppconst, flush constants.
4168 if (generate_ppconst(cctx, ppconst) == FAIL)
4169 return FAIL;
4170 ret = compile_expr0(arg, cctx);
4171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 *arg = skipwhite(*arg);
4173 if (**arg == ')')
4174 ++*arg;
4175 else if (ret == OK)
4176 {
4177 emsg(_(e_missing_close));
4178 ret = FAIL;
4179 }
4180 break;
4181
4182 default: ret = NOTDONE;
4183 break;
4184 }
4185 if (ret == FAIL)
4186 return FAIL;
4187
Bram Moolenaar1c747212020-05-09 18:28:34 +02004188 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189 {
4190 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004191 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004193 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 return FAIL;
4195 }
4196 start_leader = end_leader; // don't apply again below
4197
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004198 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004199 clear_tv(rettv);
4200 else
4201 // A constant expression can possibly be handled compile time,
4202 // return the value instead of generating code.
4203 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 }
4205 else if (ret == NOTDONE)
4206 {
4207 char_u *p;
4208 int r;
4209
4210 if (!eval_isnamec1(**arg))
4211 {
4212 semsg(_("E1015: Name expected: %s"), *arg);
4213 return FAIL;
4214 }
4215
4216 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004217 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004219 {
4220 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004223 {
4224 if (generate_ppconst(cctx, ppconst) == FAIL)
4225 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 if (r == FAIL)
4229 return FAIL;
4230 }
4231
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004232 // Handle following "[]", ".member", etc.
4233 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004234 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004235 ppconst) == FAIL)
4236 return FAIL;
4237 if (ppconst->pp_used > 0)
4238 {
4239 // apply the '!', '-' and '+' before the constant
4240 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4241 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4242 return FAIL;
4243 return OK;
4244 }
4245 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004247 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248}
4249
4250/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004251 * Give the "white on both sides" error, taking the operator from "p[len]".
4252 */
4253 void
4254error_white_both(char_u *op, int len)
4255{
4256 char_u buf[10];
4257
4258 vim_strncpy(buf, op, len);
4259 semsg(_(e_white_both), buf);
4260}
4261
4262/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263 * * number multiplication
4264 * / number division
4265 * % number modulo
4266 */
4267 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004268compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269{
4270 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004271 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004272 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004274 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004275 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 return FAIL;
4277
4278 /*
4279 * Repeat computing, until no "*", "/" or "%" is following.
4280 */
4281 for (;;)
4282 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004283 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 if (*op != '*' && *op != '/' && *op != '%')
4285 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004286 if (next != NULL)
4287 {
4288 *arg = next_line_from_context(cctx, TRUE);
4289 op = skipwhite(*arg);
4290 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004291
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004292 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004294 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004295 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 }
4297 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004298 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004299 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004301 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004302 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004304
4305 if (ppconst->pp_used == ppconst_used + 2
4306 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4307 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004308 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004309 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4310 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004311 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004313 // both are numbers: compute the result
4314 switch (*op)
4315 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004316 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004317 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004318 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004319 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004320 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004321 break;
4322 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004323 tv1->vval.v_number = res;
4324 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004325 }
4326 else
4327 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004328 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004329 generate_two_op(cctx, op);
4330 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 }
4332
4333 return OK;
4334}
4335
4336/*
4337 * + number addition
4338 * - number subtraction
4339 * .. string concatenation
4340 */
4341 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004342compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343{
4344 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004345 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004347 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348
4349 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004350 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 return FAIL;
4352
4353 /*
4354 * Repeat computing, until no "+", "-" or ".." is following.
4355 */
4356 for (;;)
4357 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004358 op = may_peek_next_line(cctx, *arg, &next);
4359 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360 break;
4361 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004362 if (next != NULL)
4363 {
4364 *arg = next_line_from_context(cctx, TRUE);
4365 op = skipwhite(*arg);
4366 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004368 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004370 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004371 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 }
4373
4374 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004375 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004376 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004378 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004379 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 return FAIL;
4381
Bram Moolenaara5565e42020-05-09 15:44:01 +02004382 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004383 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004384 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4385 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4386 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4387 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004389 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4390 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004391
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004392 // concat/subtract/add constant numbers
4393 if (*op == '+')
4394 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4395 else if (*op == '-')
4396 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4397 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004398 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004399 // concatenate constant strings
4400 char_u *s1 = tv1->vval.v_string;
4401 char_u *s2 = tv2->vval.v_string;
4402 size_t len1 = STRLEN(s1);
4403
4404 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4405 if (tv1->vval.v_string == NULL)
4406 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004407 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004408 return FAIL;
4409 }
4410 mch_memmove(tv1->vval.v_string, s1, len1);
4411 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004412 vim_free(s1);
4413 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004414 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004415 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 }
4417 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004418 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004419 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004420 if (*op == '.')
4421 {
4422 if (may_generate_2STRING(-2, cctx) == FAIL
4423 || may_generate_2STRING(-1, cctx) == FAIL)
4424 return FAIL;
4425 generate_instr_drop(cctx, ISN_CONCAT, 1);
4426 }
4427 else
4428 generate_two_op(cctx, op);
4429 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430 }
4431
4432 return OK;
4433}
4434
4435/*
4436 * expr5a == expr5b
4437 * expr5a =~ expr5b
4438 * expr5a != expr5b
4439 * expr5a !~ expr5b
4440 * expr5a > expr5b
4441 * expr5a >= expr5b
4442 * expr5a < expr5b
4443 * expr5a <= expr5b
4444 * expr5a is expr5b
4445 * expr5a isnot expr5b
4446 *
4447 * Produces instructions:
4448 * EVAL expr5a Push result of "expr5a"
4449 * EVAL expr5b Push result of "expr5b"
4450 * COMPARE one of the compare instructions
4451 */
4452 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004453compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454{
4455 exptype_T type = EXPR_UNKNOWN;
4456 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004457 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004460 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461
4462 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004463 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 return FAIL;
4465
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004466 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004467 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468
4469 /*
4470 * If there is a comparative operator, use it.
4471 */
4472 if (type != EXPR_UNKNOWN)
4473 {
4474 int ic = FALSE; // Default: do not ignore case
4475
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004476 if (next != NULL)
4477 {
4478 *arg = next_line_from_context(cctx, TRUE);
4479 p = skipwhite(*arg);
4480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 if (type_is && (p[len] == '?' || p[len] == '#'))
4482 {
4483 semsg(_(e_invexpr2), *arg);
4484 return FAIL;
4485 }
4486 // extra question mark appended: ignore case
4487 if (p[len] == '?')
4488 {
4489 ic = TRUE;
4490 ++len;
4491 }
4492 // extra '#' appended: match case (ignored)
4493 else if (p[len] == '#')
4494 ++len;
4495 // nothing appended: match case
4496
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004497 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004499 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004500 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501 }
4502
4503 // get the second variable
4504 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004505 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004506 return FAIL;
4507
Bram Moolenaara5565e42020-05-09 15:44:01 +02004508 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509 return FAIL;
4510
Bram Moolenaara5565e42020-05-09 15:44:01 +02004511 if (ppconst->pp_used == ppconst_used + 2)
4512 {
4513 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4514 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4515 int ret;
4516
4517 // Both sides are a constant, compute the result now.
4518 // First check for a valid combination of types, this is more
4519 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004520 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004521 ret = FAIL;
4522 else
4523 {
4524 ret = typval_compare(tv1, tv2, type, ic);
4525 tv1->v_type = VAR_BOOL;
4526 tv1->vval.v_number = tv1->vval.v_number
4527 ? VVAL_TRUE : VVAL_FALSE;
4528 clear_tv(tv2);
4529 --ppconst->pp_used;
4530 }
4531 return ret;
4532 }
4533
4534 generate_ppconst(cctx, ppconst);
4535 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 }
4537
4538 return OK;
4539}
4540
Bram Moolenaar7f141552020-05-09 17:35:53 +02004541static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4542
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543/*
4544 * Compile || or &&.
4545 */
4546 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004547compile_and_or(
4548 char_u **arg,
4549 cctx_T *cctx,
4550 char *op,
4551 ppconst_T *ppconst,
4552 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004554 char_u *next;
4555 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004556 int opchar = *op;
4557
4558 if (p[0] == opchar && p[1] == opchar)
4559 {
4560 garray_T *instr = &cctx->ctx_instr;
4561 garray_T end_ga;
4562
4563 /*
4564 * Repeat until there is no following "||" or "&&"
4565 */
4566 ga_init2(&end_ga, sizeof(int), 10);
4567 while (p[0] == opchar && p[1] == opchar)
4568 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004569 if (next != NULL)
4570 {
4571 *arg = next_line_from_context(cctx, TRUE);
4572 p = skipwhite(*arg);
4573 }
4574
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004575 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4576 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004578 return FAIL;
4579 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580
Bram Moolenaara5565e42020-05-09 15:44:01 +02004581 // TODO: use ppconst if the value is a constant
4582 generate_ppconst(cctx, ppconst);
4583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 if (ga_grow(&end_ga, 1) == FAIL)
4585 {
4586 ga_clear(&end_ga);
4587 return FAIL;
4588 }
4589 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4590 ++end_ga.ga_len;
4591 generate_JUMP(cctx, opchar == '|'
4592 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4593
4594 // eval the next expression
4595 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004596 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004597 return FAIL;
4598
Bram Moolenaara5565e42020-05-09 15:44:01 +02004599 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4600 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 {
4602 ga_clear(&end_ga);
4603 return FAIL;
4604 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004605
4606 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004608 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609
4610 // Fill in the end label in all jumps.
4611 while (end_ga.ga_len > 0)
4612 {
4613 isn_T *isn;
4614
4615 --end_ga.ga_len;
4616 isn = ((isn_T *)instr->ga_data)
4617 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4618 isn->isn_arg.jump.jump_where = instr->ga_len;
4619 }
4620 ga_clear(&end_ga);
4621 }
4622
4623 return OK;
4624}
4625
4626/*
4627 * expr4a && expr4a && expr4a logical AND
4628 *
4629 * Produces instructions:
4630 * EVAL expr4a Push result of "expr4a"
4631 * JUMP_AND_KEEP_IF_FALSE end
4632 * EVAL expr4b Push result of "expr4b"
4633 * JUMP_AND_KEEP_IF_FALSE end
4634 * EVAL expr4c Push result of "expr4c"
4635 * end:
4636 */
4637 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004638compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004640 int ppconst_used = ppconst->pp_used;
4641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004643 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 return FAIL;
4645
4646 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004647 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648}
4649
4650/*
4651 * expr3a || expr3b || expr3c logical OR
4652 *
4653 * Produces instructions:
4654 * EVAL expr3a Push result of "expr3a"
4655 * JUMP_AND_KEEP_IF_TRUE end
4656 * EVAL expr3b Push result of "expr3b"
4657 * JUMP_AND_KEEP_IF_TRUE end
4658 * EVAL expr3c Push result of "expr3c"
4659 * end:
4660 */
4661 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004662compile_expr2(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 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004667 if (compile_expr3(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 * Toplevel expression: expr2 ? expr1a : expr1b
4676 *
4677 * Produces instructions:
4678 * EVAL expr2 Push result of "expr"
4679 * JUMP_IF_FALSE alt jump if false
4680 * EVAL expr1a
4681 * JUMP_ALWAYS end
4682 * alt: EVAL expr1b
4683 * end:
4684 */
4685 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004686compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687{
4688 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004689 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004690 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691
Bram Moolenaar61a89812020-05-07 16:58:17 +02004692 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004693 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 return FAIL;
4695
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004696 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 if (*p == '?')
4698 {
4699 garray_T *instr = &cctx->ctx_instr;
4700 garray_T *stack = &cctx->ctx_type_stack;
4701 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004702 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004704 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004706 int has_const_expr = FALSE;
4707 int const_value = FALSE;
4708 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004710 if (next != NULL)
4711 {
4712 *arg = next_line_from_context(cctx, TRUE);
4713 p = skipwhite(*arg);
4714 }
4715
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004716 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4717 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004719 return FAIL;
4720 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721
Bram Moolenaara5565e42020-05-09 15:44:01 +02004722 if (ppconst->pp_used == ppconst_used + 1)
4723 {
4724 // the condition is a constant, we know whether the ? or the :
4725 // expression is to be evaluated.
4726 has_const_expr = TRUE;
4727 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4728 clear_tv(&ppconst->pp_tv[ppconst_used]);
4729 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004730 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4731 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004732 }
4733 else
4734 {
4735 generate_ppconst(cctx, ppconst);
4736 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4737 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738
4739 // evaluate the second expression; any type is accepted
4740 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004741 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004742 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004743 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004744 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745
Bram Moolenaara5565e42020-05-09 15:44:01 +02004746 if (!has_const_expr)
4747 {
4748 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749
Bram Moolenaara5565e42020-05-09 15:44:01 +02004750 // remember the type and drop it
4751 --stack->ga_len;
4752 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753
Bram Moolenaara5565e42020-05-09 15:44:01 +02004754 end_idx = instr->ga_len;
4755 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4756
4757 // jump here from JUMP_IF_FALSE
4758 isn = ((isn_T *)instr->ga_data) + alt_idx;
4759 isn->isn_arg.jump.jump_where = instr->ga_len;
4760 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761
4762 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004763 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764 if (*p != ':')
4765 {
4766 emsg(_(e_missing_colon));
4767 return FAIL;
4768 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004769 if (next != NULL)
4770 {
4771 *arg = next_line_from_context(cctx, TRUE);
4772 p = skipwhite(*arg);
4773 }
4774
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004775 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4776 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004778 return FAIL;
4779 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780
4781 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004782 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004783 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4784 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004786 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004787 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004788 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004789 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790
Bram Moolenaara5565e42020-05-09 15:44:01 +02004791 if (!has_const_expr)
4792 {
4793 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794
Bram Moolenaara5565e42020-05-09 15:44:01 +02004795 // If the types differ, the result has a more generic type.
4796 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4797 common_type(type1, type2, &type2, cctx->ctx_type_list);
4798
4799 // jump here from JUMP_ALWAYS
4800 isn = ((isn_T *)instr->ga_data) + end_idx;
4801 isn->isn_arg.jump.jump_where = instr->ga_len;
4802 }
4803
4804 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805 }
4806 return OK;
4807}
4808
4809/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004810 * Toplevel expression.
4811 */
4812 static int
4813compile_expr0(char_u **arg, cctx_T *cctx)
4814{
4815 ppconst_T ppconst;
4816
4817 CLEAR_FIELD(ppconst);
4818 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4819 {
4820 clear_ppconst(&ppconst);
4821 return FAIL;
4822 }
4823 if (generate_ppconst(cctx, &ppconst) == FAIL)
4824 return FAIL;
4825 return OK;
4826}
4827
4828/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 * compile "return [expr]"
4830 */
4831 static char_u *
4832compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4833{
4834 char_u *p = arg;
4835 garray_T *stack = &cctx->ctx_type_stack;
4836 type_T *stack_type;
4837
4838 if (*p != NUL && *p != '|' && *p != '\n')
4839 {
4840 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004841 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 return NULL;
4843
4844 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4845 if (set_return_type)
4846 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004847 else
4848 {
4849 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4850 && stack_type->tt_type != VAR_VOID
4851 && stack_type->tt_type != VAR_UNKNOWN)
4852 {
4853 emsg(_("E1096: Returning a value in a function without a return type"));
4854 return NULL;
4855 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004856 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4857 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860 }
4861 else
4862 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004863 // "set_return_type" cannot be TRUE, only used for a lambda which
4864 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004865 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4866 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867 {
4868 emsg(_("E1003: Missing return value"));
4869 return NULL;
4870 }
4871
4872 // No argument, return zero.
4873 generate_PUSHNR(cctx, 0);
4874 }
4875
4876 if (generate_instr(cctx, ISN_RETURN) == NULL)
4877 return NULL;
4878
4879 // "return val | endif" is possible
4880 return skipwhite(p);
4881}
4882
4883/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004884 * Get a line from the compilation context, compatible with exarg_T getline().
4885 * Return a pointer to the line in allocated memory.
4886 * Return NULL for end-of-file or some error.
4887 */
4888 static char_u *
4889exarg_getline(
4890 int c UNUSED,
4891 void *cookie,
4892 int indent UNUSED,
4893 int do_concat UNUSED)
4894{
4895 cctx_T *cctx = (cctx_T *)cookie;
4896
4897 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4898 {
4899 iemsg("Heredoc got to end");
4900 return NULL;
4901 }
4902 ++cctx->ctx_lnum;
4903 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4904 [cctx->ctx_lnum]);
4905}
4906
4907/*
4908 * Compile a nested :def command.
4909 */
4910 static char_u *
4911compile_nested_function(exarg_T *eap, cctx_T *cctx)
4912{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004913 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004914 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004915 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004916 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004917 lvar_T *lvar;
4918 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004919 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004920
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004921 // Only g:Func() can use a namespace.
4922 if (name_start[1] == ':' && !is_global)
4923 {
4924 semsg(_(e_namespace), name_start);
4925 return NULL;
4926 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004927 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4928 return NULL;
4929
Bram Moolenaar04b12692020-05-04 23:24:44 +02004930 eap->arg = name_end;
4931 eap->getline = exarg_getline;
4932 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004933 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004934 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004935 lambda_name = get_lambda_name();
4936 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004937
Bram Moolenaar822ba242020-05-24 23:00:18 +02004938 if (ufunc == NULL)
4939 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004940 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004941 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004942 return NULL;
4943
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004944 if (is_global)
4945 {
4946 char_u *func_name = vim_strnsave(name_start + 2,
4947 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004948
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004949 if (func_name == NULL)
4950 r = FAIL;
4951 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004952 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004953 }
4954 else
4955 {
4956 // Define a local variable for the function reference.
4957 lvar = reserve_local(cctx, name_start, name_end - name_start,
4958 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004959 if (lvar == NULL)
4960 return NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004961 if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL)
4962 return NULL;
4963 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4964 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004965
Bram Moolenaar61a89812020-05-07 16:58:17 +02004966 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004967 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004968}
4969
4970/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971 * Return the length of an assignment operator, or zero if there isn't one.
4972 */
4973 int
4974assignment_len(char_u *p, int *heredoc)
4975{
4976 if (*p == '=')
4977 {
4978 if (p[1] == '<' && p[2] == '<')
4979 {
4980 *heredoc = TRUE;
4981 return 3;
4982 }
4983 return 1;
4984 }
4985 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4986 return 2;
4987 if (STRNCMP(p, "..=", 3) == 0)
4988 return 3;
4989 return 0;
4990}
4991
4992// words that cannot be used as a variable
4993static char *reserved[] = {
4994 "true",
4995 "false",
4996 NULL
4997};
4998
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004999typedef enum {
5000 dest_local,
5001 dest_option,
5002 dest_env,
5003 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005004 dest_buffer,
5005 dest_window,
5006 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005007 dest_vimvar,
5008 dest_script,
5009 dest_reg,
5010} assign_dest_T;
5011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005013 * Generate the load instruction for "name".
5014 */
5015 static void
5016generate_loadvar(
5017 cctx_T *cctx,
5018 assign_dest_T dest,
5019 char_u *name,
5020 lvar_T *lvar,
5021 type_T *type)
5022{
5023 switch (dest)
5024 {
5025 case dest_option:
5026 // TODO: check the option exists
5027 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5028 break;
5029 case dest_global:
5030 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5031 break;
5032 case dest_buffer:
5033 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5034 break;
5035 case dest_window:
5036 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5037 break;
5038 case dest_tab:
5039 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5040 break;
5041 case dest_script:
5042 compile_load_scriptvar(cctx,
5043 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5044 break;
5045 case dest_env:
5046 // Include $ in the name here
5047 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5048 break;
5049 case dest_reg:
5050 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5051 break;
5052 case dest_vimvar:
5053 generate_LOADV(cctx, name + 2, TRUE);
5054 break;
5055 case dest_local:
5056 if (lvar->lv_from_outer)
5057 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5058 NULL, type);
5059 else
5060 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5061 break;
5062 }
5063}
5064
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005065 void
5066vim9_declare_error(char_u *name)
5067{
5068 char *scope = "";
5069
5070 switch (*name)
5071 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005072 case 'g': scope = _("global"); break;
5073 case 'b': scope = _("buffer"); break;
5074 case 'w': scope = _("window"); break;
5075 case 't': scope = _("tab"); break;
5076 case 'v': scope = "v:"; break;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005077 case '$': semsg(_(e_declare_env_var), name);
5078 return;
5079 case '&': semsg(_("E1052: Cannot declare an option: %s"), name);
5080 return;
5081 case '@': semsg(_("E1066: Cannot declare a register: %s"), name);
5082 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005083 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005084 }
5085 semsg(_(e_declare_var), scope, name);
5086}
5087
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005088/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005089 * Compile declaration and assignment:
5090 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005092 * Return NULL for an error.
5093 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094 */
5095 static char_u *
5096compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5097{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005098 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005099 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005100 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101 char_u *ret = NULL;
5102 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005103 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005104 int scriptvar_sid = 0;
5105 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005107 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005108 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 int oplen = 0;
5111 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005112 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005113 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005114 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005115 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005117
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005118 // Skip over the "var" or "[var, var]" to get to any "=".
5119 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5120 if (p == NULL)
5121 return *arg == '[' ? arg : NULL;
5122
5123 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005124 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005125 // TODO: should we allow this, and figure out type inference from list
5126 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005127 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005128 return NULL;
5129 }
5130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131 sp = p;
5132 p = skipwhite(p);
5133 op = p;
5134 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005135
5136 if (var_count > 0 && oplen == 0)
5137 // can be something like "[1, 2]->func()"
5138 return arg;
5139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005140 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5141 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005142 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005143 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005144 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005145
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146 if (heredoc)
5147 {
5148 list_T *l;
5149 listitem_T *li;
5150
5151 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005152 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005153 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005154 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005155
5156 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005157 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005158 {
5159 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5160 li->li_tv.vval.v_string = NULL;
5161 }
5162 generate_NEWLIST(cctx, l->lv_len);
5163 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005164 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005165 list_free(l);
5166 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005167 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005168 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005169 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005170 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005171 // for "[var, var] = expr" evaluate the expression here, loop over the
5172 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005173
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005174 p = skipwhite(op + oplen);
5175 if (compile_expr0(&p, cctx) == FAIL)
5176 return NULL;
5177 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005179 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005181 type_T *stacktype;
5182
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005183 stacktype = stack->ga_len == 0 ? &t_void
5184 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005185 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005186 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005187 emsg(_(e_cannot_use_void));
5188 goto theend;
5189 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005190 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005191 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005192 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005193 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5194 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005195 }
5196 }
5197
5198 /*
5199 * Loop over variables in "[var, var] = expr".
5200 * For "var = expr" and "let var: type" this is done only once.
5201 */
5202 if (var_count > 0)
5203 var_start = skipwhite(arg + 1); // skip over the "["
5204 else
5205 var_start = arg;
5206 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5207 {
5208 char_u *var_end = skip_var_one(var_start, FALSE);
5209 size_t varlen;
5210 int new_local = FALSE;
5211 int opt_type;
5212 int opt_flags = 0;
5213 assign_dest_T dest = dest_local;
5214 int vimvaridx = -1;
5215 lvar_T *lvar = NULL;
5216 lvar_T arg_lvar;
5217 int has_type = FALSE;
5218 int has_index = FALSE;
5219 int instr_count = -1;
5220
Bram Moolenaar65821722020-08-02 18:58:54 +02005221 if (*var_start == '@')
5222 p = var_start + 2;
5223 else
5224 {
5225 p = (*var_start == '&' || *var_start == '$')
5226 ? var_start + 1 : var_start;
5227 p = to_name_end(p, TRUE);
5228 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005229
5230 // "a: type" is declaring variable "a" with a type, not "a:".
5231 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5232 --var_end;
5233 if (is_decl && p == var_start + 2 && p[-1] == ':')
5234 --p;
5235
5236 varlen = p - var_start;
5237 vim_free(name);
5238 name = vim_strnsave(var_start, varlen);
5239 if (name == NULL)
5240 return NULL;
5241 if (!heredoc)
5242 type = &t_any;
5243
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005244 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005245 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005246 int declare_error = FALSE;
5247
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005248 if (*var_start == '&')
5249 {
5250 int cc;
5251 long numval;
5252
5253 dest = dest_option;
5254 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005255 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005256 emsg(_(e_const_option));
5257 goto theend;
5258 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005259 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005260 p = var_start;
5261 p = find_option_end(&p, &opt_flags);
5262 if (p == NULL)
5263 {
5264 // cannot happen?
5265 emsg(_(e_letunexp));
5266 goto theend;
5267 }
5268 cc = *p;
5269 *p = NUL;
5270 opt_type = get_option_value(var_start + 1, &numval,
5271 NULL, opt_flags);
5272 *p = cc;
5273 if (opt_type == -3)
5274 {
5275 semsg(_(e_unknown_option), var_start);
5276 goto theend;
5277 }
5278 if (opt_type == -2 || opt_type == 0)
5279 type = &t_string;
5280 else
5281 type = &t_number; // both number and boolean option
5282 }
5283 else if (*var_start == '$')
5284 {
5285 dest = dest_env;
5286 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005287 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005288 }
5289 else if (*var_start == '@')
5290 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005291 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005292 {
5293 emsg_invreg(var_start[1]);
5294 goto theend;
5295 }
5296 dest = dest_reg;
5297 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005298 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005299 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005300 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005301 {
5302 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005303 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005304 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005305 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005306 {
5307 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005308 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005309 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005310 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005311 {
5312 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005313 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005314 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005315 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005316 {
5317 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005318 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005319 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005320 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005321 {
5322 typval_T *vtv;
5323 int di_flags;
5324
5325 vimvaridx = find_vim_var(name + 2, &di_flags);
5326 if (vimvaridx < 0)
5327 {
5328 semsg(_(e_var_notfound), var_start);
5329 goto theend;
5330 }
5331 // We use the current value of "sandbox" here, is that OK?
5332 if (var_check_ro(di_flags, name, FALSE))
5333 goto theend;
5334 dest = dest_vimvar;
5335 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005336 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005337 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005338 }
5339 else
5340 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005341 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005342
5343 for (idx = 0; reserved[idx] != NULL; ++idx)
5344 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005345 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005346 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005347 goto theend;
5348 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005349
5350 lvar = lookup_local(var_start, varlen, cctx);
5351 if (lvar == NULL)
5352 {
5353 CLEAR_FIELD(arg_lvar);
5354 if (lookup_arg(var_start, varlen,
5355 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5356 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005357 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005358 if (is_decl)
5359 {
5360 semsg(_(e_used_as_arg), name);
5361 goto theend;
5362 }
5363 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005364 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005365 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005366 if (lvar != NULL)
5367 {
5368 if (is_decl)
5369 {
5370 semsg(_("E1017: Variable already declared: %s"), name);
5371 goto theend;
5372 }
5373 else if (lvar->lv_const)
5374 {
5375 semsg(_("E1018: Cannot assign to a constant: %s"),
5376 name);
5377 goto theend;
5378 }
5379 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005380 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005381 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005382 int script_namespace = varlen > 1
5383 && STRNCMP(var_start, "s:", 2) == 0;
5384 int script_var = (script_namespace
5385 ? lookup_script(var_start + 2, varlen - 2)
5386 : lookup_script(var_start, varlen)) == OK;
5387 imported_T *import =
5388 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005389
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005390 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005391 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005392 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5393
5394 if (is_decl)
5395 {
5396 if (script_namespace)
5397 semsg(_("E1101: Cannot declare a script variable in a function: %s"),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005398 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005399 else
5400 semsg(_("E1054: Variable already declared in the script: %s"),
5401 name);
5402 goto theend;
5403 }
5404 else if (cctx->ctx_ufunc->uf_script_ctx_version
5405 == SCRIPT_VERSION_VIM9
5406 && script_namespace
5407 && !script_var && import == NULL)
5408 {
5409 semsg(_(e_unknown_var), name);
5410 goto theend;
5411 }
5412
5413 dest = dest_script;
5414
5415 // existing script-local variables should have a type
5416 scriptvar_sid = current_sctx.sc_sid;
5417 if (import != NULL)
5418 scriptvar_sid = import->imp_sid;
5419 scriptvar_idx = get_script_item_idx(scriptvar_sid,
5420 rawname, TRUE);
5421 if (scriptvar_idx >= 0)
5422 {
5423 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5424 svar_T *sv =
5425 ((svar_T *)si->sn_var_vals.ga_data)
5426 + scriptvar_idx;
5427 type = sv->sv_type;
5428 }
5429 }
5430 else if (name[1] == ':' && name[2] != NUL)
5431 {
5432 semsg(_("E1082: Cannot use a namespaced variable: %s"),
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005433 name);
5434 goto theend;
5435 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005436 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005437 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005438 semsg(_(e_unknown_var), name);
5439 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005440 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005441 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005442 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005443
5444 if (declare_error)
5445 {
5446 vim9_declare_error(name);
5447 goto theend;
5448 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005449 }
5450
5451 // handle "a:name" as a name, not index "name" on "a"
5452 if (varlen > 1 || var_start[varlen] != ':')
5453 p = var_end;
5454
5455 if (dest != dest_option)
5456 {
5457 if (is_decl && *p == ':')
5458 {
5459 // parse optional type: "let var: type = expr"
5460 if (!VIM_ISWHITE(p[1]))
5461 {
5462 semsg(_(e_white_after), ":");
5463 goto theend;
5464 }
5465 p = skipwhite(p + 1);
5466 type = parse_type(&p, cctx->ctx_type_list);
5467 has_type = TRUE;
5468 }
5469 else if (lvar != NULL)
5470 type = lvar->lv_type;
5471 }
5472
5473 if (oplen == 3 && !heredoc && dest != dest_global
5474 && type->tt_type != VAR_STRING
5475 && type->tt_type != VAR_ANY)
5476 {
5477 emsg(_("E1019: Can only concatenate to string"));
5478 goto theend;
5479 }
5480
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005481 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005482 {
5483 if (oplen > 1 && !heredoc)
5484 {
5485 // +=, /=, etc. require an existing variable
5486 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5487 name);
5488 goto theend;
5489 }
5490
5491 // new local variable
5492 if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
5493 goto theend;
5494 lvar = reserve_local(cctx, var_start, varlen,
5495 cmdidx == CMD_const, type);
5496 if (lvar == NULL)
5497 goto theend;
5498 new_local = TRUE;
5499 }
5500
5501 member_type = type;
5502 if (var_end > var_start + varlen)
5503 {
5504 // Something follows after the variable: "var[idx]".
5505 if (is_decl)
5506 {
5507 emsg(_("E1087: cannot use an index when declaring a variable"));
5508 goto theend;
5509 }
5510
5511 if (var_start[varlen] == '[')
5512 {
5513 has_index = TRUE;
5514 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005515 member_type = &t_any;
5516 else
5517 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005518 }
5519 else
5520 {
5521 semsg("Not supported yet: %s", var_start);
5522 goto theend;
5523 }
5524 }
5525 else if (lvar == &arg_lvar)
5526 {
5527 semsg(_("E1090: Cannot assign to argument %s"), name);
5528 goto theend;
5529 }
5530
5531 if (!heredoc)
5532 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005533 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005534 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005535 if (oplen > 0 && var_count == 0)
5536 {
5537 // skip over the "=" and the expression
5538 p = skipwhite(op + oplen);
5539 compile_expr0(&p, cctx);
5540 }
5541 }
5542 else if (oplen > 0)
5543 {
5544 type_T *stacktype;
5545
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005546 // For "var = expr" evaluate the expression.
5547 if (var_count == 0)
5548 {
5549 int r;
5550
5551 // for "+=", "*=", "..=" etc. first load the current value
5552 if (*op != '=')
5553 {
5554 generate_loadvar(cctx, dest, name, lvar, type);
5555
5556 if (has_index)
5557 {
5558 // TODO: get member from list or dict
5559 emsg("Index with operation not supported yet");
5560 goto theend;
5561 }
5562 }
5563
5564 // Compile the expression. Temporarily hide the new local
5565 // variable here, it is not available to this expression.
5566 if (new_local)
5567 --cctx->ctx_locals.ga_len;
5568 instr_count = instr->ga_len;
5569 p = skipwhite(op + oplen);
5570 r = compile_expr0(&p, cctx);
5571 if (new_local)
5572 ++cctx->ctx_locals.ga_len;
5573 if (r == FAIL)
5574 goto theend;
5575 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005576 else if (semicolon && var_idx == var_count - 1)
5577 {
5578 // For "[var; var] = expr" get the rest of the list
5579 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5580 goto theend;
5581 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005582 else
5583 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005584 // For "[var, var] = expr" get the "var_idx" item from the
5585 // list.
5586 if (generate_GETITEM(cctx, var_idx) == FAIL)
5587 return FAIL;
5588 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005589
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005590 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005591 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005592 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005593 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005594 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005595 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005596 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005597 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005598 emsg(_(e_cannot_use_void));
5599 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005600 }
5601 else
5602 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005603 // An empty list or dict has a &t_void member,
5604 // for a variable that implies &t_any.
5605 if (stacktype == &t_list_empty)
5606 lvar->lv_type = &t_list_any;
5607 else if (stacktype == &t_dict_empty)
5608 lvar->lv_type = &t_dict_any;
5609 else
5610 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005611 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005612 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005613 else
5614 {
5615 type_T *use_type = lvar->lv_type;
5616
5617 if (has_index)
5618 {
5619 use_type = use_type->tt_member;
5620 if (use_type == NULL)
5621 use_type = &t_void;
5622 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005623 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005624 == FAIL)
5625 goto theend;
5626 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005627 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005628 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005629 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005630 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005631 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005632 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005633 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005634 emsg(_(e_const_req_value));
5635 goto theend;
5636 }
5637 else if (!has_type || dest == dest_option)
5638 {
5639 emsg(_(e_type_req));
5640 goto theend;
5641 }
5642 else
5643 {
5644 // variables are always initialized
5645 if (ga_grow(instr, 1) == FAIL)
5646 goto theend;
5647 switch (member_type->tt_type)
5648 {
5649 case VAR_BOOL:
5650 generate_PUSHBOOL(cctx, VVAL_FALSE);
5651 break;
5652 case VAR_FLOAT:
5653#ifdef FEAT_FLOAT
5654 generate_PUSHF(cctx, 0.0);
5655#endif
5656 break;
5657 case VAR_STRING:
5658 generate_PUSHS(cctx, NULL);
5659 break;
5660 case VAR_BLOB:
5661 generate_PUSHBLOB(cctx, NULL);
5662 break;
5663 case VAR_FUNC:
5664 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5665 break;
5666 case VAR_LIST:
5667 generate_NEWLIST(cctx, 0);
5668 break;
5669 case VAR_DICT:
5670 generate_NEWDICT(cctx, 0);
5671 break;
5672 case VAR_JOB:
5673 generate_PUSHJOB(cctx, NULL);
5674 break;
5675 case VAR_CHANNEL:
5676 generate_PUSHCHANNEL(cctx, NULL);
5677 break;
5678 case VAR_NUMBER:
5679 case VAR_UNKNOWN:
5680 case VAR_ANY:
5681 case VAR_PARTIAL:
5682 case VAR_VOID:
5683 case VAR_SPECIAL: // cannot happen
5684 generate_PUSHNR(cctx, 0);
5685 break;
5686 }
5687 }
5688 if (var_count == 0)
5689 end = p;
5690 }
5691
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005692 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005693 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005694 break;
5695
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005696 if (oplen > 0 && *op != '=')
5697 {
5698 type_T *expected = &t_number;
5699 type_T *stacktype;
5700
5701 // TODO: if type is known use float or any operation
5702
5703 if (*op == '.')
5704 expected = &t_string;
5705 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005706 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005707 goto theend;
5708
5709 if (*op == '.')
5710 generate_instr_drop(cctx, ISN_CONCAT, 1);
5711 else
5712 {
5713 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5714
5715 if (isn == NULL)
5716 goto theend;
5717 switch (*op)
5718 {
5719 case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
5720 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5721 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5722 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5723 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5724 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005725 }
5726 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005727
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005728 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005729 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005730 int r;
5731
5732 // Compile the "idx" in "var[idx]".
5733 if (new_local)
5734 --cctx->ctx_locals.ga_len;
5735 p = skipwhite(var_start + varlen + 1);
5736 r = compile_expr0(&p, cctx);
5737 if (new_local)
5738 ++cctx->ctx_locals.ga_len;
5739 if (r == FAIL)
5740 goto theend;
5741 if (*skipwhite(p) != ']')
5742 {
5743 emsg(_(e_missbrac));
5744 goto theend;
5745 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005746 if (type == &t_any)
5747 {
5748 type_T *idx_type = ((type_T **)stack->ga_data)[
5749 stack->ga_len - 1];
5750 // Index on variable of unknown type: guess the type from the
5751 // index type: number is dict, otherwise dict.
5752 // TODO: should do the assignment at runtime
5753 if (idx_type->tt_type == VAR_NUMBER)
5754 type = &t_list_any;
5755 else
5756 type = &t_dict_any;
5757 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005758 if (type->tt_type == VAR_DICT
5759 && may_generate_2STRING(-1, cctx) == FAIL)
5760 goto theend;
5761 if (type->tt_type == VAR_LIST
5762 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005763 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005764 {
5765 emsg(_(e_number_exp));
5766 goto theend;
5767 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005768
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005769 // Load the dict or list. On the stack we then have:
5770 // - value
5771 // - index
5772 // - variable
5773 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005774
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005775 if (type->tt_type == VAR_LIST)
5776 {
5777 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5778 return FAIL;
5779 }
5780 else if (type->tt_type == VAR_DICT)
5781 {
5782 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5783 return FAIL;
5784 }
5785 else
5786 {
5787 emsg(_(e_listreq));
5788 goto theend;
5789 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005790 }
5791 else
5792 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005793 switch (dest)
5794 {
5795 case dest_option:
5796 generate_STOREOPT(cctx, name + 1, opt_flags);
5797 break;
5798 case dest_global:
5799 // include g: with the name, easier to execute that way
5800 generate_STORE(cctx, ISN_STOREG, 0, name);
5801 break;
5802 case dest_buffer:
5803 // include b: with the name, easier to execute that way
5804 generate_STORE(cctx, ISN_STOREB, 0, name);
5805 break;
5806 case dest_window:
5807 // include w: with the name, easier to execute that way
5808 generate_STORE(cctx, ISN_STOREW, 0, name);
5809 break;
5810 case dest_tab:
5811 // include t: with the name, easier to execute that way
5812 generate_STORE(cctx, ISN_STORET, 0, name);
5813 break;
5814 case dest_env:
5815 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5816 break;
5817 case dest_reg:
5818 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5819 break;
5820 case dest_vimvar:
5821 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5822 break;
5823 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005824 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005825 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005826 {
5827 char_u *name_s = name;
5828
5829 // Include s: in the name for store_var()
5830 if (name[1] != ':')
5831 {
5832 int len = (int)STRLEN(name) + 3;
5833
5834 name_s = alloc(len);
5835 if (name_s == NULL)
5836 name_s = name;
5837 else
5838 vim_snprintf((char *)name_s, len,
5839 "s:%s", name);
5840 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005841 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5842 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005843 if (name_s != name)
5844 vim_free(name_s);
5845 }
5846 else
5847 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005848 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005849 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005850 break;
5851 case dest_local:
5852 if (lvar != NULL)
5853 {
5854 isn_T *isn = ((isn_T *)instr->ga_data)
5855 + instr->ga_len - 1;
5856
5857 // optimization: turn "var = 123" from ISN_PUSHNR +
5858 // ISN_STORE into ISN_STORENR
5859 if (!lvar->lv_from_outer
5860 && instr->ga_len == instr_count + 1
5861 && isn->isn_type == ISN_PUSHNR)
5862 {
5863 varnumber_T val = isn->isn_arg.number;
5864
5865 isn->isn_type = ISN_STORENR;
5866 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5867 isn->isn_arg.storenr.stnr_val = val;
5868 if (stack->ga_len > 0)
5869 --stack->ga_len;
5870 }
5871 else if (lvar->lv_from_outer)
5872 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005873 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005874 else
5875 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5876 }
5877 break;
5878 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005879 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005880
5881 if (var_idx + 1 < var_count)
5882 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005883 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005884
5885 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005886 if (var_count > 0 && !semicolon)
5887 {
5888 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5889 goto theend;
5890 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005891
Bram Moolenaarb2097502020-07-19 17:17:02 +02005892 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005893
5894theend:
5895 vim_free(name);
5896 return ret;
5897}
5898
5899/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005900 * Check if "name" can be "unlet".
5901 */
5902 int
5903check_vim9_unlet(char_u *name)
5904{
5905 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5906 {
5907 semsg(_("E1081: Cannot unlet %s"), name);
5908 return FAIL;
5909 }
5910 return OK;
5911}
5912
5913/*
5914 * Callback passed to ex_unletlock().
5915 */
5916 static int
5917compile_unlet(
5918 lval_T *lvp,
5919 char_u *name_end,
5920 exarg_T *eap,
5921 int deep UNUSED,
5922 void *coookie)
5923{
5924 cctx_T *cctx = coookie;
5925
5926 if (lvp->ll_tv == NULL)
5927 {
5928 char_u *p = lvp->ll_name;
5929 int cc = *name_end;
5930 int ret = OK;
5931
5932 // Normal name. Only supports g:, w:, t: and b: namespaces.
5933 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005934 if (*p == '$')
5935 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5936 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005937 ret = FAIL;
5938 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005939 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005940
5941 *name_end = cc;
5942 return ret;
5943 }
5944
5945 // TODO: unlet {list}[idx]
5946 // TODO: unlet {dict}[key]
5947 emsg("Sorry, :unlet not fully implemented yet");
5948 return FAIL;
5949}
5950
5951/*
5952 * compile "unlet var", "lock var" and "unlock var"
5953 * "arg" points to "var".
5954 */
5955 static char_u *
5956compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5957{
5958 char_u *p = arg;
5959
5960 if (eap->cmdidx != CMD_unlet)
5961 {
5962 emsg("Sorry, :lock and unlock not implemented yet");
5963 return NULL;
5964 }
5965
5966 if (*p == '!')
5967 {
5968 p = skipwhite(p + 1);
5969 eap->forceit = TRUE;
5970 }
5971
5972 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5973 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5974}
5975
5976/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977 * Compile an :import command.
5978 */
5979 static char_u *
5980compile_import(char_u *arg, cctx_T *cctx)
5981{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005982 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005983}
5984
5985/*
5986 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5987 */
5988 static int
5989compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5990{
5991 garray_T *instr = &cctx->ctx_instr;
5992 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5993
5994 if (endlabel == NULL)
5995 return FAIL;
5996 endlabel->el_next = *el;
5997 *el = endlabel;
5998 endlabel->el_end_label = instr->ga_len;
5999
6000 generate_JUMP(cctx, when, 0);
6001 return OK;
6002}
6003
6004 static void
6005compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6006{
6007 garray_T *instr = &cctx->ctx_instr;
6008
6009 while (*el != NULL)
6010 {
6011 endlabel_T *cur = (*el);
6012 isn_T *isn;
6013
6014 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6015 isn->isn_arg.jump.jump_where = instr->ga_len;
6016 *el = cur->el_next;
6017 vim_free(cur);
6018 }
6019}
6020
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006021 static void
6022compile_free_jump_to_end(endlabel_T **el)
6023{
6024 while (*el != NULL)
6025 {
6026 endlabel_T *cur = (*el);
6027
6028 *el = cur->el_next;
6029 vim_free(cur);
6030 }
6031}
6032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006033/*
6034 * Create a new scope and set up the generic items.
6035 */
6036 static scope_T *
6037new_scope(cctx_T *cctx, scopetype_T type)
6038{
6039 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6040
6041 if (scope == NULL)
6042 return NULL;
6043 scope->se_outer = cctx->ctx_scope;
6044 cctx->ctx_scope = scope;
6045 scope->se_type = type;
6046 scope->se_local_count = cctx->ctx_locals.ga_len;
6047 return scope;
6048}
6049
6050/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006051 * Free the current scope and go back to the outer scope.
6052 */
6053 static void
6054drop_scope(cctx_T *cctx)
6055{
6056 scope_T *scope = cctx->ctx_scope;
6057
6058 if (scope == NULL)
6059 {
6060 iemsg("calling drop_scope() without a scope");
6061 return;
6062 }
6063 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006064 switch (scope->se_type)
6065 {
6066 case IF_SCOPE:
6067 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6068 case FOR_SCOPE:
6069 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6070 case WHILE_SCOPE:
6071 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6072 case TRY_SCOPE:
6073 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6074 case NO_SCOPE:
6075 case BLOCK_SCOPE:
6076 break;
6077 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006078 vim_free(scope);
6079}
6080
6081/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006082 * compile "if expr"
6083 *
6084 * "if expr" Produces instructions:
6085 * EVAL expr Push result of "expr"
6086 * JUMP_IF_FALSE end
6087 * ... body ...
6088 * end:
6089 *
6090 * "if expr | else" Produces instructions:
6091 * EVAL expr Push result of "expr"
6092 * JUMP_IF_FALSE else
6093 * ... body ...
6094 * JUMP_ALWAYS end
6095 * else:
6096 * ... body ...
6097 * end:
6098 *
6099 * "if expr1 | elseif expr2 | else" Produces instructions:
6100 * EVAL expr Push result of "expr"
6101 * JUMP_IF_FALSE elseif
6102 * ... body ...
6103 * JUMP_ALWAYS end
6104 * elseif:
6105 * EVAL expr Push result of "expr"
6106 * JUMP_IF_FALSE else
6107 * ... body ...
6108 * JUMP_ALWAYS end
6109 * else:
6110 * ... body ...
6111 * end:
6112 */
6113 static char_u *
6114compile_if(char_u *arg, cctx_T *cctx)
6115{
6116 char_u *p = arg;
6117 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006118 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006119 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006120 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006121 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006122
Bram Moolenaara5565e42020-05-09 15:44:01 +02006123 CLEAR_FIELD(ppconst);
6124 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006125 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006126 clear_ppconst(&ppconst);
6127 return NULL;
6128 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006129 if (cctx->ctx_skip == SKIP_YES)
6130 clear_ppconst(&ppconst);
6131 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006132 {
6133 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006134 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006135 clear_ppconst(&ppconst);
6136 }
6137 else
6138 {
6139 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006140 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006141 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006142 return NULL;
6143 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006144
6145 scope = new_scope(cctx, IF_SCOPE);
6146 if (scope == NULL)
6147 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006148 scope->se_skip_save = skip_save;
6149 // "is_had_return" will be reset if any block does not end in :return
6150 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006151
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006152 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006153 {
6154 // "where" is set when ":elseif", "else" or ":endif" is found
6155 scope->se_u.se_if.is_if_label = instr->ga_len;
6156 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6157 }
6158 else
6159 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006160
6161 return p;
6162}
6163
6164 static char_u *
6165compile_elseif(char_u *arg, cctx_T *cctx)
6166{
6167 char_u *p = arg;
6168 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006169 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006170 isn_T *isn;
6171 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006172 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006173
6174 if (scope == NULL || scope->se_type != IF_SCOPE)
6175 {
6176 emsg(_(e_elseif_without_if));
6177 return NULL;
6178 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006179 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006180 if (!cctx->ctx_had_return)
6181 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006182
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006183 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006184 {
6185 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006186 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006187 return NULL;
6188 // previous "if" or "elseif" jumps here
6189 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6190 isn->isn_arg.jump.jump_where = instr->ga_len;
6191 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006192
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006193 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006194 CLEAR_FIELD(ppconst);
6195 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006196 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006197 clear_ppconst(&ppconst);
6198 return NULL;
6199 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006200 if (scope->se_skip_save == SKIP_YES)
6201 clear_ppconst(&ppconst);
6202 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006203 {
6204 // The expression results in a constant.
6205 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006206 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006207 clear_ppconst(&ppconst);
6208 scope->se_u.se_if.is_if_label = -1;
6209 }
6210 else
6211 {
6212 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006213 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006214 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006215 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006216
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006217 // "where" is set when ":elseif", "else" or ":endif" is found
6218 scope->se_u.se_if.is_if_label = instr->ga_len;
6219 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6220 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006221
6222 return p;
6223}
6224
6225 static char_u *
6226compile_else(char_u *arg, cctx_T *cctx)
6227{
6228 char_u *p = arg;
6229 garray_T *instr = &cctx->ctx_instr;
6230 isn_T *isn;
6231 scope_T *scope = cctx->ctx_scope;
6232
6233 if (scope == NULL || scope->se_type != IF_SCOPE)
6234 {
6235 emsg(_(e_else_without_if));
6236 return NULL;
6237 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006238 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006239 if (!cctx->ctx_had_return)
6240 scope->se_u.se_if.is_had_return = FALSE;
6241 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242
Bram Moolenaarefd88552020-06-18 20:50:10 +02006243 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006244 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006245 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006246 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006247 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006248 if (!cctx->ctx_had_return
6249 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6250 JUMP_ALWAYS, cctx) == FAIL)
6251 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006252 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006253
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006254 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006255 {
6256 if (scope->se_u.se_if.is_if_label >= 0)
6257 {
6258 // previous "if" or "elseif" jumps here
6259 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6260 isn->isn_arg.jump.jump_where = instr->ga_len;
6261 scope->se_u.se_if.is_if_label = -1;
6262 }
6263 }
6264
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006265 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006266 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6267 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006268
6269 return p;
6270}
6271
6272 static char_u *
6273compile_endif(char_u *arg, cctx_T *cctx)
6274{
6275 scope_T *scope = cctx->ctx_scope;
6276 ifscope_T *ifscope;
6277 garray_T *instr = &cctx->ctx_instr;
6278 isn_T *isn;
6279
6280 if (scope == NULL || scope->se_type != IF_SCOPE)
6281 {
6282 emsg(_(e_endif_without_if));
6283 return NULL;
6284 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006285 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006286 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006287 if (!cctx->ctx_had_return)
6288 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006289
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006290 if (scope->se_u.se_if.is_if_label >= 0)
6291 {
6292 // previous "if" or "elseif" jumps here
6293 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6294 isn->isn_arg.jump.jump_where = instr->ga_len;
6295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296 // Fill in the "end" label in jumps at the end of the blocks.
6297 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006298 cctx->ctx_skip = scope->se_skip_save;
6299
6300 // If all the blocks end in :return and there is an :else then the
6301 // had_return flag is set.
6302 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006304 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006305 return arg;
6306}
6307
6308/*
6309 * compile "for var in expr"
6310 *
6311 * Produces instructions:
6312 * PUSHNR -1
6313 * STORE loop-idx Set index to -1
6314 * EVAL expr Push result of "expr"
6315 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6316 * - if beyond end, jump to "end"
6317 * - otherwise get item from list and push it
6318 * STORE var Store item in "var"
6319 * ... body ...
6320 * JUMP top Jump back to repeat
6321 * end: DROP Drop the result of "expr"
6322 *
6323 */
6324 static char_u *
6325compile_for(char_u *arg, cctx_T *cctx)
6326{
6327 char_u *p;
6328 size_t varlen;
6329 garray_T *instr = &cctx->ctx_instr;
6330 garray_T *stack = &cctx->ctx_type_stack;
6331 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006332 lvar_T *loop_lvar; // loop iteration variable
6333 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006334 type_T *vartype;
6335
6336 // TODO: list of variables: "for [key, value] in dict"
6337 // parse "var"
6338 for (p = arg; eval_isnamec1(*p); ++p)
6339 ;
6340 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006341 var_lvar = lookup_local(arg, varlen, cctx);
6342 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343 {
6344 semsg(_("E1023: variable already defined: %s"), arg);
6345 return NULL;
6346 }
6347
6348 // consume "in"
6349 p = skipwhite(p);
6350 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6351 {
6352 emsg(_(e_missing_in));
6353 return NULL;
6354 }
6355 p = skipwhite(p + 2);
6356
6357
6358 scope = new_scope(cctx, FOR_SCOPE);
6359 if (scope == NULL)
6360 return NULL;
6361
6362 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006363 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6364 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006365 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006366 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006367 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006368 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006369 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006370
6371 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006372 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6373 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006374 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006375 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006376 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006377 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006378 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006379
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006380 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006381
6382 // compile "expr", it remains on the stack until "endfor"
6383 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006384 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006385 {
6386 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006387 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006389
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006390 // Now that we know the type of "var", check that it is a list, now or at
6391 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006392 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006393 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006394 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006395 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006396 return NULL;
6397 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006398 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006399 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400
6401 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006402 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006403
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006404 generate_FOR(cctx, loop_lvar->lv_idx);
6405 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406
6407 return arg;
6408}
6409
6410/*
6411 * compile "endfor"
6412 */
6413 static char_u *
6414compile_endfor(char_u *arg, cctx_T *cctx)
6415{
6416 garray_T *instr = &cctx->ctx_instr;
6417 scope_T *scope = cctx->ctx_scope;
6418 forscope_T *forscope;
6419 isn_T *isn;
6420
6421 if (scope == NULL || scope->se_type != FOR_SCOPE)
6422 {
6423 emsg(_(e_for));
6424 return NULL;
6425 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006426 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006427 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006428 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006429
6430 // At end of ":for" scope jump back to the FOR instruction.
6431 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6432
6433 // Fill in the "end" label in the FOR statement so it can jump here
6434 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6435 isn->isn_arg.forloop.for_end = instr->ga_len;
6436
6437 // Fill in the "end" label any BREAK statements
6438 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6439
6440 // Below the ":for" scope drop the "expr" list from the stack.
6441 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6442 return NULL;
6443
6444 vim_free(scope);
6445
6446 return arg;
6447}
6448
6449/*
6450 * compile "while expr"
6451 *
6452 * Produces instructions:
6453 * top: EVAL expr Push result of "expr"
6454 * JUMP_IF_FALSE end jump if false
6455 * ... body ...
6456 * JUMP top Jump back to repeat
6457 * end:
6458 *
6459 */
6460 static char_u *
6461compile_while(char_u *arg, cctx_T *cctx)
6462{
6463 char_u *p = arg;
6464 garray_T *instr = &cctx->ctx_instr;
6465 scope_T *scope;
6466
6467 scope = new_scope(cctx, WHILE_SCOPE);
6468 if (scope == NULL)
6469 return NULL;
6470
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006471 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006472
6473 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006474 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006475 return NULL;
6476
6477 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006478 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006479 JUMP_IF_FALSE, cctx) == FAIL)
6480 return FAIL;
6481
6482 return p;
6483}
6484
6485/*
6486 * compile "endwhile"
6487 */
6488 static char_u *
6489compile_endwhile(char_u *arg, cctx_T *cctx)
6490{
6491 scope_T *scope = cctx->ctx_scope;
6492
6493 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6494 {
6495 emsg(_(e_while));
6496 return NULL;
6497 }
6498 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006499 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006500
6501 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006502 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006503
6504 // Fill in the "end" label in the WHILE statement so it can jump here.
6505 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006506 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006507
6508 vim_free(scope);
6509
6510 return arg;
6511}
6512
6513/*
6514 * compile "continue"
6515 */
6516 static char_u *
6517compile_continue(char_u *arg, cctx_T *cctx)
6518{
6519 scope_T *scope = cctx->ctx_scope;
6520
6521 for (;;)
6522 {
6523 if (scope == NULL)
6524 {
6525 emsg(_(e_continue));
6526 return NULL;
6527 }
6528 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6529 break;
6530 scope = scope->se_outer;
6531 }
6532
6533 // Jump back to the FOR or WHILE instruction.
6534 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006535 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6536 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006537 return arg;
6538}
6539
6540/*
6541 * compile "break"
6542 */
6543 static char_u *
6544compile_break(char_u *arg, cctx_T *cctx)
6545{
6546 scope_T *scope = cctx->ctx_scope;
6547 endlabel_T **el;
6548
6549 for (;;)
6550 {
6551 if (scope == NULL)
6552 {
6553 emsg(_(e_break));
6554 return NULL;
6555 }
6556 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6557 break;
6558 scope = scope->se_outer;
6559 }
6560
6561 // Jump to the end of the FOR or WHILE loop.
6562 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006563 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006564 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006565 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6567 return FAIL;
6568
6569 return arg;
6570}
6571
6572/*
6573 * compile "{" start of block
6574 */
6575 static char_u *
6576compile_block(char_u *arg, cctx_T *cctx)
6577{
6578 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6579 return NULL;
6580 return skipwhite(arg + 1);
6581}
6582
6583/*
6584 * compile end of block: drop one scope
6585 */
6586 static void
6587compile_endblock(cctx_T *cctx)
6588{
6589 scope_T *scope = cctx->ctx_scope;
6590
6591 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006592 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006593 vim_free(scope);
6594}
6595
6596/*
6597 * compile "try"
6598 * Creates a new scope for the try-endtry, pointing to the first catch and
6599 * finally.
6600 * Creates another scope for the "try" block itself.
6601 * TRY instruction sets up exception handling at runtime.
6602 *
6603 * "try"
6604 * TRY -> catch1, -> finally push trystack entry
6605 * ... try block
6606 * "throw {exception}"
6607 * EVAL {exception}
6608 * THROW create exception
6609 * ... try block
6610 * " catch {expr}"
6611 * JUMP -> finally
6612 * catch1: PUSH exeception
6613 * EVAL {expr}
6614 * MATCH
6615 * JUMP nomatch -> catch2
6616 * CATCH remove exception
6617 * ... catch block
6618 * " catch"
6619 * JUMP -> finally
6620 * catch2: CATCH remove exception
6621 * ... catch block
6622 * " finally"
6623 * finally:
6624 * ... finally block
6625 * " endtry"
6626 * ENDTRY pop trystack entry, may rethrow
6627 */
6628 static char_u *
6629compile_try(char_u *arg, cctx_T *cctx)
6630{
6631 garray_T *instr = &cctx->ctx_instr;
6632 scope_T *try_scope;
6633 scope_T *scope;
6634
6635 // scope that holds the jumps that go to catch/finally/endtry
6636 try_scope = new_scope(cctx, TRY_SCOPE);
6637 if (try_scope == NULL)
6638 return NULL;
6639
6640 // "catch" is set when the first ":catch" is found.
6641 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006642 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643 if (generate_instr(cctx, ISN_TRY) == NULL)
6644 return NULL;
6645
6646 // scope for the try block itself
6647 scope = new_scope(cctx, BLOCK_SCOPE);
6648 if (scope == NULL)
6649 return NULL;
6650
6651 return arg;
6652}
6653
6654/*
6655 * compile "catch {expr}"
6656 */
6657 static char_u *
6658compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6659{
6660 scope_T *scope = cctx->ctx_scope;
6661 garray_T *instr = &cctx->ctx_instr;
6662 char_u *p;
6663 isn_T *isn;
6664
6665 // end block scope from :try or :catch
6666 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6667 compile_endblock(cctx);
6668 scope = cctx->ctx_scope;
6669
6670 // Error if not in a :try scope
6671 if (scope == NULL || scope->se_type != TRY_SCOPE)
6672 {
6673 emsg(_(e_catch));
6674 return NULL;
6675 }
6676
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006677 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 {
6679 emsg(_("E1033: catch unreachable after catch-all"));
6680 return NULL;
6681 }
6682
6683 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006684 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685 JUMP_ALWAYS, cctx) == FAIL)
6686 return NULL;
6687
6688 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006689 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006690 if (isn->isn_arg.try.try_catch == 0)
6691 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006692 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006693 {
6694 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006695 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006696 isn->isn_arg.jump.jump_where = instr->ga_len;
6697 }
6698
6699 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006700 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006701 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006702 scope->se_u.se_try.ts_caught_all = TRUE;
6703 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006704 }
6705 else
6706 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006707 char_u *end;
6708 char_u *pat;
6709 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006710 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006711 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713 // Push v:exception, push {expr} and MATCH
6714 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6715
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006716 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006717 if (*end != *p)
6718 {
6719 semsg(_("E1067: Separator mismatch: %s"), p);
6720 vim_free(tofree);
6721 return FAIL;
6722 }
6723 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006724 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006725 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006726 len = (int)(end - tofree);
6727 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006728 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006729 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006730 if (pat == NULL)
6731 return FAIL;
6732 if (generate_PUSHS(cctx, pat) == FAIL)
6733 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006735 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6736 return NULL;
6737
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006738 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006739 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6740 return NULL;
6741 }
6742
6743 if (generate_instr(cctx, ISN_CATCH) == NULL)
6744 return NULL;
6745
6746 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6747 return NULL;
6748 return p;
6749}
6750
6751 static char_u *
6752compile_finally(char_u *arg, cctx_T *cctx)
6753{
6754 scope_T *scope = cctx->ctx_scope;
6755 garray_T *instr = &cctx->ctx_instr;
6756 isn_T *isn;
6757
6758 // end block scope from :try or :catch
6759 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6760 compile_endblock(cctx);
6761 scope = cctx->ctx_scope;
6762
6763 // Error if not in a :try scope
6764 if (scope == NULL || scope->se_type != TRY_SCOPE)
6765 {
6766 emsg(_(e_finally));
6767 return NULL;
6768 }
6769
6770 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006771 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006772 if (isn->isn_arg.try.try_finally != 0)
6773 {
6774 emsg(_(e_finally_dup));
6775 return NULL;
6776 }
6777
6778 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006779 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006780
Bram Moolenaar585fea72020-04-02 22:33:21 +02006781 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006782 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006783 {
6784 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006785 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006787 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006788 }
6789
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006790 // TODO: set index in ts_finally_label jumps
6791
6792 return arg;
6793}
6794
6795 static char_u *
6796compile_endtry(char_u *arg, cctx_T *cctx)
6797{
6798 scope_T *scope = cctx->ctx_scope;
6799 garray_T *instr = &cctx->ctx_instr;
6800 isn_T *isn;
6801
6802 // end block scope from :catch or :finally
6803 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6804 compile_endblock(cctx);
6805 scope = cctx->ctx_scope;
6806
6807 // Error if not in a :try scope
6808 if (scope == NULL || scope->se_type != TRY_SCOPE)
6809 {
6810 if (scope == NULL)
6811 emsg(_(e_no_endtry));
6812 else if (scope->se_type == WHILE_SCOPE)
6813 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006814 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006815 emsg(_(e_endfor));
6816 else
6817 emsg(_(e_endif));
6818 return NULL;
6819 }
6820
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006821 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006822 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6823 {
6824 emsg(_("E1032: missing :catch or :finally"));
6825 return NULL;
6826 }
6827
6828 // Fill in the "end" label in jumps at the end of the blocks, if not done
6829 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006830 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006831
6832 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006833 if (isn->isn_arg.try.try_catch == 0)
6834 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006835 if (isn->isn_arg.try.try_finally == 0)
6836 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006837
6838 if (scope->se_u.se_try.ts_catch_label != 0)
6839 {
6840 // Last catch without match jumps here
6841 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6842 isn->isn_arg.jump.jump_where = instr->ga_len;
6843 }
6844
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006845 compile_endblock(cctx);
6846
6847 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6848 return NULL;
6849 return arg;
6850}
6851
6852/*
6853 * compile "throw {expr}"
6854 */
6855 static char_u *
6856compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6857{
6858 char_u *p = skipwhite(arg);
6859
Bram Moolenaara5565e42020-05-09 15:44:01 +02006860 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006861 return NULL;
6862 if (may_generate_2STRING(-1, cctx) == FAIL)
6863 return NULL;
6864 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6865 return NULL;
6866
6867 return p;
6868}
6869
6870/*
6871 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006872 * compile "echomsg expr"
6873 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006874 * compile "execute expr"
6875 */
6876 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006877compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006878{
6879 char_u *p = arg;
6880 int count = 0;
6881
6882 for (;;)
6883 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006884 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006885 return NULL;
6886 ++count;
6887 p = skipwhite(p);
6888 if (ends_excmd(*p))
6889 break;
6890 }
6891
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006892 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6893 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6894 else if (cmdidx == CMD_execute)
6895 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6896 else if (cmdidx == CMD_echomsg)
6897 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6898 else
6899 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006900 return p;
6901}
6902
6903/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006904 * A command that is not compiled, execute with legacy code.
6905 */
6906 static char_u *
6907compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6908{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006909 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006910 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006911 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006912
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006913 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006914 goto theend;
6915
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006916 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006917 {
6918 long argt = excmd_get_argt(eap->cmdidx);
6919 int usefilter = FALSE;
6920
6921 has_expr = argt & (EX_XFILE | EX_EXPAND);
6922
6923 // If the command can be followed by a bar, find the bar and truncate
6924 // it, so that the following command can be compiled.
6925 // The '|' is overwritten with a NUL, it is put back below.
6926 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6927 && *eap->arg == '!')
6928 // :w !filter or :r !filter or :r! filter
6929 usefilter = TRUE;
6930 if ((argt & EX_TRLBAR) && !usefilter)
6931 {
6932 separate_nextcmd(eap);
6933 if (eap->nextcmd != NULL)
6934 nextcmd = eap->nextcmd;
6935 }
6936 }
6937
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006938 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6939 {
6940 // expand filename in "syntax include [@group] filename"
6941 has_expr = TRUE;
6942 eap->arg = skipwhite(eap->arg + 7);
6943 if (*eap->arg == '@')
6944 eap->arg = skiptowhite(eap->arg);
6945 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006946
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006947 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006948 {
6949 int count = 0;
6950 char_u *start = skipwhite(line);
6951
6952 // :cmd xxx`=expr1`yyy`=expr2`zzz
6953 // PUSHS ":cmd xxx"
6954 // eval expr1
6955 // PUSHS "yyy"
6956 // eval expr2
6957 // PUSHS "zzz"
6958 // EXECCONCAT 5
6959 for (;;)
6960 {
6961 if (p > start)
6962 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006963 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006964 ++count;
6965 }
6966 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006967 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006968 return NULL;
6969 may_generate_2STRING(-1, cctx);
6970 ++count;
6971 p = skipwhite(p);
6972 if (*p != '`')
6973 {
6974 emsg(_("E1083: missing backtick"));
6975 return NULL;
6976 }
6977 start = p + 1;
6978
6979 p = (char_u *)strstr((char *)start, "`=");
6980 if (p == NULL)
6981 {
6982 if (*skipwhite(start) != NUL)
6983 {
6984 generate_PUSHS(cctx, vim_strsave(start));
6985 ++count;
6986 }
6987 break;
6988 }
6989 }
6990 generate_EXECCONCAT(cctx, count);
6991 }
6992 else
6993 generate_EXEC(cctx, line);
6994
6995theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006996 if (*nextcmd != NUL)
6997 {
6998 // the parser expects a pointer to the bar, put it back
6999 --nextcmd;
7000 *nextcmd = '|';
7001 }
7002
7003 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007004}
7005
7006/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007007 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007008 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007009 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007010 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007011add_def_function(ufunc_T *ufunc)
7012{
7013 dfunc_T *dfunc;
7014
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007015 if (def_functions.ga_len == 0)
7016 {
7017 // The first position is not used, so that a zero uf_dfunc_idx means it
7018 // wasn't set.
7019 if (ga_grow(&def_functions, 1) == FAIL)
7020 return FAIL;
7021 ++def_functions.ga_len;
7022 }
7023
Bram Moolenaar09689a02020-05-09 22:50:08 +02007024 // Add the function to "def_functions".
7025 if (ga_grow(&def_functions, 1) == FAIL)
7026 return FAIL;
7027 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7028 CLEAR_POINTER(dfunc);
7029 dfunc->df_idx = def_functions.ga_len;
7030 ufunc->uf_dfunc_idx = dfunc->df_idx;
7031 dfunc->df_ufunc = ufunc;
7032 ++def_functions.ga_len;
7033 return OK;
7034}
7035
7036/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007037 * After ex_function() has collected all the function lines: parse and compile
7038 * the lines into instructions.
7039 * Adds the function to "def_functions".
7040 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7041 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007042 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007043 * This can be used recursively through compile_lambda(), which may reallocate
7044 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007045 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007046 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007047 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007048compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007049{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007050 char_u *line = NULL;
7051 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007052 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007053 cctx_T cctx;
7054 garray_T *instr;
7055 int called_emsg_before = called_emsg;
7056 int ret = FAIL;
7057 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007058 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007059 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007060 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007061
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007062 // When using a function that was compiled before: Free old instructions.
7063 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007064 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007065 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007066 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7067 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007068 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007069 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007070 else
7071 {
7072 if (add_def_function(ufunc) == FAIL)
7073 return FAIL;
7074 new_def_function = TRUE;
7075 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007076
Bram Moolenaar985116a2020-07-12 17:31:09 +02007077 ufunc->uf_def_status = UF_COMPILING;
7078
Bram Moolenaara80faa82020-04-12 19:37:17 +02007079 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080 cctx.ctx_ufunc = ufunc;
7081 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007082 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007083 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7084 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7085 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7086 cctx.ctx_type_list = &ufunc->uf_type_list;
7087 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7088 instr = &cctx.ctx_instr;
7089
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007090 // Set the context to the function, it may be compiled when called from
7091 // another script. Set the script version to the most modern one.
7092 // The line number will be set in next_line_from_context().
7093 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007094 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7095
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007096 // Make sure error messages are OK.
7097 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7098 if (do_estack_push)
7099 estack_push_ufunc(ufunc, 1);
7100
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007101 if (ufunc->uf_def_args.ga_len > 0)
7102 {
7103 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007104 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007105 int i;
7106 char_u *arg;
7107 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007108 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007109
7110 // Produce instructions for the default values of optional arguments.
7111 // Store the instruction index in uf_def_arg_idx[] so that we know
7112 // where to start when the function is called, depending on the number
7113 // of arguments.
7114 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7115 if (ufunc->uf_def_arg_idx == NULL)
7116 goto erret;
7117 for (i = 0; i < count; ++i)
7118 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007119 garray_T *stack = &cctx.ctx_type_stack;
7120 type_T *val_type;
7121 int arg_idx = first_def_arg + i;
7122
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007123 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7124 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007125 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007126 goto erret;
7127
7128 // If no type specified use the type of the default value.
7129 // Otherwise check that the default value type matches the
7130 // specified type.
7131 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7132 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007133 {
7134 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007135 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007136 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02007137 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007138 == FAIL)
7139 {
7140 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
7141 arg_idx + 1);
7142 goto erret;
7143 }
7144
7145 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007146 goto erret;
7147 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007148 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007149
7150 if (did_set_arg_type)
7151 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007152 }
7153
7154 /*
7155 * Loop over all the lines of the function and generate instructions.
7156 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007157 for (;;)
7158 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007159 exarg_T ea;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007160 int starts_with_colon = FALSE;
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007161 char_u *cmd;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007162 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007163
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007164 // Bail out on the first error to avoid a flood of errors and report
7165 // the right line number when inside try/catch.
7166 if (emsg_before != called_emsg)
7167 goto erret;
7168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007169 if (line != NULL && *line == '|')
7170 // the line continues after a '|'
7171 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007172 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007173 && !(*line == '#' && (line == cctx.ctx_line_start
7174 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007176 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177 goto erret;
7178 }
7179 else
7180 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007181 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007182 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007183 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007185 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007186 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007187
Bram Moolenaara80faa82020-04-12 19:37:17 +02007188 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007189 ea.cmdlinep = &line;
7190 ea.cmd = skipwhite(line);
7191
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007192 // Some things can be recognized by the first character.
7193 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007194 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007195 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007196 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007197 if (ea.cmd[1] != '{')
7198 {
7199 line = (char_u *)"";
7200 continue;
7201 }
7202 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007203
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007204 case '}':
7205 {
7206 // "}" ends a block scope
7207 scopetype_T stype = cctx.ctx_scope == NULL
7208 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007209
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007210 if (stype == BLOCK_SCOPE)
7211 {
7212 compile_endblock(&cctx);
7213 line = ea.cmd;
7214 }
7215 else
7216 {
7217 emsg(_("E1025: using } outside of a block scope"));
7218 goto erret;
7219 }
7220 if (line != NULL)
7221 line = skipwhite(ea.cmd + 1);
7222 continue;
7223 }
7224
7225 case '{':
7226 // "{" starts a block scope
7227 // "{'a': 1}->func() is something else
7228 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7229 {
7230 line = compile_block(ea.cmd, &cctx);
7231 continue;
7232 }
7233 break;
7234
7235 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007236 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007237 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007238 }
7239
7240 /*
7241 * COMMAND MODIFIERS
7242 */
7243 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7244 {
7245 if (errormsg != NULL)
7246 goto erret;
7247 // empty line or comment
7248 line = (char_u *)"";
7249 continue;
7250 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007251 // TODO: use modifiers in the command
7252 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02007253 CLEAR_FIELD(cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007254
7255 // Skip ":call" to get to the function name.
7256 if (checkforcmd(&ea.cmd, "call", 3))
7257 ea.cmd = skipwhite(ea.cmd);
7258
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007259 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007260 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007261 char_u *pskip;
7262
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007263 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007264 // find what follows.
7265 // Skip over "var.member", "var[idx]" and the like.
7266 // Also "&opt = val", "$ENV = val" and "@r = val".
7267 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007268 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007269 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007270 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007271 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007272 char_u *var_end;
7273 int oplen;
7274 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007275
Bram Moolenaar65821722020-08-02 18:58:54 +02007276 if (ea.cmd[0] == '@')
7277 var_end = ea.cmd + 2;
7278 else
7279 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007280 FNE_CHECK_START | FNE_INCL_BR);
7281 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007282 if (oplen > 0)
7283 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007284 size_t len = p - ea.cmd;
7285
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007286 // Recognize an assignment if we recognize the variable
7287 // name:
7288 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007289 // "local = expr" where "local" is a local var.
7290 // "script = expr" where "script" is a script-local var.
7291 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007292 // "&opt = expr"
7293 // "$ENV = expr"
7294 // "@r = expr"
7295 if (*ea.cmd == '&'
7296 || *ea.cmd == '$'
7297 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007298 || ((len) > 2 && ea.cmd[1] == ':')
7299 || lookup_local(ea.cmd, len, &cctx) != NULL
7300 || lookup_arg(ea.cmd, len, NULL, NULL,
7301 NULL, &cctx) == OK
7302 || lookup_script(ea.cmd, len) == OK
7303 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007304 {
7305 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007306 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007307 goto erret;
7308 continue;
7309 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310 }
7311 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007312
7313 if (*ea.cmd == '[')
7314 {
7315 // [var, var] = expr
7316 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7317 if (line == NULL)
7318 goto erret;
7319 if (line != ea.cmd)
7320 continue;
7321 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322 }
7323
7324 /*
7325 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007326 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007327 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007328 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007329 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007330 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007331 ea.cmd = skip_range(ea.cmd, NULL);
7332 if (ea.cmd > cmd && !starts_with_colon)
7333 {
7334 emsg(_(e_colon_required));
7335 goto erret;
7336 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007337 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007338 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007339 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007340 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007341
7342 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7343 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007344 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007345 {
7346 line += STRLEN(line);
7347 continue;
7348 }
7349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007350 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007351 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007352 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007353 // CMD_let cannot happen, compile_assignment() above is used
7354 iemsg("Command from find_ex_command() not handled");
7355 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357 }
7358
7359 p = skipwhite(p);
7360
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007361 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007362 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007363 && ea.cmdidx != CMD_elseif
7364 && ea.cmdidx != CMD_else
7365 && ea.cmdidx != CMD_endif)
7366 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007367 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007368 continue;
7369 }
7370
Bram Moolenaarefd88552020-06-18 20:50:10 +02007371 if (ea.cmdidx != CMD_elseif
7372 && ea.cmdidx != CMD_else
7373 && ea.cmdidx != CMD_endif
7374 && ea.cmdidx != CMD_endfor
7375 && ea.cmdidx != CMD_endwhile
7376 && ea.cmdidx != CMD_catch
7377 && ea.cmdidx != CMD_finally
7378 && ea.cmdidx != CMD_endtry)
7379 {
7380 if (cctx.ctx_had_return)
7381 {
7382 emsg(_("E1095: Unreachable code after :return"));
7383 goto erret;
7384 }
7385 }
7386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387 switch (ea.cmdidx)
7388 {
7389 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007390 ea.arg = p;
7391 line = compile_nested_function(&ea, &cctx);
7392 break;
7393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007394 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007395 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007396 goto erret;
7397
7398 case CMD_return:
7399 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007400 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007401 break;
7402
7403 case CMD_let:
7404 case CMD_const:
7405 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007406 if (line == p)
7407 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007408 break;
7409
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007410 case CMD_unlet:
7411 case CMD_unlockvar:
7412 case CMD_lockvar:
7413 line = compile_unletlock(p, &ea, &cctx);
7414 break;
7415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007416 case CMD_import:
7417 line = compile_import(p, &cctx);
7418 break;
7419
7420 case CMD_if:
7421 line = compile_if(p, &cctx);
7422 break;
7423 case CMD_elseif:
7424 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007425 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426 break;
7427 case CMD_else:
7428 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007429 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007430 break;
7431 case CMD_endif:
7432 line = compile_endif(p, &cctx);
7433 break;
7434
7435 case CMD_while:
7436 line = compile_while(p, &cctx);
7437 break;
7438 case CMD_endwhile:
7439 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007440 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007441 break;
7442
7443 case CMD_for:
7444 line = compile_for(p, &cctx);
7445 break;
7446 case CMD_endfor:
7447 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007448 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449 break;
7450 case CMD_continue:
7451 line = compile_continue(p, &cctx);
7452 break;
7453 case CMD_break:
7454 line = compile_break(p, &cctx);
7455 break;
7456
7457 case CMD_try:
7458 line = compile_try(p, &cctx);
7459 break;
7460 case CMD_catch:
7461 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007462 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007463 break;
7464 case CMD_finally:
7465 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007466 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007467 break;
7468 case CMD_endtry:
7469 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007470 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007471 break;
7472 case CMD_throw:
7473 line = compile_throw(p, &cctx);
7474 break;
7475
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007476 case CMD_eval:
7477 if (compile_expr0(&p, &cctx) == FAIL)
7478 goto erret;
7479
7480 // drop the return value
7481 generate_instr_drop(&cctx, ISN_DROP, 1);
7482
7483 line = skipwhite(p);
7484 break;
7485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007488 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007489 case CMD_echomsg:
7490 case CMD_echoerr:
7491 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007492 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007493
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007494 // TODO: other commands with an expression argument
7495
Bram Moolenaarae616492020-07-28 20:07:27 +02007496 case CMD_append:
7497 case CMD_change:
7498 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007499 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007500 case CMD_xit:
7501 not_in_vim9(&ea);
7502 goto erret;
7503
Bram Moolenaar002262f2020-07-08 17:47:57 +02007504 case CMD_SIZE:
7505 semsg(_("E476: Invalid command: %s"), ea.cmd);
7506 goto erret;
7507
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007508 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007509 // Not recognized, execute with do_cmdline_cmd().
7510 ea.arg = p;
7511 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007512 break;
7513 }
7514 if (line == NULL)
7515 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007516 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007517
7518 if (cctx.ctx_type_stack.ga_len < 0)
7519 {
7520 iemsg("Type stack underflow");
7521 goto erret;
7522 }
7523 }
7524
7525 if (cctx.ctx_scope != NULL)
7526 {
7527 if (cctx.ctx_scope->se_type == IF_SCOPE)
7528 emsg(_(e_endif));
7529 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7530 emsg(_(e_endwhile));
7531 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7532 emsg(_(e_endfor));
7533 else
7534 emsg(_("E1026: Missing }"));
7535 goto erret;
7536 }
7537
Bram Moolenaarefd88552020-06-18 20:50:10 +02007538 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007539 {
7540 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7541 {
7542 emsg(_("E1027: Missing return statement"));
7543 goto erret;
7544 }
7545
7546 // Return zero if there is no return at the end.
7547 generate_PUSHNR(&cctx, 0);
7548 generate_instr(&cctx, ISN_RETURN);
7549 }
7550
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007551 {
7552 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7553 + ufunc->uf_dfunc_idx;
7554 dfunc->df_deleted = FALSE;
7555 dfunc->df_instr = instr->ga_data;
7556 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007557 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007558 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007559 if (cctx.ctx_outer_used)
7560 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007561 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007562 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007563
7564 ret = OK;
7565
7566erret:
7567 if (ret == FAIL)
7568 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007569 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007570 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7571 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007572
7573 for (idx = 0; idx < instr->ga_len; ++idx)
7574 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007575 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007576
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007577 // If using the last entry in the table and it was added above, we
7578 // might as well remove it.
7579 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007580 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007581 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007582 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007583 ufunc->uf_dfunc_idx = 0;
7584 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007585 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007586
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007587 while (cctx.ctx_scope != NULL)
7588 drop_scope(&cctx);
7589
Bram Moolenaar20431c92020-03-20 18:39:46 +01007590 // Don't execute this function body.
7591 ga_clear_strings(&ufunc->uf_lines);
7592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007593 if (errormsg != NULL)
7594 emsg(errormsg);
7595 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007596 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007597 }
7598
7599 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007600 if (do_estack_push)
7601 estack_pop();
7602
Bram Moolenaar20431c92020-03-20 18:39:46 +01007603 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007604 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007605 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007606 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007607}
7608
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007609 void
7610set_function_type(ufunc_T *ufunc)
7611{
7612 int varargs = ufunc->uf_va_name != NULL;
7613 int argcount = ufunc->uf_args.ga_len;
7614
7615 // Create a type for the function, with the return type and any
7616 // argument types.
7617 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7618 // The type is included in "tt_args".
7619 if (argcount > 0 || varargs)
7620 {
7621 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7622 argcount, &ufunc->uf_type_list);
7623 // Add argument types to the function type.
7624 if (func_type_add_arg_types(ufunc->uf_func_type,
7625 argcount + varargs,
7626 &ufunc->uf_type_list) == FAIL)
7627 return;
7628 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7629 ufunc->uf_func_type->tt_min_argcount =
7630 argcount - ufunc->uf_def_args.ga_len;
7631 if (ufunc->uf_arg_types == NULL)
7632 {
7633 int i;
7634
7635 // lambda does not have argument types.
7636 for (i = 0; i < argcount; ++i)
7637 ufunc->uf_func_type->tt_args[i] = &t_any;
7638 }
7639 else
7640 mch_memmove(ufunc->uf_func_type->tt_args,
7641 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7642 if (varargs)
7643 {
7644 ufunc->uf_func_type->tt_args[argcount] =
7645 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7646 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7647 }
7648 }
7649 else
7650 // No arguments, can use a predefined type.
7651 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7652 argcount, &ufunc->uf_type_list);
7653}
7654
7655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007656/*
7657 * Delete an instruction, free what it contains.
7658 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007659 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007660delete_instr(isn_T *isn)
7661{
7662 switch (isn->isn_type)
7663 {
7664 case ISN_EXEC:
7665 case ISN_LOADENV:
7666 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007667 case ISN_LOADB:
7668 case ISN_LOADW:
7669 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007670 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007671 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007672 case ISN_PUSHEXC:
7673 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007674 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007675 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007676 case ISN_STOREB:
7677 case ISN_STOREW:
7678 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007679 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007680 vim_free(isn->isn_arg.string);
7681 break;
7682
7683 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007684 case ISN_STORES:
7685 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007686 break;
7687
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007688 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007689 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007690 vim_free(isn->isn_arg.unlet.ul_name);
7691 break;
7692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007693 case ISN_STOREOPT:
7694 vim_free(isn->isn_arg.storeopt.so_name);
7695 break;
7696
7697 case ISN_PUSHBLOB: // push blob isn_arg.blob
7698 blob_unref(isn->isn_arg.blob);
7699 break;
7700
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007701 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007702#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007703 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007704#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007705 break;
7706
7707 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007708#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007709 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007710#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007711 break;
7712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713 case ISN_UCALL:
7714 vim_free(isn->isn_arg.ufunc.cuf_name);
7715 break;
7716
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007717 case ISN_FUNCREF:
7718 {
7719 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7720 + isn->isn_arg.funcref.fr_func;
7721 func_ptr_unref(dfunc->df_ufunc);
7722 }
7723 break;
7724
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007725 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007726 {
7727 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7728 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7729
7730 if (ufunc != NULL)
7731 {
7732 // Clear uf_dfunc_idx so that the function is deleted.
7733 clear_def_function(ufunc);
7734 ufunc->uf_dfunc_idx = 0;
7735 func_ptr_unref(ufunc);
7736 }
7737
7738 vim_free(lambda);
7739 vim_free(isn->isn_arg.newfunc.nf_global);
7740 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007741 break;
7742
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007743 case ISN_2BOOL:
7744 case ISN_2STRING:
7745 case ISN_ADDBLOB:
7746 case ISN_ADDLIST:
7747 case ISN_BCALL:
7748 case ISN_CATCH:
7749 case ISN_CHECKNR:
7750 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007751 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007752 case ISN_COMPAREANY:
7753 case ISN_COMPAREBLOB:
7754 case ISN_COMPAREBOOL:
7755 case ISN_COMPAREDICT:
7756 case ISN_COMPAREFLOAT:
7757 case ISN_COMPAREFUNC:
7758 case ISN_COMPARELIST:
7759 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007760 case ISN_COMPARESPECIAL:
7761 case ISN_COMPARESTRING:
7762 case ISN_CONCAT:
7763 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007764 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007765 case ISN_DROP:
7766 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007767 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007768 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007769 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007770 case ISN_EXECCONCAT:
7771 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007772 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007773 case ISN_LISTINDEX:
7774 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007775 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007776 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007777 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007778 case ISN_JUMP:
7779 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007780 case ISN_LOADBDICT:
7781 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007782 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007783 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007784 case ISN_LOADSCRIPT:
7785 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007786 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007787 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007788 case ISN_NEGATENR:
7789 case ISN_NEWDICT:
7790 case ISN_NEWLIST:
7791 case ISN_OPNR:
7792 case ISN_OPFLOAT:
7793 case ISN_OPANY:
7794 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007795 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007796 case ISN_PUSHF:
7797 case ISN_PUSHNR:
7798 case ISN_PUSHBOOL:
7799 case ISN_PUSHSPEC:
7800 case ISN_RETURN:
7801 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007802 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007803 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007804 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007805 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007806 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007807 case ISN_STOREDICT:
7808 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007809 case ISN_THROW:
7810 case ISN_TRY:
7811 // nothing allocated
7812 break;
7813 }
7814}
7815
7816/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007817 * Free all instructions for "dfunc".
7818 */
7819 static void
7820delete_def_function_contents(dfunc_T *dfunc)
7821{
7822 int idx;
7823
7824 ga_clear(&dfunc->df_def_args_isn);
7825
7826 if (dfunc->df_instr != NULL)
7827 {
7828 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7829 delete_instr(dfunc->df_instr + idx);
7830 VIM_CLEAR(dfunc->df_instr);
7831 }
7832
7833 dfunc->df_deleted = TRUE;
7834}
7835
7836/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007837 * When a user function is deleted, clear the contents of any associated def
7838 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007839 */
7840 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007841clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007842{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007843 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007844 {
7845 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7846 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007847
Bram Moolenaar20431c92020-03-20 18:39:46 +01007848 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007849 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007850 }
7851}
7852
7853#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007854/*
7855 * Free all functions defined with ":def".
7856 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007857 void
7858free_def_functions(void)
7859{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007860 int idx;
7861
7862 for (idx = 0; idx < def_functions.ga_len; ++idx)
7863 {
7864 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7865
7866 delete_def_function_contents(dfunc);
7867 }
7868
7869 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007870}
7871#endif
7872
7873
7874#endif // FEAT_EVAL