blob: 5f76bc2c03c21c214ee38eb9d3ac040b5f722a1d [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)
Bram Moolenaar5a849da2020-08-08 16:47:30 +0200665 // Allow for using "any" argument type, lambda's have them.
666 if (actual->tt_args[i] != &t_any && check_type(
667 expected->tt_args[i], actual->tt_args[i], FALSE)
668 == FAIL)
Bram Moolenaarb8070e32020-07-23 20:56:04 +0200669 {
670 ret = FAIL;
671 break;
672 }
673 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200674 }
675 if (ret == FAIL && give_msg)
676 type_mismatch(expected, actual);
677 }
678 return ret;
679}
680
Bram Moolenaar65b95452020-07-19 14:03:09 +0200681
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682/////////////////////////////////////////////////////////////////////
683// Following generate_ functions expect the caller to call ga_grow().
684
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200685#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
686#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688/*
689 * Generate an instruction without arguments.
690 * Returns a pointer to the new instruction, NULL if failed.
691 */
692 static isn_T *
693generate_instr(cctx_T *cctx, isntype_T isn_type)
694{
695 garray_T *instr = &cctx->ctx_instr;
696 isn_T *isn;
697
Bram Moolenaar080457c2020-03-03 21:53:32 +0100698 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100699 if (ga_grow(instr, 1) == FAIL)
700 return NULL;
701 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
702 isn->isn_type = isn_type;
703 isn->isn_lnum = cctx->ctx_lnum + 1;
704 ++instr->ga_len;
705
706 return isn;
707}
708
709/*
710 * Generate an instruction without arguments.
711 * "drop" will be removed from the stack.
712 * Returns a pointer to the new instruction, NULL if failed.
713 */
714 static isn_T *
715generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
716{
717 garray_T *stack = &cctx->ctx_type_stack;
718
Bram Moolenaar080457c2020-03-03 21:53:32 +0100719 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 stack->ga_len -= drop;
721 return generate_instr(cctx, isn_type);
722}
723
724/*
725 * Generate instruction "isn_type" and put "type" on the type stack.
726 */
727 static isn_T *
728generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
729{
730 isn_T *isn;
731 garray_T *stack = &cctx->ctx_type_stack;
732
733 if ((isn = generate_instr(cctx, isn_type)) == NULL)
734 return NULL;
735
736 if (ga_grow(stack, 1) == FAIL)
737 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200738 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100739 ++stack->ga_len;
740
741 return isn;
742}
743
744/*
745 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
746 */
747 static int
748may_generate_2STRING(int offset, cctx_T *cctx)
749{
750 isn_T *isn;
751 garray_T *stack = &cctx->ctx_type_stack;
752 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
753
754 if ((*type)->tt_type == VAR_STRING)
755 return OK;
756 *type = &t_string;
757
758 if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
759 return FAIL;
760 isn->isn_arg.number = offset;
761
762 return OK;
763}
764
765 static int
766check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
767{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200768 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200770 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100771 {
772 if (*op == '+')
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200773 emsg(_("E1051: wrong argument type for +"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 else
775 semsg(_("E1036: %c requires number or float arguments"), *op);
776 return FAIL;
777 }
778 return OK;
779}
780
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200781 static int
782generate_add_instr(
783 cctx_T *cctx,
784 vartype_T vartype,
785 type_T *type1,
786 type_T *type2)
787{
788 isn_T *isn = generate_instr_drop(cctx,
789 vartype == VAR_NUMBER ? ISN_OPNR
790 : vartype == VAR_LIST ? ISN_ADDLIST
791 : vartype == VAR_BLOB ? ISN_ADDBLOB
792#ifdef FEAT_FLOAT
793 : vartype == VAR_FLOAT ? ISN_OPFLOAT
794#endif
795 : ISN_OPANY, 1);
796
797 if (vartype != VAR_LIST && vartype != VAR_BLOB
798 && type1->tt_type != VAR_ANY
799 && type2->tt_type != VAR_ANY
800 && check_number_or_float(
801 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
802 return FAIL;
803
804 if (isn != NULL)
805 isn->isn_arg.op.op_type = EXPR_ADD;
806 return isn == NULL ? FAIL : OK;
807}
808
809/*
810 * Get the type to use for an instruction for an operation on "type1" and
811 * "type2". If they are matching use a type-specific instruction. Otherwise
812 * fall back to runtime type checking.
813 */
814 static vartype_T
815operator_type(type_T *type1, type_T *type2)
816{
817 if (type1->tt_type == type2->tt_type
818 && (type1->tt_type == VAR_NUMBER
819 || type1->tt_type == VAR_LIST
820#ifdef FEAT_FLOAT
821 || type1->tt_type == VAR_FLOAT
822#endif
823 || type1->tt_type == VAR_BLOB))
824 return type1->tt_type;
825 return VAR_ANY;
826}
827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100828/*
829 * Generate an instruction with two arguments. The instruction depends on the
830 * type of the arguments.
831 */
832 static int
833generate_two_op(cctx_T *cctx, char_u *op)
834{
835 garray_T *stack = &cctx->ctx_type_stack;
836 type_T *type1;
837 type_T *type2;
838 vartype_T vartype;
839 isn_T *isn;
840
Bram Moolenaar080457c2020-03-03 21:53:32 +0100841 RETURN_OK_IF_SKIP(cctx);
842
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200843 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100844 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
845 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200846 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847
848 switch (*op)
849 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200850 case '+':
851 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100852 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 break;
854
855 case '-':
856 case '*':
857 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
858 op) == FAIL)
859 return FAIL;
860 if (vartype == VAR_NUMBER)
861 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
862#ifdef FEAT_FLOAT
863 else if (vartype == VAR_FLOAT)
864 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
865#endif
866 else
867 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
868 if (isn != NULL)
869 isn->isn_arg.op.op_type = *op == '*'
870 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
871 break;
872
Bram Moolenaar4c683752020-04-05 21:38:23 +0200873 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200875 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876 && type2->tt_type != VAR_NUMBER))
877 {
878 emsg(_("E1035: % requires number arguments"));
879 return FAIL;
880 }
881 isn = generate_instr_drop(cctx,
882 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
883 if (isn != NULL)
884 isn->isn_arg.op.op_type = EXPR_REM;
885 break;
886 }
887
888 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200889 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890 {
891 type_T *type = &t_any;
892
893#ifdef FEAT_FLOAT
894 // float+number and number+float results in float
895 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
896 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
897 type = &t_float;
898#endif
899 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
900 }
901
902 return OK;
903}
904
905/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200906 * Get the instruction to use for comparing "type1" with "type2"
907 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200909 static isntype_T
910get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100911{
912 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100913
Bram Moolenaar4c683752020-04-05 21:38:23 +0200914 if (type1 == VAR_UNKNOWN)
915 type1 = VAR_ANY;
916 if (type2 == VAR_UNKNOWN)
917 type2 = VAR_ANY;
918
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100919 if (type1 == type2)
920 {
921 switch (type1)
922 {
923 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
924 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
925 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
926 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
927 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
928 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
929 case VAR_LIST: isntype = ISN_COMPARELIST; break;
930 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
931 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932 default: isntype = ISN_COMPAREANY; break;
933 }
934 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200935 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
937 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
938 isntype = ISN_COMPAREANY;
939
940 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
941 && (isntype == ISN_COMPAREBOOL
942 || isntype == ISN_COMPARESPECIAL
943 || isntype == ISN_COMPARENR
944 || isntype == ISN_COMPAREFLOAT))
945 {
946 semsg(_("E1037: Cannot use \"%s\" with %s"),
947 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200948 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949 }
950 if (isntype == ISN_DROP
951 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
952 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
953 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
954 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
955 && exptype != EXPR_IS && exptype != EXPR_ISNOT
956 && (type1 == VAR_BLOB || type2 == VAR_BLOB
957 || type1 == VAR_LIST || type2 == VAR_LIST))))
958 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +0100959 semsg(_("E1072: Cannot compare %s with %s"),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100960 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200961 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100962 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200963 return isntype;
964}
965
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200966 int
967check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
968{
969 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
970 return FAIL;
971 return OK;
972}
973
Bram Moolenaara5565e42020-05-09 15:44:01 +0200974/*
975 * Generate an ISN_COMPARE* instruction with a boolean result.
976 */
977 static int
978generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
979{
980 isntype_T isntype;
981 isn_T *isn;
982 garray_T *stack = &cctx->ctx_type_stack;
983 vartype_T type1;
984 vartype_T type2;
985
986 RETURN_OK_IF_SKIP(cctx);
987
988 // Get the known type of the two items on the stack. If they are matching
989 // use a type-specific instruction. Otherwise fall back to runtime type
990 // checking.
991 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
992 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
993 isntype = get_compare_isn(exptype, type1, type2);
994 if (isntype == ISN_DROP)
995 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996
997 if ((isn = generate_instr(cctx, isntype)) == NULL)
998 return FAIL;
999 isn->isn_arg.op.op_type = exptype;
1000 isn->isn_arg.op.op_ic = ic;
1001
1002 // takes two arguments, puts one bool back
1003 if (stack->ga_len >= 2)
1004 {
1005 --stack->ga_len;
1006 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
1007 }
1008
1009 return OK;
1010}
1011
1012/*
1013 * Generate an ISN_2BOOL instruction.
1014 */
1015 static int
1016generate_2BOOL(cctx_T *cctx, int invert)
1017{
1018 isn_T *isn;
1019 garray_T *stack = &cctx->ctx_type_stack;
1020
Bram Moolenaar080457c2020-03-03 21:53:32 +01001021 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
1023 return FAIL;
1024 isn->isn_arg.number = invert;
1025
1026 // type becomes bool
1027 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
1028
1029 return OK;
1030}
1031
1032 static int
1033generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
1034{
1035 isn_T *isn;
1036 garray_T *stack = &cctx->ctx_type_stack;
1037
Bram Moolenaar080457c2020-03-03 21:53:32 +01001038 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
1040 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +02001041 // TODO: whole type, e.g. for a function also arg and return types
1042 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043 isn->isn_arg.type.ct_off = offset;
1044
1045 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02001046 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047
1048 return OK;
1049}
1050
1051/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001052 * Check that
1053 * - "actual" is "expected" type or
1054 * - "actual" is a type that can be "expected" type: add a runtime check; or
1055 * - return FAIL.
1056 */
1057 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001058need_type(
1059 type_T *actual,
1060 type_T *expected,
1061 int offset,
1062 cctx_T *cctx,
1063 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001064{
1065 if (check_type(expected, actual, FALSE) == OK)
1066 return OK;
1067 if (actual->tt_type != VAR_ANY
1068 && actual->tt_type != VAR_UNKNOWN
1069 && !(actual->tt_type == VAR_FUNC
1070 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
1071 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001072 if (!silent)
1073 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001074 return FAIL;
1075 }
1076 generate_TYPECHECK(cctx, expected, offset);
1077 return OK;
1078}
1079
1080/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 * Generate an ISN_PUSHNR instruction.
1082 */
1083 static int
1084generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1085{
1086 isn_T *isn;
1087
Bram Moolenaar080457c2020-03-03 21:53:32 +01001088 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001089 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1090 return FAIL;
1091 isn->isn_arg.number = number;
1092
1093 return OK;
1094}
1095
1096/*
1097 * Generate an ISN_PUSHBOOL instruction.
1098 */
1099 static int
1100generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1101{
1102 isn_T *isn;
1103
Bram Moolenaar080457c2020-03-03 21:53:32 +01001104 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1106 return FAIL;
1107 isn->isn_arg.number = number;
1108
1109 return OK;
1110}
1111
1112/*
1113 * Generate an ISN_PUSHSPEC instruction.
1114 */
1115 static int
1116generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1117{
1118 isn_T *isn;
1119
Bram Moolenaar080457c2020-03-03 21:53:32 +01001120 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1122 return FAIL;
1123 isn->isn_arg.number = number;
1124
1125 return OK;
1126}
1127
1128#ifdef FEAT_FLOAT
1129/*
1130 * Generate an ISN_PUSHF instruction.
1131 */
1132 static int
1133generate_PUSHF(cctx_T *cctx, float_T fnumber)
1134{
1135 isn_T *isn;
1136
Bram Moolenaar080457c2020-03-03 21:53:32 +01001137 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1139 return FAIL;
1140 isn->isn_arg.fnumber = fnumber;
1141
1142 return OK;
1143}
1144#endif
1145
1146/*
1147 * Generate an ISN_PUSHS instruction.
1148 * Consumes "str".
1149 */
1150 static int
1151generate_PUSHS(cctx_T *cctx, char_u *str)
1152{
1153 isn_T *isn;
1154
Bram Moolenaar080457c2020-03-03 21:53:32 +01001155 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1157 return FAIL;
1158 isn->isn_arg.string = str;
1159
1160 return OK;
1161}
1162
1163/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001164 * Generate an ISN_PUSHCHANNEL instruction.
1165 * Consumes "channel".
1166 */
1167 static int
1168generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1169{
1170 isn_T *isn;
1171
Bram Moolenaar080457c2020-03-03 21:53:32 +01001172 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001173 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1174 return FAIL;
1175 isn->isn_arg.channel = channel;
1176
1177 return OK;
1178}
1179
1180/*
1181 * Generate an ISN_PUSHJOB instruction.
1182 * Consumes "job".
1183 */
1184 static int
1185generate_PUSHJOB(cctx_T *cctx, job_T *job)
1186{
1187 isn_T *isn;
1188
Bram Moolenaar080457c2020-03-03 21:53:32 +01001189 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001190 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001191 return FAIL;
1192 isn->isn_arg.job = job;
1193
1194 return OK;
1195}
1196
1197/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 * Generate an ISN_PUSHBLOB instruction.
1199 * Consumes "blob".
1200 */
1201 static int
1202generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1203{
1204 isn_T *isn;
1205
Bram Moolenaar080457c2020-03-03 21:53:32 +01001206 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1208 return FAIL;
1209 isn->isn_arg.blob = blob;
1210
1211 return OK;
1212}
1213
1214/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001215 * Generate an ISN_PUSHFUNC instruction with name "name".
1216 * Consumes "name".
1217 */
1218 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001219generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001220{
1221 isn_T *isn;
1222
Bram Moolenaar080457c2020-03-03 21:53:32 +01001223 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001224 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001225 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001226 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001227
1228 return OK;
1229}
1230
1231/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001232 * Generate an ISN_GETITEM instruction with "index".
1233 */
1234 static int
1235generate_GETITEM(cctx_T *cctx, int index)
1236{
1237 isn_T *isn;
1238 garray_T *stack = &cctx->ctx_type_stack;
1239 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1240 type_T *item_type = &t_any;
1241
1242 RETURN_OK_IF_SKIP(cctx);
1243
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001244 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001245 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001246 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001247 emsg(_(e_listreq));
1248 return FAIL;
1249 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001250 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001251 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1252 return FAIL;
1253 isn->isn_arg.number = index;
1254
1255 // add the item type to the type stack
1256 if (ga_grow(stack, 1) == FAIL)
1257 return FAIL;
1258 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1259 ++stack->ga_len;
1260 return OK;
1261}
1262
1263/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001264 * Generate an ISN_SLICE instruction with "count".
1265 */
1266 static int
1267generate_SLICE(cctx_T *cctx, int count)
1268{
1269 isn_T *isn;
1270
1271 RETURN_OK_IF_SKIP(cctx);
1272 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1273 return FAIL;
1274 isn->isn_arg.number = count;
1275 return OK;
1276}
1277
1278/*
1279 * Generate an ISN_CHECKLEN instruction with "min_len".
1280 */
1281 static int
1282generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1283{
1284 isn_T *isn;
1285
1286 RETURN_OK_IF_SKIP(cctx);
1287
1288 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1289 return FAIL;
1290 isn->isn_arg.checklen.cl_min_len = min_len;
1291 isn->isn_arg.checklen.cl_more_OK = more_OK;
1292
1293 return OK;
1294}
1295
1296/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297 * Generate an ISN_STORE instruction.
1298 */
1299 static int
1300generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1301{
1302 isn_T *isn;
1303
Bram Moolenaar080457c2020-03-03 21:53:32 +01001304 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1306 return FAIL;
1307 if (name != NULL)
1308 isn->isn_arg.string = vim_strsave(name);
1309 else
1310 isn->isn_arg.number = idx;
1311
1312 return OK;
1313}
1314
1315/*
1316 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1317 */
1318 static int
1319generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1320{
1321 isn_T *isn;
1322
Bram Moolenaar080457c2020-03-03 21:53:32 +01001323 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1325 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001326 isn->isn_arg.storenr.stnr_idx = idx;
1327 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328
1329 return OK;
1330}
1331
1332/*
1333 * Generate an ISN_STOREOPT instruction
1334 */
1335 static int
1336generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1337{
1338 isn_T *isn;
1339
Bram Moolenaar080457c2020-03-03 21:53:32 +01001340 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001341 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 return FAIL;
1343 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1344 isn->isn_arg.storeopt.so_flags = opt_flags;
1345
1346 return OK;
1347}
1348
1349/*
1350 * Generate an ISN_LOAD or similar instruction.
1351 */
1352 static int
1353generate_LOAD(
1354 cctx_T *cctx,
1355 isntype_T isn_type,
1356 int idx,
1357 char_u *name,
1358 type_T *type)
1359{
1360 isn_T *isn;
1361
Bram Moolenaar080457c2020-03-03 21:53:32 +01001362 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1364 return FAIL;
1365 if (name != NULL)
1366 isn->isn_arg.string = vim_strsave(name);
1367 else
1368 isn->isn_arg.number = idx;
1369
1370 return OK;
1371}
1372
1373/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001374 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001375 */
1376 static int
1377generate_LOADV(
1378 cctx_T *cctx,
1379 char_u *name,
1380 int error)
1381{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001382 int di_flags;
1383 int vidx = find_vim_var(name, &di_flags);
1384 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001385
Bram Moolenaar080457c2020-03-03 21:53:32 +01001386 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001387 if (vidx < 0)
1388 {
1389 if (error)
1390 semsg(_(e_var_notfound), name);
1391 return FAIL;
1392 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001393 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001394
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001395 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001396}
1397
1398/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001399 * Generate an ISN_UNLET instruction.
1400 */
1401 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001402generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001403{
1404 isn_T *isn;
1405
1406 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001407 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001408 return FAIL;
1409 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1410 isn->isn_arg.unlet.ul_forceit = forceit;
1411
1412 return OK;
1413}
1414
1415/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 * Generate an ISN_LOADS instruction.
1417 */
1418 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001419generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001421 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001422 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001423 int sid,
1424 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425{
1426 isn_T *isn;
1427
Bram Moolenaar080457c2020-03-03 21:53:32 +01001428 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001429 if (isn_type == ISN_LOADS)
1430 isn = generate_instr_type(cctx, isn_type, type);
1431 else
1432 isn = generate_instr_drop(cctx, isn_type, 1);
1433 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001435 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1436 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437
1438 return OK;
1439}
1440
1441/*
1442 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1443 */
1444 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001445generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446 cctx_T *cctx,
1447 isntype_T isn_type,
1448 int sid,
1449 int idx,
1450 type_T *type)
1451{
1452 isn_T *isn;
1453
Bram Moolenaar080457c2020-03-03 21:53:32 +01001454 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 if (isn_type == ISN_LOADSCRIPT)
1456 isn = generate_instr_type(cctx, isn_type, type);
1457 else
1458 isn = generate_instr_drop(cctx, isn_type, 1);
1459 if (isn == NULL)
1460 return FAIL;
1461 isn->isn_arg.script.script_sid = sid;
1462 isn->isn_arg.script.script_idx = idx;
1463 return OK;
1464}
1465
1466/*
1467 * Generate an ISN_NEWLIST instruction.
1468 */
1469 static int
1470generate_NEWLIST(cctx_T *cctx, int count)
1471{
1472 isn_T *isn;
1473 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474 type_T *type;
1475 type_T *member;
1476
Bram Moolenaar080457c2020-03-03 21:53:32 +01001477 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1479 return FAIL;
1480 isn->isn_arg.number = count;
1481
1482 // drop the value types
1483 stack->ga_len -= count;
1484
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001485 // Use the first value type for the list member type. Use "any" for an
Bram Moolenaar436472f2020-02-20 22:54:43 +01001486 // empty list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 if (count > 0)
1488 member = ((type_T **)stack->ga_data)[stack->ga_len];
1489 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001490 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001491 type = get_list_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492
1493 // add the list type to the type stack
1494 if (ga_grow(stack, 1) == FAIL)
1495 return FAIL;
1496 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1497 ++stack->ga_len;
1498
1499 return OK;
1500}
1501
1502/*
1503 * Generate an ISN_NEWDICT instruction.
1504 */
1505 static int
1506generate_NEWDICT(cctx_T *cctx, int count)
1507{
1508 isn_T *isn;
1509 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 type_T *type;
1511 type_T *member;
1512
Bram Moolenaar080457c2020-03-03 21:53:32 +01001513 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001514 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1515 return FAIL;
1516 isn->isn_arg.number = count;
1517
1518 // drop the key and value types
1519 stack->ga_len -= 2 * count;
1520
Bram Moolenaar436472f2020-02-20 22:54:43 +01001521 // Use the first value type for the list member type. Use "void" for an
1522 // empty dict.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523 if (count > 0)
1524 member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
1525 else
Bram Moolenaar436472f2020-02-20 22:54:43 +01001526 member = &t_void;
Bram Moolenaard77a8522020-04-03 21:59:57 +02001527 type = get_dict_type(member, cctx->ctx_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528
1529 // add the dict type to the type stack
1530 if (ga_grow(stack, 1) == FAIL)
1531 return FAIL;
1532 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1533 ++stack->ga_len;
1534
1535 return OK;
1536}
1537
1538/*
1539 * Generate an ISN_FUNCREF instruction.
1540 */
1541 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001542generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543{
1544 isn_T *isn;
1545 garray_T *stack = &cctx->ctx_type_stack;
1546
Bram Moolenaar080457c2020-03-03 21:53:32 +01001547 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1549 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001550 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001551 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552
1553 if (ga_grow(stack, 1) == FAIL)
1554 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001555 ((type_T **)stack->ga_data)[stack->ga_len] =
1556 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 ++stack->ga_len;
1558
1559 return OK;
1560}
1561
1562/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001563 * Generate an ISN_NEWFUNC instruction.
1564 */
1565 static int
1566generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1567{
1568 isn_T *isn;
1569 char_u *name;
1570
1571 RETURN_OK_IF_SKIP(cctx);
1572 name = vim_strsave(lambda_name);
1573 if (name == NULL)
1574 return FAIL;
1575 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1576 return FAIL;
1577 isn->isn_arg.newfunc.nf_lambda = name;
1578 isn->isn_arg.newfunc.nf_global = func_name;
1579
1580 return OK;
1581}
1582
1583/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 * Generate an ISN_JUMP instruction.
1585 */
1586 static int
1587generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1588{
1589 isn_T *isn;
1590 garray_T *stack = &cctx->ctx_type_stack;
1591
Bram Moolenaar080457c2020-03-03 21:53:32 +01001592 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1594 return FAIL;
1595 isn->isn_arg.jump.jump_when = when;
1596 isn->isn_arg.jump.jump_where = where;
1597
1598 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1599 --stack->ga_len;
1600
1601 return OK;
1602}
1603
1604 static int
1605generate_FOR(cctx_T *cctx, int loop_idx)
1606{
1607 isn_T *isn;
1608 garray_T *stack = &cctx->ctx_type_stack;
1609
Bram Moolenaar080457c2020-03-03 21:53:32 +01001610 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1612 return FAIL;
1613 isn->isn_arg.forloop.for_idx = loop_idx;
1614
1615 if (ga_grow(stack, 1) == FAIL)
1616 return FAIL;
1617 // type doesn't matter, will be stored next
1618 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1619 ++stack->ga_len;
1620
1621 return OK;
1622}
1623
1624/*
1625 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001626 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627 * Return FAIL if the number of arguments is wrong.
1628 */
1629 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001630generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631{
1632 isn_T *isn;
1633 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001634 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001635 type_T *argtypes[MAX_FUNC_ARGS];
1636 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637
Bram Moolenaar080457c2020-03-03 21:53:32 +01001638 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001639 argoff = check_internal_func(func_idx, argcount);
1640 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 return FAIL;
1642
Bram Moolenaar389df252020-07-09 21:20:47 +02001643 if (method_call && argoff > 1)
1644 {
1645 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1646 return FAIL;
1647 isn->isn_arg.shuffle.shfl_item = argcount;
1648 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1649 }
1650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1652 return FAIL;
1653 isn->isn_arg.bfunc.cbf_idx = func_idx;
1654 isn->isn_arg.bfunc.cbf_argcount = argcount;
1655
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001656 for (i = 0; i < argcount; ++i)
1657 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 stack->ga_len -= argcount; // drop the arguments
1660 if (ga_grow(stack, 1) == FAIL)
1661 return FAIL;
1662 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001663 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664 ++stack->ga_len; // add return value
1665
1666 return OK;
1667}
1668
1669/*
1670 * Generate an ISN_DCALL or ISN_UCALL instruction.
1671 * Return FAIL if the number of arguments is wrong.
1672 */
1673 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001674generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675{
1676 isn_T *isn;
1677 garray_T *stack = &cctx->ctx_type_stack;
1678 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001679 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680
Bram Moolenaar080457c2020-03-03 21:53:32 +01001681 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 if (argcount > regular_args && !has_varargs(ufunc))
1683 {
1684 semsg(_(e_toomanyarg), ufunc->uf_name);
1685 return FAIL;
1686 }
1687 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1688 {
1689 semsg(_(e_toofewarg), ufunc->uf_name);
1690 return FAIL;
1691 }
1692
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001693 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001694 {
1695 int i;
1696
1697 for (i = 0; i < argcount; ++i)
1698 {
1699 type_T *expected;
1700 type_T *actual;
1701
1702 if (i < regular_args)
1703 {
1704 if (ufunc->uf_arg_types == NULL)
1705 continue;
1706 expected = ufunc->uf_arg_types[i];
1707 }
1708 else
1709 expected = ufunc->uf_va_type->tt_member;
1710 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001711 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001712 {
1713 arg_type_mismatch(expected, actual, i + 1);
1714 return FAIL;
1715 }
1716 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001717 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001718 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1719 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001720 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001721 }
1722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001724 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001725 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001727 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 {
1729 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1730 isn->isn_arg.dfunc.cdf_argcount = argcount;
1731 }
1732 else
1733 {
1734 // A user function may be deleted and redefined later, can't use the
1735 // ufunc pointer, need to look it up again at runtime.
1736 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1737 isn->isn_arg.ufunc.cuf_argcount = argcount;
1738 }
1739
1740 stack->ga_len -= argcount; // drop the arguments
1741 if (ga_grow(stack, 1) == FAIL)
1742 return FAIL;
1743 // add return value
1744 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1745 ++stack->ga_len;
1746
1747 return OK;
1748}
1749
1750/*
1751 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1752 */
1753 static int
1754generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1755{
1756 isn_T *isn;
1757 garray_T *stack = &cctx->ctx_type_stack;
1758
Bram Moolenaar080457c2020-03-03 21:53:32 +01001759 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1761 return FAIL;
1762 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1763 isn->isn_arg.ufunc.cuf_argcount = argcount;
1764
1765 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001766 if (ga_grow(stack, 1) == FAIL)
1767 return FAIL;
1768 // add return value
1769 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1770 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771
1772 return OK;
1773}
1774
1775/*
1776 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001777 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 */
1779 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001780generate_PCALL(
1781 cctx_T *cctx,
1782 int argcount,
1783 char_u *name,
1784 type_T *type,
1785 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786{
1787 isn_T *isn;
1788 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001789 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790
Bram Moolenaar080457c2020-03-03 21:53:32 +01001791 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001792
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001793 if (type->tt_type == VAR_ANY)
1794 ret_type = &t_any;
1795 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001796 {
1797 if (type->tt_argcount != -1)
1798 {
1799 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1800
1801 if (argcount < type->tt_min_argcount - varargs)
1802 {
1803 semsg(_(e_toofewarg), "[reference]");
1804 return FAIL;
1805 }
1806 if (!varargs && argcount > type->tt_argcount)
1807 {
1808 semsg(_(e_toomanyarg), "[reference]");
1809 return FAIL;
1810 }
1811 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001812 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001813 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001814 else
1815 {
1816 semsg(_("E1085: Not a callable type: %s"), name);
1817 return FAIL;
1818 }
1819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1821 return FAIL;
1822 isn->isn_arg.pfunc.cpf_top = at_top;
1823 isn->isn_arg.pfunc.cpf_argcount = argcount;
1824
1825 stack->ga_len -= argcount; // drop the arguments
1826
1827 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001828 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001830 // If partial is above the arguments it must be cleared and replaced with
1831 // the return value.
1832 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1833 return FAIL;
1834
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 return OK;
1836}
1837
1838/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001839 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 */
1841 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001842generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843{
1844 isn_T *isn;
1845 garray_T *stack = &cctx->ctx_type_stack;
1846 type_T *type;
1847
Bram Moolenaar080457c2020-03-03 21:53:32 +01001848 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001849 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001851 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001853 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001855 if (type->tt_type != VAR_DICT && type != &t_any)
1856 {
1857 emsg(_(e_dictreq));
1858 return FAIL;
1859 }
1860 // change dict type to dict member type
1861 if (type->tt_type == VAR_DICT)
1862 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001863
1864 return OK;
1865}
1866
1867/*
1868 * Generate an ISN_ECHO instruction.
1869 */
1870 static int
1871generate_ECHO(cctx_T *cctx, int with_white, int count)
1872{
1873 isn_T *isn;
1874
Bram Moolenaar080457c2020-03-03 21:53:32 +01001875 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1877 return FAIL;
1878 isn->isn_arg.echo.echo_with_white = with_white;
1879 isn->isn_arg.echo.echo_count = count;
1880
1881 return OK;
1882}
1883
Bram Moolenaarad39c092020-02-26 18:23:43 +01001884/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001885 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001886 */
1887 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001888generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001889{
1890 isn_T *isn;
1891
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001892 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001893 return FAIL;
1894 isn->isn_arg.number = count;
1895
1896 return OK;
1897}
1898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 static int
1900generate_EXEC(cctx_T *cctx, char_u *line)
1901{
1902 isn_T *isn;
1903
Bram Moolenaar080457c2020-03-03 21:53:32 +01001904 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1906 return FAIL;
1907 isn->isn_arg.string = vim_strsave(line);
1908 return OK;
1909}
1910
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001911 static int
1912generate_EXECCONCAT(cctx_T *cctx, int count)
1913{
1914 isn_T *isn;
1915
1916 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1917 return FAIL;
1918 isn->isn_arg.number = count;
1919 return OK;
1920}
1921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922/*
1923 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001924 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001926 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1928{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 lvar_T *lvar;
1930
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001931 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001933 emsg_namelen(_(e_used_as_arg), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001934 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 }
1936
1937 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001938 return NULL;
1939 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001941 // Every local variable uses the next entry on the stack. We could re-use
1942 // the last ones when leaving a scope, but then variables used in a closure
1943 // might get overwritten. To keep things simple do not re-use stack
1944 // entries. This is less efficient, but memory is cheap these days.
1945 lvar->lv_idx = cctx->ctx_locals_count++;
1946
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001947 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 lvar->lv_const = isConst;
1949 lvar->lv_type = type;
1950
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001951 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952}
1953
1954/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001955 * Remove local variables above "new_top".
1956 */
1957 static void
1958unwind_locals(cctx_T *cctx, int new_top)
1959{
1960 if (cctx->ctx_locals.ga_len > new_top)
1961 {
1962 int idx;
1963 lvar_T *lvar;
1964
1965 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1966 {
1967 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1968 vim_free(lvar->lv_name);
1969 }
1970 }
1971 cctx->ctx_locals.ga_len = new_top;
1972}
1973
1974/*
1975 * Free all local variables.
1976 */
1977 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001978free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001979{
1980 unwind_locals(cctx, 0);
1981 ga_clear(&cctx->ctx_locals);
1982}
1983
1984/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 * Skip over a type definition and return a pointer to just after it.
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001986 * When "optional" is TRUE then a leading "?" is accepted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 */
1988 char_u *
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001989skip_type(char_u *start, int optional)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990{
1991 char_u *p = start;
1992
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001993 if (optional && *p == '?')
1994 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 while (ASCII_ISALNUM(*p) || *p == '_')
1996 ++p;
1997
1998 // Skip over "<type>"; this is permissive about white space.
1999 if (*skipwhite(p) == '<')
2000 {
2001 p = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02002002 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 p = skipwhite(p);
2004 if (*p == '>')
2005 ++p;
2006 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002007 else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1])))
2008 && STRNCMP("func", start, 4) == 0)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002009 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002010 if (*p == '(')
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002011 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002012 // handle func(args): type
2013 ++p;
2014 while (*p != ')' && *p != NUL)
2015 {
2016 char_u *sp = p;
Bram Moolenaar1c0d44f2020-05-02 19:04:58 +02002017
Bram Moolenaarace61322020-07-26 18:16:58 +02002018 if (STRNCMP(p, "...", 3) == 0)
2019 p += 3;
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02002020 p = skip_type(p, TRUE);
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002021 if (p == sp)
2022 return p; // syntax error
2023 if (*p == ',')
2024 p = skipwhite(p + 1);
2025 }
2026 if (*p == ')')
2027 {
2028 if (p[1] == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02002029 p = skip_type(skipwhite(p + 2), FALSE);
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002030 else
Bram Moolenaarbfba8652020-07-23 20:09:10 +02002031 ++p;
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002032 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002033 }
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002034 else
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002035 {
Bram Moolenaar2f1980f2020-07-22 19:30:06 +02002036 // handle func: return_type
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02002037 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002038 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002039 }
2040
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002041 return p;
2042}
2043
2044/*
2045 * Parse the member type: "<type>" and return "type" with the member set.
Bram Moolenaard77a8522020-04-03 21:59:57 +02002046 * Use "type_gap" if a new type needs to be added.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047 * Returns NULL in case of failure.
2048 */
2049 static type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02002050parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051{
2052 type_T *member_type;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002053 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054
2055 if (**arg != '<')
2056 {
2057 if (*skipwhite(*arg) == '<')
Bram Moolenaard77a8522020-04-03 21:59:57 +02002058 semsg(_(e_no_white_before), "<");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059 else
2060 emsg(_("E1008: Missing <type>"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01002061 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062 }
2063 *arg = skipwhite(*arg + 1);
2064
Bram Moolenaard77a8522020-04-03 21:59:57 +02002065 member_type = parse_type(arg, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066
2067 *arg = skipwhite(*arg);
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002068 if (**arg != '>' && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069 {
2070 emsg(_("E1009: Missing > after type"));
Bram Moolenaarcf3f8bf2020-03-26 13:15:42 +01002071 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 }
2073 ++*arg;
2074
2075 if (type->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002076 return get_list_type(member_type, type_gap);
2077 return get_dict_type(member_type, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078}
2079
2080/*
2081 * Parse a type at "arg" and advance over it.
Bram Moolenaara8c17702020-04-01 21:17:24 +02002082 * Return &t_any for failure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083 */
2084 type_T *
Bram Moolenaard77a8522020-04-03 21:59:57 +02002085parse_type(char_u **arg, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086{
2087 char_u *p = *arg;
2088 size_t len;
2089
2090 // skip over the first word
2091 while (ASCII_ISALNUM(*p) || *p == '_')
2092 ++p;
2093 len = p - *arg;
2094
2095 switch (**arg)
2096 {
2097 case 'a':
2098 if (len == 3 && STRNCMP(*arg, "any", len) == 0)
2099 {
2100 *arg += len;
2101 return &t_any;
2102 }
2103 break;
2104 case 'b':
2105 if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
2106 {
2107 *arg += len;
2108 return &t_bool;
2109 }
2110 if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
2111 {
2112 *arg += len;
2113 return &t_blob;
2114 }
2115 break;
2116 case 'c':
2117 if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
2118 {
2119 *arg += len;
2120 return &t_channel;
2121 }
2122 break;
2123 case 'd':
2124 if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
2125 {
2126 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002127 return parse_type_member(arg, &t_dict_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 }
2129 break;
2130 case 'f':
2131 if (len == 5 && STRNCMP(*arg, "float", len) == 0)
2132 {
Bram Moolenaara5d59532020-01-26 21:42:03 +01002133#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 *arg += len;
2135 return &t_float;
Bram Moolenaara5d59532020-01-26 21:42:03 +01002136#else
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002137 emsg(_("E1076: This Vim is not compiled with float support"));
Bram Moolenaara5d59532020-01-26 21:42:03 +01002138 return &t_any;
2139#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 }
2141 if (len == 4 && STRNCMP(*arg, "func", len) == 0)
2142 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002143 type_T *type;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002144 type_T *ret_type = &t_unknown;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002145 int argcount = -1;
2146 int flags = 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002147 int first_optional = -1;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002148 type_T *arg_type[MAX_FUNC_ARGS + 1];
2149
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002150 // func({type}, ...{type}): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002152 if (**arg == '(')
2153 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002154 // "func" may or may not return a value, "func()" does
2155 // not return a value.
2156 ret_type = &t_void;
2157
Bram Moolenaard77a8522020-04-03 21:59:57 +02002158 p = ++*arg;
2159 argcount = 0;
2160 while (*p != NUL && *p != ')')
2161 {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002162 if (*p == '?')
2163 {
2164 if (first_optional == -1)
2165 first_optional = argcount;
2166 ++p;
2167 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002168 else if (STRNCMP(p, "...", 3) == 0)
2169 {
2170 flags |= TTFLAG_VARARGS;
2171 p += 3;
2172 }
Bram Moolenaar01865ad2020-07-26 18:33:09 +02002173 else if (first_optional != -1)
2174 {
2175 emsg(_("E1007: mandatory argument after optional argument"));
2176 return &t_any;
2177 }
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002178
2179 arg_type[argcount++] = parse_type(&p, type_gap);
2180
2181 // Nothing comes after "...{type}".
2182 if (flags & TTFLAG_VARARGS)
2183 break;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002184
Bram Moolenaard77a8522020-04-03 21:59:57 +02002185 if (*p != ',' && *skipwhite(p) == ',')
2186 {
2187 semsg(_(e_no_white_before), ",");
2188 return &t_any;
2189 }
2190 if (*p == ',')
2191 {
2192 ++p;
2193 if (!VIM_ISWHITE(*p))
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002194 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02002195 semsg(_(e_white_after), ",");
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002196 return &t_any;
2197 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002198 }
2199 p = skipwhite(p);
2200 if (argcount == MAX_FUNC_ARGS)
2201 {
2202 emsg(_("E740: Too many argument types"));
2203 return &t_any;
2204 }
2205 }
2206
2207 p = skipwhite(p);
2208 if (*p != ')')
2209 {
2210 emsg(_(e_missing_close));
2211 return &t_any;
2212 }
2213 *arg = p + 1;
2214 }
2215 if (**arg == ':')
2216 {
2217 // parse return type
2218 ++*arg;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02002219 if (!VIM_ISWHITE(**arg))
Bram Moolenaard77a8522020-04-03 21:59:57 +02002220 semsg(_(e_white_after), ":");
2221 *arg = skipwhite(*arg);
2222 ret_type = parse_type(arg, type_gap);
2223 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002224 if (flags == 0 && first_optional == -1 && argcount <= 0)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002225 type = get_func_type(ret_type, argcount, type_gap);
2226 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002227 {
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002228 type = alloc_func_type(ret_type, argcount, type_gap);
2229 type->tt_flags = flags;
2230 if (argcount > 0)
2231 {
2232 type->tt_argcount = argcount;
2233 type->tt_min_argcount = first_optional == -1
2234 ? argcount : first_optional;
2235 if (func_type_add_arg_types(type, argcount,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002236 type_gap) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002237 return &t_any;
2238 mch_memmove(type->tt_args, arg_type,
Bram Moolenaard77a8522020-04-03 21:59:57 +02002239 sizeof(type_T *) * argcount);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002240 }
Bram Moolenaard77a8522020-04-03 21:59:57 +02002241 }
2242 return type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002243 }
2244 break;
2245 case 'j':
2246 if (len == 3 && STRNCMP(*arg, "job", len) == 0)
2247 {
2248 *arg += len;
2249 return &t_job;
2250 }
2251 break;
2252 case 'l':
2253 if (len == 4 && STRNCMP(*arg, "list", len) == 0)
2254 {
2255 *arg += len;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002256 return parse_type_member(arg, &t_list_any, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 }
2258 break;
2259 case 'n':
2260 if (len == 6 && STRNCMP(*arg, "number", len) == 0)
2261 {
2262 *arg += len;
2263 return &t_number;
2264 }
2265 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 case 's':
2267 if (len == 6 && STRNCMP(*arg, "string", len) == 0)
2268 {
2269 *arg += len;
2270 return &t_string;
2271 }
2272 break;
2273 case 'v':
2274 if (len == 4 && STRNCMP(*arg, "void", len) == 0)
2275 {
2276 *arg += len;
2277 return &t_void;
2278 }
2279 break;
2280 }
2281
2282 semsg(_("E1010: Type not recognized: %s"), *arg);
2283 return &t_any;
2284}
2285
2286/*
2287 * Check if "type1" and "type2" are exactly the same.
2288 */
2289 static int
2290equal_type(type_T *type1, type_T *type2)
2291{
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002292 int i;
2293
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294 if (type1->tt_type != type2->tt_type)
2295 return FALSE;
2296 switch (type1->tt_type)
2297 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002299 case VAR_ANY:
2300 case VAR_VOID:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 case VAR_SPECIAL:
2302 case VAR_BOOL:
2303 case VAR_NUMBER:
2304 case VAR_FLOAT:
2305 case VAR_STRING:
2306 case VAR_BLOB:
2307 case VAR_JOB:
2308 case VAR_CHANNEL:
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002309 break; // not composite is always OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002310 case VAR_LIST:
2311 case VAR_DICT:
2312 return equal_type(type1->tt_member, type2->tt_member);
2313 case VAR_FUNC:
2314 case VAR_PARTIAL:
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002315 if (!equal_type(type1->tt_member, type2->tt_member)
2316 || type1->tt_argcount != type2->tt_argcount)
2317 return FALSE;
2318 if (type1->tt_argcount < 0
2319 || type1->tt_args == NULL || type2->tt_args == NULL)
2320 return TRUE;
2321 for (i = 0; i < type1->tt_argcount; ++i)
2322 if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
2323 return FALSE;
2324 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325 }
2326 return TRUE;
2327}
2328
2329/*
2330 * Find the common type of "type1" and "type2" and put it in "dest".
2331 * "type2" and "dest" may be the same.
2332 */
2333 static void
Bram Moolenaard77a8522020-04-03 21:59:57 +02002334common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335{
2336 if (equal_type(type1, type2))
2337 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002338 *dest = type1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339 return;
2340 }
2341
2342 if (type1->tt_type == type2->tt_type)
2343 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
2345 {
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002346 type_T *common;
2347
Bram Moolenaard77a8522020-04-03 21:59:57 +02002348 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002349 if (type1->tt_type == VAR_LIST)
Bram Moolenaard77a8522020-04-03 21:59:57 +02002350 *dest = get_list_type(common, type_gap);
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002351 else
Bram Moolenaard77a8522020-04-03 21:59:57 +02002352 *dest = get_dict_type(common, type_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 return;
2354 }
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002355 if (type1->tt_type == VAR_FUNC)
2356 {
2357 type_T *common;
2358
2359 common_type(type1->tt_member, type2->tt_member, &common, type_gap);
2360 if (type1->tt_argcount == type2->tt_argcount
2361 && type1->tt_argcount >= 0)
2362 {
2363 int argcount = type1->tt_argcount;
2364 int i;
2365
2366 *dest = alloc_func_type(common, argcount, type_gap);
2367 if (type1->tt_args != NULL && type2->tt_args != NULL)
2368 {
Bram Moolenaarc5f1ef52020-04-12 17:11:27 +02002369 if (func_type_add_arg_types(*dest, argcount,
2370 type_gap) == OK)
Bram Moolenaar99aaf0c2020-04-12 14:39:53 +02002371 for (i = 0; i < argcount; ++i)
2372 common_type(type1->tt_args[i], type2->tt_args[i],
2373 &(*dest)->tt_args[i], type_gap);
2374 }
2375 }
2376 else
2377 *dest = alloc_func_type(common, -1, type_gap);
2378 return;
2379 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380 }
2381
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002382 *dest = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383}
2384
2385 char *
2386vartype_name(vartype_T type)
2387{
2388 switch (type)
2389 {
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002390 case VAR_UNKNOWN: break;
Bram Moolenaar4c683752020-04-05 21:38:23 +02002391 case VAR_ANY: return "any";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392 case VAR_VOID: return "void";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 case VAR_SPECIAL: return "special";
2394 case VAR_BOOL: return "bool";
2395 case VAR_NUMBER: return "number";
2396 case VAR_FLOAT: return "float";
2397 case VAR_STRING: return "string";
2398 case VAR_BLOB: return "blob";
2399 case VAR_JOB: return "job";
2400 case VAR_CHANNEL: return "channel";
2401 case VAR_LIST: return "list";
2402 case VAR_DICT: return "dict";
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002403
2404 case VAR_FUNC:
2405 case VAR_PARTIAL: return "func";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406 }
Bram Moolenaar4c683752020-04-05 21:38:23 +02002407 return "unknown";
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408}
2409
2410/*
2411 * Return the name of a type.
2412 * The result may be in allocated memory, in which case "tofree" is set.
2413 */
2414 char *
2415type_name(type_T *type, char **tofree)
2416{
2417 char *name = vartype_name(type->tt_type);
2418
2419 *tofree = NULL;
2420 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
2421 {
2422 char *member_free;
2423 char *member_name = type_name(type->tt_member, &member_free);
2424 size_t len;
2425
2426 len = STRLEN(name) + STRLEN(member_name) + 3;
2427 *tofree = alloc(len);
2428 if (*tofree != NULL)
2429 {
2430 vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
2431 vim_free(member_free);
2432 return *tofree;
2433 }
2434 }
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002435 if (type->tt_type == VAR_FUNC)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002436 {
2437 garray_T ga;
2438 int i;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002439 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002440
2441 ga_init2(&ga, 1, 100);
2442 if (ga_grow(&ga, 20) == FAIL)
2443 return "[unknown]";
2444 *tofree = ga.ga_data;
2445 STRCPY(ga.ga_data, "func(");
2446 ga.ga_len += 5;
2447
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002448 for (i = 0; i < type->tt_argcount; ++i)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002449 {
2450 char *arg_free;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002451 char *arg_type;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002452 int len;
2453
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002454 if (type->tt_args == NULL)
2455 arg_type = "[unknown]";
2456 else
2457 arg_type = type_name(type->tt_args[i], &arg_free);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002458 if (i > 0)
2459 {
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002460 STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002461 ga.ga_len += 2;
2462 }
2463 len = (int)STRLEN(arg_type);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002464 if (ga_grow(&ga, len + 8) == FAIL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002465 {
2466 vim_free(arg_free);
2467 return "[unknown]";
2468 }
2469 *tofree = ga.ga_data;
Bram Moolenaar08938ee2020-04-11 23:17:17 +02002470 if (varargs && i == type->tt_argcount - 1)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02002471 {
2472 STRCPY((char *)ga.ga_data + ga.ga_len, "...");
2473 ga.ga_len += 3;
2474 }
2475 else if (i >= type->tt_min_argcount)
2476 *((char *)ga.ga_data + ga.ga_len++) = '?';
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002477 STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002478 ga.ga_len += len;
2479 vim_free(arg_free);
2480 }
2481
2482 if (type->tt_member == &t_void)
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002483 STRCPY((char *)ga.ga_data + ga.ga_len, ")");
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002484 else
2485 {
2486 char *ret_free;
2487 char *ret_name = type_name(type->tt_member, &ret_free);
2488 int len;
2489
2490 len = (int)STRLEN(ret_name) + 4;
2491 if (ga_grow(&ga, len) == FAIL)
2492 {
2493 vim_free(ret_free);
2494 return "[unknown]";
2495 }
2496 *tofree = ga.ga_data;
Bram Moolenaarb8ed3aa2020-04-05 19:09:05 +02002497 STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
2498 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002499 vim_free(ret_free);
2500 }
2501 return ga.ga_data;
2502 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503
2504 return name;
2505}
2506
2507/*
2508 * Find "name" in script-local items of script "sid".
2509 * Returns the index in "sn_var_vals" if found.
2510 * If found but not in "sn_var_vals" returns -1.
2511 * If not found returns -2.
2512 */
2513 int
2514get_script_item_idx(int sid, char_u *name, int check_writable)
2515{
2516 hashtab_T *ht;
2517 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002518 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 int idx;
2520
2521 // First look the name up in the hashtable.
2522 if (sid <= 0 || sid > script_items.ga_len)
2523 return -1;
2524 ht = &SCRIPT_VARS(sid);
2525 di = find_var_in_ht(ht, 0, name, TRUE);
2526 if (di == NULL)
2527 return -2;
2528
2529 // Now find the svar_T index in sn_var_vals.
2530 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2531 {
2532 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2533
2534 if (sv->sv_tv == &di->di_tv)
2535 {
2536 if (check_writable && sv->sv_const)
2537 semsg(_(e_readonlyvar), name);
2538 return idx;
2539 }
2540 }
2541 return -1;
2542}
2543
2544/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002545 * Find "name" in imported items of the current script or in "cctx" if not
2546 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 */
2548 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002549find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550{
Bram Moolenaar086eb182020-07-01 16:00:44 +02002551 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 int idx;
2553
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002554 if (current_sctx.sc_sid <= 0)
2555 return NULL;
2556 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 if (cctx != NULL)
2558 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2559 {
2560 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2561 + idx;
2562
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002563 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2564 : STRLEN(import->imp_name) == len
2565 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 return import;
2567 }
2568
2569 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2570 {
2571 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2572
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002573 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2574 : STRLEN(import->imp_name) == len
2575 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 return import;
2577 }
2578 return NULL;
2579}
2580
2581/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002582 * Free all imported variables.
2583 */
2584 static void
2585free_imported(cctx_T *cctx)
2586{
2587 int idx;
2588
2589 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2590 {
2591 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2592
2593 vim_free(import->imp_name);
2594 }
2595 ga_clear(&cctx->ctx_imports);
2596}
2597
2598/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002599 * Return TRUE if "p" points at a "#" but not at "#{".
2600 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002601 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002602vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002603{
2604 return p[0] == '#' && p[1] != '{';
2605}
2606
2607/*
2608 * Return a pointer to the next line that isn't empty or only contains a
2609 * comment. Skips over white space.
2610 * Returns NULL if there is none.
2611 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002612 char_u *
2613peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002614{
2615 int lnum = cctx->ctx_lnum;
2616
2617 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2618 {
2619 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002620 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002621
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002622 if (line == NULL)
2623 break;
2624 p = skipwhite(line);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002625 if (*p != NUL && !vim9_comment_start(p))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002626 return p;
2627 }
2628 return NULL;
2629}
2630
2631/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002632 * Called when checking for a following operator at "arg". When the rest of
2633 * the line is empty or only a comment, peek the next line. If there is a next
2634 * line return a pointer to it and set "nextp".
2635 * Otherwise skip over white space.
2636 */
2637 static char_u *
2638may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2639{
2640 char_u *p = skipwhite(arg);
2641
2642 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002643 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002644 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002645 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002646 if (*nextp != NULL)
2647 return *nextp;
2648 }
2649 return p;
2650}
2651
2652/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002653 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002654 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002655 * Returns NULL when at the end.
2656 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002657 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002658next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002659{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002660 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002661
2662 do
2663 {
2664 ++cctx->ctx_lnum;
2665 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002666 {
2667 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002668 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002669 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002670 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002671 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002672 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002673 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002674 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002675 return line;
2676}
2677
2678/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002679 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002680 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002681 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2682 */
2683 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002684may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002685{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002686 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002687 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002688 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002689
2690 if (next == NULL)
2691 return FAIL;
2692 *arg = skipwhite(next);
2693 }
2694 return OK;
2695}
2696
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002697/*
2698 * Idem, and give an error when failed.
2699 */
2700 static int
2701may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2702{
2703 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2704 {
2705 emsg(_("E1097: line incomplete"));
2706 return FAIL;
2707 }
2708 return OK;
2709}
2710
2711
Bram Moolenaara5565e42020-05-09 15:44:01 +02002712// Structure passed between the compile_expr* functions to keep track of
2713// constants that have been parsed but for which no code was produced yet. If
2714// possible expressions on these constants are applied at compile time. If
2715// that is not possible, the code to push the constants needs to be generated
2716// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002717// Using 50 should be more than enough of 5 levels of ().
2718#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002719typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002720 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002721 int pp_used; // active entries in pp_tv[]
2722} ppconst_T;
2723
Bram Moolenaar1c747212020-05-09 18:28:34 +02002724static int compile_expr0(char_u **arg, cctx_T *cctx);
2725static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2726
Bram Moolenaara5565e42020-05-09 15:44:01 +02002727/*
2728 * Generate a PUSH instruction for "tv".
2729 * "tv" will be consumed or cleared.
2730 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2731 */
2732 static int
2733generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2734{
2735 if (tv != NULL)
2736 {
2737 switch (tv->v_type)
2738 {
2739 case VAR_UNKNOWN:
2740 break;
2741 case VAR_BOOL:
2742 generate_PUSHBOOL(cctx, tv->vval.v_number);
2743 break;
2744 case VAR_SPECIAL:
2745 generate_PUSHSPEC(cctx, tv->vval.v_number);
2746 break;
2747 case VAR_NUMBER:
2748 generate_PUSHNR(cctx, tv->vval.v_number);
2749 break;
2750#ifdef FEAT_FLOAT
2751 case VAR_FLOAT:
2752 generate_PUSHF(cctx, tv->vval.v_float);
2753 break;
2754#endif
2755 case VAR_BLOB:
2756 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2757 tv->vval.v_blob = NULL;
2758 break;
2759 case VAR_STRING:
2760 generate_PUSHS(cctx, tv->vval.v_string);
2761 tv->vval.v_string = NULL;
2762 break;
2763 default:
2764 iemsg("constant type not supported");
2765 clear_tv(tv);
2766 return FAIL;
2767 }
2768 tv->v_type = VAR_UNKNOWN;
2769 }
2770 return OK;
2771}
2772
2773/*
2774 * Generate code for any ppconst entries.
2775 */
2776 static int
2777generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2778{
2779 int i;
2780 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002781 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002782
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002783 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002784 for (i = 0; i < ppconst->pp_used; ++i)
2785 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2786 ret = FAIL;
2787 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002788 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002789 return ret;
2790}
2791
2792/*
2793 * Clear ppconst constants. Used when failing.
2794 */
2795 static void
2796clear_ppconst(ppconst_T *ppconst)
2797{
2798 int i;
2799
2800 for (i = 0; i < ppconst->pp_used; ++i)
2801 clear_tv(&ppconst->pp_tv[i]);
2802 ppconst->pp_used = 0;
2803}
2804
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002805/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002806 * Generate an instruction to load script-local variable "name", without the
2807 * leading "s:".
2808 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 */
2810 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002811compile_load_scriptvar(
2812 cctx_T *cctx,
2813 char_u *name, // variable NUL terminated
2814 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002815 char_u **end, // end of variable
2816 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002818 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
2820 imported_T *import;
2821
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002822 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002823 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002824 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002825 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2826 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 }
2828 if (idx >= 0)
2829 {
2830 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2831
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002832 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 current_sctx.sc_sid, idx, sv->sv_type);
2834 return OK;
2835 }
2836
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002837 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 if (import != NULL)
2839 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002840 if (import->imp_all)
2841 {
2842 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002843 char_u *exp_name;
2844 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002845 ufunc_T *ufunc;
2846 type_T *type;
2847
2848 // Used "import * as Name", need to lookup the member.
2849 if (*p != '.')
2850 {
2851 semsg(_("E1060: expected dot after name: %s"), start);
2852 return FAIL;
2853 }
2854 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002855 if (VIM_ISWHITE(*p))
2856 {
2857 emsg(_("E1074: no white space allowed after dot"));
2858 return FAIL;
2859 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002860
Bram Moolenaar1c991142020-07-04 13:15:31 +02002861 // isolate one name
2862 exp_name = p;
2863 while (eval_isnamec(*p))
2864 ++p;
2865 cc = *p;
2866 *p = NUL;
2867
2868 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2869 *p = cc;
2870 p = skipwhite(p);
2871
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002872 // TODO: what if it is a function?
2873 if (idx < 0)
2874 return FAIL;
2875 *end = p;
2876
2877 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2878 import->imp_sid,
2879 idx,
2880 type);
2881 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002882 else if (import->imp_funcname != NULL)
2883 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002884 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002885 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2886 import->imp_sid,
2887 import->imp_var_vals_idx,
2888 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 return OK;
2890 }
2891
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002892 if (error)
2893 semsg(_("E1050: Item not found: %s"), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 return FAIL;
2895}
2896
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002897 static int
2898generate_funcref(cctx_T *cctx, char_u *name)
2899{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002900 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002901
2902 if (ufunc == NULL)
2903 return FAIL;
2904
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002905 // Need to compile any default values to get the argument types.
2906 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2907 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2908 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002909 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002910}
2911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912/*
2913 * Compile a variable name into a load instruction.
2914 * "end" points to just after the name.
2915 * When "error" is FALSE do not give an error when not found.
2916 */
2917 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002918compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919{
2920 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002921 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002922 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002924 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925
2926 if (*(*arg + 1) == ':')
2927 {
2928 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002929 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002930 {
2931 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002933 switch (**arg)
2934 {
2935 case 'g': isn_type = ISN_LOADGDICT; break;
2936 case 'w': isn_type = ISN_LOADWDICT; break;
2937 case 't': isn_type = ISN_LOADTDICT; break;
2938 case 'b': isn_type = ISN_LOADBDICT; break;
2939 default:
2940 semsg(_(e_namespace), *arg);
2941 goto theend;
2942 }
2943 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2944 goto theend;
2945 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002946 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 else
2948 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002949 isntype_T isn_type = ISN_DROP;
2950
2951 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2952 if (name == NULL)
2953 return FAIL;
2954
2955 switch (**arg)
2956 {
2957 case 'v': res = generate_LOADV(cctx, name, error);
2958 break;
2959 case 's': res = compile_load_scriptvar(cctx, name,
2960 NULL, NULL, error);
2961 break;
2962 case 'g': isn_type = ISN_LOADG; break;
2963 case 'w': isn_type = ISN_LOADW; break;
2964 case 't': isn_type = ISN_LOADT; break;
2965 case 'b': isn_type = ISN_LOADB; break;
2966 default: semsg(_(e_namespace), *arg);
2967 goto theend;
2968 }
2969 if (isn_type != ISN_DROP)
2970 {
2971 // Global, Buffer-local, Window-local and Tabpage-local
2972 // variables can be defined later, thus we don't check if it
2973 // exists, give error at runtime.
2974 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2975 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 }
2977 }
2978 else
2979 {
2980 size_t len = end - *arg;
2981 int idx;
2982 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002983 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984
2985 name = vim_strnsave(*arg, end - *arg);
2986 if (name == NULL)
2987 return FAIL;
2988
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002989 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002991 if (!gen_load_outer)
2992 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 }
2994 else
2995 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002996 lvar_T *lvar = lookup_local(*arg, len, cctx);
2997
2998 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003000 type = lvar->lv_type;
3001 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003002 if (lvar->lv_from_outer)
3003 gen_load_outer = TRUE;
3004 else
3005 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 }
3007 else
3008 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003009 // "var" can be script-local even without using "s:" if it
3010 // already exists.
3011 if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
3012 == SCRIPT_VERSION_VIM9
3013 || lookup_script(*arg, len) == OK)
3014 res = compile_load_scriptvar(cctx, name, *arg, &end,
3015 FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003016
Bram Moolenaara5565e42020-05-09 15:44:01 +02003017 // When the name starts with an uppercase letter or "x:" it
3018 // can be a user defined function.
3019 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
3020 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 }
3022 }
3023 if (gen_load)
3024 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003025 if (gen_load_outer)
3026 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 }
3028
3029 *arg = end;
3030
3031theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003032 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 semsg(_(e_var_notfound), name);
3034 vim_free(name);
3035 return res;
3036}
3037
3038/*
3039 * Compile the argument expressions.
3040 * "arg" points to just after the "(" and is advanced to after the ")"
3041 */
3042 static int
3043compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
3044{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003045 char_u *p = *arg;
3046 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047
Bram Moolenaare6085c52020-04-12 20:19:16 +02003048 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003050 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3051 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003052 if (*p == ')')
3053 {
3054 *arg = p + 1;
3055 return OK;
3056 }
3057
Bram Moolenaara5565e42020-05-09 15:44:01 +02003058 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 return FAIL;
3060 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003061
3062 if (*p != ',' && *skipwhite(p) == ',')
3063 {
Bram Moolenaard77a8522020-04-03 21:59:57 +02003064 semsg(_(e_no_white_before), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003065 p = skipwhite(p);
3066 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003068 {
3069 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003070 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaard77a8522020-04-03 21:59:57 +02003071 semsg(_(e_white_after), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003072 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003073 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003074 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003076failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003077 emsg(_(e_missing_close));
3078 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079}
3080
3081/*
3082 * Compile a function call: name(arg1, arg2)
3083 * "arg" points to "name", "arg + varlen" to the "(".
3084 * "argcount_init" is 1 for "value->method()"
3085 * Instructions:
3086 * EVAL arg1
3087 * EVAL arg2
3088 * BCALL / DCALL / UCALL
3089 */
3090 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003091compile_call(
3092 char_u **arg,
3093 size_t varlen,
3094 cctx_T *cctx,
3095 ppconst_T *ppconst,
3096 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097{
3098 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003099 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 int argcount = argcount_init;
3101 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003102 char_u fname_buf[FLEN_FIXED + 1];
3103 char_u *tofree = NULL;
3104 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003106 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107
Bram Moolenaara5565e42020-05-09 15:44:01 +02003108 // we can evaluate "has('name')" at compile time
3109 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3110 {
3111 char_u *s = skipwhite(*arg + varlen + 1);
3112 typval_T argvars[2];
3113
3114 argvars[0].v_type = VAR_UNKNOWN;
3115 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003116 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003117 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003118 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003119 s = skipwhite(s);
3120 if (*s == ')' && argvars[0].v_type == VAR_STRING)
3121 {
3122 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3123
3124 *arg = s + 1;
3125 argvars[1].v_type = VAR_UNKNOWN;
3126 tv->v_type = VAR_NUMBER;
3127 tv->vval.v_number = 0;
3128 f_has(argvars, tv);
3129 clear_tv(&argvars[0]);
3130 ++ppconst->pp_used;
3131 return OK;
3132 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003133 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003134 }
3135
3136 if (generate_ppconst(cctx, ppconst) == FAIL)
3137 return FAIL;
3138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 if (varlen >= sizeof(namebuf))
3140 {
3141 semsg(_("E1011: name too long: %s"), name);
3142 return FAIL;
3143 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003144 vim_strncpy(namebuf, *arg, varlen);
3145 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146
3147 *arg = skipwhite(*arg + varlen + 1);
3148 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003149 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003151 if (ASCII_ISLOWER(*name) && name[1] != ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 {
3153 int idx;
3154
3155 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003156 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02003158 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003159 else
3160 semsg(_(e_unknownfunc), namebuf);
3161 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 }
3163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003165 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003167 {
3168 res = generate_CALL(cctx, ufunc, argcount);
3169 goto theend;
3170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171
3172 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003173 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 p = namebuf;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003175 if (STRNCMP(namebuf, "g:", 2) != 0
3176 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003177 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003178 garray_T *stack = &cctx->ctx_type_stack;
3179 type_T *type;
3180
3181 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3182 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003183 goto theend;
3184 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003186 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003187 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003188 if (STRNCMP(namebuf, "g:", 2) == 0)
3189 res = generate_UCALL(cctx, name, argcount);
3190 else
3191 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003192
3193theend:
3194 vim_free(tofree);
3195 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196}
3197
3198// like NAMESPACE_CHAR but with 'a' and 'l'.
3199#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3200
3201/*
3202 * Find the end of a variable or function name. Unlike find_name_end() this
3203 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003204 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 * Return a pointer to just after the name. Equal to "arg" if there is no
3206 * valid name.
3207 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003208 static char_u *
3209to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210{
3211 char_u *p;
3212
3213 // Quick check for valid starting character.
3214 if (!eval_isnamec1(*arg))
3215 return arg;
3216
3217 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3218 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3219 // and can be used in slice "[n:]".
3220 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003221 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3223 break;
3224 return p;
3225}
3226
3227/*
3228 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003229 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 */
3231 char_u *
3232to_name_const_end(char_u *arg)
3233{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003234 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 typval_T rettv;
3236
3237 if (p == arg && *arg == '[')
3238 {
3239
3240 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003241 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 p = arg;
3243 }
3244 else if (p == arg && *arg == '#' && arg[1] == '{')
3245 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003246 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003248 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 p = arg;
3250 }
3251 else if (p == arg && *arg == '{')
3252 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003253 int ret = get_lambda_tv(&p, &rettv, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01003255 // Can be "{x -> ret}()".
3256 // Can be "{'a': 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003258 ret = eval_dict(&p, &rettv, NULL, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 if (ret != OK)
3260 p = arg;
3261 }
3262
3263 return p;
3264}
3265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266/*
3267 * parse a list: [expr, expr]
3268 * "*arg" points to the '['.
3269 */
3270 static int
3271compile_list(char_u **arg, cctx_T *cctx)
3272{
3273 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003274 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 int count = 0;
3276
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003277 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003279 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003280 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003281 semsg(_(e_list_end), *arg);
3282 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003283 }
3284 if (*p == ']')
3285 {
3286 ++p;
3287 // Allow for following comment, after at least one space.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003288 if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003289 p += STRLEN(p);
3290 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003291 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003292 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 break;
3294 ++count;
3295 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003296 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003298 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3299 {
3300 semsg(_(e_white_after), ",");
3301 return FAIL;
3302 }
3303 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003304 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 p = skipwhite(p);
3306 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003307 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308
3309 generate_NEWLIST(cctx, count);
3310 return OK;
3311}
3312
3313/*
3314 * parse a lambda: {arg, arg -> expr}
3315 * "*arg" points to the '{'.
3316 */
3317 static int
3318compile_lambda(char_u **arg, cctx_T *cctx)
3319{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 typval_T rettv;
3321 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003322 evalarg_T evalarg;
3323
3324 CLEAR_FIELD(evalarg);
3325 evalarg.eval_flags = EVAL_EVALUATE;
3326 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327
3328 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003329 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003333 ++ufunc->uf_refcount;
3334 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003335 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336
3337 // The function will have one line: "return {expr}".
3338 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003339 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003341 clear_evalarg(&evalarg, NULL);
3342
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003343 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003344 {
3345 // The return type will now be known.
3346 set_function_type(ufunc);
3347
3348 return generate_FUNCREF(cctx, ufunc);
3349 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003350
3351 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 return FAIL;
3353}
3354
3355/*
3356 * Compile a lamda call: expr->{lambda}(args)
3357 * "arg" points to the "{".
3358 */
3359 static int
3360compile_lambda_call(char_u **arg, cctx_T *cctx)
3361{
3362 ufunc_T *ufunc;
3363 typval_T rettv;
3364 int argcount = 1;
3365 int ret = FAIL;
3366
3367 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003368 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369 return FAIL;
3370
3371 if (**arg != '(')
3372 {
3373 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003374 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 else
3376 semsg(_(e_missing_paren), "lambda");
3377 clear_tv(&rettv);
3378 return FAIL;
3379 }
3380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 ufunc = rettv.vval.v_partial->pt_func;
3382 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003383 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003384 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003385
3386 // The function will have one line: "return {expr}".
3387 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003388 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389
3390 // compile the arguments
3391 *arg = skipwhite(*arg + 1);
3392 if (compile_arguments(arg, cctx, &argcount) == OK)
3393 // call the compiled function
3394 ret = generate_CALL(cctx, ufunc, argcount);
3395
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003396 if (ret == FAIL)
3397 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 return ret;
3399}
3400
3401/*
3402 * parse a dict: {'key': val} or #{key: val}
3403 * "*arg" points to the '{'.
3404 */
3405 static int
3406compile_dict(char_u **arg, cctx_T *cctx, int literal)
3407{
3408 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003409 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 int count = 0;
3411 dict_T *d = dict_alloc();
3412 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003413 char_u *whitep = *arg;
3414 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415
3416 if (d == NULL)
3417 return FAIL;
3418 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003419 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 {
3421 char_u *key = NULL;
3422
Bram Moolenaar23c55272020-06-21 16:58:13 +02003423 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003424 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003425 *arg = NULL;
3426 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003427 }
3428
3429 if (**arg == '}')
3430 break;
3431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 if (literal)
3433 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02003434 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435
Bram Moolenaar2c330432020-04-13 14:41:35 +02003436 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 {
3438 semsg(_("E1014: Invalid key: %s"), *arg);
3439 return FAIL;
3440 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003441 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 if (generate_PUSHS(cctx, key) == FAIL)
3443 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003444 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 }
3446 else
3447 {
3448 isn_T *isn;
3449
Bram Moolenaara5565e42020-05-09 15:44:01 +02003450 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3453 if (isn->isn_type == ISN_PUSHS)
3454 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003455 else
3456 {
3457 type_T *keytype = ((type_T **)stack->ga_data)
3458 [stack->ga_len - 1];
3459 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
3460 return FAIL;
3461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 }
3463
3464 // Check for duplicate keys, if using string keys.
3465 if (key != NULL)
3466 {
3467 item = dict_find(d, key, -1);
3468 if (item != NULL)
3469 {
3470 semsg(_(e_duplicate_key), key);
3471 goto failret;
3472 }
3473 item = dictitem_alloc(key);
3474 if (item != NULL)
3475 {
3476 item->di_tv.v_type = VAR_UNKNOWN;
3477 item->di_tv.v_lock = 0;
3478 if (dict_add(d, item) == FAIL)
3479 dictitem_free(item);
3480 }
3481 }
3482
3483 *arg = skipwhite(*arg);
3484 if (**arg != ':')
3485 {
3486 semsg(_(e_missing_dict_colon), *arg);
3487 return FAIL;
3488 }
3489
Bram Moolenaar2c330432020-04-13 14:41:35 +02003490 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003492 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003493 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003494 *arg = NULL;
3495 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003496 }
3497
Bram Moolenaara5565e42020-05-09 15:44:01 +02003498 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 return FAIL;
3500 ++count;
3501
Bram Moolenaar2c330432020-04-13 14:41:35 +02003502 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003503 *arg = skipwhite(*arg);
3504 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003505 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003506 *arg = NULL;
3507 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 if (**arg == '}')
3510 break;
3511 if (**arg != ',')
3512 {
3513 semsg(_(e_missing_dict_comma), *arg);
3514 goto failret;
3515 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003516 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 *arg = skipwhite(*arg + 1);
3518 }
3519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 *arg = *arg + 1;
3521
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003522 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003523 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003524 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003525 *arg += STRLEN(*arg);
3526
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 dict_unref(d);
3528 return generate_NEWDICT(cctx, count);
3529
3530failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003531 if (*arg == NULL)
3532 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 dict_unref(d);
3534 return FAIL;
3535}
3536
3537/*
3538 * Compile "&option".
3539 */
3540 static int
3541compile_get_option(char_u **arg, cctx_T *cctx)
3542{
3543 typval_T rettv;
3544 char_u *start = *arg;
3545 int ret;
3546
3547 // parse the option and get the current value to get the type.
3548 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003549 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 if (ret == OK)
3551 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003552 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 char_u *name = vim_strnsave(start, *arg - start);
3554 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3555
3556 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3557 vim_free(name);
3558 }
3559 clear_tv(&rettv);
3560
3561 return ret;
3562}
3563
3564/*
3565 * Compile "$VAR".
3566 */
3567 static int
3568compile_get_env(char_u **arg, cctx_T *cctx)
3569{
3570 char_u *start = *arg;
3571 int len;
3572 int ret;
3573 char_u *name;
3574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 ++*arg;
3576 len = get_env_len(arg);
3577 if (len == 0)
3578 {
3579 semsg(_(e_syntax_at), start - 1);
3580 return FAIL;
3581 }
3582
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003583 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 name = vim_strnsave(start, len + 1);
3585 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3586 vim_free(name);
3587 return ret;
3588}
3589
3590/*
3591 * Compile "@r".
3592 */
3593 static int
3594compile_get_register(char_u **arg, cctx_T *cctx)
3595{
3596 int ret;
3597
3598 ++*arg;
3599 if (**arg == NUL)
3600 {
3601 semsg(_(e_syntax_at), *arg - 1);
3602 return FAIL;
3603 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003604 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 {
3606 emsg_invreg(**arg);
3607 return FAIL;
3608 }
3609 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3610 ++*arg;
3611 return ret;
3612}
3613
3614/*
3615 * Apply leading '!', '-' and '+' to constant "rettv".
3616 */
3617 static int
3618apply_leader(typval_T *rettv, char_u *start, char_u *end)
3619{
3620 char_u *p = end;
3621
3622 // this works from end to start
3623 while (p > start)
3624 {
3625 --p;
3626 if (*p == '-' || *p == '+')
3627 {
3628 // only '-' has an effect, for '+' we only check the type
3629#ifdef FEAT_FLOAT
3630 if (rettv->v_type == VAR_FLOAT)
3631 {
3632 if (*p == '-')
3633 rettv->vval.v_float = -rettv->vval.v_float;
3634 }
3635 else
3636#endif
3637 {
3638 varnumber_T val;
3639 int error = FALSE;
3640
3641 // tv_get_number_chk() accepts a string, but we don't want that
3642 // here
3643 if (check_not_string(rettv) == FAIL)
3644 return FAIL;
3645 val = tv_get_number_chk(rettv, &error);
3646 clear_tv(rettv);
3647 if (error)
3648 return FAIL;
3649 if (*p == '-')
3650 val = -val;
3651 rettv->v_type = VAR_NUMBER;
3652 rettv->vval.v_number = val;
3653 }
3654 }
3655 else
3656 {
3657 int v = tv2bool(rettv);
3658
3659 // '!' is permissive in the type.
3660 clear_tv(rettv);
3661 rettv->v_type = VAR_BOOL;
3662 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3663 }
3664 }
3665 return OK;
3666}
3667
3668/*
3669 * Recognize v: variables that are constants and set "rettv".
3670 */
3671 static void
3672get_vim_constant(char_u **arg, typval_T *rettv)
3673{
3674 if (STRNCMP(*arg, "v:true", 6) == 0)
3675 {
3676 rettv->v_type = VAR_BOOL;
3677 rettv->vval.v_number = VVAL_TRUE;
3678 *arg += 6;
3679 }
3680 else if (STRNCMP(*arg, "v:false", 7) == 0)
3681 {
3682 rettv->v_type = VAR_BOOL;
3683 rettv->vval.v_number = VVAL_FALSE;
3684 *arg += 7;
3685 }
3686 else if (STRNCMP(*arg, "v:null", 6) == 0)
3687 {
3688 rettv->v_type = VAR_SPECIAL;
3689 rettv->vval.v_number = VVAL_NULL;
3690 *arg += 6;
3691 }
3692 else if (STRNCMP(*arg, "v:none", 6) == 0)
3693 {
3694 rettv->v_type = VAR_SPECIAL;
3695 rettv->vval.v_number = VVAL_NONE;
3696 *arg += 6;
3697 }
3698}
3699
Bram Moolenaar696ba232020-07-29 21:20:41 +02003700 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003701get_compare_type(char_u *p, int *len, int *type_is)
3702{
3703 exptype_T type = EXPR_UNKNOWN;
3704 int i;
3705
3706 switch (p[0])
3707 {
3708 case '=': if (p[1] == '=')
3709 type = EXPR_EQUAL;
3710 else if (p[1] == '~')
3711 type = EXPR_MATCH;
3712 break;
3713 case '!': if (p[1] == '=')
3714 type = EXPR_NEQUAL;
3715 else if (p[1] == '~')
3716 type = EXPR_NOMATCH;
3717 break;
3718 case '>': if (p[1] != '=')
3719 {
3720 type = EXPR_GREATER;
3721 *len = 1;
3722 }
3723 else
3724 type = EXPR_GEQUAL;
3725 break;
3726 case '<': if (p[1] != '=')
3727 {
3728 type = EXPR_SMALLER;
3729 *len = 1;
3730 }
3731 else
3732 type = EXPR_SEQUAL;
3733 break;
3734 case 'i': if (p[1] == 's')
3735 {
3736 // "is" and "isnot"; but not a prefix of a name
3737 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3738 *len = 5;
3739 i = p[*len];
3740 if (!isalnum(i) && i != '_')
3741 {
3742 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3743 *type_is = TRUE;
3744 }
3745 }
3746 break;
3747 }
3748 return type;
3749}
3750
3751/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752 * Compile code to apply '-', '+' and '!'.
3753 */
3754 static int
3755compile_leader(cctx_T *cctx, char_u *start, char_u *end)
3756{
3757 char_u *p = end;
3758
3759 // this works from end to start
3760 while (p > start)
3761 {
3762 --p;
3763 if (*p == '-' || *p == '+')
3764 {
3765 int negate = *p == '-';
3766 isn_T *isn;
3767
3768 // TODO: check type
3769 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3770 {
3771 --p;
3772 if (*p == '-')
3773 negate = !negate;
3774 }
3775 // only '-' has an effect, for '+' we only check the type
3776 if (negate)
3777 isn = generate_instr(cctx, ISN_NEGATENR);
3778 else
3779 isn = generate_instr(cctx, ISN_CHECKNR);
3780 if (isn == NULL)
3781 return FAIL;
3782 }
3783 else
3784 {
3785 int invert = TRUE;
3786
3787 while (p > start && p[-1] == '!')
3788 {
3789 --p;
3790 invert = !invert;
3791 }
3792 if (generate_2BOOL(cctx, invert) == FAIL)
3793 return FAIL;
3794 }
3795 }
3796 return OK;
3797}
3798
3799/*
3800 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003801 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 */
3803 static int
3804compile_subscript(
3805 char_u **arg,
3806 cctx_T *cctx,
3807 char_u **start_leader,
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003808 char_u *end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003809 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810{
3811 for (;;)
3812 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003813 char_u *p = skipwhite(*arg);
3814
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003815 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003816 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003817 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003818
3819 // If a following line starts with "->{" or "->X" advance to that
3820 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003821 // Also if a following line starts with ".x".
3822 if (next != NULL &&
3823 ((next[0] == '-' && next[1] == '>'
3824 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003825 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003826 {
3827 next = next_line_from_context(cctx, TRUE);
3828 if (next == NULL)
3829 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003830 *arg = next;
3831 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003832 }
3833 }
3834
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003835 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3836 // not a function call.
3837 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003839 garray_T *stack = &cctx->ctx_type_stack;
3840 type_T *type;
3841 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003843 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003844 return FAIL;
3845
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003847 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3848
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003849 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3851 return FAIL;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003852 if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 return FAIL;
3854 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003855 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003857 char_u *pstart = p;
3858
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003859 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003860 return FAIL;
3861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 // something->method()
3863 // Apply the '!', '-' and '+' first:
3864 // -1.0->func() works like (-1.0)->func()
3865 if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
3866 return FAIL;
3867 *start_leader = end_leader; // don't apply again later
3868
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003869 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003870 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003871 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 if (**arg == '{')
3873 {
3874 // lambda call: list->{lambda}
3875 if (compile_lambda_call(arg, cctx) == FAIL)
3876 return FAIL;
3877 }
3878 else
3879 {
3880 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003881 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003882 if (!eval_isnamec1(*p))
3883 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003884 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003885 return FAIL;
3886 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003887 if (ASCII_ISALPHA(*p) && p[1] == ':')
3888 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003889 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 ;
3891 if (*p != '(')
3892 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003893 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 return FAIL;
3895 }
3896 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003897 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 return FAIL;
3899 }
3900 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003901 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003903 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003904 type_T **typep;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003905 vartype_T vtype;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003906
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003907 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003908 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003909 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003910 // TODO: blob index
3911 // TODO: more arguments
3912 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003913 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003914 return FAIL;
3915
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003916 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003917 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003918 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003919 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003920 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 return FAIL;
3922
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003923 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3924 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 if (**arg != ']')
3926 {
3927 emsg(_(e_missbrac));
3928 return FAIL;
3929 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003930 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003932 // We can index a list and a dict. If we don't know the type
3933 // we can use the index value type.
3934 // TODO: If we don't know use an instruction to figure it out at
3935 // runtime.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003936 typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003937 vtype = (*typep)->tt_type;
3938 if (*typep == &t_any)
3939 {
3940 type_T *valtype = ((type_T **)stack->ga_data)
3941 [stack->ga_len - 1];
3942 if (valtype == &t_string)
3943 vtype = VAR_DICT;
3944 }
3945 if (vtype == VAR_DICT)
3946 {
3947 if ((*typep)->tt_type == VAR_DICT)
3948 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003949 else
3950 {
3951 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3952 return FAIL;
3953 *typep = &t_any;
3954 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003955 if (may_generate_2STRING(-1, cctx) == FAIL)
3956 return FAIL;
3957 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3958 return FAIL;
3959 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003960 else if (vtype == VAR_STRING)
3961 {
3962 *typep = &t_number;
3963 if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL)
3964 return FAIL;
3965 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003966 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003967 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003968 if ((*typep)->tt_type == VAR_LIST)
3969 *typep = (*typep)->tt_member;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003970 if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003971 return FAIL;
3972 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003973 else
3974 {
3975 emsg(_(e_listdictblobreq));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003976 return FAIL;
3977 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003979 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003981 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003982 return FAIL;
3983
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003984 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003985 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3986 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003988 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003989 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 while (eval_isnamec(*p))
3991 MB_PTR_ADV(p);
3992 if (p == *arg)
3993 {
3994 semsg(_(e_syntax_at), *arg);
3995 return FAIL;
3996 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003997 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 return FAIL;
3999 *arg = p;
4000 }
4001 else
4002 break;
4003 }
4004
4005 // TODO - see handle_subscript():
4006 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4007 // Don't do this when "Func" is already a partial that was bound
4008 // explicitly (pt_auto is FALSE).
4009
4010 return OK;
4011}
4012
4013/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004014 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4015 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004017 * If the value is a constant "ppconst->pp_ret" will be set.
4018 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004019 *
4020 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 */
4022
4023/*
4024 * number number constant
4025 * 0zFFFFFFFF Blob constant
4026 * "string" string constant
4027 * 'string' literal string constant
4028 * &option-name option value
4029 * @r register contents
4030 * identifier variable value
4031 * function() function call
4032 * $VAR environment variable
4033 * (expression) nested expression
4034 * [expr, expr] List
4035 * {key: val, key: val} Dictionary
4036 * #{key: val, key: val} Dictionary with literal keys
4037 *
4038 * Also handle:
4039 * ! in front logical NOT
4040 * - in front unary minus
4041 * + in front unary plus (ignored)
4042 * trailing (arg) funcref/partial call
4043 * trailing [] subscript in String or List
4044 * trailing .name entry in Dictionary
4045 * trailing ->name() method call
4046 */
4047 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004048compile_expr7(
4049 char_u **arg,
4050 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004051 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 char_u *start_leader, *end_leader;
4054 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004055 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004056 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057
4058 /*
4059 * Skip '!', '-' and '+' characters. They are handled later.
4060 */
4061 start_leader = *arg;
4062 while (**arg == '!' || **arg == '-' || **arg == '+')
4063 *arg = skipwhite(*arg + 1);
4064 end_leader = *arg;
4065
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004066 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 switch (**arg)
4068 {
4069 /*
4070 * Number constant.
4071 */
4072 case '0': // also for blob starting with 0z
4073 case '1':
4074 case '2':
4075 case '3':
4076 case '4':
4077 case '5':
4078 case '6':
4079 case '7':
4080 case '8':
4081 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004082 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083 return FAIL;
4084 break;
4085
4086 /*
4087 * String constant: "string".
4088 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004089 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 return FAIL;
4091 break;
4092
4093 /*
4094 * Literal string constant: 'str''ing'.
4095 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004096 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 return FAIL;
4098 break;
4099
4100 /*
4101 * Constant Vim variable.
4102 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004103 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 ret = NOTDONE;
4105 break;
4106
4107 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004108 * "true" constant
4109 */
4110 case 't': if (STRNCMP(*arg, "true", 4) == 0
4111 && !eval_isnamec((*arg)[4]))
4112 {
4113 *arg += 4;
4114 rettv->v_type = VAR_BOOL;
4115 rettv->vval.v_number = VVAL_TRUE;
4116 }
4117 else
4118 ret = NOTDONE;
4119 break;
4120
4121 /*
4122 * "false" constant
4123 */
4124 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4125 && !eval_isnamec((*arg)[5]))
4126 {
4127 *arg += 5;
4128 rettv->v_type = VAR_BOOL;
4129 rettv->vval.v_number = VVAL_FALSE;
4130 }
4131 else
4132 ret = NOTDONE;
4133 break;
4134
4135 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 * List: [expr, expr]
4137 */
4138 case '[': ret = compile_list(arg, cctx);
4139 break;
4140
4141 /*
4142 * Dictionary: #{key: val, key: val}
4143 */
4144 case '#': if ((*arg)[1] == '{')
4145 {
4146 ++*arg;
4147 ret = compile_dict(arg, cctx, TRUE);
4148 }
4149 else
4150 ret = NOTDONE;
4151 break;
4152
4153 /*
4154 * Lambda: {arg, arg -> expr}
4155 * Dictionary: {'key': val, 'key': val}
4156 */
4157 case '{': {
4158 char_u *start = skipwhite(*arg + 1);
4159
4160 // Find out what comes after the arguments.
4161 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004162 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 if (ret != FAIL && *start == '>')
4164 ret = compile_lambda(arg, cctx);
4165 else
4166 ret = compile_dict(arg, cctx, FALSE);
4167 }
4168 break;
4169
4170 /*
4171 * Option value: &name
4172 */
4173 case '&': ret = compile_get_option(arg, cctx);
4174 break;
4175
4176 /*
4177 * Environment variable: $VAR.
4178 */
4179 case '$': ret = compile_get_env(arg, cctx);
4180 break;
4181
4182 /*
4183 * Register contents: @r.
4184 */
4185 case '@': ret = compile_get_register(arg, cctx);
4186 break;
4187 /*
4188 * nested expression: (expression).
4189 */
4190 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02004191
4192 // recursive!
4193 if (ppconst->pp_used <= PPSIZE - 10)
4194 {
4195 ret = compile_expr1(arg, cctx, ppconst);
4196 }
4197 else
4198 {
4199 // Not enough space in ppconst, flush constants.
4200 if (generate_ppconst(cctx, ppconst) == FAIL)
4201 return FAIL;
4202 ret = compile_expr0(arg, cctx);
4203 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 *arg = skipwhite(*arg);
4205 if (**arg == ')')
4206 ++*arg;
4207 else if (ret == OK)
4208 {
4209 emsg(_(e_missing_close));
4210 ret = FAIL;
4211 }
4212 break;
4213
4214 default: ret = NOTDONE;
4215 break;
4216 }
4217 if (ret == FAIL)
4218 return FAIL;
4219
Bram Moolenaar1c747212020-05-09 18:28:34 +02004220 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221 {
4222 // apply the '!', '-' and '+' before the constant
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004223 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004225 clear_tv(rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 return FAIL;
4227 }
4228 start_leader = end_leader; // don't apply again below
4229
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004230 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004231 clear_tv(rettv);
4232 else
4233 // A constant expression can possibly be handled compile time,
4234 // return the value instead of generating code.
4235 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 }
4237 else if (ret == NOTDONE)
4238 {
4239 char_u *p;
4240 int r;
4241
4242 if (!eval_isnamec1(**arg))
4243 {
4244 semsg(_("E1015: Name expected: %s"), *arg);
4245 return FAIL;
4246 }
4247
4248 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004249 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004251 {
4252 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004255 {
4256 if (generate_ppconst(cctx, ppconst) == FAIL)
4257 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004259 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 if (r == FAIL)
4261 return FAIL;
4262 }
4263
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004264 // Handle following "[]", ".member", etc.
4265 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004266 if (compile_subscript(arg, cctx, &start_leader, end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004267 ppconst) == FAIL)
4268 return FAIL;
4269 if (ppconst->pp_used > 0)
4270 {
4271 // apply the '!', '-' and '+' before the constant
4272 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
4273 if (apply_leader(rettv, start_leader, end_leader) == FAIL)
4274 return FAIL;
4275 return OK;
4276 }
4277 if (compile_leader(cctx, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004279 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280}
4281
4282/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004283 * Give the "white on both sides" error, taking the operator from "p[len]".
4284 */
4285 void
4286error_white_both(char_u *op, int len)
4287{
4288 char_u buf[10];
4289
4290 vim_strncpy(buf, op, len);
4291 semsg(_(e_white_both), buf);
4292}
4293
4294/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 * * number multiplication
4296 * / number division
4297 * % number modulo
4298 */
4299 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004300compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301{
4302 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004303 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004304 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004306 // get the first expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004307 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 return FAIL;
4309
4310 /*
4311 * Repeat computing, until no "*", "/" or "%" is following.
4312 */
4313 for (;;)
4314 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004315 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 if (*op != '*' && *op != '/' && *op != '%')
4317 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004318 if (next != NULL)
4319 {
4320 *arg = next_line_from_context(cctx, TRUE);
4321 op = skipwhite(*arg);
4322 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004323
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004324 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004326 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004327 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 }
4329 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004330 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004331 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004333 // get the second expression
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004334 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004336
4337 if (ppconst->pp_used == ppconst_used + 2
4338 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4339 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004340 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004341 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4342 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004343 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004345 // both are numbers: compute the result
4346 switch (*op)
4347 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004348 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004349 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004350 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004351 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004352 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004353 break;
4354 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004355 tv1->vval.v_number = res;
4356 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004357 }
4358 else
4359 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004360 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004361 generate_two_op(cctx, op);
4362 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 }
4364
4365 return OK;
4366}
4367
4368/*
4369 * + number addition
4370 * - number subtraction
4371 * .. string concatenation
4372 */
4373 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004374compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375{
4376 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004377 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004379 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380
4381 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004382 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 return FAIL;
4384
4385 /*
4386 * Repeat computing, until no "+", "-" or ".." is following.
4387 */
4388 for (;;)
4389 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004390 op = may_peek_next_line(cctx, *arg, &next);
4391 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 break;
4393 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004394 if (next != NULL)
4395 {
4396 *arg = next_line_from_context(cctx, TRUE);
4397 op = skipwhite(*arg);
4398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004400 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004402 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004403 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 }
4405
4406 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004407 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004408 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004410 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004411 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 return FAIL;
4413
Bram Moolenaara5565e42020-05-09 15:44:01 +02004414 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004415 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004416 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4417 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4418 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4419 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004421 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4422 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004423
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004424 // concat/subtract/add constant numbers
4425 if (*op == '+')
4426 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4427 else if (*op == '-')
4428 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4429 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004430 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004431 // concatenate constant strings
4432 char_u *s1 = tv1->vval.v_string;
4433 char_u *s2 = tv2->vval.v_string;
4434 size_t len1 = STRLEN(s1);
4435
4436 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4437 if (tv1->vval.v_string == NULL)
4438 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004439 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004440 return FAIL;
4441 }
4442 mch_memmove(tv1->vval.v_string, s1, len1);
4443 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004444 vim_free(s1);
4445 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004446 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004447 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 }
4449 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004450 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004451 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004452 if (*op == '.')
4453 {
4454 if (may_generate_2STRING(-2, cctx) == FAIL
4455 || may_generate_2STRING(-1, cctx) == FAIL)
4456 return FAIL;
4457 generate_instr_drop(cctx, ISN_CONCAT, 1);
4458 }
4459 else
4460 generate_two_op(cctx, op);
4461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 }
4463
4464 return OK;
4465}
4466
4467/*
4468 * expr5a == expr5b
4469 * expr5a =~ expr5b
4470 * expr5a != expr5b
4471 * expr5a !~ expr5b
4472 * expr5a > expr5b
4473 * expr5a >= expr5b
4474 * expr5a < expr5b
4475 * expr5a <= expr5b
4476 * expr5a is expr5b
4477 * expr5a isnot expr5b
4478 *
4479 * Produces instructions:
4480 * EVAL expr5a Push result of "expr5a"
4481 * EVAL expr5b Push result of "expr5b"
4482 * COMPARE one of the compare instructions
4483 */
4484 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004485compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486{
4487 exptype_T type = EXPR_UNKNOWN;
4488 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004489 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004492 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493
4494 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004495 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 return FAIL;
4497
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004498 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004499 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500
4501 /*
4502 * If there is a comparative operator, use it.
4503 */
4504 if (type != EXPR_UNKNOWN)
4505 {
4506 int ic = FALSE; // Default: do not ignore case
4507
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004508 if (next != NULL)
4509 {
4510 *arg = next_line_from_context(cctx, TRUE);
4511 p = skipwhite(*arg);
4512 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513 if (type_is && (p[len] == '?' || p[len] == '#'))
4514 {
4515 semsg(_(e_invexpr2), *arg);
4516 return FAIL;
4517 }
4518 // extra question mark appended: ignore case
4519 if (p[len] == '?')
4520 {
4521 ic = TRUE;
4522 ++len;
4523 }
4524 // extra '#' appended: match case (ignored)
4525 else if (p[len] == '#')
4526 ++len;
4527 // nothing appended: match case
4528
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004529 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004531 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004532 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 }
4534
4535 // get the second variable
4536 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004537 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004538 return FAIL;
4539
Bram Moolenaara5565e42020-05-09 15:44:01 +02004540 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 return FAIL;
4542
Bram Moolenaara5565e42020-05-09 15:44:01 +02004543 if (ppconst->pp_used == ppconst_used + 2)
4544 {
4545 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4546 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4547 int ret;
4548
4549 // Both sides are a constant, compute the result now.
4550 // First check for a valid combination of types, this is more
4551 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004552 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004553 ret = FAIL;
4554 else
4555 {
4556 ret = typval_compare(tv1, tv2, type, ic);
4557 tv1->v_type = VAR_BOOL;
4558 tv1->vval.v_number = tv1->vval.v_number
4559 ? VVAL_TRUE : VVAL_FALSE;
4560 clear_tv(tv2);
4561 --ppconst->pp_used;
4562 }
4563 return ret;
4564 }
4565
4566 generate_ppconst(cctx, ppconst);
4567 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 }
4569
4570 return OK;
4571}
4572
Bram Moolenaar7f141552020-05-09 17:35:53 +02004573static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575/*
4576 * Compile || or &&.
4577 */
4578 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004579compile_and_or(
4580 char_u **arg,
4581 cctx_T *cctx,
4582 char *op,
4583 ppconst_T *ppconst,
4584 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004586 char_u *next;
4587 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 int opchar = *op;
4589
4590 if (p[0] == opchar && p[1] == opchar)
4591 {
4592 garray_T *instr = &cctx->ctx_instr;
4593 garray_T end_ga;
4594
4595 /*
4596 * Repeat until there is no following "||" or "&&"
4597 */
4598 ga_init2(&end_ga, sizeof(int), 10);
4599 while (p[0] == opchar && p[1] == opchar)
4600 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004601 if (next != NULL)
4602 {
4603 *arg = next_line_from_context(cctx, TRUE);
4604 p = skipwhite(*arg);
4605 }
4606
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004607 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4608 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609 semsg(_(e_white_both), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004610 return FAIL;
4611 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612
Bram Moolenaara5565e42020-05-09 15:44:01 +02004613 // TODO: use ppconst if the value is a constant
4614 generate_ppconst(cctx, ppconst);
4615
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 if (ga_grow(&end_ga, 1) == FAIL)
4617 {
4618 ga_clear(&end_ga);
4619 return FAIL;
4620 }
4621 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4622 ++end_ga.ga_len;
4623 generate_JUMP(cctx, opchar == '|'
4624 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4625
4626 // eval the next expression
4627 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004628 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004629 return FAIL;
4630
Bram Moolenaara5565e42020-05-09 15:44:01 +02004631 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4632 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 {
4634 ga_clear(&end_ga);
4635 return FAIL;
4636 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004637
4638 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004640 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641
4642 // Fill in the end label in all jumps.
4643 while (end_ga.ga_len > 0)
4644 {
4645 isn_T *isn;
4646
4647 --end_ga.ga_len;
4648 isn = ((isn_T *)instr->ga_data)
4649 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4650 isn->isn_arg.jump.jump_where = instr->ga_len;
4651 }
4652 ga_clear(&end_ga);
4653 }
4654
4655 return OK;
4656}
4657
4658/*
4659 * expr4a && expr4a && expr4a logical AND
4660 *
4661 * Produces instructions:
4662 * EVAL expr4a Push result of "expr4a"
4663 * JUMP_AND_KEEP_IF_FALSE end
4664 * EVAL expr4b Push result of "expr4b"
4665 * JUMP_AND_KEEP_IF_FALSE end
4666 * EVAL expr4c Push result of "expr4c"
4667 * end:
4668 */
4669 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004670compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004672 int ppconst_used = ppconst->pp_used;
4673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004675 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 return FAIL;
4677
4678 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004679 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680}
4681
4682/*
4683 * expr3a || expr3b || expr3c logical OR
4684 *
4685 * Produces instructions:
4686 * EVAL expr3a Push result of "expr3a"
4687 * JUMP_AND_KEEP_IF_TRUE end
4688 * EVAL expr3b Push result of "expr3b"
4689 * JUMP_AND_KEEP_IF_TRUE end
4690 * EVAL expr3c Push result of "expr3c"
4691 * end:
4692 */
4693 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004694compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004696 int ppconst_used = ppconst->pp_used;
4697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004699 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700 return FAIL;
4701
4702 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004703 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704}
4705
4706/*
4707 * Toplevel expression: expr2 ? expr1a : expr1b
4708 *
4709 * Produces instructions:
4710 * EVAL expr2 Push result of "expr"
4711 * JUMP_IF_FALSE alt jump if false
4712 * EVAL expr1a
4713 * JUMP_ALWAYS end
4714 * alt: EVAL expr1b
4715 * end:
4716 */
4717 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004718compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004719{
4720 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004721 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004722 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723
Bram Moolenaar61a89812020-05-07 16:58:17 +02004724 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004725 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 return FAIL;
4727
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004728 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 if (*p == '?')
4730 {
4731 garray_T *instr = &cctx->ctx_instr;
4732 garray_T *stack = &cctx->ctx_type_stack;
4733 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004734 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004736 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004738 int has_const_expr = FALSE;
4739 int const_value = FALSE;
4740 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004741
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004742 if (next != NULL)
4743 {
4744 *arg = next_line_from_context(cctx, TRUE);
4745 p = skipwhite(*arg);
4746 }
4747
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004748 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4749 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004750 semsg(_(e_white_both), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004751 return FAIL;
4752 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753
Bram Moolenaara5565e42020-05-09 15:44:01 +02004754 if (ppconst->pp_used == ppconst_used + 1)
4755 {
4756 // the condition is a constant, we know whether the ? or the :
4757 // expression is to be evaluated.
4758 has_const_expr = TRUE;
4759 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4760 clear_tv(&ppconst->pp_tv[ppconst_used]);
4761 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004762 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4763 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004764 }
4765 else
4766 {
4767 generate_ppconst(cctx, ppconst);
4768 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4769 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770
4771 // evaluate the second expression; any type is accepted
4772 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004773 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004774 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004775 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004776 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777
Bram Moolenaara5565e42020-05-09 15:44:01 +02004778 if (!has_const_expr)
4779 {
4780 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781
Bram Moolenaara5565e42020-05-09 15:44:01 +02004782 // remember the type and drop it
4783 --stack->ga_len;
4784 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785
Bram Moolenaara5565e42020-05-09 15:44:01 +02004786 end_idx = instr->ga_len;
4787 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4788
4789 // jump here from JUMP_IF_FALSE
4790 isn = ((isn_T *)instr->ga_data) + alt_idx;
4791 isn->isn_arg.jump.jump_where = instr->ga_len;
4792 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793
4794 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004795 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796 if (*p != ':')
4797 {
4798 emsg(_(e_missing_colon));
4799 return FAIL;
4800 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004801 if (next != NULL)
4802 {
4803 *arg = next_line_from_context(cctx, TRUE);
4804 p = skipwhite(*arg);
4805 }
4806
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004807 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4808 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 semsg(_(e_white_both), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004810 return FAIL;
4811 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812
4813 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004814 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004815 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4816 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004817 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004818 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004819 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004820 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004821 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822
Bram Moolenaara5565e42020-05-09 15:44:01 +02004823 if (!has_const_expr)
4824 {
4825 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004826
Bram Moolenaara5565e42020-05-09 15:44:01 +02004827 // If the types differ, the result has a more generic type.
4828 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4829 common_type(type1, type2, &type2, cctx->ctx_type_list);
4830
4831 // jump here from JUMP_ALWAYS
4832 isn = ((isn_T *)instr->ga_data) + end_idx;
4833 isn->isn_arg.jump.jump_where = instr->ga_len;
4834 }
4835
4836 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 }
4838 return OK;
4839}
4840
4841/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004842 * Toplevel expression.
4843 */
4844 static int
4845compile_expr0(char_u **arg, cctx_T *cctx)
4846{
4847 ppconst_T ppconst;
4848
4849 CLEAR_FIELD(ppconst);
4850 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4851 {
4852 clear_ppconst(&ppconst);
4853 return FAIL;
4854 }
4855 if (generate_ppconst(cctx, &ppconst) == FAIL)
4856 return FAIL;
4857 return OK;
4858}
4859
4860/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 * compile "return [expr]"
4862 */
4863 static char_u *
4864compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4865{
4866 char_u *p = arg;
4867 garray_T *stack = &cctx->ctx_type_stack;
4868 type_T *stack_type;
4869
4870 if (*p != NUL && *p != '|' && *p != '\n')
4871 {
4872 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004873 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 return NULL;
4875
4876 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4877 if (set_return_type)
4878 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004879 else
4880 {
4881 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4882 && stack_type->tt_type != VAR_VOID
4883 && stack_type->tt_type != VAR_UNKNOWN)
4884 {
4885 emsg(_("E1096: Returning a value in a function without a return type"));
4886 return NULL;
4887 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004888 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4889 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004890 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 }
4893 else
4894 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004895 // "set_return_type" cannot be TRUE, only used for a lambda which
4896 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004897 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4898 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899 {
4900 emsg(_("E1003: Missing return value"));
4901 return NULL;
4902 }
4903
4904 // No argument, return zero.
4905 generate_PUSHNR(cctx, 0);
4906 }
4907
4908 if (generate_instr(cctx, ISN_RETURN) == NULL)
4909 return NULL;
4910
4911 // "return val | endif" is possible
4912 return skipwhite(p);
4913}
4914
4915/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004916 * Get a line from the compilation context, compatible with exarg_T getline().
4917 * Return a pointer to the line in allocated memory.
4918 * Return NULL for end-of-file or some error.
4919 */
4920 static char_u *
4921exarg_getline(
4922 int c UNUSED,
4923 void *cookie,
4924 int indent UNUSED,
4925 int do_concat UNUSED)
4926{
4927 cctx_T *cctx = (cctx_T *)cookie;
4928
4929 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4930 {
4931 iemsg("Heredoc got to end");
4932 return NULL;
4933 }
4934 ++cctx->ctx_lnum;
4935 return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
4936 [cctx->ctx_lnum]);
4937}
4938
4939/*
4940 * Compile a nested :def command.
4941 */
4942 static char_u *
4943compile_nested_function(exarg_T *eap, cctx_T *cctx)
4944{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004945 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004946 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004947 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004948 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004949 lvar_T *lvar;
4950 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004951 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004952
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004953 // Only g:Func() can use a namespace.
4954 if (name_start[1] == ':' && !is_global)
4955 {
4956 semsg(_(e_namespace), name_start);
4957 return NULL;
4958 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004959 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4960 return NULL;
4961
Bram Moolenaar04b12692020-05-04 23:24:44 +02004962 eap->arg = name_end;
4963 eap->getline = exarg_getline;
4964 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004965 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004966 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004967 lambda_name = get_lambda_name();
4968 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004969
Bram Moolenaar822ba242020-05-24 23:00:18 +02004970 if (ufunc == NULL)
4971 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004972 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004973 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004974 return NULL;
4975
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004976 if (is_global)
4977 {
4978 char_u *func_name = vim_strnsave(name_start + 2,
4979 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004980
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004981 if (func_name == NULL)
4982 r = FAIL;
4983 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004984 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004985 }
4986 else
4987 {
4988 // Define a local variable for the function reference.
4989 lvar = reserve_local(cctx, name_start, name_end - name_start,
4990 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004991 if (lvar == NULL)
4992 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004993 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004994 return NULL;
4995 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4996 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004997
Bram Moolenaar61a89812020-05-07 16:58:17 +02004998 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004999 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005000}
5001
5002/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003 * Return the length of an assignment operator, or zero if there isn't one.
5004 */
5005 int
5006assignment_len(char_u *p, int *heredoc)
5007{
5008 if (*p == '=')
5009 {
5010 if (p[1] == '<' && p[2] == '<')
5011 {
5012 *heredoc = TRUE;
5013 return 3;
5014 }
5015 return 1;
5016 }
5017 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5018 return 2;
5019 if (STRNCMP(p, "..=", 3) == 0)
5020 return 3;
5021 return 0;
5022}
5023
5024// words that cannot be used as a variable
5025static char *reserved[] = {
5026 "true",
5027 "false",
5028 NULL
5029};
5030
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005031typedef enum {
5032 dest_local,
5033 dest_option,
5034 dest_env,
5035 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005036 dest_buffer,
5037 dest_window,
5038 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005039 dest_vimvar,
5040 dest_script,
5041 dest_reg,
5042} assign_dest_T;
5043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005045 * Generate the load instruction for "name".
5046 */
5047 static void
5048generate_loadvar(
5049 cctx_T *cctx,
5050 assign_dest_T dest,
5051 char_u *name,
5052 lvar_T *lvar,
5053 type_T *type)
5054{
5055 switch (dest)
5056 {
5057 case dest_option:
5058 // TODO: check the option exists
5059 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5060 break;
5061 case dest_global:
5062 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5063 break;
5064 case dest_buffer:
5065 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5066 break;
5067 case dest_window:
5068 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5069 break;
5070 case dest_tab:
5071 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5072 break;
5073 case dest_script:
5074 compile_load_scriptvar(cctx,
5075 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5076 break;
5077 case dest_env:
5078 // Include $ in the name here
5079 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5080 break;
5081 case dest_reg:
5082 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5083 break;
5084 case dest_vimvar:
5085 generate_LOADV(cctx, name + 2, TRUE);
5086 break;
5087 case dest_local:
5088 if (lvar->lv_from_outer)
5089 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5090 NULL, type);
5091 else
5092 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5093 break;
5094 }
5095}
5096
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005097 void
5098vim9_declare_error(char_u *name)
5099{
5100 char *scope = "";
5101
5102 switch (*name)
5103 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005104 case 'g': scope = _("global"); break;
5105 case 'b': scope = _("buffer"); break;
5106 case 'w': scope = _("window"); break;
5107 case 't': scope = _("tab"); break;
5108 case 'v': scope = "v:"; break;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005109 case '$': semsg(_(e_declare_env_var), name);
5110 return;
5111 case '&': semsg(_("E1052: Cannot declare an option: %s"), name);
5112 return;
5113 case '@': semsg(_("E1066: Cannot declare a register: %s"), name);
5114 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005115 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005116 }
5117 semsg(_(e_declare_var), scope, name);
5118}
5119
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005120/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005121 * Compile declaration and assignment:
5122 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005123 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005124 * Return NULL for an error.
5125 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126 */
5127 static char_u *
5128compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5129{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005130 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005132 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005133 char_u *ret = NULL;
5134 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005135 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005136 int scriptvar_sid = 0;
5137 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005140 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005141 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142 int oplen = 0;
5143 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005144 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005145 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005146 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005149
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005150 // Skip over the "var" or "[var, var]" to get to any "=".
5151 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5152 if (p == NULL)
5153 return *arg == '[' ? arg : NULL;
5154
5155 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005156 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005157 // TODO: should we allow this, and figure out type inference from list
5158 // members?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005159 emsg(_("E1092: Cannot use a list for a declaration"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005160 return NULL;
5161 }
5162
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005163 sp = p;
5164 p = skipwhite(p);
5165 op = p;
5166 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005167
5168 if (var_count > 0 && oplen == 0)
5169 // can be something like "[1, 2]->func()"
5170 return arg;
5171
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005172 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
5173 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005174 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005175 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005176 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005177
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178 if (heredoc)
5179 {
5180 list_T *l;
5181 listitem_T *li;
5182
5183 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005184 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005185 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005186 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005187
5188 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005189 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190 {
5191 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5192 li->li_tv.vval.v_string = NULL;
5193 }
5194 generate_NEWLIST(cctx, l->lv_len);
5195 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005196 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197 list_free(l);
5198 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005199 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005201 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005203 // for "[var, var] = expr" evaluate the expression here, loop over the
5204 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005205
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005206 p = skipwhite(op + oplen);
5207 if (compile_expr0(&p, cctx) == FAIL)
5208 return NULL;
5209 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005211 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005212 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005213 type_T *stacktype;
5214
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005215 stacktype = stack->ga_len == 0 ? &t_void
5216 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005217 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005219 emsg(_(e_cannot_use_void));
5220 goto theend;
5221 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005222 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005223 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005224 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005225 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5226 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005227 }
5228 }
5229
5230 /*
5231 * Loop over variables in "[var, var] = expr".
5232 * For "var = expr" and "let var: type" this is done only once.
5233 */
5234 if (var_count > 0)
5235 var_start = skipwhite(arg + 1); // skip over the "["
5236 else
5237 var_start = arg;
5238 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5239 {
5240 char_u *var_end = skip_var_one(var_start, FALSE);
5241 size_t varlen;
5242 int new_local = FALSE;
5243 int opt_type;
5244 int opt_flags = 0;
5245 assign_dest_T dest = dest_local;
5246 int vimvaridx = -1;
5247 lvar_T *lvar = NULL;
5248 lvar_T arg_lvar;
5249 int has_type = FALSE;
5250 int has_index = FALSE;
5251 int instr_count = -1;
5252
Bram Moolenaar65821722020-08-02 18:58:54 +02005253 if (*var_start == '@')
5254 p = var_start + 2;
5255 else
5256 {
5257 p = (*var_start == '&' || *var_start == '$')
5258 ? var_start + 1 : var_start;
5259 p = to_name_end(p, TRUE);
5260 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005261
5262 // "a: type" is declaring variable "a" with a type, not "a:".
5263 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5264 --var_end;
5265 if (is_decl && p == var_start + 2 && p[-1] == ':')
5266 --p;
5267
5268 varlen = p - var_start;
5269 vim_free(name);
5270 name = vim_strnsave(var_start, varlen);
5271 if (name == NULL)
5272 return NULL;
5273 if (!heredoc)
5274 type = &t_any;
5275
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005276 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005277 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005278 int declare_error = FALSE;
5279
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005280 if (*var_start == '&')
5281 {
5282 int cc;
5283 long numval;
5284
5285 dest = dest_option;
5286 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005287 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005288 emsg(_(e_const_option));
5289 goto theend;
5290 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005291 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005292 p = var_start;
5293 p = find_option_end(&p, &opt_flags);
5294 if (p == NULL)
5295 {
5296 // cannot happen?
5297 emsg(_(e_letunexp));
5298 goto theend;
5299 }
5300 cc = *p;
5301 *p = NUL;
5302 opt_type = get_option_value(var_start + 1, &numval,
5303 NULL, opt_flags);
5304 *p = cc;
5305 if (opt_type == -3)
5306 {
5307 semsg(_(e_unknown_option), var_start);
5308 goto theend;
5309 }
5310 if (opt_type == -2 || opt_type == 0)
5311 type = &t_string;
5312 else
5313 type = &t_number; // both number and boolean option
5314 }
5315 else if (*var_start == '$')
5316 {
5317 dest = dest_env;
5318 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005319 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005320 }
5321 else if (*var_start == '@')
5322 {
Bram Moolenaar65821722020-08-02 18:58:54 +02005323 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005324 {
5325 emsg_invreg(var_start[1]);
5326 goto theend;
5327 }
5328 dest = dest_reg;
5329 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005330 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005331 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005332 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005333 {
5334 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005335 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005336 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005337 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005338 {
5339 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005340 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005341 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005342 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005343 {
5344 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005345 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005346 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005347 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005348 {
5349 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005350 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005351 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02005352 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005353 {
5354 typval_T *vtv;
5355 int di_flags;
5356
5357 vimvaridx = find_vim_var(name + 2, &di_flags);
5358 if (vimvaridx < 0)
5359 {
5360 semsg(_(e_var_notfound), var_start);
5361 goto theend;
5362 }
5363 // We use the current value of "sandbox" here, is that OK?
5364 if (var_check_ro(di_flags, name, FALSE))
5365 goto theend;
5366 dest = dest_vimvar;
5367 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02005368 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005369 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005370 }
5371 else
5372 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005373 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005374
5375 for (idx = 0; reserved[idx] != NULL; ++idx)
5376 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005377 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005378 semsg(_("E1034: Cannot use reserved name %s"), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005379 goto theend;
5380 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005381
5382 lvar = lookup_local(var_start, varlen, cctx);
5383 if (lvar == NULL)
5384 {
5385 CLEAR_FIELD(arg_lvar);
5386 if (lookup_arg(var_start, varlen,
5387 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5388 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005389 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005390 if (is_decl)
5391 {
5392 semsg(_(e_used_as_arg), name);
5393 goto theend;
5394 }
5395 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005396 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005397 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005398 if (lvar != NULL)
5399 {
5400 if (is_decl)
5401 {
5402 semsg(_("E1017: Variable already declared: %s"), name);
5403 goto theend;
5404 }
5405 else if (lvar->lv_const)
5406 {
5407 semsg(_("E1018: Cannot assign to a constant: %s"),
5408 name);
5409 goto theend;
5410 }
5411 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005412 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005413 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005414 int script_namespace = varlen > 1
5415 && STRNCMP(var_start, "s:", 2) == 0;
5416 int script_var = (script_namespace
5417 ? lookup_script(var_start + 2, varlen - 2)
5418 : lookup_script(var_start, varlen)) == OK;
5419 imported_T *import =
5420 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005421
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005422 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005423 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005424 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5425
5426 if (is_decl)
5427 {
5428 if (script_namespace)
5429 semsg(_("E1101: Cannot declare a script variable in a function: %s"),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005430 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005431 else
5432 semsg(_("E1054: Variable already declared in the script: %s"),
5433 name);
5434 goto theend;
5435 }
5436 else if (cctx->ctx_ufunc->uf_script_ctx_version
5437 == SCRIPT_VERSION_VIM9
5438 && script_namespace
5439 && !script_var && import == NULL)
5440 {
5441 semsg(_(e_unknown_var), name);
5442 goto theend;
5443 }
5444
5445 dest = dest_script;
5446
5447 // existing script-local variables should have a type
5448 scriptvar_sid = current_sctx.sc_sid;
5449 if (import != NULL)
5450 scriptvar_sid = import->imp_sid;
5451 scriptvar_idx = get_script_item_idx(scriptvar_sid,
5452 rawname, TRUE);
5453 if (scriptvar_idx >= 0)
5454 {
5455 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5456 svar_T *sv =
5457 ((svar_T *)si->sn_var_vals.ga_data)
5458 + scriptvar_idx;
5459 type = sv->sv_type;
5460 }
5461 }
5462 else if (name[1] == ':' && name[2] != NUL)
5463 {
5464 semsg(_("E1082: Cannot use a namespaced variable: %s"),
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005465 name);
5466 goto theend;
5467 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005468 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005469 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005470 semsg(_(e_unknown_var), name);
5471 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005472 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005473 else if (check_defined(var_start, varlen, cctx) == FAIL)
5474 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005475 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005476 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005477
5478 if (declare_error)
5479 {
5480 vim9_declare_error(name);
5481 goto theend;
5482 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005483 }
5484
5485 // handle "a:name" as a name, not index "name" on "a"
5486 if (varlen > 1 || var_start[varlen] != ':')
5487 p = var_end;
5488
5489 if (dest != dest_option)
5490 {
5491 if (is_decl && *p == ':')
5492 {
5493 // parse optional type: "let var: type = expr"
5494 if (!VIM_ISWHITE(p[1]))
5495 {
5496 semsg(_(e_white_after), ":");
5497 goto theend;
5498 }
5499 p = skipwhite(p + 1);
5500 type = parse_type(&p, cctx->ctx_type_list);
5501 has_type = TRUE;
5502 }
5503 else if (lvar != NULL)
5504 type = lvar->lv_type;
5505 }
5506
5507 if (oplen == 3 && !heredoc && dest != dest_global
5508 && type->tt_type != VAR_STRING
5509 && type->tt_type != VAR_ANY)
5510 {
5511 emsg(_("E1019: Can only concatenate to string"));
5512 goto theend;
5513 }
5514
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005515 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005516 {
5517 if (oplen > 1 && !heredoc)
5518 {
5519 // +=, /=, etc. require an existing variable
5520 semsg(_("E1020: cannot use an operator on a new variable: %s"),
5521 name);
5522 goto theend;
5523 }
5524
5525 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005526 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5527 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005528 goto theend;
5529 lvar = reserve_local(cctx, var_start, varlen,
5530 cmdidx == CMD_const, type);
5531 if (lvar == NULL)
5532 goto theend;
5533 new_local = TRUE;
5534 }
5535
5536 member_type = type;
5537 if (var_end > var_start + varlen)
5538 {
5539 // Something follows after the variable: "var[idx]".
5540 if (is_decl)
5541 {
5542 emsg(_("E1087: cannot use an index when declaring a variable"));
5543 goto theend;
5544 }
5545
5546 if (var_start[varlen] == '[')
5547 {
5548 has_index = TRUE;
5549 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005550 member_type = &t_any;
5551 else
5552 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005553 }
5554 else
5555 {
5556 semsg("Not supported yet: %s", var_start);
5557 goto theend;
5558 }
5559 }
5560 else if (lvar == &arg_lvar)
5561 {
5562 semsg(_("E1090: Cannot assign to argument %s"), name);
5563 goto theend;
5564 }
5565
5566 if (!heredoc)
5567 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005568 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005569 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005570 if (oplen > 0 && var_count == 0)
5571 {
5572 // skip over the "=" and the expression
5573 p = skipwhite(op + oplen);
5574 compile_expr0(&p, cctx);
5575 }
5576 }
5577 else if (oplen > 0)
5578 {
5579 type_T *stacktype;
5580
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005581 // For "var = expr" evaluate the expression.
5582 if (var_count == 0)
5583 {
5584 int r;
5585
5586 // for "+=", "*=", "..=" etc. first load the current value
5587 if (*op != '=')
5588 {
5589 generate_loadvar(cctx, dest, name, lvar, type);
5590
5591 if (has_index)
5592 {
5593 // TODO: get member from list or dict
5594 emsg("Index with operation not supported yet");
5595 goto theend;
5596 }
5597 }
5598
5599 // Compile the expression. Temporarily hide the new local
5600 // variable here, it is not available to this expression.
5601 if (new_local)
5602 --cctx->ctx_locals.ga_len;
5603 instr_count = instr->ga_len;
5604 p = skipwhite(op + oplen);
5605 r = compile_expr0(&p, cctx);
5606 if (new_local)
5607 ++cctx->ctx_locals.ga_len;
5608 if (r == FAIL)
5609 goto theend;
5610 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005611 else if (semicolon && var_idx == var_count - 1)
5612 {
5613 // For "[var; var] = expr" get the rest of the list
5614 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5615 goto theend;
5616 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005617 else
5618 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005619 // For "[var, var] = expr" get the "var_idx" item from the
5620 // list.
5621 if (generate_GETITEM(cctx, var_idx) == FAIL)
5622 return FAIL;
5623 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005624
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005625 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005626 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005627 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005628 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005629 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005630 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005631 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005632 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005633 emsg(_(e_cannot_use_void));
5634 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005635 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005636 else if ((stacktype->tt_type == VAR_FUNC
5637 || stacktype->tt_type == VAR_PARTIAL)
5638 && var_wrong_func_name(name, TRUE))
5639 {
5640 goto theend;
5641 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005642 else
5643 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005644 // An empty list or dict has a &t_void member,
5645 // for a variable that implies &t_any.
5646 if (stacktype == &t_list_empty)
5647 lvar->lv_type = &t_list_any;
5648 else if (stacktype == &t_dict_empty)
5649 lvar->lv_type = &t_dict_any;
5650 else
5651 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005652 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005653 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005654 else
5655 {
5656 type_T *use_type = lvar->lv_type;
5657
5658 if (has_index)
5659 {
5660 use_type = use_type->tt_member;
5661 if (use_type == NULL)
5662 use_type = &t_void;
5663 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005664 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005665 == FAIL)
5666 goto theend;
5667 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005668 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005669 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005670 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005671 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005672 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005673 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005674 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005675 emsg(_(e_const_req_value));
5676 goto theend;
5677 }
5678 else if (!has_type || dest == dest_option)
5679 {
5680 emsg(_(e_type_req));
5681 goto theend;
5682 }
5683 else
5684 {
5685 // variables are always initialized
5686 if (ga_grow(instr, 1) == FAIL)
5687 goto theend;
5688 switch (member_type->tt_type)
5689 {
5690 case VAR_BOOL:
5691 generate_PUSHBOOL(cctx, VVAL_FALSE);
5692 break;
5693 case VAR_FLOAT:
5694#ifdef FEAT_FLOAT
5695 generate_PUSHF(cctx, 0.0);
5696#endif
5697 break;
5698 case VAR_STRING:
5699 generate_PUSHS(cctx, NULL);
5700 break;
5701 case VAR_BLOB:
5702 generate_PUSHBLOB(cctx, NULL);
5703 break;
5704 case VAR_FUNC:
5705 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5706 break;
5707 case VAR_LIST:
5708 generate_NEWLIST(cctx, 0);
5709 break;
5710 case VAR_DICT:
5711 generate_NEWDICT(cctx, 0);
5712 break;
5713 case VAR_JOB:
5714 generate_PUSHJOB(cctx, NULL);
5715 break;
5716 case VAR_CHANNEL:
5717 generate_PUSHCHANNEL(cctx, NULL);
5718 break;
5719 case VAR_NUMBER:
5720 case VAR_UNKNOWN:
5721 case VAR_ANY:
5722 case VAR_PARTIAL:
5723 case VAR_VOID:
5724 case VAR_SPECIAL: // cannot happen
5725 generate_PUSHNR(cctx, 0);
5726 break;
5727 }
5728 }
5729 if (var_count == 0)
5730 end = p;
5731 }
5732
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005733 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005734 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005735 break;
5736
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005737 if (oplen > 0 && *op != '=')
5738 {
5739 type_T *expected = &t_number;
5740 type_T *stacktype;
5741
5742 // TODO: if type is known use float or any operation
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005743 // TODO: check operator matches variable type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005744
5745 if (*op == '.')
5746 expected = &t_string;
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005747 else if (*op == '+')
5748 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005749 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005750 if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005751 goto theend;
5752
5753 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005754 {
5755 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5756 goto theend;
5757 }
5758 else if (*op == '+')
5759 {
5760 if (generate_add_instr(cctx,
5761 operator_type(member_type, stacktype),
5762 member_type, stacktype) == FAIL)
5763 goto theend;
5764 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005765 else
5766 {
5767 isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
5768
5769 if (isn == NULL)
5770 goto theend;
5771 switch (*op)
5772 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005773 case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
5774 case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
5775 case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
5776 case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
5777 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005778 }
5779 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005780
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005781 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005782 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005783 int r;
5784
5785 // Compile the "idx" in "var[idx]".
5786 if (new_local)
5787 --cctx->ctx_locals.ga_len;
5788 p = skipwhite(var_start + varlen + 1);
5789 r = compile_expr0(&p, cctx);
5790 if (new_local)
5791 ++cctx->ctx_locals.ga_len;
5792 if (r == FAIL)
5793 goto theend;
5794 if (*skipwhite(p) != ']')
5795 {
5796 emsg(_(e_missbrac));
5797 goto theend;
5798 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005799 if (type == &t_any)
5800 {
5801 type_T *idx_type = ((type_T **)stack->ga_data)[
5802 stack->ga_len - 1];
5803 // Index on variable of unknown type: guess the type from the
5804 // index type: number is dict, otherwise dict.
5805 // TODO: should do the assignment at runtime
5806 if (idx_type->tt_type == VAR_NUMBER)
5807 type = &t_list_any;
5808 else
5809 type = &t_dict_any;
5810 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005811 if (type->tt_type == VAR_DICT
5812 && may_generate_2STRING(-1, cctx) == FAIL)
5813 goto theend;
5814 if (type->tt_type == VAR_LIST
5815 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005816 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005817 {
5818 emsg(_(e_number_exp));
5819 goto theend;
5820 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005821
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005822 // Load the dict or list. On the stack we then have:
5823 // - value
5824 // - index
5825 // - variable
5826 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005827
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005828 if (type->tt_type == VAR_LIST)
5829 {
5830 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5831 return FAIL;
5832 }
5833 else if (type->tt_type == VAR_DICT)
5834 {
5835 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5836 return FAIL;
5837 }
5838 else
5839 {
5840 emsg(_(e_listreq));
5841 goto theend;
5842 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005843 }
5844 else
5845 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005846 switch (dest)
5847 {
5848 case dest_option:
5849 generate_STOREOPT(cctx, name + 1, opt_flags);
5850 break;
5851 case dest_global:
5852 // include g: with the name, easier to execute that way
5853 generate_STORE(cctx, ISN_STOREG, 0, name);
5854 break;
5855 case dest_buffer:
5856 // include b: with the name, easier to execute that way
5857 generate_STORE(cctx, ISN_STOREB, 0, name);
5858 break;
5859 case dest_window:
5860 // include w: with the name, easier to execute that way
5861 generate_STORE(cctx, ISN_STOREW, 0, name);
5862 break;
5863 case dest_tab:
5864 // include t: with the name, easier to execute that way
5865 generate_STORE(cctx, ISN_STORET, 0, name);
5866 break;
5867 case dest_env:
5868 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5869 break;
5870 case dest_reg:
5871 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5872 break;
5873 case dest_vimvar:
5874 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5875 break;
5876 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005877 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005878 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005879 {
5880 char_u *name_s = name;
5881
5882 // Include s: in the name for store_var()
5883 if (name[1] != ':')
5884 {
5885 int len = (int)STRLEN(name) + 3;
5886
5887 name_s = alloc(len);
5888 if (name_s == NULL)
5889 name_s = name;
5890 else
5891 vim_snprintf((char *)name_s, len,
5892 "s:%s", name);
5893 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005894 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5895 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005896 if (name_s != name)
5897 vim_free(name_s);
5898 }
5899 else
5900 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005901 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005902 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005903 break;
5904 case dest_local:
5905 if (lvar != NULL)
5906 {
5907 isn_T *isn = ((isn_T *)instr->ga_data)
5908 + instr->ga_len - 1;
5909
5910 // optimization: turn "var = 123" from ISN_PUSHNR +
5911 // ISN_STORE into ISN_STORENR
5912 if (!lvar->lv_from_outer
5913 && instr->ga_len == instr_count + 1
5914 && isn->isn_type == ISN_PUSHNR)
5915 {
5916 varnumber_T val = isn->isn_arg.number;
5917
5918 isn->isn_type = ISN_STORENR;
5919 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5920 isn->isn_arg.storenr.stnr_val = val;
5921 if (stack->ga_len > 0)
5922 --stack->ga_len;
5923 }
5924 else if (lvar->lv_from_outer)
5925 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005926 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005927 else
5928 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5929 }
5930 break;
5931 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005932 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005933
5934 if (var_idx + 1 < var_count)
5935 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005936 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005937
5938 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005939 if (var_count > 0 && !semicolon)
5940 {
5941 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5942 goto theend;
5943 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005944
Bram Moolenaarb2097502020-07-19 17:17:02 +02005945 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005946
5947theend:
5948 vim_free(name);
5949 return ret;
5950}
5951
5952/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005953 * Check if "name" can be "unlet".
5954 */
5955 int
5956check_vim9_unlet(char_u *name)
5957{
5958 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5959 {
5960 semsg(_("E1081: Cannot unlet %s"), name);
5961 return FAIL;
5962 }
5963 return OK;
5964}
5965
5966/*
5967 * Callback passed to ex_unletlock().
5968 */
5969 static int
5970compile_unlet(
5971 lval_T *lvp,
5972 char_u *name_end,
5973 exarg_T *eap,
5974 int deep UNUSED,
5975 void *coookie)
5976{
5977 cctx_T *cctx = coookie;
5978
5979 if (lvp->ll_tv == NULL)
5980 {
5981 char_u *p = lvp->ll_name;
5982 int cc = *name_end;
5983 int ret = OK;
5984
5985 // Normal name. Only supports g:, w:, t: and b: namespaces.
5986 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005987 if (*p == '$')
5988 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5989 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005990 ret = FAIL;
5991 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005992 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005993
5994 *name_end = cc;
5995 return ret;
5996 }
5997
5998 // TODO: unlet {list}[idx]
5999 // TODO: unlet {dict}[key]
6000 emsg("Sorry, :unlet not fully implemented yet");
6001 return FAIL;
6002}
6003
6004/*
6005 * compile "unlet var", "lock var" and "unlock var"
6006 * "arg" points to "var".
6007 */
6008 static char_u *
6009compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6010{
6011 char_u *p = arg;
6012
6013 if (eap->cmdidx != CMD_unlet)
6014 {
6015 emsg("Sorry, :lock and unlock not implemented yet");
6016 return NULL;
6017 }
6018
6019 if (*p == '!')
6020 {
6021 p = skipwhite(p + 1);
6022 eap->forceit = TRUE;
6023 }
6024
6025 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
6026 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6027}
6028
6029/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006030 * Compile an :import command.
6031 */
6032 static char_u *
6033compile_import(char_u *arg, cctx_T *cctx)
6034{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006035 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006036}
6037
6038/*
6039 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6040 */
6041 static int
6042compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6043{
6044 garray_T *instr = &cctx->ctx_instr;
6045 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6046
6047 if (endlabel == NULL)
6048 return FAIL;
6049 endlabel->el_next = *el;
6050 *el = endlabel;
6051 endlabel->el_end_label = instr->ga_len;
6052
6053 generate_JUMP(cctx, when, 0);
6054 return OK;
6055}
6056
6057 static void
6058compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6059{
6060 garray_T *instr = &cctx->ctx_instr;
6061
6062 while (*el != NULL)
6063 {
6064 endlabel_T *cur = (*el);
6065 isn_T *isn;
6066
6067 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6068 isn->isn_arg.jump.jump_where = instr->ga_len;
6069 *el = cur->el_next;
6070 vim_free(cur);
6071 }
6072}
6073
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006074 static void
6075compile_free_jump_to_end(endlabel_T **el)
6076{
6077 while (*el != NULL)
6078 {
6079 endlabel_T *cur = (*el);
6080
6081 *el = cur->el_next;
6082 vim_free(cur);
6083 }
6084}
6085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006086/*
6087 * Create a new scope and set up the generic items.
6088 */
6089 static scope_T *
6090new_scope(cctx_T *cctx, scopetype_T type)
6091{
6092 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6093
6094 if (scope == NULL)
6095 return NULL;
6096 scope->se_outer = cctx->ctx_scope;
6097 cctx->ctx_scope = scope;
6098 scope->se_type = type;
6099 scope->se_local_count = cctx->ctx_locals.ga_len;
6100 return scope;
6101}
6102
6103/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006104 * Free the current scope and go back to the outer scope.
6105 */
6106 static void
6107drop_scope(cctx_T *cctx)
6108{
6109 scope_T *scope = cctx->ctx_scope;
6110
6111 if (scope == NULL)
6112 {
6113 iemsg("calling drop_scope() without a scope");
6114 return;
6115 }
6116 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006117 switch (scope->se_type)
6118 {
6119 case IF_SCOPE:
6120 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6121 case FOR_SCOPE:
6122 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6123 case WHILE_SCOPE:
6124 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6125 case TRY_SCOPE:
6126 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6127 case NO_SCOPE:
6128 case BLOCK_SCOPE:
6129 break;
6130 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006131 vim_free(scope);
6132}
6133
6134/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006135 * compile "if expr"
6136 *
6137 * "if expr" Produces instructions:
6138 * EVAL expr Push result of "expr"
6139 * JUMP_IF_FALSE end
6140 * ... body ...
6141 * end:
6142 *
6143 * "if expr | else" Produces instructions:
6144 * EVAL expr Push result of "expr"
6145 * JUMP_IF_FALSE else
6146 * ... body ...
6147 * JUMP_ALWAYS end
6148 * else:
6149 * ... body ...
6150 * end:
6151 *
6152 * "if expr1 | elseif expr2 | else" Produces instructions:
6153 * EVAL expr Push result of "expr"
6154 * JUMP_IF_FALSE elseif
6155 * ... body ...
6156 * JUMP_ALWAYS end
6157 * elseif:
6158 * EVAL expr Push result of "expr"
6159 * JUMP_IF_FALSE else
6160 * ... body ...
6161 * JUMP_ALWAYS end
6162 * else:
6163 * ... body ...
6164 * end:
6165 */
6166 static char_u *
6167compile_if(char_u *arg, cctx_T *cctx)
6168{
6169 char_u *p = arg;
6170 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006171 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006172 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006173 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006174 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006175
Bram Moolenaara5565e42020-05-09 15:44:01 +02006176 CLEAR_FIELD(ppconst);
6177 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006178 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006179 clear_ppconst(&ppconst);
6180 return NULL;
6181 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006182 if (cctx->ctx_skip == SKIP_YES)
6183 clear_ppconst(&ppconst);
6184 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006185 {
6186 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006187 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006188 clear_ppconst(&ppconst);
6189 }
6190 else
6191 {
6192 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006193 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006194 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006195 return NULL;
6196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006197
6198 scope = new_scope(cctx, IF_SCOPE);
6199 if (scope == NULL)
6200 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006201 scope->se_skip_save = skip_save;
6202 // "is_had_return" will be reset if any block does not end in :return
6203 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006204
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006205 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006206 {
6207 // "where" is set when ":elseif", "else" or ":endif" is found
6208 scope->se_u.se_if.is_if_label = instr->ga_len;
6209 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6210 }
6211 else
6212 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006213
6214 return p;
6215}
6216
6217 static char_u *
6218compile_elseif(char_u *arg, cctx_T *cctx)
6219{
6220 char_u *p = arg;
6221 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006222 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006223 isn_T *isn;
6224 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006225 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006226
6227 if (scope == NULL || scope->se_type != IF_SCOPE)
6228 {
6229 emsg(_(e_elseif_without_if));
6230 return NULL;
6231 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006232 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006233 if (!cctx->ctx_had_return)
6234 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006235
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006236 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006237 {
6238 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006239 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006240 return NULL;
6241 // previous "if" or "elseif" jumps here
6242 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6243 isn->isn_arg.jump.jump_where = instr->ga_len;
6244 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006245
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006246 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006247 CLEAR_FIELD(ppconst);
6248 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006249 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006250 clear_ppconst(&ppconst);
6251 return NULL;
6252 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006253 if (scope->se_skip_save == SKIP_YES)
6254 clear_ppconst(&ppconst);
6255 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006256 {
6257 // The expression results in a constant.
6258 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006259 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006260 clear_ppconst(&ppconst);
6261 scope->se_u.se_if.is_if_label = -1;
6262 }
6263 else
6264 {
6265 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006266 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006267 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006268 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006269
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006270 // "where" is set when ":elseif", "else" or ":endif" is found
6271 scope->se_u.se_if.is_if_label = instr->ga_len;
6272 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6273 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006274
6275 return p;
6276}
6277
6278 static char_u *
6279compile_else(char_u *arg, cctx_T *cctx)
6280{
6281 char_u *p = arg;
6282 garray_T *instr = &cctx->ctx_instr;
6283 isn_T *isn;
6284 scope_T *scope = cctx->ctx_scope;
6285
6286 if (scope == NULL || scope->se_type != IF_SCOPE)
6287 {
6288 emsg(_(e_else_without_if));
6289 return NULL;
6290 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006291 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006292 if (!cctx->ctx_had_return)
6293 scope->se_u.se_if.is_had_return = FALSE;
6294 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006295
Bram Moolenaarefd88552020-06-18 20:50:10 +02006296 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006297 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006298 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006299 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006300 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006301 if (!cctx->ctx_had_return
6302 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6303 JUMP_ALWAYS, cctx) == FAIL)
6304 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006305 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006306
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006307 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006308 {
6309 if (scope->se_u.se_if.is_if_label >= 0)
6310 {
6311 // previous "if" or "elseif" jumps here
6312 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6313 isn->isn_arg.jump.jump_where = instr->ga_len;
6314 scope->se_u.se_if.is_if_label = -1;
6315 }
6316 }
6317
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006318 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006319 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6320 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006321
6322 return p;
6323}
6324
6325 static char_u *
6326compile_endif(char_u *arg, cctx_T *cctx)
6327{
6328 scope_T *scope = cctx->ctx_scope;
6329 ifscope_T *ifscope;
6330 garray_T *instr = &cctx->ctx_instr;
6331 isn_T *isn;
6332
6333 if (scope == NULL || scope->se_type != IF_SCOPE)
6334 {
6335 emsg(_(e_endif_without_if));
6336 return NULL;
6337 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006338 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006339 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006340 if (!cctx->ctx_had_return)
6341 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006342
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006343 if (scope->se_u.se_if.is_if_label >= 0)
6344 {
6345 // previous "if" or "elseif" jumps here
6346 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6347 isn->isn_arg.jump.jump_where = instr->ga_len;
6348 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006349 // Fill in the "end" label in jumps at the end of the blocks.
6350 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006351 cctx->ctx_skip = scope->se_skip_save;
6352
6353 // If all the blocks end in :return and there is an :else then the
6354 // had_return flag is set.
6355 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006356
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006357 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006358 return arg;
6359}
6360
6361/*
6362 * compile "for var in expr"
6363 *
6364 * Produces instructions:
6365 * PUSHNR -1
6366 * STORE loop-idx Set index to -1
6367 * EVAL expr Push result of "expr"
6368 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6369 * - if beyond end, jump to "end"
6370 * - otherwise get item from list and push it
6371 * STORE var Store item in "var"
6372 * ... body ...
6373 * JUMP top Jump back to repeat
6374 * end: DROP Drop the result of "expr"
6375 *
6376 */
6377 static char_u *
6378compile_for(char_u *arg, cctx_T *cctx)
6379{
6380 char_u *p;
6381 size_t varlen;
6382 garray_T *instr = &cctx->ctx_instr;
6383 garray_T *stack = &cctx->ctx_type_stack;
6384 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006385 lvar_T *loop_lvar; // loop iteration variable
6386 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006387 type_T *vartype;
6388
6389 // TODO: list of variables: "for [key, value] in dict"
6390 // parse "var"
6391 for (p = arg; eval_isnamec1(*p); ++p)
6392 ;
6393 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006394 var_lvar = lookup_local(arg, varlen, cctx);
6395 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006396 {
6397 semsg(_("E1023: variable already defined: %s"), arg);
6398 return NULL;
6399 }
6400
6401 // consume "in"
6402 p = skipwhite(p);
6403 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
6404 {
6405 emsg(_(e_missing_in));
6406 return NULL;
6407 }
6408 p = skipwhite(p + 2);
6409
6410
6411 scope = new_scope(cctx, FOR_SCOPE);
6412 if (scope == NULL)
6413 return NULL;
6414
6415 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006416 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6417 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006418 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006419 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006420 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006421 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006422 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006423
6424 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006425 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6426 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006427 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006428 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006429 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006430 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006431 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006432
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006433 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006434
6435 // compile "expr", it remains on the stack until "endfor"
6436 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006437 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006438 {
6439 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006440 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006441 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006442
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006443 // Now that we know the type of "var", check that it is a list, now or at
6444 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006445 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006446 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006447 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006448 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006449 return NULL;
6450 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006451 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006452 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006453
6454 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006455 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006456
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006457 generate_FOR(cctx, loop_lvar->lv_idx);
6458 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006459
6460 return arg;
6461}
6462
6463/*
6464 * compile "endfor"
6465 */
6466 static char_u *
6467compile_endfor(char_u *arg, cctx_T *cctx)
6468{
6469 garray_T *instr = &cctx->ctx_instr;
6470 scope_T *scope = cctx->ctx_scope;
6471 forscope_T *forscope;
6472 isn_T *isn;
6473
6474 if (scope == NULL || scope->se_type != FOR_SCOPE)
6475 {
6476 emsg(_(e_for));
6477 return NULL;
6478 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006479 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006480 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006481 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006482
6483 // At end of ":for" scope jump back to the FOR instruction.
6484 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6485
6486 // Fill in the "end" label in the FOR statement so it can jump here
6487 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6488 isn->isn_arg.forloop.for_end = instr->ga_len;
6489
6490 // Fill in the "end" label any BREAK statements
6491 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6492
6493 // Below the ":for" scope drop the "expr" list from the stack.
6494 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6495 return NULL;
6496
6497 vim_free(scope);
6498
6499 return arg;
6500}
6501
6502/*
6503 * compile "while expr"
6504 *
6505 * Produces instructions:
6506 * top: EVAL expr Push result of "expr"
6507 * JUMP_IF_FALSE end jump if false
6508 * ... body ...
6509 * JUMP top Jump back to repeat
6510 * end:
6511 *
6512 */
6513 static char_u *
6514compile_while(char_u *arg, cctx_T *cctx)
6515{
6516 char_u *p = arg;
6517 garray_T *instr = &cctx->ctx_instr;
6518 scope_T *scope;
6519
6520 scope = new_scope(cctx, WHILE_SCOPE);
6521 if (scope == NULL)
6522 return NULL;
6523
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006524 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006525
6526 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006527 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006528 return NULL;
6529
6530 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006531 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006532 JUMP_IF_FALSE, cctx) == FAIL)
6533 return FAIL;
6534
6535 return p;
6536}
6537
6538/*
6539 * compile "endwhile"
6540 */
6541 static char_u *
6542compile_endwhile(char_u *arg, cctx_T *cctx)
6543{
6544 scope_T *scope = cctx->ctx_scope;
6545
6546 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6547 {
6548 emsg(_(e_while));
6549 return NULL;
6550 }
6551 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006552 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006553
6554 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006555 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006556
6557 // Fill in the "end" label in the WHILE statement so it can jump here.
6558 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006559 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006560
6561 vim_free(scope);
6562
6563 return arg;
6564}
6565
6566/*
6567 * compile "continue"
6568 */
6569 static char_u *
6570compile_continue(char_u *arg, cctx_T *cctx)
6571{
6572 scope_T *scope = cctx->ctx_scope;
6573
6574 for (;;)
6575 {
6576 if (scope == NULL)
6577 {
6578 emsg(_(e_continue));
6579 return NULL;
6580 }
6581 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6582 break;
6583 scope = scope->se_outer;
6584 }
6585
6586 // Jump back to the FOR or WHILE instruction.
6587 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006588 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6589 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006590 return arg;
6591}
6592
6593/*
6594 * compile "break"
6595 */
6596 static char_u *
6597compile_break(char_u *arg, cctx_T *cctx)
6598{
6599 scope_T *scope = cctx->ctx_scope;
6600 endlabel_T **el;
6601
6602 for (;;)
6603 {
6604 if (scope == NULL)
6605 {
6606 emsg(_(e_break));
6607 return NULL;
6608 }
6609 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6610 break;
6611 scope = scope->se_outer;
6612 }
6613
6614 // Jump to the end of the FOR or WHILE loop.
6615 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006616 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006618 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006619 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6620 return FAIL;
6621
6622 return arg;
6623}
6624
6625/*
6626 * compile "{" start of block
6627 */
6628 static char_u *
6629compile_block(char_u *arg, cctx_T *cctx)
6630{
6631 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6632 return NULL;
6633 return skipwhite(arg + 1);
6634}
6635
6636/*
6637 * compile end of block: drop one scope
6638 */
6639 static void
6640compile_endblock(cctx_T *cctx)
6641{
6642 scope_T *scope = cctx->ctx_scope;
6643
6644 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006645 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006646 vim_free(scope);
6647}
6648
6649/*
6650 * compile "try"
6651 * Creates a new scope for the try-endtry, pointing to the first catch and
6652 * finally.
6653 * Creates another scope for the "try" block itself.
6654 * TRY instruction sets up exception handling at runtime.
6655 *
6656 * "try"
6657 * TRY -> catch1, -> finally push trystack entry
6658 * ... try block
6659 * "throw {exception}"
6660 * EVAL {exception}
6661 * THROW create exception
6662 * ... try block
6663 * " catch {expr}"
6664 * JUMP -> finally
6665 * catch1: PUSH exeception
6666 * EVAL {expr}
6667 * MATCH
6668 * JUMP nomatch -> catch2
6669 * CATCH remove exception
6670 * ... catch block
6671 * " catch"
6672 * JUMP -> finally
6673 * catch2: CATCH remove exception
6674 * ... catch block
6675 * " finally"
6676 * finally:
6677 * ... finally block
6678 * " endtry"
6679 * ENDTRY pop trystack entry, may rethrow
6680 */
6681 static char_u *
6682compile_try(char_u *arg, cctx_T *cctx)
6683{
6684 garray_T *instr = &cctx->ctx_instr;
6685 scope_T *try_scope;
6686 scope_T *scope;
6687
6688 // scope that holds the jumps that go to catch/finally/endtry
6689 try_scope = new_scope(cctx, TRY_SCOPE);
6690 if (try_scope == NULL)
6691 return NULL;
6692
6693 // "catch" is set when the first ":catch" is found.
6694 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006695 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006696 if (generate_instr(cctx, ISN_TRY) == NULL)
6697 return NULL;
6698
6699 // scope for the try block itself
6700 scope = new_scope(cctx, BLOCK_SCOPE);
6701 if (scope == NULL)
6702 return NULL;
6703
6704 return arg;
6705}
6706
6707/*
6708 * compile "catch {expr}"
6709 */
6710 static char_u *
6711compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6712{
6713 scope_T *scope = cctx->ctx_scope;
6714 garray_T *instr = &cctx->ctx_instr;
6715 char_u *p;
6716 isn_T *isn;
6717
6718 // end block scope from :try or :catch
6719 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6720 compile_endblock(cctx);
6721 scope = cctx->ctx_scope;
6722
6723 // Error if not in a :try scope
6724 if (scope == NULL || scope->se_type != TRY_SCOPE)
6725 {
6726 emsg(_(e_catch));
6727 return NULL;
6728 }
6729
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006730 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006731 {
6732 emsg(_("E1033: catch unreachable after catch-all"));
6733 return NULL;
6734 }
6735
6736 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006737 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006738 JUMP_ALWAYS, cctx) == FAIL)
6739 return NULL;
6740
6741 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006742 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006743 if (isn->isn_arg.try.try_catch == 0)
6744 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006745 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006746 {
6747 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006748 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006749 isn->isn_arg.jump.jump_where = instr->ga_len;
6750 }
6751
6752 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006753 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006754 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006755 scope->se_u.se_try.ts_caught_all = TRUE;
6756 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006757 }
6758 else
6759 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006760 char_u *end;
6761 char_u *pat;
6762 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006763 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006764 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006765
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006766 // Push v:exception, push {expr} and MATCH
6767 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6768
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006769 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006770 if (*end != *p)
6771 {
6772 semsg(_("E1067: Separator mismatch: %s"), p);
6773 vim_free(tofree);
6774 return FAIL;
6775 }
6776 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006777 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006778 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006779 len = (int)(end - tofree);
6780 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006781 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006782 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006783 if (pat == NULL)
6784 return FAIL;
6785 if (generate_PUSHS(cctx, pat) == FAIL)
6786 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006787
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006788 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6789 return NULL;
6790
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006791 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006792 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6793 return NULL;
6794 }
6795
6796 if (generate_instr(cctx, ISN_CATCH) == NULL)
6797 return NULL;
6798
6799 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6800 return NULL;
6801 return p;
6802}
6803
6804 static char_u *
6805compile_finally(char_u *arg, cctx_T *cctx)
6806{
6807 scope_T *scope = cctx->ctx_scope;
6808 garray_T *instr = &cctx->ctx_instr;
6809 isn_T *isn;
6810
6811 // end block scope from :try or :catch
6812 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6813 compile_endblock(cctx);
6814 scope = cctx->ctx_scope;
6815
6816 // Error if not in a :try scope
6817 if (scope == NULL || scope->se_type != TRY_SCOPE)
6818 {
6819 emsg(_(e_finally));
6820 return NULL;
6821 }
6822
6823 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006824 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006825 if (isn->isn_arg.try.try_finally != 0)
6826 {
6827 emsg(_(e_finally_dup));
6828 return NULL;
6829 }
6830
6831 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006832 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006833
Bram Moolenaar585fea72020-04-02 22:33:21 +02006834 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006835 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006836 {
6837 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006838 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006839 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006840 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006841 }
6842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006843 // TODO: set index in ts_finally_label jumps
6844
6845 return arg;
6846}
6847
6848 static char_u *
6849compile_endtry(char_u *arg, cctx_T *cctx)
6850{
6851 scope_T *scope = cctx->ctx_scope;
6852 garray_T *instr = &cctx->ctx_instr;
6853 isn_T *isn;
6854
6855 // end block scope from :catch or :finally
6856 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6857 compile_endblock(cctx);
6858 scope = cctx->ctx_scope;
6859
6860 // Error if not in a :try scope
6861 if (scope == NULL || scope->se_type != TRY_SCOPE)
6862 {
6863 if (scope == NULL)
6864 emsg(_(e_no_endtry));
6865 else if (scope->se_type == WHILE_SCOPE)
6866 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006867 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868 emsg(_(e_endfor));
6869 else
6870 emsg(_(e_endif));
6871 return NULL;
6872 }
6873
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006874 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006875 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6876 {
6877 emsg(_("E1032: missing :catch or :finally"));
6878 return NULL;
6879 }
6880
6881 // Fill in the "end" label in jumps at the end of the blocks, if not done
6882 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006883 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006884
6885 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006886 if (isn->isn_arg.try.try_catch == 0)
6887 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006888 if (isn->isn_arg.try.try_finally == 0)
6889 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006890
6891 if (scope->se_u.se_try.ts_catch_label != 0)
6892 {
6893 // Last catch without match jumps here
6894 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6895 isn->isn_arg.jump.jump_where = instr->ga_len;
6896 }
6897
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006898 compile_endblock(cctx);
6899
6900 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6901 return NULL;
6902 return arg;
6903}
6904
6905/*
6906 * compile "throw {expr}"
6907 */
6908 static char_u *
6909compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6910{
6911 char_u *p = skipwhite(arg);
6912
Bram Moolenaara5565e42020-05-09 15:44:01 +02006913 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914 return NULL;
6915 if (may_generate_2STRING(-1, cctx) == FAIL)
6916 return NULL;
6917 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6918 return NULL;
6919
6920 return p;
6921}
6922
6923/*
6924 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006925 * compile "echomsg expr"
6926 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006927 * compile "execute expr"
6928 */
6929 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006930compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006931{
6932 char_u *p = arg;
6933 int count = 0;
6934
6935 for (;;)
6936 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006937 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006938 return NULL;
6939 ++count;
6940 p = skipwhite(p);
6941 if (ends_excmd(*p))
6942 break;
6943 }
6944
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006945 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6946 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6947 else if (cmdidx == CMD_execute)
6948 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6949 else if (cmdidx == CMD_echomsg)
6950 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6951 else
6952 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006953 return p;
6954}
6955
6956/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006957 * A command that is not compiled, execute with legacy code.
6958 */
6959 static char_u *
6960compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6961{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006962 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006963 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006964 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006965
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006966 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006967 goto theend;
6968
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006969 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006970 {
6971 long argt = excmd_get_argt(eap->cmdidx);
6972 int usefilter = FALSE;
6973
6974 has_expr = argt & (EX_XFILE | EX_EXPAND);
6975
6976 // If the command can be followed by a bar, find the bar and truncate
6977 // it, so that the following command can be compiled.
6978 // The '|' is overwritten with a NUL, it is put back below.
6979 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6980 && *eap->arg == '!')
6981 // :w !filter or :r !filter or :r! filter
6982 usefilter = TRUE;
6983 if ((argt & EX_TRLBAR) && !usefilter)
6984 {
6985 separate_nextcmd(eap);
6986 if (eap->nextcmd != NULL)
6987 nextcmd = eap->nextcmd;
6988 }
6989 }
6990
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006991 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6992 {
6993 // expand filename in "syntax include [@group] filename"
6994 has_expr = TRUE;
6995 eap->arg = skipwhite(eap->arg + 7);
6996 if (*eap->arg == '@')
6997 eap->arg = skiptowhite(eap->arg);
6998 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006999
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007000 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007001 {
7002 int count = 0;
7003 char_u *start = skipwhite(line);
7004
7005 // :cmd xxx`=expr1`yyy`=expr2`zzz
7006 // PUSHS ":cmd xxx"
7007 // eval expr1
7008 // PUSHS "yyy"
7009 // eval expr2
7010 // PUSHS "zzz"
7011 // EXECCONCAT 5
7012 for (;;)
7013 {
7014 if (p > start)
7015 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007016 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007017 ++count;
7018 }
7019 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007020 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007021 return NULL;
7022 may_generate_2STRING(-1, cctx);
7023 ++count;
7024 p = skipwhite(p);
7025 if (*p != '`')
7026 {
7027 emsg(_("E1083: missing backtick"));
7028 return NULL;
7029 }
7030 start = p + 1;
7031
7032 p = (char_u *)strstr((char *)start, "`=");
7033 if (p == NULL)
7034 {
7035 if (*skipwhite(start) != NUL)
7036 {
7037 generate_PUSHS(cctx, vim_strsave(start));
7038 ++count;
7039 }
7040 break;
7041 }
7042 }
7043 generate_EXECCONCAT(cctx, count);
7044 }
7045 else
7046 generate_EXEC(cctx, line);
7047
7048theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007049 if (*nextcmd != NUL)
7050 {
7051 // the parser expects a pointer to the bar, put it back
7052 --nextcmd;
7053 *nextcmd = '|';
7054 }
7055
7056 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007057}
7058
7059/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007060 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007061 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007062 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007063 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007064add_def_function(ufunc_T *ufunc)
7065{
7066 dfunc_T *dfunc;
7067
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007068 if (def_functions.ga_len == 0)
7069 {
7070 // The first position is not used, so that a zero uf_dfunc_idx means it
7071 // wasn't set.
7072 if (ga_grow(&def_functions, 1) == FAIL)
7073 return FAIL;
7074 ++def_functions.ga_len;
7075 }
7076
Bram Moolenaar09689a02020-05-09 22:50:08 +02007077 // Add the function to "def_functions".
7078 if (ga_grow(&def_functions, 1) == FAIL)
7079 return FAIL;
7080 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7081 CLEAR_POINTER(dfunc);
7082 dfunc->df_idx = def_functions.ga_len;
7083 ufunc->uf_dfunc_idx = dfunc->df_idx;
7084 dfunc->df_ufunc = ufunc;
7085 ++def_functions.ga_len;
7086 return OK;
7087}
7088
7089/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007090 * After ex_function() has collected all the function lines: parse and compile
7091 * the lines into instructions.
7092 * Adds the function to "def_functions".
7093 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7094 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007095 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007096 * This can be used recursively through compile_lambda(), which may reallocate
7097 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007098 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007099 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007100 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007101compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007102{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007103 char_u *line = NULL;
7104 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007105 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007106 cctx_T cctx;
7107 garray_T *instr;
7108 int called_emsg_before = called_emsg;
7109 int ret = FAIL;
7110 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007111 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007112 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007113 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007114
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007115 // When using a function that was compiled before: Free old instructions.
7116 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007117 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007118 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007119 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7120 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007121 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007122 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007123 else
7124 {
7125 if (add_def_function(ufunc) == FAIL)
7126 return FAIL;
7127 new_def_function = TRUE;
7128 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129
Bram Moolenaar985116a2020-07-12 17:31:09 +02007130 ufunc->uf_def_status = UF_COMPILING;
7131
Bram Moolenaara80faa82020-04-12 19:37:17 +02007132 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133 cctx.ctx_ufunc = ufunc;
7134 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007135 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007136 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7137 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7138 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7139 cctx.ctx_type_list = &ufunc->uf_type_list;
7140 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7141 instr = &cctx.ctx_instr;
7142
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007143 // Set the context to the function, it may be compiled when called from
7144 // another script. Set the script version to the most modern one.
7145 // The line number will be set in next_line_from_context().
7146 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007147 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7148
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007149 // Make sure error messages are OK.
7150 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7151 if (do_estack_push)
7152 estack_push_ufunc(ufunc, 1);
7153
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007154 if (ufunc->uf_def_args.ga_len > 0)
7155 {
7156 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007157 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007158 int i;
7159 char_u *arg;
7160 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007161 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007162
7163 // Produce instructions for the default values of optional arguments.
7164 // Store the instruction index in uf_def_arg_idx[] so that we know
7165 // where to start when the function is called, depending on the number
7166 // of arguments.
7167 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7168 if (ufunc->uf_def_arg_idx == NULL)
7169 goto erret;
7170 for (i = 0; i < count; ++i)
7171 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007172 garray_T *stack = &cctx.ctx_type_stack;
7173 type_T *val_type;
7174 int arg_idx = first_def_arg + i;
7175
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007176 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7177 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007178 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007179 goto erret;
7180
7181 // If no type specified use the type of the default value.
7182 // Otherwise check that the default value type matches the
7183 // specified type.
7184 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7185 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007186 {
7187 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007188 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007189 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02007190 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007191 == FAIL)
7192 {
7193 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
7194 arg_idx + 1);
7195 goto erret;
7196 }
7197
7198 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007199 goto erret;
7200 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007201 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007202
7203 if (did_set_arg_type)
7204 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007205 }
7206
7207 /*
7208 * Loop over all the lines of the function and generate instructions.
7209 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007210 for (;;)
7211 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007212 exarg_T ea;
7213 cmdmod_T save_cmdmod;
7214 int starts_with_colon = FALSE;
7215 char_u *cmd;
7216 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007217
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007218 // Bail out on the first error to avoid a flood of errors and report
7219 // the right line number when inside try/catch.
7220 if (emsg_before != called_emsg)
7221 goto erret;
7222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007223 if (line != NULL && *line == '|')
7224 // the line continues after a '|'
7225 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007226 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007227 && !(*line == '#' && (line == cctx.ctx_line_start
7228 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007229 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007230 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007231 goto erret;
7232 }
7233 else
7234 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007235 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007236 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007237 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007238 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007239 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007240 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007241
Bram Moolenaara80faa82020-04-12 19:37:17 +02007242 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007243 ea.cmdlinep = &line;
7244 ea.cmd = skipwhite(line);
7245
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007246 // Some things can be recognized by the first character.
7247 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007248 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007249 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007250 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007251 if (ea.cmd[1] != '{')
7252 {
7253 line = (char_u *)"";
7254 continue;
7255 }
7256 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007257
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007258 case '}':
7259 {
7260 // "}" ends a block scope
7261 scopetype_T stype = cctx.ctx_scope == NULL
7262 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007263
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007264 if (stype == BLOCK_SCOPE)
7265 {
7266 compile_endblock(&cctx);
7267 line = ea.cmd;
7268 }
7269 else
7270 {
7271 emsg(_("E1025: using } outside of a block scope"));
7272 goto erret;
7273 }
7274 if (line != NULL)
7275 line = skipwhite(ea.cmd + 1);
7276 continue;
7277 }
7278
7279 case '{':
7280 // "{" starts a block scope
7281 // "{'a': 1}->func() is something else
7282 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7283 {
7284 line = compile_block(ea.cmd, &cctx);
7285 continue;
7286 }
7287 break;
7288
7289 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007290 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007291 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007292 }
7293
7294 /*
7295 * COMMAND MODIFIERS
7296 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007297 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007298 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
7299 {
7300 if (errormsg != NULL)
7301 goto erret;
7302 // empty line or comment
7303 line = (char_u *)"";
7304 continue;
7305 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02007306 // TODO: use modifiers in the command
7307 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007308 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007309
7310 // Skip ":call" to get to the function name.
7311 if (checkforcmd(&ea.cmd, "call", 3))
7312 ea.cmd = skipwhite(ea.cmd);
7313
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007314 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007315 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007316 char_u *pskip;
7317
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007318 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007319 // find what follows.
7320 // Skip over "var.member", "var[idx]" and the like.
7321 // Also "&opt = val", "$ENV = val" and "@r = val".
7322 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007323 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007324 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007325 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007326 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007327 char_u *var_end;
7328 int oplen;
7329 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007330
Bram Moolenaar65821722020-08-02 18:58:54 +02007331 if (ea.cmd[0] == '@')
7332 var_end = ea.cmd + 2;
7333 else
7334 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007335 FNE_CHECK_START | FNE_INCL_BR);
7336 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007337 if (oplen > 0)
7338 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007339 size_t len = p - ea.cmd;
7340
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007341 // Recognize an assignment if we recognize the variable
7342 // name:
7343 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007344 // "local = expr" where "local" is a local var.
7345 // "script = expr" where "script" is a script-local var.
7346 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007347 // "&opt = expr"
7348 // "$ENV = expr"
7349 // "@r = expr"
7350 if (*ea.cmd == '&'
7351 || *ea.cmd == '$'
7352 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007353 || ((len) > 2 && ea.cmd[1] == ':')
7354 || lookup_local(ea.cmd, len, &cctx) != NULL
7355 || lookup_arg(ea.cmd, len, NULL, NULL,
7356 NULL, &cctx) == OK
7357 || lookup_script(ea.cmd, len) == OK
7358 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007359 {
7360 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007361 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007362 goto erret;
7363 continue;
7364 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365 }
7366 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007367
7368 if (*ea.cmd == '[')
7369 {
7370 // [var, var] = expr
7371 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7372 if (line == NULL)
7373 goto erret;
7374 if (line != ea.cmd)
7375 continue;
7376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377 }
7378
7379 /*
7380 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007381 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007382 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007383 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007384 if (*cmd != '\'')
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007385 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007386 ea.cmd = skip_range(ea.cmd, NULL);
7387 if (ea.cmd > cmd && !starts_with_colon)
7388 {
7389 emsg(_(e_colon_required));
7390 goto erret;
7391 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007392 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007393 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007394 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007395 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007396
7397 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7398 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007399 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007400 {
7401 line += STRLEN(line);
7402 continue;
7403 }
7404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007405 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007406 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007407 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007408 // CMD_let cannot happen, compile_assignment() above is used
7409 iemsg("Command from find_ex_command() not handled");
7410 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007411 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007412 }
7413
7414 p = skipwhite(p);
7415
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007416 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02007417 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007418 && ea.cmdidx != CMD_elseif
7419 && ea.cmdidx != CMD_else
7420 && ea.cmdidx != CMD_endif)
7421 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007422 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007423 continue;
7424 }
7425
Bram Moolenaarefd88552020-06-18 20:50:10 +02007426 if (ea.cmdidx != CMD_elseif
7427 && ea.cmdidx != CMD_else
7428 && ea.cmdidx != CMD_endif
7429 && ea.cmdidx != CMD_endfor
7430 && ea.cmdidx != CMD_endwhile
7431 && ea.cmdidx != CMD_catch
7432 && ea.cmdidx != CMD_finally
7433 && ea.cmdidx != CMD_endtry)
7434 {
7435 if (cctx.ctx_had_return)
7436 {
7437 emsg(_("E1095: Unreachable code after :return"));
7438 goto erret;
7439 }
7440 }
7441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007442 switch (ea.cmdidx)
7443 {
7444 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007445 ea.arg = p;
7446 line = compile_nested_function(&ea, &cctx);
7447 break;
7448
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007450 emsg(_("E1086: Cannot use :function inside :def"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007451 goto erret;
7452
7453 case CMD_return:
7454 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007455 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456 break;
7457
7458 case CMD_let:
7459 case CMD_const:
7460 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007461 if (line == p)
7462 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007463 break;
7464
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007465 case CMD_unlet:
7466 case CMD_unlockvar:
7467 case CMD_lockvar:
7468 line = compile_unletlock(p, &ea, &cctx);
7469 break;
7470
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007471 case CMD_import:
7472 line = compile_import(p, &cctx);
7473 break;
7474
7475 case CMD_if:
7476 line = compile_if(p, &cctx);
7477 break;
7478 case CMD_elseif:
7479 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007480 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007481 break;
7482 case CMD_else:
7483 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007484 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007485 break;
7486 case CMD_endif:
7487 line = compile_endif(p, &cctx);
7488 break;
7489
7490 case CMD_while:
7491 line = compile_while(p, &cctx);
7492 break;
7493 case CMD_endwhile:
7494 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007495 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496 break;
7497
7498 case CMD_for:
7499 line = compile_for(p, &cctx);
7500 break;
7501 case CMD_endfor:
7502 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007503 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007504 break;
7505 case CMD_continue:
7506 line = compile_continue(p, &cctx);
7507 break;
7508 case CMD_break:
7509 line = compile_break(p, &cctx);
7510 break;
7511
7512 case CMD_try:
7513 line = compile_try(p, &cctx);
7514 break;
7515 case CMD_catch:
7516 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007517 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007518 break;
7519 case CMD_finally:
7520 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007521 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007522 break;
7523 case CMD_endtry:
7524 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007525 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007526 break;
7527 case CMD_throw:
7528 line = compile_throw(p, &cctx);
7529 break;
7530
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007531 case CMD_eval:
7532 if (compile_expr0(&p, &cctx) == FAIL)
7533 goto erret;
7534
7535 // drop the return value
7536 generate_instr_drop(&cctx, ISN_DROP, 1);
7537
7538 line = skipwhite(p);
7539 break;
7540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007541 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007542 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007543 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007544 case CMD_echomsg:
7545 case CMD_echoerr:
7546 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007547 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007548
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007549 // TODO: other commands with an expression argument
7550
Bram Moolenaarae616492020-07-28 20:07:27 +02007551 case CMD_append:
7552 case CMD_change:
7553 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007554 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007555 case CMD_xit:
7556 not_in_vim9(&ea);
7557 goto erret;
7558
Bram Moolenaar002262f2020-07-08 17:47:57 +02007559 case CMD_SIZE:
7560 semsg(_("E476: Invalid command: %s"), ea.cmd);
7561 goto erret;
7562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007563 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007564 // Not recognized, execute with do_cmdline_cmd().
7565 ea.arg = p;
7566 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007567 break;
7568 }
7569 if (line == NULL)
7570 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007571 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007572
7573 if (cctx.ctx_type_stack.ga_len < 0)
7574 {
7575 iemsg("Type stack underflow");
7576 goto erret;
7577 }
7578 }
7579
7580 if (cctx.ctx_scope != NULL)
7581 {
7582 if (cctx.ctx_scope->se_type == IF_SCOPE)
7583 emsg(_(e_endif));
7584 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7585 emsg(_(e_endwhile));
7586 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7587 emsg(_(e_endfor));
7588 else
7589 emsg(_("E1026: Missing }"));
7590 goto erret;
7591 }
7592
Bram Moolenaarefd88552020-06-18 20:50:10 +02007593 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007594 {
7595 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7596 {
7597 emsg(_("E1027: Missing return statement"));
7598 goto erret;
7599 }
7600
7601 // Return zero if there is no return at the end.
7602 generate_PUSHNR(&cctx, 0);
7603 generate_instr(&cctx, ISN_RETURN);
7604 }
7605
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007606 {
7607 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7608 + ufunc->uf_dfunc_idx;
7609 dfunc->df_deleted = FALSE;
7610 dfunc->df_instr = instr->ga_data;
7611 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007612 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007613 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007614 if (cctx.ctx_outer_used)
7615 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007616 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007618
7619 ret = OK;
7620
7621erret:
7622 if (ret == FAIL)
7623 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007624 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007625 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7626 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007627
7628 for (idx = 0; idx < instr->ga_len; ++idx)
7629 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007630 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007631
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007632 // If using the last entry in the table and it was added above, we
7633 // might as well remove it.
7634 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007635 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007636 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007637 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007638 ufunc->uf_dfunc_idx = 0;
7639 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007640 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007641
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007642 while (cctx.ctx_scope != NULL)
7643 drop_scope(&cctx);
7644
Bram Moolenaar20431c92020-03-20 18:39:46 +01007645 // Don't execute this function body.
7646 ga_clear_strings(&ufunc->uf_lines);
7647
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007648 if (errormsg != NULL)
7649 emsg(errormsg);
7650 else if (called_emsg == called_emsg_before)
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01007651 emsg(_("E1028: compile_def_function failed"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007652 }
7653
7654 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007655 if (do_estack_push)
7656 estack_pop();
7657
Bram Moolenaar20431c92020-03-20 18:39:46 +01007658 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007659 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007660 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007661 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007662}
7663
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007664 void
7665set_function_type(ufunc_T *ufunc)
7666{
7667 int varargs = ufunc->uf_va_name != NULL;
7668 int argcount = ufunc->uf_args.ga_len;
7669
7670 // Create a type for the function, with the return type and any
7671 // argument types.
7672 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7673 // The type is included in "tt_args".
7674 if (argcount > 0 || varargs)
7675 {
7676 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7677 argcount, &ufunc->uf_type_list);
7678 // Add argument types to the function type.
7679 if (func_type_add_arg_types(ufunc->uf_func_type,
7680 argcount + varargs,
7681 &ufunc->uf_type_list) == FAIL)
7682 return;
7683 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7684 ufunc->uf_func_type->tt_min_argcount =
7685 argcount - ufunc->uf_def_args.ga_len;
7686 if (ufunc->uf_arg_types == NULL)
7687 {
7688 int i;
7689
7690 // lambda does not have argument types.
7691 for (i = 0; i < argcount; ++i)
7692 ufunc->uf_func_type->tt_args[i] = &t_any;
7693 }
7694 else
7695 mch_memmove(ufunc->uf_func_type->tt_args,
7696 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7697 if (varargs)
7698 {
7699 ufunc->uf_func_type->tt_args[argcount] =
7700 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7701 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7702 }
7703 }
7704 else
7705 // No arguments, can use a predefined type.
7706 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7707 argcount, &ufunc->uf_type_list);
7708}
7709
7710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007711/*
7712 * Delete an instruction, free what it contains.
7713 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007714 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007715delete_instr(isn_T *isn)
7716{
7717 switch (isn->isn_type)
7718 {
7719 case ISN_EXEC:
7720 case ISN_LOADENV:
7721 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007722 case ISN_LOADB:
7723 case ISN_LOADW:
7724 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007725 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007726 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007727 case ISN_PUSHEXC:
7728 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007729 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007730 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007731 case ISN_STOREB:
7732 case ISN_STOREW:
7733 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007734 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007735 vim_free(isn->isn_arg.string);
7736 break;
7737
7738 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007739 case ISN_STORES:
7740 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007741 break;
7742
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007743 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007744 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007745 vim_free(isn->isn_arg.unlet.ul_name);
7746 break;
7747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007748 case ISN_STOREOPT:
7749 vim_free(isn->isn_arg.storeopt.so_name);
7750 break;
7751
7752 case ISN_PUSHBLOB: // push blob isn_arg.blob
7753 blob_unref(isn->isn_arg.blob);
7754 break;
7755
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007756 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007757#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007758 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007759#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007760 break;
7761
7762 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007763#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007764 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007765#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007766 break;
7767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007768 case ISN_UCALL:
7769 vim_free(isn->isn_arg.ufunc.cuf_name);
7770 break;
7771
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007772 case ISN_FUNCREF:
7773 {
7774 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7775 + isn->isn_arg.funcref.fr_func;
7776 func_ptr_unref(dfunc->df_ufunc);
7777 }
7778 break;
7779
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007780 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007781 {
7782 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7783 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7784
7785 if (ufunc != NULL)
7786 {
7787 // Clear uf_dfunc_idx so that the function is deleted.
7788 clear_def_function(ufunc);
7789 ufunc->uf_dfunc_idx = 0;
7790 func_ptr_unref(ufunc);
7791 }
7792
7793 vim_free(lambda);
7794 vim_free(isn->isn_arg.newfunc.nf_global);
7795 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007796 break;
7797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007798 case ISN_2BOOL:
7799 case ISN_2STRING:
7800 case ISN_ADDBLOB:
7801 case ISN_ADDLIST:
7802 case ISN_BCALL:
7803 case ISN_CATCH:
7804 case ISN_CHECKNR:
7805 case ISN_CHECKTYPE:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007806 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007807 case ISN_COMPAREANY:
7808 case ISN_COMPAREBLOB:
7809 case ISN_COMPAREBOOL:
7810 case ISN_COMPAREDICT:
7811 case ISN_COMPAREFLOAT:
7812 case ISN_COMPAREFUNC:
7813 case ISN_COMPARELIST:
7814 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815 case ISN_COMPARESPECIAL:
7816 case ISN_COMPARESTRING:
7817 case ISN_CONCAT:
7818 case ISN_DCALL:
Bram Moolenaar389df252020-07-09 21:20:47 +02007819 case ISN_SHUFFLE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007820 case ISN_DROP:
7821 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007822 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007823 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007824 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007825 case ISN_EXECCONCAT:
7826 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007827 case ISN_FOR:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007828 case ISN_LISTINDEX:
7829 case ISN_STRINDEX:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007830 case ISN_GETITEM:
Bram Moolenaar9af78762020-06-16 11:34:42 +02007831 case ISN_SLICE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007832 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007833 case ISN_JUMP:
7834 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007835 case ISN_LOADBDICT:
7836 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007837 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007838 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007839 case ISN_LOADSCRIPT:
7840 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007841 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007842 case ISN_LOADWDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007843 case ISN_NEGATENR:
7844 case ISN_NEWDICT:
7845 case ISN_NEWLIST:
7846 case ISN_OPNR:
7847 case ISN_OPFLOAT:
7848 case ISN_OPANY:
7849 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007850 case ISN_PCALL_END:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007851 case ISN_PUSHF:
7852 case ISN_PUSHNR:
7853 case ISN_PUSHBOOL:
7854 case ISN_PUSHSPEC:
7855 case ISN_RETURN:
7856 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02007857 case ISN_STOREOUTER:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007858 case ISN_STOREV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007859 case ISN_STORENR:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007860 case ISN_STOREREG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007861 case ISN_STORESCRIPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007862 case ISN_STOREDICT:
7863 case ISN_STORELIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007864 case ISN_THROW:
7865 case ISN_TRY:
7866 // nothing allocated
7867 break;
7868 }
7869}
7870
7871/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007872 * Free all instructions for "dfunc".
7873 */
7874 static void
7875delete_def_function_contents(dfunc_T *dfunc)
7876{
7877 int idx;
7878
7879 ga_clear(&dfunc->df_def_args_isn);
7880
7881 if (dfunc->df_instr != NULL)
7882 {
7883 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7884 delete_instr(dfunc->df_instr + idx);
7885 VIM_CLEAR(dfunc->df_instr);
7886 }
7887
7888 dfunc->df_deleted = TRUE;
7889}
7890
7891/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007892 * When a user function is deleted, clear the contents of any associated def
7893 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007894 */
7895 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007896clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007897{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007898 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007899 {
7900 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7901 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007902
Bram Moolenaar20431c92020-03-20 18:39:46 +01007903 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007904 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007905 }
7906}
7907
7908#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007909/*
7910 * Free all functions defined with ":def".
7911 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007912 void
7913free_def_functions(void)
7914{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007915 int idx;
7916
7917 for (idx = 0; idx < def_functions.ga_len; ++idx)
7918 {
7919 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7920
7921 delete_def_function_contents(dfunc);
7922 }
7923
7924 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007925}
7926#endif
7927
7928
7929#endif // FEAT_EVAL