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